downshift 5.1.0-beta.0 → 5.2.1

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.
@@ -626,7 +626,8 @@
626
626
 
627
627
  function getA11yStatusMessage(_ref2) {
628
628
  var isOpen = _ref2.isOpen,
629
- resultCount = _ref2.resultCount;
629
+ resultCount = _ref2.resultCount,
630
+ previousResultCount = _ref2.previousResultCount;
630
631
 
631
632
  if (!isOpen) {
632
633
  return '';
@@ -636,7 +637,11 @@
636
637
  return 'No results are available.';
637
638
  }
638
639
 
639
- return resultCount + " result" + (resultCount === 1 ? ' is' : 's are') + " available, use up and down arrow keys to navigate. Press Enter key to select.";
640
+ if (resultCount !== previousResultCount) {
641
+ return resultCount + " result" + (resultCount === 1 ? ' is' : 's are') + " available, use up and down arrow keys to navigate. Press Enter key to select.";
642
+ }
643
+
644
+ return '';
640
645
  }
641
646
  /**
642
647
  * Takes an argument and if it's an array, returns the first item in the array
@@ -965,8 +970,12 @@
965
970
  touchEnd: touchEnd
966
971
  });
967
972
 
968
- var Downshift = /*#__PURE__*/function () {
969
- var Downshift = /*#__PURE__*/function (_Component) {
973
+ var Downshift =
974
+ /*#__PURE__*/
975
+ function () {
976
+ var Downshift =
977
+ /*#__PURE__*/
978
+ function (_Component) {
970
979
  _inheritsLoose(Downshift, _Component);
971
980
 
972
981
  function Downshift(_props) {
@@ -1240,6 +1249,10 @@
1240
1249
  }
1241
1250
  },
1242
1251
  Enter: function Enter(event) {
1252
+ if (event.which === 229) {
1253
+ return;
1254
+ }
1255
+
1243
1256
  var _this$getState2 = this.getState(),
1244
1257
  isOpen = _this$getState2.isOpen,
1245
1258
  highlightedIndex = _this$getState2.highlightedIndex;
@@ -2070,6 +2083,65 @@
2070
2083
  inputValue: ''
2071
2084
  };
2072
2085
 
2086
+ function callOnChangeProps(action, state, newState) {
2087
+ var props = action.props,
2088
+ type = action.type;
2089
+ var changes = {};
2090
+ Object.keys(state).forEach(function (key) {
2091
+ invokeOnChangeHandler(key, props, state, newState);
2092
+
2093
+ if (newState[key] !== state[key]) {
2094
+ changes[key] = newState[key];
2095
+ }
2096
+ });
2097
+
2098
+ if (props.onStateChange && Object.keys(changes).length) {
2099
+ props.onStateChange(_extends({
2100
+ type: type
2101
+ }, changes));
2102
+ }
2103
+ }
2104
+
2105
+ function invokeOnChangeHandler(key, props, state, newState) {
2106
+ var handler = "on" + capitalizeString(key) + "Change";
2107
+
2108
+ if (props[handler] && newState[key] !== undefined && newState[key] !== state[key]) {
2109
+ props[handler](newState);
2110
+ }
2111
+ }
2112
+ /**
2113
+ * Default state reducer that returns the changes.
2114
+ *
2115
+ * @param {Object} s state.
2116
+ * @param {Object} a action with changes.
2117
+ * @returns {Object} changes.
2118
+ */
2119
+
2120
+
2121
+ function stateReducer(s, a) {
2122
+ return a.changes;
2123
+ }
2124
+ /**
2125
+ * Returns a message to be added to aria-live region when item is selected.
2126
+ *
2127
+ * @param {Object} selectionParameters Parameters required to build the message.
2128
+ * @returns {string} The a11y message.
2129
+ */
2130
+
2131
+
2132
+ function getA11ySelectionMessage(selectionParameters) {
2133
+ var selectedItem = selectionParameters.selectedItem,
2134
+ itemToStringLocal = selectionParameters.itemToString;
2135
+ return selectedItem ? itemToStringLocal(selectedItem) + " has been selected." : '';
2136
+ }
2137
+ /**
2138
+ * Debounced call for updating the a11y message.
2139
+ */
2140
+
2141
+
2142
+ var updateA11yStatus = debounce(function (getA11yMessage, document) {
2143
+ setStatus(getA11yMessage(), document);
2144
+ }, 200);
2073
2145
  function getElementIds(_ref) {
2074
2146
  var id = _ref.id,
2075
2147
  labelId = _ref.labelId,
@@ -2086,7 +2158,6 @@
2086
2158
  toggleButtonId: toggleButtonId || uniqueId + "-toggle-button"
2087
2159
  };
2088
2160
  }
2089
-
2090
2161
  function getItemIndex(index, item, items) {
2091
2162
  if (index !== undefined) {
2092
2163
  return index;
@@ -2115,57 +2186,39 @@
2115
2186
  });
2116
2187
  };
2117
2188
  }
