Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
103 changes: 43 additions & 60 deletions examples/mutiple-with-maxCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,62 @@ import React, { useState } from 'react';
import TreeSelect from '../src';

export default () => {
const [value, setValue] = useState<string[]>(['1']);
const [checkValue, setCheckValue] = useState<string[]>(['1']);
const [value, setValue] = useState<string[]>();
const [checkValue, setCheckValue] = useState<string[]>();

const treeData = [
{
key: '1',
value: '1',
title: '1',
title: 'Parent 1',
value: 'parent1',
children: [
{
key: '1-1',
value: '1-1',
title: '1-1',
title: 'Child 1-1',
value: 'child1-1',
},
{
key: '1-2',
value: '1-2',
title: '1-2',
disabled: true,
title: 'Child 1-2',
value: 'child1-2',
children: [
{
key: '1-2-1',
value: '1-2-1',
title: '1-2-1',
disabled: true,
title: 'Child 1-2-1',
value: 'child1-2-1',
children: [
{
title: 'child 1-2-1-1',
value: 'child1-2-1-1',
children: [
{
title: 'child 1-2-1-1-1',
value: 'child1-2-1-1-1',
},
],
},
{
title: 'child 1-2-1-2',
value: 'child1-2-1-2',
},
{
title: 'child 1-2-1-3',
value: 'child1-2-1-3',
},
],
},
{
key: '1-2-2',
value: '1-2-2',
title: '1-2-2',
title: 'Child 1-2-2',
value: 'child1-2-2',
},
{
title: 'Child 1-2-3',
value: 'child1-2-3',
},
{
title: 'Child 1-2-4',
value: 'child1-2-4',
},
],
},
{
key: '1-3',
value: '1-3',
title: '1-3',
},
],
},
{
key: '2',
value: '2',
title: '2',
},
{
key: '3',
value: '3',
title: '3',
},
{
key: '4',
value: '4',
title: '4',
},
];

const onChange = (val: string[]) => {
Expand All @@ -69,36 +70,18 @@ export default () => {

return (
<>
<h2>multiple with maxCount</h2>
<TreeSelect
style={{ width: 300 }}
fieldNames={{ value: 'value', label: 'title' }}
multiple
maxCount={3}
treeData={treeData}
/>
<h2>checkable with maxCount</h2>
<h2>maxCount = 3</h2>
<TreeSelect
style={{ width: 300 }}
treeCheckable
multiple
// showCheckedStrategy="SHOW_ALL"
// showCheckedStrategy="SHOW_PARENT"
maxCount={4}
maxCount={3}
treeData={treeData}
onChange={onChange}
value={value}
/>
<h2>checkable with maxCount and treeCheckStrictly</h2>
<TreeSelect
style={{ width: 300 }}
multiple
treeCheckable
treeCheckStrictly
maxCount={3}
treeData={treeData}
onChange={onCheckChange}
value={checkValue}
/>
</>
);
};
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 { 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,15 +178,45 @@ 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;
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;
}
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
Loading