{"version":3,"file":"useOption.mjs","sources":["../../../../../../packages/components/select/src/useOption.ts"],"sourcesContent":["// @ts-nocheck\nimport { computed, getCurrentInstance, inject, toRaw, watch } from 'vue'\nimport { get } from 'lodash-unified'\nimport { ensureArray, escapeStringRegexp, isObject } from '@element-plus/utils'\nimport { selectGroupKey, selectKey } from './token'\n\nexport function useOption(props, states) {\n // inject\n const select = inject(selectKey)\n const selectGroup = inject(selectGroupKey, { disabled: false })\n\n // computed\n const itemSelected = computed(() => {\n return contains(ensureArray(select.props.modelValue), props.value)\n })\n\n const limitReached = computed(() => {\n if (select.props.multiple) {\n const modelValue = ensureArray(select.props.modelValue ?? [])\n return (\n !itemSelected.value &&\n modelValue.length >= select.props.multipleLimit &&\n select.props.multipleLimit > 0\n )\n } else {\n return false\n }\n })\n\n const currentLabel = computed(() => {\n return props.label || (isObject(props.value) ? '' : props.value)\n })\n\n const currentValue = computed(() => {\n return props.value || props.label || ''\n })\n\n const isDisabled = computed(() => {\n return props.disabled || states.groupDisabled || limitReached.value\n })\n\n const instance = getCurrentInstance()\n\n const contains = (arr = [], target) => {\n if (!isObject(props.value)) {\n return arr && arr.includes(target)\n } else {\n const valueKey = select.props.valueKey\n return (\n arr &&\n arr.some((item) => {\n return toRaw(get(item, valueKey)) === get(target, valueKey)\n })\n )\n }\n }\n\n const hoverItem = () => {\n if (!props.disabled && !selectGroup.disabled) {\n select.states.hoveringIndex = select.optionsArray.indexOf(instance.proxy)\n }\n }\n\n const updateOption = (query: string) => {\n const regexp = new RegExp(escapeStringRegexp(query), 'i')\n states.visible = regexp.test(currentLabel.value) || props.created\n }\n\n watch(\n () => currentLabel.value,\n () => {\n if (!props.created && !select.props.remote) select.setSelected()\n }\n )\n\n watch(\n () => props.value,\n (val, oldVal) => {\n const { remote, valueKey } = select.props\n\n if (val !== oldVal) {\n select.onOptionDestroy(oldVal, instance.proxy)\n select.onOptionCreate(instance.proxy)\n }\n\n if (!props.created && !remote) {\n if (\n valueKey &&\n isObject(val) &&\n isObject(oldVal) &&\n val[valueKey] === oldVal[valueKey]\n ) {\n return\n }\n select.setSelected()\n }\n }\n )\n\n watch(\n () => selectGroup.disabled,\n () => {\n states.groupDisabled = selectGroup.disabled\n },\n { immediate: true }\n )\n\n return {\n select,\n currentLabel,\n currentValue,\n itemSelected,\n isDisabled,\n hoverItem,\n updateOption,\n }\n}\n"],"names":["ensureArray"],"mappings":";;;;;;;AAIO,SAAS,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE;AACzC,EAAE,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;AACnC,EAAE,MAAM,WAAW,GAAG,MAAM,CAAC,cAAc,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC;AAClE,EAAE,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM;AACtC,IAAI,OAAO,QAAQ,CAACA,SAAW,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;AACvE,GAAG,CAAC,CAAC;AACL,EAAE,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM;AACtC,IAAI,IAAI,EAAE,CAAC;AACX,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE;AAC/B,MAAM,MAAM,UAAU,GAAGA,SAAW,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,UAAU,KAAK,IAAI,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;AACvF,MAAM,OAAO,CAAC,YAAY,CAAC,KAAK,IAAI,UAAU,CAAC,MAAM,IAAI,MAAM,CAAC,KAAK,CAAC,aAAa,IAAI,MAAM,CAAC,KAAK,CAAC,aAAa,GAAG,CAAC,CAAC;AACtH,KAAK,MAAM;AACX,MAAM,OAAO,KAAK,CAAC;AACnB,KAAK;AACL,GAAG,CAAC,CAAC;AACL,EAAE,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM;AACtC,IAAI,OAAO,KAA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