downshift 7.2.2-alpha.0 → 7.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "downshift",
3
- "version": "7.2.2-alpha.0",
3
+ "version": "7.3.0",
4
4
  "description": "🏎 A set of primitives to build simple, flexible, WAI-ARIA compliant React autocomplete, combobox or select dropdown components.",
5
5
  "main": "dist/downshift.cjs.js",
6
6
  "react-native": "dist/downshift.native.cjs.js",
@@ -517,9 +517,18 @@ var Downshift = /*#__PURE__*/function () {
517
517
  };
518
518
  _this.input = null;
519
519
  _this.items = [];
520
+ // itemCount can be changed asynchronously
521
+ // from within downshift (so it can't come from a prop)
522
+ // this is why we store it as an instance and use
523
+ // getItemCount rather than just use items.length
524
+ // (to support windowing + async)
520
525
  _this.itemCount = null;
521
526
  _this.previousResultCount = 0;
522
527
  _this.timeoutIds = [];
528
+ /**
529
+ * @param {Function} fn the function to call after the time
530
+ * @param {Number} time the time to wait
531
+ */
523
532
  _this.internalSetTimeout = function (fn, time) {
524
533
  var id = setTimeout(function () {
525
534
  _this.timeoutIds = _this.timeoutIds.filter(function (i) {
@@ -574,6 +583,14 @@ var Downshift = /*#__PURE__*/function () {
574
583
  _this.selectHighlightedItem = function (otherStateToSet, cb) {
575
584
  return _this.selectItemAtIndex(_this.getState().highlightedIndex, otherStateToSet, cb);
576
585
  };
586
+ // any piece of our state can live in two places:
587
+ // 1. Uncontrolled: it's internal (this.state)
588
+ // We will call this.setState to update that state
589
+ // 2. Controlled: it's external (this.props)
590
+ // We will call this.props.onStateChange to update that state
591
+ //
592
+ // In addition, we'll call this.props.onChange if the
593
+ // selectedItem is changed.
577
594
  _this.internalSetState = function (stateToSet, cb) {
578
595
  var isItemSelected, onChangeArg;
579
596
  var onStateChangeArg = {};
@@ -656,6 +673,7 @@ var Downshift = /*#__PURE__*/function () {
656
673
  _this.props.onUserAction(onStateChangeArg, _this.getStateAndHelpers());
657
674
  });
658
675
  };
676
+ //////////////////////////// ROOT
659
677
  _this.rootRef = function (node) {
660
678
  return _this._rootNode = node;
661
679
  };
@@ -678,6 +696,7 @@ var Downshift = /*#__PURE__*/function () {
678
696
  isOpen = _this$getState.isOpen;
679
697
  return _extends__default["default"]((_extends2 = {}, _extends2[refKey] = handleRefs(ref, _this.rootRef), _extends2.role = 'combobox', _extends2['aria-expanded'] = isOpen, _extends2['aria-haspopup'] = 'listbox', _extends2['aria-owns'] = isOpen ? _this.menuId : null, _extends2['aria-labelledby'] = _this.labelId, _extends2), rest);
680
698
  };
699
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\ ROOT
681
700
  _this.keyDownHandlers = {
682
701
  ArrowDown: function ArrowDown(event) {
683
702
  var _this2 = this;
@@ -762,6 +781,7 @@ var Downshift = /*#__PURE__*/function () {
762
781
  }));
763
782
  }
764
783
  };
784
+ //////////////////////////// BUTTON
765
785
  _this.buttonKeyDownHandlers = _extends__default["default"]({}, _this.keyDownHandlers, {
766
786
  ' ': function _(event) {
767
787
  event.preventDefault();
@@ -885,12 +905,16 @@ var Downshift = /*#__PURE__*/function () {
885
905
  }
886
906
  });
887
907
  };
908
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ BUTTON
909
+ /////////////////////////////// LABEL
888
910
  _this.getLabelProps = function (props) {
889
911
  return _extends__default["default"]({
890
912
  htmlFor: _this.inputId,
891
913
  id: _this.labelId
892
914
  }, props);
893
915
  };
916
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ LABEL
917
+ /////////////////////////////// INPUT
894
918
  _this.getInputProps = function (_temp4) {
895
919
  var _ref4 = _temp4 === void 0 ? {} : _temp4,
896
920
  onKeyDown = _ref4.onKeyDown,
@@ -951,6 +975,8 @@ var Downshift = /*#__PURE__*/function () {
951
975
  }
952
976
  });
953
977
  };
978
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ INPUT
979
+ /////////////////////////////// MENU
954
980
  _this.menuRef = function (node) {
955
981
  _this._menuNode = node;
956
982
  };
@@ -969,6 +995,8 @@ var Downshift = /*#__PURE__*/function () {
969
995
  _this.getMenuProps.suppressRefError = suppressRefError;
970
996
  return _extends__default["default"]((_extends3 = {}, _extends3[refKey] = handleRefs(ref, _this.menuRef), _extends3.role = 'listbox', _extends3['aria-labelledby'] = props && props['aria-label'] ? null : _this.labelId, _extends3.id = _this.menuId, _extends3), props);
971
997
  };
998
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ MENU
999
+ /////////////////////////////// ITEM
972
1000
  _this.getItemProps = function (_temp7) {
973
1001
  var _enabledEventHandlers;
974
1002
  var _ref7 = _temp7 === void 0 ? {} : _temp7,
@@ -1032,6 +1060,7 @@ var Downshift = /*#__PURE__*/function () {
1032
1060
  'aria-selected': _this.getState().highlightedIndex === index
1033
1061
  }, eventHandlers, rest);
1034
1062
  };
