downshift 8.1.1 → 8.2.1

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.
@@ -5,7 +5,7 @@ import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose';
5
5
  import PropTypes from 'prop-types';
6
6
  import React, { cloneElement, Component, useRef, useEffect, useCallback, useLayoutEffect, useReducer, useMemo } from 'react';
7
7
  import { isForwardRef } from 'react-is';
8
- import compute from 'compute-scroll-into-view';
8
+ import { compute } from 'compute-scroll-into-view';
9
9
  import { __assign } from 'tslib';
10
10
 
11
11
  var idCounter = 0;
@@ -368,7 +368,7 @@ function targetWithinDownshift(target, downshiftElements, environment, checkActi
368
368
  if (checkActiveElement === void 0) {
369
369
  checkActiveElement = true;
370
370
  }
371
- return downshiftElements.some(function (contextNode) {
371
+ return environment && downshiftElements.some(function (contextNode) {
372
372
  return contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment));
373
373
  });
374
374
  }
@@ -395,28 +395,12 @@ var cleanupStatus = debounce(function (documentProp) {
395
395
  getStatusDiv(documentProp).textContent = '';
396
396
  }, 500);
397
397
 
398
- /**
399
- * @param {String} status the status message
400
- * @param {Object} documentProp document passed by the user.
401
- */
402
- function setStatus(status, documentProp) {
403
- var div = getStatusDiv(documentProp);
404
- if (!status) {
405
- return;
406
- }
407
- div.textContent = status;
408
- cleanupStatus(documentProp);
409
- }
410
-
411
398
  /**
412
399
  * Get the status node or create it if it does not already exist.
413
400
  * @param {Object} documentProp document passed by the user.
414
401
  * @return {HTMLElement} the status node.
415
402
  */
