@rescui/select 0.11.7 → 0.11.9-RUI-226-Update-card-cn-6e43cc89.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/select.js CHANGED
@@ -1,27 +1,4 @@
1
- import "core-js/modules/es.object.to-string.js";
2
- import "core-js/modules/web.dom-collections.for-each.js";
3
- import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
4
- import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
5
- import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
6
- import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
7
- import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
8
- import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
9
- import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties";
10
- var _excluded = ["options", "size", "mode", "theme", "className", "value", "onChange", "placeholder", "isClearable", "isLoading", "isDisabled", "isSearchable", "noOptionsMessage", "placement", "boldLabel"];
11
-
12
- function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
-
14
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : _Object$getOwnPropertyDescriptors ? Object.defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } return target; }
15
-
16
- import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
17
- import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
18
- import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
19
1
  import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes";
20
- import "core-js/modules/es.array.join.js";
21
- import "core-js/modules/es.regexp.constructor.js";
22
- import "core-js/modules/es.regexp.exec.js";
23
- import "core-js/modules/es.regexp.to-string.js";
24
- import "core-js/modules/es.function.name.js";
25
2
  import React, { useState, useRef, useEffect, useMemo, useCallback } from 'react';
26
3
  import PropTypes from 'prop-types';
27
4
  import cn from 'classnames';
