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
9.4 KiB

{"ast":null,"code":"import { inject, computed, getCurrentInstance, toRaw, watch } from 'vue';\nimport { castArray, get } from 'lodash-unified';\nimport '../../../utils/index.mjs';\nimport { selectKey, selectGroupKey } from './token.mjs';\nimport { isObject } from '@vue/shared';\nimport { escapeStringRegexp } from '../../../utils/strings.mjs';\nfunction useOption(props, states) {\n const select = inject(selectKey);\n const selectGroup = inject(selectGroupKey, {\n disabled: false\n });\n const itemSelected = computed(() => {\n return contains(castArray(select.props.modelValue), props.value);\n });\n const limitReached = computed(() => {\n var _a;\n if (select.props.multiple) {\n const modelValue = castArray((_a = select.props.modelValue) != null ? _a : []);\n return !itemSelected.value && modelValue.length >= select.props.multipleLimit && select.props.multipleLimit > 0;\n } else {\n return false;\n }\n });\n const currentLabel = computed(() => {\n return props.label || (isObject(props.value) ? \"\" : props.value);\n });\n const currentValue = computed(() => {\n return props.value || props.label || \"\";\n });\n const isDisabled = computed(() => {\n return props.disabled || states.groupDisabled || limitReached.value;\n });\n const instance = getCurrentInstance();\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 arr && arr.some(item => {\n return toRaw(get(item, valueKey)) === get(target, valueKey);\n });\n }\n };\n const hoverItem = () => {\n if (!props.disabled && !selectGroup.disabled) {\n select.states.hoveringIndex = select.optionsArray.indexOf(instance.proxy);\n }\n };\n const updateOption = query => {\n const regexp = new RegExp(escapeStringRegexp(query), \"i\");\n states.visible = regexp.test(currentLabel.value) || props.created;\n };\n watch(() => currentLabel.value, () => {\n if (!props.created && !select.props.remote) select.setSelected();\n });\n watch(() => props.value, (val, oldVal) => {\n const {\n remote,\n valueKey\n } = select.props;\n if (val !== oldVal) {\n select.onOptionDestroy(oldVal, instance.proxy);\n select.onOptionCreate(instance.proxy);\n }\n if (!props.created && !remote) {\n if (valueKey && isObject(val) && isObject(oldVal) && val[valueKey] === oldVal[valueKey]) {\n return;\n }\n select.setSelected();\n }\n });\n watch(() => selectGroup.disabled, () => {\n states.groupDisabled = selectGroup.disabled;\n }, {\n immediate: true\n });\n return {\n select,\n currentLabel,\n currentValue,\n itemSelected,\n isDisabled,\n hoverItem,\n updateOption\n };\n}\nexport { useOption };","map":{"version":3,"names":["useOption","props","states","select","inject","selectKey","selectGroup","selectGroupKey","disabled","itemSelected","computed","contains","castArray","modelValue","value","limitReached","_a","multiple","length","multipleLimit","currentLabel","label","isObject","currentValue","isDisabled","groupDisabled","instance","getCurrentInstance","arr","target","includes","valueKey","some","item","toRaw","get","hoverItem","hoveringIndex","optionsArray","indexOf","proxy","updateOption","query","regexp","RegExp","escapeStringRegexp","visible","test","created","watch","remote","setSelected","val","oldVal","onOptionDestroy","onOptionCreate","immediate"],"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"],"mappings":";;;;;;AAIO,SAASA,SAASA,CAACC,KAAK,EAAEC,MAAM,EAAE;EACvC,MAAMC,MAAM,GAAGC,MAAM,CAACC,SAAS,CAAC;EAChC,MAAMC,WAAW,GAAGF,MAAM,CAACG,cAAc,EAAE;IAAEC,QAAQ,EAAE;EAAK,CAAE,CAAC;EAC/D,MAAMC,YAAY,GAAGC,QAAQ,CAAC,MAAM;IAClC,OAAOC,QAAQ,CAACC,SAAW,CAACT,MAAM,CAACF,KAAK,CAACY,UAAU,CAAC,EAAEZ,KAAK,CAACa,KAAK,CAAC;EACtE,CAAG,CAAC;EACF,MAAMC,YAAY,GAAGL,QAAQ,CAAC,MAAM;IAClC,IAAIM,EAAE;IACN,IAAIb,MAAM,CAACF,KAAK,CAACgB,QAAQ,EAAE;MACzB,MAAMJ,UAAU,G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