Skip to content

Commit 839a09d

Browse files
authored
fix(components): [TreeSelect] incorrect props interaction and css paths (element-plus#7156)
* fix(components): [tree-select] `inheritAttrs` causes duplicate attributes * fix(components): [TreeSelect] css path incorrect * fix(components): [TreeSelect] `defaultExpandedKeys` contains `undefined` * fix(components): [TreeSelect] only expand on click node when the `check-strictly` is false and auto expand selected parent node * test(components): [TreeSelect] update render/filter/props unit test
1 parent 2cfaa1d commit 839a09d

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

packages/components/tree-select/__tests__/tree-select.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const createComponent = ({
3939
},
4040
],
4141
'onUpdate:modelValue': (val: string) => (value.value = val),
42+
renderAfterExpand: false,
4243
...props,
4344
}
4445
},
@@ -205,15 +206,15 @@ describe('TreeSelect.vue', () => {
205206
})
206207

207208
test('filter', async () => {
208-
const { select, tree } = createComponent({
209+
const { tree } = createComponent({
209210
props: {
210211
filterable: true,
211212
},
212213
})
213214

214-
select.vm.query = '一级 1'
215+
tree.vm.filter('一级 1')
215216
await nextTick()
216-
expect(tree.findAll('.el-tree-node').length).toBe(1)
217+
expect(tree.findAll('.el-tree-node:not(.is-hidden)').length).toBe(1)
217218
})
218219

219220
test('props', async () => {
@@ -240,7 +241,7 @@ describe('TreeSelect.vue', () => {
240241
})
241242

242243
await nextTick()
243-
expect(tree.find('.el-tree-node').text()).toBe('1')
244+
expect(tree.find('.el-select-dropdown__item').text()).toBe('1')
244245
wrapper.vm.modelValue = '2'
245246
await nextTick()
246247
expect(select.vm.selectedLabel).toBe('2')

packages/components/tree-select/src/tree-select.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { useTree } from './tree'
88
99
export default defineComponent({
1010
name: 'ElTreeSelect',
11+
// disable `ElSelect` inherit current attrs
12+
inheritAttrs: false,
1113
props: {
1214
...ElSelect.props,
1315
...ElTree.props,

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,25 @@ export const useTree = (
7070
...pick(toRefs(props), Object.keys(ElTree.props)),
7171
...attrs,
7272
nodeKey: key,
73-
defaultExpandedKeys: computed(() =>
74-
props.defaultExpandedKeys
75-
? props.defaultExpandedKeys.concat(props.modelValue)
76-
: toValidArray(props.modelValue)
77-
),
73+
74+
// only expand on click node when the `check-strictly` is false
75+
expandOnClickNode: computed(() => {
76+
return !props.checkStrictly
77+
}),
78+
79+
// auto expand selected parent node
80+
defaultExpandedKeys: computed(() => {
81+
const values = toValidArray(props.modelValue)
82+
const parentKeys = tree.value
83+
? values
84+
.map((item) => tree.value?.getNode(item)?.parent?.key)
85+
.filter((item) => isValidValue(item))
86+
: values
87+
return props.defaultExpandedKeys
88+
? props.defaultExpandedKeys.concat(parentKeys)
89+
: parentKeys
90+
}),
91+
7892
renderContent: (h, { node, data, store }) => {
7993
return h(
8094
TreeSelectOption,
@@ -131,6 +145,10 @@ export const useTree = (
131145
}
132146
}
133147

148+
function isValidValue(val: any) {
149+
return val || val === 0
150+
}
151+
134152
function toValidArray(val: any) {
135-
return Array.isArray(val) ? val : val || val === 0 ? [val] : []
153+
return Array.isArray(val) ? val : isValidValue(val) ? [val] : []
136154
}

0 commit comments

Comments
 (0)