Files
element-plus/packages/components/tree-select/src/cache-options.ts
yujinpan 52ccdf6ed3 fix(components): [tree-select] props cacheData not reactive (#10885)
* fix(components): [tree-select] `cacheData` not reactive

* fix(components): [tree-select] cache options not overwrite the original
2022-12-05 12:49:06 +08:00

40 lines
984 B
TypeScript

import { defineComponent, inject, watch } from 'vue'
import { selectKey } from '@element-plus/components/select'
import type { SelectContext } from '@element-plus/components/select'
import type { PropType } from 'vue'
// same as el-option instance,
// these are required for `cachedOptions`
export type CacheOption = {
value: string | number | boolean | object
currentLabel: string | number
isDisabled: boolean
}
export default defineComponent({
props: {
data: {
type: Array as PropType<CacheOption[]>,
default: () => [],
},
},
setup(props) {
const select = inject(selectKey) as NonNullable<SelectContext>
watch(
() => props.data,
() => {
props.data.forEach((item) => {
if (!select.cachedOptions.has(item.value)) {
select.cachedOptions.set(item.value, item)
}
})
select.setSelected()
},
{ immediate: true, deep: true }
)
return () => undefined
},
})