Skip to content

Commit 3daaa8d

Browse files
committed
chore: add warning
1 parent b14a46b commit 3daaa8d

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

src/OptionList.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
4949
treeTitleRender,
5050
onPopupScroll,
5151
leftMaxCount,
52-
showCheckedStrategy,
52+
leafCountOnly,
53+
valueEntities,
5354
} = React.useContext(TreeSelectContext);
5455

5556
const {
@@ -180,12 +181,16 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
180181
return false;
181182
}
182183

183-
console.log('--->', node);
184+
// console.log('--->', node);
184185

185186
if (leftMaxCount === null) {
186187
return false;
187188
}
188189

190+
if (!leafCountOnly && leftMaxCount <= 0) {
191+
return true;
192+
}
193+
189194
// const cacheKey = `${nodeValue}-${checkedKeys.join(',')}-${maxCount}`;
190195

191196
// // check cache

src/TreeSelect.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
226226
const mergedTreeData = useTreeData(treeData, children, treeDataSimpleMode);
227227

228228
const { keyEntities, valueEntities } = useDataEntities(mergedTreeData, mergedFieldNames);
229+
console.log('-->', valueEntities);
229230

230231
/** Get `missingRawValues` which not exist in the tree yet */
231232
const splitRawValues = React.useCallback(
@@ -630,7 +631,9 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
630631
treeTitleRender,
631632
onPopupScroll,
632633
leftMaxCount: maxCount ? maxCount - cachedDisplayValues.length : null,
633-
showCheckedStrategy: mergedShowCheckedStrategy,
634+
leafCountOnly:
635+
mergedShowCheckedStrategy === 'SHOW_CHILD' && !treeCheckStrictly && !!treeCheckable,
636+
valueEntities,
634637
};
635638
}, [
636639
virtual,
@@ -647,6 +650,9 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
647650
maxCount,
648651
cachedDisplayValues.length,
649652
mergedShowCheckedStrategy,
653+
treeCheckStrictly,
654+
treeCheckable,
655+
valueEntities,
650656
]);
651657

652658
// ======================= Legacy Context =======================

src/TreeSelectContext.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import type { ExpandAction } from 'rc-tree/lib/Tree';
33
import type { DataNode, FieldNames, Key } from './interface';
4-
import type { CheckedStrategy } from './utils/strategyUtil';
4+
import type useDataEntities from './hooks/useDataEntities';
55

66
export interface TreeSelectContextProps {
77
virtual?: boolean;
@@ -15,8 +15,12 @@ export interface TreeSelectContextProps {
1515
treeExpandAction?: ExpandAction;
1616
treeTitleRender?: (node: any) => React.ReactNode;
1717
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;
18-
leftMaxCount?: number | null;
19-
showCheckedStrategy?: CheckedStrategy;
18+
19+
// For `maxCount` usage
20+
leftMaxCount: number | null;
21+
/** When `true`, only take leaf node as count, or take all as count with `maxCount` limitation */
22+
leafCountOnly: boolean;
23+
valueEntities: ReturnType<typeof useDataEntities>['valueEntities'];
2024
}
2125

2226
const TreeSelectContext = React.createContext<TreeSelectContextProps>(null as any);

src/utils/warningPropsUtil.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ function warningProps(props: TreeSelectProps & { searchPlaceholder?: string }) {
1010
labelInValue,
1111
value,
1212
multiple,
13+
showCheckedStrategy,
14+
maxCount,
1315
} = props;
1416

1517
warning(!searchPlaceholder, '`searchPlaceholder` has been removed.');
@@ -20,7 +22,7 @@ function warningProps(props: TreeSelectProps & { searchPlaceholder?: string }) {
2022

2123
if (labelInValue || treeCheckStrictly) {
2224
warning(
23-
toArray(value).every((val) => val && typeof val === 'object' && 'value' in val),
25+
toArray(value).every(val => val && typeof val === 'object' && 'value' in val),
2426
'Invalid prop `value` supplied to `TreeSelect`. You should use { label: string, value: string | number } or [{ label: string, value: string | number }] instead.',
2527
);
2628
}
@@ -33,6 +35,13 @@ function warningProps(props: TreeSelectProps & { searchPlaceholder?: string }) {
3335
} else {
3436
warning(!Array.isArray(value), '`value` should not be array when `TreeSelect` is single mode.');
3537
}
38+
39+
if (maxCount && (showCheckedStrategy === 'SHOW_ALL' || showCheckedStrategy === 'SHOW_PARENT')) {
40+
warning(
41+
false,
42+
'`maxCount` not work with `showCheckedStrategy=SHOW_ALL` or `showCheckedStrategy=SHOW_PARENT`.',
43+
);
44+
}
3645
}
3746

3847
export default warningProps;

0 commit comments

Comments
 (0)