UNPKG

@fesjs/fes-design

Version:
59 lines (56 loc) 2.48 kB
import { defineComponent, inject, getCurrentInstance, reactive, toRefs, ref, provide, onBeforeMount, onBeforeUnmount, createVNode } from 'vue'; import _defineProperty from '@babel/runtime/helpers/esm/defineProperty'; import { SELECT_PROVIDE_KEY } from './const'; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } const selectGroupOptionProps = { label: { type: String, default: '' }, disabled: { type: Boolean, default: false } }; var groupOption = defineComponent({ name: 'FSelectGroupOption', props: selectGroupOptionProps, setup(props, ctx) { const parent = inject(SELECT_PROVIDE_KEY, null); if (!parent) { console.warn('[FSelectGroupOption]: FSelectGroupOption 必须搭配 FSelect 组件使用!'); } const instance = getCurrentInstance(); const { addOption, removeOption, parentGroupOption } = parent; const selectGroupOption = reactive(_objectSpread(_objectSpread({ id: instance.uid }, toRefs(props)), {}, { slots: ctx.slots, children: [] // 若 children 为数组类型,则自动判断 __isGroup 为 true })); const selectGroupOptionRef = ref(); provide(SELECT_PROVIDE_KEY, { addOption, removeOption, parentGroupOption: selectGroupOption }); onBeforeMount(() => { addOption(selectGroupOption, parentGroupOption); }); onBeforeUnmount(() => { removeOption(instance.uid, parentGroupOption); }); return () => { var _ctx$slots$default, _ctx$slots; return createVNode("span", { "ref": selectGroupOptionRef }, [(_ctx$slots$default = (_ctx$slots = ctx.slots).default) === null || _ctx$slots$default === void 0 ? void 0 : _ctx$slots$default.call(_ctx$slots)]); }; } }); export { groupOption as default, selectGroupOptionProps };