downshift 5.4.3 → 5.4.7

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.
@@ -350,7 +350,12 @@ function getNextWrappingIndex(moveAmount, baseIndex, itemCount, getItemNodeFromI
350
350
  }
351
351
 
352
352
  var nonDisabledNewIndex = getNextNonDisabledIndex(moveAmount, newIndex, itemCount, getItemNodeFromIndex, circular);
353
- return nonDisabledNewIndex === -1 ? baseIndex : nonDisabledNewIndex;
353
+
354
+ if (nonDisabledNewIndex === -1) {
355
+ return baseIndex >= itemCount ? -1 : baseIndex;
356
+ }
357
+
358
+ return nonDisabledNewIndex;
354
359
  }
355
360
  /**
356
361
  * Returns the next index in the list of an item that is not disabled.
@@ -629,7 +634,7 @@ function () {
629
634
  // See https://github.com/downshift-js/downshift/issues/217 for more info.
630
635
 
631
636
  if (!isStateToSetFunction && stateToSet.hasOwnProperty('inputValue')) {
632
- _this.props.onInputValueChange(stateToSet.inputValue, _extends(_extends({}, _this.getStateAndHelpers()), stateToSet));
637
+ _this.props.onInputValueChange(stateToSet.inputValue, _extends({}, _this.getStateAndHelpers(), stateToSet));
633
638
  }
634
639
 
635
640
  return _this.setState(function (state) {
@@ -679,7 +684,7 @@ function () {
679
684
  // earlier, so we'll call it now that we know what the inputValue state will be.
680
685
 
681
686
  if (isStateToSetFunction && newStateToSet.hasOwnProperty('inputValue')) {
682
- _this.props.onInputValueChange(newStateToSet.inputValue, _extends(_extends({}, _this.getStateAndHelpers()), newStateToSet));
687
+ _this.props.onInputValueChange(newStateToSet.inputValue, _extends({}, _this.getStateAndHelpers(), newStateToSet));
683
688
  }
684
689
 
685
690
  return nextState;
@@ -834,7 +839,7 @@ function () {
834
839
  });
835
840
  }
836
841
  };
837
- _this.buttonKeyDownHandlers = _extends(_extends({}, _this.keyDownHandlers), {}, {
842
+ _this.buttonKeyDownHandlers = _extends({}, _this.keyDownHandlers, {
838
843
  ' ': function _(event) {
839
844
  event.preventDefault();
840
845
  this.toggleMenu({
@@ -842,7 +847,7 @@ function () {
842
847
  });
843
848
  }
844
849
  });
845
- _this.inputKeyDownHandlers = _extends(_extends({}, _this.keyDownHandlers), {}, {
850
+ _this.inputKeyDownHandlers = _extends({}, _this.keyDownHandlers, {
846
851
  Home: function Home(event) {
847
852
  var _this4 = this;
848
853
 
@@ -906,13 +911,13 @@ function () {
906
911
  onBlur: callAllEventHandlers(onBlur, _this.buttonHandleBlur)
907
912
  };
908
913
  var eventHandlers = rest.disabled ? {} : enabledEventHandlers;
909
- return _extends(_extends({
914
+ return _extends({
910
915
  type: 'button',
911
916
  role: 'button',
912
917
  'aria-label': isOpen ? 'close menu' : 'open menu',
913
918
  'aria-haspopup': true,
914
919
  'data-toggle': true
915
- }, eventHandlers), rest);
920
+ }, eventHandlers, rest);
916
921
  };
917
922
 
918
923
  _this.buttonHandleKeyUp = function (event) {
@@ -1004,7 +1009,7 @@ function () {
1004
1009
  /* istanbul ignore if (react-native) */
1005
1010
 
1006
1011
 
1007
- return _extends(_extends({
1012
+ return _extends({
1008
1013
  'aria-autocomplete': 'list',
1009
1014
  'aria-activedescendant': isOpen && typeof highlightedIndex === 'number' && highlightedIndex >= 0 ? _this.getItemId(highlightedIndex) : null,
1010
1015
  'aria-controls': isOpen ? _this.menuId : null,
@@ -1014,7 +1019,7 @@ function () {
1014
1019
  autoComplete: 'off',
1015
1020
  value: inputValue,
1016
1021
  id: _this.inputId
1017
- }, eventHandlers), rest);
1022
+ }, eventHandlers, rest);
1018
1023
  };
