From 6601f5e3e587d7d8a1b821701fcd99f1f3c8f372 Mon Sep 17 00:00:00 2001 From: chenxian Date: Mon, 25 Jul 2022 18:27:44 +0800 Subject: [PATCH] fix(Tree): fix select-all bug on parent node when treeCheckedStrategy set as "child", close #3984, #3936 --- src/tree-select/tree-select.jsx | 5 +---- src/tree/view/tree.jsx | 7 ++----- src/tree/view/util.js | 10 +++++++++- test/tree-select/index-spec.js | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/tree-select/tree-select.jsx b/src/tree-select/tree-select.jsx index 0c19c26b1d..f19680ee80 100644 --- a/src/tree-select/tree-select.jsx +++ b/src/tree-select/tree-select.jsx @@ -433,10 +433,7 @@ class TreeSelect extends Component { let keys = this.getKeysByValue(value); - if (treeCheckedStrategy !== 'child') { - // there's no need to calculate all checked value when treeCheckedStrategy=child, close #3936 - keys = getAllCheckedKeys(keys, this.state._k2n, this.state._p2n); - } + keys = getAllCheckedKeys(keys, this.state._k2n, this.state._p2n); switch (treeCheckedStrategy) { case 'parent': diff --git a/src/tree/view/tree.jsx b/src/tree/view/tree.jsx index 3e64921eba..64373d8b91 100644 --- a/src/tree/view/tree.jsx +++ b/src/tree/view/tree.jsx @@ -130,7 +130,7 @@ const getCheckedKeys = (props, willReceiveProps, _k2n, _p2n) => { checkedKeys = []; } - const { checkStrictly, checkedStrategy } = props; // TODO TEST + const { checkStrictly } = props; // TODO TEST if (checkStrictly) { if (isPlainObject(checkedKeys)) { const { checked, indeterminate } = checkedKeys; @@ -142,10 +142,7 @@ const getCheckedKeys = (props, willReceiveProps, _k2n, _p2n) => { checkedKeys = checkedKeys.filter(key => !!_k2n[key]); } else { - if (checkedStrategy !== 'child') { - // checkedStrategy 为 child 时不需要计算所有可选值 - checkedKeys = getAllCheckedKeys(checkedKeys, _k2n, _p2n); - } + checkedKeys = getAllCheckedKeys(checkedKeys, _k2n, _p2n); checkedKeys = checkedKeys.filter(key => !!_k2n[key]); diff --git a/src/tree/view/util.js b/src/tree/view/util.js index b2e52fd24b..f73ca02bd3 100644 --- a/src/tree/view/util.js +++ b/src/tree/view/util.js @@ -163,7 +163,15 @@ export function getAllCheckedKeys(checkedKeys, _k2n, _p2n) { const parent = _p2n[parentPos]; if (parent.checkable === false || parent.disabled || parent.checkboxDisabled) continue; const parentChecked = parent.children.every(child => isNodeChecked(child, flatKeys)); - if (parentChecked) { + + const isAllChildrenDisabled = parent.children.every(child => { + return child.disabled; + }); + + // don't auto select parent when all children are disabled, + // fix https://github.com/alibaba-fusion/next/issues/3936 + + if (parentChecked && !isAllChildrenDisabled) { parent.children.forEach(removeKey); addParentKey(i, parent); } else { diff --git a/test/tree-select/index-spec.js b/test/tree-select/index-spec.js index bdeabe3155..0a7360bf99 100644 --- a/test/tree-select/index-spec.js +++ b/test/tree-select/index-spec.js @@ -356,6 +356,22 @@ describe('TreeSelect', () => { assert.deepEqual(getLabels(wrapper), ['test1']); }); + it('should set parent node checked if all child nodes is checked even treeCheckedStrategy is "child"', () => { + wrapper = mount( + + ) + + assertChecked('3', true); + }) + it('should render parent tag when set treeCheckedStrategy to all', () => { wrapper = mount(