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.
@@ -2,9 +2,9 @@ import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/esm/objectWith
2
2
  import _extends from '@babel/runtime/helpers/esm/extends';
3
3
  import _assertThisInitialized from '@babel/runtime/helpers/esm/assertThisInitialized';
4
4
  import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose';
5
- import React, { cloneElement, Component, useRef, useEffect, useCallback, useLayoutEffect, useReducer, useMemo } from 'preact';
5
+ import React, { cloneElement, Component, useRef, useEffect, useCallback, useLayoutEffect, useReducer, useMemo } from 'preact/compat';
6
6
  import { isForwardRef } from 'react-is';
7
- import compute from 'compute-scroll-into-view';
7
+ import { compute } from 'compute-scroll-into-view';
8
8
  import PropTypes from 'prop-types';
9
9
  import { __assign } from 'tslib';
10
10
 
@@ -375,7 +375,7 @@ function targetWithinDownshift(target, downshiftElements, environment, checkActi
375
375
  if (checkActiveElement === void 0) {
376
376
  checkActiveElement = true;
377
377
  }
378
- return downshiftElements.some(function (contextNode) {
378
+ return environment && downshiftElements.some(function (contextNode) {
379
379
  return contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment));
380
380
  });
381
381
  }
@@ -402,28 +402,12 @@ var cleanupStatus = debounce(function (documentProp) {
402
402
  getStatusDiv(documentProp).textContent = '';
403
403
  }, 500);
404
404
 
405
- /**
406
- * @param {String} status the status message
407
- * @param {Object} documentProp document passed by the user.
408
- */
409
- function setStatus(status, documentProp) {
410
- var div = getStatusDiv(documentProp);
411
- if (!status) {
412
- return;
413
- }
414
- div.textContent = status;
415
- cleanupStatus(documentProp);
416
- }
417
-
418
405
  /**
419
406
  * Get the status node or create it if it does not already exist.
420
407
  * @param {Object} documentProp document passed by the user.
421
408
  * @return {HTMLElement} the status node.
422
409
  */
