Skip to content

Commit 3b37259

Browse files
committed
fix type
1 parent c4f2950 commit 3b37259

11 files changed

+71
-157
lines changed

src/LegacyContext.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import * as React from 'react';
22
import type { DataEntity, IconType } from 'rc-tree/lib/interface';
3-
import type { LegacyDataNode, SafeKey } from './interface';
3+
import type { LegacyDataNode, SafeKey, Key } from './interface';
44

55
interface LegacyContextProps {
66
checkable: boolean | React.ReactNode;
7-
checkedKeys: SafeKey[];
8-
halfCheckedKeys: SafeKey[];
9-
treeExpandedKeys: SafeKey[];
10-
treeDefaultExpandedKeys: SafeKey[];
11-
onTreeExpand: (keys: SafeKey[]) => void;
7+
checkedKeys: Key[];
8+
halfCheckedKeys: Key[];
9+
treeExpandedKeys: Key[];
10+
treeDefaultExpandedKeys: Key[];
11+
onTreeExpand: (keys: Key[]) => void;
1212
treeDefaultExpandAll: boolean;
1313
treeIcon: IconType;
1414
showTreeIcon: boolean;
1515
switcherIcon: IconType;
1616
treeLine: boolean;
1717
treeNodeFilterProp: string;
18-
treeLoadedKeys: SafeKey[];
18+
treeLoadedKeys: Key[];
1919
treeMotion: any;
2020
loadData: (treeNode: LegacyDataNode) => Promise<unknown>;
21-
onTreeLoad: (loadedKeys: SafeKey[]) => void;
21+
onTreeLoad: (loadedKeys: Key[]) => void;
2222

2323
keyEntities: Record<SafeKey, DataEntity<any>>;
2424
}

src/OptionList.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import useMemo from 'rc-util/lib/hooks/useMemo';
88
import * as React from 'react';
99
import LegacyContext from './LegacyContext';
1010
import TreeSelectContext from './TreeSelectContext';
11-
import type { SafeKey, TreeDataNode } from './interface';
11+
import type { Key, SafeKey } from './interface';
1212
import { getAllKeys, isCheckDisabled } from './utils/valueUtil';
1313

1414
const HIDDEN_STYLE = {
@@ -23,7 +23,7 @@ const HIDDEN_STYLE = {
2323
};
2424

2525
interface TreeEventInfo {
26-
node: { key: SafeKey };
26+
node: { key: Key };
2727
selected?: boolean;
2828
checked?: boolean;
2929
}
@@ -77,8 +77,8 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
7777
);
7878

7979
// ========================== Active ==========================
80-
const [activeKey, setActiveKey] = React.useState<SafeKey>(null);
81-
const activeEntity = keyEntities[activeKey];
80+
const [activeKey, setActiveKey] = React.useState<Key>(null);
81+
const activeEntity = keyEntities[activeKey as SafeKey];
8282

