fix(components): [select] Filter duplicate emits of "update options" (#11884)

* fix(components): [select] Filter duplicate emits of "update options"

* perf(components): [select] Code standardization
This commit is contained in:
Travis
2023-03-08 15:13:15 +08:00
committed by GitHub
parent a83fd9b6d6
commit 8d752c74b9

View File

@@ -6,6 +6,18 @@ export default defineComponent({
name: 'ElOptions',
emits: ['update-options'],
setup(_, { slots, emit }) {
let cachedOptions: any[] = []
function isSameOptions(a: any[], b: any[]) {
if (a.length !== b.length) return false
for (const [index] of a.entries()) {
if (a[index] != b[index]) {
return false
}
}
return true
}
return () => {
const children = slots.default?.()!
@@ -19,7 +31,10 @@ export default defineComponent({
)
.map((item: VNode) => item.props?.label)
emit('update-options', filteredOptions)
if (!isSameOptions(filteredOptions, cachedOptions)) {
cachedOptions = filteredOptions
emit('update-options', filteredOptions)
}
}
}