Files
element-plus/packages/components/select/src/options.ts
dopamine 38cbfbb0b0 chore: [select] remove @ts-nocheck directive (#19098)
* chore: [select] remove @ts-nocheck directive

* Update packages/components/select/src/option.vue

Co-authored-by: qiang <qw13131wang@gmail.com>

* chore: delete useless type

* Update packages/components/select/src/useSelect.ts

Co-authored-by: qiang <qw13131wang@gmail.com>

* fix: type error

* fix: type error

* chore: use buildProps

* chore: add type

* Update packages/components/select/src/useSelect.ts

Co-authored-by: btea <2356281422@qq.com>

* chore: fix type

* chore: fix type

* chore: fix types

---------

Co-authored-by: warmthsea <2586244885@qq.com>
Co-authored-by: sea <45450994+warmthsea@users.noreply.github.com>
Co-authored-by: qiang <qw13131wang@gmail.com>
Co-authored-by: btea <2356281422@qq.com>
2025-03-29 17:58:52 +08:00

55 lines
1.6 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'
import type { OptionValue } from './type'
export default defineComponent({
name: 'ElOptions',
setup(_, { slots }) {
const select = inject(selectKey)
let cachedValueList: OptionValue[] = []
return () => {
const children = slots.default?.()!
const valueList: OptionValue[] = []
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
}
},
})