423
410
  function getStatusDiv(documentProp) {
424
- if (documentProp === void 0) {
425
- documentProp = document;
426
- }
427
411
  var statusDiv = documentProp.getElementById('a11y-status-message');
428
412
  if (statusDiv) {
429
413
  return statusDiv;
@@ -447,6 +431,19 @@ function getStatusDiv(documentProp) {
447
431
  return statusDiv;
448
432
  }
449
433
 
434
+ /**
435
+ * @param {String} status the status message
436
+ * @param {Object} documentProp document passed by the user.
437
+ */
438
+ function setStatus(status, documentProp) {
439
+ if (!status || !documentProp) {
440
+ return;
441
+ }
442
+ var div = getStatusDiv(documentProp);
443
+ div.textContent = status;
444
+ cleanupStatus(documentProp);
445
+ }
446
+
450
447
  var unknown = process.env.NODE_ENV !== "production" ? '__autocomplete_unknown__' : 0;
451
448
  var mouseUp = process.env.NODE_ENV !== "production" ? '__autocomplete_mouseup__' : 1;
452
449
  var itemMouseEnter = process.env.NODE_ENV !== "production" ? '__autocomplete_item_mouseenter__' : 2;
@@ -507,7 +504,6 @@ var Downshift = /*#__PURE__*/function () {
507
504
  _this.getItemId = _this.props.getItemId || function (index) {
508
505
  return _this.id + "-item-" + index;
509
506
  };
510
- _this.input = null;
511
507
  _this.items = [];
512
508
  // itemCount can be changed asynchronously
513
509
  // from within downshift (so it can't come from a prop)
@@ -601,6 +597,7 @@ var Downshift = /*#__PURE__*/function () {
601
597
  _this.props.onInputValueChange(stateToSet.inputValue, _extends({}, _this.getStateAndHelpers(), stateToSet));
602
598
  }
603
599
  return _this.setState(function (state) {
600
+ var _newStateToSet;
604
601
  state = _this.getState(state);
605
602
  var newStateToSet = isStateToSetFunction ? stateToSet(state) : stateToSet;
606
603
 
@@ -619,7 +616,7 @@ var Downshift = /*#__PURE__*/function () {
619
616
  if (isItemSelected && newStateToSet.selectedItem !== state.selectedItem) {
620
617
  onChangeArg = newStateToSet.selectedItem;
621
618
  }
622
- newStateToSet.type = newStateToSet.type || unknown;
619
+ (_newStateToSet = newStateToSet).type || (_newStateToSet.type = unknown);
623
620
  Object.keys(newStateToSet).forEach(function (key) {
624
621
  // onStateChangeArg should only have the state that is
625
622
  // actually changing
@@ -868,8 +865,13 @@ var Downshift = /*#__PURE__*/function () {
868
865
  // handle odd case for Safari and Firefox which
869
866
  // don't give the button the focus properly.
870
867
  /* istanbul ignore if (can't reasonably test this) */
871
- if (_this.props.environment.document.activeElement === _this.props.environment.document.body) {
872
- event.target.focus();
868
+ if (_this.props.environment) {
869
+ var _this$props$environme = _this.props.environment.document,
870
+ body = _this$props$environme.body,
871
+ activeElement = _this$props$environme.activeElement;
872
+ if (body && body === activeElement) {
873
+ event.target.focus();
874
+ }
873
875
  }
874
876
  // to simplify testing components that use downshift, we'll not wrap this in a setTimeout
875
877
  // if the NODE_ENV is test. With the proper build system, this should be dead code eliminated
@@ -891,7 +893,11 @@ var Downshift = /*#__PURE__*/function () {
891
893
  var blurTarget = event.target; // Save blur target for comparison with activeElement later
892
894
  // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not body element
893
895
  _this.internalSetTimeout(function () {
894
- 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)
896
+ if (_this.isMouseDown || !_this.props.environment) {
897
+ return;
898
+ }
899
+ var activeElement = _this.props.environment.document.activeElement;
900
+ 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)
895
901
  ) {
896
902
  _this.reset({
897
903
  type: blurButton
@@ -961,8 +967,13 @@ var Downshift = /*#__PURE__*/function () {
961
967
  _this.inputHandleBlur = function () {
962
968
  // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not the body element
963
969
  _this.internalSetTimeout(function () {
964
- 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);
965
- if (!_this.isMouseDown && !downshiftButtonIsActive) {
970
+ var _activeElement$datase;
971
+ if (_this.isMouseDown || !_this.props.environment) {
972
+ return;
973
+ }
974
+ var activeElement = _this.props.environment.document.activeElement;
975
+ var downshiftButtonIsActive = (activeElement == null || (_activeElement$datase = activeElement.dataset) == null ? void 0 : _activeElement$datase.toggle) && _this._rootNode && _this._rootNode.contains(activeElement);
976
+ if (!downshiftButtonIsActive) {
966
977
  _this.reset({
967
978
  type: blurInput
968
979
  });
@@ -1107,6 +1118,10 @@ var Downshift = /*#__PURE__*/function () {
1107
1118
  }, cb);
1108
1119
  };
1109
1120
  _this.updateStatus = debounce(function () {
1121
+ var _this$props;
1122
+ if (!((_this$props = _this.props) != null && (_this$props = _this$props.environment) != null && _this$props.document)) {
1123
+ return;
1124
+ }
1110
1125
  var state = _this.getState();
1111
1126
  var item = _this.items[state.highlightedIndex];
1112
1127
  var resultCount = _this.getItemCount();
@@ -1119,17 +1134,17 @@ var Downshift = /*#__PURE__*/function () {
1119
1134
  _this.previousResultCount = resultCount;
1120
1135
  setStatus(status, _this.props.environment.document);
1121
1136
  }, 200);
1122
- var _this$props = _this.props,
1123
- defaultHighlightedIndex = _this$props.defaultHighlightedIndex,
1124
- _this$props$initialHi = _this$props.initialHighlightedIndex,
1125
- _highlightedIndex = _this$props$initialHi === void 0 ? defaultHighlightedIndex : _this$props$initialHi,
1126
- defaultIsOpen = _this$props.defaultIsOpen,
1127
- _this$props$initialIs = _this$props.initialIsOpen,
1128
- _isOpen = _this$props$initialIs === void 0 ? defaultIsOpen : _this$props$initialIs,
1129
- _this$props$initialIn = _this$props.initialInputValue,
1130
- _inputValue = _this$props$initialIn === void 0 ? '' : _this$props$initialIn,
1131
- _this$props$initialSe = _this$props.initialSelectedItem,
1132
- _selectedItem = _this$props$initialSe === void 0 ? null : _this$props$initialSe;
1137
+ var _this$props2 = _this.props,
1138
+ defaultHighlightedIndex = _this$props2.defaultHighlightedIndex,
1139
+ _this$props2$initialH = _this$props2.initialHighlightedIndex,
1140
+ _highlightedIndex = _this$props2$initialH === void 0 ? defaultHighlightedIndex : _this$props2$initialH,
1141
+ defaultIsOpen = _this$props2.defaultIsOpen,
1142
+ _this$props2$initialI = _this$props2.initialIsOpen,
1143
+ _isOpen = _this$props2$initialI === void 0 ? defaultIsOpen : _this$props2$initialI,
1144
+ _this$props2$initialI2 = _this$props2.initialInputValue,
1145
+ _inputValue = _this$props2$initialI2 === void 0 ? '' : _this$props2$initialI2,
1146
+ _this$props2$initialS = _this$props2.initialSelectedItem,
1147
+ _selectedItem = _this$props2$initialS === void 0 ? null : _this$props2$initialS;
1133
1148
  var _state = _this.getState({
1134
1149
  highlightedIndex: _highlightedIndex,
1135
1150
  isOpen: _isOpen,
@@ -1182,7 +1197,7 @@ var Downshift = /*#__PURE__*/function () {
1182
1197
  return itemCount;
1183
1198
  };
1184
1199
  _proto.getItemNodeFromIndex = function getItemNodeFromIndex(index) {
1185
- return this.props.environment.document.getElementById(this.getItemId(index));
1200
+ return this.props.environment ? this.props.environment.document.getElementById(this.getItemId(index)) : null;
1186
1201
  };
1187
1202
  _proto.scrollHighlightedItemIntoView = function scrollHighlightedItemIntoView() {
1188
1203
  /* istanbul ignore else (react-native) */
@@ -1269,8 +1284,12 @@ var Downshift = /*#__PURE__*/function () {
1269
1284
  validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
1270
1285
  }
1271
1286
 
1272
- /* istanbul ignore if (react-native) */
1273
- {
1287
+ /* istanbul ignore if (react-native or SSR) */
1288
+ if (!this.props.environment) {
1289
+ this.cleanup = function () {
1290
+ _this4.internalClearTimeouts();
1291
+ };
1292
+ } else {
1274
1293
  // this.isMouseDown helps us track whether the mouse is currently held down.
1275
1294
  // This is useful when the user clicks on an item in the list, but holds the mouse
1276
1295
  // down long enough for the list to disappear (because the blur event fires on the input)
@@ -1439,7 +1458,7 @@ var Downshift = /*#__PURE__*/function () {
1439
1458
  return prevItem !== item;
1440
1459
  },
1441
1460
  environment: /* istanbul ignore next (ssr) */
1442
- typeof window === 'undefined' ? {} : window,
1461
+ typeof window === 'undefined' ? undefined : window,
1443
1462
  stateReducer: function stateReducer(state, stateToSet) {
1444
1463
  return stateToSet;
1445
1464
  },
@@ -1680,7 +1699,7 @@ var defaultProps$3 = {
1680
1699
  getA11ySelectionMessage: getA11ySelectionMessage,
1681
1700
  scrollIntoView: scrollIntoView,
1682
1701
  environment: /* istanbul ignore next (ssr) */
1683
- typeof window === 'undefined' ? {} : window
1702
+ typeof window === 'undefined' ? undefined : window
1684
1703
  };
1685
1704
  function getDefaultValue$1(props, propKey, defaultStateValues) {
1686
1705
  if (defaultStateValues === void 0) {
@@ -1759,7 +1778,7 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1759
1778
  isTouchMove: false
1760
1779
  });
1761
1780
  useEffect(function () {
1762
- if ((environment == null ? void 0 : environment.addEventListener) == null) {
1781
+ if (!environment) {
1763
1782
  return;
1764
1783
  }
1765
1784
 
@@ -1868,7 +1887,7 @@ function useA11yMessageSetter(getA11yMessage, dependencyArray, _ref3) {
1868
1887
  rest = _objectWithoutPropertiesLoose(_ref3, _excluded$3);
1869
1888
  // Sets a11y status message on changes in state.
1870
1889
  useEffect(function () {
1871
- if (isInitialMount || false) {
1890
+ if (isInitialMount || false || !(environment != null && environment.document)) {
1872
1891
  return;
1873
1892
  }
1874
1893
  updateA11yStatus(function () {
@@ -2065,7 +2084,6 @@ function getItemIndexByCharacterKey(_a) {
2065
2084
  }
2066
2085
  return highlightedIndex;
2067
2086
  }
2068
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
2069
2087
  var propTypes$2 = __assign(__assign({}, commonDropdownPropTypes), { items: PropTypes.array.isRequired, isItemDisabled: PropTypes.func, getA11ySelectionMessage: PropTypes.func });
2070
2088
  /**
2071
2089
  * Default implementation for status message. Only added when menu is open.
@@ -2256,7 +2274,7 @@ function downshiftSelectReducer(state, action) {
2256
2274
  }
2257
2275
  /* eslint-enable complexity */
2258
2276
 
2259
- var _excluded$2 = ["onMouseLeave", "refKey", "onKeyDown", "onBlur", "ref"],
2277
+ var _excluded$2 = ["onMouseLeave", "refKey", "ref"],
2260
2278
  _excluded2$2 = ["onBlur", "onClick", "onPress", "onKeyDown", "refKey", "ref"],
2261
2279
  _excluded3$1 = ["item", "index", "onMouseMove", "onClick", "onPress", "refKey", "disabled", "ref"];
2262
2280
  useSelect.stateChangeTypes = stateChangeTypes$2;
@@ -2527,10 +2545,8 @@ function useSelect(userProps) {
2527
2545
  var _ref = _temp === void 0 ? {} : _temp,
2528
2546
  onMouseLeave = _ref.onMouseLeave,
2529
2547
  _ref$refKey = _ref.refKey,
2530
- refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey;
2531
- _ref.onKeyDown;
2532
- _ref.onBlur;
2533
- var ref = _ref.ref,
2548
+ refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
2549
+ ref = _ref.ref,
2534
2550
  rest = _objectWithoutPropertiesLoose(_ref, _excluded$2);
2535
2551
  var _ref2 = _temp2 === void 0 ? {} : _temp2,
2536
2552
  _ref2$suppressRefErro = _ref2.suppressRefError,
@@ -3024,12 +3040,18 @@ function useCombobox(userProps) {
3024
3040
  }, []);
3025
3041
  // Reset itemRefs on close.
3026
3042
  useEffect(function () {
3027
- var _environment$document;
3028
3043
  if (!isOpen) {
3029
3044
  itemRefs.current = {};
3030
- } else if (((_environment$document = environment.document) == null ? void 0 : _environment$document.activeElement) !== inputRef.current) {
3031
- var _inputRef$current;
3032
- inputRef == null || (_inputRef$current = inputRef.current) == null || _inputRef$current.focus();
3045
+ }
3046
+ }, [isOpen]);
3047
+ // Reset itemRefs on close.
3048
+ useEffect(function () {
3049
+ var _inputRef$current;
3050
+ if (!isOpen || !(environment != null && environment.document) || !(inputRef != null && (_inputRef$current = inputRef.current) != null && _inputRef$current.focus)) {
3051
+ return;
3052
+ }
3053
+ if (environment.document.activeElement !== inputRef.current) {
3054
+ inputRef.current.focus();
3033
3055
  }
3034
3056
  }, [isOpen, environment]);
3035
3057
 
@@ -3243,7 +3265,7 @@ function useCombobox(userProps) {
3243
3265
  };
3244
3266
  var inputHandleBlur = function inputHandleBlur(event) {
3245
3267
  /* istanbul ignore else */
3246
- if (latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
3268
+ if (environment != null && environment.document && latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
3247
3269
  var isBlurByTabChange = event.relatedTarget === null && environment.document.activeElement !== environment.document.body;
3248
3270
  dispatch({
3249
3271
  type: InputBlur,
@@ -3622,7 +3644,7 @@ function useMultipleSelection(userProps) {
3622
3644
  // Effects.
3623
3645
  /* Sets a11y status message on changes in selectedItem. */
3624
3646
  useEffect(function () {
3625
- if (isInitialMountRef.current || false) {
3647
+ if (isInitialMountRef.current || false || !(environment != null && environment.document)) {
3626
3648
  return;
3627
3649
  }
3628
3650
  if (selectedItems.length < previousSelectedItemsRef.current.length) {