Files
element-plus/packages/components/select/src/options.ts
dopamine 8618a6bcd0 refactor: import shared utilities from @element-plus/utils (#18048)
chore: import shared utilities from `@element-plus/utils`
2024-08-29 09:04:03 +08:00

53 lines
1.5 KiB
TypeScript

import { defineComponent, inject } from 'vue'
import { isEqual } from 'lodash-unified'
import { isArray, isFunction, isString } from '@element-plus/utils'
import { selectKey } from './token'
import type { Component, VNode, VNodeNormalizedChildren } from 'vue'
export default defineComponent({
name: 'ElOptions',
setup(_, { slots }) {
const select = inject(selectKey)
let cachedValueList: any[] = []
return () => {
const children = slots.default?.()!
const valueList: any[] = []
function filterOptions(children?: VNodeNormalizedChildren) {
if (!isArray(children)) return
;(children as VNode[]).forEach((item) => {
const name = ((item?.type || {}) as Component)?.name
if (name === 'ElOptionGroup') {
filterOptions(
!isString(item.children) &&
!isArray(item.children) &&
isFunction(item.children?.default)
? item.children?.default()
: item.children
)
} else if (name === 'ElOption') {
valueList.push(item.props?.value)
} else if (isArray(item.children)) {
filterOptions(item.children)
}
})
}
if (children.length) {
filterOptions(children[0]?.children)
}
if (!isEqual(valueList, cachedValueList)) {
cachedValueList = valueList
if (select) {
select.states.optionValues = valueList
}
}
return children
}
},
})