OptionList.js
11.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
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var React = _interopRequireWildcard(require("react"));
var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
var _pickAttrs = _interopRequireDefault(require("rc-util/lib/pickAttrs"));
var _useMemo = _interopRequireDefault(require("rc-util/lib/hooks/useMemo"));
var _classnames = _interopRequireDefault(require("classnames"));
var _rcVirtualList = _interopRequireDefault(require("rc-virtual-list"));
var _TransBtn = _interopRequireDefault(require("./TransBtn"));
/**
* Using virtual list of option display.
* Will fallback to dom if use customize render.
*/
var OptionList = function OptionList(_ref, ref) {
var prefixCls = _ref.prefixCls,
id = _ref.id,
flattenOptions = _ref.flattenOptions,
childrenAsData = _ref.childrenAsData,
values = _ref.values,
searchValue = _ref.searchValue,
multiple = _ref.multiple,
defaultActiveFirstOption = _ref.defaultActiveFirstOption,
height = _ref.height,
itemHeight = _ref.itemHeight,
notFoundContent = _ref.notFoundContent,
open = _ref.open,
menuItemSelectedIcon = _ref.menuItemSelectedIcon,
virtual = _ref.virtual,
onSelect = _ref.onSelect,
onToggleOpen = _ref.onToggleOpen,
onActiveValue = _ref.onActiveValue,
onScroll = _ref.onScroll,
onMouseEnter = _ref.onMouseEnter;
var itemPrefixCls = "".concat(prefixCls, "-item");
var memoFlattenOptions = (0, _useMemo.default)(function () {
return flattenOptions;
}, [open, flattenOptions], function (prev, next) {
return next[0] && prev[1] !== next[1];
}); // =========================== List ===========================
var listRef = React.useRef(null);
var onListMouseDown = function onListMouseDown(event) {
event.preventDefault();
};
var scrollIntoView = function scrollIntoView(index) {
if (listRef.current) {
listRef.current.scrollTo({
index: index
});
}
}; // ========================== Active ==========================
var getEnabledActiveIndex = function getEnabledActiveIndex(index) {
var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var len = memoFlattenOptions.length;
for (var i = 0; i < len; i += 1) {
var current = (index + i * offset + len) % len;
var _memoFlattenOptions$c = memoFlattenOptions[current],
group = _memoFlattenOptions$c.group,
data = _memoFlattenOptions$c.data;
if (!group && !data.disabled) {
return current;
}
}
return -1;
};
var _React$useState = React.useState(function () {
return getEnabledActiveIndex(0);
}),
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
activeIndex = _React$useState2[0],
setActiveIndex = _React$useState2[1];
var setActive = function setActive(index) {
var fromKeyboard = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
setActiveIndex(index);
var info = {
source: fromKeyboard ? 'keyboard' : 'mouse'
}; // Trigger active event
var flattenItem = memoFlattenOptions[index];
if (!flattenItem) {
onActiveValue(null, -1, info);
return;
}
onActiveValue(flattenItem.data.value, index, info);
}; // Auto active first item when list length or searchValue changed
React.useEffect(function () {
setActive(defaultActiveFirstOption !== false ? getEnabledActiveIndex(0) : -1);
}, [memoFlattenOptions.length, searchValue]); // Auto scroll to item position in single mode
React.useEffect(function () {
/**
* React will skip `onChange` when component update.
* `setActive` function will call root accessibility state update which makes re-render.
* So we need to delay to let Input component trigger onChange first.
*/
var timeoutId = setTimeout(function () {
if (!multiple && open && values.size === 1) {
var value = Array.from(values)[0];
var index = memoFlattenOptions.findIndex(function (_ref2) {
var data = _ref2.data;
return data.value === value;
});
setActive(index);
scrollIntoView(index);
}
}); // Force trigger scrollbar visible when open
if (open) {
var _listRef$current;
(_listRef$current = listRef.current) === null || _listRef$current === void 0 ? void 0 : _listRef$current.scrollTo(undefined);
}
return function () {
return clearTimeout(timeoutId);
};
}, [open]); // ========================== Values ==========================
var onSelectValue = function onSelectValue(value) {
if (value !== undefined) {
onSelect(value, {
selected: !values.has(value)
});
} // Single mode should always close by select
if (!multiple) {
onToggleOpen(false);
}
}; // ========================= Keyboard =========================
React.useImperativeHandle(ref, function () {
return {
onKeyDown: function onKeyDown(event) {
var which = event.which;
switch (which) {
// >>> Arrow keys
case _KeyCode.default.UP:
case _KeyCode.default.DOWN:
{
var offset = 0;
if (which === _KeyCode.default.UP) {
offset = -1;
} else if (which === _KeyCode.default.DOWN) {
offset = 1;
}
if (offset !== 0) {
var nextActiveIndex = getEnabledActiveIndex(activeIndex + offset, offset);
scrollIntoView(nextActiveIndex);
setActive(nextActiveIndex, true);
}
break;
}
// >>> Select
case _KeyCode.default.ENTER:
{
// value
var item = memoFlattenOptions[activeIndex];
if (item && !item.data.disabled) {
onSelectValue(item.data.value);
} else {
onSelectValue(undefined);
}
if (open) {
event.preventDefault();
}
break;
}
// >>> Close
case _KeyCode.default.ESC:
{
onToggleOpen(false);
}
}
},
onKeyUp: function onKeyUp() {},
scrollTo: function scrollTo(index) {
scrollIntoView(index);
}
};
}); // ========================== Render ==========================
if (memoFlattenOptions.length === 0) {
return React.createElement("div", {
role: "listbox",
id: "".concat(id, "_list"),
className: "".concat(itemPrefixCls, "-empty"),
onMouseDown: onListMouseDown
}, notFoundContent);
}
function renderItem(index) {
var item = memoFlattenOptions[index];
if (!item) return null;
var itemData = item.data || {};
var value = itemData.value,
label = itemData.label,
children = itemData.children;
var attrs = (0, _pickAttrs.default)(itemData, true);
var mergedLabel = childrenAsData ? children : label;
return item ? React.createElement("div", Object.assign({
"aria-label": typeof mergedLabel === 'string' ? mergedLabel : null
}, attrs, {
key: index,
role: "option",
id: "".concat(id, "_list_").concat(index),
"aria-selected": values.has(value)
}), value) : null;
}
return React.createElement(React.Fragment, null, React.createElement("div", {
role: "listbox",
id: "".concat(id, "_list"),
style: {
height: 0,
width: 0,
overflow: 'hidden'
}
}, renderItem(activeIndex - 1), renderItem(activeIndex), renderItem(activeIndex + 1)), React.createElement(_rcVirtualList.default, {
itemKey: "key",
ref: listRef,
data: memoFlattenOptions,
height: height,
itemHeight: itemHeight,
fullHeight: false,
onMouseDown: onListMouseDown,
onScroll: onScroll,
virtual: virtual,
onMouseEnter: onMouseEnter
}, function (_ref3, itemIndex) {
var _classNames;
var group = _ref3.group,
groupOption = _ref3.groupOption,
data = _ref3.data;
var label = data.label,
key = data.key; // Group
if (group) {
return React.createElement("div", {
className: (0, _classnames.default)(itemPrefixCls, "".concat(itemPrefixCls, "-group"))
}, label !== undefined ? label : key);
}
var disabled = data.disabled,
value = data.value,
title = data.title,
children = data.children,
style = data.style,
className = data.className,
otherProps = (0, _objectWithoutProperties2.default)(data, ["disabled", "value", "title", "children", "style", "className"]); // Option
var selected = values.has(value);
var optionPrefixCls = "".concat(itemPrefixCls, "-option");
var optionClassName = (0, _classnames.default)(itemPrefixCls, optionPrefixCls, className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(optionPrefixCls, "-grouped"), groupOption), (0, _defineProperty2.default)(_classNames, "".concat(optionPrefixCls, "-active"), activeIndex === itemIndex && !disabled), (0, _defineProperty2.default)(_classNames, "".concat(optionPrefixCls, "-disabled"), disabled), (0, _defineProperty2.default)(_classNames, "".concat(optionPrefixCls, "-selected"), selected), _classNames));
var mergedLabel = childrenAsData ? children : label;
var iconVisible = !menuItemSelectedIcon || typeof menuItemSelectedIcon === 'function' || selected;
var content = mergedLabel || value; // https://github.com/ant-design/ant-design/issues/26717
var optionTitle = typeof content === 'string' || typeof content === 'number' ? content.toString() : undefined;
if (title !== undefined) {
optionTitle = title;
}
return React.createElement("div", Object.assign({}, otherProps, {
"aria-selected": selected,
className: optionClassName,
title: optionTitle,
onMouseMove: function onMouseMove() {
if (activeIndex === itemIndex || disabled) {
return;
}
setActive(itemIndex);
},
onClick: function onClick() {
if (!disabled) {
onSelectValue(value);
}
},
style: style
}), React.createElement("div", {
className: "".concat(optionPrefixCls, "-content")
}, content), React.isValidElement(menuItemSelectedIcon) || selected, iconVisible && React.createElement(_TransBtn.default, {
className: "".concat(itemPrefixCls, "-option-state"),
customizeIcon: menuItemSelectedIcon,
customizeIconProps: {
isSelected: selected
}
}, selected ? '✓' : null));
}));
};
var RefOptionList = React.forwardRef(OptionList);
RefOptionList.displayName = 'OptionList';
var _default = RefOptionList;
exports.default = _default;