Skip to content

Commit c2e456c

Browse files
SimonaliaChenlzq4047
authored andcommitted
Cascader: expose getCheckedNodes and fix options change bug (ElemeFE#16709)
1 parent 8fb0394 commit c2e456c

6 files changed

Lines changed: 81 additions & 24 deletions

File tree

examples/docs/en-US/cascader.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,11 @@ You can customize the content of cascader node.
19361936
| visible-change | triggers when the dropdown appears/disappears | true when it appears, and false otherwise |
19371937
| remove-tag | triggers when remove tag in multiple selection mode | the value of the tag which is removed |
19381938

1939+
### Cascader Methods
1940+
| Method Name | Description | Parameters |
1941+
| ---- | ---- | ---- |
1942+
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |
1943+
19391944
### Cascader Slots
19401945
| Slot Name | Description |
19411946
|---------|-------------|
@@ -1955,6 +1960,12 @@ You can customize the content of cascader node.
19551960
| change | triggers when the binding value changes | value |
19561961
| expand-change | triggers when expand option changes | an array of the expanding node's parent nodes |
19571962

1963+
### CascaderPanel Methods
1964+
| Method Name | Description | Parameters |
1965+
| ---- | ---- | ---- |
1966+
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |
1967+
| clearCheckedNodes | clear checked nodes | - |
1968+
19581969
### CascaderPanel Slots
19591970
| Slot Name | Description |
19601971
|---------|-------------|

examples/docs/es/cascader.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,11 @@ Puede personalizar el contenido del nodo de cascada.
19391939
| visible-change | se activa cuando aparece/desaparece el menú desplegable | verdadero cuando aparece, y falso de otra manera |
19401940
| remove-tag | se activa cuando se quita la etiqueta en modo de selección múltiple | el valor de la etiqueta que se quita |
19411941

1942+
### Cascader Methods
1943+
| Method Name | Description | Parameters |
1944+
| ---- | ---- | ---- |
1945+
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |
1946+
19421947
### Slots de Cascader
19431948
| Nombre del slot | Descripción |
19441949
|---------|-------------|
@@ -1958,6 +1963,12 @@ Puede personalizar el contenido del nodo de cascada.
19581963
| change | se desencadena cuando cambia el valor ligado. | valor |
19591964
| expand-change | se desencadena cuando las opciones expandidas cambian | un array de los nodos padres del nodo en expansión |
19601965

1966+
### CascaderPanel Methods
1967+
| Method Name | Description | Parameters |
1968+
| ---- | ---- | ---- |
1969+
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |
1970+
| clearCheckedNodes | clear checked nodes | - |
1971+
19611972
### Slots de CascaderPanel
19621973
| Nombre del slot | Descripción |
19631974
|---------|-------------|

examples/docs/fr-FR/cascader.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,11 @@ Vous pouvez personnaliser le contenu du noeud cascader.
19361936
| visible-change | Se déclenche lorsque le menu déroulant apparaît / disparaît | vrai quand il apparaît, et faux sinon |
19371937
| remove-tag | Se déclenche lors de la suppression d'une balise en mode de sélection multiple | la valeur de la balise qui est supprimée |
19381938

1939+
### Cascader Methods
1940+
| Method Name | Description | Parameters |
1941+
| ---- | ---- | ---- |
1942+
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |
1943+
19391944
### Cascader Slots
19401945
| Slot Name | Description |
19411946
|---------|-------------|
@@ -1955,6 +1960,12 @@ Vous pouvez personnaliser le contenu du noeud cascader.
19551960
| change | Se déclenche lorsque la valeur de liaison change | value |
19561961
| expand-change | Se déclenche lorsque l'option d'agrandissement change | an array of the expanding node's parent nodes |
19571962

1963+
### CascaderPanel Methods
1964+
| Method Name | Description | Parameters |
1965+
| ---- | ---- | ---- |
1966+
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |
1967+
| clearCheckedNodes | clear checked nodes | - |
1968+
19581969
### CascaderPanel Slots
19591970
| Slot Name | Description |
19601971
|---------|-------------|

examples/docs/zh-CN/cascader.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,11 @@
19151915
| visible-change | 下拉框出现/隐藏时触发 | 出现则为 true,隐藏则为 false |
19161916
| remove-tag | 在多选模式下,移除Tag时触发 | 移除的Tag对应的节点的值 |
19171917

1918+
### Cascader Methods
1919+
| 方法名 | 说明 | 参数 |
1920+
| ---- | ---- | ---- |
1921+
| getCheckedNodes | 获取选中的节点 | (leafOnly) 是否只是叶子节点,默认值为 `false` |
1922+
19181923
### Cascader Slots
19191924
| 名称 | 说明 |
19201925
|---------|-------------|
@@ -1934,6 +1939,12 @@
19341939
| change | 当选中节点变化时触发 | 选中节点的值 |
19351940
| expand-change | 当展开节点发生变化时触发 | 各父级选项值组成的数组 |
19361941

1942+
### CascaderPanel Methods
1943+
| 方法名 | 说明 | 参数 |
1944+
| ---- | ---- | ---- |
1945+
| getCheckedNodes | 获取选中的节点数组 | (leafOnly) 是否只是叶子节点,默认值为 `false` |
1946+
| clearCheckedNodes | 清空选中的节点 | - |
1947+
19371948
### CascaderPanel Slots
19381949
| 名称 | 说明 |
19391950
|---------|-------------|

packages/cascader-panel/src/cascader-panel.vue

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,24 @@ export default {
195195
});
196196
},
197197
syncActivePath() {
198-
let { checkedValue, store, multiple } = this;
199-
if (isEmpty(checkedValue)) {
198+
const { store, multiple, activePath, checkedValue } = this;
199+
200+
if (!isEmpty(activePath)) {
201+
const nodes = activePath.map(node => this.getNodeByValue(node.getValue()));
202+
this.expandNodes(nodes);
203+
} else if (!isEmpty(checkedValue)) {
204+
const value = multiple ? checkedValue[0] : checkedValue;
205+
const checkedNode = this.getNodeByValue(value) || {};
206+
const nodes = (checkedNode.pathNodes || []).slice(0, -1);
207+
this.expandNodes(nodes);
208+
} else {
200209
this.activePath = [];
201210
this.menus = [store.getNodes()];
202-
} else {
203-
checkedValue = multiple ? checkedValue[0] : checkedValue;
204-
const checkedNode = this.getNodeByValue(checkedValue) || {};
205-
const nodes = [];
206-
let { parent } = checkedNode;
207-
while (parent) {
208-
nodes.unshift(parent);
209-
parent = parent.parent;
210-
}
211-
nodes.forEach(node => this.handleExpand(node, true /* silent */));
212211
}
213212
},
213+
expandNodes(nodes) {
214+
nodes.forEach(node => this.handleExpand(node, true /* silent */));
215+
},
214216
calculateCheckedNodePaths() {
215217
const { checkedValue, multiple } = this;
216218
const checkedValues = multiple
@@ -259,24 +261,26 @@ export default {
259261
}
260262
},
261263
handleExpand(node, silent) {
264+
const { activePath } = this;
262265
const { level } = node;
263-
const path = this.activePath.slice(0, level - 1);
266+
const path = activePath.slice(0, level - 1);
264267
const menus = this.menus.slice(0, level);
265268
266269
if (!node.isLeaf) {
267270
path.push(node);
268271
menus.push(node.children);
269272
}
270273
271-
if (valueEquals(path, this.activePath)) return;
272-
273274
this.activePath = path;
274275
this.menus = menus;
275276
276277
if (!silent) {
277278
const pathValues = path.map(node => node.getValue());
278-
this.$emit('active-item-change', pathValues); // Deprecated
279-
this.$emit('expand-change', pathValues);
279+
const activePathValues = activePath.map(node => node.getValue());
280+
if (!valueEquals(pathValues, activePathValues)) {
281+
this.$emit('active-item-change', pathValues); // Deprecated
282+
this.$emit('expand-change', pathValues);
283+
}
280284
}
281285
},
282286
handleCheckChange(value) {
@@ -321,6 +325,7 @@ export default {
321325
};
322326
config.lazyLoad(node, resolve);
323327
},
328+
324329
/**
325330
* public methods
326331
*/

packages/cascader/src/cascader.vue

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,19 @@ export default {
307307
}
308308
},
309309
checkedValue(val) {
310-
const { value } = this;
310+
const { value, dropDownVisible } = this;
311+
const { checkStrictly, multiple } = this.config;
312+
311313
if (!isEqual(val, value) || isUndefined(value)) {
314+
this.computePresentContent();
315+
// hide dropdown when single mode
316+
if (!multiple && !checkStrictly && dropDownVisible) {
317+
this.toggleDropDownVisible(false);
318+
}
319+
312320
this.$emit('input', val);
313321
this.$emit('change', val);
314322
this.dispatch('ElFormItem', 'el.form.change', [val]);
315-
this.computePresentContent();
316323
}
317324
},
318325
options: {
@@ -465,16 +472,13 @@ export default {
465472
});
466473
},
467474
computePresentContent() {
475+
// nextTick is required, because checked nodes may not change right now
468476
this.$nextTick(() => {
469-
const { multiple, checkStrictly } = this.config;
470-
if (multiple) {
477+
if (this.config.multiple) {
471478
this.computePresentTags();
472479
this.presentText = this.presentTags.length ? ' ' : null;
473480
} else {
474481
this.computePresentText();
475-
if (!checkStrictly && this.dropDownVisible) {
476-
this.toggleDropDownVisible(false);
477-
}
478482
}
479483
});
480484
},
@@ -630,6 +634,10 @@ export default {
630634
this.updatePopper();
631635
}
632636
},
637+
638+
/**
639+
* public methods
640+
*/
633641
getCheckedNodes(leafOnly) {
634642
return this.panel.getCheckedNodes(leafOnly);
635643
}

0 commit comments

Comments
 (0)