Skip to content

Commit

Permalink
fix(Tree): fix select-all bug on parent node when treeCheckedStrategy…
Browse files Browse the repository at this point in the history
… set as "child", close #3984, #3936
  • Loading branch information
lakerswgq committed Jul 25, 2022
1 parent b1930b5 commit 6601f5e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/tree-select/tree-select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
7 changes: 2 additions & 5 deletions src/tree/view/tree.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);

Expand Down
10 changes: 9 additions & 1 deletion src/tree/view/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions test/tree-select/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<TreeSelect
defaultVisible
treeCheckable
treeDefaultExpandAll
dataSource={dataSource}
style={{ width: 200 }}
treeCheckedStrategy="child"
value={['6']}
/>
)

assertChecked('3', true);
})

it('should render parent tag when set treeCheckedStrategy to all', () => {
wrapper = mount(
<TreeSelect
Expand Down

0 comments on commit 6601f5e

Please sign in to comment.