Skip to content

Commit 52ccdf6

Browse files
authored
fix(components): [tree-select] props cacheData not reactive (element-plus#10885)
* fix(components): [tree-select] `cacheData` not reactive * fix(components): [tree-select] cache options not overwrite the original
1 parent 9694d04 commit 52ccdf6

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

packages/components/tree-select/__tests__/tree-select.test.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { nextTick, ref } from 'vue'
1+
import { nextTick, reactive, ref } from 'vue'
22
import { mount } from '@vue/test-utils'
33
import { describe, expect, test, vi } from 'vitest'
44
import TreeSelect from '../src/tree-select.vue'
@@ -453,6 +453,7 @@ describe('TreeSelect.vue', () => {
453453

454454
test('show correct label when lazy load', async () => {
455455
const modelValue = ref<number>(1)
456+
const cacheData = reactive([{ value: 3, label: '3-label' }])
456457
const { select } = createComponent({
457458
props: {
458459
data: [],
@@ -461,7 +462,7 @@ describe('TreeSelect.vue', () => {
461462
load: (node: object, resolve: (p: any) => any[]) => {
462463
resolve([{ value: 2, label: '2-label', isLeaf: true }])
463464
},
464-
cacheData: [{ value: 3, label: '3-label' }],
465+
cacheData,
465466
},
466467
})
467468

@@ -473,5 +474,15 @@ describe('TreeSelect.vue', () => {
473474
modelValue.value = 3
474475
await nextTick()
475476
expect(select.vm.selectedLabel).toBe('3-label')
477+
478+
// no load & set cache lazy will be correct label
479+
modelValue.value = 1
480+
await nextTick()
481+
cacheData.push({
482+
value: 1,
483+
label: '1-label',
484+
})
485+
await nextTick()
486+
expect(select.vm.selectedLabel).toBe('1-label')
476487
})
477488
})

packages/components/tree-select/src/cache-options.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, inject } from 'vue'
1+
import { defineComponent, inject, watch } from 'vue'
22
import { selectKey } from '@element-plus/components/select'
33
import type { SelectContext } from '@element-plus/components/select'
44
import type { PropType } from 'vue'
@@ -21,7 +21,18 @@ export default defineComponent({
2121
setup(props) {
2222
const select = inject(selectKey) as NonNullable<SelectContext>
2323

24-
props.data.forEach((item) => select.cachedOptions.set(item.value, item))
24+
watch(
25+
() => props.data,
26+
() => {
27+
props.data.forEach((item) => {
28+
if (!select.cachedOptions.has(item.value)) {
29+
select.cachedOptions.set(item.value, item)
30+
}
31+
})
32+
select.setSelected()
33+
},
34+
{ immediate: true, deep: true }
35+
)
2536

2637
return () => undefined
2738
},

0 commit comments

Comments
 (0)