@councilbox/councilbox-ui 2.0.0 → 2.0.2

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.
@@ -23,7 +23,13 @@ var _IconButton = _interopRequireDefault(require("../IconButton/IconButton"));
23
23
 
24
24
  var _useDynamicStyles2 = _interopRequireDefault(require("../../HOCs/useDynamicStyles"));
25
25
 
26
- var _excluded = ["children", "className", "onChange", "onInputChange", "label", "value", "inputValue", "style", "styles", "sx", "prefixClass", "options", "getOptionLabel", "getOptionSelected", "defaultValue", "filterOptions", "freeSolo", "clearOnBlur", "selectOnFocus"];
26
+ var _LoadingSection = _interopRequireDefault(require("../LoadingSection/LoadingSection"));
27
+
28
+ var _autoDetectScroll = require("../../utils/autoDetectScroll");
29
+
30
+ var _utils = require("../../utils");
31
+
32
+ var _excluded = ["children", "className", "onChange", "onInputChange", "label", "value", "inputValue", "style", "styles", "sx", "loadingText", "loading", "prefixClass", "options", "getOptionLabel", "clearable", "getOptionSelected", "defaultValue", "filterOptions", "freeSolo", "InputProps", "clearOnBlur", "selectOnFocus"];
27
33
 
28
34
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
35
 