8383
// ========================== Values ==========================
8484
const mergedCheckedKeys = React.useMemo(() => {
@@ -112,8 +112,8 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
112112
};
113113

114114
// =========================== Keys ===========================
115-
const [expandedKeys, setExpandedKeys] = React.useState<SafeKey[]>(treeDefaultExpandedKeys);
116-
const [searchExpandedKeys, setSearchExpandedKeys] = React.useState<SafeKey[]>(null);
115+
const [expandedKeys, setExpandedKeys] = React.useState<Key[]>(treeDefaultExpandedKeys);
116+
const [searchExpandedKeys, setSearchExpandedKeys] = React.useState<Key[]>(null);
117117

118118
const mergedExpandedKeys = React.useMemo(() => {
119119
if (treeExpandedKeys) {
@@ -129,7 +129,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
129129
// eslint-disable-next-line react-hooks/exhaustive-deps
130130
}, [searchValue]);
131131

132-
const onInternalExpand = (keys: SafeKey[]) => {
132+
const onInternalExpand = (keys: Key[]) => {
133133
setExpandedKeys(keys);
134134
setSearchExpandedKeys(keys);
135135

@@ -143,7 +143,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
143143
event.preventDefault();
144144
};
145145

146-
const onInternalSelect = (__: SafeKey[], info: TreeEventInfo) => {
146+
const onInternalSelect = (__: Key[], info: TreeEventInfo) => {
147147
const { node } = info;
148148

149149
if (checkable && isCheckDisabled(node)) {
@@ -227,7 +227,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
227227
ref={treeRef}
228228
focusable={false}
229229
prefixCls={`${prefixCls}-tree`}
230-
treeData={memoTreeData as TreeDataNode[]}
230+
treeData={memoTreeData}
231231
height={listHeight}
232232
itemHeight={listItemHeight}
233233
itemScrollOffset={listItemScrollOffset}

src/TreeNode.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* istanbul ignore file */
22
import type * as React from 'react';
3-
import type { DataNode, SafeKey } from './interface';
3+
import type { DataNode, Key } from './interface';
44

55
export interface TreeNodeProps extends Omit<DataNode, 'children'> {
6-
value: SafeKey;
6+
value: Key;
77
children?: React.ReactNode;
88
}
99

src/TreeSelect.tsx

Lines changed: 19 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -28,72 +28,21 @@ import type { CheckedStrategy } from './utils/strategyUtil';
2828
import { formatStrategyValues, SHOW_ALL, SHOW_CHILD, SHOW_PARENT } from './utils/strategyUtil';
2929
import { fillFieldNames, isNil, toArray } from './utils/valueUtil';
3030
import warningProps from './utils/warningPropsUtil';
31-
import type { LabeledValueType, SafeKey, SelectSource, DefaultValueType } from './interface';
32-
33-
export type OnInternalSelect = (value: SafeKey, info: { selected: boolean }) => void;
34-
35-
/** @deprecated This is only used for legacy compatible. Not works on new code. */
36-
export interface LegacyCheckedNode {
37-
pos: string;
38-
node: React.ReactElement;
39-
children?: LegacyCheckedNode[];
40-
}
41-
42-
export interface ChangeEventExtra {
43-
/** @deprecated Please save prev value by control logic instead */
44-
preValue: LabeledValueType[];
45-
triggerValue: SafeKey;
46-
/** @deprecated Use `onSelect` or `onDeselect` instead. */
47-
selected?: boolean;
48-
/** @deprecated Use `onSelect` or `onDeselect` instead. */
49-
checked?: boolean;
50-
51-
// Not sure if exist user still use this. We have to keep but not recommend user to use
52-
/** @deprecated This prop not work as react node anymore. */
53-
triggerNode: React.ReactElement;
54-
/** @deprecated This prop not work as react node anymore. */
55-
allCheckedNodes: LegacyCheckedNode[];
56-
}
57-
58-
export interface FieldNames {
59-
value?: string;
60-
label?: string;
61-
children?: string;
62-
}
63-
64-
export interface InternalFieldName extends Omit<FieldNames, 'label'> {
65-
_title: string[];
66-
}
67-
68-
export interface SimpleModeConfig {
69-
id?: SafeKey;
70-
pId?: SafeKey;
71-
rootPId?: SafeKey;
72-
}
73-
74-
export interface BaseOptionType {
75-
disabled?: boolean;
76-
checkable?: boolean;
77-
disableCheckbox?: boolean;
78-
children?: BaseOptionType[];
79-
[name: string]: any;
80-
}
81-
82-
export interface DefaultOptionType extends BaseOptionType {
83-
value?: SafeKey;
84-
title?: React.ReactNode | ((data: DefaultOptionType) => React.ReactNode);
85-
label?: React.ReactNode;
86-
key?: SafeKey;
87-
children?: DefaultOptionType[];
88-
}
89-
90-
export interface LegacyDataNode extends DefaultOptionType {
91-
props: any;
92-
}
93-
export interface TreeSelectProps<
94-
ValueType = any,
95-
OptionType extends BaseOptionType = DefaultOptionType,
96-
> extends Omit<BaseSelectPropsWithoutPrivate, 'mode'> {
31+
import type {
32+
LabeledValueType,
33+
SafeKey,
34+
Key,
35+
DataNode,
36+
SimpleModeConfig,
37+
ChangeEventExtra,
38+
SelectSource,
39+
DefaultValueType,
40+
FieldNames,
41+
LegacyDataNode,
42+
} from './interface';
43+
44+
export interface TreeSelectProps<ValueType = any, OptionType extends BaseOptionType = DataNode>
45+
extends Omit<BaseSelectPropsWithoutPrivate, 'mode'> {
9746
prefixCls?: string;
9847
id?: string;
9948
children?: React.ReactNode;
@@ -109,7 +58,7 @@ export interface TreeSelectProps<
10958
inputValue?: string;
11059
onSearch?: (value: string) => void;
11160
autoClearSearchValue?: boolean;
112-
filterTreeNode?: boolean | ((inputValue: string, treeNode: DefaultOptionType) => boolean);
61+
filterTreeNode?: boolean | ((inputValue: string, treeNode: DataNode) => boolean);
11362
treeNodeFilterProp?: string;
11463

11564
// >>> Select
@@ -255,7 +204,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
255204
}
256205

257206
// ========================= FieldNames =========================
258-
const mergedFieldNames: InternalFieldName = React.useMemo(
207+
const mergedFieldNames: FieldNames = React.useMemo(
259208
() => fillFieldNames(fieldNames),
260209
/* eslint-disable react-hooks/exhaustive-deps */
261210
[JSON.stringify(fieldNames)],
@@ -310,7 +259,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
310259

311260
// =========================== Label ============================
312261
const getLabel = React.useCallback(
313-
(item: DefaultOptionType) => {
262+
(item: DataNode) => {
314263
if (item) {
315264
if (treeNodeLabelProp) {
316265
return item[treeNodeLabelProp];
@@ -760,7 +709,7 @@ if (process.env.NODE_ENV !== 'production') {
760709

761710
const GenericTreeSelect = TreeSelect as unknown as (<
762711
ValueType = any,
763-
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
712+
OptionType extends BaseOptionType | DataNode = DataNode,
764713
>(
765714
props: React.PropsWithChildren<TreeSelectProps<ValueType, OptionType>> & {
766715
ref?: React.Ref<BaseSelectRef>;

src/TreeSelectContext.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as React from 'react';
22
import type { ExpandAction } from 'rc-tree/lib/Tree';
3-
import type { DefaultOptionType, InternalFieldName, OnInternalSelect } from './TreeSelect';
3+
import type { DataNode, FieldNames, Key } from './interface';
44

55
export interface TreeSelectContextProps {
66
virtual?: boolean;
77
dropdownMatchSelectWidth?: boolean | number;
88
listHeight: number;
99
listItemHeight: number;
1010
listItemScrollOffset?: number;
11-
treeData: DefaultOptionType[];
12-
fieldNames: InternalFieldName;
13-
onSelect: OnInternalSelect;
11+
treeData: DataNode[];
12+
fieldNames: FieldNames;
13+
onSelect: (value: Key, info: { selected: boolean }) => void;
1414
treeExpandAction?: ExpandAction;
1515
treeTitleRender?: (node: any) => React.ReactNode;
1616
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;

src/hooks/useFilterTreeData.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import * as React from 'react';
2-
import type { DefaultOptionType, InternalFieldName, TreeSelectProps } from '../TreeSelect';
2+
import type { TreeSelectProps } from '../TreeSelect';
3+
import type { DataNode, FieldNames } from '../interface';
34
import { fillLegacyProps } from '../utils/legacyUtil';
45

56
type FilterFn = NonNullable<TreeSelectProps['filterTreeNode']>;
67

78
const useFilterTreeData = (
8-
treeData: DefaultOptionType[],
9+
treeData: DataNode[],
910
searchValue: string,
1011
options: {
11-
fieldNames: InternalFieldName;
12+
fieldNames: FieldNames;
1213
treeNodeFilterProp: string;
1314
filterTreeNode: TreeSelectProps['filterTreeNode'];
1415
},
@@ -27,8 +28,8 @@ const useFilterTreeData = (
2728
: (_, dataNode) =>
2829
String(dataNode[treeNodeFilterProp]).toUpperCase().includes(searchValue.toUpperCase());
2930

30-
const filterTreeNodes = (nodes: DefaultOptionType[], keepAll = false): DefaultOptionType[] =>
31-
nodes.reduce<DefaultOptionType[]>((filtered, node) => {
31+
const filterTreeNodes = (nodes: DataNode[], keepAll = false): DataNode[] =>
32+
nodes.reduce<DataNode[]>((filtered, node) => {
3233
const children = node[fieldChildren];
3334
const isMatch = keepAll || filterOptionFunc(searchValue, fillLegacyProps(node));
3435
const filteredChildren = filterTreeNodes(children || [], isMatch);

src/hooks/useTreeData.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as React from 'react';
22
import type { DataNode, SimpleModeConfig } from '../interface';
33
import { convertChildrenToData } from '../utils/legacyUtil';
4-
import type { DefaultOptionType } from '../TreeSelect';
54

65
function buildTreeStructure(nodes: DataNode[], config: SimpleModeConfig): DataNode[] {
76
const { id, pId, rootPId } = config;
@@ -37,7 +36,7 @@ export default function useTreeData(
3736
treeData: DataNode[],
3837
children: React.ReactNode,
3938
simpleMode: boolean | SimpleModeConfig,
40-
): DefaultOptionType[] {
39+
): DataNode[] {
4140
return React.useMemo(() => {
4241
if (treeData) {
4342
if (simpleMode) {

src/interface.ts

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type * as React from 'react';
2-
import type { SafeKey } from 'rc-tree/lib/interface';
2+
import type { SafeKey, Key, DataNode, FieldNames } from 'rc-tree/lib/interface';
33

4-
export type { SafeKey };
4+
export type { SafeKey, Key, DataNode, FieldNames };
55

66
export type SelectSource = 'option' | 'selection' | 'input' | 'clear';
77

88
export interface LabeledValueType {
9-
key?: SafeKey;
9+
key?: Key;
1010
value?: SafeKey;
1111
label?: React.ReactNode;
1212
/** Only works on `treeCheckStrictly` */
@@ -15,44 +15,13 @@ export interface LabeledValueType {
1515

1616
export type DefaultValueType = SafeKey | LabeledValueType | (SafeKey | LabeledValueType)[];
1717

18-
export interface DataNode {
19-
value?: SafeKey;
20-
title?: React.ReactNode | ((data: DataNode) => React.ReactNode);
21-
label?: React.ReactNode;
22-
key?: SafeKey;
23-
disabled?: boolean;
24-
disableCheckbox?: boolean;
25-
checkable?: boolean;
26-
children?: DataNode[];
27-
28-
/** Customize data info */
29-
[prop: string]: any;
30-
}
31-
32-
export interface InternalDataEntity {
33-
key: SafeKey;
34-
value: SafeKey;
35-
title?: React.ReactNode | ((data: InternalDataEntity) => React.ReactNode);
36-
disableCheckbox?: boolean;
37-
disabled?: boolean;
38-
children?: InternalDataEntity[];
39-
40-
/** Origin DataNode */
41-
node: DataNode;
42-
}
43-
4418
export interface LegacyDataNode extends DataNode {
4519
props: any;
4620
}
4721

48-
export interface TreeDataNode extends DataNode {
49-
key: SafeKey;
50-
children?: TreeDataNode[];
51-
}
52-
5322
export interface FlattenDataNode {
54-
data: InternalDataEntity;
55-
key: SafeKey;
23+
data: DataNode;
24+
key: Key;
5625
value: SafeKey;
5726
level: number;
5827
parent?: FlattenDataNode;
@@ -86,9 +55,3 @@ export interface ChangeEventExtra {
8655
/** @deprecated This prop not work as react node anymore. */
8756
allCheckedNodes: LegacyCheckedNode[];
8857
}
89-
90-
export interface FieldNames {
91-
value?: string;
92-
label?: string;
93-
children?: string;
94-
}

0 commit comments

Comments
 (0)