-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathPanel.tsx
214 lines (194 loc) · 5.79 KB
/
Panel.tsx
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
import classNames from 'classnames';
import { useEvent, useMergedState } from 'rc-util';
import * as React from 'react';
import type {
CascaderProps,
DefaultOptionType,
InternalCascaderProps,
InternalValueType,
SingleValueType,
} from './Cascader';
import type { CascaderContextProps } from './context';
import CascaderContext from './context';
import useMissingValues from './hooks/useMissingValues';
import useOptions from './hooks/useOptions';
import useSelect from './hooks/useSelect';
import useValues from './hooks/useValues';
import RawOptionList from './OptionList/List';
import { fillFieldNames, toRawValues } from './utils/commonUtil';
import { toPathOptions } from './utils/treeUtil';
export type PickType =
| 'value'
| 'defaultValue'
| 'changeOnSelect'
| 'onChange'
| 'options'
| 'prefixCls'
| 'checkable'
| 'fieldNames'
| 'showCheckedStrategy'
| 'loadData'
| 'expandTrigger'
| 'expandIcon'
| 'loadingIcon'
| 'className'
| 'style'
| 'direction'
| 'notFoundContent'
| 'disabled'
| 'defaultActiveKey';
export type PanelProps<
OptionType extends DefaultOptionType = DefaultOptionType,
ValueField extends keyof OptionType = keyof OptionType,
Multiple extends boolean | React.ReactNode = false,
> = Pick<CascaderProps<OptionType, ValueField, Multiple>, PickType>;
function noop() {}
export default function Panel<
OptionType extends DefaultOptionType = DefaultOptionType,
ValueField extends keyof OptionType = keyof OptionType,
Multiple extends boolean | React.ReactNode = false,
>(props: PanelProps<OptionType, ValueField, Multiple>) {
const {
prefixCls = 'rc-cascader',
style,
className,
options,
checkable,
defaultValue,
value,
fieldNames,
changeOnSelect,
onChange,
showCheckedStrategy,
loadData,
expandTrigger,
expandIcon = '>',
loadingIcon,
direction,
notFoundContent = 'Not Found',
disabled,
defaultActiveKey,
} = props as Pick<InternalCascaderProps, PickType>;
// ======================== Multiple ========================
const multiple = !!checkable;
// ========================= Values =========================
const [rawValues, setRawValues] = useMergedState<
InternalValueType | undefined,
SingleValueType[]
>(defaultValue, { value, postState: toRawValues });
// ========================= FieldNames =========================
const mergedFieldNames = React.useMemo(
() => fillFieldNames(fieldNames),
/* eslint-disable react-hooks/exhaustive-deps */
[JSON.stringify(fieldNames)],
/* eslint-enable react-hooks/exhaustive-deps */
);
// =========================== Option ===========================
const [mergedOptions, getPathKeyEntities, getValueByKeyPath] = useOptions(
mergedFieldNames,
options,
);
// ========================= Values =========================
const getMissingValues = useMissingValues(mergedOptions, mergedFieldNames);
// Fill `rawValues` with checked conduction values
const [checkedValues, halfCheckedValues, missingCheckedValues] = useValues(
multiple,
rawValues,
getPathKeyEntities,
getValueByKeyPath,
getMissingValues,
);
// =========================== Change ===========================
const triggerChange = useEvent((nextValues: InternalValueType) => {
setRawValues(nextValues);
// Save perf if no need trigger event
if (onChange) {
const nextRawValues = toRawValues(nextValues);
const valueOptions = nextRawValues.map(valueCells =>
toPathOptions(valueCells, mergedOptions, mergedFieldNames).map(valueOpt => valueOpt.option),
);
const triggerValues = multiple ? nextRawValues : nextRawValues[0];
const triggerOptions = multiple ? valueOptions : valueOptions[0];
onChange(triggerValues, triggerOptions);
}
});
// =========================== Select ===========================
const handleSelection = useSelect(
multiple,
triggerChange,
checkedValues,
halfCheckedValues,
missingCheckedValues,
getPathKeyEntities,
getValueByKeyPath,
showCheckedStrategy,
);
const onInternalSelect = useEvent((valuePath: SingleValueType) => {
handleSelection(valuePath);
});
// ======================== Context =========================
const cascaderContext = React.useMemo<CascaderContextProps>(
() => ({
options: mergedOptions,
fieldNames: mergedFieldNames,
values: checkedValues,
halfValues: halfCheckedValues,
changeOnSelect,
onSelect: onInternalSelect,
checkable,
searchOptions: [],
dropdownPrefixCls: undefined,
loadData,
expandTrigger,
expandIcon,
loadingIcon,
dropdownMenuColumnStyle: undefined,
}),
[
mergedOptions,
mergedFieldNames,
checkedValues,
halfCheckedValues,
changeOnSelect,
onInternalSelect,
checkable,
loadData,
expandTrigger,
expandIcon,
loadingIcon,
],
);
// ========================= Render =========================
const panelPrefixCls = `${prefixCls}-panel`;
const isEmpty = !mergedOptions.length;
return (
<CascaderContext.Provider value={cascaderContext}>
<div
className={classNames(
panelPrefixCls,
{
[`${panelPrefixCls}-rtl`]: direction === 'rtl',
[`${panelPrefixCls}-empty`]: isEmpty,
},
className,
)}
style={style}
>
{isEmpty ? (
notFoundContent
) : (
<RawOptionList
prefixCls={prefixCls}
searchValue=""
multiple={multiple}
toggleOpen={noop}
open
direction={direction}
disabled={disabled}
defaultActiveKey={defaultActiveKey}
/>
)}
</div>
</CascaderContext.Provider>
);
}