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.
@@ -9,7 +9,7 @@ var _inheritsLoose = require('@babel/runtime/helpers/inheritsLoose');
9
9
  var PropTypes = require('prop-types');
10
10
  var React = require('react');
11
11
  var reactIs = require('react-is');
12
- var compute = require('compute-scroll-into-view');
12
+ var computeScrollIntoView = require('compute-scroll-into-view');
13
13
  var tslib = require('tslib');
14
14
 
15
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -20,7 +20,6 @@ var _assertThisInitialized__default = /*#__PURE__*/_interopDefaultLegacy(_assert
20
20
  var _inheritsLoose__default = /*#__PURE__*/_interopDefaultLegacy(_inheritsLoose);
21
21
  var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
22
22
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
23
- var compute__default = /*#__PURE__*/_interopDefaultLegacy(compute);
24
23
 
25
24
  var idCounter = 0;
26
25
 
@@ -46,7 +45,7 @@ function scrollIntoView(node, menuNode) {
46
45
  if (!node) {
47
46
  return;
48
47
  }
49
- var actions = compute__default["default"](node, {
48
+ var actions = computeScrollIntoView.compute(node, {
50
49
  boundary: menuNode,
51
50
  block: 'nearest',
52
51
  scrollMode: 'if-needed'
@@ -382,7 +381,7 @@ function targetWithinDownshift(target, downshiftElements, environment, checkActi
382
381
  if (checkActiveElement === void 0) {
383
382
  checkActiveElement = true;
384
383
  }
385
- return downshiftElements.some(function (contextNode) {
384
+ return environment && downshiftElements.some(function (contextNode) {
386
385
  return contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment));
387
386
  });
388
387
  }
@@ -409,28 +408,12 @@ var cleanupStatus = debounce(function (documentProp) {
409
408
  getStatusDiv(documentProp).textContent = '';
410
409
  }, 500);
411
410
 
412
- /**
413
- * @param {String} status the status message
414
- * @param {Object} documentProp document passed by the user.
415
- */
416
- function setStatus(status, documentProp) {
417
- var div = getStatusDiv(documentProp);
418
- if (!status) {
419
- return;
420
- }
421
- div.textContent = status;
422
- cleanupStatus(documentProp);
423
- }
424
-
425
411
  /**
426
412
  * Get the status node or create it if it does not already exist.
427
413
  * @param {Object} documentProp document passed by the user.
428
414
  * @return {HTMLElement} the status node.
429
415
  */
430
416
  function getStatusDiv(documentProp) {
431
- if (documentProp === void 0) {
432
- documentProp = document;
433
- }
434
417
  var statusDiv = documentProp.getElementById('a11y-status-message');
435
418
  if (statusDiv) {
436
419
  return statusDiv;
@@ -454,6 +437,19 @@ function getStatusDiv(documentProp) {
454
437
  return statusDiv;
455
438
  }
456
439
 
440
+ /**
441
+ * @param {String} status the status message
442
+ * @param {Object} documentProp document passed by the user.
443
+ */
444
+ function setStatus(status, documentProp) {
445
+ if (!status || !documentProp) {
446
+ return;
447
+ }
448
+ var div = getStatusDiv(documentProp);
449
+ div.textContent = status;
450
+ cleanupStatus(documentProp);
451
+ }
452
+
457
453
  var unknown = process.env.NODE_ENV !== "production" ? '__autocomplete_unknown__' : 0;
458
454
  var mouseUp = process.env.NODE_ENV !== "production" ? '__autocomplete_mouseup__' : 1;
459
455
  var itemMouseEnter = process.env.NODE_ENV !== "production" ? '__autocomplete_item_mouseenter__' : 2;
@@ -514,7 +510,6 @@ var Downshift = /*#__PURE__*/function () {
514
510
  _this.getItemId = _this.props.getItemId || function (index) {
515
511
  return _this.id + "-item-" + index;
516
512
  };
517
- _this.input = null;
518
513
  _this.items = [];
519
514
  // itemCount can be changed asynchronously
520
515
  // from within downshift (so it can't come from a prop)
@@ -608,6 +603,7 @@ var Downshift = /*#__PURE__*/function () {
608
603
  _this.props.onInputValueChange(stateToSet.inputValue, _extends__default["default"]({}, _this.getStateAndHelpers(), stateToSet));
609
604
  }
610
605
  return _this.setState(function (state) {
606
+ var _newStateToSet;
611
607
  state = _this.getState(state);
612
608
  var newStateToSet = isStateToSetFunction ? stateToSet(state) : stateToSet;
613
609
 
@@ -626,7 +622,7 @@ var Downshift = /*#__PURE__*/function () {
626
622
  if (isItemSelected && newStateToSet.selectedItem !== state.selectedItem) {
627
623
  onChangeArg = newStateToSet.selectedItem;
628
624
  }
629
- newStateToSet.type = newStateToSet.type || unknown;
625
+ (_newStateToSet = newStateToSet).type || (_newStateToSet.type = unknown);
630
626
  Object.keys(newStateToSet).forEach(function (key) {
631
627
  // onStateChangeArg should only have the state that is
632
628
  // actually changing
@@ -875,8 +871,13 @@ var Downshift = /*#__PURE__*/function () {
875
871
  // handle odd case for Safari and Firefox which
876
872
  // don't give the button the focus properly.
877
873
  /* istanbul ignore if (can't reasonably test this) */
878
- if (_this.props.environment.document.activeElement === _this.props.environment.document.body) {
879
- event.target.focus();
874
+ if (_this.props.environment) {
875
+ var _this$props$environme = _this.props.environment.document,
876
+ body = _this$props$environme.body,
877
+ activeElement = _this$props$environme.activeElement;
878
+ if (body && body === activeElement) {
879
+ event.target.focus();
880
+ }
880
881
  }
881
882
  // to simplify testing components that use downshift, we'll not wrap this in a setTimeout
882
883
  // if the NODE_ENV is test. With the proper build system, this should be dead code eliminated
@@ -898,7 +899,11 @@ var Downshift = /*#__PURE__*/function () {
898
899
  var blurTarget = event.target; // Save blur target for comparison with activeElement later
899
900
  // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not body element
900
901
  _this.internalSetTimeout(function () {
901
- 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)
902
+ if (_this.isMouseDown || !_this.props.environment) {
903
+ return;
904
+ }
905
+ var activeElement = _this.props.environment.document.activeElement;
906
+ 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)
902
907
  ) {
903
908
  _this.reset({
904
909
  type: blurButton
@@ -968,8 +973,13 @@ var Downshift = /*#__PURE__*/function () {
968
973
  _this.inputHandleBlur = function () {
969
974
  // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not the body element
970
975
  _this.internalSetTimeout(function () {
971
- 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);
972
- if (!_this.isMouseDown && !downshiftButtonIsActive) {
976
+ var _activeElement$datase;
977
+ if (_this.isMouseDown || !_this.props.environment) {
978
+ return;
979
+ }
980
+ var activeElement = _this.props.environment.document.activeElement;
981
+ var downshiftButtonIsActive = (activeElement == null || (_activeElement$datase = activeElement.dataset) == null ? void 0 : _activeElement$datase.toggle) && _this._rootNode && _this._rootNode.contains(activeElement);
982
+ if (!downshiftButtonIsActive) {
973
983
  _this.reset({
974
984
  type: blurInput
975
985
  });
@@ -1114,6 +1124,10 @@ var Downshift = /*#__PURE__*/function () {
1114
1124
  }, cb);
1115
1125
  };
1116
1126
  _this.updateStatus = debounce(function () {
1127
+ var _this$props;
1128
+ if (!((_this$props = _this.props) != null && (_this$props = _this$props.environment) != null && _this$props.document)) {
1129
+ return;
1130
+ }
1117
1131
  var state = _this.getState();
1118
1132
  var item = _this.items[state.highlightedIndex];
1119
1133
  var resultCount = _this.getItemCount();
@@ -1126,17 +1140,17 @@ var Downshift = /*#__PURE__*/function () {
1126
1140
  _this.previousResultCount = resultCount;
1127
1141
  setStatus(status, _this.props.environment.document);
1128
1142
  }, 200);
1129
- var _this$props = _this.props,
1130
- defaultHighlightedIndex = _this$props.defaultHighlightedIndex,
1131
- _this$props$initialHi = _this$props.initialHighlightedIndex,
1132
- _highlightedIndex = _this$props$initialHi === void 0 ? defaultHighlightedIndex : _this$props$initialHi,
1133
- defaultIsOpen = _this$props.defaultIsOpen,
1134
- _this$props$initialIs = _this$props.initialIsOpen,
1135
- _isOpen = _this$props$initialIs === void 0 ? defaultIsOpen : _this$props$initialIs,
1136
- _this$props$initialIn = _this$props.initialInputValue,
1137
- _inputValue = _this$props$initialIn === void 0 ? '' : _this$props$initialIn,
1138
- _this$props$initialSe = _this$props.initialSelectedItem,
1139
- _selectedItem = _this$props$initialSe === void 0 ? null : _this$props$initialSe;
1143
+ var _this$props2 = _this.props,
1144
+ defaultHighlightedIndex = _this$props2.defaultHighlightedIndex,
1145
+ _this$props2$initialH = _this$props2.initialHighlightedIndex,
1146
+ _highlightedIndex = _this$props2$initialH === void 0 ? defaultHighlightedIndex : _this$props2$initialH,
1147
+ defaultIsOpen = _this$props2.defaultIsOpen,
1148
+ _this$props2$initialI = _this$props2.initialIsOpen,
1149
+ _isOpen = _this$props2$initialI === void 0 ? defaultIsOpen : _this$props2$initialI,
1150
+ _this$props2$initialI2 = _this$props2.initialInputValue,
1151
+ _inputValue = _this$props2$initialI2 === void 0 ? '' : _this$props2$initialI2,
1152
+ _this$props2$initialS = _this$props2.initialSelectedItem,
1153
+ _selectedItem = _this$props2$initialS === void 0 ? null : _this$props2$initialS;
1140
1154
  var _state = _this.getState({
1141
1155
  highlightedIndex: _highlightedIndex,
1142
1156
  isOpen: _isOpen,
@@ -1189,7 +1203,7 @@ var Downshift = /*#__PURE__*/function () {
1189
1203
  return itemCount;
1190
1204
  };
1191
1205
  _proto.getItemNodeFromIndex = function getItemNodeFromIndex(index) {
1192
- return this.props.environment.document.getElementById(this.getItemId(index));
1206
+ return this.props.environment ? this.props.environment.document.getElementById(this.getItemId(index)) : null;
1193
1207
  };
1194
1208
  _proto.scrollHighlightedItemIntoView = function scrollHighlightedItemIntoView() {
1195
1209
  /* istanbul ignore else (react-native) */
@@ -1276,8 +1290,12 @@ var Downshift = /*#__PURE__*/function () {
1276
1290
  validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
1277
1291
  }
1278
1292
 
1279
- /* istanbul ignore if (react-native) */
1280
- {
1293
+ /* istanbul ignore if (react-native or SSR) */
1294
+ if (!this.props.environment) {
1295
+ this.cleanup = function () {
1296
+ _this4.internalClearTimeouts();
1297
+ };
1298
+ } else {
1281
1299
  // this.isMouseDown helps us track whether the mouse is currently held down.
1282
1300
  // This is useful when the user clicks on an item in the list, but holds the mouse
1283
1301
  // down long enough for the list to disappear (because the blur event fires on the input)
@@ -1446,7 +1464,7 @@ var Downshift = /*#__PURE__*/function () {
1446
1464
  return prevItem !== item;
1447
1465
  },
1448
1466
  environment: /* istanbul ignore next (ssr) */
1449
- typeof window === 'undefined' ? {} : window,
1467
+ typeof window === 'undefined' || false ? undefined : window,
1450
1468
  stateReducer: function stateReducer(state, stateToSet) {
1451
1469
  return stateToSet;
1452
1470
  },
@@ -1733,7 +1751,7 @@ var defaultProps$3 = {
1733
1751
  getA11ySelectionMessage: getA11ySelectionMessage,
1734
1752
  scrollIntoView: scrollIntoView,
1735
1753
  environment: /* istanbul ignore next (ssr) */
1736
- typeof window === 'undefined' ? {} : window
1754
+ typeof window === 'undefined' || false ? undefined : window
1737
1755
  };
1738
1756
  function getDefaultValue$1(props, propKey, defaultStateValues) {
1739
1757
  if (defaultStateValues === void 0) {
@@ -1812,7 +1830,7 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1812
1830
  isTouchMove: false
1813
1831
  });
1814
1832
  React.useEffect(function () {
1815
- if ((environment == null ? void 0 : environment.addEventListener) == null) {
1833
+ if (!environment) {
1816
1834
  return;
1817
1835
  }
1818
1836
 
@@ -1921,7 +1939,7 @@ function useA11yMessageSetter(getA11yMessage, dependencyArray, _ref3) {
1921
1939
  rest = _objectWithoutPropertiesLoose__default["default"](_ref3, _excluded$3);
1922
1940
  // Sets a11y status message on changes in state.
1923
1941
  React.useEffect(function () {
1924
- if (isInitialMount || false) {
1942
+ if (isInitialMount || false || !(environment != null && environment.document)) {
1925
1943
  return;
1926
1944
  }
1927
1945
  updateA11yStatus(function () {
@@ -2118,7 +2136,6 @@ function getItemIndexByCharacterKey(_a) {
2118
2136
  }
2119
2137
  return highlightedIndex;
2120
2138
  }
2121
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2122
2139
  var propTypes$2 = tslib.__assign(tslib.__assign({}, commonDropdownPropTypes), { items: PropTypes__default["default"].array.isRequired, isItemDisabled: PropTypes__default["default"].func, getA11ySelectionMessage: PropTypes__default["default"].func });
2123
2140
  /**
2124
2141
  * Default implementation for status message. Only added when menu is open.
@@ -2309,9 +2326,9 @@ function downshiftSelectReducer(state, action) {
2309
2326
  }
2310
2327
  /* eslint-enable complexity */
2311
2328
 
2312
- var _excluded$2 = ["onMouseLeave", "refKey", "onKeyDown", "onBlur", "ref"],
2329
+ var _excluded$2 = ["onMouseLeave", "refKey", "ref"],
2313
2330
  _excluded2$2 = ["onBlur", "onClick", "onPress", "onKeyDown", "refKey", "ref"],
2314
- _excluded3$1 = ["item", "index", "onMouseMove", "onClick", "onPress", "refKey", "disabled", "ref"];
2331
+ _excluded3$1 = ["item", "index", "onMouseMove", "onClick", "onMouseDown", "onPress", "refKey", "disabled", "ref"];
2315
2332
  useSelect.stateChangeTypes = stateChangeTypes$2;
2316
2333
  function useSelect(userProps) {
2317
2334
  if (userProps === void 0) {
@@ -2580,10 +2597,8 @@ function useSelect(userProps) {
2580
2597
  var _ref = _temp === void 0 ? {} : _temp,
2581
2598
  onMouseLeave = _ref.onMouseLeave,
2582
2599
  _ref$refKey = _ref.refKey,
2583
- refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey;
2584
- _ref.onKeyDown;
2585
- _ref.onBlur;
2586
- var ref = _ref.ref,
2600
+ refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
2601
+ ref = _ref.ref,
2587
2602
  rest = _objectWithoutPropertiesLoose__default["default"](_ref, _excluded$2);
2588
2603
  var _ref2 = _temp2 === void 0 ? {} : _temp2,
2589
2604
  _ref2$suppressRefErro = _ref2.suppressRefError,
@@ -2655,7 +2670,8 @@ function useSelect(userProps) {
2655
2670
  itemProp = _ref5.item,
2656
2671
  indexProp = _ref5.index,
2657
2672
  onMouseMove = _ref5.onMouseMove,
2658
- onClick = _ref5.onClick;
2673
+ onClick = _ref5.onClick,
2674
+ onMouseDown = _ref5.onMouseDown;
2659
2675
  _ref5.onPress;
2660
2676
  var _ref5$refKey = _ref5.refKey,
2661
2677
  refKey = _ref5$refKey === void 0 ? 'ref' : _ref5$refKey,
@@ -2689,6 +2705,10 @@ function useSelect(userProps) {
2689
2705
  index: index
2690
2706
  });
2691
2707
  };
2708
+ var itemHandleMouseDown = function itemHandleMouseDown(e) {
2709
+ return e.preventDefault();
2710
+ }; // keep focus on the toggle after item click select.
2711
+
2692
2712
  var itemProps = _extends__default["default"]((_extends4 = {}, _extends4[refKey] = handleRefs(ref, function (itemNode) {
2693
2713
  if (itemNode) {
2694
2714
  itemRefs.current[elementIds.getItemId(index)] = itemNode;
@@ -2701,6 +2721,7 @@ function useSelect(userProps) {
2701
2721
  }
2702
2722
  }
2703
2723
  itemProps.onMouseMove = callAllEventHandlers(onMouseMove, itemHandleMouseMove);
2724
+ itemProps.onMouseDown = callAllEventHandlers(onMouseDown, itemHandleMouseDown);
2704
2725
  return itemProps;
2705
2726
  }, [latest, elementIds, shouldScrollRef, dispatch]);
2706
2727
  return {
@@ -3077,12 +3098,18 @@ function useCombobox(userProps) {
3077
3098
  }, []);
3078
3099
  // Reset itemRefs on close.
3079
3100
  React.useEffect(function () {
3080
- var _environment$document;
3081
3101
  if (!isOpen) {
3082
3102
  itemRefs.current = {};
3083
- } else if (((_environment$document = environment.document) == null ? void 0 : _environment$document.activeElement) !== inputRef.current) {
3084
- var _inputRef$current;
3085
- inputRef == null || (_inputRef$current = inputRef.current) == null || _inputRef$current.focus();
3103
+ }
3104
+ }, [isOpen]);
3105
+ // Reset itemRefs on close.
3106
+ React.useEffect(function () {
3107
+ var _inputRef$current;
3108
+ if (!isOpen || !(environment != null && environment.document) || !(inputRef != null && (_inputRef$current = inputRef.current) != null && _inputRef$current.focus)) {
3109
+ return;
3110
+ }
3111
+ if (environment.document.activeElement !== inputRef.current) {
3112
+ inputRef.current.focus();
3086
3113
  }
3087
3114
  }, [isOpen, environment]);
3088
3115
 
@@ -3233,7 +3260,8 @@ function useCombobox(userProps) {
3233
3260
  };
3234
3261
  var itemHandleMouseDown = function itemHandleMouseDown(e) {
3235
3262
  return e.preventDefault();
3236
- };
3263
+ }; // keep focus on the input after item click select.
3264
+
3237
3265
  return _extends__default["default"]((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (itemNode) {
3238
3266
  if (itemNode) {
3239
3267
  itemRefs.current[elementIds.getItemId(index)] = itemNode;
@@ -3296,7 +3324,7 @@ function useCombobox(userProps) {
3296
3324
  };
3297
3325
  var inputHandleBlur = function inputHandleBlur(event) {
3298
3326
  /* istanbul ignore else */
3299
- if (latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
3327
+ if (environment != null && environment.document && latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
3300
3328
  var isBlurByTabChange = event.relatedTarget === null && environment.document.activeElement !== environment.document.body;
3301
3329
  dispatch({
3302
3330
  type: InputBlur,
@@ -3675,7 +3703,7 @@ function useMultipleSelection(userProps) {
3675
3703
  // Effects.
3676
3704
  /* Sets a11y status message on changes in selectedItem. */
3677
3705
  React.useEffect(function () {
3678
- if (isInitialMountRef.current || false) {
3706
+ if (isInitialMountRef.current || false || !(environment != null && environment.document)) {
3679
3707
  return;
3680
3708
  }
3681
3709
  if (selectedItems.length < previousSelectedItemsRef.current.length) {