2118
-
2119
2189
  function isAcceptedCharacterKey(key) {
2120
2190
  return /^\S{1}$/.test(key);
2121
2191
  }
2122
-
2123
2192
  function capitalizeString(string) {
2124
2193
  return "" + string.slice(0, 1).toUpperCase() + string.slice(1);
2125
2194
  }
2195
+ /**
2196
+ * Computes the controlled state using a the previous state, props,
2197
+ * two reducers, one from downshift and an optional one from the user.
2198
+ * Also calls the onChange handlers for state values that have changed.
2199
+ *
2200
+ * @param {Function} reducer Reducer function from downshift.
2201
+ * @param {Object} initialState Initial state of the hook.
2202
+ * @param {Object} props The hook props.
2203
+ * @returns {Array} An array with the state and an action dispatcher.
2204
+ */
2126
2205
 
2127
- function invokeOnChangeHandler(key, props, state, newState) {
2128
- var handler = "on" + capitalizeString(key) + "Change";
2129
-
2130
- if (props[handler] && newState[key] !== undefined && newState[key] !== state[key]) {
2131
- props[handler](newState);
2132
- }
2133
- }
2134
-
2135
- function callOnChangeProps(action, state, newState) {
2136
- var props = action.props,
2137
- type = action.type;
2138
- var changes = {};
2139
- Object.keys(state).forEach(function (key) {
2140
- invokeOnChangeHandler(key, props, state, newState);
2206
+ function useControlledState(reducer, initialState, props) {
2207
+ var _useState = preact.useState(initialState),
2208
+ uncontrolledState = _useState[0],
2209
+ setState = _useState[1];
2141
2210
 
2142
- if (newState[key] !== state[key]) {
2143
- changes[key] = newState[key];
2144
- }
2145
- });
2211
+ var state = getState(uncontrolledState, props);
2146
2212
 
2147
- if (props.onStateChange && Object.keys(changes).length) {
2148
- props.onStateChange(_extends({
2149
- type: type
2150
- }, changes));
2151
- }
2152
- }
2153
-
2154
- function useEnhancedReducer(reducer, initialState, props) {
2155
- var enhancedReducer = preact.useCallback(function (state, action) {
2156
- state = getState(state, action.props);
2157
- var stateReduceLocal = action.props.stateReducer;
2213
+ var dispatch = function (action) {
2214
+ var stateReducerFromProps = action.props.stateReducer;
2158
2215
  var changes = reducer(state, action);
2159
- var newState = stateReduceLocal(state, _extends({}, action, {
2216
+ var newState = stateReducerFromProps(state, _extends({}, action, {
2160
2217
  changes: changes
2161
2218
  }));
2162
2219
  callOnChangeProps(action, state, newState);
2163
- return newState;
2164
- }, [reducer]);
2165
-
2166
- var _useReducer = preact.useReducer(enhancedReducer, initialState),
2167
- state = _useReducer[0],
2168
- dispatch = _useReducer[1];
2220
+ setState(newState);
2221
+ };
2169
2222
 
2170
2223
  return [getState(state, props), function dispatchWithProps(action) {
2171
2224
  return dispatch(_extends({
@@ -2173,32 +2226,6 @@
2173
2226
  }, action));
2174
2227
  }];
2175
2228
  }
2176
- /**
2177
- * Default state reducer that returns the changes.
2178
- *
2179
- * @param {Object} s state.
2180
- * @param {Object} a action with changes.
2181
- * @returns {Object} changes.
2182
- */
2183
-
2184
-
2185
- function stateReducer(s, a) {
2186
- return a.changes;
2187
- }
2188
- /**
2189
- * Returns a message to be added to aria-live region when item is selected.
2190
- *
2191
- * @param {Object} selectionParameters Parameters required to build the message.
2192
- * @returns {string} The a11y message.
2193
- */
2194
-
2195
-
2196
- function getA11ySelectionMessage(selectionParameters) {
2197
- var selectedItem = selectionParameters.selectedItem,
2198
- itemToStringLocal = selectionParameters.itemToString;
2199
- return itemToStringLocal(selectedItem) + " has been selected.";
2200
- }
2201
-
2202
2229
  var defaultProps = {
2203
2230
  itemToString: itemToString,
2204
2231
  stateReducer: stateReducer,
@@ -2209,7 +2236,6 @@
2209
2236
  /* istanbul ignore next (ssr) */
2210
2237
  ? {} : window
2211
2238
  };
2212
-
2213
2239
  function getDefaultValue(props, propKey, defaultStateValues) {
2214
2240
  if (defaultStateValues === void 0) {
2215
2241
  defaultStateValues = dropdownDefaultStateValues;
@@ -2223,7 +2249,6 @@
2223
2249
 
2224
2250
  return defaultStateValues[propKey];
2225
2251
  }
2226
-
2227
2252
  function getInitialValue(props, propKey, defaultStateValues) {
2228
2253
  if (defaultStateValues === void 0) {
2229
2254
  defaultStateValues = dropdownDefaultStateValues;
@@ -2241,7 +2266,6 @@
2241
2266
 
2242
2267
  return getDefaultValue(props, propKey, defaultStateValues);
2243
2268
  }
2244
-
2245
2269
  function getInitialState(props) {
2246
2270
  var selectedItem = getInitialValue(props, 'selectedItem');
2247
2271
  var isOpen = getInitialValue(props, 'isOpen');
@@ -2254,7 +2278,6 @@
2254
2278
  inputValue: inputValue
2255
2279
  };
2256
2280
  }
2257
-
2258
2281
  function getHighlightedIndexOnOpen(props, state, offset, getItemNodeFromIndex) {
2259
2282
  var items = props.items,
2260
2283
  initialHighlightedIndex = props.initialHighlightedIndex,
@@ -2361,7 +2384,8 @@
2361
2384
 
2362
2385
  function getA11yStatusMessage$1(_ref) {
2363
2386
  var isOpen = _ref.isOpen,
2364
- resultCount = _ref.resultCount;
2387
+ resultCount = _ref.resultCount,
2388
+ previousResultCount = _ref.previousResultCount;
2365
2389
 
2366
2390
  if (!isOpen) {
2367
2391
  return '';
@@ -2371,7 +2395,11 @@
2371
2395
  return 'No results are available.';
2372
2396
  }
2373
2397
 
2374
- return resultCount + " result" + (resultCount === 1 ? ' is' : 's are') + " available, use up and down arrow keys to navigate. Press Enter or Space Bar keys to select.";
2398
+ if (resultCount !== previousResultCount) {
2399
+ return resultCount + " result" + (resultCount === 1 ? ' is' : 's are') + " available, use up and down arrow keys to navigate. Press Enter or Space Bar keys to select.";
2400
+ }
2401
+
2402
+ return '';
2375
2403
  }
2376
2404
 
2377
2405
  var defaultProps$1 = _extends({}, defaultProps, {
@@ -2618,24 +2646,23 @@
2618
2646
  var props = _extends({}, defaultProps$1, {}, userProps);
2619
2647
 
2620
2648
  var items = props.items,
2621
- itemToString = props.itemToString,
2622
- getA11yStatusMessage = props.getA11yStatusMessage,
2623
- getA11ySelectionMessage = props.getA11ySelectionMessage,
2624
2649
  scrollIntoView = props.scrollIntoView,
2625
2650
  environment = props.environment,
2626
2651
  initialIsOpen = props.initialIsOpen,
2627
- defaultIsOpen = props.defaultIsOpen; // Initial state depending on controlled props.
2652
+ defaultIsOpen = props.defaultIsOpen,
2653
+ itemToString = props.itemToString,
2654
+ getA11ySelectionMessage = props.getA11ySelectionMessage,
2655
+ getA11yStatusMessage = props.getA11yStatusMessage; // Initial state depending on controlled props.
2628
2656
 
2629
2657
  var initialState = getInitialState(props); // Reducer init.
2630
2658
 
2631
- var _useEnhancedReducer = useEnhancedReducer(downshiftSelectReducer, initialState, props),
2632
- _useEnhancedReducer$ = _useEnhancedReducer[0],
2633
- isOpen = _useEnhancedReducer$.isOpen,
2634
- highlightedIndex = _useEnhancedReducer$.highlightedIndex,
2635
- selectedItem = _useEnhancedReducer$.selectedItem,
2636
- inputValue = _useEnhancedReducer$.inputValue,
2637
- dispatch = _useEnhancedReducer[1];
2638
- /* Refs */
2659
+ var _useControlledState = useControlledState(downshiftSelectReducer, initialState, props),
2660
+ _useControlledState$ = _useControlledState[0],
2661
+ isOpen = _useControlledState$.isOpen,
2662
+ highlightedIndex = _useControlledState$.highlightedIndex,
2663
+ selectedItem = _useControlledState$.selectedItem,
2664
+ inputValue = _useControlledState$.inputValue,
2665
+ dispatch = _useControlledState[1]; // Refs
2639
2666
 
2640
2667
 
2641
2668
  var toggleButtonRef = preact.useRef(null);
@@ -2647,13 +2674,14 @@
2647
2674
  isMouseDown: false,
2648
2675
  isTouchMove: false
2649
2676
  });
2650
- var elementIds = preact.useRef(getElementIds(props)); // Some utils.
2677
+ var elementIds = preact.useRef(getElementIds(props));
2678
+ var previousResultCountRef = preact.useRef(); // Some utils.
2651
2679
 
2652
2680
  var getItemNodeFromIndex = function (index) {
2653
2681
  return environment.document.getElementById(elementIds.current.getItemId(index));
2654
2682
  }; // Effects.
2655
2683
 
2656
- /* Sets a11y status message on changes in isOpen. */
2684
+ /* Sets a11y status message on changes in state. */
2657
2685
 
2658
2686
 
2659
2687
  preact.useEffect(function () {
@@ -2661,16 +2689,20 @@
2661
2689
  return;
2662
2690
  }
2663
2691
 
2664
- setStatus(getA11yStatusMessage({
2665
- highlightedIndex: highlightedIndex,
2666
- inputValue: inputValue,
2667
- isOpen: isOpen,
2668
- itemToString: itemToString,
2669
- resultCount: items.length,
2670
- highlightedItem: items[highlightedIndex],
2671
- selectedItem: selectedItem
2672
- }), environment.document); // eslint-disable-next-line react-hooks/exhaustive-deps
2673
- }, [isOpen]);
2692
+ var previousResultCount = previousResultCountRef.current;
2693
+ updateA11yStatus(function () {
2694
+ return getA11yStatusMessage({
2695
+ isOpen: isOpen,
2696
+ highlightedIndex: highlightedIndex,
2697
+ selectedItem: selectedItem,
2698
+ inputValue: inputValue,
2699
+ highlightedItem: items[highlightedIndex],
2700
+ resultCount: items.length,
2701
+ itemToString: itemToString,
2702
+ previousResultCount: previousResultCount
2703
+ });
2704
+ }, environment.document); // eslint-disable-next-line react-hooks/exhaustive-deps
2705
+ }, [isOpen, highlightedIndex, selectedItem, inputValue]);
2674
2706
  /* Sets a11y status message on changes in selectedItem. */
2675
2707
 
2676
2708
  preact.useEffect(function () {
@@ -2678,15 +2710,19 @@
2678
2710
  return;
2679
2711
  }
2680
2712
 
2681
- setStatus(getA11ySelectionMessage({
2682
- highlightedIndex: highlightedIndex,
2683
- inputValue: inputValue,
2684
- isOpen: isOpen,
2685
- itemToString: itemToString,
2686
- resultCount: items.length,
2687
- highlightedItem: items[highlightedIndex],
2688
- selectedItem: selectedItem
2689
- }), environment.document); // eslint-disable-next-line react-hooks/exhaustive-deps
2713
+ var previousResultCount = previousResultCountRef.current;
2714
+ updateA11yStatus(function () {
2715
+ return getA11ySelectionMessage({
2716
+ isOpen: isOpen,
2717
+ highlightedIndex: highlightedIndex,
2718
+ selectedItem: selectedItem,
2719
+ inputValue: inputValue,
2720
+ highlightedItem: items[highlightedIndex],
2721
+ resultCount: items.length,
2722
+ itemToString: itemToString,
2723
+ previousResultCount: previousResultCount
2724
+ });
2725
+ }, environment.document); // eslint-disable-next-line react-hooks/exhaustive-deps
2690
2726
  }, [selectedItem]);
2691
2727
  /* Sets cleanup for the keysSoFar after 500ms. */
2692
2728
 
@@ -2743,6 +2779,13 @@
2743
2779
  } // eslint-disable-next-line react-hooks/exhaustive-deps
2744
2780
 
2745
2781
  }, [highlightedIndex]);
