-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathList.tsx
268 lines (226 loc) · 7.57 KB
/
List.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
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
/* eslint-disable default-case */
import classNames from 'classnames';
import type { useBaseProps } from 'rc-select';
import type { RefOptionListProps } from 'rc-select/lib/OptionList';
import * as React from 'react';
import type { DefaultOptionType, SingleValueType } from '../Cascader';
import CascaderContext from '../context';
import {
getFullPathKeys,
isLeaf,
scrollIntoParentView,
toPathKey,
toPathKeys,
toPathValueStr,
} from '../utils/commonUtil';
import { toPathOptions } from '../utils/treeUtil';
import CacheContent from './CacheContent';
import Column, { FIX_LABEL } from './Column';
import useActive from './useActive';
import useKeyboard from './useKeyboard';
export type RawOptionListProps = Pick<
ReturnType<typeof useBaseProps>,
| 'prefixCls'
| 'multiple'
| 'searchValue'
| 'toggleOpen'
| 'notFoundContent'
| 'direction'
| 'open'
| 'disabled'
> & { defaultActiveKey?: React.Key[]; };
const RawOptionList = React.forwardRef<RefOptionListProps, RawOptionListProps>((props, ref) => {
const {
prefixCls,
multiple,
searchValue,
toggleOpen,
notFoundContent,
direction,
open,
disabled,
defaultActiveKey,
} = props;
const containerRef = React.useRef<HTMLDivElement>(null);
const rtl = direction === 'rtl';
const {
options,
values,
halfValues,
fieldNames,
changeOnSelect,
onSelect,
searchOptions,
dropdownPrefixCls,
loadData,
expandTrigger,
} = React.useContext(CascaderContext);
const mergedPrefixCls = dropdownPrefixCls || prefixCls;
// ========================= loadData =========================
const [loadingKeys, setLoadingKeys] = React.useState<React.Key[]>([]);
const internalLoadData = (valueCells: React.Key[]) => {
// Do not load when search
if (!loadData || searchValue) {
return;
}
const optionList = toPathOptions(valueCells, options, fieldNames);
const rawOptions = optionList.map(({ option }) => option);
const lastOption = rawOptions[rawOptions.length - 1];
if (lastOption && !isLeaf(lastOption, fieldNames)) {
const pathKey = toPathKey(valueCells);
setLoadingKeys(keys => [...keys, pathKey]);
loadData(rawOptions);
}
};
// zombieJ: This is bad. We should make this same as `rc-tree` to use Promise instead.
React.useEffect(() => {
if (loadingKeys.length) {
loadingKeys.forEach(loadingKey => {
const valueStrCells = toPathValueStr(loadingKey as string);
const optionList = toPathOptions(valueStrCells, options, fieldNames, true).map(
({ option }) => option,
);
const lastOption = optionList[optionList.length - 1];
if (!lastOption || lastOption[fieldNames.children] || isLeaf(lastOption, fieldNames)) {
setLoadingKeys(keys => keys.filter(key => key !== loadingKey));
}
});
}
}, [options, loadingKeys, fieldNames]);
// ========================== Values ==========================
const checkedSet = React.useMemo(() => new Set(toPathKeys(values)), [values]);
const halfCheckedSet = React.useMemo(() => new Set(toPathKeys(halfValues)), [halfValues]);
// ====================== Accessibility =======================
const [activeValueCells, setActiveValueCells] = useActive(multiple, open, defaultActiveKey);
// =========================== Path ===========================
const onPathOpen = (nextValueCells: React.Key[]) => {
setActiveValueCells(nextValueCells);
// Trigger loadData
internalLoadData(nextValueCells);
};
const isSelectable = (option: DefaultOptionType) => {
if (disabled) {
return false;
}
const { disabled: optionDisabled } = option;
const isMergedLeaf = isLeaf(option, fieldNames);
return !optionDisabled && (isMergedLeaf || changeOnSelect || multiple);
};
const onPathSelect = (valuePath: SingleValueType, leaf: boolean, fromKeyboard = false) => {
onSelect(valuePath);
if (!multiple && (leaf || (changeOnSelect && (expandTrigger === 'hover' || fromKeyboard)))) {
toggleOpen(false);
}
};
// ========================== Option ==========================
const mergedOptions = React.useMemo(() => {
if (searchValue) {
return searchOptions;
}
return options;
}, [searchValue, searchOptions, options]);
// ========================== Column ==========================
const optionColumns = React.useMemo(() => {
const optionList = [{ options: mergedOptions }];
let currentList = mergedOptions;
const fullPathKeys = getFullPathKeys(currentList, fieldNames);
for (let i = 0; i < activeValueCells.length; i += 1) {
const activeValueCell = activeValueCells[i];
const currentOption = currentList.find(
(option, index) =>
(fullPathKeys[index] ? toPathKey(fullPathKeys[index]) : option[fieldNames.value]) ===
activeValueCell,
);
const subOptions = currentOption?.[fieldNames.children];
if (!subOptions?.length) {
break;
}
currentList = subOptions;
optionList.push({ options: subOptions });
}
return optionList;
}, [mergedOptions, activeValueCells, fieldNames]);
// ========================= Keyboard =========================
const onKeyboardSelect = (selectValueCells: SingleValueType, option: DefaultOptionType) => {
if (isSelectable(option)) {
onPathSelect(selectValueCells, isLeaf(option, fieldNames), true);
}
};
useKeyboard(ref, mergedOptions, fieldNames, activeValueCells, onPathOpen, onKeyboardSelect, {
direction,
searchValue,
toggleOpen,
open,
});
// >>>>> Active Scroll
React.useEffect(() => {
if (searchValue) {
return;
}
for (let i = 0; i < activeValueCells.length; i += 1) {
const cellPath = activeValueCells.slice(0, i + 1);
const cellKeyPath = toPathKey(cellPath);
const ele = containerRef.current?.querySelector<HTMLElement>(
`li[data-path-key="${cellKeyPath.replace(/\\{0,2}"/g, '\\"')}"]`, // matches unescaped double quotes
);
if (ele) {
scrollIntoParentView(ele);
}
}
}, [activeValueCells, searchValue]);
// ========================== Render ==========================
// >>>>> Empty
const isEmpty = !optionColumns[0]?.options?.length;
const emptyList: DefaultOptionType[] = [
{
[fieldNames.value as 'value']: '__EMPTY__',
[FIX_LABEL as 'label']: notFoundContent,
disabled: true,
},
];
const columnProps = {
...props,
multiple: !isEmpty && multiple,
onSelect: onPathSelect,
onActive: onPathOpen,
onToggleOpen: toggleOpen,
checkedSet,
halfCheckedSet,
loadingKeys,
isSelectable,
};
// >>>>> Columns
const mergedOptionColumns = isEmpty ? [{ options: emptyList }] : optionColumns;
const columnNodes: React.ReactElement[] = mergedOptionColumns.map((col, index) => {
const prevValuePath = activeValueCells.slice(0, index);
const activeValue = activeValueCells[index];
return (
<Column
key={index}
{...columnProps}
prefixCls={mergedPrefixCls}
options={col.options}
prevValuePath={prevValuePath}
activeValue={activeValue}
/>
);
});
// >>>>> Render
return (
<CacheContent open={open}>
<div
className={classNames(`${mergedPrefixCls}-menus`, {
[`${mergedPrefixCls}-menu-empty`]: isEmpty,
[`${mergedPrefixCls}-rtl`]: rtl,
})}
ref={containerRef}
>
{columnNodes}
</div>
</CacheContent>
);
});
if (process.env.NODE_ENV !== 'production') {
RawOptionList.displayName = 'RawOptionList';
}
export default RawOptionList;