-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathuseSelectValues.ts
More file actions
61 lines (56 loc) · 1.96 KB
/
useSelectValues.ts
File metadata and controls
61 lines (56 loc) · 1.96 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
import * as React from 'react';
import type { DefaultValueType } from 'rc-select/lib/interface/generator';
import type { DataEntity } from 'rc-tree/lib/interface';
import type { RawValueType, FlattenDataNode, Key, LabelValueType } from '../interface';
import type { SkipType } from './useKeyValueMapping';
import { getRawValueLabeled } from '../utils/valueUtil';
import type { CheckedStrategy } from '../utils/strategyUtil';
import { formatStrategyKeys } from '../utils/strategyUtil';
interface Config {
treeConduction: boolean;
/** Current `value` of TreeSelect */
value: DefaultValueType;
showCheckedStrategy: CheckedStrategy;
conductKeyEntities: Record<Key, DataEntity>;
getEntityByKey: (key: Key, skipType?: SkipType, ignoreDisabledCheck?: boolean) => FlattenDataNode;
getEntityByValue: (
value: RawValueType,
skipType?: SkipType,
ignoreDisabledCheck?: boolean,
) => FlattenDataNode;
getLabelProp: (entity: FlattenDataNode, value: RawValueType) => React.ReactNode;
treeNodeLabelProp?: string;
}
/** Return */
export default function useSelectValues(
rawValues: RawValueType[],
{
value,
getEntityByValue,
getEntityByKey,
treeConduction,
showCheckedStrategy,
conductKeyEntities,
getLabelProp,
treeNodeLabelProp,
}: Config,
): LabelValueType[] {
return React.useMemo(() => {
let mergedRawValues = rawValues;
if (treeConduction) {
const rawKeys = formatStrategyKeys(
rawValues.map(val => {
const entity = getEntityByValue(val);
return entity ? entity.key : val;
}),
showCheckedStrategy,
conductKeyEntities,
);
mergedRawValues = rawKeys.map(key => {
const entity = getEntityByKey(key);
return entity ? entity.data.value : key;
});
}
return getRawValueLabeled(mergedRawValues, value, getEntityByValue, getLabelProp);
}, [rawValues, value, treeConduction, showCheckedStrategy, getEntityByValue, treeNodeLabelProp]);
}