2782
+ preact.useEffect(function () {
2783
+ if (isInitialMount.current) {
2784
+ return;
2785
+ }
2786
+
2787
+ previousResultCountRef.current = items.length;
2788
+ });
2746
2789
  /* Make initial ref false. */
2747
2790
 
2748
2791
  preact.useEffect(function () {
@@ -3066,6 +3109,51 @@
3066
3109
  };
3067
3110
  }
3068
3111
 
3112
+ var InputKeyDownArrowDown = '__input_keydown_arrow_down__';
3113
+ var InputKeyDownArrowUp = '__input_keydown_arrow_up__';
3114
+ var InputKeyDownEscape = '__input_keydown_escape__';
3115
+ var InputKeyDownHome = '__input_keydown_home__';
3116
+ var InputKeyDownEnd = '__input_keydown_end__';
3117
+ var InputKeyDownEnter = '__input_keydown_enter__';
3118
+ var InputChange = '__input_change__';
3119
+ var InputBlur = '__input_blur__';
3120
+ var MenuMouseLeave$1 = '__menu_mouse_leave__';
3121
+ var ItemMouseMove$1 = '__item_mouse_move__';
3122
+ var ItemClick$1 = '__item_click__';
3123
+ var ToggleButtonClick$1 = '__togglebutton_click__';
3124
+ var FunctionToggleMenu$1 = '__function_toggle_menu__';
3125
+ var FunctionOpenMenu$1 = '__function_open_menu__';
3126
+ var FunctionCloseMenu$1 = '__function_close_menu__';
3127
+ var FunctionSetHighlightedIndex$1 = '__function_set_highlighted_index__';
3128
+ var FunctionSelectItem$1 = '__function_select_item__';
3129
+ var FunctionSetInputValue$1 = '__function_set_input_value__';
3130
+ var FunctionReset$1 = '__function_reset__';
3131
+ var ControlledPropUpdatedSelectedItem = '__controlled_prop_updated_selected_item__';
3132
+
3133
+ var stateChangeTypes$2 = /*#__PURE__*/Object.freeze({
3134
+ __proto__: null,
3135
+ InputKeyDownArrowDown: InputKeyDownArrowDown,
3136
+ InputKeyDownArrowUp: InputKeyDownArrowUp,
3137
+ InputKeyDownEscape: InputKeyDownEscape,
3138
+ InputKeyDownHome: InputKeyDownHome,
3139
+ InputKeyDownEnd: InputKeyDownEnd,
3140
+ InputKeyDownEnter: InputKeyDownEnter,
3141
+ InputChange: InputChange,
3142
+ InputBlur: InputBlur,
3143
+ MenuMouseLeave: MenuMouseLeave$1,
3144
+ ItemMouseMove: ItemMouseMove$1,
3145
+ ItemClick: ItemClick$1,
3146
+ ToggleButtonClick: ToggleButtonClick$1,
3147
+ FunctionToggleMenu: FunctionToggleMenu$1,
3148
+ FunctionOpenMenu: FunctionOpenMenu$1,
3149
+ FunctionCloseMenu: FunctionCloseMenu$1,
3150
+ FunctionSetHighlightedIndex: FunctionSetHighlightedIndex$1,
3151
+ FunctionSelectItem: FunctionSelectItem$1,
3152
+ FunctionSetInputValue: FunctionSetInputValue$1,
3153
+ FunctionReset: FunctionReset$1,
3154
+ ControlledPropUpdatedSelectedItem: ControlledPropUpdatedSelectedItem
3155
+ });
3156
+
3069
3157
  function getElementIds$1(_ref) {
3070
3158
  var id = _ref.id,
3071
3159
  inputId = _ref.inputId,
@@ -3078,7 +3166,6 @@
3078
3166
  id: id
3079
3167
  }, rest)));
