Skip to content

Commit

Permalink
Cascader: expose getCheckedNodes and fix options change bug (ElemeFE#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonaliaChen authored and oleksiikhr committed Jan 13, 2020
1 parent ca2c9cc commit 4b119e5
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 24 deletions.
11 changes: 11 additions & 0 deletions examples/docs/en-US/cascader.md
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,11 @@ You can customize the content of cascader node.
| visible-change | triggers when the dropdown appears/disappears | true when it appears, and false otherwise |
| remove-tag | triggers when remove tag in multiple selection mode | the value of the tag which is removed |

### Cascader Methods
| Method Name | Description | Parameters |
| ---- | ---- | ---- |
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |

### Cascader Slots
| Slot Name | Description |
|---------|-------------|
Expand All @@ -1955,6 +1960,12 @@ You can customize the content of cascader node.
| change | triggers when the binding value changes | value |
| expand-change | triggers when expand option changes | an array of the expanding node's parent nodes |

### CascaderPanel Methods
| Method Name | Description | Parameters |
| ---- | ---- | ---- |
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |
| clearCheckedNodes | clear checked nodes | - |

### CascaderPanel Slots
| Slot Name | Description |
|---------|-------------|
Expand Down
11 changes: 11 additions & 0 deletions examples/docs/es/cascader.md
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,11 @@ Puede personalizar el contenido del nodo de cascada.
| visible-change | se activa cuando aparece/desaparece el menú desplegable | verdadero cuando aparece, y falso de otra manera |
| remove-tag | se activa cuando se quita la etiqueta en modo de selección múltiple | el valor de la etiqueta que se quita |

### Cascader Methods
| Method Name | Description | Parameters |
| ---- | ---- | ---- |
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |

### Slots de Cascader
| Nombre del slot | Descripción |
|---------|-------------|
Expand All @@ -1958,6 +1963,12 @@ Puede personalizar el contenido del nodo de cascada.
| change | se desencadena cuando cambia el valor ligado. | valor |
| expand-change | se desencadena cuando las opciones expandidas cambian | un array de los nodos padres del nodo en expansión |

### CascaderPanel Methods
| Method Name | Description | Parameters |
| ---- | ---- | ---- |
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |
| clearCheckedNodes | clear checked nodes | - |

### Slots de CascaderPanel
| Nombre del slot | Descripción |
|---------|-------------|
Expand Down
11 changes: 11 additions & 0 deletions examples/docs/fr-FR/cascader.md
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,11 @@ Vous pouvez personnaliser le contenu du noeud cascader.
| visible-change | Se déclenche lorsque le menu déroulant apparaît / disparaît | vrai quand il apparaît, et faux sinon |
| 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 |

### Cascader Methods
| Method Name | Description | Parameters |
| ---- | ---- | ---- |
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |

### Cascader Slots
| Slot Name | Description |
|---------|-------------|
Expand All @@ -1955,6 +1960,12 @@ Vous pouvez personnaliser le contenu du noeud cascader.
| change | Se déclenche lorsque la valeur de liaison change | value |
| expand-change | Se déclenche lorsque l'option d'agrandissement change | an array of the expanding node's parent nodes |

### CascaderPanel Methods
| Method Name | Description | Parameters |
| ---- | ---- | ---- |
| getCheckedNodes | get an array of currently selected node | (leafOnly) whether only return the leaf checked nodes, default is `false` |
| clearCheckedNodes | clear checked nodes | - |

### CascaderPanel Slots
| Slot Name | Description |
|---------|-------------|
Expand Down
11 changes: 11 additions & 0 deletions examples/docs/zh-CN/cascader.md
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,11 @@
| visible-change | 下拉框出现/隐藏时触发 | 出现则为 true,隐藏则为 false |
| remove-tag | 在多选模式下,移除Tag时触发 | 移除的Tag对应的节点的值 |

### Cascader Methods
| 方法名 | 说明 | 参数 |
| ---- | ---- | ---- |
| getCheckedNodes | 获取选中的节点 | (leafOnly) 是否只是叶子节点,默认值为 `false` |

### Cascader Slots
| 名称 | 说明 |
|---------|-------------|
Expand All @@ -1934,6 +1939,12 @@
| change | 当选中节点变化时触发 | 选中节点的值 |
| expand-change | 当展开节点发生变化时触发 | 各父级选项值组成的数组 |

### CascaderPanel Methods
| 方法名 | 说明 | 参数 |
| ---- | ---- | ---- |
| getCheckedNodes | 获取选中的节点数组 | (leafOnly) 是否只是叶子节点,默认值为 `false` |
| clearCheckedNodes | 清空选中的节点 | - |

### CascaderPanel Slots
| 名称 | 说明 |
|---------|-------------|
Expand Down
39 changes: 22 additions & 17 deletions packages/cascader-panel/src/cascader-panel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,24 @@ export default {
});
},
syncActivePath() {
let { checkedValue, store, multiple } = this;
if (isEmpty(checkedValue)) {
const { store, multiple, activePath, checkedValue } = this;
if (!isEmpty(activePath)) {
const nodes = activePath.map(node => this.getNodeByValue(node.getValue()));
this.expandNodes(nodes);
} else if (!isEmpty(checkedValue)) {
const value = multiple ? checkedValue[0] : checkedValue;
const checkedNode = this.getNodeByValue(value) || {};
const nodes = (checkedNode.pathNodes || []).slice(0, -1);
this.expandNodes(nodes);
} else {
this.activePath = [];
this.menus = [store.getNodes()];
} else {
checkedValue = multiple ? checkedValue[0] : checkedValue;
const checkedNode = this.getNodeByValue(checkedValue) || {};
const nodes = [];
let { parent } = checkedNode;
while (parent) {
nodes.unshift(parent);
parent = parent.parent;
}
nodes.forEach(node => this.handleExpand(node, true /* silent */));
}
},
expandNodes(nodes) {
nodes.forEach(node => this.handleExpand(node, true /* silent */));
},
calculateCheckedNodePaths() {
const { checkedValue, multiple } = this;
const checkedValues = multiple
Expand Down Expand Up @@ -259,24 +261,26 @@ export default {
}
},
handleExpand(node, silent) {
const { activePath } = this;
const { level } = node;
const path = this.activePath.slice(0, level - 1);
const path = activePath.slice(0, level - 1);
const menus = this.menus.slice(0, level);
if (!node.isLeaf) {
path.push(node);
menus.push(node.children);
}
if (valueEquals(path, this.activePath)) return;
this.activePath = path;
this.menus = menus;
if (!silent) {
const pathValues = path.map(node => node.getValue());
this.$emit('active-item-change', pathValues); // Deprecated
this.$emit('expand-change', pathValues);
const activePathValues = activePath.map(node => node.getValue());
if (!valueEquals(pathValues, activePathValues)) {
this.$emit('active-item-change', pathValues); // Deprecated
this.$emit('expand-change', pathValues);
}
}
},
handleCheckChange(value) {
Expand Down Expand Up @@ -321,6 +325,7 @@ export default {
};
config.lazyLoad(node, resolve);
},
/**
* public methods
*/
Expand Down
22 changes: 15 additions & 7 deletions packages/cascader/src/cascader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,19 @@ export default {
}
},
checkedValue(val) {
const { value } = this;
const { value, dropDownVisible } = this;
const { checkStrictly, multiple } = this.config;
if (!isEqual(val, value) || isUndefined(value)) {
this.computePresentContent();
// hide dropdown when single mode
if (!multiple && !checkStrictly && dropDownVisible) {
this.toggleDropDownVisible(false);
}
this.$emit('input', val);
this.$emit('change', val);
this.dispatch('ElFormItem', 'el.form.change', [val]);
this.computePresentContent();
}
},
options: {
Expand Down Expand Up @@ -465,16 +472,13 @@ export default {
});
},
computePresentContent() {
// nextTick is required, because checked nodes may not change right now
this.$nextTick(() => {
const { multiple, checkStrictly } = this.config;
if (multiple) {
if (this.config.multiple) {
this.computePresentTags();
this.presentText = this.presentTags.length ? ' ' : null;
} else {
this.computePresentText();
if (!checkStrictly && this.dropDownVisible) {
this.toggleDropDownVisible(false);
}
}
});
},
Expand Down Expand Up @@ -630,6 +634,10 @@ export default {
this.updatePopper();
}
},
/**
* public methods
*/
getCheckedNodes(leafOnly) {
return this.panel.getCheckedNodes(leafOnly);
}
Expand Down

0 comments on commit 4b119e5

Please sign in to comment.