@@ -37,35 +14,33 @@ import { useStrongMemo, getDefaultCursorIndex, isGroupsEmpty, getMovedCursorInde
37
14
  import { ARROW_DOWN, ARROW_UP, ENTER, SPACE, TAB, ESCAPE, BACKSPACE } from './key-codes.js';
38
15
  import styles from './parts/common-select.p.module.css.js'; // eslint-disable-next-line complexity
39
16
 
40
- var Option = function Option(_ref) {
41
- var selected = _ref.selected,
42
- cursorIndex = _ref.cursorIndex,
43
- item = _ref.item,
44
- isLastItem = _ref.isLastItem,
45
- highlightedItemRef = _ref.highlightedItemRef,
46
- handleSelectIndex = _ref.handleSelectIndex,
47
- handleHighlightIndex = _ref.handleHighlightIndex;
17
+ const Option = _ref => {
18
+ let {
19
+ selected,
20
+ cursorIndex,
21
+ item,
22
+ isLastItem,
23
+ highlightedItemRef,
24
+ handleSelectIndex,
25
+ handleHighlightIndex
26
+ } = _ref;
48
27
 
49
28
  if (item.type === OptionGroupTypes.OPTION) {
50
- var _context;
51
-
52
- var index = item.index,
53
- label = item.label;
54
- var value = getOptionValue(item);
55
- var isSelected = selected && value === getOptionValue(selected);
56
- var isHighlighted = isIndexMatchCursor(cursorIndex, index);
29
+ const {
30
+ index,
31
+ label
32
+ } = item;
33
+ const value = getOptionValue(item);
34
+ const isSelected = selected && value === getOptionValue(selected);
35
+ const isHighlighted = isIndexMatchCursor(cursorIndex, index);
57
36
  return /*#__PURE__*/React.createElement(MenuItem, {
58
37
  selected: isSelected,
59
38
  highlighted: isHighlighted,
60
39
  ref: isHighlighted ? highlightedItemRef : null,
61
- onMouseMove: function onMouseMove() {
62
- return handleHighlightIndex(index);
63
- },
64
- onClick: function onClick() {
65
- return handleSelectIndex(index);
66
- },
40
+ onMouseMove: () => handleHighlightIndex(index),
41
+ onClick: () => handleSelectIndex(index),
67
42
  className: cn(isLastItem && styles.lastOption),
68
- "data-test": _filterInstanceProperty(_context = ['select-menu-item', isHighlighted && 'select-menu-item-highlighted', isSelected && 'select-menu-item-selected']).call(_context, Boolean).join(' ')
43
+ "data-test": ['select-menu-item', isHighlighted && 'select-menu-item-highlighted', isSelected && 'select-menu-item-selected'].filter(Boolean).join(' ')
69
44
  }, label);
70
45
  }
71
46
 
@@ -76,140 +51,96 @@ var Option = function Option(_ref) {
76
51
  }
77
52
 
78
53
  if (item.type === OptionGroupTypes.HEADER) {
79
- var _label = item.label;
54
+ const {
55
+ label
56
+ } = item;
80
57
  return /*#__PURE__*/React.createElement(GroupHeader, {
81
- text: _label
58
+ text: label
82
59
  });
83
60
  }
84
61
 
85
62
  return null;
86
63
  };
87
64
 
88
- var getOptionKey = function getOptionKey(item, i) {
65
+ const getOptionKey = (item, i) => {
89
66
  if (item.type === OptionGroupTypes.OPTION) {
90
- var _context2;
91
-
92
- var value = getOptionValue(item);
93
- return _concatInstanceProperty(_context2 = "".concat(value, "_")).call(_context2, item.index[0]);
67
+ const value = getOptionValue(item);
68
+ return `${value}_${item.index[0]}`;
94
69
  } else if (item.type === OptionGroupTypes.DIVIDER) {
95
- return "divider".concat(i);
70
+ return `divider${i}`;
96
71
  } else if (item.type === OptionGroupTypes.HEADER) {
97
- return "header".concat(i);
72
+ return `header${i}`;
98
73
  }
99
74
 
100
75
  return null;
101
76
  };
102
77
 
103
- var SIZE_STYLES = {
78
+ const SIZE_STYLES = {
104
79
  s: styles.sizeS,
105
80
  m: styles.sizeM,
106
81
  l: styles.sizeL
107
82
  };
108
- var MENU_SIZE_STYLES = {
83
+ const MENU_SIZE_STYLES = {
109
84
  s: styles.menuSizeS,
110
85
  m: styles.menuSizeM,
111
86
  l: styles.menuSizeL
112
87
  }; // eslint-disable-next-line complexity
113
88
 
114
- var Select = function Select(_ref2) {
115
- var _context5;
116
-
117
- var options = _ref2.options,
118
- _ref2$size = _ref2.size,
119
- size = _ref2$size === void 0 ? 'm' : _ref2$size,
120
- _ref2$mode = _ref2.mode,
121
- mode = _ref2$mode === void 0 ? 'classic' : _ref2$mode,
122
- themeFromProps = _ref2.theme,
123
- _ref2$className = _ref2.className,
124
- className = _ref2$className === void 0 ? null : _ref2$className,
125
- _ref2$value = _ref2.value,
126
- value = _ref2$value === void 0 ? undefined : _ref2$value,
127
- onChange = _ref2.onChange,
128
- placeholder = _ref2.placeholder,
129
- _ref2$isClearable = _ref2.isClearable,
130
- isClearable = _ref2$isClearable === void 0 ? false : _ref2$isClearable,
131
- _ref2$isLoading = _ref2.isLoading,
132
- isLoading = _ref2$isLoading === void 0 ? false : _ref2$isLoading,
133
- _ref2$isDisabled = _ref2.isDisabled,
134
- isDisabled = _ref2$isDisabled === void 0 ? false : _ref2$isDisabled,
135
- _ref2$isSearchable = _ref2.isSearchable,
136
- isSearchable = _ref2$isSearchable === void 0 ? false : _ref2$isSearchable,
137
- noOptionsMessage = _ref2.noOptionsMessage,
138
- _ref2$placement = _ref2.placement,
139
- placement = _ref2$placement === void 0 ? 'bottom' : _ref2$placement,
140
- boldLabel = _ref2.boldLabel,
141
- restProps = _objectWithoutProperties(_ref2, _excluded);
142
-
143
- var _useState = useState(null),
144
- _useState2 = _slicedToArray(_useState, 2),
145
- triggerFormWrapperRef = _useState2[0],
146
- setTriggerFormWrapperRef = _useState2[1];
147
-
148
- var theme = useThemeWithUndefined(themeFromProps);
149
- var dropdownRef = useRef(null);
150
- var highlightedItemRef = /*#__PURE__*/React.createRef();
151
-
152
- var _useState3 = useState(false),
153
- _useState4 = _slicedToArray(_useState3, 2),
154
- isKeyboardNavigating = _useState4[0],
155
- setIsKeyboardNavigating = _useState4[1];
156
-
157
- var skipScrollingRef = useRef(false);
158
-
159
- var _useState5 = useState(''),
160
- _useState6 = _slicedToArray(_useState5, 2),
161
- searchString = _useState6[0],
162
- setSearchString = _useState6[1];
163
-
164
- var groups = useStrongMemo(function () {
165
- return convertToOptionGroupsIfNeeded(options);
166
- }, [options]);
167
- var filteredGroups = useStrongMemo(function () {
89
+ const Select = _ref2 => {
90
+ let {
91
+ options,
92
+ size = 'm',
93
+ mode = 'classic',
94
+ theme: themeFromProps,
95
+ className = null,
96
+ value = undefined,
97
+ onChange,
98
+ placeholder,
99
+ isClearable = false,
100
+ isLoading = false,
101
+ isDisabled = false,
102
+ isSearchable = false,
103
+ noOptionsMessage,
104
+ placement = 'bottom',
105
+ boldLabel,
106
+ ...restProps
107
+ } = _ref2;
108
+ const [triggerFormWrapperRef, setTriggerFormWrapperRef] = useState(null);
109
+ const theme = useThemeWithUndefined(themeFromProps);
110
+ const dropdownRef = useRef(null);
111
+ const highlightedItemRef = /*#__PURE__*/React.createRef();
112
+ const [isKeyboardNavigating, setIsKeyboardNavigating] = useState(false);
113
+ const skipScrollingRef = useRef(false);
114
+ const [searchString, setSearchString] = useState('');
115
+ const groups = useStrongMemo(() => convertToOptionGroupsIfNeeded(options), [options]);
116
+ const filteredGroups = useStrongMemo(() => {
168
117
  if (searchString === '') {
169
118
  return groups;
170
119
  }
171
120
 
172
- var filter = new RegExp("(?:^|\\s)".concat(escapeRegex(searchString)), 'i');
173
- return _mapInstanceProperty(groups).call(groups, function (g) {
174
- var _context3;
175
-
176
- return _objectSpread(_objectSpread({}, g), {}, {
177
- options: _filterInstanceProperty(_context3 = g.options).call(_context3, function (_ref3) {
178
- var label = _ref3.label;
179
- return filter.test(label);
180
- })
181
- });
182
- });
121
+ const filter = new RegExp(`(?:^|\\s)${escapeRegex(searchString)}`, 'i');
122
+ return groups.map(g => ({ ...g,
123
+ options: g.options.filter(_ref3 => {
124
+ let {
125
+ label
126
+ } = _ref3;
127
+ return filter.test(label);
128
+ })
129
+ }));
183
130
  }, [searchString, groups]);
184
-
185
- var _useState7 = useState(false),
186
- _useState8 = _slicedToArray(_useState7, 2),
187
- menuIsOpen = _useState8[0],
188
- setMenuIsOpen = _useState8[1];
189
-
190
- var _useState9 = useState(function () {
191
- return getDefaultCursorIndex(filteredGroups);
192
- }),
193
- _useState10 = _slicedToArray(_useState9, 2),
194
- cursorIndex = _useState10[0],
195
- setCursorIndex = _useState10[1];
196
-
197
- useEffect(function () {
131
+ const [menuIsOpen, setMenuIsOpen] = useState(false);
132
+ const [cursorIndex, setCursorIndex] = useState(() => getDefaultCursorIndex(filteredGroups));
133
+ useEffect(() => {
198
134
  setCursorIndex(getDefaultCursorIndex(filteredGroups));
199
135
 
200
136
  if (searchString !== '') {
201
137
  setMenuIsOpen(true);
202
138
  }
203
139
  }, [searchString, filteredGroups]);
204
- var isEmptyMenu = useMemo(function () {
205
- return isGroupsEmpty(filteredGroups);
206
- }, [filteredGroups]);
207
- var handleSelectIndex = useCallback(function (index) {
208
- var _index = _slicedToArray(index, 2),
209
- grpIndex = _index[0],
210
- optIndex = _index[1];
211
-
212
- var selectedOption = filteredGroups[grpIndex].options[optIndex];
140
+ const isEmptyMenu = useMemo(() => isGroupsEmpty(filteredGroups), [filteredGroups]);
141
+ const handleSelectIndex = useCallback(index => {
142
+ const [grpIndex, optIndex] = index;
143
+ const selectedOption = filteredGroups[grpIndex].options[optIndex];
213
144
  setMenuIsOpen(false);
214
145
  setSearchString('');
215
146
  setIsKeyboardNavigating(false);
@@ -218,32 +149,28 @@ var Select = function Select(_ref2) {
218
149
  onChange(selectedOption);
219
150
  }
220
151
  }, [onChange, filteredGroups]);
221
- useEffect(function () {
152
+ useEffect(() => {
222
153
  if (dropdownRef.current) {
223
154
  dropdownRef.current.scheduleUpdate();
224
155
  }
225
156
  }, [searchString]);
226
- var handleMouseMove = useCallback(function () {
227
- return setIsKeyboardNavigating(false);
228
- }, []);
229
- useEffect(function () {
157
+ const handleMouseMove = useCallback(() => setIsKeyboardNavigating(false), []);
158
+ useEffect(() => {
230
159
  if (isKeyboardNavigating) {
231
160
  document.addEventListener('mousemove', handleMouseMove);
232
161
  }
233
162
 
234
- return function () {
235
- return document.removeEventListener('mousemove', handleMouseMove);
236
- };
163
+ return () => document.removeEventListener('mousemove', handleMouseMove);
237
164
  }, [handleMouseMove, isKeyboardNavigating]);
238
- useEffect(function () {
165
+ useEffect(() => {
239
166
  if (isKeyboardNavigating && highlightedItemRef.current) {
240
167
  if (skipScrollingRef.current) {
241
168
  skipScrollingRef.current = false;
242
169
  } else {
243
- var elementToScrollTo = highlightedItemRef.current;
170
+ let elementToScrollTo = highlightedItemRef.current;
244
171
 
245
172
  if (cursorIndex[0] === 0 && cursorIndex[1] === 0) {
246
- var groupHeader = highlightedItemRef.current.previousSibling;
173
+ const groupHeader = highlightedItemRef.current.previousSibling;
247
174
 
248
175
  if (groupHeader) {
249
176
  elementToScrollTo = groupHeader;
@@ -259,15 +186,15 @@ var Select = function Select(_ref2) {
259
186
  }
260
187
  }
261
188
  }, [highlightedItemRef, cursorIndex, isKeyboardNavigating]);
262
- var handleClose = useCallback(function () {
189
+ const handleClose = useCallback(() => {
263
190
  setSearchString('');
264
191
  setMenuIsOpen(false);
265
192
  setIsKeyboardNavigating(false);
266
193
  }, []);
267
- var handleKeyNavigation = useCallback( // eslint-disable-next-line complexity
268
- function (e) {
194
+ const handleKeyNavigation = useCallback( // eslint-disable-next-line complexity
195
+ e => {
269
196
  if (e.key === ARROW_DOWN || e.key === ARROW_UP) {
270
- var isDownDirection = e.key === ARROW_DOWN;
197
+ const isDownDirection = e.key === ARROW_DOWN;
271
198
  setCursorIndex(getMovedCursorIndex(filteredGroups, cursorIndex, isDownDirection));
272
199
  setIsKeyboardNavigating(true);
273
200
  e.preventDefault();
@@ -287,20 +214,16 @@ var Select = function Select(_ref2) {
287
214
  handleClose();
288
215
  }
289
216
  }, [filteredGroups, cursorIndex, handleClose, handleSelectIndex, isSearchable]);
290
- var handleClick = useCallback(function () {
217
+ const handleClick = useCallback(() => {
291
218
  if (isDisabled) {
292
219
  return;
293
220
  }
294
221
 
295
- setMenuIsOpen(function (currentMenuIsOpen) {
296
- return !currentMenuIsOpen;
297
- });
222
+ setMenuIsOpen(currentMenuIsOpen => !currentMenuIsOpen);
298
223
  setSearchString('');
299
224
  }, [isDisabled]);
300
- var handleSearchInputChange = useCallback(function (searchValue) {
301
- return setSearchString(searchValue);
302
- }, []);
303
- var handleClearClick = useCallback(function () {
225
+ const handleSearchInputChange = useCallback(searchValue => setSearchString(searchValue), []);
226
+ const handleClearClick = useCallback(() => {
304
227
  setCursorIndex(getDefaultCursorIndex(filteredGroups));
305
228
  setMenuIsOpen(false);
306
229
 
@@ -308,7 +231,7 @@ var Select = function Select(_ref2) {
308
231
  onChange(null);
309
232
  }
310
233
  }, [filteredGroups, onChange]);
311
- var handleTriggerKeyDown = useCallback(function (e) {
234
+ const handleTriggerKeyDown = useCallback(e => {
312
235
  if (e.key === ESCAPE) {
313
236
  handleClose();
314
237
  e.preventDefault();
@@ -321,9 +244,9 @@ var Select = function Select(_ref2) {
321
244
  if (menuIsOpen) {
322
245
  handleKeyNavigation(e);
323
246
  } else {
324
- var target = e.target;
325
- var trigger = target.closest('[data-target="trigger"]');
326
- var isTriggerTarget = trigger === target;
247
+ const target = e.target;
248
+ const trigger = target.closest('[data-target="trigger"]');
249
+ const isTriggerTarget = trigger === target;
327
250
 
328
251
  if (shouldOpenSelect(e.key, isTriggerTarget)) {
329
252
  setMenuIsOpen(true);
@@ -341,17 +264,17 @@ var Select = function Select(_ref2) {
341
264
  }
342
265
  }
343
266
  }, [menuIsOpen, isDisabled, isClearable, handleClose, handleClearClick, handleKeyNavigation]);
344
- var handleHighlightIndex = useCallback(function (index) {
267
+ const handleHighlightIndex = useCallback(index => {
345
268
  if (!isIndexMatchCursor(cursorIndex, index)) {
346
269
  setCursorIndex(index);
347
270
  }
348
271
  }, [cursorIndex]); // We use invisible and "untouchable" dummy element to observe `size` changes from css to trigger force rerenders
349
272
 
350
- var observableRef = useObservableForceRerender();
351
- var sizeFromCss = null;
273
+ const observableRef = useObservableForceRerender();
274
+ let sizeFromCss = null;
352
275
 
353
276
  if (triggerFormWrapperRef && menuIsOpen) {
354
- var _context4;
277
+ var _context;
355
278
 
356
279
  // In theory this part of the code might cause issues with SSR.
357
280
  // But in this specific context, because of checking ref "triggerFormWrapperRef",
@@ -360,14 +283,14 @@ var Select = function Select(_ref2) {
360
283
  // Also, there will be no layout shifts on the following renders because
361
284
  // it's a dropdown-content and in most cases it opens only on-demand.
362
285
  // In additional, we use "observable" el to watch size changes between rerenders (@media query case).
363
- var triggerFormWrapperStyles = getComputedStyle(triggerFormWrapperRef);
364
- var rawSizeFromCss = triggerFormWrapperStyles.getPropertyValue('--_rs-select-size');
365
- sizeFromCss = _includesInstanceProperty(_context4 = ['s', 'm', 'l']).call(_context4, rawSizeFromCss) ? rawSizeFromCss : null;
286
+ const triggerFormWrapperStyles = getComputedStyle(triggerFormWrapperRef);
287
+ const rawSizeFromCss = triggerFormWrapperStyles.getPropertyValue('--_rs-select-size');
288
+ sizeFromCss = _includesInstanceProperty(_context = ['s', 'm', 'l']).call(_context, rawSizeFromCss) ? rawSizeFromCss : null;
366
289
  }
367
290
 
368
- var actualSize = sizeFromCss || size;
369
- var maxHeight = calculateMaxHeight(actualSize);
370
- var withGroupsLabel = !withoutGroupsLabel(filteredGroups);
291
+ const actualSize = sizeFromCss || size;
292
+ const maxHeight = calculateMaxHeight(actualSize);
293
+ const withGroupsLabel = !withoutGroupsLabel(filteredGroups);
371
294
  return /*#__PURE__*/React.createElement(Dropdown, {
372
295
  theme: theme,
373
296
  onRequestClose: handleClose,
@@ -380,8 +303,10 @@ var Select = function Select(_ref2) {
380
303
  maxHeight: maxHeight,
381
304
  leastMaxHeight: maxHeight,
382
305
  disableFocusLock: true,
383
- trigger: function trigger(_ref4) {
384
- var ref = _ref4.ref;
306
+ trigger: _ref4 => {
307
+ let {
308
+ ref
309
+ } = _ref4;
385
310
  return /*#__PURE__*/React.createElement(FormTriggerWrapper, {
386
311
  ref: setTriggerFormWrapperRef,
387
312
  className: cn(className, SIZE_STYLES[size]),
@@ -426,18 +351,16 @@ var Select = function Select(_ref2) {
426
351
  disableCursor: isKeyboardNavigating,
427
352
  className: cn(styles.menu, withGroupsLabel && styles.withGroupsLabel, MENU_SIZE_STYLES[actualSize]),
428
353
  enableKeyboardNavigation: false
429
- }, !isEmptyMenu ? _mapInstanceProperty(_context5 = getPlainOptionsFromGroups(filteredGroups)).call(_context5, function (item, i, collection) {
430
- return /*#__PURE__*/React.createElement(Option, {
431
- item: item,
432
- isLastItem: i === collection.length - 1,
433
- key: getOptionKey(item, i),
434
- selected: value,
435
- cursorIndex: cursorIndex,
436
- highlightedItemRef: highlightedItemRef,
437
- handleSelectIndex: handleSelectIndex,
438
- handleHighlightIndex: handleHighlightIndex
439
- });
440
- }) : /*#__PURE__*/React.createElement(MenuItem, {
354
+ }, !isEmptyMenu ? getPlainOptionsFromGroups(filteredGroups).map((item, i, collection) => /*#__PURE__*/React.createElement(Option, {
355
+ item: item,
356
+ isLastItem: i === collection.length - 1,
357
+ key: getOptionKey(item, i),
358
+ selected: value,
359
+ cursorIndex: cursorIndex,
360
+ highlightedItemRef: highlightedItemRef,
361
+ handleSelectIndex: handleSelectIndex,
362
+ handleHighlightIndex: handleHighlightIndex
363
+ })) : /*#__PURE__*/React.createElement(MenuItem, {
441
364
  disabled: true
442
365
  }, getEmptyMessage(isLoading, isSearchable && searchString.length > 0, noOptionsMessage))));
443
366
  };