@rescui/select 0.1.1 → 0.2.0

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.
@@ -1,4 +1,4 @@
1
1
  var styles = {
2
- "menu": "_menu_o1h6k8_1"
2
+ "menu": "_menu_6jc7fw_1"
3
3
  };
4
4
  export default styles;
@@ -8,6 +8,7 @@ export declare type FormTriggerWrapperProps = {
8
8
  error?: boolean | React.ReactNode;
9
9
  label?: React.ReactNode;
10
10
  note?: React.ReactNode;
11
+ onKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
11
12
  };
12
13
  declare const FormTriggerWrapper: React.FC<FormTriggerWrapperProps>;
13
14
  export default FormTriggerWrapper;
@@ -68,7 +68,8 @@ var FormTriggerWrapper = function FormTriggerWrapper(_ref4) {
68
68
  error = _ref4.error,
69
69
  label = _ref4.label,
70
70
  note = _ref4.note,
71
- children = _ref4.children;
71
+ children = _ref4.children,
72
+ onKeyDown = _ref4.onKeyDown;
72
73
  var theme = useTheme(themeFromProps);
73
74
  return /*#__PURE__*/React.createElement(ThemeProvider, {
74
75
  theme: theme
@@ -77,7 +78,9 @@ var FormTriggerWrapper = function FormTriggerWrapper(_ref4) {
77
78
  "data-test": "select-wrapper"
78
79
  }, /*#__PURE__*/React.createElement(Label, {
79
80
  size: size
80
- }, label), children, /*#__PURE__*/React.createElement(Error, {
81
+ }, label), /*#__PURE__*/React.createElement("div", {
82
+ onKeyDown: onKeyDown
83
+ }, children), /*#__PURE__*/React.createElement(Error, {
81
84
  size: size
82
85
  }, error), /*#__PURE__*/React.createElement(Note, {
83
86
  size: size
@@ -1,9 +1,9 @@
1
1
  var styles = {
2
- "errorMessage": "_errorMessage_8huogz_3",
3
- "sizeL": "_sizeL_8huogz_7",
4
- "label": "_label_8huogz_8",
5
- "note": "_note_8huogz_11",
6
- "sizeM": "_sizeM_8huogz_16",
7
- "sizeS": "_sizeS_8huogz_25"
2
+ "errorMessage": "_errorMessage_mmt5mf_3",
3
+ "sizeL": "_sizeL_mmt5mf_7",
4
+ "label": "_label_mmt5mf_8",
5
+ "note": "_note_mmt5mf_11",
6
+ "sizeM": "_sizeM_mmt5mf_16",
7
+ "sizeS": "_sizeS_mmt5mf_25"
8
8
  };
9
9
  export default styles;
@@ -10,10 +10,6 @@ export interface MultiselectProps {
10
10
  * Array of options
11
11
  */
12
12
  options: SelectOptionOrGroup[];
13
- /**
14
- * Default value for Multiselect state. `onChange` callback is required.
15
- */
16
- defaultValue?: SelectOption[];
17
13
  /**
18
14
  * Is Multiselect preserve single string
19
15
  */
@@ -49,7 +45,7 @@ export interface MultiselectProps {
49
45
  /**
50
46
  * Value of the select
51
47
  */
52
- value?: SelectOption[];
48
+ value: SelectOption[];
53
49
  /**
54
50
  * Change value callback. `value => ...`
55
51
  */
@@ -75,6 +71,7 @@ export interface MultiselectProps {
75
71
  * Text to display when there are no options
76
72
  */
77
73
  noOptionsMessage?: NoOptionsMessage;
74
+ name?: string;
78
75
  }
79
76
  declare const Multiselect: React.FC<MultiselectProps>;
80
77
  export default Multiselect;
@@ -1,39 +1,48 @@
1
+ import _toConsumableArray from "@babel/runtime-corejs3/helpers/esm/toConsumableArray";
1
2
  import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
2
3
  import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties";
3
- var _excluded = ["options", "size", "mode", "theme", "className", "defaultValue", "value", "onChange", "placeholder", "isSingleLine", "isClearable", "isLoading", "isDisabled", "noOptionsMessage", "placement"];
4
+ import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
5
+ var _excluded = ["options", "size", "mode", "theme", "className", "value", "onChange", "placeholder", "isSingleLine", "isClearable", "isLoading", "isDisabled", "noOptionsMessage", "placement"];
6
+
7
+ function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) { symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
8
+
9
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { Object.defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
10
+
11
+ import "core-js/modules/web.dom-collections.for-each.js";
12
+ import "core-js/modules/es.function.name.js";
4
13
  import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
5
14
  import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
6
- import React, { useRef, useState, useReducer, useEffect, useCallback } from 'react';
15
+ import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
16
+ import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
17
+ import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
18
+ import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
19
+ import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
20
+ import React, { useRef, useState, useEffect, useMemo, useCallback } from 'react';
7
21
  import PropTypes from 'prop-types';
8
22
  import scrollIntoView from 'scroll-into-view-if-needed';
9
23
  import { CheckIcon } from '@rescui/icons';
10
24
  import Dropdown from '@rescui/dropdown';
11
25
  import { Menu, MenuItem, Divider, GroupHeader } from '@rescui/menu';
12
26
  import { useTheme } from '@rescui/ui-contexts';
13
- import { convertToOptionGroupsIfNeeded, reverseTheme, getEmptyMessage, shouldOpenSelect, calculateMaxHeight } from '../utils.js';
27
+ import { useStrongMemo, getDefaultCursorIndex, isGroupsEmpty, getOptionValue, getMovedCursorIndex, shouldOpenSelect, isIndexMatchCursor, reverseTheme, getPlainOptionsFromGroups, getEmptyMessage, withoutGroupsLabel, OptionGroupTypes, convertToOptionGroupsIfNeeded, calculateMaxHeight } from '../utils.js';
14
28
  import { optionType, optionsGroupType, noOptionsMessageType } from '../common-prop-types.js';
15
- import { multiselectReducer, getInitialState } from '../state/multiselect/reducer.js';
16
- import { setGroups, closeMenu, openMenu, setCursorIndex, cursorMovedDown, cursorMovedUp, clearSelectedAndCloseMenu } from '../state/select/actions.js';
17
- import { setSelectedValue, setHideSelected, addSelectedIndex, removeSelectedIndex, removeSelectedOption } from '../state/multiselect/actions.js';
18
- import { withoutGroupsLabel, OptionGroupTypes, getOptionValue, isIndexMatchCursor, getCursorIndex } from '../state/select/selectors.js';
19
- import { isEmpty, getSelectedArray, getPlainOptions, isSelected, getGroups } from '../state/multiselect/selectors.js';
20
- import { ESCAPE, BACKSPACE, ARROW_DOWN, ARROW_UP, ENTER, SPACE, TAB } from '../key-codes.js';
29
+ import { ARROW_DOWN, ARROW_UP, ENTER, SPACE, TAB, ESCAPE, BACKSPACE } from '../key-codes.js';
21
30
  import SelectTrigger from './trigger.js';
22
31
  import FormTriggerWrapper from './form-trigger-wrapper.js';
23
- import styles from './common-select.module.pcss.js';
24
- /* eslint-disable no-nested-ternary */
25
- // eslint-disable-next-line complexity
32
+ import styles from './common-select.module.pcss.js'; // eslint-disable-next-line complexity
26
33
 
27
34
  var Option = function Option(_ref) {
28
- var state = _ref.state,
35
+ var groups = _ref.groups,
36
+ selected = _ref.selected,
37
+ cursorIndex = _ref.cursorIndex,
29
38
  size = _ref.size,
30
39
  item = _ref.item,
31
40
  isSingleLine = _ref.isSingleLine,
32
41
  isLastItem = _ref.isLastItem,
33
42
  highlightedItemRef = _ref.highlightedItemRef,
34
43
  handleHighlightIndex = _ref.handleHighlightIndex,
35
- toggleSelectIndex = _ref.toggleSelectIndex;
36
- var marginInPx = withoutGroupsLabel(state) ? 0 : {
44
+ handleSelectIndex = _ref.handleSelectIndex;
45
+ var marginInPx = withoutGroupsLabel(groups) ? 0 : {
37
46
  l: 12,
38
47
  m: 6,
39
48
  s: 0
@@ -43,8 +52,10 @@ var Option = function Option(_ref) {
43
52
  var index = item.index,
44
53
  label = item.label;
45
54
  var value = getOptionValue(item);
46
- var isItemSelected = isSelected(state, value);
47
- var isHighlighted = isIndexMatchCursor(state, index);
55
+ var isItemSelected = Boolean(selected && selected.length && selected.some(function (s) {
56
+ return value === getOptionValue(s);
57
+ }));
58
+ var isHighlighted = isIndexMatchCursor(cursorIndex, index);
48
59
  var icon = isSingleLine && isItemSelected ? /*#__PURE__*/React.createElement(CheckIcon, {
49
60
  size: size
50
61
  }) : null;
@@ -56,7 +67,7 @@ var Option = function Option(_ref) {
56
67
  return handleHighlightIndex(index);
57
68
  },
58
69
  onClick: function onClick() {
59
- return toggleSelectIndex(index);
70
+ return handleSelectIndex(index);
60
71
  },
61
72
  style: {
62
73
  marginBottom: isLastItem ? "".concat(marginInPx, "px") : ''
@@ -98,42 +109,83 @@ var getOptionKey = function getOptionKey(item, i) {
98
109
  return null;
99
110
  };
100
111
 
101
- function callOnChangeIfNeeded(onChange, state, action) {
102
- if (onChange) {
103
- onChange(getSelectedArray(multiselectReducer(state, action)));
112
+ function filterSelected(groups, selected) {
113
+ var selectedValues = {};
114
+
115
+ if (selected) {
116
+ selected.forEach(function (s) {
117
+ selectedValues[getOptionValue(s)] = true;
118
+ });
104
119
  }
120
+
121
+ return _mapInstanceProperty(groups).call(groups, function (g) {
122
+ var newOptions = [];
123
+
124
+ if (g.options) {
125
+ var _context2;
126
+
127
+ newOptions = _filterInstanceProperty(_context2 = g.options).call(_context2, function (opt) {
128
+ return !selectedValues[getOptionValue(opt)];
129
+ });
130
+ }
131
+
132
+ return _objectSpread(_objectSpread({}, g), {}, {
133
+ options: newOptions
134
+ });
135
+ });
105
136
  }
106
137
 
107
- var Multiselect = function Multiselect(_ref2) {
108
- var _context2;
109
-
110
- var options = _ref2.options,
111
- _ref2$size = _ref2.size,
112
- size = _ref2$size === void 0 ? 'm' : _ref2$size,
113
- _ref2$mode = _ref2.mode,
114
- mode = _ref2$mode === void 0 ? 'classic' : _ref2$mode,
115
- themeFromProps = _ref2.theme,
116
- _ref2$className = _ref2.className,
117
- className = _ref2$className === void 0 ? null : _ref2$className,
118
- _ref2$defaultValue = _ref2.defaultValue,
119
- defaultValue = _ref2$defaultValue === void 0 ? [] : _ref2$defaultValue,
120
- _ref2$value = _ref2.value,
121
- value = _ref2$value === void 0 ? undefined : _ref2$value,
122
- onChange = _ref2.onChange,
123
- _ref2$placeholder = _ref2.placeholder,
124
- placeholder = _ref2$placeholder === void 0 ? 'Placeholder' : _ref2$placeholder,
125
- _ref2$isSingleLine = _ref2.isSingleLine,
126
- isSingleLine = _ref2$isSingleLine === void 0 ? false : _ref2$isSingleLine,
127
- _ref2$isClearable = _ref2.isClearable,
128
- isClearable = _ref2$isClearable === void 0 ? false : _ref2$isClearable,
129
- _ref2$isLoading = _ref2.isLoading,
130
- isLoading = _ref2$isLoading === void 0 ? false : _ref2$isLoading,
131
- _ref2$isDisabled = _ref2.isDisabled,
132
- isDisabled = _ref2$isDisabled === void 0 ? false : _ref2$isDisabled,
133
- noOptionsMessage = _ref2.noOptionsMessage,
134
- _ref2$placement = _ref2.placement,
135
- placement = _ref2$placement === void 0 ? 'bottom' : _ref2$placement,
136
- restProps = _objectWithoutProperties(_ref2, _excluded);
138
+ var NativeSelectedOptions = function NativeSelectedOptions(_ref2) {
139
+ var name = _ref2.name,
140
+ selected = _ref2.selected;
141
+
142
+ if (!selected || !Array.isArray(selected) || !selected.length) {
143
+ return /*#__PURE__*/React.createElement("input", {
144
+ type: "hidden",
145
+ name: name,
146
+ value: ""
147
+ });
148
+ }
149
+
150
+ return /*#__PURE__*/React.createElement(React.Fragment, null, _mapInstanceProperty(selected).call(selected, function (_ref3) {
151
+ var value = _ref3.value;
152
+ return /*#__PURE__*/React.createElement("input", {
153
+ key: value,
154
+ type: "hidden",
155
+ name: name,
156
+ value: value
157
+ });
158
+ }));
159
+ };
160
+
161
+ var Multiselect = function Multiselect(_ref4) {
162
+ var _context4;
163
+
164
+ var options = _ref4.options,
165
+ _ref4$size = _ref4.size,
166
+ size = _ref4$size === void 0 ? 'm' : _ref4$size,
167
+ _ref4$mode = _ref4.mode,
168
+ mode = _ref4$mode === void 0 ? 'classic' : _ref4$mode,
169
+ themeFromProps = _ref4.theme,
170
+ _ref4$className = _ref4.className,
171
+ className = _ref4$className === void 0 ? null : _ref4$className,
172
+ _ref4$value = _ref4.value,
173
+ value = _ref4$value === void 0 ? undefined : _ref4$value,
174
+ onChange = _ref4.onChange,
175
+ _ref4$placeholder = _ref4.placeholder,
176
+ placeholder = _ref4$placeholder === void 0 ? 'Placeholder' : _ref4$placeholder,
177
+ _ref4$isSingleLine = _ref4.isSingleLine,
178
+ isSingleLine = _ref4$isSingleLine === void 0 ? false : _ref4$isSingleLine,
179
+ _ref4$isClearable = _ref4.isClearable,
180
+ isClearable = _ref4$isClearable === void 0 ? false : _ref4$isClearable,
181
+ _ref4$isLoading = _ref4.isLoading,
182
+ isLoading = _ref4$isLoading === void 0 ? false : _ref4$isLoading,
183
+ _ref4$isDisabled = _ref4.isDisabled,
184
+ isDisabled = _ref4$isDisabled === void 0 ? false : _ref4$isDisabled,
185
+ noOptionsMessage = _ref4.noOptionsMessage,
186
+ _ref4$placement = _ref4.placement,
187
+ placement = _ref4$placement === void 0 ? 'bottom' : _ref4$placement,
188
+ restProps = _objectWithoutProperties(_ref4, _excluded);
137
189
 
138
190
  var dropdownRef = useRef(null);
139
191
  var highlightedItemRef = useRef();
@@ -145,40 +197,59 @@ var Multiselect = function Multiselect(_ref2) {
145
197
  setIsKeyboardNavigating = _useState2[1];
146
198
 
147
199
  var skipScrollingRef = useRef(false);
200
+ var groups = useStrongMemo(function () {
201
+ return convertToOptionGroupsIfNeeded(options);
202
+ }, [options]);
203
+ var filteredGroups = useStrongMemo(function () {
204
+ if (isSingleLine) {
205
+ return groups;
206
+ }
148
207
 
149
- var _useReducer = useReducer(multiselectReducer, getInitialState(convertToOptionGroupsIfNeeded(options), defaultValue, !isSingleLine)),
150
- _useReducer2 = _slicedToArray(_useReducer, 2),
151
- state = _useReducer2[0],
152
- dispatch = _useReducer2[1];
208
+ return filterSelected(groups, value);
209
+ }, [isSingleLine, value, groups]);
210
+
211
+ var _useState3 = useState(false),
212
+ _useState4 = _slicedToArray(_useState3, 2),
213
+ menuIsOpen = _useState4[0],
214
+ setMenuIsOpen = _useState4[1];
215
+
216
+ var _useState5 = useState(function () {
217
+ return getDefaultCursorIndex(filteredGroups);
218
+ }),
219
+ _useState6 = _slicedToArray(_useState5, 2),
220
+ cursorIndex = _useState6[0],
221
+ setCursorIndex = _useState6[1];
153
222
 
154
223
  useEffect(function () {
155
- dispatch(setGroups(convertToOptionGroupsIfNeeded(options)));
156
- }, [options]);
157
- useEffect(function () {
158
- if (value !== undefined) {
159
- dispatch(setSelectedValue(value));
224
+ setCursorIndex(getDefaultCursorIndex(filteredGroups));
225
+ }, [filteredGroups, value]);
226
+ var isEmptyMenu = useMemo(function () {
227
+ return isGroupsEmpty(filteredGroups);
228
+ }, [filteredGroups]);
229
+ var handleSelectIndex = useCallback(function (index) {
230
+ var _context3;
231
+
232
+ var selectedOption = filteredGroups[index[0]].options[index[1]];
233
+ var selectedOptionValue = getOptionValue(selectedOption);
234
+ var optionAdded = !value.some(function (option) {
235
+ return getOptionValue(option) === selectedOptionValue;
236
+ });
237
+ var newSelected = optionAdded ? _concatInstanceProperty(_context3 = []).call(_context3, _toConsumableArray(value), [selectedOption]) : _filterInstanceProperty(value).call(value, function (option) {
238
+ return getOptionValue(option) !== selectedOptionValue;
239
+ });
240
+
241
+ if (onChange) {
242
+ onChange(newSelected);
160
243
  }
161
- }, [value]);
162
- useEffect(function () {
163
- dispatch(setHideSelected(!isSingleLine));
164
- }, [isSingleLine]);
165
- var isEmptyList = isEmpty(state);
166
- var menuIsOpen = state.menuIsOpen;
167
-
168
- var toggleSelectIndex = function toggleSelectIndex(index) {
169
- var selectedOption = getGroups(state)[index[0]].options[index[1]];
170
- var optionAdded = !isSelected(state, getOptionValue(selectedOption));
171
- var toggleSelectAction = optionAdded ? addSelectedIndex(index) : removeSelectedIndex(index);
172
- dispatch(toggleSelectAction);
173
- setIsKeyboardNavigating(false);
174
- callOnChangeIfNeeded(onChange, state, toggleSelectAction);
175
- };
176
244
 
245
+ setMenuIsOpen(false);
246
+ setIsKeyboardNavigating(false);
247
+ }, [onChange, filteredGroups, value]);
177
248
  useEffect(function () {
178
249
  if (dropdownRef.current) {
179
250
  dropdownRef.current.scheduleUpdate();
180
251
  }
181
- }, [state.searchString, state.selected]);
252
+ }, [value]);
182
253
  var handleMouseMove = useCallback(function () {
183
254
  return setIsKeyboardNavigating(false);
184
255
  }, []);
@@ -198,7 +269,7 @@ var Multiselect = function Multiselect(_ref2) {
198
269
  } else {
199
270
  var elementToScrollTo = highlightedItemRef.current;
200
271
 
201
- if (state.cursorIndex[0] === 0 && state.cursorIndex[1] === 0) {
272
+ if (cursorIndex[0] === 0 && cursorIndex[1] === 0) {
202
273
  var groupHeader = highlightedItemRef.current.previousSibling;
203
274
 
204
275
  if (groupHeader) {
@@ -214,27 +285,23 @@ var Multiselect = function Multiselect(_ref2) {
214
285
  });
215
286
  }
216
287
  }
217
- }, [highlightedItemRef, state.cursorIndex, isKeyboardNavigating]);
218
-
219
- var handleClose = function handleClose() {
220
- dispatch(closeMenu());
288
+ }, [highlightedItemRef, cursorIndex, isKeyboardNavigating]);
289
+ var handleClose = useCallback(function () {
290
+ setMenuIsOpen(false);
221
291
  setIsKeyboardNavigating(false);
222
- }; // eslint-disable-next-line complexity
223
-
224
-
225
- var handleKeyNavigation = function handleKeyNavigation(e) {
292
+ }, []);
293
+ var handleKeyNavigation = useCallback( // eslint-disable-next-line complexity
294
+ function (e) {
226
295
  if (e.key === ARROW_DOWN || e.key === ARROW_UP) {
227
296
  var isDownDirection = e.key === ARROW_DOWN;
228
- dispatch(isDownDirection ? cursorMovedDown() : cursorMovedUp());
297
+ setCursorIndex(getMovedCursorIndex(filteredGroups, cursorIndex, isDownDirection));
229
298
  setIsKeyboardNavigating(true);
230
299
  e.preventDefault();
231
300
  }
232
301
 
233
302
  if (e.key === ENTER || e.key === SPACE) {
234
- var cursorIndex = getCursorIndex(state);
235
-
236
303
  if (cursorIndex !== null) {
237
- toggleSelectIndex(cursorIndex);
304
+ handleSelectIndex(cursorIndex);
238
305
  } else {
239
306
  handleClose();
240
307
  }
@@ -245,25 +312,26 @@ var Multiselect = function Multiselect(_ref2) {
245
312
  if (e.key === TAB) {
246
313
  handleClose();
247
314
  }
248
- };
249
-
250
- var handleClick = function handleClick() {
315
+ }, [filteredGroups, cursorIndex, handleClose, handleSelectIndex]);
316
+ var handleClick = useCallback(function () {
251
317
  if (isDisabled) {
252
318
  return;
253
319
  }
254
320
 
255
- var action = menuIsOpen ? closeMenu() : openMenu();
256
- dispatch(action);
257
- };
258
-
259
- var handleClearClick = function handleClearClick() {
260
- var action = clearSelectedAndCloseMenu();
261
- dispatch(action);
262
- callOnChangeIfNeeded(onChange, state, action);
263
- }; // eslint-disable-next-line complexity
321
+ setMenuIsOpen(function (currentMenuIsOpen) {
322
+ return !currentMenuIsOpen;
323
+ });
324
+ }, [isDisabled]);
325
+ var handleClearClick = useCallback(function () {
326
+ setCursorIndex(getDefaultCursorIndex(groups));
327
+ setMenuIsOpen(false);
264
328
 
329
+ if (onChange) {
330
+ onChange([]);
331
+ }
332
+ }, [groups, onChange]); // eslint-disable-next-line complexity
265
333
 
266
- var handleTriggerKeyDown = function handleTriggerKeyDown(e) {
334
+ var handleTriggerKeyDown = useCallback(function (e) {
267
335
  if (e.key === ESCAPE) {
268
336
  handleClose();
269
337
  e.preventDefault();
@@ -281,7 +349,7 @@ var Multiselect = function Multiselect(_ref2) {
281
349
  var isTriggerTarget = trigger === target;
282
350
 
283
351
  if (shouldOpenSelect(e.key, isTriggerTarget)) {
284
- dispatch(openMenu());
352
+ setMenuIsOpen(true);
285
353
  setIsKeyboardNavigating(true);
286
354
  skipScrollingRef.current = true;
287
355
  trigger.focus();
@@ -294,20 +362,23 @@ var Multiselect = function Multiselect(_ref2) {
294
362
  e.preventDefault();
295
363
  }
296
364
  }
297
- };
365
+ }, [handleClearClick, handleClose, handleKeyNavigation, isClearable, isDisabled, menuIsOpen]);
366
+ var handleUnselectClick = useCallback(function (unselectedOption) {
367
+ var unselectedOptionValue = getOptionValue(unselectedOption);
298
368
 
299
- var handleUnselectClick = function handleUnselectClick(option) {
300
- var action = removeSelectedOption(option);
301
- dispatch(action);
302
- callOnChangeIfNeeded(onChange, state, action);
303
- };
369
+ var newSelected = _filterInstanceProperty(value).call(value, function (option) {
370
+ return getOptionValue(option) !== unselectedOptionValue;
371
+ });
304
372
 
305
- var handleHighlightIndex = function handleHighlightIndex(index) {
306
- if (!isIndexMatchCursor(state, index)) {
307
- dispatch(setCursorIndex(index));
373
+ if (onChange) {
374
+ onChange(newSelected);
308
375
  }
309
- };
310
-
376
+ }, [onChange, value]);
377
+ var handleHighlightIndex = useCallback(function (index) {
378
+ if (!isIndexMatchCursor(cursorIndex, index)) {
379
+ setCursorIndex(index);
380
+ }
381
+ }, [cursorIndex]);
311
382
  var maxHeight = calculateMaxHeight(size);
312
383
  return /*#__PURE__*/React.createElement(Dropdown, {
313
384
  theme: theme,
@@ -320,23 +391,26 @@ var Multiselect = function Multiselect(_ref2) {
320
391
  maxWidth: "trigger",
321
392
  maxHeight: maxHeight,
322
393
  leastMaxHeight: maxHeight,
323
- trigger: function trigger(_ref3) {
324
- var ref = _ref3.ref;
394
+ trigger: function trigger(_ref5) {
395
+ var ref = _ref5.ref;
325
396
  return /*#__PURE__*/React.createElement(FormTriggerWrapper, {
326
397
  className: className,
327
398
  theme: theme,
328
399
  size: size,
329
400
  error: restProps.error,
330
401
  label: restProps.label,
331
- note: restProps.note
332
- }, /*#__PURE__*/React.createElement(SelectTrigger, {
402
+ note: restProps.note,
403
+ onKeyDown: handleTriggerKeyDown
404
+ }, restProps.name && /*#__PURE__*/React.createElement(NativeSelectedOptions, {
405
+ name: restProps.name,
406
+ selected: value
407
+ }), /*#__PURE__*/React.createElement(SelectTrigger, {
333
408
  theme: theme,
334
409
  mode: mode,
335
410
  size: size,
336
411
  isOpen: menuIsOpen,
337
412
  triggerRef: ref,
338
- onKeyDown: handleTriggerKeyDown,
339
- selected: value ? value : getSelectedArray(state),
413
+ selected: value,
340
414
  placeholder: placeholder,
341
415
  isClearable: isClearable,
342
416
  isLoading: isLoading,
@@ -358,16 +432,18 @@ var Multiselect = function Multiselect(_ref2) {
358
432
  disableCursor: isKeyboardNavigating,
359
433
  iconPlacement: 'right',
360
434
  className: styles.menu
361
- }, !isEmptyList ? _mapInstanceProperty(_context2 = getPlainOptions(state)).call(_context2, function (item, i, collection) {
435
+ }, !isEmptyMenu ? _mapInstanceProperty(_context4 = getPlainOptionsFromGroups(filteredGroups)).call(_context4, function (item, i, collection) {
362
436
  return /*#__PURE__*/React.createElement(Option, {
363
437
  key: getOptionKey(item, i),
364
- state: state,
438
+ groups: filteredGroups,
439
+ selected: value,
440
+ cursorIndex: cursorIndex,
365
441
  size: size,
366
442
  item: item,
367
443
  isSingleLine: isSingleLine,
368
444
  isLastItem: i === collection.length - 1,
369
445
  highlightedItemRef: highlightedItemRef,
370
- toggleSelectIndex: toggleSelectIndex,
446
+ handleSelectIndex: handleSelectIndex,
371
447
  handleHighlightIndex: handleHighlightIndex
372
448
  });
373
449
  }) : /*#__PURE__*/React.createElement(MenuItem, {
@@ -378,7 +454,6 @@ var Multiselect = function Multiselect(_ref2) {
378
454
  Multiselect.propTypes = {
379
455
  className: PropTypes.string,
380
456
  options: PropTypes.arrayOf(PropTypes.oneOfType([optionType, optionsGroupType])),
381
- defaultValue: PropTypes.arrayOf(optionType),
382
457
  value: PropTypes.arrayOf(optionType),
383
458
  onChange: PropTypes.func,
384
459
  size: PropTypes.oneOf(['m', 's', 'l']),
@@ -394,6 +469,7 @@ Multiselect.propTypes = {
394
469
  note: PropTypes.node,
395
470
  id: PropTypes.string,
396
471
  noOptionsMessage: noOptionsMessageType,
397
- placement: PropTypes.oneOf(['auto-start', 'auto', 'auto-end', 'top-start', 'top', 'top-end', 'right-start', 'right', 'right-end', 'bottom-end', 'bottom', 'bottom-start', 'left-end', 'left', 'left-start'])
472
+ placement: PropTypes.oneOf(['auto-start', 'auto', 'auto-end', 'top-start', 'top', 'top-end', 'right-start', 'right', 'right-end', 'bottom-end', 'bottom', 'bottom-start', 'left-end', 'left', 'left-start']),
473
+ name: PropTypes.string
398
474
  };
399
475
  export default Multiselect;
@@ -13,8 +13,7 @@ export declare type NativeSelectProps = {
13
13
  theme?: Theme;
14
14
  className?: string;
15
15
  style?: object;
16
- defaultValue?: SelectOption;
17
- value?: SelectOption;
16
+ value: SelectOption;
18
17
  onChange?: (newValue: SelectOption) => void;
19
18
  placeholder?: string;
20
19
  isLoading?: boolean;
@@ -24,6 +23,7 @@ export declare type NativeSelectProps = {
24
23
  note?: React.ReactNode;
25
24
  error?: boolean | React.ReactNode;
26
25
  noOptionsMessage?: NoOptionsMessage;
26
+ name?: string;
27
27
  };
28
28
  declare const NativeSelect: React.FC<NativeSelectProps>;
29
29
  export default NativeSelect;