3080
3168
  }
3081
-
3082
3169
  function getInitialState$1(props) {
3083
3170
  var initialState = getInitialState(props);
3084
3171
  var selectedItem = initialState.selectedItem;
@@ -3092,7 +3179,6 @@
3092
3179
  inputValue: inputValue
3093
3180
  });
3094
3181
  }
3095
-
3096
3182
  var propTypes$1 = {
3097
3183
  items: PropTypes.array.isRequired,
3098
3184
  itemToString: PropTypes.func,
@@ -3133,55 +3219,43 @@
3133
3219
  })
3134
3220
  })
3135
3221
  };
3222
+ /**
3223
+ * The useCombobox version of useControlledState, which also
3224
+ * checks if the controlled prop selectedItem changed between
3225
+ * renders. If so, it will also update inputValue with its
3226
+ * string equivalent. It uses the common useControlledState to
3227
+ * compute the rest of the state.
3228
+ *
3229
+ * @param {Function} reducer Reducer function from downshift.
3230
+ * @param {Object} initialState Initial state of the hook.
3231
+ * @param {Object} props The hook props.
3232
+ * @returns {Array} An array with the state and an action dispatcher.
3233
+ */
3234
+
3235
+ function useControlledState$1(reducer, initialState, props) {
3236
+ var _useControlledStateCo = useControlledState(reducer, initialState, props),
3237
+ newState = _useControlledStateCo[0],
3238
+ dispatch = _useControlledStateCo[1];
3239
+
3240
+ var previousSelectedItemRef = preact.useRef(null);
3241
+ var selectedItem = props.selectedItem,
3242
+ itemToString = props.itemToString; // ToDo: if needed, make same approach as selectedItemChanged from Downshift.
3243
+
3244
+ if (isControlledProp(props, 'selectedItem') && previousSelectedItemRef.current !== selectedItem) {
3245
+ dispatch({
3246
+ type: ControlledPropUpdatedSelectedItem,
3247
+ inputValue: itemToString(selectedItem)
3248
+ });
3249
+ }
3136
3250
 
3251
+ previousSelectedItemRef.current = selectedItem;
3252
+ return [newState, dispatch];
3253
+ }
3137
3254
  var defaultProps$2 = _extends({}, defaultProps, {
3138
3255
  getA11yStatusMessage: getA11yStatusMessage,
3139
3256
  circularNavigation: true
3140
3257
  });