1063
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ITEM
1035
1064
  _this.clearItems = function () {
1036
1065
  _this.items = [];
1037
1066
  };
@@ -1239,10 +1268,7 @@ var Downshift = /*#__PURE__*/function () {
1239
1268
  isOpen: isOpen,
1240
1269
  selectedItem: selectedItem
1241
1270
  };
1242
- }
1243
-
1244
- //////////////////////////// ROOT
1245
- ;
1271
+ };
1246
1272
  _proto.componentDidMount = function componentDidMount() {
1247
1273
  var _this7 = this;
1248
1274
  /* istanbul ignore if (react-native) */
@@ -1710,8 +1736,11 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1710
1736
  isTouchMove: false
1711
1737
  });
1712
1738
  preact.useEffect(function () {
1739
+ if ((environment == null ? void 0 : environment.addEventListener) == null) {
1740
+ return;
1741
+ }
1713
1742
 
1714
- // The same strategy for checking if a click occurred inside or outside downsift
1743
+ // The same strategy for checking if a click occurred inside or outside downshift
1715
1744
  // as in downshift.js.
1716
1745
  var onMouseDown = function onMouseDown() {
1717
1746
  mouseAndTouchTrackersRef.current.isMouseDown = true;
@@ -2983,13 +3012,14 @@ function useCombobox(userProps) {
2983
3012
  }, []);
2984
3013
  // Reset itemRefs on close.
2985
3014
  preact.useEffect(function () {
3015
+ var _environment$document;
2986
3016
  if (!isOpen) {
2987
3017
  itemRefs.current = {};
2988
- } else if (document.activeElement !== inputRef.current) {
3018
+ } else if (((_environment$document = environment.document) == null ? void 0 : _environment$document.activeElement) !== inputRef.current) {
2989
3019
  var _inputRef$current;
2990
3020
  inputRef == null ? void 0 : (_inputRef$current = inputRef.current) == null ? void 0 : _inputRef$current.focus();
2991
3021
  }
2992
- }, [isOpen]);
3022
+ }, [isOpen, environment]);
2993
3023
 
2994
3024
  /* Event handler functions */