@@ -62,12 +68,26 @@ var InputAutocomplete = function InputAutocomplete(_ref) {
62
68
  style = _ref.style,
63
69
  styles = _ref.styles,
64
70
  sx = _ref.sx,
71
+ _ref$loadingText = _ref.loadingText,
72
+ loadingText = _ref$loadingText === void 0 ? 'Buscando resultados...' : _ref$loadingText,
73
+ _ref$loading = _ref.loading,
74
+ loading = _ref$loading === void 0 ? false : _ref$loading,
65
75
  propPrefixClass = _ref.prefixClass,
66
76
  options = _ref.options,
67
77
  _ref$getOptionLabel = _ref.getOptionLabel,
68
78
  getOptionLabel = _ref$getOptionLabel === void 0 ? function (option) {
79
+ if (!option) {
80
+ return '';
81
+ }
82
+
83
+ if (typeof option === 'string') {
84
+ return option;
85
+ }
86
+
69
87
  return option.label || option.text || option.translation || option.value;
70
88
  } : _ref$getOptionLabel,
89
+ _ref$clearable = _ref.clearable,
90
+ clearable = _ref$clearable === void 0 ? true : _ref$clearable,
71
91
  _ref$getOptionSelecte = _ref.getOptionSelected,
72
92
  getOptionSelected = _ref$getOptionSelecte === void 0 ? function (option, valueSelected) {
73
93
  return option === valueSelected;
@@ -76,10 +96,12 @@ var InputAutocomplete = function InputAutocomplete(_ref) {
76
96
  filterOptions = _ref.filterOptions,
77
97
  _ref$freeSolo = _ref.freeSolo,
78
98
  freeSolo = _ref$freeSolo === void 0 ? false : _ref$freeSolo,
99
+ _ref$InputProps = _ref.InputProps,
100
+ InputProps = _ref$InputProps === void 0 ? {} : _ref$InputProps,
79
101
  _ref$clearOnBlur = _ref.clearOnBlur,
80
102
  clearOnBlur = _ref$clearOnBlur === void 0 ? true : _ref$clearOnBlur,
81
103
  _ref$selectOnFocus = _ref.selectOnFocus,
82
- selectOnFocus = _ref$selectOnFocus === void 0 ? false : _ref$selectOnFocus,
104
+ selectOnFocus = _ref$selectOnFocus === void 0 ? !freeSolo : _ref$selectOnFocus,
83
105
  props = _objectWithoutProperties(_ref, _excluded);
84
106
 
85
107
  var _useDynamicStyles = (0, _useDynamicStyles2.default)(sx),
@@ -88,130 +110,142 @@ var InputAutocomplete = function InputAutocomplete(_ref) {
88
110
 
89
111
  var prefixClass = propPrefixClass || dynamicPrefixClass;
90
112
 
91
- var _React$useState = _react.default.useState(false),
113
+ var _React$useState = _react.default.useState({
114
+ showDropdown: false,
115
+ showSearchInput: false
116
+ }),
92
117
  _React$useState2 = _slicedToArray(_React$useState, 2),
93
- isOpen = _React$useState2[0],
94
- setIsOpen = _React$useState2[1];
95
-
96
- var _React$useState3 = _react.default.useState(false),
118
+ uiState = _React$useState2[0],
119
+ setUIstate = _React$useState2[1];
120
+
121
+ var _React$useState3 = _react.default.useState({
122
+ top: null,
123
+ bottom: null,
124
+ left: '0px',
125
+ width: '0px'
126
+ }),
97
127
  _React$useState4 = _slicedToArray(_React$useState3, 2),
98
- isClosing = _React$useState4[0],
99
- setIsClosing = _React$useState4[1];
128
+ positions = _React$useState4[0],
129
+ setPositions = _React$useState4[1];
100
130
 
101
- var _React$useState5 = _react.default.useState(value || null),
131
+ var _React$useState5 = _react.default.useState(false),
102
132
  _React$useState6 = _slicedToArray(_React$useState5, 2),
103
- selectedValue = _React$useState6[0],
104
- setSelectedValue = _React$useState6[1];
133
+ isClosing = _React$useState6[0],
134
+ setIsClosing = _React$useState6[1];
105
135
 
106
- var _React$useState7 = _react.default.useState(propInputValue || ''),
136
+ var _React$useState7 = _react.default.useState(value || null),
107
137
  _React$useState8 = _slicedToArray(_React$useState7, 2),
108
- inputValue = _React$useState8[0],
109
- setInputValue = _React$useState8[1];
138
+ internalValue = _React$useState8[0],
139
+ setInternalValue = _React$useState8[1];
110
140
 
111
- var _React$useState9 = _react.default.useState(options),
141
+ var _React$useState9 = _react.default.useState(propInputValue || value && getOptionLabel(value) || ''),
112
142
  _React$useState10 = _slicedToArray(_React$useState9, 2),
113
- filteredOptions = _React$useState10[0],
114
- setFilteredOptions = _React$useState10[1];
143
+ internalInputValue = _React$useState10[0],
144
+ setInternalInputValue = _React$useState10[1];
145
+
146
+ var searchTextRef = _react.default.useRef(freeSolo ? internalInputValue : '');
147
+
148
+ var selectedValue = value !== undefined ? value : internalValue;
149
+ var inputValue = propInputValue !== undefined ? propInputValue : internalInputValue;
115
150
 
116
151
  var selectRef = _react.default.useRef(null);
117
152
 
118
153
  var dropdownRef = _react.default.useRef(null);
119
154
 
120
- var shouldShowDropdown = isOpen && filteredOptions.length > 0;
155
+ var inputRef = _react.default.useRef(null);
121
156
 
122
- _react.default.useEffect(function () {
123
- if (propInputValue !== undefined) {
124
- setInputValue(propInputValue);
125
- }
126
- }, [propInputValue]);
127
-
128
- _react.default.useEffect(function () {
129
- setSelectedValue(value);
130
- }, [value]);
157
+ var optionContainerRef = _react.default.useRef(null);
131
158
 
132
- _react.default.useEffect(function () {
159
+ var filteredOptions = _react.default.useMemo(function () {
133
160
  var newFilteredOptions = options;
161
+ var searchValue = searchTextRef.current;
134
162
 
135
- if (filterOptions) {
136
- newFilteredOptions = filterOptions(options, {
137
- inputValue: inputValue
138
- });
139
- } else {
140
- newFilteredOptions = options.filter(function (option) {
141
- var optionLabel = getOptionLabel(option);
142
- return optionLabel.toLowerCase().includes(inputValue.toLowerCase());
143
- });
163
+ if (searchValue) {
164
+ if (filterOptions) {
165
+ newFilteredOptions = filterOptions(options, {
166
+ inputValue: searchValue
167
+ });
168
+ } else {
169
+ newFilteredOptions = options.filter(function (option) {
170
+ var optionLabel = getOptionLabel(option);
171
+ return optionLabel && searchValue && optionLabel.toLowerCase().includes(searchValue.toLowerCase());
172
+ });
173
+ }
144
174
  }
145
175
 
146
- setFilteredOptions(newFilteredOptions);
147
- }, [inputValue, options]);
176
+ return newFilteredOptions;
177
+ }, [options, searchTextRef.current]);
178
+
179
+ var _useKeyItemNavigation = (0, _utils.useKeyItemNavigation)({
180
+ containerRef: optionContainerRef
181
+ }),
182
+ handleKeyDown = _useKeyItemNavigation.handleKeyDown;
183
+
184
+ var shouldShowDropdown = uiState.showDropdown && filteredOptions.length > 0 && !isClosing || loading;
148
185
 
149
186
  var closeDropdown = function closeDropdown() {
150
187
  setIsClosing(true);
151
188
  setTimeout(function () {
152
- setIsOpen(false);
189
+ setUIstate(_objectSpread(_objectSpread({}, uiState), {}, {
190
+ showDropdown: false,
191
+ showSearchInput: !clearOnBlur
192
+ }));
153
193
  setIsClosing(false);
194
+
195
+ if (!freeSolo) {
196
+ searchTextRef.current = '';
197
+ }
154
198
  }, 150);
155
199
  };
156
200
 
157
- var handleOptionClick = function handleOptionClick(e, option) {
158
- var optionLabel = getOptionLabel(option);
159
-
201
+ var updateInputValue = function updateInputValue(e, newValue) {
160
202
  if (props.inputValue === undefined) {
161
- setInputValue(optionLabel);
203
+ setInternalInputValue(newValue);
162
204
  }
163
205
 
164
- if (props.value === undefined) {
165
- setSelectedValue(option);
206
+ if (onInputChange) {
207
+ onInputChange(e, newValue);
166
208
  }
167
209
 
210
+ searchTextRef.current = newValue;
211
+ };
212
+
213
+ var updateValue = function updateValue(e, newValue) {
168
214
  if (onChange) {
169
- onChange(e, option);
215
+ onChange(e, newValue);
216
+ } else {
217
+ setInternalValue(newValue);
170
218
  }
219
+ };
171
220
 
221
+ var handleOptionClick = function handleOptionClick(e, option) {
222
+ var optionLabel = getOptionLabel(option);
223
+ updateInputValue(e, optionLabel);
224
+ updateValue(e, option);
172
225
  closeDropdown();
173
226
  };
174
227
 
175
228
  var handleInputChange = function handleInputChange(e) {
176
- var newInputValue = e.target.value;
177
-
178
- if (props.inputValue === undefined) {
179
- setInputValue(newInputValue);
180
- }
229
+ var newValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
230
+ var newInputValue = newValue || e.target.value;
181
231
 
182
- if (onInputChange) {
183
- onInputChange(e, newInputValue);
232
+ if (freeSolo) {
233
+ updateValue(e, newInputValue);
184
234
  }
185
235
 
186
- setIsOpen(true);
236
+ updateInputValue(e, newInputValue);
187
237
  };
188
238
 
189
239
  var handleInputBlur = function handleInputBlur(e) {
190
- if (!freeSolo) {
191
- var matchedOption = options.find(function (option) {
192
- return getOptionLabel(option).toLowerCase() === inputValue.toLowerCase();
193
- });
240
+ handleInputChange(e, selectedValue ? getOptionLabel(selectedValue) : '');
194
241
 
195
- if (matchedOption) {
196
- if (props.value === undefined) {
197
- setSelectedValue(matchedOption);
198
- }
199
-
200
- if (props.inputValue === undefined) {
201
- setInputValue(getOptionLabel(matchedOption));
202
- }
203
-
204
- if (onChange) {
205
- onChange(e, matchedOption);
206
- }
207
- } else if (props.inputValue === undefined) {
208
- setInputValue(selectedValue ? getOptionLabel(selectedValue) : '');
242
+ if (clearOnBlur) {
243
+ if (!freeSolo) {
244
+ handleInputChange(e, selectedValue ? getOptionLabel(selectedValue) : '');
209
245
  }
210
246
  }
211
247
 
212
- if (clearOnBlur) {
213
- closeDropdown();
214
- }
248
+ closeDropdown();
215
249
  };
216
250
 
217
251
  var handleInputFocus = function handleInputFocus(e) {
@@ -219,54 +253,58 @@ var InputAutocomplete = function InputAutocomplete(_ref) {
219
253
  e.target.select();
220
254
  }
221
255
 
222
- setIsOpen(true);
256
+ setUIstate(_objectSpread(_objectSpread({}, uiState), {}, {
257
+ showDropdown: true,
258
+ showSearchInput: true
259
+ }));
223
260
  };
224
261
 
225
262
  var updateDropdownPosition = function updateDropdownPosition() {
226
263
  var _selectRef$current, _selectRef$current2, _selectRef$current2$c;
227
264
 
228
- if (selectRef.current && selectRef !== null && selectRef !== void 0 && (_selectRef$current = selectRef.current) !== null && _selectRef$current !== void 0 && _selectRef$current.children[0] && selectRef !== null && selectRef !== void 0 && (_selectRef$current2 = selectRef.current) !== null && _selectRef$current2 !== void 0 && (_selectRef$current2$c = _selectRef$current2.children[0]) !== null && _selectRef$current2$c !== void 0 && _selectRef$current2$c.children[0] && dropdownRef.current) {
265
+ if (selectRef.current && selectRef !== null && selectRef !== void 0 && (_selectRef$current = selectRef.current) !== null && _selectRef$current !== void 0 && _selectRef$current.children[0] && selectRef !== null && selectRef !== void 0 && (_selectRef$current2 = selectRef.current) !== null && _selectRef$current2 !== void 0 && (_selectRef$current2$c = _selectRef$current2.children[0]) !== null && _selectRef$current2$c !== void 0 && _selectRef$current2$c.children[0]) {
229
266
  var _selectRef$current3, _selectRef$current3$c, _selectRef$current3$c2;
230
267
 
231
268
  var selectRect = selectRef === null || selectRef === void 0 ? void 0 : (_selectRef$current3 = selectRef.current) === null || _selectRef$current3 === void 0 ? void 0 : (_selectRef$current3$c = _selectRef$current3.children[0]) === null || _selectRef$current3$c === void 0 ? void 0 : (_selectRef$current3$c2 = _selectRef$current3$c.children[0]) === null || _selectRef$current3$c2 === void 0 ? void 0 : _selectRef$current3$c2.getBoundingClientRect();
232
- var dropdownHeight = dropdownRef.current.offsetHeight + 15;
233
- var isOverflowingBelow = selectRect.bottom + dropdownHeight > window.innerHeight;
234
- var topPosition = isOverflowingBelow ? window.innerHeight - dropdownHeight : selectRect.bottom;
235
- dropdownRef.current.style.top = "".concat(topPosition, "px");
236
- dropdownRef.current.style.left = "".concat(selectRect.left, "px");
237
- dropdownRef.current.style.width = "".concat(selectRect.width, "px");
269
+ var isOverflowingBelow = selectRect.bottom > window.innerHeight * 0.60;
270
+
271
+ var newPositions = _objectSpread({}, positions);
272
+
273
+ if (isOverflowingBelow) {
274
+ newPositions.bottom = "".concat(window.innerHeight - selectRect.top, "px");
275
+ delete newPositions.top;
276
+ } else {
277
+ delete newPositions.bottom;
278
+ newPositions.top = "".concat(selectRect.bottom, "px");
279
+ }
280
+
281
+ newPositions.left = "".concat(selectRect.left, "px");
282
+ newPositions.width = "".concat(selectRect.width, "px");
283
+ setPositions(_objectSpread({}, newPositions));
238
284
  }
239
285
  };
240
286
 
241
287
  _react.default.useEffect(function () {
242
288
  updateDropdownPosition();
243
- }, [isOpen]);
289
+ }, [shouldShowDropdown, document.body.clientHeight > window.innerHeight]);
244
290
 
245
291
  _react.default.useEffect(function () {
246
- var handleResize = function handleResize() {
247
- if (isOpen) {
248
- updateDropdownPosition();
249
- }
250
- };
251
-
252
- window.addEventListener('resize', handleResize);
253
- return function () {
254
- window.removeEventListener('resize', handleResize);
255
- };
256
- }, [isOpen]);
292
+ var referenceEl = (0, _autoDetectScroll.unwrapElement)(inputRef.current);
293
+ var ancestors = (0, _autoDetectScroll.getOverflowAncestors)(referenceEl) || [];
294
+ ancestors.forEach(function (ancestor) {
295
+ ancestor.addEventListener('scroll', updateDropdownPosition, {
296
+ passive: true
297
+ });
298
+ ancestor.addEventListener('resize', updateDropdownPosition);
299
+ });
300
+ }, [inputRef.current]);
257
301
 
258
302
  return /*#__PURE__*/_react.default.createElement("div", {
259
- className: prefixClass("cbx-inputAutocomplete-container ".concat(isOpen ? 'cbx-inputAutocomplete-container-open' : '')),
260
- onClick: function onClick() {
261
- if (isOpen && !inputValue) {
262
- closeDropdown();
263
- } else {
264
- setIsOpen(true);
265
- }
266
- },
303
+ className: prefixClass("cbx-inputAutocomplete-container ".concat(uiState.showSearchInput ? 'cbx-inputAutocomplete-container-open' : '')),
267
304
  style: _objectSpread(_objectSpread(_objectSpread({
268
305
  width: props.fullWidth && '100%'
269
306
  }, styles), style), stylesInLine),
307
+ id: "cbx-prueba",
270
308
  ref: selectRef
271
309
  }, /*#__PURE__*/_react.default.createElement(_InputText.default, _extends({
272
310
  label: label,
@@ -275,48 +313,77 @@ var InputAutocomplete = function InputAutocomplete(_ref) {
275
313
  shrink: !!inputValue || !!selectedValue,
276
314
  prefixClass: prefixClass,
277
315
  fullWidth: props.fullWidth,
316
+ ref: inputRef,
317
+ onKeyDown: function onKeyDown(e) {
318
+ e.stopPropagation();
319
+
320
+ if (e.keyCode === 9) {
321
+ closeDropdown();
322
+ }
323
+
324
+ if (e.key === 'Escape') {
325
+ closeDropdown();
326
+ }
327
+
328
+ handleKeyDown(e);
329
+ },
278
330
  onChange: handleInputChange,
279
- onFocus: handleInputFocus,
280
- onBlur: handleInputBlur,
331
+ onClick: handleInputFocus,
281
332
  defaultValue: defaultValue,
282
- InputProps: {
283
- endAdornment: inputValue && /*#__PURE__*/_react.default.createElement(_IconButton.default, {
333
+ InputProps: _objectSpread({
334
+ endAdornment: clearable && inputValue && (loading ? /*#__PURE__*/_react.default.createElement(_LoadingSection.default, {
335
+ size: 20,
336
+ thickness: 3,
337
+ color: "var(--primary)"
338
+ }) : /*#__PURE__*/_react.default.createElement(_IconButton.default, {
284
339
  prefixClass: prefixClass,
285
340
  className: "cbx-inputAutocomplete-icon-button",
286
341
  onClick: function onClick(e) {
287
342
  e.stopPropagation();
288
343
  e.preventDefault();
289
- setFilteredOptions(options);
290
-
291
- if (props.inputValue === undefined) {
292
- setInputValue('');
293
- }
294
-
295
- if (onInputChange) {
296
- onInputChange(e, '');
297
- }
344
+ searchTextRef.current = '';
345
+ updateInputValue(e, '');
346
+ updateValue(e, null);
347
+ inputRef.current.focus();
298
348
  },
299
349
  icon: /*#__PURE__*/_react.default.createElement("i", {
300
350
  className: "ri-close-fill",
301
351
  tabIndex: "-1"
302
352
  })
303
- })
304
- }
353
+ }))
354
+ }, InputProps)
305
355
  }, props, {
306
356
  type: 'autocomplete'
307
- })), isOpen && /*#__PURE__*/(0, _reactDom.createPortal)( /*#__PURE__*/_react.default.createElement("div", {
357
+ })), (uiState.showDropdown || loading) && (positions.top || positions.bottom) && /*#__PURE__*/(0, _reactDom.createPortal)( /*#__PURE__*/_react.default.createElement("div", {
308
358
  className: prefixClass('cbx-inputAutocomplete-presentation')
309
359
  }, /*#__PURE__*/_react.default.createElement(OutsideClickHandler, {
310
360
  excludedRefs: [selectRef, dropdownRef],
311
361
  onOutsideClick: function onOutsideClick() {
312
- closeDropdown();
362
+ handleInputBlur({
363
+ target: {
364
+ value: searchTextRef.current
365
+ }
366
+ });
313
367
  }
314
- }), /*#__PURE__*/_react.default.createElement("div", {
315
- className: prefixClass("cbx-inputAutocomplete-options-container ".concat(shouldShowDropdown && !isClosing ? 'cbx-inputAutocomplete-options-container-open' : 'cbx-inputAutocomplete-options-container-close')),
368
+ }), shouldShowDropdown && /*#__PURE__*/_react.default.createElement("div", {
369
+ className: prefixClass("cbx-inputAutocomplete-options-container ".concat(uiState.showDropdown && !isClosing ? 'cbx-inputAutocomplete-options-container-open' : 'cbx-inputAutocomplete-options-container-close')),
370
+ style: _objectSpread({}, positions),
371
+ onKeyDown: function onKeyDown(e) {
372
+ if (e.keyCode === 9) {
373
+ closeDropdown();
374
+ }
375
+
376
+ if (e.key === 'Escape') {
377
+ closeDropdown();
378
+ }
379
+
380
+ handleKeyDown(e);
381
+ },
316
382
  ref: dropdownRef
317
383
  }, /*#__PURE__*/_react.default.createElement("ul", {
318
- className: prefixClass('cbx-inputAutocomplete-options')
319
- }, filteredOptions === null || filteredOptions === void 0 ? void 0 : filteredOptions.map(function (item, index) {
384
+ className: prefixClass('cbx-inputAutocomplete-options'),
385
+ ref: optionContainerRef
386
+ }, !loading && (filteredOptions === null || filteredOptions === void 0 ? void 0 : filteredOptions.map(function (item, index) {
320
387
  return /*#__PURE__*/_react.default.createElement(_MenuItem.default, _extends({}, item, {
321
388
  value: item.value || getOptionLabel(item),
322
389
  key: "".concat(item.value || getOptionLabel(item), "-").concat(index),
@@ -326,7 +393,9 @@ var InputAutocomplete = function InputAutocomplete(_ref) {
326
393
  active: getOptionSelected(item, selectedValue),
327
394
  fullWidth: props.fullWidth
328
395
  }), getOptionLabel(item));
329
- })))), document.body));
396
+ })), loading && /*#__PURE__*/_react.default.createElement(_MenuItem.default, {
397
+ fullWidth: props.fullWidth
398
+ }, loadingText)))), document.body));
330
399
  };
331
400
 
332
401
  InputAutocomplete.displayName = "InputAutocomplete";
@@ -345,7 +414,7 @@ var OutsideClickHandler = function OutsideClickHandler(_ref2) {
345
414
  });
346
415
 
347
416
  if (wrapperRef.current && !wrapperRef.current.contains(event.target) && !clickedExcluded) {
348
- onOutsideClick();
417
+ onOutsideClick(event);
349
418
  }
350
419
  };