3141
3258
 
3142
- var InputKeyDownArrowDown = '__input_keydown_arrow_down__';
3143
- var InputKeyDownArrowUp = '__input_keydown_arrow_up__';
3144
- var InputKeyDownEscape = '__input_keydown_escape__';
3145
- var InputKeyDownHome = '__input_keydown_home__';
3146
- var InputKeyDownEnd = '__input_keydown_end__';
3147
- var InputKeyDownEnter = '__input_keydown_enter__';
3148
- var InputChange = '__input_change__';
3149
- var InputBlur = '__input_blur__';
3150
- var MenuMouseLeave$1 = '__menu_mouse_leave__';
3151
- var ItemMouseMove$1 = '__item_mouse_move__';
3152
- var ItemClick$1 = '__item_click__';
3153
- var ToggleButtonClick$1 = '__togglebutton_click__';
3154
- var FunctionToggleMenu$1 = '__function_toggle_menu__';
3155
- var FunctionOpenMenu$1 = '__function_open_menu__';
3156
- var FunctionCloseMenu$1 = '__function_close_menu__';
3157
- var FunctionSetHighlightedIndex$1 = '__function_set_highlighted_index__';
3158
- var FunctionSelectItem$1 = '__function_select_item__';
3159
- var FunctionSetInputValue$1 = '__function_set_input_value__';
3160
- var FunctionReset$1 = '__function_reset__';
3161
-
3162
- var stateChangeTypes$2 = /*#__PURE__*/Object.freeze({
3163
- __proto__: null,
3164
- InputKeyDownArrowDown: InputKeyDownArrowDown,
3165
- InputKeyDownArrowUp: InputKeyDownArrowUp,
3166
- InputKeyDownEscape: InputKeyDownEscape,
3167
- InputKeyDownHome: InputKeyDownHome,
3168
- InputKeyDownEnd: InputKeyDownEnd,
3169
- InputKeyDownEnter: InputKeyDownEnter,
3170
- InputChange: InputChange,
3171
- InputBlur: InputBlur,
3172
- MenuMouseLeave: MenuMouseLeave$1,
3173
- ItemMouseMove: ItemMouseMove$1,
3174
- ItemClick: ItemClick$1,
3175
- ToggleButtonClick: ToggleButtonClick$1,
3176
- FunctionToggleMenu: FunctionToggleMenu$1,
3177
- FunctionOpenMenu: FunctionOpenMenu$1,
3178
- FunctionCloseMenu: FunctionCloseMenu$1,
3179
- FunctionSetHighlightedIndex: FunctionSetHighlightedIndex$1,
3180
- FunctionSelectItem: FunctionSelectItem$1,
3181
- FunctionSetInputValue: FunctionSetInputValue$1,
3182
- FunctionReset: FunctionReset$1
3183
- });
3184
-
3185
3259
  /* eslint-disable complexity */
