downshift 8.1.1 → 8.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.
@@ -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
@@ -873,8 +869,13 @@ var Downshift = /*#__PURE__*/function () {
873
869
  // handle odd case for Safari and Firefox which
874
870
  // don't give the button the focus properly.
875
871
  /* istanbul ignore if (can't reasonably test this) */
876
- if (_this.props.environment.document.activeElement === _this.props.environment.document.body) {
877
- event.target.focus();
872
+ if (_this.props.environment) {
873
+ var _this$props$environme = _this.props.environment.document,
874
+ body = _this$props$environme.body,
875
+ activeElement = _this$props$environme.activeElement;
876
+ if (body && body === activeElement) {
877
+ event.target.focus();
878
+ }
878
879
  }
879
880
  // to simplify testing components that use downshift, we'll not wrap this in a setTimeout
880
881
  // if the NODE_ENV is test. With the proper build system, this should be dead code eliminated
@@ -896,7 +897,11 @@ var Downshift = /*#__PURE__*/function () {
896
897
  var blurTarget = event.target; // Save blur target for comparison with activeElement later
897
898
  // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not body element
898
899
  _this.internalSetTimeout(function () {
899
- 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)
900
+ if (_this.isMouseDown || !_this.props.environment) {
901
+ return;
902
+ }
903
+ var activeElement = _this.props.environment.document.activeElement;
904
+ 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)
900
905
  ) {
901
906
  _this.reset({
902
907
  type: blurButton
@@ -966,8 +971,13 @@ var Downshift = /*#__PURE__*/function () {
966
971
  _this.inputHandleBlur = function () {
967
972
  // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not the body element
968
973
  _this.internalSetTimeout(function () {
969
- 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);
970
- if (!_this.isMouseDown && !downshiftButtonIsActive) {
974
+ var _activeElement$datase;
975
+ if (_this.isMouseDown || !_this.props.environment) {
976
+ return;
977
+ }
978
+ var activeElement = _this.props.environment.document.activeElement;
979
+ var downshiftButtonIsActive = (activeElement == null || (_activeElement$datase = activeElement.dataset) == null ? void 0 : _activeElement$datase.toggle) && _this._rootNode && _this._rootNode.contains(activeElement);
980
+ if (!downshiftButtonIsActive) {
971
981
  _this.reset({
972
982
  type: blurInput
973
983
  });
@@ -1112,6 +1122,10 @@ var Downshift = /*#__PURE__*/function () {
1112
1122
  }, cb);
1113
1123
  };
1114
1124
  _this.updateStatus = debounce(function () {
1125
+ var _this$props;
1126
+ if (!((_this$props = _this.props) != null && (_this$props = _this$props.environment) != null && _this$props.document)) {
1127
+ return;
1128
+ }
1115
1129
  var state = _this.getState();
1116
1130
  var item = _this.items[state.highlightedIndex];
1117
1131
  var resultCount = _this.getItemCount();
@@ -1124,17 +1138,17 @@ var Downshift = /*#__PURE__*/function () {
1124
1138
  _this.previousResultCount = resultCount;
1125
1139
  setStatus(status, _this.props.environment.document);
1126
1140
  }, 200);
1127
- var _this$props = _this.props,
1128
- defaultHighlightedIndex = _this$props.defaultHighlightedIndex,
1129
- _this$props$initialHi = _this$props.initialHighlightedIndex,
1130
- _highlightedIndex = _this$props$initialHi === void 0 ? defaultHighlightedIndex : _this$props$initialHi,
1131
- defaultIsOpen = _this$props.defaultIsOpen,
1132
- _this$props$initialIs = _this$props.initialIsOpen,
1133
- _isOpen = _this$props$initialIs === void 0 ? defaultIsOpen : _this$props$initialIs,
1134
- _this$props$initialIn = _this$props.initialInputValue,
1135
- _inputValue = _this$props$initialIn === void 0 ? '' : _this$props$initialIn,
1136
- _this$props$initialSe = _this$props.initialSelectedItem,
1137
- _selectedItem = _this$props$initialSe === void 0 ? null : _this$props$initialSe;
1141
+ var _this$props2 = _this.props,
1142
+ defaultHighlightedIndex = _this$props2.defaultHighlightedIndex,
1143
+ _this$props2$initialH = _this$props2.initialHighlightedIndex,
1144
+ _highlightedIndex = _this$props2$initialH === void 0 ? defaultHighlightedIndex : _this$props2$initialH,
1145
+ defaultIsOpen = _this$props2.defaultIsOpen,
1146
+ _this$props2$initialI = _this$props2.initialIsOpen,
1147
+ _isOpen = _this$props2$initialI === void 0 ? defaultIsOpen : _this$props2$initialI,
1148
+ _this$props2$initialI2 = _this$props2.initialInputValue,
1149
+ _inputValue = _this$props2$initialI2 === void 0 ? '' : _this$props2$initialI2,
1150
+ _this$props2$initialS = _this$props2.initialSelectedItem,
1151
+ _selectedItem = _this$props2$initialS === void 0 ? null : _this$props2$initialS;
1138
1152
  var _state = _this.getState({
1139
1153
  highlightedIndex: _highlightedIndex,
1140
1154
  isOpen: _isOpen,
@@ -1187,7 +1201,7 @@ var Downshift = /*#__PURE__*/function () {
1187
1201
  return itemCount;
1188
1202
  };
1189
1203
  _proto.getItemNodeFromIndex = function getItemNodeFromIndex(index) {
1190
- return this.props.environment.document.getElementById(this.getItemId(index));
1204
+ return this.props.environment ? this.props.environment.document.getElementById(this.getItemId(index)) : null;
1191
1205
  };
1192
1206
  _proto.scrollHighlightedItemIntoView = function scrollHighlightedItemIntoView() {
1193
1207
  /* istanbul ignore else (react-native) */
@@ -1274,8 +1288,12 @@ var Downshift = /*#__PURE__*/function () {
1274
1288
  validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
1275
1289
  }
1276
1290
 
1277
- /* istanbul ignore if (react-native) */
1278
- {
1291
+ /* istanbul ignore if (react-native or SSR) */
1292
+ if (!this.props.environment) {
1293
+ this.cleanup = function () {
1294
+ _this4.internalClearTimeouts();
1295
+ };
1296
+ } else {
1279
1297
  // this.isMouseDown helps us track whether the mouse is currently held down.
1280
1298
  // This is useful when the user clicks on an item in the list, but holds the mouse
1281
1299
  // down long enough for the list to disappear (because the blur event fires on the input)
@@ -1444,7 +1462,7 @@ var Downshift = /*#__PURE__*/function () {
1444
1462
  return prevItem !== item;
1445
1463
  },
1446
1464
  environment: /* istanbul ignore next (ssr) */
1447
- typeof window === 'undefined' ? {} : window,
1465
+ typeof window === 'undefined' ? undefined : window,
1448
1466
  stateReducer: function stateReducer(state, stateToSet) {
1449
1467
  return stateToSet;
1450
1468
  },
@@ -1728,7 +1746,7 @@ var defaultProps$3 = {
1728
1746
  getA11ySelectionMessage: getA11ySelectionMessage,
1729
1747
  scrollIntoView: scrollIntoView,
1730
1748
  environment: /* istanbul ignore next (ssr) */
1731
- typeof window === 'undefined' ? {} : window
1749
+ typeof window === 'undefined' ? undefined : window
1732
1750
  };
1733
1751
  function getDefaultValue$1(props, propKey, defaultStateValues) {
1734
1752
  if (defaultStateValues === void 0) {
@@ -1807,7 +1825,7 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1807
1825
  isTouchMove: false
1808
1826
  });
1809
1827
  React.useEffect(function () {
1810
- if ((environment == null ? void 0 : environment.addEventListener) == null) {
1828
+ if (!environment) {
1811
1829
  return;
1812
1830
  }
1813
1831
 
@@ -1916,7 +1934,7 @@ function useA11yMessageSetter(getA11yMessage, dependencyArray, _ref3) {
1916
1934
  rest = _objectWithoutPropertiesLoose__default["default"](_ref3, _excluded$3);
1917
1935
  // Sets a11y status message on changes in state.
1918
1936
  React.useEffect(function () {
1919
- if (isInitialMount || false) {
1937
+ if (isInitialMount || false || !(environment != null && environment.document)) {
1920
1938
  return;
1921
1939
  }
1922
1940
  updateA11yStatus(function () {
@@ -2113,7 +2131,6 @@ function getItemIndexByCharacterKey(_a) {
2113
2131
  }
2114
2132
  return highlightedIndex;
2115
2133
  }
2116
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2117
2134
  var propTypes$2 = tslib.__assign(tslib.__assign({}, commonDropdownPropTypes), { items: PropTypes__default["default"].array.isRequired, isItemDisabled: PropTypes__default["default"].func, getA11ySelectionMessage: PropTypes__default["default"].func });
2118
2135
  /**
2119
2136
  * Default implementation for status message. Only added when menu is open.
@@ -2304,7 +2321,7 @@ function downshiftSelectReducer(state, action) {
2304
2321
  }
2305
2322
  /* eslint-enable complexity */
2306
2323
 
2307
- var _excluded$2 = ["onMouseLeave", "refKey", "onKeyDown", "onBlur", "ref"],
2324
+ var _excluded$2 = ["onMouseLeave", "refKey", "ref"],
2308
2325
  _excluded2$2 = ["onBlur", "onClick", "onPress", "onKeyDown", "refKey", "ref"],
2309
2326
  _excluded3$1 = ["item", "index", "onMouseMove", "onClick", "onPress", "refKey", "disabled", "ref"];
2310
2327
  useSelect.stateChangeTypes = stateChangeTypes$2;
@@ -2575,10 +2592,8 @@ function useSelect(userProps) {
2575
2592
  var _ref = _temp === void 0 ? {} : _temp,
2576
2593
  onMouseLeave = _ref.onMouseLeave,
2577
2594
  _ref$refKey = _ref.refKey,
2578
- refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey;
2579
- _ref.onKeyDown;
2580
- _ref.onBlur;
2581
- var ref = _ref.ref,
2595
+ refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
2596
+ ref = _ref.ref,
2582
2597
  rest = _objectWithoutPropertiesLoose__default["default"](_ref, _excluded$2);
2583
2598
  var _ref2 = _temp2 === void 0 ? {} : _temp2,
2584
2599
  _ref2$suppressRefErro = _ref2.suppressRefError,
@@ -3060,12 +3075,18 @@ function useCombobox(userProps) {
3060
3075
  }, []);
3061
3076
  // Reset itemRefs on close.
3062
3077
  React.useEffect(function () {
3063
- var _environment$document;
3064
3078
  if (!isOpen) {
3065
3079
  itemRefs.current = {};
3066
- } else if (((_environment$document = environment.document) == null ? void 0 : _environment$document.activeElement) !== inputRef.current) {
3067
- var _inputRef$current;
3068
- inputRef == null || (_inputRef$current = inputRef.current) == null || _inputRef$current.focus();
3080
+ }
3081
+ }, [isOpen]);
3082
+ // Reset itemRefs on close.
3083
+ React.useEffect(function () {
3084
+ var _inputRef$current;
3085
+ if (!isOpen || !(environment != null && environment.document) || !(inputRef != null && (_inputRef$current = inputRef.current) != null && _inputRef$current.focus)) {
3086
+ return;
3087
+ }
3088
+ if (environment.document.activeElement !== inputRef.current) {
3089
+ inputRef.current.focus();
3069
3090
  }
3070
3091
  }, [isOpen, environment]);
3071
3092
 
@@ -3279,7 +3300,7 @@ function useCombobox(userProps) {
3279
3300
  };
3280
3301
  var inputHandleBlur = function inputHandleBlur(event) {
3281
3302
  /* istanbul ignore else */
3282
- if (latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
3303
+ if (environment != null && environment.document && latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
3283
3304
  var isBlurByTabChange = event.relatedTarget === null && environment.document.activeElement !== environment.document.body;
3284
3305
  dispatch({
3285
3306
  type: InputBlur,
@@ -3658,7 +3679,7 @@ function useMultipleSelection(userProps) {
3658
3679
  // Effects.
3659
3680
  /* Sets a11y status message on changes in selectedItem. */
3660
3681
  React.useEffect(function () {
3661
- if (isInitialMountRef.current || false) {
3682
+ if (isInitialMountRef.current || false || !(environment != null && environment.document)) {
3662
3683
  return;
3663
3684
  }
3664
3685
  if (selectedItems.length < previousSelectedItemsRef.current.length) {