UNPKG

@vuesax-alpha/nightly

Version:
143 lines (140 loc) 4.79 kB
import { defineComponent, toRef, inject, ref, computed, reactive, onBeforeUnmount, watch, openBlock, createElementBlock, normalizeClass, renderSlot, createTextVNode, toDisplayString } from 'vue'; import { isObject } from 'lodash-unified'; import '../../../hooks/index.mjs'; import '../../../utils/index.mjs'; import { selectContextKey, selectRegisterKey, optionGroupRegisterKey, optionGroupContextKey } from './tokens.mjs'; import { optionProps } from './option.mjs'; import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs'; import { useNamespace } from '../../../hooks/use-namespace/index.mjs'; import { throwError } from '../../../utils/error.mjs'; import { escapeStringRegexp } from '../../../utils/strings.mjs'; const __default__ = defineComponent({ name: "VsOption" }); const _sfc_main = defineComponent({ ...__default__, props: optionProps, setup(__props) { const props = __props; const ns = useNamespace("select"); const value = toRef(props, "value"); const select = inject(selectContextKey); const selectRegister = inject(selectRegisterKey); const optionGroupRegister = inject(optionGroupRegisterKey, void 0); if (!select || !selectRegister) { throwError( "Select Option", "`Option` component must be called inside `select` or `option-group` component" ); } const selectGroup = inject(optionGroupContextKey, { disabled: false }); const el = ref(); const isObject$1 = computed(() => isObject(props.value)); const limitReached = computed(() => { if (select.multiple && select.multipleLimit) { const modelValue = select.selectedArray; return !isSelected.value && modelValue.length >= select.multipleLimit && select.multipleLimit > 0; } return false; }); const currentLabel = computed(() => { return props.label || (isObject$1.value ? "" : `${props.value}`); }); const isDisabled = computed(() => { return props.disabled || states.groupDisabled || limitReached.value; }); const isSelected = computed(() => { return select.selectedArray.some((e) => e.value == states.value); }); const states = reactive({ index: -1, el, value, currentLabel, isDisabled, groupDisabled: false, visible: true, hit: false, hover: false, created: props.created }); const { unregister, updateOption } = selectRegister(states); const { unregister: optionGroupUnregister } = (optionGroupRegister == null ? void 0 : optionGroupRegister(states)) || {}; onBeforeUnmount(() => { unregister(); optionGroupUnregister == null ? void 0 : optionGroupUnregister(); }); const hoverItem = () => { if (!props.disabled && !selectGroup.disabled) { select.hoverIndex = select.optionsArray.indexOf(states); } }; watch(currentLabel, () => { if (!props.created) select.setSelected(); }); watch( () => props.value, (val, oldVal) => { if (!Object.is(val, oldVal)) { updateOption(states); } if (!props.created) select.setSelected(); } ); watch( () => selectGroup.disabled, (val) => { states.groupDisabled = val; }, { immediate: true } ); watch( () => select.queryChange, (query) => { const regexp = new RegExp(escapeStringRegexp(`${query}`), "i"); states.visible = regexp.test(`${currentLabel.value}`) || props.created; if (!states.visible) { select.states.filteredOptionsCount--; } } ); const optionKls = computed(() => [ ns.e("option"), ns.is("hover", states.hover), ns.is("active", isSelected.value), ns.is("disabled", isDisabled.value), ns.is("hidden", !states.visible) ]); const selectOptionClick = () => { if (props.disabled !== true && states.groupDisabled !== true) { select.handleOptionSelect(states, true); } }; return (_ctx, _cache) => { return openBlock(), createElementBlock( "button", { ref_key: "el", ref: el, class: normalizeClass(optionKls.value), onMouseenter: hoverItem, onClick: selectOptionClick }, [ renderSlot(_ctx.$slots, "default", {}, () => [ createTextVNode( toDisplayString(currentLabel.value), 1 ) ]) ], 34 ); }; } }); var Option = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/runner/work/vuesax-alpha/vuesax-alpha/packages/components/select/src/option.vue"]]); export { Option as default }; //# sourceMappingURL=option2.mjs.map