3186
3260
 
3187
3261
  function downshiftUseComboboxReducer(state, action) {
@@ -3321,6 +3395,7 @@
3321
3395
  };
3322
3396
  break;
3323
3397
 
3398
+ case ControlledPropUpdatedSelectedItem:
3324
3399
  case FunctionSetInputValue$1:
3325
3400
  changes = {
3326
3401
  inputValue: action.inputValue
@@ -3361,20 +3436,20 @@
3361
3436
  defaultIsOpen = props.defaultIsOpen,
3362
3437
  items = props.items,
3363
3438
  scrollIntoView = props.scrollIntoView,
3364
- getA11ySelectionMessage = props.getA11ySelectionMessage,
3439
+ environment = props.environment,
3365
3440
  getA11yStatusMessage = props.getA11yStatusMessage,
3366
- itemToString = props.itemToString,
3367
- environment = props.environment; // Initial state depending on controlled props.
3441
+ getA11ySelectionMessage = props.getA11ySelectionMessage,
3442
+ itemToString = props.itemToString; // Initial state depending on controlled props.
3368
3443
 
3369
3444
  var initialState = getInitialState$1(props); // Reducer init.
3370
3445
 
3371
- var _useEnhancedReducer = useEnhancedReducer(downshiftUseComboboxReducer, initialState, props),
3372
- _useEnhancedReducer$ = _useEnhancedReducer[0],
3373
- isOpen = _useEnhancedReducer$.isOpen,
3374
- highlightedIndex = _useEnhancedReducer$.highlightedIndex,
3375
- selectedItem = _useEnhancedReducer$.selectedItem,
3376
- inputValue = _useEnhancedReducer$.inputValue,
3377
- dispatch = _useEnhancedReducer[1];
3446
+ var _useControlledState = useControlledState$1(downshiftUseComboboxReducer, initialState, props),
3447
+ _useControlledState$ = _useControlledState[0],
3448
+ isOpen = _useControlledState$.isOpen,
3449
+ highlightedIndex = _useControlledState$.highlightedIndex,
3450
+ selectedItem = _useControlledState$.selectedItem,
3451
+ inputValue = _useControlledState$.inputValue,
3452
+ dispatch = _useControlledState[1];
3378
3453
  /* Refs */
3379
3454
 
3380
3455
 
@@ -3391,25 +3466,30 @@
3391
3466
  isTouchMove: false
3392
3467
  });