1019
1024
 
1020
1025
  _this.inputHandleKeyDown = function (event) {
@@ -1134,11 +1139,11 @@ function () {
1134
1139
  var eventHandlers = rest.disabled ? {
1135
1140
  onMouseDown: enabledEventHandlers.onMouseDown
1136
1141
  } : enabledEventHandlers;
1137
- return _extends(_extends({
1142
+ return _extends({
1138
1143
  id: _this.getItemId(index),
1139
1144
  role: 'option',
1140
1145
  'aria-selected': _this.getState().highlightedIndex === index
1141
- }, eventHandlers), rest);
1146
+ }, eventHandlers, rest);
1142
1147
  };
1143
1148
 
1144
1149
  _this.clearItems = function () {
@@ -1171,11 +1176,11 @@ function () {
1171
1176
 
1172
1177
  _this.internalSetState(function (_ref9) {
1173
1178
  var isOpen = _ref9.isOpen;
1174
- return _extends(_extends({
1179
+ return _extends({
1175
1180
  isOpen: !isOpen
1176
1181
  }, isOpen && {
1177
1182
  highlightedIndex: _this.props.defaultHighlightedIndex
1178
- }), otherStateToSet);
1183
+ }, otherStateToSet);
1179
1184
  }, function () {
1180
1185
  var _this$getState7 = _this.getState(),
1181
1186
  isOpen = _this$getState7.isOpen,
@@ -1823,7 +1828,7 @@ function useEnhancedReducer(reducer, initialState, props) {
1823
1828
  actionRef.current = action;
1824
1829
  state = getState(state, action.props);
1825
1830
  var changes = reducer(state, action);
1826
- var newState = action.props.stateReducer(state, _extends(_extends({}, action), {}, {
1831
+ var newState = action.props.stateReducer(state, _extends({}, action, {
1827
1832
  changes: changes
1828
1833
  }));
1829
1834
  return newState;
@@ -1912,7 +1917,7 @@ function getInitialState(props) {
1912
1917
  var highlightedIndex = getInitialValue(props, 'highlightedIndex');
1913
1918
  var inputValue = getInitialValue(props, 'inputValue');
1914
1919
  return {
1915
- highlightedIndex: highlightedIndex < 0 && selectedItem ? props.items.indexOf(selectedItem) : highlightedIndex,
1920
+ highlightedIndex: highlightedIndex < 0 && selectedItem && isOpen ? props.items.indexOf(selectedItem) : highlightedIndex,
1916
1921
  isOpen: isOpen,
1917
1922
  selectedItem: selectedItem,
1918
1923
  inputValue: inputValue
@@ -2023,6 +2028,9 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
2023
2028
  */
2024
2029
 
2025
2030
  function useGetterPropsCalledChecker() {
2031
+ var isNotProduction = process.env.NODE_ENV !== 'production';
2032
+ var isInitialMountRef = useRef(true);
2033
+
2026
2034
  for (var _len = arguments.length, propKeys = new Array(_len), _key = 0; _key < _len; _key++) {
2027
2035
  propKeys[_key] = arguments[_key];
2028
2036
  }
@@ -2031,33 +2039,32 @@ function useGetterPropsCalledChecker() {
2031
2039
  acc[propKey] = {};
2032
2040
  return acc;
2033
2041
  }, {}));
2042
+ useEffect(function () {
2043
+ if (!isNotProduction) {
2044
+ return;
2045
+ }
2034
2046
 
2035
- if (process.env.NODE_ENV !== 'production') {
2036
2047
  Object.keys(getterPropsCalledRef.current).forEach(function (propKey) {
2037
- getterPropsCalledRef.current[propKey] = null;
2038
- });
2039
- }
2048
+ var propCallInfo = getterPropsCalledRef.current[propKey];
2040
2049
 
2041
- useEffect(function () {
2042
- if (process.env.NODE_ENV !== 'production') {
2043
- Object.keys(getterPropsCalledRef.current).forEach(function (propKey) {
2044
- if (!getterPropsCalledRef.current[propKey]) {
2050
+ if (isInitialMountRef.current) {
2051
+ if (!Object.keys(propCallInfo).length) {
2045
2052
  // eslint-disable-next-line no-console
2046
2053
  console.error("downshift: You forgot to call the " + propKey + " getter function on your component / element.");
2047
2054
  return;
2048
2055
  }
2056
+ }
2049
2057
 
2050
- var _getterPropsCalledRef = getterPropsCalledRef.current[propKey],
2051
- suppressRefError = _getterPropsCalledRef.suppressRefError,
2052
- refKey = _getterPropsCalledRef.refKey,
2053
- elementRef = _getterPropsCalledRef.elementRef;
2058
+ var suppressRefError = propCallInfo.suppressRefError,
2059
+ refKey = propCallInfo.refKey,
2060
+ elementRef = propCallInfo.elementRef;
2054
2061
 
2055
- if ((!elementRef || !elementRef.current) && !suppressRefError) {
2056
- // eslint-disable-next-line no-console
2057
- console.error("downshift: The ref prop \"" + refKey + "\" from " + propKey + " was not applied correctly on your element.");
2058
- }
2059
- });
2060
- }
2062
+ if ((!elementRef || !elementRef.current) && !suppressRefError) {
2063
+ // eslint-disable-next-line no-console
2064
+ console.error("downshift: The ref prop \"" + refKey + "\" from " + propKey + " was not applied correctly on your element.");
2065
+ }
2066
+ });
2067
+ isInitialMountRef.current = false;
2061
2068
  });
2062
2069
  var setGetterPropCallInfo = useCallback(function (propKey, suppressRefError, refKey, elementRef) {
2063
2070
  if (process.env.NODE_ENV !== 'production') {
@@ -2165,7 +2172,7 @@ function getA11yStatusMessage$1(_ref) {
2165
2172
  return '';
2166
2173
  }
2167
2174
 
2168
- var defaultProps$1 = _extends(_extends({}, defaultProps), {}, {
2175
+ var defaultProps$1 = _extends({}, defaultProps, {
2169
2176
  getA11yStatusMessage: getA11yStatusMessage$1
2170
2177
  });
2171
2178
 
@@ -2391,7 +2398,7 @@ function downshiftSelectReducer(state, action) {
2391
2398
  throw new Error('Reducer called without proper action type.');
2392
2399
  }
2393
2400
 
2394
- return _extends(_extends({}, state), changes);
2401
+ return _extends({}, state, changes);
2395
2402
  }
2396
2403
  /* eslint-enable complexity */
2397
2404
 
@@ -2411,7 +2418,7 @@ function useSelect(userProps) {
2411
2418
  } // Props defaults and destructuring.
2412
2419
 
2413
2420
 
2414
- var props = _extends(_extends({}, defaultProps$1), userProps);
2421
+ var props = _extends({}, defaultProps$1, userProps);
2415
2422
 
2416
2423
  var items = props.items,
2417
2424
  scrollIntoView = props.scrollIntoView,
@@ -2727,11 +2734,11 @@ function useSelect(userProps) {
2727
2734
 
2728
2735
  var latestState = latest.current.state;
2729
2736
  setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
2730
- return _extends(_extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
2737
+ return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
2731
2738
  menuRef.current = menuNode;
2732
2739
  }), _extends2.id = elementIdsRef.current.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = elementIdsRef.current.labelId, _extends2.tabIndex = -1, _extends2), latestState.isOpen && latestState.highlightedIndex > -1 && {
2733
2740
  'aria-activedescendant': elementIdsRef.current.getItemId(latestState.highlightedIndex)
2734
- }), {}, {
2741
+ }, {
2735
2742
  onMouseLeave: callAllEventHandlers(onMouseLeave, function menuHandleMouseLeave() {
2736
2743
  dispatch({
2737
2744
  type: MenuMouseLeave
@@ -2962,7 +2969,7 @@ function getInitialState$1(props) {
2962
2969
  inputValue = props.itemToString(selectedItem);
2963
2970
  }
2964
2971
 
2965
- return _extends(_extends({}, initialState), {}, {
2972
+ return _extends({}, initialState, {
2966
2973
  inputValue: inputValue
2967
2974
  });
2968
2975
  }
@@ -3027,20 +3034,21 @@ function useControlledReducer$1(reducer, initialState, props) {
3027
3034
  dispatch = _useEnhancedReducer[1]; // ToDo: if needed, make same approach as selectedItemChanged from Downshift.
3028
3035
 
3029
3036
 
3030
- if (isControlledProp(props, 'selectedItem')) {
3031
- if (previousSelectedItemRef.current !== props.selectedItem) {
3032
- dispatch({
3033
- type: ControlledPropUpdatedSelectedItem,
3034
- inputValue: props.itemToString(props.selectedItem)
3035
- });
3036
- }
3037
-
3038
- previousSelectedItemRef.current = state.selectedItem === previousSelectedItemRef.current ? props.selectedItem : state.selectedItem;
3039
- }
3037
+ useEffect(function () {
3038
+ if (isControlledProp(props, 'selectedItem')) {
3039
+ if (previousSelectedItemRef.current !== props.selectedItem) {
3040
+ dispatch({
3041
+ type: ControlledPropUpdatedSelectedItem,
3042
+ inputValue: props.itemToString(props.selectedItem)
3043
+ });
3044
+ }
3040
3045
 
3046
+ previousSelectedItemRef.current = state.selectedItem === previousSelectedItemRef.current ? props.selectedItem : state.selectedItem;
3047
+ }
3048
+ });
3041
3049
  return [getState(state, props), dispatch];
3042
3050
  }
3043
- var defaultProps$2 = _extends(_extends({}, defaultProps), {}, {
3051
+ var defaultProps$2 = _extends({}, defaultProps, {
3044
3052
  getA11yStatusMessage: getA11yStatusMessage,
3045
3053
  circularNavigation: true
3046
3054
  });
@@ -3205,7 +3213,7 @@ function downshiftUseComboboxReducer(state, action) {
3205
3213
  throw new Error('Reducer called without proper action type.');
3206
3214
  }
3207
3215
 
3208
- return _extends(_extends({}, state), changes);
3216
+ return _extends({}, state, changes);
3209
3217
  }
3210
3218
  /* eslint-enable complexity */
3211
3219
 
@@ -3225,7 +3233,7 @@ function useCombobox(userProps) {
3225
3233
  } // Props defaults and destructuring.
3226
3234
 
3227
3235
 
3228
- var props = _extends(_extends({}, defaultProps$2), userProps);
3236
+ var props = _extends({}, defaultProps$2, userProps);
3229
3237
 
3230
3238
  var initialIsOpen = props.initialIsOpen,
3231
3239
  defaultIsOpen = props.defaultIsOpen,
@@ -3261,8 +3269,7 @@ function useCombobox(userProps) {
3261
3269
 
3262
3270
  var previousResultCountRef = useRef(); // used for checking when props are moving from controlled to uncontrolled.
3263
3271
 
3264
- var prevPropsRef = useRef(props); // used to store information about getter props being called on render.
3265
- // utility callback to get item element.
3272
+ var prevPropsRef = useRef(props); // utility callback to get item element.
3266
3273
 
3267
3274
  var latest = useLatestRef({
3268
3275
  state: state,
@@ -3478,7 +3485,7 @@ function useCombobox(userProps) {
3478
3485
 
3479
3486
  var onSelectKey = 'onClick';
3480
3487
  var customClickHandler = onClick;
3481
- return _extends(_extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (itemNode) {
3488
+ return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (itemNode) {
3482
3489
  if (itemNode) {
3483
3490
  itemRefs.current[elementIdsRef.current.getItemId(itemIndex)] = itemNode;
3484
3491
  }
@@ -3499,7 +3506,11 @@ function useCombobox(userProps) {
3499
3506
  type: ItemClick$1,
3500
3507
  index: index
3501
3508
  });
3502
- }), _ref4)), rest);
3509
+
3510
+ if (inputRef.current) {
3511
+ inputRef.current.focus();
3512
+ }
3513
+ }), _ref4), rest);
3503
3514
  }, [dispatch, latest]);
3504
3515
  var getToggleButtonProps = useCallback(function (_temp4) {
3505
3516
  var _extends4;
@@ -3522,11 +3533,11 @@ function useCombobox(userProps) {
3522
3533
  }
3523
3534
  };
3524
3535
 
3525
- return _extends(_extends((_extends4 = {}, _extends4[refKey] = handleRefs(ref, function (toggleButtonNode) {
3536
+ return _extends((_extends4 = {}, _extends4[refKey] = handleRefs(ref, function (toggleButtonNode) {
3526
3537
  toggleButtonRef.current = toggleButtonNode;
3527
3538
  }), _extends4.id = elementIdsRef.current.toggleButtonId, _extends4.tabIndex = -1, _extends4), !rest.disabled && _extends({}, {
3528
3539
  onClick: callAllEventHandlers(onClick, toggleButtonHandleClick)
3529
- })), rest);
3540
+ }), rest);
3530
3541
  }, [dispatch, latest]);
3531
3542
  var getInputProps = useCallback(function (_temp5, _temp6) {
3532
3543
  var _extends5;
@@ -3586,17 +3597,17 @@ function useCombobox(userProps) {
3586
3597
  /* istanbul ignore if (react-native) */
3587
3598
 
3588
3599
 
3589
- return _extends(_extends(_extends((_extends5 = {}, _extends5[refKey] = handleRefs(ref, function (inputNode) {
3600
+ return _extends((_extends5 = {}, _extends5[refKey] = handleRefs(ref, function (inputNode) {
3590
3601
  inputRef.current = inputNode;
3591
3602
  }), _extends5.id = elementIdsRef.current.inputId, _extends5['aria-autocomplete'] = 'list', _extends5['aria-controls'] = elementIdsRef.current.menuId, _extends5), latestState.isOpen && latestState.highlightedIndex > -1 && {
3592
3603
  'aria-activedescendant': elementIdsRef.current.getItemId(latestState.highlightedIndex)
3593
- }), {}, {
3604
+ }, {
3594
3605
  'aria-labelledby': elementIdsRef.current.labelId,
3595
3606
  // https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
3596
3607
  // revert back since autocomplete="nope" is ignored on latest Chrome and Opera
3597
3608
  autoComplete: 'off',
3598
3609
  value: latestState.inputValue
3599
- }, eventHandlers), rest);
3610
+ }, eventHandlers, rest);
3600
3611
  }, [dispatch, inputKeyDownHandlers, latest, mouseAndTouchTrackersRef, setGetterPropCallInfo]);
3601
3612
  var getComboboxProps = useCallback(function (_temp7, _temp8) {
3602
3613
  var _extends6;
@@ -3949,7 +3960,7 @@ function downshiftMultipleSelectionReducer(state, action) {
3949
3960
  throw new Error('Reducer called without proper action type.');
3950
3961
  }
3951
3962
 
3952
- return _extends(_extends({}, state), changes);
3963
+ return _extends({}, state, changes);
3953
3964
  }
3954
3965
 
3955
3966
  useMultipleSelection.stateChangeTypes = stateChangeTypes$3;
@@ -3960,7 +3971,7 @@ function useMultipleSelection(userProps) {
3960
3971
  }
3961
3972
 
3962
3973
  // Props defaults and destructuring.
3963
- var props = _extends(_extends({}, defaultProps$3), userProps);
3974
+ var props = _extends({}, defaultProps$3, userProps);
3964
3975
 
3965
3976
  var getA11yRemovalMessage = props.getA11yRemovalMessage,
3966
3977
  itemToString = props.itemToString,
@@ -4129,7 +4140,7 @@ function useMultipleSelection(userProps) {
4129
4140
  suppressRefError = _ref5$suppressRefErro === void 0 ? false : _ref5$suppressRefErro;
4130
4141
 
4131
4142
  setGetterPropCallInfo('getDropdownProps', suppressRefError, refKey, dropdownRef);
4132
- return _extends(_extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (dropdownNode) {
4143
+ return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (dropdownNode) {
4133
4144
  if (dropdownNode) {
4134
4145
  dropdownRef.current = dropdownNode;
4135
4146
  }
@@ -4146,7 +4157,7 @@ function useMultipleSelection(userProps) {
4146
4157
  type: DropdownClick
4147
4158
  });
4148
4159
  })
4149
- }), rest);
4160
+ }, rest);
4150
4161
  }, [dispatch, dropdownKeyDownHandlers, setGetterPropCallInfo]); // returns
4151
4162
 
4152
4163
  var addSelectedItem = useCallback(function (selectedItem) {