数通智联化工云平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
47 KiB

2 years ago
{"version":3,"file":"useSelect.mjs","sources":["../../../../../../packages/components/select-v2/src/useSelect.ts"],"sourcesContent":["// @ts-nocheck\nimport { computed, nextTick, onMounted, reactive, ref, watch } from 'vue'\nimport { isArray, isFunction, isObject } from '@vue/shared'\nimport { get, isEqual, isNil, debounce as lodashDebounce } from 'lodash-unified'\nimport { useResizeObserver } from '@vueuse/core'\nimport { useLocale, useNamespace } from '@element-plus/hooks'\nimport { CHANGE_EVENT, UPDATE_MODEL_EVENT } from '@element-plus/constants'\nimport {\n ValidateComponentsMap,\n debugWarn,\n escapeStringRegexp,\n} from '@element-plus/utils'\nimport { useFormItem, useFormSize } from '@element-plus/components/form'\n\nimport { ArrowUp } from '@element-plus/icons-vue'\nimport { useAllowCreate } from './useAllowCreate'\n\nimport { flattenOptions } from './util'\n\nimport { useInput } from './useInput'\nimport type ElTooltip from '@element-plus/components/tooltip'\nimport type { SelectProps } from './defaults'\nimport type { CSSProperties, ExtractPropTypes } from 'vue'\nimport type { Option, OptionType } from './select.types'\n\nconst DEFAULT_INPUT_PLACEHOLDER = ''\nconst MINIMUM_INPUT_WIDTH = 11\nconst TAG_BASE_WIDTH = {\n larget: 51,\n default: 42,\n small: 33,\n}\n\nconst useSelect = (props: ExtractPropTypes<typeof SelectProps>, emit) => {\n // inject\n const { t } = useLocale()\n const nsSelectV2 = useNamespace('select-v2')\n const nsInput = useNamespace('input')\n const { form: elForm, formItem: elFormItem } = useFormItem()\n\n const states = reactive({\n inputValue: DEFAULT_INPUT_PLACEHOLDER,\n displayInputValue: DEFAULT_INPUT_PLACEHOLDER,\n calculatedWidth: 0,\n cachedPlaceholder: '',\n cachedOptions: [] as Option[],\n createdOptions: [] as Option[],\n createdLabel: '',\n createdSelected: false,\n currentPlaceholder: '',\n hoveringIndex: -1,\n comboBoxHovering: false,\n isOnComposition: false,\n isSilentBlur: false,\n isComposing: false,\n inputLength: 20,\n selectWidth: 200,\n initialInputHeight: 0,\n previousQuery: null,\n previousValue: undefined,\n query: '',\n selectedLabel: '',\n softFocus: false,\n tagInMultiLine: false,\n })\n\n // data refs\n const selectedIndex = ref(-1)\n const popperSize = ref(-1)\n\n // DOM & Component refs\n const controlRef = ref(null)\n const inputRef = ref(null) // el-input ref\n const menuRef = ref(null)\n const popper = ref<InstanceType<typeof ElTooltip> | null>(null)\n const selectRef = ref(null)\n const selectionRef = ref(null) // tags ref\n const calculatorRef = ref<HTMLElement>(null)\n\n // the controller of the expanded popup\n const expanded = ref(false)\n\n const selectDisabled = computed(() => props.disabled || elForm?.disabled)\n\n const popupHeight = computed(() => {\n const totalHeight = filteredOptions.value.length * 34\n return totalHeight > props.height ? props.height : totalHeight\n })\n\n const hasModelValue = computed(() => {\n return !isNil(props.modelValue)\n })\n\n const showClearBtn = computed(() => {\n const hasValue = props.multiple\n ? Array.isArray(props.modelValue) && props.modelValue.length > 0\n : hasModelValue.value\n\n const criteria =\n props.clearable &&\n !selectDisabled.value &&\n states.comboBoxHovering &&\n hasValue\n return criteria\n })\n\n const iconComponent = computed(() =>\n props.remote && props.filterable ? '' : ArrowUp\n )\n\n const iconReverse = computed(\n () => iconComponent.value && nsSelectV2.is('reverse', expanded.value)\n )\n\n const validateState = computed(() => elFormItem?.validateState || '')\n const validateIcon = computed(\n () => ValidateComponentsMap[validateState.value]\n )\n\n const debounce = computed(() => (props.remote ? 300 : 0))\n\n // filteredOptions includes flatten the data into one dimensional array.\n const emptyText = computed(() => {\n const options = filteredOptions.value\n if (props.loading) {\n return props.loadingText || t