2995
3025
  var inputKeyDownHandlers = preact.useMemo(function () {
@@ -504,9 +504,18 @@ var Downshift = /*#__PURE__*/function () {
504
504
  };
505
505
  _this.input = null;
506
506
  _this.items = [];
507
+ // itemCount can be changed asynchronously
508
+ // from within downshift (so it can't come from a prop)
509
+ // this is why we store it as an instance and use
510
+ // getItemCount rather than just use items.length
511
+ // (to support windowing + async)
507
512
  _this.itemCount = null;
508
513
  _this.previousResultCount = 0;
509
514
  _this.timeoutIds = [];
515
+ /**
516
+ * @param {Function} fn the function to call after the time
517
+ * @param {Number} time the time to wait
518
+ */
510
519
  _this.internalSetTimeout = function (fn, time) {
511
520
  var id = setTimeout(function () {
512
521
  _this.timeoutIds = _this.timeoutIds.filter(function (i) {
@@ -561,6 +570,14 @@ var Downshift = /*#__PURE__*/function () {
561
570
  _this.selectHighlightedItem = function (otherStateToSet, cb) {
562
571
  return _this.selectItemAtIndex(_this.getState().highlightedIndex, otherStateToSet, cb);
563
572
  };
573
+ // any piece of our state can live in two places:
574
+ // 1. Uncontrolled: it's internal (this.state)
575
+ // We will call this.setState to update that state
576
+ // 2. Controlled: it's external (this.props)
577
+ // We will call this.props.onStateChange to update that state
578
+ //
579
+ // In addition, we'll call this.props.onChange if the
580
+ // selectedItem is changed.
564
581
  _this.internalSetState = function (stateToSet, cb) {
565
582
  var isItemSelected, onChangeArg;
566
583
  var onStateChangeArg = {};
@@ -643,6 +660,7 @@ var Downshift = /*#__PURE__*/function () {
643
660
  _this.props.onUserAction(onStateChangeArg, _this.getStateAndHelpers());
644
661
  });
645
662
  };
663
+ //////////////////////////// ROOT
646
664
  _this.rootRef = function (node) {
647
665
  return _this._rootNode = node;
648
666
  };
@@ -665,6 +683,7 @@ var Downshift = /*#__PURE__*/function () {
665
683
  isOpen = _this$getState.isOpen;
666
684
  return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, _this.rootRef), _extends2.role = 'combobox', _extends2['aria-expanded'] = isOpen, _extends2['aria-haspopup'] = 'listbox', _extends2['aria-owns'] = isOpen ? _this.menuId : null, _extends2['aria-labelledby'] = _this.labelId, _extends2), rest);
667
685
  };
686
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\ ROOT
668
687
  _this.keyDownHandlers = {
669
688
  ArrowDown: function ArrowDown(event) {
670
689
  var _this2 = this;
@@ -749,6 +768,7 @@ var Downshift = /*#__PURE__*/function () {
749
768
  }));
750
769
  }
751
770
  };
771
+ //////////////////////////// BUTTON
752
772
  _this.buttonKeyDownHandlers = _extends({}, _this.keyDownHandlers, {
753
773
  ' ': function _(event) {
754
774
  event.preventDefault();
@@ -872,12 +892,16 @@ var Downshift = /*#__PURE__*/function () {
872
892
  }
873
893
  });
874
894
  };
895
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ BUTTON
896
+ /////////////////////////////// LABEL
875
897
  _this.getLabelProps = function (props) {
876
898
  return _extends({
877
899
  htmlFor: _this.inputId,
878
900
  id: _this.labelId
879
901
  }, props);
880
902
  };
903
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ LABEL
904
+ /////////////////////////////// INPUT
881
905
  _this.getInputProps = function (_temp4) {
882
906
  var _ref4 = _temp4 === void 0 ? {} : _temp4,
883
907
  onKeyDown = _ref4.onKeyDown,
@@ -938,6 +962,8 @@ var Downshift = /*#__PURE__*/function () {
938
962
  }
939
963
  });
940
964
  };
965
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ INPUT
966
+ /////////////////////////////// MENU
941
967
  _this.menuRef = function (node) {
942
968
  _this._menuNode = node;
943
969
  };
@@ -956,6 +982,8 @@ var Downshift = /*#__PURE__*/function () {
956
982
  _this.getMenuProps.suppressRefError = suppressRefError;
957
983
  return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, _this.menuRef), _extends3.role = 'listbox', _extends3['aria-labelledby'] = props && props['aria-label'] ? null : _this.labelId, _extends3.id = _this.menuId, _extends3), props);
958
984
  };
985
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ MENU
986
+ /////////////////////////////// ITEM
959
987
  _this.getItemProps = function (_temp7) {
960
988
  var _enabledEventHandlers;
961
989
  var _ref7 = _temp7 === void 0 ? {} : _temp7,
@@ -1019,6 +1047,7 @@ var Downshift = /*#__PURE__*/function () {
1019
1047
  'aria-selected': _this.getState().highlightedIndex === index
1020
1048
  }, eventHandlers, rest);
1021
1049
  };
1050
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ITEM
1022
1051
  _this.clearItems = function () {
1023
1052
  _this.items = [];
1024
1053
  };
@@ -1226,10 +1255,7 @@ var Downshift = /*#__PURE__*/function () {
1226
1255
  isOpen: isOpen,
1227
1256
  selectedItem: selectedItem
1228
1257
  };
