element-plus
Version:
A Component Library for Vue 3
1 lines • 3.65 kB
Source Map (JSON)
{"version":3,"file":"option-group.vue.mjs","sources":["../../../../../../packages/components/select/src/option-group.vue"],"sourcesContent":["<template>\n <ul v-show=\"visible\" ref=\"groupRef\" :class=\"ns.be('group', 'wrap')\">\n <li :class=\"ns.be('group', 'title')\">{{ label }}</li>\n <li>\n <ul :class=\"ns.b('group')\">\n <slot />\n </ul>\n </li>\n </ul>\n</template>\n\n<script lang=\"ts\">\nimport {\n computed,\n defineComponent,\n getCurrentInstance,\n isVNode,\n onMounted,\n provide,\n reactive,\n ref,\n toRefs,\n} from 'vue'\nimport { useMutationObserver } from '@vueuse/core'\nimport { ensureArray, isArray } from '@element-plus/utils'\nimport { useNamespace } from '@element-plus/hooks'\nimport { selectGroupKey } from './token'\n\nimport type { Component, VNode, VNodeArrayChildren } from 'vue'\nimport type { OptionInternalInstance, OptionPublicInstance } from './type'\n\nexport default defineComponent({\n name: 'ElOptionGroup',\n componentName: 'ElOptionGroup',\n\n props: {\n /**\n * @description name of the group\n */\n label: String,\n /**\n * @description whether to disable all options in this group\n */\n disabled: Boolean,\n },\n setup(props) {\n const ns = useNamespace('select')\n const groupRef = ref<HTMLElement>()\n const instance = getCurrentInstance()!\n const children = ref<OptionPublicInstance[]>([])\n\n provide(\n selectGroupKey,\n reactive({\n ...toRefs(props),\n })\n )\n\n const visible = computed(() =>\n children.value.some((option) => option.visible === true)\n )\n\n const isOption = (\n node: VNode\n ): node is VNode & { component: OptionInternalInstance } =>\n (node.type as Component).name === 'ElOption' && !!node.component?.proxy\n\n // get all instances of options\n const flattedChildren = (node: VNode | VNodeArrayChildren) => {\n const nodes = ensureArray(node) as VNode[] | VNodeArrayChildren\n const children: OptionPublicInstance[] = []\n\n nodes.forEach((child) => {\n if (!isVNode(child)) return\n\n if (isOption(child)) {\n children.push(child.component.proxy)\n } else if (isArray(child.children) && child.children.length) {\n children.push(...flattedChildren(child.children))\n } else if (child.component?.subTree) {\n children.push(...flattedChildren(child.component.subTree))\n }\n })\n\n return children\n }\n\n const updateChildren = () => {\n children.value = flattedChildren(instance.subTree)\n }\n\n onMounted(() => {\n updateChildren()\n })\n\n useMutationObserver(groupRef, updateChildren, {\n attributes: true,\n subtree: true,\n childList: true,\n })\n\n return {\n groupRef,\n visible,\n ns,\n }\n },\n})\n</script>\n"],"names":["_withDirectives","_openBlock","_createElementBlock","_normalizeClass","_createElementVNode","_renderSlot"],"mappings":";;;;;AACE,EAAA,OAAAA,cAAA,EAAAC,WAAA,EAAAC,kBAAA;AAAA,IAOK,IAAA;AAAA,IAAA;AAAA,MAPgB,GAAA,EAAI,UAAA;AAAA,MAAY,OAAKC,cAAA,CAAE,IAAA,CAAA,GAAG,EAAA,CAAE,OAAA,EAAA,MAAA,CAAA;AAAA,KAAA;AAAA;MAC/CC,kBAAA;AAAA,QAAqD,IAAA;AAAA,QAAA;AAAA,UAAhD,OAAKD,cAAA,CAAE,IAAA,CAAA,GAAG,EAAA,CAAE,OAAA,EAAA,OAAA,CAAA;AAAA,SAAA;AAAA,wBAAuB,KAAA,KAAK,CAAA;AAAA,QAAA;AAAA;AAAA,OAAA;AAAA,MAC7CC,kBAAA,CAIK,MAAA,IAAA,EAAA;AAAA,QAHHA,kBAAA;AAAA,UAEK,IAAA;AAAA,UAAA;AAAA,YAFA,OAAKD,cAAA,CAAE,IAAA,CAAA,EAAA,CAAG,CAAA,CAAC,OAAA,CAAA;AAAA,WAAA;AAAA;YACdE,UAAA,CAAQ,IAAA,CAAA,MAAA,EAAA,SAAA;AAAA,WAAA;AAAA;;;;;;;;YAJF,IAAA,CAAA,OAAO;AAAA,GAAA,CAAA;;;;;;"}