Skip to content
Open
Changes from 1 commit
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
35 changes: 26 additions & 9 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { RefOptionListProps } from '@rc-component/select/lib/OptionList';
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,14 +178,31 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
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;
const getLeafCheckableCount = (children: DataEntity<DataNode>[]): number => {
Copy link
Member

Choose a reason for hiding this comment

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

这样会有性能问题,最好是收集可见区域的节点,通过染色来做标记。避免递归产生大量的性能损耗。

Copy link
Author

Choose a reason for hiding this comment

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

那是不是可以通过栈+染色的形式来进行一个优化

return children.reduce((count, current) => {
const currentValue = current.node[fieldNames.value];
const currentEntity = valueEntities.get(currentValue);
const isCurrentLeaf = (currentEntity.children || []).length === 0;

if (isCurrentLeaf) {
if (
!current.node.disabled &&
!current.node.disableCheckbox &&
!checkedKeys.includes(currentValue)
) {
return count + 1;
}
} else if (
!current.node.disabled &&
!current.node.disableCheckbox &&
!checkedKeys.includes(currentValue)
) {
return count + getLeafCheckableCount(currentEntity.children);
}
return count;
}, 0);
};
const checkableChildrenCount = getLeafCheckableCount(entity.children);
disabledCache.set(value, checkableChildrenCount > leftMaxCount);
} else {
disabledCache.set(value, false);
Expand Down