downshift 8.1.0 → 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'
@@ -60,17 +59,6 @@ function scrollIntoView(node, menuNode) {
60
59
  });
61
60
  }
62
61
 
63
- /**
64
- * @param {HTMLElement} parent the parent node
65
- * @param {HTMLElement} child the child node
66
- * @param {Window} environment The window context where downshift renders.
67
- * @return {Boolean} whether the parent is the child or the child is in the parent
68
- */
69
- function isOrContainsNode(parent, child, environment) {
70
- var result = parent === child || child instanceof environment.Node && parent.contains && parent.contains(child);
71
- return result;
72
- }
73
-
74
62
  /**
75
63
  * Simple debounce implementation. Will call the given
76
64
  * function once after the time given has passed since
@@ -368,25 +356,6 @@ function getNonDisabledIndex(start, backwards, items, isItemDisabled, circular)
368
356
  return -1;
369
357
  }
370
358
 
371
- /**
372
- * Checks if event target is within the downshift elements.
373
- *
374
- * @param {EventTarget} target Target to check.
375
- * @param {HTMLElement[]} downshiftElements The elements that form downshift (list, toggle button etc).
376
- * @param {Window} environment The window context where downshift renders.
377
- * @param {boolean} checkActiveElement Whether to also check activeElement.
378
- *
379
- * @returns {boolean} Whether or not the target is within downshift elements.
380
- */
381
- function targetWithinDownshift(target, downshiftElements, environment, checkActiveElement) {
382
- if (checkActiveElement === void 0) {
383
- checkActiveElement = true;
384
- }
385
- return downshiftElements.some(function (contextNode) {
386
- return contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment));
387
- });
388
- }
389
-
390
359
  // eslint-disable-next-line import/no-mutable-exports
391
360
  var validateControlledUnchanged = noop;
392
361
  /* istanbul ignore next */
@@ -409,28 +378,12 @@ var cleanupStatus = debounce(function (documentProp) {
409
378
  getStatusDiv(documentProp).textContent = '';
410
379
  }, 500);
411
380
 
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
381
  /**
426
382
  * Get the status node or create it if it does not already exist.
427
383
  * @param {Object} documentProp document passed by the user.
428
384
  * @return {HTMLElement} the status node.
429
385
  */