3393
3468
  var elementIds = preact.useRef(getElementIds$1(props));
3469
+ var previousResultCountRef = preact.useRef();
3394
3470
  /* Effects */
3395
3471
 
3396
- /* Sets a11y status message on changes in isOpen. */
3472
+ /* Sets a11y status message on changes in state. */
3397
3473
 
3398
3474
  preact.useEffect(function () {
3399
3475
  if (isInitialMount.current) {
3400
3476
  return;
3401
3477
  }
3402
3478
 
3403
- setStatus(getA11yStatusMessage({
3404
- highlightedIndex: highlightedIndex,
3405
- inputValue: inputValue,
3406
- isOpen: isOpen,
3407
- itemToString: itemToString,
3408
- resultCount: items.length,
3409
- highlightedItem: items[highlightedIndex],
3410
- selectedItem: selectedItem
3411
- }), environment.document); // eslint-disable-next-line react-hooks/exhaustive-deps
3412
- }, [isOpen]);
3479
+ var previousResultCount = previousResultCountRef.current;
3480
+ updateA11yStatus(function () {
3481
+ return getA11yStatusMessage({
3482
+ isOpen: isOpen,
3483
+ highlightedIndex: highlightedIndex,
3484
+ selectedItem: selectedItem,
3485
+ inputValue: inputValue,
3486
+ highlightedItem: items[highlightedIndex],
3487
+ resultCount: items.length,
3488
+ itemToString: itemToString,
3489
+ previousResultCount: previousResultCount
3490
+ });
3491
+ }, environment.document); // eslint-disable-next-line react-hooks/exhaustive-deps
3492
+ }, [isOpen, highlightedIndex, selectedItem, inputValue]);
3413
3493
  /* Sets a11y status message on changes in selectedItem. */