1229
- }
1230
-
1231
- //////////////////////////// ROOT
1232
- ;
1258
+ };
1233
1259
  _proto.componentDidMount = function componentDidMount() {
1234
1260
  var _this7 = this;
1235
1261
  /* istanbul ignore if (react-native) */
@@ -1697,8 +1723,11 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1697
1723
  isTouchMove: false
1698
1724
  });
1699
1725
  useEffect(function () {
1726
+ if ((environment == null ? void 0 : environment.addEventListener) == null) {
1727
+ return;
1728
+ }
1700
1729
 
1701
- // The same strategy for checking if a click occurred inside or outside downsift
1730
+ // The same strategy for checking if a click occurred inside or outside downshift
1702
1731
  // as in downshift.js.
1703
1732
  var onMouseDown = function onMouseDown() {
1704
1733
  mouseAndTouchTrackersRef.current.isMouseDown = true;
@@ -2970,13 +2999,14 @@ function useCombobox(userProps) {
2970
2999
  }, []);
2971
3000
  // Reset itemRefs on close.
2972
3001
  useEffect(function () {
3002
+ var _environment$document;
2973
3003
  if (!isOpen) {
2974
3004
  itemRefs.current = {};
2975
- } else if (document.activeElement !== inputRef.current) {
3005
+ } else if (((_environment$document = environment.document) == null ? void 0 : _environment$document.activeElement) !== inputRef.current) {
2976
3006
  var _inputRef$current;
2977
3007
  inputRef == null ? void 0 : (_inputRef$current = inputRef.current) == null ? void 0 : _inputRef$current.focus();
2978
3008
  }
2979
- }, [isOpen]);
3009
+ }, [isOpen, environment]);
2980
3010
 
2981
3011
  /* Event handler functions */
2982
3012
  var inputKeyDownHandlers = useMemo(function () {
@@ -794,9 +794,18 @@
794
794
  };
795
795
  _this.input = null;
796
796
  _this.items = [];
797
+ // itemCount can be changed asynchronously
798
+ // from within downshift (so it can't come from a prop)
799
+ // this is why we store it as an instance and use
800
+ // getItemCount rather than just use items.length
801
+ // (to support windowing + async)
797
802
  _this.itemCount = null;
798
803
  _this.previousResultCount = 0;
799
804
  _this.timeoutIds = [];
805
+ /**
806
+ * @param {Function} fn the function to call after the time
807
+ * @param {Number} time the time to wait
808
+ */
800
809
  _this.internalSetTimeout = function (fn, time) {
801
810
  var id = setTimeout(function () {
802
811
  _this.timeoutIds = _this.timeoutIds.filter(function (i) {
@@ -851,6 +860,14 @@
851
860
  _this.selectHighlightedItem = function (otherStateToSet, cb) {
852
861
  return _this.selectItemAtIndex(_this.getState().highlightedIndex, otherStateToSet, cb);
853
862
  };
863
+ // any piece of our state can live in two places:
864
+ // 1. Uncontrolled: it's internal (this.state)
865
+ // We will call this.setState to update that state
866
+ // 2. Controlled: it's external (this.props)
867
+ // We will call this.props.onStateChange to update that state
868
+ //
869
+ // In addition, we'll call this.props.onChange if the
870
+ // selectedItem is changed.
854
871
  _this.internalSetState = function (stateToSet, cb) {
855
872
  var isItemSelected, onChangeArg;
856
873
  var onStateChangeArg = {};
@@ -933,6 +950,7 @@
933
950
  _this.props.onUserAction(onStateChangeArg, _this.getStateAndHelpers());
934
951
  });
935
952
  };
953
+ //////////////////////////// ROOT
936
954
  _this.rootRef = function (node) {
937
955
  return _this._rootNode = node;
938
956
  };
@@ -955,6 +973,7 @@
955
973
  isOpen = _this$getState.isOpen;
956
974
  return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, _this.rootRef), _extends2.role = 'combobox', _extends2['aria-expanded'] = isOpen, _extends2['aria-haspopup'] = 'listbox', _extends2['aria-owns'] = isOpen ? _this.menuId : null, _extends2['aria-labelledby'] = _this.labelId, _extends2), rest);
957
975
  };
