downshift 8.3.1 → 8.3.2

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.
@@ -255,6 +255,9 @@ function pickState(state) {
255
255
  * @returns {Object} The merged controlled state.
256
256
  */
257
257
  function getState(state, props) {
258
+ if (!state || !props) {
259
+ return state;
260
+ }
258
261
  return Object.keys(state).reduce(function (prevState, key) {
259
262
  prevState[key] = isControlledProp(props, key) ? props[key] : state[key];
260
263
  return prevState;
@@ -1545,7 +1548,7 @@ function validateGetRootPropsCalledCorrectly(element, _ref13) {
1545
1548
  }
1546
1549
  }
1547
1550
 
1548
- var _excluded$3 = ["isInitialMount", "highlightedIndex", "items", "environment"];
1551
+ var _excluded$3 = ["highlightedIndex", "items", "environment"];
1549
1552
  var dropdownDefaultStateValues = {
1550
1553
  highlightedIndex: -1,
1551
1554
  isOpen: false,
@@ -1723,9 +1726,10 @@ function useEnhancedReducer(reducer, props, createInitialState, isStateEqual) {
1723
1726
  }, [propsRef]);
1724
1727
  var action = actionRef.current;
1725
1728
  React.useEffect(function () {
1726
- var shouldCallOnChangeProps = action && prevStateRef.current && !isStateEqual(prevStateRef.current, state);
1729
+ var prevState = getState(prevStateRef.current, action == null ? void 0 : action.props);
1730
+ var shouldCallOnChangeProps = action && prevStateRef.current && !isStateEqual(prevState, state);
1727
1731
  if (shouldCallOnChangeProps) {
1728
- callOnChangeProps(action, getState(prevStateRef.current, action.props), state);
1732
+ callOnChangeProps(action, prevState, state);
1729
1733
  }
1730
1734
  prevStateRef.current = state;
1731
1735
  }, [state, action, isStateEqual]);
@@ -1830,7 +1834,8 @@ function getHighlightedIndexOnOpen(props, state, offset) {
1830
1834
  function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, handleBlur) {
1831
1835
  var mouseAndTouchTrackersRef = React.useRef({
1832
1836
  isMouseDown: false,
1833
- isTouchMove: false
1837
+ isTouchMove: false,
1838
+ isTouchEnd: false
1834
1839
  });
1835
1840
  React.useEffect(function () {
1836
1841
  if (!environment) {
@@ -1840,6 +1845,7 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1840
1845
  // The same strategy for checking if a click occurred inside or outside downshift
1841
1846
  // as in downshift.js.
1842
1847
  var onMouseDown = function onMouseDown() {
1848
+ mouseAndTouchTrackersRef.current.isTouchEnd = false; // reset this one.
1843
1849
  mouseAndTouchTrackersRef.current.isMouseDown = true;
1844
1850
  };
1845
1851
  var onMouseUp = function onMouseUp(event) {
@@ -1851,12 +1857,14 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1851
1857
  }
1852
1858
  };
1853
1859
  var onTouchStart = function onTouchStart() {
1860
+ mouseAndTouchTrackersRef.current.isTouchEnd = false;
1854
1861
  mouseAndTouchTrackersRef.current.isTouchMove = false;
1855
1862
  };
1856
1863
  var onTouchMove = function onTouchMove() {
1857
1864
  mouseAndTouchTrackersRef.current.isTouchMove = true;
1858
1865
  };
1859
1866
  var onTouchEnd = function onTouchEnd(event) {
1867
+ mouseAndTouchTrackersRef.current.isTouchEnd = true;
1860
1868
  if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, downshiftElementRefs.map(function (ref) {
1861
1869
  return ref.current;
1862
1870
  }), environment, false)) {
@@ -1935,11 +1943,11 @@ if (process.env.NODE_ENV !== 'production') {
1935
1943
  };
1936
1944
  }
1937
1945
  function useA11yMessageSetter(getA11yMessage, dependencyArray, _ref3) {
1938
- var isInitialMount = _ref3.isInitialMount,
1939
- highlightedIndex = _ref3.highlightedIndex,
1946
+ var highlightedIndex = _ref3.highlightedIndex,
1940
1947
  items = _ref3.items,
1941
1948
  environment = _ref3.environment,
1942
1949
  rest = _objectWithoutPropertiesLoose__default["default"](_ref3, _excluded$3);
1950
+ var isInitialMount = useIsInitialMount();
1943
1951
  // Sets a11y status message on changes in state.
1944
1952
  React.useEffect(function () {
1945
1953
  if (isInitialMount || false || !(environment != null && environment.document)) {
@@ -1984,11 +1992,11 @@ var useControlPropsValidator = noop;
1984
1992
  /* istanbul ignore next */
1985
1993
  if (process.env.NODE_ENV !== 'production') {
1986
1994
  useControlPropsValidator = function useControlPropsValidator(_ref5) {
1987
- var isInitialMount = _ref5.isInitialMount,
1988
- props = _ref5.props,
1995
+ var props = _ref5.props,
1989
1996
  state = _ref5.state;
1990
1997
  // used for checking when props are moving from controlled to uncontrolled.
1991
1998
  var prevPropsRef = React.useRef(props);
1999
+ var isInitialMount = useIsInitialMount();
1992
2000
  React.useEffect(function () {
1993
2001
  if (isInitialMount) {
1994
2002
  return;
@@ -2037,6 +2045,20 @@ function isDropdownsStateEqual(prevState, newState) {
2037
2045
  return prevState.isOpen === newState.isOpen && prevState.inputValue === newState.inputValue && prevState.highlightedIndex === newState.highlightedIndex && prevState.selectedItem === newState.selectedItem;
2038
2046
  }
2039
2047
 
2048
+ /**
2049
+ * Tracks if it's the first render.
2050
+ */
2051
+ function useIsInitialMount() {
2052
+ var isInitialMountRef = React__default["default"].useRef(true);
2053
+ React__default["default"].useEffect(function () {
2054
+ isInitialMountRef.current = false;
2055
+ return function () {
2056
+ isInitialMountRef.current = true;
2057
+ };
2058
+ }, []);
2059
+ return isInitialMountRef.current;
2060
+ }
2061
+
2040
2062
  // Shared between all exports.
2041
2063
  var commonPropTypes = {
2042
2064
  environment: PropTypes__default["default"].shape({
@@ -2377,7 +2399,7 @@ function useSelect(userProps) {
2377
2399
  var elementIds = useElementIds(props);
2378
2400
  // used to keep track of how many items we had on previous cycle.
2379
2401
  var previousResultCountRef = React.useRef();
2380
- var isInitialMountRef = React.useRef(true);
2402
+ var isInitialMount = useIsInitialMount();
2381
2403
  // utility callback to get item element.
2382
2404
  var latest = useLatestRef({
2383
2405
  state: state,
@@ -2392,7 +2414,6 @@ function useSelect(userProps) {
2392
2414
  // Effects.
2393
2415
  // Sets a11y status message on changes in state.
2394
2416
  useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items], _extends__default["default"]({
2395
- isInitialMount: isInitialMountRef.current,
2396
2417
  previousResultCount: previousResultCountRef.current,
2397
2418
  items: items,
2398
2419
  environment: environment,
@@ -2400,7 +2421,6 @@ function useSelect(userProps) {
2400
2421
  }, state));
2401
2422
  // Sets a11y status message on changes in selectedItem.
2402
2423
  useA11yMessageSetter(getA11ySelectionMessage, [selectedItem], _extends__default["default"]({
2403
- isInitialMount: isInitialMountRef.current,
2404
2424
  previousResultCount: previousResultCountRef.current,
2405
2425
  items: items,
2406
2426
  environment: environment,
@@ -2440,12 +2460,11 @@ function useSelect(userProps) {
2440
2460
  clearTimeoutRef.current(dispatch);
2441
2461
  }, [dispatch, inputValue]);
2442
2462
  useControlPropsValidator({
2443
- isInitialMount: isInitialMountRef.current,
2444
2463
  props: props,
2445
2464
  state: state
2446
2465
  });
2447
2466
  React.useEffect(function () {
2448
- if (isInitialMountRef.current) {
2467
+ if (isInitialMount) {
2449
2468
  return;
2450
2469
  }
2451
2470
  previousResultCountRef.current = items.length;
@@ -2465,13 +2484,6 @@ function useSelect(userProps) {
2465
2484
  });
2466
2485
  });
2467
2486
  var setGetterPropCallInfo = useGetterPropsCalledChecker('getMenuProps', 'getToggleButtonProps');
2468
- // Make initial ref false.
2469
- React.useEffect(function () {
2470
- isInitialMountRef.current = false;
2471
- return function () {
2472
- isInitialMountRef.current = true;
2473
- };
2474
- }, []);
2475
2487
  // Reset itemRefs on close.
2476
2488
  React.useEffect(function () {
2477
2489
  if (!isOpen) {
@@ -2711,7 +2723,7 @@ function useSelect(userProps) {
2711
2723
  index = _getItemAndIndex[1];
2712
2724
  var disabled = latestProps.isItemDisabled(item, index);
2713
2725
  var itemHandleMouseMove = function itemHandleMouseMove() {
2714
- if (index === latestState.highlightedIndex) {
2726
+ if (mouseAndTouchTrackersRef.current.isTouchEnd || index === latestState.highlightedIndex) {
2715
2727
  return;
2716
2728
  }
2717
2729
  shouldScrollRef.current = false;
@@ -2745,7 +2757,7 @@ function useSelect(userProps) {
2745
2757
  itemProps.onMouseMove = callAllEventHandlers(onMouseMove, itemHandleMouseMove);
2746
2758
  itemProps.onMouseDown = callAllEventHandlers(onMouseDown, itemHandleMouseDown);
2747
2759
  return itemProps;
2748
- }, [latest, elementIds, shouldScrollRef, dispatch]);
2760
+ }, [latest, elementIds, mouseAndTouchTrackersRef, shouldScrollRef, dispatch]);
2749
2761
  return {
2750
2762
  // prop getters.
2751
2763
  getToggleButtonProps: getToggleButtonProps,
@@ -2860,13 +2872,14 @@ function useControlledReducer(reducer, props, createInitialState, isStateEqual)
2860
2872
  var _useEnhancedReducer = useEnhancedReducer(reducer, props, createInitialState, isStateEqual),
2861
2873
  state = _useEnhancedReducer[0],
2862
2874
  dispatch = _useEnhancedReducer[1];
2863
-
2864
- // ToDo: if needed, make same approach as selectedItemChanged from Downshift.
2875
+ var isInitialMount = useIsInitialMount();
2865
2876
  React.useEffect(function () {
2866
2877
  if (!isControlledProp(props, 'selectedItem')) {
2867
2878
  return;
2868
2879
  }
2869
- if (props.selectedItemChanged(previousSelectedItemRef.current, props.selectedItem)) {
2880
+ if (!isInitialMount &&
2881
+ // on first mount we already have the proper inputValue for a initial selected item.
2882
+ props.selectedItemChanged(previousSelectedItemRef.current, props.selectedItem)) {
2870
2883
  dispatch({
2871
2884
  type: ControlledPropUpdatedSelectedItem,
2872
2885
  inputValue: props.itemToString(props.selectedItem)
@@ -3044,7 +3057,8 @@ function useCombobox(userProps) {
3044
3057
  var itemRefs = React.useRef({});
3045
3058
  var inputRef = React.useRef(null);
3046
3059
  var toggleButtonRef = React.useRef(null);
3047
- var isInitialMountRef = React.useRef(true);
3060
+ var isInitialMount = useIsInitialMount();
3061
+
3048
3062
  // prevent id re-generation between renders.
3049
3063
  var elementIds = useElementIds(props);
3050
3064
  // used to keep track of how many items we had on previous cycle.
@@ -3061,7 +3075,6 @@ function useCombobox(userProps) {
3061
3075
  // Effects.
3062
3076
  // Sets a11y status message on changes in state.
3063
3077
  useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items], _extends__default["default"]({
3064
- isInitialMount: isInitialMountRef.current,
3065
3078
  previousResultCount: previousResultCountRef.current,
3066
3079
  items: items,
3067
3080
  environment: environment,
@@ -3069,7 +3082,6 @@ function useCombobox(userProps) {
3069
3082
  }, state));
3070
3083
  // Sets a11y status message on changes in selectedItem.
3071
3084
  useA11yMessageSetter(getA11ySelectionMessage, [selectedItem], _extends__default["default"]({
3072
- isInitialMount: isInitialMountRef.current,
3073
3085
  previousResultCount: previousResultCountRef.current,
3074
3086
  items: items,
3075
3087
  environment: environment,
@@ -3085,7 +3097,6 @@ function useCombobox(userProps) {
3085
3097
  getItemNodeFromIndex: getItemNodeFromIndex
3086
3098
  });
3087
3099
  useControlPropsValidator({
3088
- isInitialMount: isInitialMountRef.current,
3089
3100
  props: props,
3090
3101
  state: state
3091
3102
  });
@@ -3098,10 +3109,9 @@ function useCombobox(userProps) {
3098
3109
  // eslint-disable-next-line react-hooks/exhaustive-deps
3099
3110
  }, []);
3100
3111
  React.useEffect(function () {
3101
- if (isInitialMountRef.current) {
3102
- return;
3112
+ if (!isInitialMount) {
3113
+ previousResultCountRef.current = items.length;
3103
3114
  }
3104
- previousResultCountRef.current = items.length;
3105
3115
  });
3106
3116
  // Add mouse/touch events to document.
3107
3117
  var mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [inputRef, menuRef, toggleButtonRef], environment, function () {
@@ -3111,13 +3121,6 @@ function useCombobox(userProps) {
3111
3121
  });
3112
3122
  });
3113
3123
  var setGetterPropCallInfo = useGetterPropsCalledChecker('getInputProps', 'getMenuProps');
3114
- // Make initial ref false.
3115
- React.useEffect(function () {
3116
- isInitialMountRef.current = false;
3117
- return function () {
3118
- isInitialMountRef.current = true;
3119
- };
3120
- }, []);
3121
3124
  // Reset itemRefs on close.
3122
3125
  React.useEffect(function () {
3123
3126
  if (!isOpen) {
@@ -3264,7 +3267,7 @@ function useCombobox(userProps) {
3264
3267
  var onSelectKey = 'onClick';
3265
3268
  var customClickHandler = onClick;
3266
3269
  var itemHandleMouseMove = function itemHandleMouseMove() {
3267
- if (index === latestState.highlightedIndex) {
3270
+ if (mouseAndTouchTrackersRef.current.isTouchEnd || index === latestState.highlightedIndex) {
3268
3271
  return;
3269
3272
  }
3270
3273
  shouldScrollRef.current = false;
@@ -3292,7 +3295,7 @@ function useCombobox(userProps) {
3292
3295
  onMouseMove: callAllEventHandlers(onMouseMove, itemHandleMouseMove),
3293
3296
  onMouseDown: callAllEventHandlers(onMouseDown, itemHandleMouseDown)
3294
3297
  }, rest);
3295
- }, [dispatch, latest, shouldScrollRef, elementIds]);
3298
+ }, [dispatch, elementIds, latest, mouseAndTouchTrackersRef, shouldScrollRef]);
3296
3299
  var getToggleButtonProps = React.useCallback(function (_temp4) {
3297
3300
  var _extends4;
3298
3301
  var _ref5 = _temp4 === void 0 ? {} : _temp4,
@@ -3724,7 +3727,7 @@ function useMultipleSelection(userProps) {
3724
3727
  selectedItems = state.selectedItems;
3725
3728
 
3726
3729
  // Refs.
3727
- var isInitialMountRef = React.useRef(true);
3730
+ var isInitialMount = useIsInitialMount();
3728
3731
  var dropdownRef = React.useRef(null);
3729
3732
  var previousSelectedItemsRef = React.useRef(selectedItems);
3730
3733
  var selectedItemRefs = React.useRef();
@@ -3737,7 +3740,7 @@ function useMultipleSelection(userProps) {
3737
3740
  // Effects.
3738
3741
  /* Sets a11y status message on changes in selectedItem. */
3739
3742
  React.useEffect(function () {
3740
- if (isInitialMountRef.current || false || !(environment != null && environment.document)) {
3743
+ if (isInitialMount || false || !(environment != null && environment.document)) {
3741
3744
  return;
3742
3745
  }
3743
3746
  if (selectedItems.length < previousSelectedItemsRef.current.length) {
@@ -3758,7 +3761,7 @@ function useMultipleSelection(userProps) {
3758
3761
  }, [selectedItems.length]);
3759
3762
  // Sets focus on active item.
3760
3763
  React.useEffect(function () {
3761
- if (isInitialMountRef.current) {
3764
+ if (isInitialMount) {
3762
3765
  return;
3763
3766
  }
3764
3767
  if (activeIndex === -1 && dropdownRef.current) {
@@ -3766,20 +3769,12 @@ function useMultipleSelection(userProps) {
3766
3769
  } else if (selectedItemRefs.current[activeIndex]) {
3767
3770
  selectedItemRefs.current[activeIndex].focus();
3768
3771
  }
3769
- }, [activeIndex]);
3772
+ }, [activeIndex, isInitialMount]);
3770
3773
  useControlPropsValidator({
3771
- isInitialMount: isInitialMountRef.current,
3772
3774
  props: props,
3773
3775
  state: state
3774
3776
  });
3775
3777
  var setGetterPropCallInfo = useGetterPropsCalledChecker('getDropdownProps');
3776
- // Make initial ref false.
3777
- React.useEffect(function () {
3778
- isInitialMountRef.current = false;
3779
- return function () {
3780
- isInitialMountRef.current = true;
3781
- };
3782
- }, []);
3783
3778
 
3784
3779
  // Event handler functions.
3785
3780
  var selectedItemKeyDownHandlers = React.useMemo(function () {
@@ -242,6 +242,9 @@ function pickState(state) {
242
242
  * @returns {Object} The merged controlled state.
243
243
  */
244
244
  function getState(state, props) {
245
+ if (!state || !props) {
246
+ return state;
247
+ }
245
248
  return Object.keys(state).reduce(function (prevState, key) {
246
249
  prevState[key] = isControlledProp(props, key) ? props[key] : state[key];
247
250
  return prevState;
@@ -1532,7 +1535,7 @@ function validateGetRootPropsCalledCorrectly(element, _ref13) {
1532
1535
  }
1533
1536
  }
1534
1537
 
1535
- var _excluded$3 = ["isInitialMount", "highlightedIndex", "items", "environment"];
1538
+ var _excluded$3 = ["highlightedIndex", "items", "environment"];
1536
1539
  var dropdownDefaultStateValues = {
1537
1540
  highlightedIndex: -1,
1538
1541
  isOpen: false,
@@ -1710,9 +1713,10 @@ function useEnhancedReducer(reducer, props, createInitialState, isStateEqual) {
1710
1713
  }, [propsRef]);
1711
1714
  var action = actionRef.current;
1712
1715
  useEffect(function () {
1713
- var shouldCallOnChangeProps = action && prevStateRef.current && !isStateEqual(prevStateRef.current, state);
1716
+ var prevState = getState(prevStateRef.current, action == null ? void 0 : action.props);
1717
+ var shouldCallOnChangeProps = action && prevStateRef.current && !isStateEqual(prevState, state);
1714
1718
  if (shouldCallOnChangeProps) {
1715
- callOnChangeProps(action, getState(prevStateRef.current, action.props), state);
1719
+ callOnChangeProps(action, prevState, state);
1716
1720
  }
1717
1721
  prevStateRef.current = state;
1718
1722
  }, [state, action, isStateEqual]);
@@ -1817,7 +1821,8 @@ function getHighlightedIndexOnOpen(props, state, offset) {
1817
1821
  function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, handleBlur) {
1818
1822
  var mouseAndTouchTrackersRef = useRef({
1819
1823
  isMouseDown: false,
1820
- isTouchMove: false
1824
+ isTouchMove: false,
1825
+ isTouchEnd: false
1821
1826
  });
1822
1827
  useEffect(function () {
1823
1828
  if (!environment) {
@@ -1827,6 +1832,7 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1827
1832
  // The same strategy for checking if a click occurred inside or outside downshift
1828
1833
  // as in downshift.js.
1829
1834
  var onMouseDown = function onMouseDown() {
1835
+ mouseAndTouchTrackersRef.current.isTouchEnd = false; // reset this one.
1830
1836
  mouseAndTouchTrackersRef.current.isMouseDown = true;
1831
1837
  };
1832
1838
  var onMouseUp = function onMouseUp(event) {
@@ -1838,12 +1844,14 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
1838
1844
  }
1839
1845
  };
1840
1846
  var onTouchStart = function onTouchStart() {
1847
+ mouseAndTouchTrackersRef.current.isTouchEnd = false;
1841
1848
  mouseAndTouchTrackersRef.current.isTouchMove = false;
1842
1849
  };
1843
1850
  var onTouchMove = function onTouchMove() {
1844
1851
  mouseAndTouchTrackersRef.current.isTouchMove = true;
1845
1852
  };
1846
1853
  var onTouchEnd = function onTouchEnd(event) {
1854
+ mouseAndTouchTrackersRef.current.isTouchEnd = true;
1847
1855
  if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, downshiftElementRefs.map(function (ref) {
1848
1856
  return ref.current;
1849
1857
  }), environment, false)) {
@@ -1922,11 +1930,11 @@ if (process.env.NODE_ENV !== 'production') {
1922
1930
  };
1923
1931
  }
1924
1932
  function useA11yMessageSetter(getA11yMessage, dependencyArray, _ref3) {
1925
- var isInitialMount = _ref3.isInitialMount,
1926
- highlightedIndex = _ref3.highlightedIndex,
1933
+ var highlightedIndex = _ref3.highlightedIndex,
1927
1934
  items = _ref3.items,
1928
1935
  environment = _ref3.environment,
1929
1936
  rest = _objectWithoutPropertiesLoose(_ref3, _excluded$3);
1937
+ var isInitialMount = useIsInitialMount();
1930
1938
  // Sets a11y status message on changes in state.
1931
1939
  useEffect(function () {
1932
1940
  if (isInitialMount || false || !(environment != null && environment.document)) {
@@ -1971,11 +1979,11 @@ var useControlPropsValidator = noop;
1971
1979
  /* istanbul ignore next */
1972
1980
  if (process.env.NODE_ENV !== 'production') {
1973
1981
  useControlPropsValidator = function useControlPropsValidator(_ref5) {
1974
- var isInitialMount = _ref5.isInitialMount,
1975
- props = _ref5.props,
1982
+ var props = _ref5.props,
1976
1983
  state = _ref5.state;
1977
1984
  // used for checking when props are moving from controlled to uncontrolled.
1978
1985
  var prevPropsRef = useRef(props);
1986
+ var isInitialMount = useIsInitialMount();
1979
1987
  useEffect(function () {
1980
1988
  if (isInitialMount) {
1981
1989
  return;
@@ -2024,6 +2032,20 @@ function isDropdownsStateEqual(prevState, newState) {
2024
2032
  return prevState.isOpen === newState.isOpen && prevState.inputValue === newState.inputValue && prevState.highlightedIndex === newState.highlightedIndex && prevState.selectedItem === newState.selectedItem;
2025
2033
  }
2026
2034
 
2035
+ /**
2036
+ * Tracks if it's the first render.
2037
+ */
2038
+ function useIsInitialMount() {
2039
+ var isInitialMountRef = React.useRef(true);
2040
+ React.useEffect(function () {
2041
+ isInitialMountRef.current = false;
2042
+ return function () {
2043
+ isInitialMountRef.current = true;
2044
+ };
2045
+ }, []);
2046
+ return isInitialMountRef.current;
2047
+ }
2048
+
2027
2049
  // Shared between all exports.
2028
2050
  var commonPropTypes = {
2029
2051
  environment: PropTypes.shape({
@@ -2364,7 +2386,7 @@ function useSelect(userProps) {
2364
2386
  var elementIds = useElementIds(props);
2365
2387
  // used to keep track of how many items we had on previous cycle.
2366
2388
  var previousResultCountRef = useRef();
2367
- var isInitialMountRef = useRef(true);
2389
+ var isInitialMount = useIsInitialMount();
2368
2390
  // utility callback to get item element.
2369
2391
  var latest = useLatestRef({
2370
2392
  state: state,
@@ -2379,7 +2401,6 @@ function useSelect(userProps) {
2379
2401
  // Effects.
2380
2402
  // Sets a11y status message on changes in state.
2381
2403
  useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items], _extends({
2382
- isInitialMount: isInitialMountRef.current,
2383
2404
  previousResultCount: previousResultCountRef.current,
2384
2405
  items: items,
2385
2406
  environment: environment,
@@ -2387,7 +2408,6 @@ function useSelect(userProps) {
2387
2408
  }, state));
2388
2409
  // Sets a11y status message on changes in selectedItem.
2389
2410
  useA11yMessageSetter(getA11ySelectionMessage, [selectedItem], _extends({
2390
- isInitialMount: isInitialMountRef.current,
2391
2411
  previousResultCount: previousResultCountRef.current,
2392
2412
  items: items,
2393
2413
  environment: environment,
@@ -2427,12 +2447,11 @@ function useSelect(userProps) {
2427
2447
  clearTimeoutRef.current(dispatch);
2428
2448
  }, [dispatch, inputValue]);
2429
2449
  useControlPropsValidator({
2430
- isInitialMount: isInitialMountRef.current,
2431
2450
  props: props,
2432
2451
  state: state
2433
2452
  });
2434
2453
  useEffect(function () {
2435
- if (isInitialMountRef.current) {
2454
+ if (isInitialMount) {
2436
2455
  return;
2437
2456
  }
2438
2457
  previousResultCountRef.current = items.length;
@@ -2452,13 +2471,6 @@ function useSelect(userProps) {
2452
2471
  });
2453
2472
  });
2454
2473
  var setGetterPropCallInfo = useGetterPropsCalledChecker('getMenuProps', 'getToggleButtonProps');
2455
- // Make initial ref false.
2456
- useEffect(function () {
2457
- isInitialMountRef.current = false;
2458
- return function () {
2459
- isInitialMountRef.current = true;
2460
- };
2461
- }, []);
2462
2474
  // Reset itemRefs on close.
2463
2475
  useEffect(function () {
2464
2476
  if (!isOpen) {
@@ -2698,7 +2710,7 @@ function useSelect(userProps) {
2698
2710
  index = _getItemAndIndex[1];
2699
2711
  var disabled = latestProps.isItemDisabled(item, index);
2700
2712
  var itemHandleMouseMove = function itemHandleMouseMove() {
2701
- if (index === latestState.highlightedIndex) {
2713
+ if (mouseAndTouchTrackersRef.current.isTouchEnd || index === latestState.highlightedIndex) {
2702
2714
  return;
2703
2715
  }
2704
2716
  shouldScrollRef.current = false;
@@ -2732,7 +2744,7 @@ function useSelect(userProps) {
2732
2744
  itemProps.onMouseMove = callAllEventHandlers(onMouseMove, itemHandleMouseMove);
2733
2745
  itemProps.onMouseDown = callAllEventHandlers(onMouseDown, itemHandleMouseDown);
2734
2746
  return itemProps;
2735
- }, [latest, elementIds, shouldScrollRef, dispatch]);
2747
+ }, [latest, elementIds, mouseAndTouchTrackersRef, shouldScrollRef, dispatch]);
2736
2748
  return {
2737
2749
  // prop getters.
2738
2750
  getToggleButtonProps: getToggleButtonProps,
@@ -2847,13 +2859,14 @@ function useControlledReducer(reducer, props, createInitialState, isStateEqual)
2847
2859
  var _useEnhancedReducer = useEnhancedReducer(reducer, props, createInitialState, isStateEqual),
2848
2860
  state = _useEnhancedReducer[0],
2849
2861
  dispatch = _useEnhancedReducer[1];
2850
-
2851
- // ToDo: if needed, make same approach as selectedItemChanged from Downshift.
2862
+ var isInitialMount = useIsInitialMount();
2852
2863
  useEffect(function () {
2853
2864
  if (!isControlledProp(props, 'selectedItem')) {
2854
2865
  return;
2855
2866
  }
2856
- if (props.selectedItemChanged(previousSelectedItemRef.current, props.selectedItem)) {
2867
+ if (!isInitialMount &&
2868
+ // on first mount we already have the proper inputValue for a initial selected item.
2869
+ props.selectedItemChanged(previousSelectedItemRef.current, props.selectedItem)) {
2857
2870
  dispatch({
2858
2871
  type: ControlledPropUpdatedSelectedItem,
2859
2872
  inputValue: props.itemToString(props.selectedItem)
@@ -3031,7 +3044,8 @@ function useCombobox(userProps) {
3031
3044
  var itemRefs = useRef({});
3032
3045
  var inputRef = useRef(null);
3033
3046
  var toggleButtonRef = useRef(null);
3034
- var isInitialMountRef = useRef(true);
3047
+ var isInitialMount = useIsInitialMount();
3048
+
3035
3049
  // prevent id re-generation between renders.
3036
3050
  var elementIds = useElementIds(props);
3037
3051
  // used to keep track of how many items we had on previous cycle.
@@ -3048,7 +3062,6 @@ function useCombobox(userProps) {
3048
3062
  // Effects.
3049
3063
  // Sets a11y status message on changes in state.
3050
3064
  useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items], _extends({
3051
- isInitialMount: isInitialMountRef.current,
3052
3065
  previousResultCount: previousResultCountRef.current,
3053
3066
  items: items,
3054
3067
  environment: environment,
@@ -3056,7 +3069,6 @@ function useCombobox(userProps) {
3056
3069
  }, state));
3057
3070
  // Sets a11y status message on changes in selectedItem.
3058
3071
  useA11yMessageSetter(getA11ySelectionMessage, [selectedItem], _extends({
3059
- isInitialMount: isInitialMountRef.current,
3060
3072
  previousResultCount: previousResultCountRef.current,
3061
3073
  items: items,
3062
3074
  environment: environment,
@@ -3072,7 +3084,6 @@ function useCombobox(userProps) {
3072
3084
  getItemNodeFromIndex: getItemNodeFromIndex
3073
3085
  });
3074
3086
  useControlPropsValidator({
3075
- isInitialMount: isInitialMountRef.current,
3076
3087
  props: props,
3077
3088
  state: state
3078
3089
  });
@@ -3085,10 +3096,9 @@ function useCombobox(userProps) {
3085
3096
  // eslint-disable-next-line react-hooks/exhaustive-deps
3086
3097
  }, []);
3087
3098
  useEffect(function () {
3088
- if (isInitialMountRef.current) {
3089
- return;
3099
+ if (!isInitialMount) {
3100
+ previousResultCountRef.current = items.length;
3090
3101
  }
3091
- previousResultCountRef.current = items.length;
3092
3102
  });
3093
3103
  // Add mouse/touch events to document.
3094
3104
  var mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [inputRef, menuRef, toggleButtonRef], environment, function () {
@@ -3098,13 +3108,6 @@ function useCombobox(userProps) {
3098
3108
  });
3099
3109
  });
3100
3110
  var setGetterPropCallInfo = useGetterPropsCalledChecker('getInputProps', 'getMenuProps');
3101
- // Make initial ref false.
3102
- useEffect(function () {
3103
- isInitialMountRef.current = false;
3104
- return function () {
3105
- isInitialMountRef.current = true;
3106
- };
3107
- }, []);
3108
3111
  // Reset itemRefs on close.
3109
3112
  useEffect(function () {
3110
3113
  if (!isOpen) {
@@ -3251,7 +3254,7 @@ function useCombobox(userProps) {
3251
3254
  var onSelectKey = 'onClick';
3252
3255
  var customClickHandler = onClick;
3253
3256
  var itemHandleMouseMove = function itemHandleMouseMove() {
3254
- if (index === latestState.highlightedIndex) {
3257
+ if (mouseAndTouchTrackersRef.current.isTouchEnd || index === latestState.highlightedIndex) {
3255
3258
  return;
3256
3259
  }
3257
3260
  shouldScrollRef.current = false;
@@ -3279,7 +3282,7 @@ function useCombobox(userProps) {
3279
3282
  onMouseMove: callAllEventHandlers(onMouseMove, itemHandleMouseMove),
3280
3283
  onMouseDown: callAllEventHandlers(onMouseDown, itemHandleMouseDown)
3281
3284
  }, rest);
3282
- }, [dispatch, latest, shouldScrollRef, elementIds]);
3285
+ }, [dispatch, elementIds, latest, mouseAndTouchTrackersRef, shouldScrollRef]);
3283
3286
  var getToggleButtonProps = useCallback(function (_temp4) {
3284
3287
  var _extends4;
3285
3288
  var _ref5 = _temp4 === void 0 ? {} : _temp4,
@@ -3711,7 +3714,7 @@ function useMultipleSelection(userProps) {
3711
3714
  selectedItems = state.selectedItems;
3712
3715
 
3713
3716
  // Refs.
3714
- var isInitialMountRef = useRef(true);
3717
+ var isInitialMount = useIsInitialMount();
3715
3718
  var dropdownRef = useRef(null);
3716
3719
  var previousSelectedItemsRef = useRef(selectedItems);
3717
3720
  var selectedItemRefs = useRef();
@@ -3724,7 +3727,7 @@ function useMultipleSelection(userProps) {
3724
3727
  // Effects.
3725
3728
  /* Sets a11y status message on changes in selectedItem. */
3726
3729
  useEffect(function () {
3727
- if (isInitialMountRef.current || false || !(environment != null && environment.document)) {
3730
+ if (isInitialMount || false || !(environment != null && environment.document)) {
3728
3731
  return;
3729
3732
  }
3730
3733
  if (selectedItems.length < previousSelectedItemsRef.current.length) {
@@ -3745,7 +3748,7 @@ function useMultipleSelection(userProps) {
3745
3748
  }, [selectedItems.length]);
3746
3749
  // Sets focus on active item.
3747
3750
  useEffect(function () {
3748
- if (isInitialMountRef.current) {
3751
+ if (isInitialMount) {
3749
3752
  return;
3750
3753
  }
3751
3754
  if (activeIndex === -1 && dropdownRef.current) {
@@ -3753,20 +3756,12 @@ function useMultipleSelection(userProps) {
3753
3756
  } else if (selectedItemRefs.current[activeIndex]) {
3754
3757
  selectedItemRefs.current[activeIndex].focus();
3755
3758
  }
3756
- }, [activeIndex]);
3759
+ }, [activeIndex, isInitialMount]);
3757
3760
  useControlPropsValidator({
3758
- isInitialMount: isInitialMountRef.current,
3759
3761
  props: props,
3760
3762
  state: state
3761
3763
  });
3762
3764
  var setGetterPropCallInfo = useGetterPropsCalledChecker('getDropdownProps');
3763
- // Make initial ref false.
3764
- useEffect(function () {
3765
- isInitialMountRef.current = false;
3766
- return function () {
3767
- isInitialMountRef.current = true;
3768
- };
3769
- }, []);
3770
3765
 
3771
3766
  // Event handler functions.
3772
3767
  var selectedItemKeyDownHandlers = useMemo(function () {