UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 3.24 kB
{"version":3,"file":"option-group.mjs","names":[],"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"],"mappings":";;;;;;qCACE,mBAOK,MAAA;EAPgB,KAAI;EAAY,OAAK,eAAE,KAAA,GAAG,GAAE,SAAA,OAAA,CAAA;EAAA,GAC/C,mBAAqD,MAAA,EAAhD,OAAK,eAAE,KAAA,GAAG,GAAE,SAAA,QAAA,CAAA,EAAA,kBAAuB,KAAA,MAAK,EAAA,EAAA,EAC7C,mBAIK,MAAA,MAAA,CAHH,mBAEK,MAAA,EAFA,OAAK,eAAE,KAAA,GAAG,EAAC,QAAA,CAAA,EAAA,GACd,WAAQ,KAAA,QAAA,UAAA,CAAA,uBAJF,KAAA,QAAO,CAAA,CAAA"}