430
386
  function getStatusDiv(documentProp) {
431
- if (documentProp === void 0) {
432
- documentProp = document;
433
- }
434
387
  var statusDiv = documentProp.getElementById('a11y-status-message');
435
388
  if (statusDiv) {
436
389
  return statusDiv;
@@ -454,6 +407,19 @@ function getStatusDiv(documentProp) {
454
407
  return statusDiv;
455
408
  }
456
409
 
410
+ /**
411
+ * @param {String} status the status message
412
+ * @param {Object} documentProp document passed by the user.
413
+ */
414
+ function setStatus(status, documentProp) {
415
+ if (!status || !documentProp) {
416
+ return;
417
+ }
418
+ var div = getStatusDiv(documentProp);
419
+ div.textContent = status;
420
+ cleanupStatus(documentProp);
421
+ }
422
+
457
423
  var unknown = process.env.NODE_ENV !== "production" ? '__autocomplete_unknown__' : 0;
458
424
  var mouseUp = process.env.NODE_ENV !== "production" ? '__autocomplete_mouseup__' : 1;
459
425
  var itemMouseEnter = process.env.NODE_ENV !== "production" ? '__autocomplete_item_mouseenter__' : 2;
@@ -514,7 +480,6 @@ var Downshift = /*#__PURE__*/function () {
514
480
  _this.getItemId = _this.props.getItemId || function (index) {
515
481
  return _this.id + "-item-" + index;
516
482
  };
517
- _this.input = null;
518
483
  _this.items = [];
519
484
  // itemCount can be changed asynchronously
520
485
  // from within downshift (so it can't come from a prop)
@@ -608,6 +573,7 @@ var Downshift = /*#__PURE__*/function () {
608
573
  _this.props.onInputValueChange(stateToSet.inputValue, _extends__default["default"]({}, _this.getStateAndHelpers(), stateToSet));
609
574
  }
610
575
  return _this.setState(function (state) {
576
+ var _newStateToSet;
611
577
  state = _this.getState(state);
612
578
  var newStateToSet = isStateToSetFunction ? stateToSet(state) : stateToSet;
613
579
 
@@ -626,7 +592,7 @@ var Downshift = /*#__PURE__*/function () {
626
592
  if (isItemSelected && newStateToSet.selectedItem !== state.selectedItem) {
627
593
  onChangeArg = newStateToSet.selectedItem;
628
594
  }
629
- newStateToSet.type = newStateToSet.type || unknown;
595
+ (_newStateToSet = newStateToSet).type || (_newStateToSet.type = unknown);
630
596
  Object.keys(newStateToSet).forEach(function (key) {
631
597
  // onStateChangeArg should only have the state that is
632
598
  // actually changing
@@ -890,7 +856,11 @@ var Downshift = /*#__PURE__*/function () {
890
856
  var blurTarget = event.target; // Save blur target for comparison with activeElement later
891
857
  // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not body element
892
858
  _this.internalSetTimeout(function () {
893
- 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)
859
+ if (_this.isMouseDown || !_this.props.environment) {
860
+ return;
861
+ }
862
+ var activeElement = _this.props.environment.document.activeElement;
863
+ 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)
894
864
  ) {
895
865
  _this.reset({
896
866
  type: blurButton
@@ -975,8 +945,13 @@ var Downshift = /*#__PURE__*/function () {
975
945
  _this.inputHandleBlur = function () {
976
946
  // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not the body element
977
947
  _this.internalSetTimeout(function () {
978
- 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);
979
- if (!_this.isMouseDown && !downshiftButtonIsActive) {
948
+ var _activeElement$datase;
949
+ if (_this.isMouseDown || !_this.props.environment) {
950
+ return;
951
+ }
952
+ var activeElement = _this.props.environment.document.activeElement;
953
+ var downshiftButtonIsActive = (activeElement == null || (_activeElement$datase = activeElement.dataset) == null ? void 0 : _activeElement$datase.toggle) && _this._rootNode && _this._rootNode.contains(activeElement);
954
+ if (!downshiftButtonIsActive) {
980
955
  _this.reset({
981
956
  type: blurInput
982
957
  });
@@ -1121,6 +1096,10 @@ var Downshift = /*#__PURE__*/function () {
1121
1096
  }, cb);
1122
1097
  };
1123
1098
  _this.updateStatus = debounce(function () {
1099
+ var _this$props;
1100
+ if (!((_this$props = _this.props) != null && (_this$props = _this$props.environment) != null && _this$props.document)) {
1101
+ return;
1102
+ }
1124
1103
  var state = _this.getState();
1125
1104
  var item = _this.items[state.highlightedIndex];
1126
1105
  var resultCount = _this.getItemCount();
@@ -1133,17 +1112,17 @@ var Downshift = /*#__PURE__*/function () {
1133
1112
  _this.previousResultCount = resultCount;
1134
1113
  setStatus(status, _this.props.environment.document);
1135
1114
  }, 200);
1136
- var _this$props = _this.props,
1137
- defaultHighlightedIndex = _this$props.defaultHighlightedIndex,
1138
- _this$props$initialHi = _this$props.initialHighlightedIndex,
1139
- _highlightedIndex = _this$props$initialHi === void 0 ? defaultHighlightedIndex : _this$props$initialHi,
1140
- defaultIsOpen = _this$props.defaultIsOpen,
1141
- _this$props$initialIs = _this$props.initialIsOpen,
1142
- _isOpen = _this$props$initialIs === void 0 ? defaultIsOpen : _this$props$initialIs,
1143
- _this$props$initialIn = _this$props.initialInputValue,
1144
- _inputValue = _this$props$initialIn === void 0 ? '' : _this$props$initialIn,
1145
- _this$props$initialSe = _this$props.initialSelectedItem,
1146
- _selectedItem = _this$props$initialSe === void 0 ? null : _this$props$initialSe;
1115
+ var _this$props2 = _this.props,
1116
+ defaultHighlightedIndex = _this$props2.defaultHighlightedIndex,
1117
+ _this$props2$initialH = _this$props2.initialHighlightedIndex,
1118
+ _highlightedIndex = _this$props2$initialH === void 0 ? defaultHighlightedIndex : _this$props2$initialH,
1119
+ defaultIsOpen = _this$props2.defaultIsOpen,
1120
+ _this$props2$initialI = _this$props2.initialIsOpen,
1121
+ _isOpen = _this$props2$initialI === void 0 ? defaultIsOpen : _this$props2$initialI,
1122
+ _this$props2$initialI2 = _this$props2.initialInputValue,
1123
+ _inputValue = _this$props2$initialI2 === void 0 ? '' : _this$props2$initialI2,
1124
+ _this$props2$initialS = _this$props2.initialSelectedItem,
1125
+ _selectedItem = _this$props2$initialS === void 0 ? null : _this$props2$initialS;
1147
1126
  var _state = _this.getState({
1148
1127
  highlightedIndex: _highlightedIndex,
1149
1128
  isOpen: _isOpen,
@@ -1196,7 +1175,7 @@ var Downshift = /*#__PURE__*/function () {
1196
1175
  return itemCount;
1197
1176
  };
1198
1177
  _proto.getItemNodeFromIndex = function getItemNodeFromIndex(index) {
1199
- return this.props.environment.document.getElementById(this.getItemId(index));
1178
+ return this.props.environment ? this.props.environment.document.getElementById(this.getItemId(index)) : null;
1200
1179
  };
1201
1180
  _proto.scrollHighlightedItemIntoView = function scrollHighlightedItemIntoView() {
1202
1181
  };
@@ -1278,7 +1257,7 @@ var Downshift = /*#__PURE__*/function () {
1278
1257
  validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
1279
1258
  }
1280
1259
 
1281
- /* istanbul ignore if (react-native) */
1260
+ /* istanbul ignore if (react-native or SSR) */
1282
1261
  {
1283
1262
  this.cleanup = function () {
1284
1263
  _this4.internalClearTimeouts();
@@ -1384,7 +1363,7 @@ var Downshift = /*#__PURE__*/function () {
1384
1363
  return prevItem !== item;
1385
1364
  },
1386
1365
  environment: /* istanbul ignore next (ssr) */
1387
- typeof window === 'undefined' ? {} : window,
1366
+ typeof window === 'undefined' ? undefined : window,
1388
1367
  stateReducer: function stateReducer(state, stateToSet) {
1389
1368
  return stateToSet;
1390
1369
  },
@@ -1668,7 +1647,7 @@ var defaultProps$3 = {
1668
1647
  getA11ySelectionMessage: getA11ySelectionMessage,
1669
1648
  scrollIntoView: scrollIntoView,
1670
1649
  environment: /* istanbul ignore next (ssr) */
1671
- typeof window === 'undefined' ? {} : window
1650
+ typeof window === 'undefined' ? undefined : window
1672
1651
  };
1673
1652
  function getDefaultValue$1(props, propKey, defaultStateValues) {
1674
1653
  if (defaultStateValues === void 0) {
@@ -1747,50 +1726,9 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1747
1726
  isTouchMove: false
1748
1727
  });
1749
1728
  React.useEffect(function () {
1750
- if ((environment == null ? void 0 : environment.addEventListener) == null) {
1729
+ {
1751
1730
  return;
1752
1731
  }
1753
-
1754
- // The same strategy for checking if a click occurred inside or outside downshift
1755
- // as in downshift.js.
1756
- var onMouseDown = function onMouseDown() {
1757
- mouseAndTouchTrackersRef.current.isMouseDown = true;
1758
- };
1759
- var onMouseUp = function onMouseUp(event) {
1760
- mouseAndTouchTrackersRef.current.isMouseDown = false;
1761
- if (isOpen && !targetWithinDownshift(event.target, downshiftElementRefs.map(function (ref) {
1762
- return ref.current;
1763
- }), environment)) {
1764
- handleBlur();
1765
- }
1766
- };
1767
- var onTouchStart = function onTouchStart() {
1768
- mouseAndTouchTrackersRef.current.isTouchMove = false;
1769
- };
1770
- var onTouchMove = function onTouchMove() {
1771
- mouseAndTouchTrackersRef.current.isTouchMove = true;
1772
- };
1773
- var onTouchEnd = function onTouchEnd(event) {
1774
- if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, downshiftElementRefs.map(function (ref) {
1775
- return ref.current;
1776
- }), environment, false)) {
1777
- handleBlur();
1778
- }
1779
- };
1780
- environment.addEventListener('mousedown', onMouseDown);
1781
- environment.addEventListener('mouseup', onMouseUp);
1782
- environment.addEventListener('touchstart', onTouchStart);
1783
- environment.addEventListener('touchmove', onTouchMove);
1784
- environment.addEventListener('touchend', onTouchEnd);
1785
-
1786
- // eslint-disable-next-line consistent-return
1787
- return function cleanup() {
1788
- environment.removeEventListener('mousedown', onMouseDown);
1789
- environment.removeEventListener('mouseup', onMouseUp);
1790
- environment.removeEventListener('touchstart', onTouchStart);
1791
- environment.removeEventListener('touchmove', onTouchMove);
1792
- environment.removeEventListener('touchend', onTouchEnd);
1793
- };
1794
1732
  // eslint-disable-next-line react-hooks/exhaustive-deps
1795
1733
  }, [isOpen, environment]);
1796
1734
  return mouseAndTouchTrackersRef;
@@ -1856,7 +1794,7 @@ function useA11yMessageSetter(getA11yMessage, dependencyArray, _ref3) {
1856
1794
  rest = _objectWithoutPropertiesLoose__default["default"](_ref3, _excluded$3);
1857
1795
  // Sets a11y status message on changes in state.
1858
1796
  React.useEffect(function () {
1859
- if (isInitialMount || true) {
1797
+ if (isInitialMount || true || !(environment != null && environment.document)) {
1860
1798
  return;
1861
1799
  }
1862
1800
  updateA11yStatus(function () {
@@ -2053,7 +1991,6 @@ function getItemIndexByCharacterKey(_a) {
2053
1991
  }
2054
1992
  return highlightedIndex;
2055
1993
  }
2056
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2057
1994
  var propTypes$2 = tslib.__assign(tslib.__assign({}, commonDropdownPropTypes), { items: PropTypes__default["default"].array.isRequired, isItemDisabled: PropTypes__default["default"].func, getA11ySelectionMessage: PropTypes__default["default"].func });
2058
1995
  /**
2059
1996
  * Default implementation for status message. Only added when menu is open.
@@ -2244,7 +2181,7 @@ function downshiftSelectReducer(state, action) {
2244
2181
  }
2245
2182
  /* eslint-enable complexity */
2246
2183
 
2247
- var _excluded$2 = ["onMouseLeave", "refKey", "onKeyDown", "onBlur", "ref"],
2184
+ var _excluded$2 = ["onMouseLeave", "refKey", "ref"],
2248
2185
  _excluded2$2 = ["onBlur", "onClick", "onPress", "onKeyDown", "refKey", "ref"],
2249
2186
  _excluded3$1 = ["item", "index", "onMouseMove", "onClick", "onPress", "refKey", "disabled", "ref"];
2250
2187
  useSelect.stateChangeTypes = stateChangeTypes$2;
@@ -2363,11 +2300,7 @@ function useSelect(userProps) {
2363
2300
  // eslint-disable-next-line react-hooks/exhaustive-deps
2364
2301
  }, []);
2365
2302
  // Add mouse/touch events to document.
2366
- var mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [menuRef, toggleButtonRef], environment, function () {
2367
- dispatch({
2368
- type: ToggleButtonBlur
2369
- });
2370
- });
2303
+ var mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [menuRef, toggleButtonRef], environment);
2371
2304
  var setGetterPropCallInfo = useGetterPropsCalledChecker('getMenuProps', 'getToggleButtonProps');
2372
2305
  // Make initial ref false.
2373
2306
  React.useEffect(function () {
@@ -2515,10 +2448,8 @@ function useSelect(userProps) {
2515
2448
  var _ref = _temp === void 0 ? {} : _temp,
2516
2449
  onMouseLeave = _ref.onMouseLeave,
2517
2450
  _ref$refKey = _ref.refKey,
2518
- refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey;
2519
- _ref.onKeyDown;
2520
- _ref.onBlur;
2521
- var ref = _ref.ref,
2451
+ refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
2452
+ ref = _ref.ref,
2522
2453
  rest = _objectWithoutPropertiesLoose__default["default"](_ref, _excluded$2);
2523
2454
  var _ref2 = _temp2 === void 0 ? {} : _temp2,
2524
2455
  _ref2$suppressRefErro = _ref2.suppressRefError,
@@ -2984,12 +2915,7 @@ function useCombobox(userProps) {
2984
2915
  previousResultCountRef.current = items.length;
2985
2916
  });
2986
2917
  // Add mouse/touch events to document.
2987
- var mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [inputRef, menuRef, toggleButtonRef], environment, function () {
2988
- dispatch({
2989
- type: InputBlur,
2990
- selectItem: false
2991
- });
2992
- });
2918
+ var mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [inputRef, menuRef, toggleButtonRef], environment);
2993
2919
  var setGetterPropCallInfo = useGetterPropsCalledChecker('getInputProps', 'getMenuProps');
2994
2920
  // Make initial ref false.
2995
2921
  React.useEffect(function () {
@@ -3000,12 +2926,18 @@ function useCombobox(userProps) {
3000
2926
  }, []);
3001
2927
  // Reset itemRefs on close.
3002
2928
  React.useEffect(function () {
3003
- var _environment$document;
3004
2929
  if (!isOpen) {
3005
2930
  itemRefs.current = {};
3006
- } else if (((_environment$document = environment.document) == null ? void 0 : _environment$document.activeElement) !== inputRef.current) {
3007
- var _inputRef$current;
3008
- inputRef == null || (_inputRef$current = inputRef.current) == null ? void 0 : _inputRef$current.focus();
2931
+ }
2932
+ }, [isOpen]);
2933
+ // Reset itemRefs on close.
2934
+ React.useEffect(function () {
2935
+ var _inputRef$current;
2936
+ if (!isOpen || !(environment != null && environment.document) || !(inputRef != null && (_inputRef$current = inputRef.current) != null && _inputRef$current.focus)) {
2937
+ return;
2938
+ }
2939
+ if (environment.document.activeElement !== inputRef.current) {
2940
+ inputRef.current.focus();
3009
2941
  }
3010
2942
  }, [isOpen, environment]);
3011
2943
 
@@ -3219,7 +3151,7 @@ function useCombobox(userProps) {
3219
3151
  };
3220
3152
  var inputHandleBlur = function inputHandleBlur(event) {
3221
3153
  /* istanbul ignore else */
3222
- if (latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
3154
+ if (environment != null && environment.document && latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
3223
3155
  var isBlurByTabChange = event.relatedTarget === null && environment.document.activeElement !== environment.document.body;
3224
3156
  dispatch({
3225
3157
  type: InputBlur,
@@ -3610,7 +3542,7 @@ function useMultipleSelection(userProps) {
3610
3542
  // Effects.
3611
3543
  /* Sets a11y status message on changes in selectedItem. */
3612
3544
  React.useEffect(function () {
3613
- if (isInitialMountRef.current || true) {
3545
+ if (isInitialMountRef.current || true || !(environment != null && environment.document)) {
3614
3546
  return;
3615
3547
  }
3616
3548
  if (selectedItems.length < previousSelectedItemsRef.current.length) {