Skip to content

[WIP]fix:parent node not disabled when child nodes count greater than maxCount #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { TreeProps } from '@rc-component/tree';
import Tree from '@rc-component/tree';
import { UnstableContext } from '@rc-component/tree';
import type { EventDataNode, ScrollTo } from '@rc-component/tree/lib/interface';
import type { DataEntity, EventDataNode, ScrollTo } from '@rc-component/tree/lib/interface';
import KeyCode from '@rc-component/util/lib/KeyCode';
import useMemo from '@rc-component/util/lib/hooks/useMemo';
import * as React from 'react';
Expand Down Expand Up @@ -178,15 +178,45 @@
const isLeaf = (entity.children || []).length === 0;

if (!isLeaf) {
const checkableChildren = entity.children.filter(
childTreeNode =>
!childTreeNode.node.disabled &&
!childTreeNode.node.disableCheckbox &&
!checkedKeys.includes(childTreeNode.node[fieldNames.value]),
);

const checkableChildrenCount = checkableChildren.length;
disabledCache.set(value, checkableChildrenCount > leftMaxCount);
const visited = new Set<string>();
const stack: DataEntity<DataNode>[] = [entity];
let checkableCount = 0;

while (stack.length > 0) {
const currentEntity = stack.pop();
const currentValue = currentEntity.node[fieldNames.value];

if (visited.has(currentValue)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 visited 是在 getDisabledWithCache 里申明的,那每次进来都会是一个新的

continue;

Check warning on line 190 in src/OptionList.tsx

View check run for this annotation

Codecov / codecov/patch

src/OptionList.tsx#L190

Added line #L190 was not covered by tests
}
visited.add(currentValue);

const isCurrentLeaf = (currentEntity.children || []).length === 0;
const isDisabled =
currentEntity.node.disabled ||
currentEntity.node.disableCheckbox ||
checkedKeys.includes(currentValue);

if (isCurrentLeaf) {
if (!isDisabled) {
checkableCount++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实深度遍历的话可以考虑直接做个 dig function,然后计数器超过 maxCount 直接短路就行了。


// break early
if (checkableCount > leftMaxCount) {
disabledCache.set(value, true);
break;
}
}
continue;
}

if (!isDisabled) {
for (let i = currentEntity.children.length - 1; i >= 0; i--) {
stack.push(currentEntity.children[i]);
}
}
}
disabledCache.set(value, checkableCount > leftMaxCount);
} else {
disabledCache.set(value, false);
}
Expand Down