-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathgenerate.tsx
More file actions
635 lines (564 loc) · 19.2 KB
/
Copy pathgenerate.tsx
File metadata and controls
635 lines (564 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import * as React from 'react';
import { useMemo } from 'react';
import type { SelectProps, RefSelectProps, GenerateConfig } from 'rc-select/lib/generate';
import generateSelector from 'rc-select/lib/generate';
import { getLabeledValue } from 'rc-select/lib/utils/valueUtil';
import { convertDataToEntities } from 'rc-tree/lib/utils/treeUtil';
import { conductCheck } from 'rc-tree/lib/utils/conductUtil';
import type { IconType } from 'rc-tree/lib/interface';
import omit from 'rc-util/lib/omit';
import type { FilterFunc } from 'rc-select/lib/interface/generator';
import { INTERNAL_PROPS_MARK } from 'rc-select/lib/interface/generator';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import warning from 'rc-util/lib/warning';
import TreeNode from './TreeNode';
import type {
Key,
DefaultValueType,
DataNode,
LabelValueType,
SimpleModeConfig,
RawValueType,
ChangeEventExtra,
LegacyDataNode,
SelectSource,
FlattenDataNode,
FieldNames,
} from './interface';
import {
flattenOptions,
filterOptions,
isValueDisabled,
findValueOption,
addValue,
removeValue,
getRawValueLabeled,
toArray,
fillFieldNames,
} from './utils/valueUtil';
import warningProps from './utils/warningPropsUtil';
import { SelectContext } from './Context';
import useTreeData from './hooks/useTreeData';
import useKeyValueMap from './hooks/useKeyValueMap';
import useKeyValueMapping from './hooks/useKeyValueMapping';
import type { CheckedStrategy } from './utils/strategyUtil';
import { formatStrategyKeys, SHOW_ALL, SHOW_PARENT, SHOW_CHILD } from './utils/strategyUtil';
import { fillAdditionalInfo } from './utils/legacyUtil';
import useSelectValues from './hooks/useSelectValues';
const OMIT_PROPS: (keyof TreeSelectProps)[] = [
'expandedKeys' as any,
'treeData',
'treeCheckable',
'showCheckedStrategy',
'searchPlaceholder',
'treeLine',
'treeIcon',
'showTreeIcon',
'switcherIcon',
'treeNodeFilterProp',
'filterTreeNode',
'dropdownPopupAlign',
'treeDefaultExpandAll',
'treeCheckStrictly',
'treeExpandedKeys',
'treeLoadedKeys',
'treeMotion',
'onTreeExpand',
'onTreeLoad',
'labelRender',
'loadData',
'treeDataSimpleMode',
'treeNodeLabelProp',
'treeDefaultExpandedKeys',
];
export interface TreeSelectProps<ValueType = DefaultValueType>
extends Omit<
SelectProps<DataNode[], ValueType>,
| 'onChange'
| 'mode'
| 'menuItemSelectedIcon'
| 'dropdownAlign'
| 'backfill'
| 'getInputElement'
| 'optionLabelProp'
| 'tokenSeparators'
| 'filterOption'
| 'fieldNames'
> {
multiple?: boolean;
showArrow?: boolean;
showSearch?: boolean;
open?: boolean;
defaultOpen?: boolean;
value?: ValueType;
defaultValue?: ValueType;
disabled?: boolean;
placeholder?: React.ReactNode;
/** @deprecated Use `searchValue` instead */
inputValue?: string;
searchValue?: string;
autoClearSearchValue?: boolean;
maxTagPlaceholder?: (omittedValues: LabelValueType[]) => React.ReactNode;
fieldNames?: FieldNames;
loadData?: (dataNode: LegacyDataNode) => Promise<unknown>;
treeNodeFilterProp?: string;
treeNodeLabelProp?: string;
treeDataSimpleMode?: boolean | SimpleModeConfig;
treeExpandedKeys?: Key[];
treeDefaultExpandedKeys?: Key[];
treeLoadedKeys?: Key[];
treeCheckable?: boolean | React.ReactNode;
treeCheckStrictly?: boolean;
showCheckedStrategy?: CheckedStrategy;
treeDefaultExpandAll?: boolean;
treeData?: DataNode[];
treeLine?: boolean;
treeIcon?: IconType;
showTreeIcon?: boolean;
switcherIcon?: IconType;
treeMotion?: any;
children?: React.ReactNode;
filterTreeNode?: boolean | FilterFunc<LegacyDataNode>;
dropdownPopupAlign?: any;
// Event
onSearch?: (value: string) => void;
onChange?: (value: ValueType, labelList: React.ReactNode[], extra: ChangeEventExtra) => void;
onTreeExpand?: (expandedKeys: Key[]) => void;
onTreeLoad?: (loadedKeys: Key[]) => void;
onDropdownVisibleChange?: (open: boolean) => void;
// Legacy
/** `searchPlaceholder` has been removed since search box has been merged into input box */
searchPlaceholder?: React.ReactNode;
/** @private This is not standard API since we only used in `rc-cascader`. Do not use in your production */
labelRender?: (entity: FlattenDataNode, value: RawValueType) => React.ReactNode;
}
export default function generate(config: {
prefixCls: string;
optionList: GenerateConfig<any>['components']['optionList'];
}) {
const { prefixCls, optionList } = config;
const RefSelect = generateSelector<DataNode[]>({
prefixCls,
components: {
optionList,
},
// Not use generate since we will handle ourself
convertChildrenToData: () => null,
flattenOptions,
// Handle `optionLabelProp` in TreeSelect component
getLabeledValue: getLabeledValue as any,
filterOptions,
isValueDisabled,
findValueOption,
omitDOMProps: (props: TreeSelectProps<any>) => omit(props, OMIT_PROPS),
});
RefSelect.displayName = 'Select';
// =================================================================================
// = Tree =
// =================================================================================
const RefTreeSelect = React.forwardRef<RefSelectProps, TreeSelectProps>((props, ref) => {
const {
fieldNames,
multiple,
treeCheckable,
treeCheckStrictly,
showCheckedStrategy = 'SHOW_CHILD',
labelInValue,
loadData,
treeLoadedKeys,
treeNodeFilterProp = 'value',
treeNodeLabelProp,
treeDataSimpleMode,
treeData,
treeExpandedKeys,
treeDefaultExpandedKeys,
treeDefaultExpandAll,
children,
treeIcon,
showTreeIcon,
switcherIcon,
treeLine,
treeMotion,
filterTreeNode,
dropdownPopupAlign,
onChange,
onTreeExpand,
onTreeLoad,
onDropdownVisibleChange,
onSelect,
onDeselect,
labelRender,
} = props;
const mergedCheckable: React.ReactNode | boolean = treeCheckable || treeCheckStrictly;
const mergedMultiple = multiple || mergedCheckable;
const treeConduction = treeCheckable && !treeCheckStrictly;
const mergedLabelInValue = treeCheckStrictly || labelInValue;
// ======================= Tree Data =======================
// FieldNames
const mergedFieldNames = fillFieldNames(fieldNames, true);
// Legacy both support `label` or `title` if not set.
// We have to fallback to function to handle this
const getTreeNodeTitle = (node: DataNode): React.ReactNode => {
if (!treeData) {
return node.title;
}
if (mergedFieldNames?.label) {
return node[mergedFieldNames.label];
}
return node.label || node.title;
};
const getTreeNodeLabelProp = (entity: FlattenDataNode, val: RawValueType): React.ReactNode => {
if (labelRender) {
return labelRender(entity, val);
}
// Skip since entity not exist
if (!entity) {
return undefined;
}
const { node } = entity.data;
if (treeNodeLabelProp) {
return node[treeNodeLabelProp];
}
return getTreeNodeTitle(node);
};
const mergedTreeData = useTreeData(treeData, children, {
getLabelProp: getTreeNodeTitle,
simpleMode: treeDataSimpleMode,
fieldNames: mergedFieldNames,
});
const flattedOptions = useMemo(() => flattenOptions(mergedTreeData), [mergedTreeData]);
const [cacheKeyMap, cacheValueMap] = useKeyValueMap(flattedOptions);
const [getEntityByKey, getEntityByValue] = useKeyValueMapping(cacheKeyMap, cacheValueMap);
// Only generate keyEntities for check conduction when is `treeCheckable`
const { keyEntities: conductKeyEntities } = useMemo(() => {
if (treeConduction) {
return convertDataToEntities(mergedTreeData as any);
}
return { keyEntities: null };
}, [mergedTreeData, treeCheckable, treeCheckStrictly]);
// ========================== Ref ==========================
const selectRef = React.useRef<RefSelectProps>(null);
React.useImperativeHandle(ref, () => ({
scrollTo: selectRef.current.scrollTo,
focus: selectRef.current.focus,
blur: selectRef.current.blur,
/** @private Internal usage. It's save to remove if `rc-cascader` not use it any longer */
getEntityByValue,
}));
// ========================= Value =========================
const [value, setValue] = useMergedState<DefaultValueType>(props.defaultValue, {
value: props.value,
});
/** Get `missingRawValues` which not exist in the tree yet */
const splitRawValues = (newRawValues: RawValueType[]) => {
const missingRawValues = [];
const existRawValues = [];
// Keep missing value in the cache
newRawValues.forEach(val => {
if (getEntityByValue(val)) {
existRawValues.push(val);
} else {
missingRawValues.push(val);
}
});
return { missingRawValues, existRawValues };
};
const [rawValues, rawHalfCheckedKeys]: [RawValueType[], RawValueType[]] = useMemo(() => {
const valueHalfCheckedKeys: RawValueType[] = [];
const newRawValues: RawValueType[] = [];
toArray(value).forEach(item => {
if (item && typeof item === 'object' && 'value' in item) {
if (item.halfChecked && treeCheckStrictly) {
const entity = getEntityByValue(item.value);
valueHalfCheckedKeys.push(entity ? entity.key : item.value);
} else {
newRawValues.push(item.value);
}
} else {
newRawValues.push(item as RawValueType);
}
});
// We need do conduction of values
if (treeConduction) {
const { missingRawValues, existRawValues } = splitRawValues(newRawValues);
const keyList = existRawValues.map(val => getEntityByValue(val).key);
const { checkedKeys, halfCheckedKeys } = conductCheck(keyList, true, conductKeyEntities);
return [
[...missingRawValues, ...checkedKeys.map(key => getEntityByKey(key).data.value)],
halfCheckedKeys,
];
}
return [newRawValues, valueHalfCheckedKeys];
}, [
value,
flattedOptions,
mergedMultiple,
mergedLabelInValue,
treeCheckable,
treeCheckStrictly,
]);
const selectValues = useSelectValues(rawValues, {
treeConduction,
value,
showCheckedStrategy,
conductKeyEntities,
getEntityByValue,
getEntityByKey,
getLabelProp: getTreeNodeLabelProp,
treeNodeLabelProp,
});
const triggerChange = (
newRawValues: RawValueType[],
extra: { triggerValue: RawValueType; selected: boolean },
source: SelectSource,
) => {
setValue(mergedMultiple ? newRawValues : newRawValues[0]);
if (onChange) {
let eventValues: RawValueType[] = newRawValues;
if (treeConduction && showCheckedStrategy !== 'SHOW_ALL') {
const keyList = newRawValues.map(val => {
const entity = getEntityByValue(val);
return entity ? entity.key : val;
});
const formattedKeyList = formatStrategyKeys(
keyList,
showCheckedStrategy,
conductKeyEntities,
);
eventValues = formattedKeyList.map(key => {
const entity = getEntityByKey(key);
return entity ? entity.data.value : key;
});
}
const { triggerValue, selected } = extra || {
triggerValue: undefined,
selected: undefined,
};
let returnValues = mergedLabelInValue
? getRawValueLabeled(eventValues, value, getEntityByValue, getTreeNodeLabelProp)
: eventValues;
// We need fill half check back
if (treeCheckStrictly) {
const halfValues = rawHalfCheckedKeys
.map(key => {
const entity = getEntityByKey(key);
return entity ? entity.data.value : key;
})
.filter(val => !eventValues.includes(val));
returnValues = [
...(returnValues as LabelValueType[]),
...getRawValueLabeled(halfValues, value, getEntityByValue, getTreeNodeLabelProp),
];
}
const additionalInfo = {
// [Legacy] Always return as array contains label & value
preValue: selectValues,
triggerValue,
} as ChangeEventExtra;
// [Legacy] Fill legacy data if user query.
// This is expansive that we only fill when user query
// https://github.com/react-component/tree-select/blob/fe33eb7c27830c9ac70cd1fdb1ebbe7bc679c16a/src/Select.jsx
let showPosition = true;
if (treeCheckStrictly || (source === 'selection' && !selected)) {
showPosition = false;
}
fillAdditionalInfo(
additionalInfo,
triggerValue,
newRawValues,
mergedTreeData,
showPosition,
);
if (mergedCheckable) {
additionalInfo.checked = selected;
} else {
additionalInfo.selected = selected;
}
onChange(
mergedMultiple ? returnValues : returnValues[0],
mergedLabelInValue
? null
: eventValues.map(val => {
const entity = getEntityByValue(val);
return entity ? entity.data.title : null;
}),
additionalInfo,
);
}
};
const onInternalSelect = (
selectValue: RawValueType,
option: DataNode,
source: SelectSource,
) => {
const eventValue = mergedLabelInValue ? selectValue : selectValue;
if (!mergedMultiple) {
// Single mode always set value
triggerChange([selectValue], { selected: true, triggerValue: selectValue }, source);
} else {
let newRawValues = addValue(rawValues, selectValue);
// Add keys if tree conduction
if (treeConduction) {
// Should keep missing values
const { missingRawValues, existRawValues } = splitRawValues(newRawValues);
const keyList = existRawValues.map(val => getEntityByValue(val).key);
const { checkedKeys } = conductCheck(keyList, true, conductKeyEntities);
newRawValues = [
...missingRawValues,
...checkedKeys.map(key => getEntityByKey(key).data.value),
];
}
triggerChange(newRawValues, { selected: true, triggerValue: selectValue }, source);
}
if (onSelect) {
onSelect(eventValue, option);
}
};
const onInternalDeselect = (
selectValue: RawValueType,
option: DataNode,
source: SelectSource,
) => {
const eventValue = mergedLabelInValue ? selectValue : selectValue;
let newRawValues = removeValue(rawValues, selectValue);
// Remove keys if tree conduction
if (treeConduction) {
const { missingRawValues, existRawValues } = splitRawValues(newRawValues);
const keyList = existRawValues.map(val => getEntityByValue(val).key);
const { checkedKeys } = conductCheck(
keyList,
{ checked: false, halfCheckedKeys: rawHalfCheckedKeys },
conductKeyEntities,
);
newRawValues = [
...missingRawValues,
...checkedKeys.map(key => getEntityByKey(key).data.value),
];
}
triggerChange(newRawValues, { selected: false, triggerValue: selectValue }, source);
if (onDeselect) {
onDeselect(eventValue, option);
}
};
const onInternalClear = () => {
triggerChange([], null, 'clear');
};
// ========================= Open ==========================
const onInternalDropdownVisibleChange = React.useCallback(
(open: boolean) => {
if (onDropdownVisibleChange) {
const legacyParam = {};
Object.defineProperty(legacyParam, 'documentClickClose', {
get() {
warning(false, 'Second param of `onDropdownVisibleChange` has been removed.');
return false;
},
});
(onDropdownVisibleChange as any)(open, legacyParam);
}
},
[onDropdownVisibleChange],
);
// ======================== Warning ========================
if (process.env.NODE_ENV !== 'production') {
warningProps(props);
}
// ======================== Render =========================
// We pass some props into select props style
const selectProps: Partial<SelectProps<any, any>> = {
optionLabelProp: null,
optionFilterProp: treeNodeFilterProp,
dropdownAlign: dropdownPopupAlign,
internalProps: {
mark: INTERNAL_PROPS_MARK,
onClear: onInternalClear,
skipTriggerChange: true,
skipTriggerSelect: true,
onRawSelect: onInternalSelect,
onRawDeselect: onInternalDeselect,
},
};
if ('filterTreeNode' in props) {
selectProps.filterOption = filterTreeNode;
}
const selectContext = React.useMemo(
() => ({
checkable: mergedCheckable,
loadData,
treeLoadedKeys,
onTreeLoad,
checkedKeys: rawValues,
halfCheckedKeys: rawHalfCheckedKeys,
treeDefaultExpandAll,
treeExpandedKeys,
treeDefaultExpandedKeys,
onTreeExpand,
treeIcon,
treeMotion,
showTreeIcon,
switcherIcon,
treeLine,
treeNodeFilterProp,
getEntityByKey,
getEntityByValue,
}),
[
mergedCheckable,
loadData,
treeLoadedKeys,
onTreeLoad,
rawValues,
rawHalfCheckedKeys,
treeDefaultExpandAll,
treeExpandedKeys,
treeDefaultExpandedKeys,
onTreeExpand,
treeIcon,
treeMotion,
showTreeIcon,
switcherIcon,
treeLine,
treeNodeFilterProp,
getEntityByKey,
getEntityByValue,
],
);
return (
<SelectContext.Provider value={selectContext}>
<RefSelect
ref={selectRef}
mode={mergedMultiple ? 'multiple' : null}
{...props}
{...selectProps}
value={selectValues}
// We will handle this ourself since we need calculate conduction
labelInValue
options={mergedTreeData}
onChange={null}
onSelect={null}
onDeselect={null}
onDropdownVisibleChange={onInternalDropdownVisibleChange}
/>
</SelectContext.Provider>
);
});
RefTreeSelect.displayName = 'TreeSelect';
// =================================================================================
// = Generic =
// =================================================================================
const TreeSelect = RefTreeSelect as any as (<ValueType = DefaultValueType>(
props: React.PropsWithChildren<TreeSelectProps<ValueType>> & {
ref?: React.Ref<RefSelectProps>;
},
) => React.ReactElement) & {
TreeNode: typeof TreeNode;
SHOW_ALL: typeof SHOW_ALL;
SHOW_PARENT: typeof SHOW_PARENT;
SHOW_CHILD: typeof SHOW_CHILD;
};
TreeSelect.TreeNode = TreeNode;
TreeSelect.SHOW_ALL = SHOW_ALL;
TreeSelect.SHOW_PARENT = SHOW_PARENT;
TreeSelect.SHOW_CHILD = SHOW_CHILD;
return TreeSelect;
}