416
403
  function getStatusDiv(documentProp) {
417
- if (documentProp === void 0) {
418
- documentProp = document;
419
- }
420
404
  var statusDiv = documentProp.getElementById('a11y-status-message');
421
405
  if (statusDiv) {
422
406
  return statusDiv;
@@ -440,6 +424,19 @@ function getStatusDiv(documentProp) {
440
424
  return statusDiv;
441
425
  }
442
426
 
427
+ /**
428
+ * @param {String} status the status message
429
+ * @param {Object} documentProp document passed by the user.
430
+ */
431
+ function setStatus(status, documentProp) {
432
+ if (!status || !documentProp) {
433
+ return;
434
+ }
435
+ var div = getStatusDiv(documentProp);
436
+ div.textContent = status;
437
+ cleanupStatus(documentProp);
438
+ }
439
+
443
440
  var unknown = process.env.NODE_ENV !== "production" ? '__autocomplete_unknown__' : 0;
444
441
  var mouseUp = process.env.NODE_ENV !== "production" ? '__autocomplete_mouseup__' : 1;
445
442
  var itemMouseEnter = process.env.NODE_ENV !== "production" ? '__autocomplete_item_mouseenter__' : 2;
@@ -500,7 +497,6 @@ var Downshift = /*#__PURE__*/function () {
500
497
  _this.getItemId = _this.props.getItemId || function (index) {
501
498
  return _this.id + "-item-" + index;
502
499
  };
503
- _this.input = null;
504
500
  _this.items = [];
505
501
  // itemCount can be changed asynchronously
506
502
  // from within downshift (so it can't come from a prop)
@@ -594,6 +590,7 @@ var Downshift = /*#__PURE__*/function () {
594
590
  _this.props.onInputValueChange(stateToSet.inputValue, _extends({}, _this.getStateAndHelpers(), stateToSet));
595
591
  }
596
592
  return _this.setState(function (state) {
593
+ var _newStateToSet;
597
594
  state = _this.getState(state);
598
595
  var newStateToSet = isStateToSetFunction ? stateToSet(state) : stateToSet;
599
596
 
@@ -612,7 +609,7 @@ var Downshift = /*#__PURE__*/function () {
612
609
  if (isItemSelected && newStateToSet.selectedItem !== state.selectedItem) {
613
610
  onChangeArg = newStateToSet.selectedItem;
614
611
  }
615
- newStateToSet.type = newStateToSet.type || unknown;
612
+ (_newStateToSet = newStateToSet).type || (_newStateToSet.type = unknown);
616
613
  Object.keys(newStateToSet).forEach(function (key) {
617
614
  // onStateChangeArg should only have the state that is
618
615
  // actually changing
@@ -861,8 +858,13 @@ var Downshift = /*#__PURE__*/function () {
861
858
  // handle odd case for Safari and Firefox which
862
859
  // don't give the button the focus properly.
863
860
  /* istanbul ignore if (can't reasonably test this) */
864
- if (_this.props.environment.document.activeElement === _this.props.environment.document.body) {
865
- event.target.focus();
861
+ if (_this.props.environment) {
862
+ var _this$props$environme = _this.props.environment.document,
863
+ body = _this$props$environme.body,
864
+ activeElement = _this$props$environme.activeElement;
865
+ if (body && body === activeElement) {
866
+ event.target.focus();
867
+ }
866
868
  }
867
869
  // to simplify testing components that use downshift, we'll not wrap this in a setTimeout
868
870
  // if the NODE_ENV is test. With the proper build system, this should be dead code eliminated
@@ -884,7 +886,11 @@ var Downshift = /*#__PURE__*/function () {
884
886
  var blurTarget = event.target; // Save blur target for comparison with activeElement later
885
887
  // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not body element
886
888
  _this.internalSetTimeout(function () {
887
- if (!_this.isMouseDown && (_this.props.environment.document.activeElement == null || _this.props.environment.document.activeElement.id !== _this.inputId) && _this.props.environment.document.activeElement !== blurTarget // Do nothing if we refocus the same element again (to solve issue in Safari on iOS)
889
+ if (_this.isMouseDown || !_this.props.environment) {
890
+ return;
891
+ }
892
+ var activeElement = _this.props.environment.document.activeElement;
893
+ if ((activeElement == null || activeElement.id !== _this.inputId) && activeElement !== blurTarget // Do nothing if we refocus the same element again (to solve issue in Safari on iOS)
888
894
  ) {
889
895
  _this.reset({
890
896
  type: blurButton
@@ -954,8 +960,13 @@ var Downshift = /*#__PURE__*/function () {
954
960
  _this.inputHandleBlur = function () {
955
961
  // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not the body element
956
962
  _this.internalSetTimeout(function () {
957
- var downshiftButtonIsActive = _this.props.environment.document && !!_this.props.environment.document.activeElement && !!_this.props.environment.document.activeElement.dataset && _this.props.environment.document.activeElement.dataset.toggle && _this._rootNode && _this._rootNode.contains(_this.props.environment.document.activeElement);
958
- if (!_this.isMouseDown && !downshiftButtonIsActive) {
963
+ var _activeElement$datase;
964
+ if (_this.isMouseDown || !_this.props.environment) {
965
+ return;
966
+ }
967
+ var activeElement = _this.props.environment.document.activeElement;
968
+ var downshiftButtonIsActive = (activeElement == null || (_activeElement$datase = activeElement.dataset) == null ? void 0 : _activeElement$datase.toggle) && _this._rootNode && _this._rootNode.contains(activeElement);
969
+ if (!downshiftButtonIsActive) {
959
970
  _this.reset({
960
971
  type: blurInput
961
972
  });
@@ -1100,6 +1111,10 @@ var Downshift = /*#__PURE__*/function () {
1100
1111
  }, cb);
1101
1112
  };
1102
1113
  _this.updateStatus = debounce(function () {
1114
+ var _this$props;
1115
+ if (!((_this$props = _this.props) != null && (_this$props = _this$props.environment) != null && _this$props.document)) {
1116
+ return;
1117
+ }
1103
1118
  var state = _this.getState();
1104
1119
  var item = _this.items[state.highlightedIndex];
1105
1120
  var resultCount = _this.getItemCount();
@@ -1112,17 +1127,17 @@ var Downshift = /*#__PURE__*/function () {
1112
1127
  _this.previousResultCount = resultCount;
1113
1128
  setStatus(status, _this.props.environment.document);
1114
1129
  }, 200);
1115
- var _this$props = _this.props,
1116
- defaultHighlightedIndex = _this$props.defaultHighlightedIndex,
1117
- _this$props$initialHi = _this$props.initialHighlightedIndex,
1118
- _highlightedIndex = _this$props$initialHi === void 0 ? defaultHighlightedIndex : _this$props$initialHi,
1119
- defaultIsOpen = _this$props.defaultIsOpen,
1120
- _this$props$initialIs = _this$props.initialIsOpen,
1121
- _isOpen = _this$props$initialIs === void 0 ? defaultIsOpen : _this$props$initialIs,
1122
- _this$props$initialIn = _this$props.initialInputValue,
1123
- _inputValue = _this$props$initialIn === void 0 ? '' : _this$props$initialIn,
1124
- _this$props$initialSe = _this$props.initialSelectedItem,
1125
- _selectedItem = _this$props$initialSe === void 0 ? null : _this$props$initialSe;
1130
+ var _this$props2 = _this.props,
1131
+ defaultHighlightedIndex = _this$props2.defaultHighlightedIndex,
1132
+ _this$props2$initialH = _this$props2.initialHighlightedIndex,
1133
+ _highlightedIndex = _this$props2$initialH === void 0 ? defaultHighlightedIndex : _this$props2$initialH,
1134
+ defaultIsOpen = _this$props2.defaultIsOpen,
1135
+ _this$props2$initialI = _this$props2.initialIsOpen,
1136
+ _isOpen = _this$props2$initialI === void 0 ? defaultIsOpen : _this$props2$initialI,
1137
+ _this$props2$initialI2 = _this$props2.initialInputValue,
1138
+ _inputValue = _this$props2$initialI2 === void 0 ? '' : _this$props2$initialI2,
1139
+ _this$props2$initialS = _this$props2.initialSelectedItem,
1140
+ _selectedItem = _this$props2$initialS === void 0 ? null : _this$props2$initialS;
1126
1141
  var _state = _this.getState({
1127
1142
  highlightedIndex: _highlightedIndex,
1128
1143
  isOpen: _isOpen,
@@ -1175,7 +1190,7 @@ var Downshift = /*#__PURE__*/function () {
1175
1190
  return itemCount;
1176
1191
  };
1177
1192
  _proto.getItemNodeFromIndex = function getItemNodeFromIndex(index) {
1178
- return this.props.environment.document.getElementById(this.getItemId(index));
1193
+ return this.props.environment ? this.props.environment.document.getElementById(this.getItemId(index)) : null;
1179
1194
  };
1180
1195
  _proto.scrollHighlightedItemIntoView = function scrollHighlightedItemIntoView() {
1181
1196
  /* istanbul ignore else (react-native) */
@@ -1262,8 +1277,12 @@ var Downshift = /*#__PURE__*/function () {
1262
1277
  validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
1263
1278
  }
1264
1279
 
1265
- /* istanbul ignore if (react-native) */
1266
- {
1280
+ /* istanbul ignore if (react-native or SSR) */
1281
+ if (!this.props.environment) {
1282
+ this.cleanup = function () {
1283
+ _this4.internalClearTimeouts();
1284
+ };
1285
+ } else {
1267
1286
  // this.isMouseDown helps us track whether the mouse is currently held down.
1268
1287
  // This is useful when the user clicks on an item in the list, but holds the mouse
1269
1288
  // down long enough for the list to disappear (because the blur event fires on the input)
@@ -1432,7 +1451,7 @@ var Downshift = /*#__PURE__*/function () {
1432
1451
  return prevItem !== item;
1433
1452
  },
1434
1453
  environment: /* istanbul ignore next (ssr) */
1435
- typeof window === 'undefined' ? {} : window,
1454
+ typeof window === 'undefined' || false ? undefined : window,
1436
1455
  stateReducer: function stateReducer(state, stateToSet) {
1437
1456
  return stateToSet;
1438
1457
  },
@@ -1719,7 +1738,7 @@ var defaultProps$3 = {
1719
1738
  getA11ySelectionMessage: getA11ySelectionMessage,
1720
1739
  scrollIntoView: scrollIntoView,
1721
1740
  environment: /* istanbul ignore next (ssr) */
1722
- typeof window === 'undefined' ? {} : window
1741
+ typeof window === 'undefined' || false ? undefined : window
1723
1742
  };
1724
1743
  function getDefaultValue$1(props, propKey, defaultStateValues) {
1725
1744
  if (defaultStateValues === void 0) {
@@ -1798,7 +1817,7 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1798
1817
  isTouchMove: false
1799
1818
  });
1800
1819
  useEffect(function () {
1801
- if ((environment == null ? void 0 : environment.addEventListener) == null) {
1820
+ if (!environment) {
1802
1821
  return;
1803
1822
  }
1804
1823
 
@@ -1907,7 +1926,7 @@ function useA11yMessageSetter(getA11yMessage, dependencyArray, _ref3) {
1907
1926
  rest = _objectWithoutPropertiesLoose(_ref3, _excluded$3);
1908
1927
  // Sets a11y status message on changes in state.
1909
1928
  useEffect(function () {
1910
- if (isInitialMount || false) {
1929
+ if (isInitialMount || false || !(environment != null && environment.document)) {
1911
1930
  return;
1912
1931
  }
1913
1932
  updateA11yStatus(function () {
@@ -2104,7 +2123,6 @@ function getItemIndexByCharacterKey(_a) {
2104
2123
  }
2105
2124
  return highlightedIndex;
2106
2125
  }
2107
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2108
2126
  var propTypes$2 = __assign(__assign({}, commonDropdownPropTypes), { items: PropTypes.array.isRequired, isItemDisabled: PropTypes.func, getA11ySelectionMessage: PropTypes.func });
2109
2127
  /**
2110
2128
  * Default implementation for status message. Only added when menu is open.
@@ -2295,9 +2313,9 @@ function downshiftSelectReducer(state, action) {
2295
2313
  }
2296
2314
  /* eslint-enable complexity */
2297
2315
 
2298
- var _excluded$2 = ["onMouseLeave", "refKey", "onKeyDown", "onBlur", "ref"],
2316
+ var _excluded$2 = ["onMouseLeave", "refKey", "ref"],
2299
2317
  _excluded2$2 = ["onBlur", "onClick", "onPress", "onKeyDown", "refKey", "ref"],
2300
- _excluded3$1 = ["item", "index", "onMouseMove", "onClick", "onPress", "refKey", "disabled", "ref"];
2318
+ _excluded3$1 = ["item", "index", "onMouseMove", "onClick", "onMouseDown", "onPress", "refKey", "disabled", "ref"];
2301
2319
  useSelect.stateChangeTypes = stateChangeTypes$2;
2302
2320
  function useSelect(userProps) {
2303
2321
  if (userProps === void 0) {
@@ -2566,10 +2584,8 @@ function useSelect(userProps) {
2566
2584
  var _ref = _temp === void 0 ? {} : _temp,
2567
2585
  onMouseLeave = _ref.onMouseLeave,
2568
2586
  _ref$refKey = _ref.refKey,
2569
- refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey;
2570
- _ref.onKeyDown;
2571
- _ref.onBlur;
2572
- var ref = _ref.ref,
2587
+ refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
2588
+ ref = _ref.ref,
2573
2589
  rest = _objectWithoutPropertiesLoose(_ref, _excluded$2);
2574
2590
  var _ref2 = _temp2 === void 0 ? {} : _temp2,
2575
2591
  _ref2$suppressRefErro = _ref2.suppressRefError,
@@ -2641,7 +2657,8 @@ function useSelect(userProps) {
2641
2657
  itemProp = _ref5.item,
2642
2658
  indexProp = _ref5.index,
2643
2659
  onMouseMove = _ref5.onMouseMove,
2644
- onClick = _ref5.onClick;
2660
+ onClick = _ref5.onClick,
2661
+ onMouseDown = _ref5.onMouseDown;
2645
2662
  _ref5.onPress;
2646
2663
  var _ref5$refKey = _ref5.refKey,
2647
2664
  refKey = _ref5$refKey === void 0 ? 'ref' : _ref5$refKey,
@@ -2675,6 +2692,10 @@ function useSelect(userProps) {
2675
2692
  index: index
2676
2693
  });
2677
2694
  };
2695
+ var itemHandleMouseDown = function itemHandleMouseDown(e) {
2696
+ return e.preventDefault();
2697
+ }; // keep focus on the toggle after item click select.
2698
+
2678
2699
  var itemProps = _extends((_extends4 = {}, _extends4[refKey] = handleRefs(ref, function (itemNode) {
2679
2700
  if (itemNode) {
2680
2701
  itemRefs.current[elementIds.getItemId(index)] = itemNode;
@@ -2687,6 +2708,7 @@ function useSelect(userProps) {
2687
2708
  }
2688
2709
  }
2689
2710
  itemProps.onMouseMove = callAllEventHandlers(onMouseMove, itemHandleMouseMove);
2711
+ itemProps.onMouseDown = callAllEventHandlers(onMouseDown, itemHandleMouseDown);
2690
2712
  return itemProps;
2691
2713
  }, [latest, elementIds, shouldScrollRef, dispatch]);
2692
2714
  return {
@@ -3063,12 +3085,18 @@ function useCombobox(userProps) {
3063
3085
  }, []);
3064
3086
  // Reset itemRefs on close.
3065
3087
  useEffect(function () {
3066
- var _environment$document;
3067
3088
  if (!isOpen) {
3068
3089
  itemRefs.current = {};
3069
- } else if (((_environment$document = environment.document) == null ? void 0 : _environment$document.activeElement) !== inputRef.current) {
3070
- var _inputRef$current;
3071
- inputRef == null || (_inputRef$current = inputRef.current) == null || _inputRef$current.focus();
3090
+ }
3091
+ }, [isOpen]);
3092
+ // Reset itemRefs on close.
3093
+ useEffect(function () {
3094
+ var _inputRef$current;
3095
+ if (!isOpen || !(environment != null && environment.document) || !(inputRef != null && (_inputRef$current = inputRef.current) != null && _inputRef$current.focus)) {
3096
+ return;
3097
+ }
3098
+ if (environment.document.activeElement !== inputRef.current) {
3099
+ inputRef.current.focus();
3072
3100
  }
3073
3101
  }, [isOpen, environment]);
3074
3102
 
@@ -3219,7 +3247,8 @@ function useCombobox(userProps) {
3219
3247
  };
3220
3248
  var itemHandleMouseDown = function itemHandleMouseDown(e) {
3221
3249
  return e.preventDefault();
3222
- };
3250
+ }; // keep focus on the input after item click select.
3251
+
3223
3252
  return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (itemNode) {
3224
3253
  if (itemNode) {
3225
3254
  itemRefs.current[elementIds.getItemId(index)] = itemNode;
@@ -3282,7 +3311,7 @@ function useCombobox(userProps) {
3282
3311
  };
3283
3312
  var inputHandleBlur = function inputHandleBlur(event) {
3284
3313
  /* istanbul ignore else */
3285
- if (latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
3314
+ if (environment != null && environment.document && latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
3286
3315
  var isBlurByTabChange = event.relatedTarget === null && environment.document.activeElement !== environment.document.body;
3287
3316
  dispatch({
3288
3317
  type: InputBlur,
@@ -3661,7 +3690,7 @@ function useMultipleSelection(userProps) {
3661
3690
  // Effects.
3662
3691
  /* Sets a11y status message on changes in selectedItem. */
3663
3692
  useEffect(function () {
3664
- if (isInitialMountRef.current || false) {
3693
+ if (isInitialMountRef.current || false || !(environment != null && environment.document)) {
3665
3694
  return;
3666
3695
  }
3667
3696
  if (selectedItems.length < previousSelectedItemsRef.current.length) {