351
420
 
@@ -23,11 +23,15 @@
23
23
  max-width: 100%;
24
24
  }
25
25
 
26
- .cbx-menuItem:hover {
26
+ .cbx-menuItem:hover, .cbx-menuItem:focus {
27
27
  text-decoration: none;
28
28
  background-color: var(--gray1);
29
29
  }
30
30
 
31
+ .cbx-menuItem:focus {
32
+ outline: none !important;
33
+ }
34
+
31
35
  .cbx-menuItem-container {
32
36
  justify-content: space-between;
33
37
  }
@@ -118,6 +118,10 @@ var MenuItem = function MenuItem(_ref) {
118
118
  onClick: function onClick(e) {
119
119
  return handleClick(e);
120
120
  },
121
+ onKeyDown: function onKeyDown(e) {
122
+ return e.key === 'Enter' && handleClick(e);
123
+ },
124
+ tabIndex: 0,
121
125
  style: _objectSpread(_objectSpread({}, style), stylesInLine),
122
126
  value: "".concat(value)
123
127
  }, props), /*#__PURE__*/_react.default.createElement("div", {
@@ -19,6 +19,8 @@ var _InputText = _interopRequireDefault(require("../InputText/InputText"));
19
19
 
20
20
  var _useDynamicStyles2 = _interopRequireDefault(require("../../HOCs/useDynamicStyles"));
21
21
 
22
+ var _utils = require("../../utils");
23
+
22
24
  var _excluded = ["children", "onChange", "label", "value", "style", "sx", "multiple", "renderValue", "prefixClass", "fullWidth", "width", "disabled"];
23
25
 
24
26
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -103,6 +105,14 @@ var SelectInput = function SelectInput(_ref) {
103
105
 
104
106
  var dropdownRef = _react.default.useRef(null);
105
107
 
108
+ var optionContainerRef = _react.default.useRef(null);
109
+
110
+ var _useKeyItemNavigation = (0, _utils.useKeyItemNavigation)({
111
+ containerRef: optionContainerRef,
112
+ canLoop: false
113
+ }),
114
+ handleKeyDown = _useKeyItemNavigation.handleKeyDown;
115
+
106
116
  if (width === '100%') {
107
117
  fullWidth = true;
108
118
  }
@@ -115,7 +125,7 @@ var SelectInput = function SelectInput(_ref) {
115
125
  }, 150);
116
126
  };
117
127
 
118
- var handleOptionClick = function handleOptionClick(option) {
128
+ var handleOptionClick = function handleOptionClick(option, event) {
119
129
  var optionValue = option.props.value;
120
130
  var optionText = option.props.children;
121
131
 
@@ -138,10 +148,12 @@ var SelectInput = function SelectInput(_ref) {
138
148
  setInternalValue(newSelected);
139
149
 
140
150
  if (onChange) {
141
- onChange({
151
+ onChange(event, {
142
152
  target: {
143
153
  value: newSelected.map(function (opt) {
144
154
  return opt.value;
155
+ }).filter(function (val) {
156
+ return val !== undefined;
145
157
  })
146
158
  }
147
159
  });
@@ -153,7 +165,7 @@ var SelectInput = function SelectInput(_ref) {
153
165
  });
154
166
 
155
167
  if (onChange) {
156
- onChange({
168
+ onChange(event, {
157
169
  target: {
158
170
  value: optionValue
159
171
  }
@@ -214,8 +226,8 @@ var SelectInput = function SelectInput(_ref) {
214
226
  key: child.key || "".concat(child.props.value, "-").concat(index),
215
227
  active: checkSelectedOptions(child.props.value),
216
228
  selected: checkSelectedOptions(child.props.value),
217
- onClick: function onClick() {
218
- return handleOptionClick(child);
229
+ onClick: function onClick(event) {
230
+ return handleOptionClick(child, event);
219
231
  },
220
232
  fullWidth: fullWidth
221
233
  });
@@ -258,8 +270,11 @@ var SelectInput = function SelectInput(_ref) {
258
270
 
259
271
  return /*#__PURE__*/_react.default.createElement("div", {
260
272
  className: prefixClass("cbx-selectInput-container ".concat(isOpen ? 'cbx-selectInput-container-open' : '', " ").concat(fullWidth ? 'cbx-selectInput-container-fullWidth' : '', " ").concat(disabled ? 'cbx-selectInput-container-disabled' : '')),
261
- onClick: function onClick() {
273
+ onClick: function onClick(e) {
274
+ e.preventDefault();
275
+ e.stopPropagation();
262
276
  setIsOpen(!disabled);
277
+ selectRef.current.focus();
263
278
  },
264
279
  style: _objectSpread(_objectSpread({
265
280
  width: width !== '100%' && width
@@ -275,9 +290,29 @@ var SelectInput = function SelectInput(_ref) {
275
290
  fullWidth: fullWidth || width !== '100%',
276
291
  disabled: disabled
277
292
  }, props, {
293
+ onKeyDown: function onKeyDown(e) {
294
+ e.preventDefault();
295
+ e.stopPropagation();
296
+
297
+ if (e.key === 'ArrowDown') {
298
+ setIsOpen(true);
299
+ }
300
+
301
+ if (e.key === 'ArrowUp') {
302
+ e.preventDefault();
303
+ setIsOpen(true);
304
+ }
305
+
306
+ if (e.key === 'Escape') {
307
+ closeDropdown();
308
+ }
309
+
310
+ handleKeyDown(e);
311
+ },
278
312
  type: 'select'
279
313
  })), isOpen && /*#__PURE__*/(0, _reactDom.createPortal)( /*#__PURE__*/_react.default.createElement("div", {
280
- className: prefixClass('cbx-selectInput-presentation')
314
+ className: prefixClass('cbx-selectInput-presentation'),
315
+ onKeyDown: handleKeyDown
281
316
  }, /*#__PURE__*/_react.default.createElement("div", {
282
317
  className: prefixClass('cbx-selectInput-backdrop'),
283
318
  onClick: function onClick() {
@@ -287,7 +322,8 @@ var SelectInput = function SelectInput(_ref) {
287
322
  className: prefixClass("cbx-selectInput-options-container ".concat(isOpen && !isClosing ? 'cbx-selectInput-options-container-open' : 'cbx-selectInput-options-container-close')),
288
323
  ref: dropdownRef
289
324
  }, /*#__PURE__*/_react.default.createElement("ul", {
290
- className: prefixClass('cbx-selectInput-options')
325
+ className: prefixClass('cbx-selectInput-options'),
326
+ ref: optionContainerRef
291
327
  }, enhancedChildren))), document.body));
292
328
  };
293
329