976
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\ ROOT
958
977
  _this.keyDownHandlers = {
959
978
  ArrowDown: function ArrowDown(event) {
960
979
  var _this2 = this;
@@ -1039,6 +1058,7 @@
1039
1058
  }));
1040
1059
  }
1041
1060
  };
1061
+ //////////////////////////// BUTTON
1042
1062
  _this.buttonKeyDownHandlers = _extends({}, _this.keyDownHandlers, {
1043
1063
  ' ': function _(event) {
1044
1064
  event.preventDefault();
@@ -1158,12 +1178,16 @@
1158
1178
  }
1159
1179
  });
1160
1180
  };
1181
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ BUTTON
1182
+ /////////////////////////////// LABEL
1161
1183
  _this.getLabelProps = function (props) {
1162
1184
  return _extends({
1163
1185
  htmlFor: _this.inputId,
1164
1186
  id: _this.labelId
1165
1187
  }, props);
1166
1188
  };
1189
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ LABEL
1190
+ /////////////////////////////// INPUT
1167
1191
  _this.getInputProps = function (_temp4) {
1168
1192
  var _ref4 = _temp4 === void 0 ? {} : _temp4,
1169
1193
  onKeyDown = _ref4.onKeyDown,
@@ -1224,6 +1248,8 @@
1224
1248
  }
1225
1249
  });
1226
1250
  };
1251
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ INPUT
1252
+ /////////////////////////////// MENU
1227
1253
  _this.menuRef = function (node) {
1228
1254
  _this._menuNode = node;
1229
1255
  };
@@ -1242,6 +1268,8 @@
1242
1268
  _this.getMenuProps.suppressRefError = suppressRefError;
1243
1269
  return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, _this.menuRef), _extends3.role = 'listbox', _extends3['aria-labelledby'] = props && props['aria-label'] ? null : _this.labelId, _extends3.id = _this.menuId, _extends3), props);
1244
1270
  };
1271
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ MENU
1272
+ /////////////////////////////// ITEM
1245
1273
  _this.getItemProps = function (_temp7) {
1246
1274
  var _enabledEventHandlers;
1247
1275
  var _ref7 = _temp7 === void 0 ? {} : _temp7,
@@ -1305,6 +1333,7 @@
1305
1333
  'aria-selected': _this.getState().highlightedIndex === index
1306
1334
  }, eventHandlers, rest);
1307
1335
  };
1336
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ITEM
1308
1337
  _this.clearItems = function () {
1309
1338
  _this.items = [];
1310
1339
  };
@@ -1512,10 +1541,7 @@
1512
1541
  isOpen: isOpen,
1513
1542
  selectedItem: selectedItem
1514
1543
  };
1515
- }
1516
-
1517
- //////////////////////////// ROOT
1518
- ;
1544
+ };
1519
1545
  _proto.componentDidMount = function componentDidMount() {
1520
1546
  var _this7 = this;
1521
1547
  /* istanbul ignore if (react-native) */
@@ -1980,8 +2006,11 @@
1980
2006
  isTouchMove: false
1981
2007
  });
1982
2008
  preact.useEffect(function () {
2009
+ if ((environment == null ? void 0 : environment.addEventListener) == null) {
2010
+ return;
2011
+ }
1983
2012
 
1984
- // The same strategy for checking if a click occurred inside or outside downsift
2013
+ // The same strategy for checking if a click occurred inside or outside downshift
1985
2014
  // as in downshift.js.
1986
2015
  var onMouseDown = function onMouseDown() {
1987
2016
  mouseAndTouchTrackersRef.current.isMouseDown = true;
@@ -3279,13 +3308,14 @@
3279
3308
  }, []);
3280
3309
  // Reset itemRefs on close.
3281
3310
  preact.useEffect(function () {
3311
+ var _environment$document;
3282
3312
  if (!isOpen) {
3283
3313
  itemRefs.current = {};
3284
- } else if (document.activeElement !== inputRef.current) {
3314
+ } else if (((_environment$document = environment.document) == null ? void 0 : _environment$document.activeElement) !== inputRef.current) {
3285
3315
  var _inputRef$current;
3286
3316
  inputRef == null ? void 0 : (_inputRef$current = inputRef.current) == null ? void 0 : _inputRef$current.focus();
3287
3317
  }
3288
- }, [isOpen]);
3318
+ }, [isOpen, environment]);
3289
3319
 
3290
3320
  /* Event handler functions */
3291
3321
  var inputKeyDownHandlers = preact.useMemo(function () {