3414
3494
 
3415
3495
  preact.useEffect(function () {
@@ -3417,15 +3497,19 @@
3417
3497
  return;
3418
3498
  }
3419
3499
 
3420
- setStatus(getA11ySelectionMessage({
3421
- highlightedIndex: highlightedIndex,
3422
- inputValue: inputValue,
3423
- isOpen: isOpen,
3424
- itemToString: itemToString,
3425
- resultCount: items.length,
3426
- highlightedItem: items[highlightedIndex],
3427
- selectedItem: selectedItem
3428
- }), environment.document); // eslint-disable-next-line react-hooks/exhaustive-deps
3500
+ var previousResultCount = previousResultCountRef.current;
3501
+ updateA11yStatus(function () {
3502
+ return getA11ySelectionMessage({
3503
+ isOpen: isOpen,
3504
+ highlightedIndex: highlightedIndex,
3505
+ selectedItem: selectedItem,
3506
+ inputValue: inputValue,
3507
+ highlightedItem: items[highlightedIndex],
3508
+ resultCount: items.length,
3509
+ itemToString: itemToString,
3510
+ previousResultCount: previousResultCount
3511
+ });
3512
+ }, environment.document); // eslint-disable-next-line react-hooks/exhaustive-deps
3429
3513
  }, [selectedItem]);
3430
3514
  /* Scroll on highlighted item if change comes from keyboard. */
3431
3515
 
@@ -3455,8 +3539,13 @@
3455
3539
  } // eslint-disable-next-line react-hooks/exhaustive-deps
3456
3540
 
3457
3541
  }, [isOpen]);
3458
- /* Make initial ref false. */
3542
+ preact.useEffect(function () {
3543
+ if (isInitialMount.current) {
3544
+ return;
3545
+ }
3459
3546
 
3547
+ previousResultCountRef.current = items.length;
3548
+ });
3460
3549
  preact.useEffect(function () {
3461
3550
  isInitialMount.current = false;
3462
3551
  }, []);
@@ -3552,6 +3641,11 @@
3552
3641
  });
3553
3642
  },
3554
3643
  Enter: function Enter(event) {
3644
+ // if IME composing, wait for next Enter keydown event.
3645
+ if (event.which === 229) {
3646
+ return;
3647
+ }
3648
+
3555
3649
  event.preventDefault();
3556
3650
  dispatch({
3557
3651
  type: InputKeyDownEnter,
@@ -4082,11 +4176,11 @@
4082
4176
  keyNavigationNext = props.keyNavigationNext,
4083
4177
  keyNavigationPrevious = props.keyNavigationPrevious; // Reducer init.
4084
4178
 
4085
- var _useEnhancedReducer = useEnhancedReducer(downshiftMultipleSelectionReducer, getInitialState$2(props), props),
4086
- _useEnhancedReducer$ = _useEnhancedReducer[0],
4087
- activeIndex = _useEnhancedReducer$.activeIndex,
4088
- selectedItems = _useEnhancedReducer$.selectedItems,
4089
- dispatch = _useEnhancedReducer[1]; // Refs.
4179
+ var _useControlledState = useControlledState(downshiftMultipleSelectionReducer, getInitialState$2(props), props),
4180
+ _useControlledState$ = _useControlledState[0],
4181
+ activeIndex = _useControlledState$.activeIndex,
4182
+ selectedItems = _useControlledState$.selectedItems,
4183
+ dispatch = _useControlledState[1]; // Refs.
4090
4184
 
4091
4185
 
4092
4186
  var isInitialMount = preact.useRef(true);