downshift 9.3.2 → 9.4.0-alpha.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.
Files changed (155) hide show
  1. package/dist/downshift.cjs.cjs +617 -698
  2. package/dist/downshift.d.ts +49 -55
  3. package/dist/downshift.esm.mjs +618 -699
  4. package/dist/downshift.native.cjs.cjs +575 -653
  5. package/dist/downshift.nativeweb.cjs.cjs +615 -693
  6. package/dist/downshift.types.d.ts +205 -0
  7. package/dist/downshift.umd.js +621 -702
  8. package/dist/downshift.umd.js.map +1 -1
  9. package/dist/downshift.umd.min.js +1 -1
  10. package/dist/downshift.umd.min.js.map +1 -1
  11. package/dist/hooks/testUtils/MemoizedItem.d.ts +10 -0
  12. package/dist/hooks/testUtils/fixtures.d.ts +16 -0
  13. package/dist/hooks/testUtils/index.d.ts +4 -0
  14. package/dist/hooks/testUtils/interactions.d.ts +16 -0
  15. package/dist/hooks/testUtils/testCases.d.ts +2 -0
  16. package/dist/hooks/useCombobox/__tests__/utils/index.d.ts +5 -0
  17. package/dist/hooks/useCombobox/__tests__/utils/interactions.d.ts +6 -0
  18. package/dist/hooks/useCombobox/__tests__/utils/renderCombobox.d.ts +72 -0
  19. package/dist/hooks/useCombobox/__tests__/utils/renderUseCombobox.d.ts +2 -0
  20. package/dist/hooks/useCombobox/index.d.ts +4 -94
  21. package/dist/hooks/useCombobox/index.types.d.ts +302 -0
  22. package/dist/hooks/useCombobox/reducer.d.ts +4 -1
  23. package/dist/hooks/useCombobox/stateChangeTypes.d.ts +24 -23
  24. package/dist/hooks/useCombobox/utils/getInitialState.d.ts +10 -0
  25. package/dist/hooks/useCombobox/utils/index.d.ts +3 -0
  26. package/dist/hooks/useCombobox/{utils.d.ts → utils/propTypes.d.ts} +2 -30
  27. package/dist/hooks/useCombobox/utils/useControlledReducer.d.ts +16 -0
  28. package/dist/hooks/useMultipleSelection/index.d.ts +25 -13
  29. package/dist/hooks/useMultipleSelection/index.types.d.ts +117 -0
  30. package/dist/hooks/useMultipleSelection/reducer.d.ts +1 -1
  31. package/dist/hooks/useMultipleSelection/utils.d.ts +14 -15
  32. package/dist/hooks/useSelect/__tests__/utils/getItemIndexByCharacter.d.ts +7 -0
  33. package/dist/hooks/useSelect/__tests__/utils/index.d.ts +6 -0
  34. package/dist/hooks/useSelect/__tests__/utils/renderSelect.d.ts +71 -0
  35. package/dist/hooks/useSelect/__tests__/utils/renderUseSelect.d.ts +2 -0
  36. package/dist/hooks/useSelect/__tests__/utils/stateChangeTestCases.d.ts +182 -0
  37. package/dist/hooks/useSelect/index.d.ts +4 -71
  38. package/dist/hooks/useSelect/index.types.d.ts +266 -0
  39. package/dist/hooks/useSelect/reducer.d.ts +4 -1
  40. package/dist/hooks/useSelect/stateChangeTypes.d.ts +23 -22
  41. package/dist/hooks/useSelect/utils/index.d.ts +0 -1
  42. package/dist/hooks/useTagGroup/__tests__/utils/renderTagGroup.d.ts +1 -1
  43. package/dist/hooks/useTagGroup/index.types.d.ts +10 -13
  44. package/dist/hooks/useTagGroup/reducer.d.ts +4 -2
  45. package/dist/hooks/useTagGroup/utils/index.d.ts +3 -1
  46. package/dist/hooks/utils/__tests__/dropdownDefaultProps.test.d.ts +1 -0
  47. package/dist/hooks/utils/__tests__/getChangesOnSelection.test.d.ts +1 -0
  48. package/dist/hooks/utils/__tests__/getDefaultHighlightedIndex.test.d.ts +1 -0
  49. package/dist/hooks/utils/__tests__/getDefaultValue.test.d.ts +1 -0
  50. package/dist/hooks/utils/__tests__/getHighlightedIndexOnOpen.test.d.ts +1 -0
  51. package/dist/hooks/utils/__tests__/getInitialValue.test.d.ts +1 -0
  52. package/dist/hooks/utils/__tests__/getItemAndIndex.test.d.ts +1 -0
  53. package/dist/hooks/utils/__tests__/isDropdownStateEqual.test.d.ts +1 -0
  54. package/dist/hooks/utils/__tests__/useA11yMessageStatus.test.d.ts +1 -0
  55. package/dist/hooks/utils/__tests__/useControlPropsValidator.test.d.ts +1 -0
  56. package/dist/hooks/utils/__tests__/useControlledReducer.test.d.ts +1 -0
  57. package/dist/hooks/utils/__tests__/useElementIds.legacy.test.d.ts +1 -0
  58. package/dist/hooks/utils/__tests__/useElementIds.r18.test.d.ts +1 -0
  59. package/dist/hooks/utils/__tests__/useEnhancedReducer.test.d.ts +1 -0
  60. package/dist/hooks/utils/__tests__/useGetterPropsCalledChecker.test.d.ts +1 -0
  61. package/dist/hooks/utils/__tests__/useIsInitialMount.test.d.ts +1 -0
  62. package/dist/hooks/utils/__tests__/useMouseAndTouchTracker.test.d.ts +1 -0
  63. package/dist/hooks/utils/__tests__/useScrollIntoView.test.d.ts +1 -0
  64. package/dist/hooks/utils/callOnChangeProps.d.ts +4 -0
  65. package/dist/hooks/utils/dropdownDefaultProps.d.ts +13 -0
  66. package/dist/hooks/{utils.dropdown/defaultStateValues.d.ts → utils/dropdownDefaultStateValues.d.ts} +2 -2
  67. package/dist/hooks/{utils.dropdown/propTypes.d.ts → utils/dropdownPropTypes.d.ts} +1 -1
  68. package/dist/hooks/utils/getChangesOnSelection.d.ts +17 -0
  69. package/dist/hooks/utils/getDefaultHighlightedIndex.d.ts +7 -0
  70. package/dist/hooks/utils/getDefaultValue.d.ts +8 -0
  71. package/dist/hooks/utils/getHighlightedIndexOnOpen.d.ts +16 -0
  72. package/dist/hooks/utils/getInitialState.d.ts +23 -0
  73. package/dist/hooks/utils/getInitialValue.d.ts +14 -0
  74. package/dist/hooks/utils/index.d.ts +24 -0
  75. package/dist/hooks/utils/index.types.d.ts +26 -0
  76. package/dist/hooks/utils/isDropdownStateEqual.d.ts +16 -0
  77. package/dist/hooks/utils/reducer.d.ts +27 -0
  78. package/dist/hooks/utils/useControlPropsValidator.d.ts +6 -0
  79. package/dist/hooks/utils/useControlledReducer.d.ts +13 -0
  80. package/dist/hooks/utils/useEnhancedReducer.d.ts +15 -0
  81. package/dist/hooks/utils/useGetterPropsCalledChecker.d.ts +2 -0
  82. package/dist/hooks/utils/useMouseAndTouchTracker.d.ts +15 -0
  83. package/dist/hooks/utils/useScrollIntoView.d.ts +11 -0
  84. package/dist/index.d.ts +1 -1
  85. package/dist/utils/__tests__/callAllEventHandlers.test.d.ts +1 -0
  86. package/dist/utils/__tests__/debounce.test.d.ts +1 -0
  87. package/dist/utils/__tests__/generateId.r18.test.d.ts +1 -0
  88. package/dist/utils/__tests__/generateId.test.d.ts +1 -0
  89. package/dist/utils/__tests__/getHighlightedIndex.test.d.ts +1 -0
  90. package/dist/utils/__tests__/getNonDisabledIndex.test.d.ts +1 -0
  91. package/dist/utils/__tests__/getState.test.d.ts +1 -0
  92. package/dist/utils/__tests__/handleRefs.test.d.ts +1 -0
  93. package/dist/utils/__tests__/normalizeArrowKey.test.d.ts +1 -0
  94. package/dist/utils/__tests__/scrollIntoView.test.d.ts +1 -0
  95. package/dist/utils/__tests__/setA11yStatus.test.d.ts +1 -0
  96. package/dist/utils/__tests__/targetWithinDownshift.test.d.ts +1 -0
  97. package/dist/utils/__tests__/useLatestRef.test.d.ts +1 -0
  98. package/dist/utils/__tests__/validateControlledUnchanged.test.d.ts +1 -0
  99. package/dist/utils/__tests__/validatePropTypes.test.d.ts +1 -0
  100. package/dist/utils/getHighlightedIndex.d.ts +11 -0
  101. package/dist/utils/getNonDisabledIndex.d.ts +11 -0
  102. package/dist/utils/getState.d.ts +12 -0
  103. package/dist/utils/handleRefs.d.ts +2 -0
  104. package/dist/{utils-ts → utils}/index.d.ts +5 -1
  105. package/dist/utils/normalizeArrowKey.d.ts +6 -0
  106. package/dist/utils/targetWithinDownshift.d.ts +12 -0
  107. package/dist/utils/validateControlledUnchanged.d.ts +1 -0
  108. package/dist/utils/validatePropTypes.d.ts +2 -0
  109. package/dist/utils.legacy.d.ts +54 -0
  110. package/flow-typed/npm/downshift_v2.x.x.js.flow +87 -62
  111. package/package.json +2 -4
  112. package/preact/dist/downshift.cjs.cjs +617 -698
  113. package/preact/dist/downshift.cjs.js +4265 -0
  114. package/preact/dist/downshift.esm.js +4238 -0
  115. package/preact/dist/downshift.esm.mjs +618 -699
  116. package/preact/dist/downshift.umd.js +617 -698
  117. package/preact/dist/downshift.umd.js.map +1 -1
  118. package/preact/dist/downshift.umd.min.js +1 -1
  119. package/preact/dist/downshift.umd.min.js.map +1 -1
  120. package/typings/index.d.ts +13 -21
  121. package/dist/hooks/reducer.d.ts +0 -1
  122. package/dist/hooks/useSelect/utils/defaultProps.d.ts +0 -8
  123. package/dist/hooks/utils-ts/callOnChangeProps.d.ts +0 -2
  124. package/dist/hooks/utils-ts/getDefaultValue.d.ts +0 -2
  125. package/dist/hooks/utils-ts/getInitialValue.d.ts +0 -2
  126. package/dist/hooks/utils-ts/index.d.ts +0 -11
  127. package/dist/hooks/utils-ts/stateReducer.d.ts +0 -6
  128. package/dist/hooks/utils-ts/useControlledReducer.d.ts +0 -12
  129. package/dist/hooks/utils-ts/useEnhancedReducer.d.ts +0 -13
  130. package/dist/hooks/utils.d.ts +0 -58
  131. package/dist/hooks/utils.dropdown/defaultProps.d.ts +0 -9
  132. package/dist/hooks/utils.dropdown/index.d.ts +0 -3
  133. package/dist/utils-ts/getState.d.ts +0 -22
  134. package/dist/utils-ts/handleRefs.d.ts +0 -2
  135. package/dist/utils-ts/validatePropTypes.d.ts +0 -2
  136. package/dist/utils.d.ts +0 -123
  137. package/typings/index.legacy.d.ts +0 -888
  138. /package/dist/hooks/{utils-ts/__tests__/getItemAndIndex.test.d.ts → useCombobox/utils/__tests__/getInitialState.test.d.ts} +0 -0
  139. /package/dist/hooks/{utils.dropdown/__tests__/useElementIds.legacy.test.d.ts → useCombobox/utils/__tests__/useControlledReducer.test.d.ts} +0 -0
  140. /package/dist/hooks/{utils.dropdown/__tests__/useElementIds.r18.test.d.ts → useSelect/utils/__tests__/getItemIndexByCharacterKey.test.d.ts} +0 -0
  141. /package/dist/{utils-ts/__tests__/getState.test.d.ts → hooks/utils/__tests__/callOnChangeProps.test.d.ts} +0 -0
  142. /package/dist/{utils-ts/__tests__/handleRefs.test.d.ts → hooks/utils/__tests__/capitalizeString.test.d.ts} +0 -0
  143. /package/dist/hooks/{utils-ts → utils}/capitalizeString.d.ts +0 -0
  144. /package/dist/hooks/{utils-ts → utils}/getItemAndIndex.d.ts +0 -0
  145. /package/dist/hooks/{utils-ts → utils}/propTypes.d.ts +0 -0
  146. /package/dist/hooks/{utils-ts → utils}/useA11yMessageStatus.d.ts +0 -0
  147. /package/dist/hooks/{utils.dropdown → utils}/useElementIds.d.ts +0 -0
  148. /package/dist/hooks/{utils-ts → utils}/useIsInitialMount.d.ts +0 -0
  149. /package/dist/{utils-ts → utils}/callAllEventHandlers.d.ts +0 -0
  150. /package/dist/{utils-ts → utils}/debounce.d.ts +0 -0
  151. /package/dist/{utils-ts → utils}/generateId.d.ts +0 -0
  152. /package/dist/{utils-ts → utils}/noop.d.ts +0 -0
  153. /package/dist/{utils-ts → utils}/scrollIntoView.d.ts +0 -0
  154. /package/dist/{utils-ts → utils}/setA11yStatus.d.ts +0 -0
  155. /package/dist/{utils-ts → utils}/useLatestRef.d.ts +0 -0
@@ -0,0 +1,4265 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _objectWithoutPropertiesLoose = require('@babel/runtime/helpers/objectWithoutPropertiesLoose');
6
+ var _extends = require('@babel/runtime/helpers/extends');
7
+ var _inheritsLoose = require('@babel/runtime/helpers/inheritsLoose');
8
+ var React = require('preact/compat');
9
+ var reactIs = require('react-is');
10
+ var computeScrollIntoView = require('compute-scroll-into-view');
11
+ var PropTypes = require('prop-types');
12
+
13
+ function _interopNamespaceDefault(e) {
14
+ var n = Object.create(null);
15
+ if (e) {
16
+ Object.keys(e).forEach(function (k) {
17
+ if (k !== 'default') {
18
+ var d = Object.getOwnPropertyDescriptor(e, k);
19
+ Object.defineProperty(n, k, d.get ? d : {
20
+ enumerable: true,
21
+ get: function () { return e[k]; }
22
+ });
23
+ }
24
+ });
25
+ }
26
+ n.default = e;
27
+ return Object.freeze(n);
28
+ }
29
+
30
+ var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
31
+
32
+ var unknown = process.env.NODE_ENV !== "production" ? '__autocomplete_unknown__' : 0;
33
+ var mouseUp = process.env.NODE_ENV !== "production" ? '__autocomplete_mouseup__' : 1;
34
+ var itemMouseEnter = process.env.NODE_ENV !== "production" ? '__autocomplete_item_mouseenter__' : 2;
35
+ var keyDownArrowUp = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_arrow_up__' : 3;
36
+ var keyDownArrowDown = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_arrow_down__' : 4;
37
+ var keyDownEscape = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_escape__' : 5;
38
+ var keyDownEnter = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_enter__' : 6;
39
+ var keyDownHome = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_home__' : 7;
40
+ var keyDownEnd = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_end__' : 8;
41
+ var clickItem = process.env.NODE_ENV !== "production" ? '__autocomplete_click_item__' : 9;
42
+ var blurInput = process.env.NODE_ENV !== "production" ? '__autocomplete_blur_input__' : 10;
43
+ var changeInput = process.env.NODE_ENV !== "production" ? '__autocomplete_change_input__' : 11;
44
+ var keyDownSpaceButton = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_space_button__' : 12;
45
+ var clickButton = process.env.NODE_ENV !== "production" ? '__autocomplete_click_button__' : 13;
46
+ var blurButton = process.env.NODE_ENV !== "production" ? '__autocomplete_blur_button__' : 14;
47
+ var controlledPropUpdatedSelectedItem = process.env.NODE_ENV !== "production" ? '__autocomplete_controlled_prop_updated_selected_item__' : 15;
48
+ var touchEnd = process.env.NODE_ENV !== "production" ? '__autocomplete_touchend__' : 16;
49
+
50
+ var stateChangeTypes$4 = /*#__PURE__*/Object.freeze({
51
+ __proto__: null,
52
+ blurButton: blurButton,
53
+ blurInput: blurInput,
54
+ changeInput: changeInput,
55
+ clickButton: clickButton,
56
+ clickItem: clickItem,
57
+ controlledPropUpdatedSelectedItem: controlledPropUpdatedSelectedItem,
58
+ itemMouseEnter: itemMouseEnter,
59
+ keyDownArrowDown: keyDownArrowDown,
60
+ keyDownArrowUp: keyDownArrowUp,
61
+ keyDownEnd: keyDownEnd,
62
+ keyDownEnter: keyDownEnter,
63
+ keyDownEscape: keyDownEscape,
64
+ keyDownHome: keyDownHome,
65
+ keyDownSpaceButton: keyDownSpaceButton,
66
+ mouseUp: mouseUp,
67
+ touchEnd: touchEnd,
68
+ unknown: unknown
69
+ });
70
+
71
+ var idCounter = 0;
72
+
73
+ /**
74
+ * This generates a unique ID for an instance of Downshift
75
+ * @return {string} the unique ID
76
+ */
77
+ function generateId() {
78
+ return String(idCounter++);
79
+ }
80
+
81
+ /**
82
+ * Resets idCounter to 0. Used for SSR.
83
+ */
84
+ function resetIdCounter() {
85
+ // istanbul ignore next
86
+ if ('useId' in React__namespace) {
87
+ console.warn("It is not necessary to call resetIdCounter when using React 18+");
88
+ return;
89
+ }
90
+ idCounter = 0;
91
+ }
92
+
93
+ function useLatestRef(val) {
94
+ var ref = React__namespace.useRef(val);
95
+ // technically this is not "concurrent mode safe" because we're manipulating
96
+ // the value during render (so it's not idempotent). However, the places this
97
+ // hook is used is to support memoizing callbacks which will be called
98
+ // *during* render, so we need the latest values *during* render.
99
+ // If not for this, then we'd probably want to use useLayoutEffect instead.
100
+ ref.current = val;
101
+ return ref;
102
+ }
103
+
104
+ function handleRefs$1() {
105
+ for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
106
+ refs[_key] = arguments[_key];
107
+ }
108
+ return function (node) {
109
+ refs.forEach(function (ref) {
110
+ if (typeof ref === 'function') {
111
+ ref(node);
112
+ } else if (ref) {
113
+ ref.current = node;
114
+ }
115
+ });
116
+ };
117
+ }
118
+
119
+ /**
120
+ * This is intended to be used to compose event handlers.
121
+ * They are executed in order until one of them sets
122
+ * `event.preventDownshiftDefault = true`.
123
+ * @param fns the event handler functions
124
+ * @return the event handler to add to an element
125
+ */
126
+ function callAllEventHandlers$1() {
127
+ for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
128
+ fns[_key] = arguments[_key];
129
+ }
130
+ return function (event) {
131
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
132
+ args[_key2 - 1] = arguments[_key2];
133
+ }
134
+ return fns.some(function (fn) {
135
+ if (fn) {
136
+ fn.apply(void 0, [event].concat(args));
137
+ }
138
+ return event.preventDownshiftDefault || event.hasOwnProperty('nativeEvent') && event.nativeEvent.preventDownshiftDefault;
139
+ });
140
+ };
141
+ }
142
+
143
+ /**
144
+ * Simple debounce implementation. Will call the given
145
+ * function once after the time given has passed since
146
+ * it was last called.
147
+ */
148
+ function debounce$1(fn, time) {
149
+ var timeoutId;
150
+ function cancel() {
151
+ if (timeoutId) {
152
+ clearTimeout(timeoutId);
153
+ }
154
+ }
155
+ function wrapper() {
156
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
157
+ args[_key] = arguments[_key];
158
+ }
159
+ cancel();
160
+ timeoutId = setTimeout(function () {
161
+ timeoutId = null;
162
+ fn.apply(void 0, args);
163
+ }, time);
164
+ }
165
+ wrapper.cancel = cancel;
166
+ return wrapper;
167
+ }
168
+
169
+ var cleanupStatus = debounce$1(function (document) {
170
+ getStatusDiv(document).textContent = '';
171
+ }, 500);
172
+
173
+ /**
174
+ * Get the status node or create it if it does not already exist.
175
+ */
176
+ function getStatusDiv(document) {
177
+ var statusDiv = document.getElementById('a11y-status-message');
178
+ if (statusDiv) {
179
+ return statusDiv;
180
+ }
181
+ statusDiv = document.createElement('div');
182
+ statusDiv.setAttribute('id', 'a11y-status-message');
183
+ statusDiv.setAttribute('role', 'status');
184
+ statusDiv.setAttribute('aria-live', 'polite');
185
+ statusDiv.setAttribute('aria-relevant', 'additions text');
186
+ Object.assign(statusDiv.style, {
187
+ border: '0',
188
+ clip: 'rect(0 0 0 0)',
189
+ height: '1px',
190
+ margin: '-1px',
191
+ overflow: 'hidden',
192
+ padding: '0',
193
+ position: 'absolute',
194
+ width: '1px'
195
+ });
196
+ document.body.appendChild(statusDiv);
197
+ return statusDiv;
198
+ }
199
+
200
+ /**
201
+ * Sets aria live status to a div element that's visually hidden.
202
+ */
203
+ function setStatus(status, document) {
204
+ if (!status || !document) {
205
+ return;
206
+ }
207
+ var div = getStatusDiv(document);
208
+ div.textContent = status;
209
+ cleanupStatus(document);
210
+ }
211
+
212
+ /**
213
+ * Removes the status element from the DOM
214
+ */
215
+ function cleanupStatusDiv(document) {
216
+ var statusDiv = document == null ? void 0 : document.getElementById('a11y-status-message');
217
+ if (statusDiv) {
218
+ statusDiv.remove();
219
+ }
220
+ }
221
+
222
+ function noop() {}
223
+
224
+ // eslint-disable-next-line import/no-mutable-exports
225
+ var validatePropTypes$1 = noop;
226
+ /* istanbul ignore next */
227
+ if (process.env.NODE_ENV !== 'production') {
228
+ validatePropTypes$1 = function validatePropTypes(options, caller, propTypes) {
229
+ PropTypes.checkPropTypes(propTypes, options, 'prop', caller.name);
230
+ };
231
+ }
232
+
233
+ /**
234
+ * This will perform a shallow merge of the given state object
235
+ * with the state coming from props
236
+ * (for the controlled component scenario)
237
+ * This is used in state updater functions so they're referencing
238
+ * the right state regardless of where it comes from.
239
+ *
240
+ * @param state The state of the component/hook.
241
+ * @param props The props that may contain controlled values.
242
+ * @returns The merged controlled state.
243
+ */
244
+ function getState(state, props) {
245
+ if (!props) {
246
+ return state;
247
+ }
248
+ var keys = Object.keys(state);
249
+ return keys.reduce(function (newState, key) {
250
+ if (props[key] !== undefined) {
251
+ newState[key] = props[key];
252
+ }
253
+ return newState;
254
+ }, _extends({}, state));
255
+ }
256
+
257
+ /**
258
+ * Scroll node into view if necessary
259
+ * @param {HTMLElement} node the element that should scroll into view
260
+ * @param {HTMLElement} menuNode the menu element of the component
261
+ */
262
+ function scrollIntoView(node, menuNode) {
263
+ if (!node) {
264
+ return;
265
+ }
266
+ var actions = computeScrollIntoView.compute(node, {
267
+ boundary: menuNode,
268
+ block: 'nearest',
269
+ scrollMode: 'if-needed'
270
+ });
271
+ actions.forEach(function (_ref) {
272
+ var el = _ref.el,
273
+ top = _ref.top,
274
+ left = _ref.left;
275
+ el.scrollTop = top;
276
+ el.scrollLeft = left;
277
+ });
278
+ }
279
+
280
+ /**
281
+ * Accepts a parameter and returns it if it's a function
282
+ * or a noop function if it's not. This allows us to
283
+ * accept a callback, but not worry about it if it's not
284
+ * passed.
285
+ * @param {Function} cb the callback
286
+ * @return {Function} a function
287
+ */
288
+ function cbToCb(cb) {
289
+ return typeof cb === 'function' ? cb : noop;
290
+ }
291
+
292
+ /**
293
+ * @param {HTMLElement} parent the parent node
294
+ * @param {HTMLElement} child the child node
295
+ * @param {Window} environment The window context where downshift renders.
296
+ * @return {Boolean} whether the parent is the child or the child is in the parent
297
+ */
298
+ function isOrContainsNode(parent, child, environment) {
299
+ var result = parent === child || child instanceof environment.Node && parent.contains && parent.contains(child);
300
+ return result;
301
+ }
302
+
303
+ /**
304
+ * Simple debounce implementation. Will call the given
305
+ * function once after the time given has passed since
306
+ * it was last called.
307
+ * @param {Function} fn the function to call after the time
308
+ * @param {Number} time the time to wait
309
+ * @return {Function} the debounced function
310
+ */
311
+ function debounce(fn, time) {
312
+ var timeoutId;
313
+ function cancel() {
314
+ if (timeoutId) {
315
+ clearTimeout(timeoutId);
316
+ }
317
+ }
318
+ function wrapper() {
319
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
320
+ args[_key] = arguments[_key];
321
+ }
322
+ cancel();
323
+ timeoutId = setTimeout(function () {
324
+ timeoutId = null;
325
+ fn.apply(void 0, args);
326
+ }, time);
327
+ }
328
+ wrapper.cancel = cancel;
329
+ return wrapper;
330
+ }
331
+
332
+ /**
333
+ * This is intended to be used to compose event handlers.
334
+ * They are executed in order until one of them sets
335
+ * `event.preventDownshiftDefault = true`.
336
+ * @param {...Function} fns the event handler functions
337
+ * @return {Function} the event handler to add to an element
338
+ */
339
+ function callAllEventHandlers() {
340
+ for (var _len2 = arguments.length, fns = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
341
+ fns[_key2] = arguments[_key2];
342
+ }
343
+ return function (event) {
344
+ for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
345
+ args[_key3 - 1] = arguments[_key3];
346
+ }
347
+ return fns.some(function (fn) {
348
+ if (fn) {
349
+ fn.apply(void 0, [event].concat(args));
350
+ }
351
+ return event.preventDownshiftDefault || event.hasOwnProperty('nativeEvent') && event.nativeEvent.preventDownshiftDefault;
352
+ });
353
+ };
354
+ }
355
+ function handleRefs() {
356
+ for (var _len4 = arguments.length, refs = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
357
+ refs[_key4] = arguments[_key4];
358
+ }
359
+ return function (node) {
360
+ refs.forEach(function (ref) {
361
+ if (typeof ref === 'function') {
362
+ ref(node);
363
+ } else if (ref) {
364
+ ref.current = node;
365
+ }
366
+ });
367
+ };
368
+ }
369
+
370
+ /**
371
+ * Default implementation for status message. Only added when menu is open.
372
+ * Will specify if there are results in the list, and if so, how many,
373
+ * and what keys are relevant.
374
+ *
375
+ * @param {Object} param the downshift state and other relevant properties
376
+ * @return {String} the a11y status message
377
+ */
378
+ function getA11yStatusMessage(_ref) {
379
+ var isOpen = _ref.isOpen,
380
+ resultCount = _ref.resultCount,
381
+ previousResultCount = _ref.previousResultCount;
382
+ if (!isOpen) {
383
+ return '';
384
+ }
385
+ if (!resultCount) {
386
+ return 'No results are available.';
387
+ }
388
+ if (resultCount !== previousResultCount) {
389
+ return resultCount + " result" + (resultCount === 1 ? ' is' : 's are') + " available, use up and down arrow keys to navigate. Press Enter key to select.";
390
+ }
391
+ return '';
392
+ }
393
+
394
+ /**
395
+ * Takes an argument and if it's an array, returns the first item in the array
396
+ * otherwise returns the argument
397
+ * @param {*} arg the maybe-array
398
+ * @param {*} defaultValue the value if arg is falsey not defined
399
+ * @return {*} the arg or it's first item
400
+ */
401
+ function unwrapArray(arg, defaultValue) {
402
+ arg = Array.isArray(arg) ? /* istanbul ignore next (preact) */arg[0] : arg;
403
+ if (!arg && defaultValue) {
404
+ return defaultValue;
405
+ } else {
406
+ return arg;
407
+ }
408
+ }
409
+
410
+ /**
411
+ * @param {Object} element (P)react element
412
+ * @return {Boolean} whether it's a DOM element
413
+ */
414
+ function isDOMElement(element) {
415
+ /* istanbul ignore if */
416
+ {
417
+ // then this is preact or preact X
418
+ return typeof element.type === 'string' || typeof element.nodeName === 'string';
419
+ }
420
+ }
421
+
422
+ /**
423
+ * @param {Object} element (P)react element
424
+ * @return {Object} the props
425
+ */
426
+ function getElementProps(element) {
427
+ // props for react, attributes for preact
428
+
429
+ /* istanbul ignore if */
430
+ {
431
+ return element.props || element.attributes;
432
+ }
433
+ }
434
+
435
+ /**
436
+ * Throws a helpful error message for required properties. Useful
437
+ * to be used as a default in destructuring or object params.
438
+ * @param {String} fnName the function name
439
+ * @param {String} propName the prop name
440
+ */
441
+ function requiredProp(fnName, propName) {
442
+ // eslint-disable-next-line no-console
443
+ console.error("The property \"" + propName + "\" is required in \"" + fnName + "\"");
444
+ }
445
+ var stateKeys = ['highlightedIndex', 'inputValue', 'isOpen', 'selectedItem', 'type'];
446
+ /**
447
+ * @param {Object} state the state object
448
+ * @return {Object} state that is relevant to downshift
449
+ */
450
+ function pickState(state) {
451
+ if (state === void 0) {
452
+ state = {};
453
+ }
454
+ var result = {};
455
+ stateKeys.forEach(function (k) {
456
+ if (state.hasOwnProperty(k)) {
457
+ result[k] = state[k];
458
+ }
459
+ });
460
+ return result;
461
+ }
462
+
463
+ /**
464
+ * This determines whether a prop is a "controlled prop" meaning it is
465
+ * state which is controlled by the outside of this component rather
466
+ * than within this component.
467
+ *
468
+ * @param {Object} props The props that may contain controlled values.
469
+ * @param {String} key the key to check
470
+ * @return {Boolean} whether it is a controlled controlled prop
471
+ */
472
+ function isControlledProp(props, key) {
473
+ return props[key] !== undefined;
474
+ }
475
+
476
+ /**
477
+ * Normalizes the 'key' property of a KeyboardEvent in IE/Edge
478
+ * @param {Object} event a keyboardEvent object
479
+ * @return {String} keyboard key
480
+ */
481
+ function normalizeArrowKey(event) {
482
+ var key = event.key,
483
+ keyCode = event.keyCode;
484
+ /* istanbul ignore next (ie) */
485
+ if (keyCode >= 37 && keyCode <= 40 && key.indexOf('Arrow') !== 0) {
486
+ return "Arrow" + key;
487
+ }
488
+ return key;
489
+ }
490
+
491
+ /**
492
+ * Simple check if the value passed is object literal
493
+ * @param {*} obj any things
494
+ * @return {Boolean} whether it's object literal
495
+ */
496
+ function isPlainObject(obj) {
497
+ return Object.prototype.toString.call(obj) === '[object Object]';
498
+ }
499
+
500
+ /**
501
+ * Returns the next non-disabled highlightedIndex value.
502
+ *
503
+ * @param {number} start The current highlightedIndex.
504
+ * @param {number} offset The offset from the current highlightedIndex to start searching.
505
+ * @param {unknown[]} items The items array.
506
+ * @param {(item: unknown, index: number) => boolean} isItemDisabled Function that tells if an item is disabled or not.
507
+ * @param {boolean?} circular If the search reaches the end, if it can search again starting from the other end.
508
+ * @returns {number} The next highlightedIndex.
509
+ */
510
+ function getHighlightedIndex(start, offset, items, isItemDisabled, circular) {
511
+ if (circular === void 0) {
512
+ circular = false;
513
+ }
514
+ var count = items.length;
515
+ if (count === 0) {
516
+ return -1;
517
+ }
518
+ var itemsLastIndex = count - 1;
519
+ if (typeof start !== 'number' || start < 0 || start > itemsLastIndex) {
520
+ start = offset > 0 ? -1 : itemsLastIndex + 1;
521
+ }
522
+ var current = start + offset;
523
+ if (current < 0) {
524
+ current = circular ? itemsLastIndex : 0;
525
+ } else if (current > itemsLastIndex) {
526
+ current = circular ? 0 : itemsLastIndex;
527
+ }
528
+ var highlightedIndex = getNonDisabledIndex(current, offset < 0, items, isItemDisabled, circular);
529
+ if (highlightedIndex === -1) {
530
+ return start >= count ? -1 : start;
531
+ }
532
+ return highlightedIndex;
533
+ }
534
+
535
+ /**
536
+ * Returns the next non-disabled highlightedIndex value.
537
+ *
538
+ * @param {number} start The current highlightedIndex.
539
+ * @param {boolean} backwards If true, it will search backwards from the start.
540
+ * @param {unknown[]} items The items array.
541
+ * @param {(item: unknown, index: number) => boolean} isItemDisabled Function that tells if an item is disabled or not.
542
+ * @param {boolean} circular If the search reaches the end, if it can search again starting from the other end.
543
+ * @returns {number} The next non-disabled index.
544
+ */
545
+ function getNonDisabledIndex(start, backwards, items, isItemDisabled, circular) {
546
+ if (circular === void 0) {
547
+ circular = false;
548
+ }
549
+ var count = items.length;
550
+ if (backwards) {
551
+ for (var index = start; index >= 0; index--) {
552
+ if (!isItemDisabled(items[index], index)) {
553
+ return index;
554
+ }
555
+ }
556
+ } else {
557
+ for (var _index = start; _index < count; _index++) {
558
+ if (!isItemDisabled(items[_index], _index)) {
559
+ return _index;
560
+ }
561
+ }
562
+ }
563
+ if (circular) {
564
+ return getNonDisabledIndex(backwards ? count - 1 : 0, backwards, items, isItemDisabled);
565
+ }
566
+ return -1;
567
+ }
568
+
569
+ /**
570
+ * Checks if event target is within the downshift elements.
571
+ *
572
+ * @param {EventTarget} target Target to check.
573
+ * @param {HTMLElement[]} downshiftElements The elements that form downshift (list, toggle button etc).
574
+ * @param {Window} environment The window context where downshift renders.
575
+ * @param {boolean} checkActiveElement Whether to also check activeElement.
576
+ *
577
+ * @returns {boolean} Whether or not the target is within downshift elements.
578
+ */
579
+ function targetWithinDownshift(target, downshiftElements, environment, checkActiveElement) {
580
+ if (checkActiveElement === void 0) {
581
+ checkActiveElement = true;
582
+ }
583
+ return environment && downshiftElements.some(function (contextNode) {
584
+ return contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment));
585
+ });
586
+ }
587
+
588
+ // eslint-disable-next-line import/no-mutable-exports
589
+ var validateControlledUnchanged = noop;
590
+ /* istanbul ignore next */
591
+ if (process.env.NODE_ENV !== 'production') {
592
+ validateControlledUnchanged = function validateControlledUnchanged(state, prevProps, nextProps) {
593
+ var warningDescription = "This prop should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled Downshift element for the lifetime of the component. More info: https://github.com/downshift-js/downshift#control-props";
594
+ Object.keys(state).forEach(function (propKey) {
595
+ if (prevProps[propKey] !== undefined && nextProps[propKey] === undefined) {
596
+ // eslint-disable-next-line no-console
597
+ console.error("downshift: A component has changed the controlled prop \"" + propKey + "\" to be uncontrolled. " + warningDescription);
598
+ } else if (prevProps[propKey] === undefined && nextProps[propKey] !== undefined) {
599
+ // eslint-disable-next-line no-console
600
+ console.error("downshift: A component has changed the uncontrolled prop \"" + propKey + "\" to be controlled. " + warningDescription);
601
+ }
602
+ });
603
+ };
604
+ }
605
+
606
+ var _excluded$4 = ["refKey", "ref"],
607
+ _excluded2$4 = ["onClick", "onPress", "onKeyDown", "onKeyUp", "onBlur"],
608
+ _excluded3$3 = ["onKeyDown", "onBlur", "onChange", "onInput", "onChangeText"],
609
+ _excluded4$2 = ["refKey", "ref"],
610
+ _excluded5 = ["onMouseMove", "onMouseDown", "onClick", "onPress", "index", "item"];
611
+ var Downshift = /*#__PURE__*/function () {
612
+ var Downshift = /*#__PURE__*/function (_Component) {
613
+ function Downshift(_props) {
614
+ var _this;
615
+ _this = _Component.call(this, _props) || this;
616
+ // fancy destructuring + defaults + aliases
617
+ // this basically says each value of state should either be set to
618
+ // the initial value or the default value if the initial value is not provided
619
+ _this.id = _this.props.id || "downshift-" + generateId();
620
+ _this.menuId = _this.props.menuId || _this.id + "-menu";
621
+ _this.labelId = _this.props.labelId || _this.id + "-label";
622
+ _this.inputId = _this.props.inputId || _this.id + "-input";
623
+ _this.getItemId = _this.props.getItemId || function (index) {
624
+ return _this.id + "-item-" + index;
625
+ };
626
+ _this.items = [];
627
+ // itemCount can be changed asynchronously
628
+ // from within downshift (so it can't come from a prop)
629
+ // this is why we store it as an instance and use
630
+ // getItemCount rather than just use items.length
631
+ // (to support windowing + async)
632
+ _this.itemCount = null;
633
+ _this.previousResultCount = 0;
634
+ _this.timeoutIds = [];
635
+ /**
636
+ * @param {Function} fn the function to call after the time
637
+ * @param {Number} time the time to wait
638
+ */
639
+ _this.internalSetTimeout = function (fn, time) {
640
+ var id = setTimeout(function () {
641
+ _this.timeoutIds = _this.timeoutIds.filter(function (i) {
642
+ return i !== id;
643
+ });
644
+ fn();
645
+ }, time);
646
+ _this.timeoutIds.push(id);
647
+ };
648
+ _this.setItemCount = function (count) {
649
+ _this.itemCount = count;
650
+ };
651
+ _this.unsetItemCount = function () {
652
+ _this.itemCount = null;
653
+ };
654
+ _this.isItemDisabled = function (_item, index) {
655
+ var currentElementNode = _this.getItemNodeFromIndex(index);
656
+ return currentElementNode && currentElementNode.hasAttribute('disabled');
657
+ };
658
+ _this.setHighlightedIndex = function (highlightedIndex, otherStateToSet) {
659
+ if (highlightedIndex === void 0) {
660
+ highlightedIndex = _this.props.defaultHighlightedIndex;
661
+ }
662
+ if (otherStateToSet === void 0) {
663
+ otherStateToSet = {};
664
+ }
665
+ otherStateToSet = pickState(otherStateToSet);
666
+ _this.internalSetState(_extends({
667
+ highlightedIndex: highlightedIndex
668
+ }, otherStateToSet));
669
+ };
670
+ _this.clearSelection = function (cb) {
671
+ _this.internalSetState({
672
+ selectedItem: null,
673
+ inputValue: '',
674
+ highlightedIndex: _this.props.defaultHighlightedIndex,
675
+ isOpen: _this.props.defaultIsOpen
676
+ }, cb);
677
+ };
678
+ _this.selectItem = function (item, otherStateToSet, cb) {
679
+ otherStateToSet = pickState(otherStateToSet);
680
+ _this.internalSetState(_extends({
681
+ isOpen: _this.props.defaultIsOpen,
682
+ highlightedIndex: _this.props.defaultHighlightedIndex,
683
+ selectedItem: item,
684
+ inputValue: _this.props.itemToString(item)
685
+ }, otherStateToSet), cb);
686
+ };
687
+ _this.selectItemAtIndex = function (itemIndex, otherStateToSet, cb) {
688
+ var item = _this.items[itemIndex];
689
+ if (item == null) {
690
+ return;
691
+ }
692
+ _this.selectItem(item, otherStateToSet, cb);
693
+ };
694
+ _this.selectHighlightedItem = function (otherStateToSet, cb) {
695
+ return _this.selectItemAtIndex(_this.getState().highlightedIndex, otherStateToSet, cb);
696
+ };
697
+ // any piece of our state can live in two places:
698
+ // 1. Uncontrolled: it's internal (this.state)
699
+ // We will call this.setState to update that state
700
+ // 2. Controlled: it's external (this.props)
701
+ // We will call this.props.onStateChange to update that state
702
+ //
703
+ // In addition, we'll call this.props.onChange if the
704
+ // selectedItem is changed.
705
+ _this.internalSetState = function (stateToSet, cb) {
706
+ var isItemSelected, onChangeArg;
707
+ var onStateChangeArg = {};
708
+ var isStateToSetFunction = typeof stateToSet === 'function';
709
+
710
+ // we want to call `onInputValueChange` before the `setState` call
711
+ // so someone controlling the `inputValue` state gets notified of
712
+ // the input change as soon as possible. This avoids issues with
713
+ // preserving the cursor position.
714
+ // See https://github.com/downshift-js/downshift/issues/217 for more info.
715
+ if (!isStateToSetFunction && stateToSet.hasOwnProperty('inputValue')) {
716
+ _this.props.onInputValueChange(stateToSet.inputValue, _extends({}, _this.getStateAndHelpers(), stateToSet));
717
+ }
718
+ return _this.setState(function (state) {
719
+ var _newStateToSet;
720
+ state = _this.getState(state);
721
+ var newStateToSet = isStateToSetFunction ? stateToSet(state) : stateToSet;
722
+
723
+ // Your own function that could modify the state that will be set.
724
+ newStateToSet = _this.props.stateReducer(state, newStateToSet);
725
+
726
+ // checks if an item is selected, regardless of if it's different from
727
+ // what was selected before
728
+ // used to determine if onSelect and onChange callbacks should be called
729
+ isItemSelected = newStateToSet.hasOwnProperty('selectedItem');
730
+ // this keeps track of the object we want to call with setState
731
+ var nextState = {};
732
+ // we need to call on change if the outside world is controlling any of our state
733
+ // and we're trying to update that state. OR if the selection has changed and we're
734
+ // trying to update the selection
735
+ if (isItemSelected && newStateToSet.selectedItem !== state.selectedItem) {
736
+ onChangeArg = newStateToSet.selectedItem;
737
+ }
738
+ (_newStateToSet = newStateToSet).type || (_newStateToSet.type = unknown);
739
+ Object.keys(newStateToSet).forEach(function (key) {
740
+ // onStateChangeArg should only have the state that is
741
+ // actually changing
742
+ if (state[key] !== newStateToSet[key]) {
743
+ onStateChangeArg[key] = newStateToSet[key];
744
+ }
745
+ // the type is useful for the onStateChangeArg
746
+ // but we don't actually want to set it in internal state.
747
+ // this is an undocumented feature for now... Not all internalSetState
748
+ // calls support it and I'm not certain we want them to yet.
749
+ // But it enables users controlling the isOpen state to know when
750
+ // the isOpen state changes due to mouseup events which is quite handy.
751
+ if (key === 'type') {
752
+ return;
753
+ }
754
+ newStateToSet[key];
755
+ // if it's coming from props, then we don't care to set it internally
756
+ if (!isControlledProp(_this.props, key)) {
757
+ nextState[key] = newStateToSet[key];
758
+ }
759
+ });
760
+
761
+ // if stateToSet is a function, then we weren't able to call onInputValueChange
762
+ // earlier, so we'll call it now that we know what the inputValue state will be.
763
+ if (isStateToSetFunction && newStateToSet.hasOwnProperty('inputValue')) {
764
+ _this.props.onInputValueChange(newStateToSet.inputValue, _extends({}, _this.getStateAndHelpers(), newStateToSet));
765
+ }
766
+ return nextState;
767
+ }, function () {
768
+ // call the provided callback if it's a function
769
+ cbToCb(cb)();
770
+
771
+ // only call the onStateChange and onChange callbacks if
772
+ // we have relevant information to pass them.
773
+ var hasMoreStateThanType = Object.keys(onStateChangeArg).length > 1;
774
+ if (hasMoreStateThanType) {
775
+ _this.props.onStateChange(onStateChangeArg, _this.getStateAndHelpers());
776
+ }
777
+ if (isItemSelected) {
778
+ _this.props.onSelect(stateToSet.selectedItem, _this.getStateAndHelpers());
779
+ }
780
+ if (onChangeArg !== undefined) {
781
+ _this.props.onChange(onChangeArg, _this.getStateAndHelpers());
782
+ }
783
+ // this is currently undocumented and therefore subject to change
784
+ // We'll try to not break it, but just be warned.
785
+ _this.props.onUserAction(onStateChangeArg, _this.getStateAndHelpers());
786
+ });
787
+ };
788
+ //////////////////////////// ROOT
789
+ _this.rootRef = function (node) {
790
+ return _this._rootNode = node;
791
+ };
792
+ _this.getRootProps = function (_temp, _temp2) {
793
+ var _extends2;
794
+ var _ref = _temp === void 0 ? {} : _temp,
795
+ _ref$refKey = _ref.refKey,
796
+ refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
797
+ ref = _ref.ref,
798
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded$4);
799
+ var _ref2 = _temp2 === void 0 ? {} : _temp2,
800
+ _ref2$suppressRefErro = _ref2.suppressRefError,
801
+ suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
802
+ // this is used in the render to know whether the user has called getRootProps.
803
+ // It uses that to know whether to apply the props automatically
804
+ _this.getRootProps.called = true;
805
+ _this.getRootProps.refKey = refKey;
806
+ _this.getRootProps.suppressRefError = suppressRefError;
807
+ var _this$getState = _this.getState(),
808
+ isOpen = _this$getState.isOpen;
809
+ 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 : undefined, _extends2['aria-labelledby'] = _this.labelId, _extends2), rest);
810
+ };
811
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\ ROOT
812
+ _this.keyDownHandlers = {
813
+ ArrowDown: function ArrowDown(event) {
814
+ var _this2 = this;
815
+ event.preventDefault();
816
+ if (this.getState().isOpen) {
817
+ var amount = event.shiftKey ? 5 : 1;
818
+ this.moveHighlightedIndex(amount, {
819
+ type: keyDownArrowDown
820
+ });
821
+ } else {
822
+ this.internalSetState({
823
+ isOpen: true,
824
+ type: keyDownArrowDown
825
+ }, function () {
826
+ var itemCount = _this2.getItemCount();
827
+ if (itemCount > 0) {
828
+ var _this2$getState = _this2.getState(),
829
+ highlightedIndex = _this2$getState.highlightedIndex;
830
+ var nextHighlightedIndex = getHighlightedIndex(highlightedIndex, 1, {
831
+ length: itemCount
832
+ }, _this2.isItemDisabled, true);
833
+ _this2.setHighlightedIndex(nextHighlightedIndex, {
834
+ type: keyDownArrowDown
835
+ });
836
+ }
837
+ });
838
+ }
839
+ },
840
+ ArrowUp: function ArrowUp(event) {
841
+ var _this3 = this;
842
+ event.preventDefault();
843
+ if (this.getState().isOpen) {
844
+ var amount = event.shiftKey ? -5 : -1;
845
+ this.moveHighlightedIndex(amount, {
846
+ type: keyDownArrowUp
847
+ });
848
+ } else {
849
+ this.internalSetState({
850
+ isOpen: true,
851
+ type: keyDownArrowUp
852
+ }, function () {
853
+ var itemCount = _this3.getItemCount();
854
+ if (itemCount > 0) {
855
+ var _this3$getState = _this3.getState(),
856
+ highlightedIndex = _this3$getState.highlightedIndex;
857
+ var nextHighlightedIndex = getHighlightedIndex(highlightedIndex, -1, {
858
+ length: itemCount
859
+ }, _this3.isItemDisabled, true);
860
+ _this3.setHighlightedIndex(nextHighlightedIndex, {
861
+ type: keyDownArrowUp
862
+ });
863
+ }
864
+ });
865
+ }
866
+ },
867
+ Enter: function Enter(event) {
868
+ if (event.which === 229) {
869
+ return;
870
+ }
871
+ var _this$getState2 = this.getState(),
872
+ isOpen = _this$getState2.isOpen,
873
+ highlightedIndex = _this$getState2.highlightedIndex;
874
+ if (isOpen && highlightedIndex != null) {
875
+ event.preventDefault();
876
+ var item = this.items[highlightedIndex];
877
+ var itemNode = this.getItemNodeFromIndex(highlightedIndex);
878
+ if (item == null || itemNode && itemNode.hasAttribute('disabled')) {
879
+ return;
880
+ }
881
+ this.selectHighlightedItem({
882
+ type: keyDownEnter
883
+ });
884
+ }
885
+ },
886
+ Escape: function Escape(event) {
887
+ event.preventDefault();
888
+ this.reset(_extends({
889
+ type: keyDownEscape
890
+ }, !this.state.isOpen && {
891
+ selectedItem: null,
892
+ inputValue: ''
893
+ }));
894
+ }
895
+ };
896
+ //////////////////////////// BUTTON
897
+ _this.buttonKeyDownHandlers = _extends({}, _this.keyDownHandlers, {
898
+ ' ': function _(event) {
899
+ event.preventDefault();
900
+ this.toggleMenu({
901
+ type: keyDownSpaceButton
902
+ });
903
+ }
904
+ });
905
+ _this.inputKeyDownHandlers = _extends({}, _this.keyDownHandlers, {
906
+ Home: function Home(event) {
907
+ var _this$getState3 = this.getState(),
908
+ isOpen = _this$getState3.isOpen;
909
+ if (!isOpen) {
910
+ return;
911
+ }
912
+ event.preventDefault();
913
+ var itemCount = this.getItemCount();
914
+ if (itemCount <= 0 || !isOpen) {
915
+ return;
916
+ }
917
+
918
+ // get next non-disabled starting downwards from 0 if that's disabled.
919
+ var newHighlightedIndex = getNonDisabledIndex(0, false, {
920
+ length: itemCount
921
+ }, this.isItemDisabled);
922
+ this.setHighlightedIndex(newHighlightedIndex, {
923
+ type: keyDownHome
924
+ });
925
+ },
926
+ End: function End(event) {
927
+ var _this$getState4 = this.getState(),
928
+ isOpen = _this$getState4.isOpen;
929
+ if (!isOpen) {
930
+ return;
931
+ }
932
+ event.preventDefault();
933
+ var itemCount = this.getItemCount();
934
+ if (itemCount <= 0 || !isOpen) {
935
+ return;
936
+ }
937
+
938
+ // get next non-disabled starting upwards from last index if that's disabled.
939
+ var newHighlightedIndex = getNonDisabledIndex(itemCount - 1, true, {
940
+ length: itemCount
941
+ }, this.isItemDisabled);
942
+ this.setHighlightedIndex(newHighlightedIndex, {
943
+ type: keyDownEnd
944
+ });
945
+ }
946
+ });
947
+ _this.getToggleButtonProps = function (_temp3) {
948
+ var _ref3 = _temp3 === void 0 ? {} : _temp3,
949
+ onClick = _ref3.onClick;
950
+ _ref3.onPress;
951
+ var onKeyDown = _ref3.onKeyDown,
952
+ onKeyUp = _ref3.onKeyUp,
953
+ onBlur = _ref3.onBlur,
954
+ rest = _objectWithoutPropertiesLoose(_ref3, _excluded2$4);
955
+ var _this$getState5 = _this.getState(),
956
+ isOpen = _this$getState5.isOpen;
957
+ var enabledEventHandlers = {
958
+ onClick: callAllEventHandlers(onClick, _this.buttonHandleClick),
959
+ onKeyDown: callAllEventHandlers(onKeyDown, _this.buttonHandleKeyDown),
960
+ onKeyUp: callAllEventHandlers(onKeyUp, _this.buttonHandleKeyUp),
961
+ onBlur: callAllEventHandlers(onBlur, _this.buttonHandleBlur)
962
+ };
963
+ var eventHandlers = rest.disabled ? {} : enabledEventHandlers;
964
+ return _extends({
965
+ type: 'button',
966
+ role: 'button',
967
+ 'aria-label': isOpen ? 'close menu' : 'open menu',
968
+ 'aria-haspopup': true,
969
+ 'data-toggle': true
970
+ }, eventHandlers, rest);
971
+ };
972
+ _this.buttonHandleKeyUp = function (event) {
973
+ // Prevent click event from emitting in Firefox
974
+ event.preventDefault();
975
+ };
976
+ _this.buttonHandleKeyDown = function (event) {
977
+ var key = normalizeArrowKey(event);
978
+ if (_this.buttonKeyDownHandlers[key]) {
979
+ _this.buttonKeyDownHandlers[key].call(_this, event);
980
+ }
981
+ };
982
+ _this.buttonHandleClick = function (event) {
983
+ event.preventDefault();
984
+ // handle odd case for Safari and Firefox which
985
+ // don't give the button the focus properly.
986
+ /* istanbul ignore if (can't reasonably test this) */
987
+ if (_this.props.environment) {
988
+ var _this$props$environme = _this.props.environment.document,
989
+ body = _this$props$environme.body,
990
+ activeElement = _this$props$environme.activeElement;
991
+ if (body && body === activeElement) {
992
+ event.target.focus();
993
+ }
994
+ }
995
+ // to simplify testing components that use downshift, we'll not wrap this in a setTimeout
996
+ // if the NODE_ENV is test. With the proper build system, this should be dead code eliminated
997
+ // when building for production and should therefore have no impact on production code.
998
+ if (process.env.NODE_ENV === 'test') {
999
+ _this.toggleMenu({
1000
+ type: clickButton
1001
+ });
1002
+ } else {
1003
+ // Ensure that toggle of menu occurs after the potential blur event in iOS
1004
+ _this.internalSetTimeout(function () {
1005
+ return _this.toggleMenu({
1006
+ type: clickButton
1007
+ });
1008
+ });
1009
+ }
1010
+ };
1011
+ _this.buttonHandleBlur = function (event) {
1012
+ var blurTarget = event.target; // Save blur target for comparison with activeElement later
1013
+ // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not body element
1014
+ _this.internalSetTimeout(function () {
1015
+ if (_this.isMouseDown || !_this.props.environment) {
1016
+ return;
1017
+ }
1018
+ var activeElement = _this.props.environment.document.activeElement;
1019
+ 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)
1020
+ ) {
1021
+ _this.reset({
1022
+ type: blurButton
1023
+ });
1024
+ }
1025
+ });
1026
+ };
1027
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ BUTTON
1028
+ /////////////////////////////// LABEL
1029
+ _this.getLabelProps = function (props) {
1030
+ return _extends({
1031
+ htmlFor: _this.inputId,
1032
+ id: _this.labelId
1033
+ }, props);
1034
+ };
1035
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ LABEL
1036
+ /////////////////////////////// INPUT
1037
+ _this.getInputProps = function (_temp4) {
1038
+ var _ref4 = _temp4 === void 0 ? {} : _temp4,
1039
+ onKeyDown = _ref4.onKeyDown,
1040
+ onBlur = _ref4.onBlur,
1041
+ onChange = _ref4.onChange,
1042
+ onInput = _ref4.onInput;
1043
+ _ref4.onChangeText;
1044
+ var rest = _objectWithoutPropertiesLoose(_ref4, _excluded3$3);
1045
+ var onChangeKey;
1046
+ var eventHandlers = {};
1047
+
1048
+ /* istanbul ignore next (preact) */
1049
+ {
1050
+ onChangeKey = 'onInput';
1051
+ }
1052
+ var _this$getState6 = _this.getState(),
1053
+ inputValue = _this$getState6.inputValue,
1054
+ isOpen = _this$getState6.isOpen,
1055
+ highlightedIndex = _this$getState6.highlightedIndex;
1056
+ if (!rest.disabled) {
1057
+ var _eventHandlers;
1058
+ eventHandlers = (_eventHandlers = {}, _eventHandlers[onChangeKey] = callAllEventHandlers(onChange, onInput, _this.inputHandleChange), _eventHandlers.onKeyDown = callAllEventHandlers(onKeyDown, _this.inputHandleKeyDown), _eventHandlers.onBlur = callAllEventHandlers(onBlur, _this.inputHandleBlur), _eventHandlers);
1059
+ }
1060
+ return _extends({
1061
+ 'aria-autocomplete': 'list',
1062
+ 'aria-activedescendant': isOpen && typeof highlightedIndex === 'number' && highlightedIndex >= 0 ? _this.getItemId(highlightedIndex) : undefined,
1063
+ 'aria-controls': isOpen ? _this.menuId : undefined,
1064
+ 'aria-labelledby': rest && rest['aria-label'] ? undefined : _this.labelId,
1065
+ // https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
1066
+ // revert back since autocomplete="nope" is ignored on latest Chrome and Opera
1067
+ autoComplete: 'off',
1068
+ value: inputValue,
1069
+ id: _this.inputId
1070
+ }, eventHandlers, rest);
1071
+ };
1072
+ _this.inputHandleKeyDown = function (event) {
1073
+ var key = normalizeArrowKey(event);
1074
+ if (key && _this.inputKeyDownHandlers[key]) {
1075
+ _this.inputKeyDownHandlers[key].call(_this, event);
1076
+ }
1077
+ };
1078
+ _this.inputHandleChange = function (event) {
1079
+ _this.internalSetState({
1080
+ type: changeInput,
1081
+ isOpen: true,
1082
+ inputValue: event.target.value,
1083
+ highlightedIndex: _this.props.defaultHighlightedIndex
1084
+ });
1085
+ };
1086
+ _this.inputHandleBlur = function () {
1087
+ // Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not the body element
1088
+ _this.internalSetTimeout(function () {
1089
+ var _activeElement$datase;
1090
+ if (_this.isMouseDown || !_this.props.environment) {
1091
+ return;
1092
+ }
1093
+ var activeElement = _this.props.environment.document.activeElement;
1094
+ var downshiftButtonIsActive = (activeElement == null || (_activeElement$datase = activeElement.dataset) == null ? void 0 : _activeElement$datase.toggle) && _this._rootNode && _this._rootNode.contains(activeElement);
1095
+ if (!downshiftButtonIsActive) {
1096
+ _this.reset({
1097
+ type: blurInput
1098
+ });
1099
+ }
1100
+ });
1101
+ };
1102
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ INPUT
1103
+ /////////////////////////////// MENU
1104
+ _this.menuRef = function (node) {
1105
+ _this._menuNode = node;
1106
+ };
1107
+ _this.getMenuProps = function (_temp5, _temp6) {
1108
+ var _extends3;
1109
+ var _ref5 = _temp5 === void 0 ? {} : _temp5,
1110
+ _ref5$refKey = _ref5.refKey,
1111
+ refKey = _ref5$refKey === void 0 ? 'ref' : _ref5$refKey,
1112
+ ref = _ref5.ref,
1113
+ props = _objectWithoutPropertiesLoose(_ref5, _excluded4$2);
1114
+ var _ref6 = _temp6 === void 0 ? {} : _temp6,
1115
+ _ref6$suppressRefErro = _ref6.suppressRefError,
1116
+ suppressRefError = _ref6$suppressRefErro === void 0 ? false : _ref6$suppressRefErro;
1117
+ _this.getMenuProps.called = true;
1118
+ _this.getMenuProps.refKey = refKey;
1119
+ _this.getMenuProps.suppressRefError = suppressRefError;
1120
+ return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, _this.menuRef), _extends3.role = 'listbox', _extends3['aria-labelledby'] = props && props['aria-label'] ? undefined : _this.labelId, _extends3.id = _this.menuId, _extends3), props);
1121
+ };
1122
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ MENU
1123
+ /////////////////////////////// ITEM
1124
+ _this.getItemProps = function (_temp7) {
1125
+ var _enabledEventHandlers;
1126
+ var _ref7 = _temp7 === void 0 ? {} : _temp7,
1127
+ onMouseMove = _ref7.onMouseMove,
1128
+ onMouseDown = _ref7.onMouseDown,
1129
+ onClick = _ref7.onClick;
1130
+ _ref7.onPress;
1131
+ var index = _ref7.index,
1132
+ _ref7$item = _ref7.item,
1133
+ item = _ref7$item === void 0 ? process.env.NODE_ENV === 'production' ? /* istanbul ignore next */undefined : requiredProp('getItemProps', 'item') : _ref7$item,
1134
+ rest = _objectWithoutPropertiesLoose(_ref7, _excluded5);
1135
+ if (index === undefined) {
1136
+ _this.items.push(item);
1137
+ index = _this.items.indexOf(item);
1138
+ } else {
1139
+ _this.items[index] = item;
1140
+ }
1141
+ var onSelectKey = 'onClick';
1142
+ var customClickHandler = onClick;
1143
+ var enabledEventHandlers = (_enabledEventHandlers = {
1144
+ // onMouseMove is used over onMouseEnter here. onMouseMove
1145
+ // is only triggered on actual mouse movement while onMouseEnter
1146
+ // can fire on DOM changes, interrupting keyboard navigation
1147
+ onMouseMove: callAllEventHandlers(onMouseMove, function () {
1148
+ if (index === _this.getState().highlightedIndex) {
1149
+ return;
1150
+ }
1151
+ _this.setHighlightedIndex(index, {
1152
+ type: itemMouseEnter
1153
+ });
1154
+
1155
+ // We never want to manually scroll when changing state based
1156
+ // on `onMouseMove` because we will be moving the element out
1157
+ // from under the user which is currently scrolling/moving the
1158
+ // cursor
1159
+ _this.avoidScrolling = true;
1160
+ _this.internalSetTimeout(function () {
1161
+ return _this.avoidScrolling = false;
1162
+ }, 250);
1163
+ }),
1164
+ onMouseDown: callAllEventHandlers(onMouseDown, function (event) {
1165
+ // This prevents the activeElement from being changed
1166
+ // to the item so it can remain with the current activeElement
1167
+ // which is a more common use case.
1168
+ event.preventDefault();
1169
+ })
1170
+ }, _enabledEventHandlers[onSelectKey] = callAllEventHandlers(customClickHandler, function () {
1171
+ _this.selectItemAtIndex(index, {
1172
+ type: clickItem
1173
+ });
1174
+ }), _enabledEventHandlers);
1175
+
1176
+ // Passing down the onMouseDown handler to prevent redirect
1177
+ // of the activeElement if clicking on disabled items
1178
+ var eventHandlers = rest.disabled ? {
1179
+ onMouseDown: enabledEventHandlers.onMouseDown
1180
+ } : enabledEventHandlers;
1181
+ return _extends({
1182
+ id: _this.getItemId(index),
1183
+ role: 'option',
1184
+ 'aria-selected': _this.getState().highlightedIndex === index
1185
+ }, eventHandlers, rest);
1186
+ };
1187
+ //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ITEM
1188
+ _this.clearItems = function () {
1189
+ _this.items = [];
1190
+ };
1191
+ _this.reset = function (otherStateToSet, cb) {
1192
+ if (otherStateToSet === void 0) {
1193
+ otherStateToSet = {};
1194
+ }
1195
+ otherStateToSet = pickState(otherStateToSet);
1196
+ _this.internalSetState(function (_ref8) {
1197
+ var selectedItem = _ref8.selectedItem;
1198
+ return _extends({
1199
+ isOpen: _this.props.defaultIsOpen,
1200
+ highlightedIndex: _this.props.defaultHighlightedIndex,
1201
+ inputValue: _this.props.itemToString(selectedItem)
1202
+ }, otherStateToSet);
1203
+ }, cb);
1204
+ };
1205
+ _this.toggleMenu = function (otherStateToSet, cb) {
1206
+ if (otherStateToSet === void 0) {
1207
+ otherStateToSet = {};
1208
+ }
1209
+ otherStateToSet = pickState(otherStateToSet);
1210
+ _this.internalSetState(function (_ref9) {
1211
+ var isOpen = _ref9.isOpen;
1212
+ return _extends({
1213
+ isOpen: !isOpen
1214
+ }, isOpen && {
1215
+ highlightedIndex: _this.props.defaultHighlightedIndex
1216
+ }, otherStateToSet);
1217
+ }, function () {
1218
+ var _this$getState7 = _this.getState(),
1219
+ isOpen = _this$getState7.isOpen,
1220
+ highlightedIndex = _this$getState7.highlightedIndex;
1221
+ if (isOpen) {
1222
+ if (_this.getItemCount() > 0 && typeof highlightedIndex === 'number') {
1223
+ _this.setHighlightedIndex(highlightedIndex, otherStateToSet);
1224
+ }
1225
+ }
1226
+ cbToCb(cb)();
1227
+ });
1228
+ };
1229
+ _this.openMenu = function (cb) {
1230
+ _this.internalSetState({
1231
+ isOpen: true
1232
+ }, cb);
1233
+ };
1234
+ _this.closeMenu = function (cb) {
1235
+ _this.internalSetState({
1236
+ isOpen: false
1237
+ }, cb);
1238
+ };
1239
+ _this.updateStatus = debounce(function () {
1240
+ var _this$props;
1241
+ if (!((_this$props = _this.props) != null && (_this$props = _this$props.environment) != null && _this$props.document)) {
1242
+ return;
1243
+ }
1244
+ var state = _this.getState();
1245
+ var item = _this.items[state.highlightedIndex];
1246
+ var resultCount = _this.getItemCount();
1247
+ var status = _this.props.getA11yStatusMessage(_extends({
1248
+ itemToString: _this.props.itemToString,
1249
+ previousResultCount: _this.previousResultCount,
1250
+ resultCount: resultCount,
1251
+ highlightedItem: item
1252
+ }, state));
1253
+ _this.previousResultCount = resultCount;
1254
+ setStatus(status, _this.props.environment.document);
1255
+ }, 200);
1256
+ var _this$props2 = _this.props,
1257
+ defaultHighlightedIndex = _this$props2.defaultHighlightedIndex,
1258
+ _this$props2$initialH = _this$props2.initialHighlightedIndex,
1259
+ _highlightedIndex = _this$props2$initialH === void 0 ? defaultHighlightedIndex : _this$props2$initialH,
1260
+ defaultIsOpen = _this$props2.defaultIsOpen,
1261
+ _this$props2$initialI = _this$props2.initialIsOpen,
1262
+ _isOpen = _this$props2$initialI === void 0 ? defaultIsOpen : _this$props2$initialI,
1263
+ _this$props2$initialI2 = _this$props2.initialInputValue,
1264
+ _inputValue = _this$props2$initialI2 === void 0 ? '' : _this$props2$initialI2,
1265
+ _this$props2$initialS = _this$props2.initialSelectedItem,
1266
+ _selectedItem = _this$props2$initialS === void 0 ? null : _this$props2$initialS;
1267
+ var _state = _this.getState({
1268
+ highlightedIndex: _highlightedIndex,
1269
+ isOpen: _isOpen,
1270
+ inputValue: _inputValue,
1271
+ selectedItem: _selectedItem
1272
+ });
1273
+ if (_state.selectedItem != null && _this.props.initialInputValue === undefined) {
1274
+ _state.inputValue = _this.props.itemToString(_state.selectedItem);
1275
+ }
1276
+ _this.state = _state;
1277
+ return _this;
1278
+ }
1279
+ _inheritsLoose(Downshift, _Component);
1280
+ var _proto = Downshift.prototype;
1281
+ /**
1282
+ * Clear all running timeouts
1283
+ */
1284
+ _proto.internalClearTimeouts = function internalClearTimeouts() {
1285
+ this.timeoutIds.forEach(function (id) {
1286
+ clearTimeout(id);
1287
+ });
1288
+ this.timeoutIds = [];
1289
+ }
1290
+
1291
+ /**
1292
+ * Gets the state based on internal state or props
1293
+ * If a state value is passed via props, then that
1294
+ * is the value given, otherwise it's retrieved from
1295
+ * stateToMerge
1296
+ *
1297
+ * @param {Object} stateToMerge defaults to this.state
1298
+ * @return {Object} the state
1299
+ */;
1300
+ _proto.getState = function getState$1(stateToMerge) {
1301
+ if (stateToMerge === void 0) {
1302
+ stateToMerge = this.state;
1303
+ }
1304
+ return getState(stateToMerge, this.props);
1305
+ };
1306
+ _proto.getItemCount = function getItemCount() {
1307
+ // things read better this way. They're in priority order:
1308
+ // 1. `this.itemCount`
1309
+ // 2. `this.props.itemCount`
1310
+ // 3. `this.items.length`
1311
+ var itemCount = this.items.length;
1312
+ if (this.itemCount != null) {
1313
+ itemCount = this.itemCount;
1314
+ } else if (this.props.itemCount !== undefined) {
1315
+ itemCount = this.props.itemCount;
1316
+ }
1317
+ return itemCount;
1318
+ };
1319
+ _proto.getItemNodeFromIndex = function getItemNodeFromIndex(index) {
1320
+ return this.props.environment ? this.props.environment.document.getElementById(this.getItemId(index)) : null;
1321
+ };
1322
+ _proto.scrollHighlightedItemIntoView = function scrollHighlightedItemIntoView() {
1323
+ /* istanbul ignore else (react-native) */
1324
+ {
1325
+ var node = this.getItemNodeFromIndex(this.getState().highlightedIndex);
1326
+ this.props.scrollIntoView(node, this._menuNode);
1327
+ }
1328
+ };
1329
+ _proto.moveHighlightedIndex = function moveHighlightedIndex(amount, otherStateToSet) {
1330
+ var itemCount = this.getItemCount();
1331
+ var _this$getState8 = this.getState(),
1332
+ highlightedIndex = _this$getState8.highlightedIndex;
1333
+ if (itemCount > 0) {
1334
+ var nextHighlightedIndex = getHighlightedIndex(highlightedIndex, amount, {
1335
+ length: itemCount
1336
+ }, this.isItemDisabled, true);
1337
+ this.setHighlightedIndex(nextHighlightedIndex, otherStateToSet);
1338
+ }
1339
+ };
1340
+ _proto.getStateAndHelpers = function getStateAndHelpers() {
1341
+ var _this$getState9 = this.getState(),
1342
+ highlightedIndex = _this$getState9.highlightedIndex,
1343
+ inputValue = _this$getState9.inputValue,
1344
+ selectedItem = _this$getState9.selectedItem,
1345
+ isOpen = _this$getState9.isOpen;
1346
+ var itemToString = this.props.itemToString;
1347
+ var id = this.id;
1348
+ var getRootProps = this.getRootProps,
1349
+ getToggleButtonProps = this.getToggleButtonProps,
1350
+ getLabelProps = this.getLabelProps,
1351
+ getMenuProps = this.getMenuProps,
1352
+ getInputProps = this.getInputProps,
1353
+ getItemProps = this.getItemProps,
1354
+ openMenu = this.openMenu,
1355
+ closeMenu = this.closeMenu,
1356
+ toggleMenu = this.toggleMenu,
1357
+ selectItem = this.selectItem,
1358
+ selectItemAtIndex = this.selectItemAtIndex,
1359
+ selectHighlightedItem = this.selectHighlightedItem,
1360
+ setHighlightedIndex = this.setHighlightedIndex,
1361
+ clearSelection = this.clearSelection,
1362
+ clearItems = this.clearItems,
1363
+ reset = this.reset,
1364
+ setItemCount = this.setItemCount,
1365
+ unsetItemCount = this.unsetItemCount,
1366
+ setState = this.internalSetState;
1367
+ return {
1368
+ // prop getters
1369
+ getRootProps: getRootProps,
1370
+ getToggleButtonProps: getToggleButtonProps,
1371
+ getLabelProps: getLabelProps,
1372
+ getMenuProps: getMenuProps,
1373
+ getInputProps: getInputProps,
1374
+ getItemProps: getItemProps,
1375
+ // actions
1376
+ reset: reset,
1377
+ openMenu: openMenu,
1378
+ closeMenu: closeMenu,
1379
+ toggleMenu: toggleMenu,
1380
+ selectItem: selectItem,
1381
+ selectItemAtIndex: selectItemAtIndex,
1382
+ selectHighlightedItem: selectHighlightedItem,
1383
+ setHighlightedIndex: setHighlightedIndex,
1384
+ clearSelection: clearSelection,
1385
+ clearItems: clearItems,
1386
+ setItemCount: setItemCount,
1387
+ unsetItemCount: unsetItemCount,
1388
+ setState: setState,
1389
+ // props
1390
+ itemToString: itemToString,
1391
+ // derived
1392
+ id: id,
1393
+ // state
1394
+ highlightedIndex: highlightedIndex,
1395
+ inputValue: inputValue,
1396
+ isOpen: isOpen,
1397
+ selectedItem: selectedItem
1398
+ };
1399
+ };
1400
+ _proto.componentDidMount = function componentDidMount() {
1401
+ var _this4 = this;
1402
+ /* istanbul ignore if (react-native) */
1403
+ if (process.env.NODE_ENV !== 'production' && true && this.getMenuProps.called && !this.getMenuProps.suppressRefError) {
1404
+ validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
1405
+ }
1406
+
1407
+ /* istanbul ignore if (react-native or SSR) */
1408
+ if (!this.props.environment) {
1409
+ this.cleanup = function () {
1410
+ _this4.internalClearTimeouts();
1411
+ };
1412
+ } else {
1413
+ // this.isMouseDown helps us track whether the mouse is currently held down.
1414
+ // This is useful when the user clicks on an item in the list, but holds the mouse
1415
+ // down long enough for the list to disappear (because the blur event fires on the input)
1416
+ // this.isMouseDown is used in the blur handler on the input to determine whether the blur event should
1417
+ // trigger hiding the menu.
1418
+ var onMouseDown = function onMouseDown() {
1419
+ _this4.isMouseDown = true;
1420
+ };
1421
+ var onMouseUp = function onMouseUp(event) {
1422
+ _this4.isMouseDown = false;
1423
+ // if the target element or the activeElement is within a downshift node
1424
+ // then we don't want to reset downshift
1425
+ var contextWithinDownshift = targetWithinDownshift(event.target, [_this4._rootNode, _this4._menuNode], _this4.props.environment);
1426
+ if (!contextWithinDownshift && _this4.getState().isOpen) {
1427
+ _this4.reset({
1428
+ type: mouseUp
1429
+ }, function () {
1430
+ return _this4.props.onOuterClick(_this4.getStateAndHelpers());
1431
+ });
1432
+ }
1433
+ };
1434
+ // Touching an element in iOS gives focus and hover states, but touching out of
1435
+ // the element will remove hover, and persist the focus state, resulting in the
1436
+ // blur event not being triggered.
1437
+ // this.isTouchMove helps us track whether the user is tapping or swiping on a touch screen.
1438
+ // If the user taps outside of Downshift, the component should be reset,
1439
+ // but not if the user is swiping
1440
+ var onTouchStart = function onTouchStart() {
1441
+ _this4.isTouchMove = false;
1442
+ };
1443
+ var onTouchMove = function onTouchMove() {
1444
+ _this4.isTouchMove = true;
1445
+ };
1446
+ var onTouchEnd = function onTouchEnd(event) {
1447
+ var contextWithinDownshift = targetWithinDownshift(event.target, [_this4._rootNode, _this4._menuNode], _this4.props.environment, false);
1448
+ if (!_this4.isTouchMove && !contextWithinDownshift && _this4.getState().isOpen) {
1449
+ _this4.reset({
1450
+ type: touchEnd
1451
+ }, function () {
1452
+ return _this4.props.onOuterClick(_this4.getStateAndHelpers());
1453
+ });
1454
+ }
1455
+ };
1456
+ var environment = this.props.environment;
1457
+ environment.addEventListener('mousedown', onMouseDown);
1458
+ environment.addEventListener('mouseup', onMouseUp);
1459
+ environment.addEventListener('touchstart', onTouchStart);
1460
+ environment.addEventListener('touchmove', onTouchMove);
1461
+ environment.addEventListener('touchend', onTouchEnd);
1462
+ this.cleanup = function () {
1463
+ _this4.internalClearTimeouts();
1464
+ _this4.updateStatus.cancel();
1465
+ environment.removeEventListener('mousedown', onMouseDown);
1466
+ environment.removeEventListener('mouseup', onMouseUp);
1467
+ environment.removeEventListener('touchstart', onTouchStart);
1468
+ environment.removeEventListener('touchmove', onTouchMove);
1469
+ environment.removeEventListener('touchend', onTouchEnd);
1470
+ };
1471
+ }
1472
+ };
1473
+ _proto.shouldScroll = function shouldScroll(prevState, prevProps) {
1474
+ var _ref0 = this.props.highlightedIndex === undefined ? this.getState() : this.props,
1475
+ currentHighlightedIndex = _ref0.highlightedIndex;
1476
+ var _ref1 = prevProps.highlightedIndex === undefined ? prevState : prevProps,
1477
+ prevHighlightedIndex = _ref1.highlightedIndex;
1478
+ var scrollWhenOpen = currentHighlightedIndex && this.getState().isOpen && !prevState.isOpen;
1479
+ var scrollWhenNavigating = currentHighlightedIndex !== prevHighlightedIndex;
1480
+ return scrollWhenOpen || scrollWhenNavigating;
1481
+ };
1482
+ _proto.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
1483
+ if (process.env.NODE_ENV !== 'production') {
1484
+ validateControlledUnchanged(this.state, prevProps, this.props);
1485
+ /* istanbul ignore if (react-native) */
1486
+ if (this.getMenuProps.called && !this.getMenuProps.suppressRefError) {
1487
+ validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
1488
+ }
1489
+ }
1490
+ if (isControlledProp(this.props, 'selectedItem') && this.props.selectedItemChanged(prevProps.selectedItem, this.props.selectedItem)) {
1491
+ this.internalSetState({
1492
+ type: controlledPropUpdatedSelectedItem,
1493
+ inputValue: this.props.itemToString(this.props.selectedItem)
1494
+ });
1495
+ }
1496
+ if (!this.avoidScrolling && this.shouldScroll(prevState, prevProps)) {
1497
+ this.scrollHighlightedItemIntoView();
1498
+ }
1499
+
1500
+ /* istanbul ignore else (react-native) */
1501
+ {
1502
+ this.updateStatus();
1503
+ }
1504
+ };
1505
+ _proto.componentWillUnmount = function componentWillUnmount() {
1506
+ this.cleanup(); // avoids memory leak
1507
+ };
1508
+ _proto.render = function render() {
1509
+ var children = unwrapArray(this.props.children, noop);
1510
+ // because the items are rerendered every time we call the children
1511
+ // we clear this out each render and it will be populated again as
1512
+ // getItemProps is called.
1513
+ this.clearItems();
1514
+ // we reset this so we know whether the user calls getRootProps during
1515
+ // this render. If they do then we don't need to do anything,
1516
+ // if they don't then we need to clone the element they return and
1517
+ // apply the props for them.
1518
+ this.getRootProps.called = false;
1519
+ this.getRootProps.refKey = undefined;
1520
+ this.getRootProps.suppressRefError = undefined;
1521
+ // we do something similar for getMenuProps
1522
+ this.getMenuProps.called = false;
1523
+ this.getMenuProps.refKey = undefined;
1524
+ this.getMenuProps.suppressRefError = undefined;
1525
+ // we do something similar for getLabelProps
1526
+ this.getLabelProps.called = false;
1527
+ // and something similar for getInputProps
1528
+ this.getInputProps.called = false;
1529
+ var element = unwrapArray(children(this.getStateAndHelpers()));
1530
+ if (!element) {
1531
+ return null;
1532
+ }
1533
+ if (this.getRootProps.called || this.props.suppressRefError) {
1534
+ if (process.env.NODE_ENV !== 'production' && !this.getRootProps.suppressRefError && !this.props.suppressRefError) {
1535
+ validateGetRootPropsCalledCorrectly(element, this.getRootProps);
1536
+ }
1537
+ return element;
1538
+ } else if (isDOMElement(element)) {
1539
+ // they didn't apply the root props, but we can clone
1540
+ // this and apply the props ourselves
1541
+ return React.cloneElement(element, this.getRootProps(getElementProps(element)));
1542
+ }
1543
+
1544
+ /* istanbul ignore else */
1545
+ if (process.env.NODE_ENV !== 'production') {
1546
+ // they didn't apply the root props, but they need to
1547
+ // otherwise we can't query around the autocomplete
1548
+
1549
+ throw new Error('downshift: If you return a non-DOM element, you must apply the getRootProps function');
1550
+ }
1551
+
1552
+ /* istanbul ignore next */
1553
+ return undefined;
1554
+ };
1555
+ return Downshift;
1556
+ }(React.Component);
1557
+ Downshift.defaultProps = {
1558
+ defaultHighlightedIndex: null,
1559
+ defaultIsOpen: false,
1560
+ getA11yStatusMessage: getA11yStatusMessage,
1561
+ itemToString: function itemToString(i) {
1562
+ if (i == null) {
1563
+ return '';
1564
+ }
1565
+ if (process.env.NODE_ENV !== 'production' && isPlainObject(i) && !i.hasOwnProperty('toString')) {
1566
+ // eslint-disable-next-line no-console
1567
+ console.warn('downshift: An object was passed to the default implementation of `itemToString`. You should probably provide your own `itemToString` implementation. Please refer to the `itemToString` API documentation.', 'The object that was passed:', i);
1568
+ }
1569
+ return String(i);
1570
+ },
1571
+ onStateChange: noop,
1572
+ onInputValueChange: noop,
1573
+ onUserAction: noop,
1574
+ onChange: noop,
1575
+ onSelect: noop,
1576
+ onOuterClick: noop,
1577
+ selectedItemChanged: function selectedItemChanged(prevItem, item) {
1578
+ return prevItem !== item;
1579
+ },
1580
+ environment: /* istanbul ignore next (ssr) */
1581
+ typeof window === 'undefined' || false ? undefined : window,
1582
+ stateReducer: function stateReducer(state, stateToSet) {
1583
+ return stateToSet;
1584
+ },
1585
+ suppressRefError: false,
1586
+ scrollIntoView: scrollIntoView
1587
+ };
1588
+ Downshift.stateChangeTypes = stateChangeTypes$4;
1589
+ return Downshift;
1590
+ }();
1591
+ function validateGetMenuPropsCalledCorrectly(node, _ref10) {
1592
+ var refKey = _ref10.refKey;
1593
+ if (!node) {
1594
+ // eslint-disable-next-line no-console
1595
+ console.error("downshift: The ref prop \"" + refKey + "\" from getMenuProps was not applied correctly on your menu element.");
1596
+ }
1597
+ }
1598
+ function validateGetRootPropsCalledCorrectly(element, _ref11) {
1599
+ var refKey = _ref11.refKey;
1600
+ var refKeySpecified = refKey !== 'ref';
1601
+ var isComposite = !isDOMElement(element);
1602
+ if (isComposite && !refKeySpecified && !reactIs.isForwardRef(element)) {
1603
+ // eslint-disable-next-line no-console
1604
+ console.error('downshift: You returned a non-DOM element. You must specify a refKey in getRootProps');
1605
+ } else if (!isComposite && refKeySpecified) {
1606
+ // eslint-disable-next-line no-console
1607
+ console.error("downshift: You returned a DOM element. You should not specify a refKey in getRootProps. You specified \"" + refKey + "\"");
1608
+ }
1609
+ if (!reactIs.isForwardRef(element) && !getElementProps(element)[refKey]) {
1610
+ // eslint-disable-next-line no-console
1611
+ console.error("downshift: You must apply the ref prop \"" + refKey + "\" from getRootProps onto your root element.");
1612
+ }
1613
+ }
1614
+
1615
+ function capitalizeString(string) {
1616
+ return "" + string.slice(0, 1).toUpperCase() + string.slice(1);
1617
+ }
1618
+
1619
+ function callOnChangeProps(action, props, state, newState) {
1620
+ var type = action.type;
1621
+ var changes = {};
1622
+ var keys = Object.keys(state);
1623
+ for (var _i = 0, _keys = keys; _i < _keys.length; _i++) {
1624
+ var key = _keys[_i];
1625
+ invokeOnChangeHandler(key, action, props, state, newState);
1626
+ if (newState[key] !== state[key]) {
1627
+ changes[key] = newState[key];
1628
+ }
1629
+ }
1630
+ if (props.onStateChange && Object.keys(changes).length) {
1631
+ props.onStateChange(_extends({
1632
+ type: type
1633
+ }, changes));
1634
+ }
1635
+ }
1636
+ function invokeOnChangeHandler(key, action, props, state, newState) {
1637
+ if (newState[key] === state[key]) {
1638
+ return;
1639
+ }
1640
+ var handlerKey = "on" + capitalizeString(key) + "Change";
1641
+ var handler = props[handlerKey];
1642
+ if (typeof handler !== 'function') {
1643
+ return;
1644
+ }
1645
+ var type = action.type;
1646
+ handler(_extends({
1647
+ type: type
1648
+ }, newState));
1649
+ }
1650
+
1651
+ /**
1652
+ * Computes the controlled state using a the previous state, props,
1653
+ * two reducers, one from downshift and an optional one from the user.
1654
+ * Also calls the onChange handlers for state values that have changed.
1655
+ *
1656
+ * @param {Function} reducer Reducer function from downshift.
1657
+ * @param {Object} props The hook props, also passed to createInitialState.
1658
+ * @param {Function} createInitialState Function that returns the initial state.
1659
+ * @param {Function} isStateEqual Function that checks if a previous state is equal to the next.
1660
+ * @returns {Array} An array with the state and an action dispatcher.
1661
+ */
1662
+ function useEnhancedReducer(reducer, props, createInitialState, isStateEqual) {
1663
+ var prevStateRef = React__namespace.useRef(null);
1664
+ var actionRef = React__namespace.useRef(undefined);
1665
+ var propsRef = useLatestRef(props);
1666
+ var enhancedReducer = React__namespace.useCallback(function (state, action) {
1667
+ actionRef.current = action;
1668
+ state = getState(state, propsRef.current);
1669
+ var changes = reducer(state, propsRef.current, action);
1670
+ var newState = propsRef.current.stateReducer(state, _extends({}, action, {
1671
+ changes: changes
1672
+ }));
1673
+ return _extends({}, state, newState);
1674
+ }, [propsRef, reducer]);
1675
+ var _React$useReducer = React__namespace.useReducer(enhancedReducer, props, createInitialState),
1676
+ state = _React$useReducer[0],
1677
+ dispatch = _React$useReducer[1];
1678
+ var action = actionRef.current;
1679
+ React__namespace.useEffect(function () {
1680
+ var _prevStateRef$current;
1681
+ var prevState = getState((_prevStateRef$current = prevStateRef.current) != null ? _prevStateRef$current : {}, propsRef.current);
1682
+ var shouldCallOnChangeProps = action && prevStateRef.current && !isStateEqual(prevState, state);
1683
+ if (shouldCallOnChangeProps) {
1684
+ callOnChangeProps(action, propsRef.current, prevState, state);
1685
+ }
1686
+ prevStateRef.current = state;
1687
+ }, [state, action, isStateEqual, propsRef]);
1688
+ return [state, dispatch];
1689
+ }
1690
+
1691
+ /**
1692
+ * Wraps the useEnhancedReducer and applies the controlled prop values before
1693
+ * returning the new state.
1694
+ *
1695
+ * @param {Function} reducer Reducer function from downshift.
1696
+ * @param {Object} props The hook props, also passed to createInitialState.
1697
+ * @param {Function} createInitialState Function that returns the initial state.
1698
+ * @param {Function} isStateEqual Function that checks if a previous state is equal to the next.
1699
+ * @returns {Array} An array with the state and an action dispatcher.
1700
+ */
1701
+ function useControlledReducer$1(reducer, props, createInitialState, isStateEqual) {
1702
+ var _useEnhancedReducer = useEnhancedReducer(reducer, props, createInitialState, isStateEqual),
1703
+ state = _useEnhancedReducer[0],
1704
+ dispatch = _useEnhancedReducer[1];
1705
+ return [getState(state, props), dispatch];
1706
+ }
1707
+
1708
+ /**
1709
+ * Returns both the item and index when both or either is passed.
1710
+ *
1711
+ * @param itemProp The item which could be undefined.
1712
+ * @param indexProp The index which could be undefined.
1713
+ * @param items The array of items to get the item based on index.
1714
+ * @param errorMessage The error to be thrown if index and item could not be returned for any reason.
1715
+ * @returns An array with item and index.
1716
+ */
1717
+ function getItemAndIndex(itemProp, indexProp, items, errorMessage) {
1718
+ if (itemProp !== undefined && indexProp !== undefined) {
1719
+ return [itemProp, indexProp];
1720
+ }
1721
+ if (itemProp !== undefined) {
1722
+ var index = items.indexOf(itemProp);
1723
+ if (index < 0) {
1724
+ throw new Error(errorMessage);
1725
+ }
1726
+ return [itemProp, items.indexOf(itemProp)];
1727
+ }
1728
+ if (indexProp !== undefined) {
1729
+ var item = items[indexProp];
1730
+ if (item === undefined) {
1731
+ throw new Error(errorMessage);
1732
+ }
1733
+ return [item, indexProp];
1734
+ }
1735
+ throw new Error(errorMessage);
1736
+ }
1737
+
1738
+ /**
1739
+ * Tracks if it's the first render.
1740
+ */
1741
+ function useIsInitialMount() {
1742
+ var isInitialMountRef = React__namespace.useRef(true);
1743
+ React__namespace.useEffect(function () {
1744
+ isInitialMountRef.current = false;
1745
+ return function () {
1746
+ isInitialMountRef.current = true;
1747
+ };
1748
+ }, []);
1749
+ return isInitialMountRef.current;
1750
+ }
1751
+
1752
+ /**
1753
+ * Default state reducer that returns the changes.
1754
+ *
1755
+ */
1756
+ function stateReducer(_s, a) {
1757
+ return a.changes;
1758
+ }
1759
+
1760
+ // Shared between all exports.
1761
+ var propTypes$5 = {
1762
+ environment: PropTypes.shape({
1763
+ addEventListener: PropTypes.func.isRequired,
1764
+ removeEventListener: PropTypes.func.isRequired,
1765
+ document: PropTypes.shape({
1766
+ createElement: PropTypes.func.isRequired,
1767
+ getElementById: PropTypes.func.isRequired,
1768
+ activeElement: PropTypes.any.isRequired,
1769
+ body: PropTypes.any.isRequired
1770
+ }).isRequired,
1771
+ Node: PropTypes.func.isRequired
1772
+ }),
1773
+ itemToKey: PropTypes.func,
1774
+ stateReducer: PropTypes.func
1775
+ };
1776
+
1777
+ function getDefaultValue$1(props, propKey, defaultStateValues) {
1778
+ var defaultValue = props["default" + capitalizeString(propKey)];
1779
+ if (defaultValue !== undefined) {
1780
+ return defaultValue;
1781
+ }
1782
+ return defaultStateValues[propKey];
1783
+ }
1784
+
1785
+ function getInitialValue$1(props, propKey, defaultStateValues) {
1786
+ var value = props[propKey];
1787
+ if (value !== undefined) {
1788
+ return value;
1789
+ }
1790
+ var initialValue = props["initial" + capitalizeString(propKey)];
1791
+ if (initialValue !== undefined) {
1792
+ return initialValue;
1793
+ }
1794
+ return getDefaultValue$1(props, propKey, defaultStateValues);
1795
+ }
1796
+
1797
+ /**
1798
+ * Debounced call for updating the a11y message.
1799
+ */
1800
+ var updateA11yStatus = debounce$1(function (status, document) {
1801
+ setStatus(status, document);
1802
+ }, 200);
1803
+
1804
+ /**
1805
+ * Adds an a11y aria live status message if getA11yStatusMessage is passed.
1806
+ * @param getA11yStatusMessage The function that builds the status message.
1807
+ * @param options The options to be passed to getA11yStatusMessage if called.
1808
+ * @param dependencyArray The dependency array that triggers the status message setter via useEffect.
1809
+ * @param environment The environment object containing the document.
1810
+ */
1811
+ function useA11yMessageStatus(getA11yStatusMessage, options, dependencyArray, environment) {
1812
+ var document = environment == null ? void 0 : environment.document;
1813
+ var isInitialMount = useIsInitialMount();
1814
+
1815
+ // Adds an a11y aria live status message if getA11yStatusMessage is passed.
1816
+ React__namespace.useEffect(function () {
1817
+ if (!getA11yStatusMessage || isInitialMount || false || !document) {
1818
+ return;
1819
+ }
1820
+ var status = getA11yStatusMessage(options);
1821
+ updateA11yStatus(status, document);
1822
+
1823
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1824
+ }, dependencyArray);
1825
+
1826
+ // Cleanup the status message container.
1827
+ React__namespace.useEffect(function () {
1828
+ return function () {
1829
+ updateA11yStatus.cancel();
1830
+ cleanupStatusDiv(document);
1831
+ };
1832
+ }, [document]);
1833
+ }
1834
+
1835
+ // Shared between useSelect, useCombobox, Downshift.
1836
+ var propTypes$4 = _extends({}, propTypes$5, {
1837
+ getA11yStatusMessage: PropTypes.func,
1838
+ highlightedIndex: PropTypes.number,
1839
+ defaultHighlightedIndex: PropTypes.number,
1840
+ initialHighlightedIndex: PropTypes.number,
1841
+ isOpen: PropTypes.bool,
1842
+ defaultIsOpen: PropTypes.bool,
1843
+ initialIsOpen: PropTypes.bool,
1844
+ selectedItem: PropTypes.any,
1845
+ initialSelectedItem: PropTypes.any,
1846
+ defaultSelectedItem: PropTypes.any,
1847
+ id: PropTypes.string,
1848
+ labelId: PropTypes.string,
1849
+ menuId: PropTypes.string,
1850
+ getItemId: PropTypes.func,
1851
+ toggleButtonId: PropTypes.string,
1852
+ onSelectedItemChange: PropTypes.func,
1853
+ onHighlightedIndexChange: PropTypes.func,
1854
+ onStateChange: PropTypes.func,
1855
+ onIsOpenChange: PropTypes.func,
1856
+ scrollIntoView: PropTypes.func
1857
+ });
1858
+
1859
+ var defaultProps$3 = {
1860
+ itemToString: function itemToString(item) {
1861
+ return item ? String(item) : '';
1862
+ },
1863
+ itemToKey: function itemToKey(item) {
1864
+ return item;
1865
+ },
1866
+ stateReducer: stateReducer,
1867
+ scrollIntoView: scrollIntoView,
1868
+ environment: /* istanbul ignore next (ssr) */
1869
+ typeof window === 'undefined' || false ? undefined : window
1870
+ };
1871
+
1872
+ var defaultStateValues$1 = {
1873
+ highlightedIndex: -1,
1874
+ isOpen: false,
1875
+ selectedItem: null,
1876
+ inputValue: ''
1877
+ };
1878
+
1879
+ // istanbul ignore next
1880
+ var useIsomorphicLayoutEffect = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
1881
+
1882
+ // istanbul ignore next
1883
+ var useElementIds$1 = 'useId' in React__namespace // Avoid conditional useId call
1884
+ ? function useElementIds(_ref) {
1885
+ var id = _ref.id,
1886
+ labelId = _ref.labelId,
1887
+ menuId = _ref.menuId,
1888
+ getItemId = _ref.getItemId,
1889
+ toggleButtonId = _ref.toggleButtonId,
1890
+ inputId = _ref.inputId;
1891
+ // Avoid conditional useId call
1892
+ var reactId = "downshift-" + React__namespace.useId();
1893
+ if (!id) {
1894
+ id = reactId;
1895
+ }
1896
+ var elementIds = React__namespace.useMemo(function () {
1897
+ return {
1898
+ labelId: labelId || id + "-label",
1899
+ menuId: menuId || id + "-menu",
1900
+ getItemId: getItemId || function (index) {
1901
+ return id + "-item-" + index;
1902
+ },
1903
+ toggleButtonId: toggleButtonId || id + "-toggle-button",
1904
+ inputId: inputId || id + "-input"
1905
+ };
1906
+ }, [getItemId, id, inputId, labelId, menuId, toggleButtonId]);
1907
+ return elementIds;
1908
+ } : function useElementIds(_ref2) {
1909
+ var _ref2$id = _ref2.id,
1910
+ id = _ref2$id === void 0 ? "downshift-" + generateId() : _ref2$id,
1911
+ labelId = _ref2.labelId,
1912
+ menuId = _ref2.menuId,
1913
+ getItemId = _ref2.getItemId,
1914
+ toggleButtonId = _ref2.toggleButtonId,
1915
+ inputId = _ref2.inputId;
1916
+ var elementIds = React__namespace.useMemo(function () {
1917
+ return {
1918
+ labelId: labelId || id + "-label",
1919
+ menuId: menuId || id + "-menu",
1920
+ getItemId: getItemId || function (index) {
1921
+ return id + "-item-" + index;
1922
+ },
1923
+ toggleButtonId: toggleButtonId || id + "-toggle-button",
1924
+ inputId: inputId || id + "-input"
1925
+ };
1926
+ }, [getItemId, id, inputId, labelId, menuId, toggleButtonId]);
1927
+ return elementIds;
1928
+ };
1929
+ function isAcceptedCharacterKey(key) {
1930
+ return /^\S{1}$/.test(key);
1931
+ }
1932
+ function getInitialState$3(props) {
1933
+ var selectedItem = getInitialValue$1(props, 'selectedItem', defaultStateValues$1);
1934
+ var isOpen = getInitialValue$1(props, 'isOpen', defaultStateValues$1);
1935
+ var highlightedIndex = getInitialHighlightedIndex(props);
1936
+ var inputValue = getInitialValue$1(props, 'inputValue', defaultStateValues$1);
1937
+ return {
1938
+ highlightedIndex: highlightedIndex < 0 && selectedItem && isOpen ? props.items.findIndex(function (item) {
1939
+ return props.itemToKey(item) === props.itemToKey(selectedItem);
1940
+ }) : highlightedIndex,
1941
+ isOpen: isOpen,
1942
+ selectedItem: selectedItem,
1943
+ inputValue: inputValue
1944
+ };
1945
+ }
1946
+ function getHighlightedIndexOnOpen(props, state, offset) {
1947
+ var items = props.items,
1948
+ initialHighlightedIndex = props.initialHighlightedIndex,
1949
+ defaultHighlightedIndex = props.defaultHighlightedIndex,
1950
+ isItemDisabled = props.isItemDisabled,
1951
+ itemToKey = props.itemToKey;
1952
+ var selectedItem = state.selectedItem,
1953
+ highlightedIndex = state.highlightedIndex;
1954
+ if (items.length === 0) {
1955
+ return -1;
1956
+ }
1957
+
1958
+ // initialHighlightedIndex will give value to highlightedIndex on initial state only.
1959
+ if (initialHighlightedIndex !== undefined && highlightedIndex === initialHighlightedIndex && !isItemDisabled(items[initialHighlightedIndex], initialHighlightedIndex)) {
1960
+ return initialHighlightedIndex;
1961
+ }
1962
+ if (defaultHighlightedIndex !== undefined && !isItemDisabled(items[defaultHighlightedIndex], defaultHighlightedIndex)) {
1963
+ return defaultHighlightedIndex;
1964
+ }
1965
+ if (selectedItem) {
1966
+ return items.findIndex(function (item) {
1967
+ return itemToKey(selectedItem) === itemToKey(item);
1968
+ });
1969
+ }
1970
+ if (offset < 0 && !isItemDisabled(items[items.length - 1], items.length - 1)) {
1971
+ return items.length - 1;
1972
+ }
1973
+ if (offset > 0 && !isItemDisabled(items[0], 0)) {
1974
+ return 0;
1975
+ }
1976
+ return -1;
1977
+ }
1978
+ /**
1979
+ * Tracks mouse and touch events, such as mouseDown, touchMove and touchEnd.
1980
+ *
1981
+ * @param {Window} environment The environment to add the event listeners to, for instance window.
1982
+ * @param {() => void} handleBlur The function that is called if mouseDown or touchEnd occured outside the downshiftElements.
1983
+ * @param {Array<{current: HTMLElement}>} downshiftElementsRefs The refs for the elements that should not trigger a blur action from mouseDown or touchEnd.
1984
+ * @returns {{isMouseDown: boolean, isTouchMove: boolean, isTouchEnd: boolean}} The mouse and touch events information, if any of are happening.
1985
+ */
1986
+ function useMouseAndTouchTracker(environment, handleBlur, downshiftRefs) {
1987
+ var mouseAndTouchTrackersRef = React__namespace.useRef({
1988
+ isMouseDown: false,
1989
+ isTouchMove: false,
1990
+ isTouchEnd: false
1991
+ });
1992
+ var getDownshiftElements = React__namespace.useCallback(function () {
1993
+ return downshiftRefs.map(function (ref) {
1994
+ return ref.current;
1995
+ });
1996
+ }, [downshiftRefs]);
1997
+ React__namespace.useEffect(function () {
1998
+ if (!environment) {
1999
+ return noop;
2000
+ }
2001
+ function onMouseDown() {
2002
+ mouseAndTouchTrackersRef.current.isTouchEnd = false; // reset this one.
2003
+ mouseAndTouchTrackersRef.current.isMouseDown = true;
2004
+ }
2005
+ function onMouseUp(event) {
2006
+ mouseAndTouchTrackersRef.current.isMouseDown = false;
2007
+ if (!targetWithinDownshift(event.target, getDownshiftElements(), environment)) {
2008
+ handleBlur();
2009
+ }
2010
+ }
2011
+ function onTouchStart() {
2012
+ mouseAndTouchTrackersRef.current.isTouchEnd = false;
2013
+ mouseAndTouchTrackersRef.current.isTouchMove = false;
2014
+ }
2015
+ function onTouchMove() {
2016
+ mouseAndTouchTrackersRef.current.isTouchMove = true;
2017
+ }
2018
+ function onTouchEnd(event) {
2019
+ mouseAndTouchTrackersRef.current.isTouchEnd = true;
2020
+ if (!mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, getDownshiftElements(), environment, false)) {
2021
+ handleBlur();
2022
+ }
2023
+ }
2024
+ environment.addEventListener('mousedown', onMouseDown);
2025
+ environment.addEventListener('mouseup', onMouseUp);
2026
+ environment.addEventListener('touchstart', onTouchStart);
2027
+ environment.addEventListener('touchmove', onTouchMove);
2028
+ environment.addEventListener('touchend', onTouchEnd);
2029
+ return function cleanup() {
2030
+ environment.removeEventListener('mousedown', onMouseDown);
2031
+ environment.removeEventListener('mouseup', onMouseUp);
2032
+ environment.removeEventListener('touchstart', onTouchStart);
2033
+ environment.removeEventListener('touchmove', onTouchMove);
2034
+ environment.removeEventListener('touchend', onTouchEnd);
2035
+ };
2036
+ }, [environment, getDownshiftElements, handleBlur]);
2037
+ return mouseAndTouchTrackersRef.current;
2038
+ }
2039
+
2040
+ /* istanbul ignore next */
2041
+ // eslint-disable-next-line import/no-mutable-exports
2042
+ var useGetterPropsCalledChecker = function useGetterPropsCalledChecker() {
2043
+ return noop;
2044
+ };
2045
+ /**
2046
+ * Custom hook that checks if getter props are called correctly.
2047
+ *
2048
+ * @param {...any} propKeys Getter prop names to be handled.
2049
+ * @returns {Function} Setter function called inside getter props to set call information.
2050
+ */
2051
+ /* istanbul ignore next */
2052
+ if (process.env.NODE_ENV !== 'production') {
2053
+ useGetterPropsCalledChecker = function useGetterPropsCalledChecker() {
2054
+ for (var _len = arguments.length, propKeys = new Array(_len), _key = 0; _key < _len; _key++) {
2055
+ propKeys[_key] = arguments[_key];
2056
+ }
2057
+ var getterPropsCalledRef = React__namespace.useRef(propKeys.reduce(function (acc, propKey) {
2058
+ acc[propKey] = {};
2059
+ return acc;
2060
+ }, {}));
2061
+ React__namespace.useEffect(function () {
2062
+ Object.keys(getterPropsCalledRef.current).forEach(function (propKey) {
2063
+ var propCallInfo = getterPropsCalledRef.current[propKey];
2064
+ if (!Object.keys(propCallInfo).length) {
2065
+ // eslint-disable-next-line no-console
2066
+ console.error("downshift: You forgot to call the " + propKey + " getter function on your component / element.");
2067
+ return;
2068
+ }
2069
+ var suppressRefError = propCallInfo.suppressRefError,
2070
+ refKey = propCallInfo.refKey,
2071
+ elementRef = propCallInfo.elementRef;
2072
+ if (suppressRefError) {
2073
+ return;
2074
+ }
2075
+ if (!(elementRef != null && elementRef.current)) {
2076
+ // eslint-disable-next-line no-console
2077
+ console.error("downshift: The ref prop \"" + refKey + "\" from " + propKey + " was not applied correctly on your element.");
2078
+ }
2079
+ });
2080
+ }, []);
2081
+ var setGetterPropCallInfo = React__namespace.useCallback(function (propKey, suppressRefError, refKey, elementRef) {
2082
+ getterPropsCalledRef.current[propKey] = {
2083
+ suppressRefError: suppressRefError,
2084
+ refKey: refKey,
2085
+ elementRef: elementRef
2086
+ };
2087
+ }, []);
2088
+ return setGetterPropCallInfo;
2089
+ };
2090
+ }
2091
+ function useScrollIntoView(_ref3) {
2092
+ var highlightedIndex = _ref3.highlightedIndex,
2093
+ isOpen = _ref3.isOpen,
2094
+ itemRefs = _ref3.itemRefs,
2095
+ getItemNodeFromIndex = _ref3.getItemNodeFromIndex,
2096
+ menuElement = _ref3.menuElement,
2097
+ scrollIntoViewProp = _ref3.scrollIntoView;
2098
+ // used not to scroll on highlight by mouse.
2099
+ var shouldScrollRef = React__namespace.useRef(true);
2100
+ // Scroll on highlighted item if change comes from keyboard.
2101
+ useIsomorphicLayoutEffect(function () {
2102
+ if (highlightedIndex < 0 || !isOpen || !Object.keys(itemRefs.current).length) {
2103
+ return;
2104
+ }
2105
+ if (shouldScrollRef.current === false) {
2106
+ shouldScrollRef.current = true;
2107
+ } else {
2108
+ scrollIntoViewProp(getItemNodeFromIndex(highlightedIndex), menuElement);
2109
+ }
2110
+ // eslint-disable-next-line react-hooks/exhaustive-deps
2111
+ }, [highlightedIndex]);
2112
+ return shouldScrollRef;
2113
+ }
2114
+
2115
+ // eslint-disable-next-line import/no-mutable-exports
2116
+ var useControlPropsValidator = noop;
2117
+ /* istanbul ignore next */
2118
+ if (process.env.NODE_ENV !== 'production') {
2119
+ useControlPropsValidator = function useControlPropsValidator(_ref4) {
2120
+ var props = _ref4.props,
2121
+ state = _ref4.state;
2122
+ // used for checking when props are moving from controlled to uncontrolled.
2123
+ var prevPropsRef = React__namespace.useRef(props);
2124
+ var isInitialMount = useIsInitialMount();
2125
+ React__namespace.useEffect(function () {
2126
+ if (isInitialMount) {
2127
+ return;
2128
+ }
2129
+ validateControlledUnchanged(state, prevPropsRef.current, props);
2130
+ prevPropsRef.current = props;
2131
+ }, [state, props, isInitialMount]);
2132
+ };
2133
+ }
2134
+
2135
+ /**
2136
+ * Handles selection on Enter / Alt + ArrowUp. Closes the menu and resets the highlighted index, unless there is a highlighted.
2137
+ * In that case, selects the item and resets to defaults for open state and highlighted idex.
2138
+ * @param {Object} props The useCombobox props.
2139
+ * @param {number} highlightedIndex The index from the state.
2140
+ * @param {boolean} inputValue Also return the input value for state.
2141
+ * @returns The changes for the state.
2142
+ */
2143
+ function getChangesOnSelection(props, highlightedIndex, inputValue) {
2144
+ var _props$items;
2145
+ if (inputValue === void 0) {
2146
+ inputValue = true;
2147
+ }
2148
+ var shouldSelect = ((_props$items = props.items) == null ? void 0 : _props$items.length) && highlightedIndex >= 0;
2149
+ return _extends({
2150
+ isOpen: false,
2151
+ highlightedIndex: -1
2152
+ }, shouldSelect && _extends({
2153
+ selectedItem: props.items[highlightedIndex],
2154
+ isOpen: getDefaultValue$1(props, 'isOpen', defaultStateValues$1),
2155
+ highlightedIndex: getDefaultValue$1(props, 'highlightedIndex', defaultStateValues$1)
2156
+ }, inputValue && {
2157
+ inputValue: props.itemToString(props.items[highlightedIndex])
2158
+ }));
2159
+ }
2160
+
2161
+ /**
2162
+ * Check if a state is equal for dropdowns, by comparing isOpen, inputValue, highlightedIndex and selected item.
2163
+ * Used by useSelect and useCombobox.
2164
+ *
2165
+ * @param {Object} prevState
2166
+ * @param {Object} newState
2167
+ * @returns {boolean} Wheather the states are deeply equal.
2168
+ */
2169
+ function isDropdownsStateEqual(prevState, newState) {
2170
+ return prevState.isOpen === newState.isOpen && prevState.inputValue === newState.inputValue && prevState.highlightedIndex === newState.highlightedIndex && prevState.selectedItem === newState.selectedItem;
2171
+ }
2172
+
2173
+ /**
2174
+ * Returns the new highlightedIndex based on the defaultHighlightedIndex prop, if it's not disabled.
2175
+ *
2176
+ * @param {Object} props Props from useCombobox or useSelect.
2177
+ * @returns {number} The highlighted index.
2178
+ */
2179
+ function getDefaultHighlightedIndex(props) {
2180
+ var highlightedIndex = getDefaultValue$1(props, 'highlightedIndex', defaultStateValues$1);
2181
+ if (highlightedIndex > -1 && props.isItemDisabled(props.items[highlightedIndex], highlightedIndex)) {
2182
+ return -1;
2183
+ }
2184
+ return highlightedIndex;
2185
+ }
2186
+
2187
+ /**
2188
+ * Returns the new highlightedIndex based on the initialHighlightedIndex prop, if not disabled.
2189
+ *
2190
+ * @param {Object} props Props from useCombobox or useSelect.
2191
+ * @returns {number} The highlighted index.
2192
+ */
2193
+ function getInitialHighlightedIndex(props) {
2194
+ var highlightedIndex = getInitialValue$1(props, 'highlightedIndex', defaultStateValues$1);
2195
+ if (highlightedIndex > -1 && props.isItemDisabled(props.items[highlightedIndex], highlightedIndex)) {
2196
+ return -1;
2197
+ }
2198
+ return highlightedIndex;
2199
+ }
2200
+
2201
+ function downshiftCommonReducer(state, props, action, stateChangeTypes) {
2202
+ var type = action.type;
2203
+ var changes;
2204
+ switch (type) {
2205
+ case stateChangeTypes.ItemMouseMove:
2206
+ changes = {
2207
+ highlightedIndex: action.disabled ? -1 : action.index
2208
+ };
2209
+ break;
2210
+ case stateChangeTypes.MenuMouseLeave:
2211
+ changes = {
2212
+ highlightedIndex: -1
2213
+ };
2214
+ break;
2215
+ case stateChangeTypes.ToggleButtonClick:
2216
+ case stateChangeTypes.FunctionToggleMenu:
2217
+ changes = {
2218
+ isOpen: !state.isOpen,
2219
+ highlightedIndex: state.isOpen ? -1 : getHighlightedIndexOnOpen(props, state, 0)
2220
+ };
2221
+ break;
2222
+ case stateChangeTypes.FunctionOpenMenu:
2223
+ changes = {
2224
+ isOpen: true,
2225
+ highlightedIndex: getHighlightedIndexOnOpen(props, state, 0)
2226
+ };
2227
+ break;
2228
+ case stateChangeTypes.FunctionCloseMenu:
2229
+ changes = {
2230
+ isOpen: false
2231
+ };
2232
+ break;
2233
+ case stateChangeTypes.FunctionSetHighlightedIndex:
2234
+ changes = {
2235
+ highlightedIndex: props.isItemDisabled(props.items[action.highlightedIndex], action.highlightedIndex) ? -1 : action.highlightedIndex
2236
+ };
2237
+ break;
2238
+ case stateChangeTypes.FunctionSetInputValue:
2239
+ changes = {
2240
+ inputValue: action.inputValue
2241
+ };
2242
+ break;
2243
+ case stateChangeTypes.FunctionReset:
2244
+ changes = {
2245
+ highlightedIndex: getDefaultHighlightedIndex(props),
2246
+ isOpen: getDefaultValue$1(props, 'isOpen', defaultStateValues$1),
2247
+ selectedItem: getDefaultValue$1(props, 'selectedItem', defaultStateValues$1),
2248
+ inputValue: getDefaultValue$1(props, 'inputValue', defaultStateValues$1)
2249
+ };
2250
+ break;
2251
+ default:
2252
+ throw new Error('Reducer called without proper action type.');
2253
+ }
2254
+ return _extends({}, state, changes);
2255
+ }
2256
+ /* eslint-enable complexity */
2257
+
2258
+ var propTypes$3 = _extends({}, propTypes$4, {
2259
+ items: PropTypes.array.isRequired,
2260
+ isItemDisabled: PropTypes.func
2261
+ });
2262
+
2263
+ var defaultProps$2 = _extends({}, defaultProps$3, {
2264
+ isItemDisabled: function isItemDisabled() {
2265
+ return false;
2266
+ }
2267
+ });
2268
+
2269
+ function getItemIndexByCharacterKey(_ref) {
2270
+ var keysSoFar = _ref.keysSoFar,
2271
+ highlightedIndex = _ref.highlightedIndex,
2272
+ items = _ref.items,
2273
+ itemToString = _ref.itemToString,
2274
+ isItemDisabled = _ref.isItemDisabled;
2275
+ var lowerCasedKeysSoFar = keysSoFar.toLowerCase();
2276
+ for (var _index = 0; _index < items.length; _index++) {
2277
+ // if we already have a search query in progress, we also consider the current highlighted item.
2278
+ var offsetIndex = (_index + highlightedIndex + (keysSoFar.length < 2 ? 1 : 0)) % items.length;
2279
+ var _item = items[offsetIndex];
2280
+ if (_item !== undefined && itemToString(_item).toLowerCase().startsWith(lowerCasedKeysSoFar) && !isItemDisabled(_item, offsetIndex)) {
2281
+ return offsetIndex;
2282
+ }
2283
+ }
2284
+ return highlightedIndex;
2285
+ }
2286
+
2287
+ var ToggleButtonClick$1 = process.env.NODE_ENV !== "production" ? '__togglebutton_click__' : 0;
2288
+ var ToggleButtonKeyDownArrowDown = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_arrow_down__' : 1;
2289
+ var ToggleButtonKeyDownArrowUp = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_arrow_up__' : 2;
2290
+ var ToggleButtonKeyDownCharacter = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_character__' : 3;
2291
+ var ToggleButtonKeyDownEscape = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_escape__' : 4;
2292
+ var ToggleButtonKeyDownHome = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_home__' : 5;
2293
+ var ToggleButtonKeyDownEnd = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_end__' : 6;
2294
+ var ToggleButtonKeyDownEnter = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_enter__' : 7;
2295
+ var ToggleButtonKeyDownSpaceButton = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_space_button__' : 8;
2296
+ var ToggleButtonKeyDownPageUp = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_page_up__' : 9;
2297
+ var ToggleButtonKeyDownPageDown = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_page_down__' : 10;
2298
+ var ToggleButtonBlur = process.env.NODE_ENV !== "production" ? '__togglebutton_blur__' : 11;
2299
+ var MenuMouseLeave$1 = process.env.NODE_ENV !== "production" ? '__menu_mouse_leave__' : 12;
2300
+ var ItemMouseMove$1 = process.env.NODE_ENV !== "production" ? '__item_mouse_move__' : 13;
2301
+ var ItemClick$1 = process.env.NODE_ENV !== "production" ? '__item_click__' : 14;
2302
+ var FunctionToggleMenu$1 = process.env.NODE_ENV !== "production" ? '__function_toggle_menu__' : 15;
2303
+ var FunctionOpenMenu$1 = process.env.NODE_ENV !== "production" ? '__function_open_menu__' : 16;
2304
+ var FunctionCloseMenu$1 = process.env.NODE_ENV !== "production" ? '__function_close_menu__' : 17;
2305
+ var FunctionSetHighlightedIndex$1 = process.env.NODE_ENV !== "production" ? '__function_set_highlighted_index__' : 18;
2306
+ var FunctionSelectItem$1 = process.env.NODE_ENV !== "production" ? '__function_select_item__' : 19;
2307
+ var FunctionSetInputValue$1 = process.env.NODE_ENV !== "production" ? '__function_set_input_value__' : 20;
2308
+ var FunctionReset$2 = process.env.NODE_ENV !== "production" ? '__function_reset__' : 21;
2309
+
2310
+ var stateChangeTypes$3 = /*#__PURE__*/Object.freeze({
2311
+ __proto__: null,
2312
+ FunctionCloseMenu: FunctionCloseMenu$1,
2313
+ FunctionOpenMenu: FunctionOpenMenu$1,
2314
+ FunctionReset: FunctionReset$2,
2315
+ FunctionSelectItem: FunctionSelectItem$1,
2316
+ FunctionSetHighlightedIndex: FunctionSetHighlightedIndex$1,
2317
+ FunctionSetInputValue: FunctionSetInputValue$1,
2318
+ FunctionToggleMenu: FunctionToggleMenu$1,
2319
+ ItemClick: ItemClick$1,
2320
+ ItemMouseMove: ItemMouseMove$1,
2321
+ MenuMouseLeave: MenuMouseLeave$1,
2322
+ ToggleButtonBlur: ToggleButtonBlur,
2323
+ ToggleButtonClick: ToggleButtonClick$1,
2324
+ ToggleButtonKeyDownArrowDown: ToggleButtonKeyDownArrowDown,
2325
+ ToggleButtonKeyDownArrowUp: ToggleButtonKeyDownArrowUp,
2326
+ ToggleButtonKeyDownCharacter: ToggleButtonKeyDownCharacter,
2327
+ ToggleButtonKeyDownEnd: ToggleButtonKeyDownEnd,
2328
+ ToggleButtonKeyDownEnter: ToggleButtonKeyDownEnter,
2329
+ ToggleButtonKeyDownEscape: ToggleButtonKeyDownEscape,
2330
+ ToggleButtonKeyDownHome: ToggleButtonKeyDownHome,
2331
+ ToggleButtonKeyDownPageDown: ToggleButtonKeyDownPageDown,
2332
+ ToggleButtonKeyDownPageUp: ToggleButtonKeyDownPageUp,
2333
+ ToggleButtonKeyDownSpaceButton: ToggleButtonKeyDownSpaceButton
2334
+ });
2335
+
2336
+ /* eslint-disable complexity */
2337
+ function downshiftSelectReducer(state, props, action) {
2338
+ var _props$items;
2339
+ var type = action.type,
2340
+ altKey = action.altKey;
2341
+ var changes;
2342
+ switch (type) {
2343
+ case ItemClick$1:
2344
+ changes = {
2345
+ isOpen: getDefaultValue$1(props, 'isOpen', defaultStateValues$1),
2346
+ highlightedIndex: getDefaultHighlightedIndex(props),
2347
+ selectedItem: props.items[action.index]
2348
+ };
2349
+ break;
2350
+ case ToggleButtonKeyDownCharacter:
2351
+ {
2352
+ var lowercasedKey = action.key;
2353
+ var inputValue = "" + state.inputValue + lowercasedKey;
2354
+ var prevHighlightedIndex = !state.isOpen && state.selectedItem ? props.items.findIndex(function (item) {
2355
+ return props.itemToKey(item) === props.itemToKey(state.selectedItem);
2356
+ }) : state.highlightedIndex;
2357
+ var highlightedIndex = getItemIndexByCharacterKey({
2358
+ keysSoFar: inputValue,
2359
+ highlightedIndex: prevHighlightedIndex,
2360
+ items: props.items,
2361
+ itemToString: props.itemToString,
2362
+ isItemDisabled: props.isItemDisabled
2363
+ });
2364
+ changes = {
2365
+ inputValue: inputValue,
2366
+ highlightedIndex: highlightedIndex,
2367
+ isOpen: true
2368
+ };
2369
+ }
2370
+ break;
2371
+ case ToggleButtonKeyDownArrowDown:
2372
+ {
2373
+ var _highlightedIndex = state.isOpen ? getHighlightedIndex(state.highlightedIndex, 1, props.items, props.isItemDisabled) : altKey && state.selectedItem == null ? -1 : getHighlightedIndexOnOpen(props, state, 1);
2374
+ changes = {
2375
+ highlightedIndex: _highlightedIndex,
2376
+ isOpen: true
2377
+ };
2378
+ }
2379
+ break;
2380
+ case ToggleButtonKeyDownArrowUp:
2381
+ if (state.isOpen && altKey) {
2382
+ changes = getChangesOnSelection(props, state.highlightedIndex, false);
2383
+ } else {
2384
+ var _highlightedIndex2 = state.isOpen ? getHighlightedIndex(state.highlightedIndex, -1, props.items, props.isItemDisabled) : getHighlightedIndexOnOpen(props, state, -1);
2385
+ changes = {
2386
+ highlightedIndex: _highlightedIndex2,
2387
+ isOpen: true
2388
+ };
2389
+ }
2390
+ break;
2391
+ // only triggered when menu is open.
2392
+ case ToggleButtonKeyDownEnter:
2393
+ case ToggleButtonKeyDownSpaceButton:
2394
+ changes = getChangesOnSelection(props, state.highlightedIndex, false);
2395
+ break;
2396
+ case ToggleButtonKeyDownHome:
2397
+ changes = {
2398
+ highlightedIndex: getNonDisabledIndex(0, false, props.items, props.isItemDisabled),
2399
+ isOpen: true
2400
+ };
2401
+ break;
2402
+ case ToggleButtonKeyDownEnd:
2403
+ changes = {
2404
+ highlightedIndex: getNonDisabledIndex(props.items.length - 1, true, props.items, props.isItemDisabled),
2405
+ isOpen: true
2406
+ };
2407
+ break;
2408
+ case ToggleButtonKeyDownPageUp:
2409
+ changes = {
2410
+ highlightedIndex: getHighlightedIndex(state.highlightedIndex, -10, props.items, props.isItemDisabled)
2411
+ };
2412
+ break;
2413
+ case ToggleButtonKeyDownPageDown:
2414
+ changes = {
2415
+ highlightedIndex: getHighlightedIndex(state.highlightedIndex, 10, props.items, props.isItemDisabled)
2416
+ };
2417
+ break;
2418
+ case ToggleButtonKeyDownEscape:
2419
+ changes = {
2420
+ isOpen: false,
2421
+ highlightedIndex: -1
2422
+ };
2423
+ break;
2424
+ case ToggleButtonBlur:
2425
+ changes = _extends({
2426
+ isOpen: false,
2427
+ highlightedIndex: -1
2428
+ }, state.highlightedIndex >= 0 && ((_props$items = props.items) == null ? void 0 : _props$items.length) && {
2429
+ selectedItem: props.items[state.highlightedIndex]
2430
+ });
2431
+ break;
2432
+ case FunctionSelectItem$1:
2433
+ changes = {
2434
+ selectedItem: action.selectedItem
2435
+ };
2436
+ break;
2437
+ default:
2438
+ return downshiftCommonReducer(state, props, action, stateChangeTypes$3);
2439
+ }
2440
+ return _extends({}, state, changes);
2441
+ }
2442
+ /* eslint-enable complexity */
2443
+
2444
+ var _excluded$3 = ["onClick"],
2445
+ _excluded2$3 = ["onMouseLeave", "refKey", "ref"],
2446
+ _excluded3$2 = ["onBlur", "onClick", "onPress", "onKeyDown", "refKey", "ref"],
2447
+ _excluded4$1 = ["item", "index", "onMouseMove", "onClick", "onMouseDown", "onPress", "refKey", "disabled", "ref"];
2448
+ useSelect.stateChangeTypes = stateChangeTypes$3;
2449
+ function useSelect(userProps) {
2450
+ if (userProps === void 0) {
2451
+ userProps = {};
2452
+ }
2453
+ validatePropTypes$1(userProps, useSelect, propTypes$3);
2454
+ // Props defaults and destructuring.
2455
+ var props = _extends({}, defaultProps$2, userProps);
2456
+ var scrollIntoView = props.scrollIntoView,
2457
+ environment = props.environment,
2458
+ getA11yStatusMessage = props.getA11yStatusMessage;
2459
+ // Initial state depending on controlled props.
2460
+ var _useControlledReducer = useControlledReducer$1(downshiftSelectReducer, props, getInitialState$3, isDropdownsStateEqual),
2461
+ state = _useControlledReducer[0],
2462
+ dispatch = _useControlledReducer[1];
2463
+ var isOpen = state.isOpen,
2464
+ highlightedIndex = state.highlightedIndex,
2465
+ selectedItem = state.selectedItem,
2466
+ inputValue = state.inputValue;
2467
+ // Element efs.
2468
+ var toggleButtonRef = React.useRef(null);
2469
+ var menuRef = React.useRef(null);
2470
+ var itemRefs = React.useRef({});
2471
+
2472
+ // used to keep the inputValue clearTimeout object between renders.
2473
+ var clearTimeoutRef = React.useRef(null);
2474
+ // prevent id re-generation between renders.
2475
+ var elementIds = useElementIds$1(props);
2476
+ // utility callback to get item element.
2477
+ var latest = useLatestRef({
2478
+ state: state,
2479
+ props: props
2480
+ });
2481
+
2482
+ // Some utils.
2483
+ var getItemNodeFromIndex = React.useCallback(function (index) {
2484
+ return itemRefs.current[elementIds.getItemId(index)];
2485
+ }, [elementIds]);
2486
+
2487
+ // Effects.
2488
+ // Adds an a11y aria live status message if getA11yStatusMessage is passed.
2489
+ useA11yMessageStatus(getA11yStatusMessage, state, [isOpen, highlightedIndex, selectedItem, inputValue], environment);
2490
+ // Scroll on highlighted item if change comes from keyboard.
2491
+ var shouldScrollRef = useScrollIntoView({
2492
+ menuElement: menuRef.current,
2493
+ highlightedIndex: highlightedIndex,
2494
+ isOpen: isOpen,
2495
+ itemRefs: itemRefs,
2496
+ scrollIntoView: scrollIntoView,
2497
+ getItemNodeFromIndex: getItemNodeFromIndex
2498
+ });
2499
+ // Sets cleanup for the keysSoFar callback, debounded after 500ms.
2500
+ React.useEffect(function () {
2501
+ // init the clean function here as we need access to dispatch.
2502
+ clearTimeoutRef.current = debounce(function (outerDispatch) {
2503
+ outerDispatch({
2504
+ type: FunctionSetInputValue$1,
2505
+ inputValue: ''
2506
+ });
2507
+ }, 500);
2508
+
2509
+ // Cancel any pending debounced calls on mount
2510
+ return function () {
2511
+ clearTimeoutRef.current.cancel();
2512
+ };
2513
+ }, []);
2514
+ // Invokes the keysSoFar callback set up above.
2515
+ React.useEffect(function () {
2516
+ if (!inputValue) {
2517
+ return;
2518
+ }
2519
+ clearTimeoutRef.current(dispatch);
2520
+ }, [dispatch, inputValue]);
2521
+ useControlPropsValidator({
2522
+ props: props,
2523
+ state: state
2524
+ });
2525
+ // Focus the toggle button on first render if required.
2526
+ React.useEffect(function () {
2527
+ var focusOnOpen = getInitialValue$1(props, 'isOpen', defaultStateValues$1);
2528
+ if (focusOnOpen && toggleButtonRef.current) {
2529
+ toggleButtonRef.current.focus();
2530
+ }
2531
+ // eslint-disable-next-line react-hooks/exhaustive-deps
2532
+ }, []);
2533
+ var handleBlurInTracker = React.useCallback(function handleBlur() {
2534
+ if (latest.current.state.isOpen) {
2535
+ dispatch({
2536
+ type: ToggleButtonBlur
2537
+ });
2538
+ }
2539
+ }, [dispatch, latest]);
2540
+ var downshiftRefs = React.useMemo(function () {
2541
+ return [menuRef, toggleButtonRef];
2542
+ }, []);
2543
+ var mouseAndTouchTrackers = useMouseAndTouchTracker(environment, handleBlurInTracker, downshiftRefs);
2544
+ var setGetterPropCallInfo = useGetterPropsCalledChecker('getMenuProps', 'getToggleButtonProps');
2545
+ // Reset itemRefs on close.
2546
+ React.useEffect(function () {
2547
+ if (!isOpen) {
2548
+ itemRefs.current = {};
2549
+ }
2550
+ }, [isOpen]);
2551
+
2552
+ // Event handler functions.
2553
+ var toggleButtonKeyDownHandlers = React.useMemo(function () {
2554
+ return {
2555
+ ArrowDown: function ArrowDown(event) {
2556
+ event.preventDefault();
2557
+ dispatch({
2558
+ type: ToggleButtonKeyDownArrowDown,
2559
+ altKey: event.altKey
2560
+ });
2561
+ },
2562
+ ArrowUp: function ArrowUp(event) {
2563
+ event.preventDefault();
2564
+ dispatch({
2565
+ type: ToggleButtonKeyDownArrowUp,
2566
+ altKey: event.altKey
2567
+ });
2568
+ },
2569
+ Home: function Home(event) {
2570
+ event.preventDefault();
2571
+ dispatch({
2572
+ type: ToggleButtonKeyDownHome
2573
+ });
2574
+ },
2575
+ End: function End(event) {
2576
+ event.preventDefault();
2577
+ dispatch({
2578
+ type: ToggleButtonKeyDownEnd
2579
+ });
2580
+ },
2581
+ Escape: function Escape() {
2582
+ if (latest.current.state.isOpen) {
2583
+ dispatch({
2584
+ type: ToggleButtonKeyDownEscape
2585
+ });
2586
+ }
2587
+ },
2588
+ Enter: function Enter(event) {
2589
+ event.preventDefault();
2590
+ dispatch({
2591
+ type: latest.current.state.isOpen ? ToggleButtonKeyDownEnter : ToggleButtonClick$1
2592
+ });
2593
+ },
2594
+ PageUp: function PageUp(event) {
2595
+ if (latest.current.state.isOpen) {
2596
+ event.preventDefault();
2597
+ dispatch({
2598
+ type: ToggleButtonKeyDownPageUp
2599
+ });
2600
+ }
2601
+ },
2602
+ PageDown: function PageDown(event) {
2603
+ if (latest.current.state.isOpen) {
2604
+ event.preventDefault();
2605
+ dispatch({
2606
+ type: ToggleButtonKeyDownPageDown
2607
+ });
2608
+ }
2609
+ },
2610
+ ' ': function _(event) {
2611
+ event.preventDefault();
2612
+ var currentState = latest.current.state;
2613
+ if (!currentState.isOpen) {
2614
+ dispatch({
2615
+ type: ToggleButtonClick$1
2616
+ });
2617
+ return;
2618
+ }
2619
+ if (currentState.inputValue) {
2620
+ dispatch({
2621
+ type: ToggleButtonKeyDownCharacter,
2622
+ key: ' '
2623
+ });
2624
+ } else {
2625
+ dispatch({
2626
+ type: ToggleButtonKeyDownSpaceButton
2627
+ });
2628
+ }
2629
+ }
2630
+ };
2631
+ }, [dispatch, latest]);
2632
+
2633
+ // Action functions.
2634
+ var toggleMenu = React.useCallback(function () {
2635
+ dispatch({
2636
+ type: FunctionToggleMenu$1
2637
+ });
2638
+ }, [dispatch]);
2639
+ var closeMenu = React.useCallback(function () {
2640
+ dispatch({
2641
+ type: FunctionCloseMenu$1
2642
+ });
2643
+ }, [dispatch]);
2644
+ var openMenu = React.useCallback(function () {
2645
+ dispatch({
2646
+ type: FunctionOpenMenu$1
2647
+ });
2648
+ }, [dispatch]);
2649
+ var setHighlightedIndex = React.useCallback(function (newHighlightedIndex) {
2650
+ dispatch({
2651
+ type: FunctionSetHighlightedIndex$1,
2652
+ highlightedIndex: newHighlightedIndex
2653
+ });
2654
+ }, [dispatch]);
2655
+ var selectItem = React.useCallback(function (newSelectedItem) {
2656
+ dispatch({
2657
+ type: FunctionSelectItem$1,
2658
+ selectedItem: newSelectedItem
2659
+ });
2660
+ }, [dispatch]);
2661
+ var reset = React.useCallback(function () {
2662
+ dispatch({
2663
+ type: FunctionReset$2
2664
+ });
2665
+ }, [dispatch]);
2666
+ var setInputValue = React.useCallback(function (newInputValue) {
2667
+ dispatch({
2668
+ type: FunctionSetInputValue$1,
2669
+ inputValue: newInputValue
2670
+ });
2671
+ }, [dispatch]);
2672
+ // Getter functions.
2673
+ var getLabelProps = React.useCallback(function (_temp) {
2674
+ var _ref = _temp === void 0 ? {} : _temp,
2675
+ onClick = _ref.onClick,
2676
+ labelProps = _objectWithoutPropertiesLoose(_ref, _excluded$3);
2677
+ var labelHandleClick = function labelHandleClick() {
2678
+ var _toggleButtonRef$curr;
2679
+ (_toggleButtonRef$curr = toggleButtonRef.current) == null || _toggleButtonRef$curr.focus();
2680
+ };
2681
+ return _extends({
2682
+ id: elementIds.labelId,
2683
+ htmlFor: elementIds.toggleButtonId,
2684
+ onClick: callAllEventHandlers(onClick, labelHandleClick)
2685
+ }, labelProps);
2686
+ }, [elementIds]);
2687
+ var getMenuProps = React.useCallback(function (_temp2, _temp3) {
2688
+ var _extends2;
2689
+ var _ref2 = _temp2 === void 0 ? {} : _temp2,
2690
+ onMouseLeave = _ref2.onMouseLeave,
2691
+ _ref2$refKey = _ref2.refKey,
2692
+ refKey = _ref2$refKey === void 0 ? 'ref' : _ref2$refKey,
2693
+ ref = _ref2.ref,
2694
+ rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$3);
2695
+ var _ref3 = _temp3 === void 0 ? {} : _temp3,
2696
+ _ref3$suppressRefErro = _ref3.suppressRefError,
2697
+ suppressRefError = _ref3$suppressRefErro === void 0 ? false : _ref3$suppressRefErro;
2698
+ var menuHandleMouseLeave = function menuHandleMouseLeave() {
2699
+ dispatch({
2700
+ type: MenuMouseLeave$1
2701
+ });
2702
+ };
2703
+ setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
2704
+ return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
2705
+ menuRef.current = menuNode;
2706
+ }), _extends2.id = elementIds.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = rest && rest['aria-label'] ? undefined : "" + elementIds.labelId, _extends2.onMouseLeave = callAllEventHandlers(onMouseLeave, menuHandleMouseLeave), _extends2), rest);
2707
+ }, [dispatch, setGetterPropCallInfo, elementIds]);
2708
+ var getToggleButtonProps = React.useCallback(function (_temp4, _temp5) {
2709
+ var _extends3;
2710
+ var _ref4 = _temp4 === void 0 ? {} : _temp4,
2711
+ onBlur = _ref4.onBlur,
2712
+ onClick = _ref4.onClick;
2713
+ _ref4.onPress;
2714
+ var onKeyDown = _ref4.onKeyDown,
2715
+ _ref4$refKey = _ref4.refKey,
2716
+ refKey = _ref4$refKey === void 0 ? 'ref' : _ref4$refKey,
2717
+ ref = _ref4.ref,
2718
+ rest = _objectWithoutPropertiesLoose(_ref4, _excluded3$2);
2719
+ var _ref5 = _temp5 === void 0 ? {} : _temp5,
2720
+ _ref5$suppressRefErro = _ref5.suppressRefError,
2721
+ suppressRefError = _ref5$suppressRefErro === void 0 ? false : _ref5$suppressRefErro;
2722
+ var latestState = latest.current.state;
2723
+ var toggleButtonHandleClick = function toggleButtonHandleClick() {
2724
+ dispatch({
2725
+ type: ToggleButtonClick$1
2726
+ });
2727
+ };
2728
+ var toggleButtonHandleBlur = function toggleButtonHandleBlur() {
2729
+ if (latestState.isOpen && !mouseAndTouchTrackers.isMouseDown) {
2730
+ dispatch({
2731
+ type: ToggleButtonBlur
2732
+ });
2733
+ }
2734
+ };
2735
+ var toggleButtonHandleKeyDown = function toggleButtonHandleKeyDown(event) {
2736
+ var key = normalizeArrowKey(event);
2737
+ if (key && toggleButtonKeyDownHandlers[key]) {
2738
+ toggleButtonKeyDownHandlers[key](event);
2739
+ } else if (isAcceptedCharacterKey(key)) {
2740
+ dispatch({
2741
+ type: ToggleButtonKeyDownCharacter,
2742
+ key: key
2743
+ });
2744
+ }
2745
+ };
2746
+ var toggleProps = _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (toggleButtonNode) {
2747
+ toggleButtonRef.current = toggleButtonNode;
2748
+ }), _extends3['aria-activedescendant'] = latestState.isOpen && latestState.highlightedIndex > -1 ? elementIds.getItemId(latestState.highlightedIndex) : '', _extends3['aria-controls'] = elementIds.menuId, _extends3['aria-expanded'] = latest.current.state.isOpen, _extends3['aria-haspopup'] = 'listbox', _extends3['aria-labelledby'] = rest && rest['aria-label'] ? undefined : "" + elementIds.labelId, _extends3.id = elementIds.toggleButtonId, _extends3.role = 'combobox', _extends3.tabIndex = 0, _extends3.onBlur = callAllEventHandlers(onBlur, toggleButtonHandleBlur), _extends3), rest);
2749
+ if (!rest.disabled) {
2750
+ /* istanbul ignore if (react-native) */
2751
+ {
2752
+ toggleProps.onClick = callAllEventHandlers(onClick, toggleButtonHandleClick);
2753
+ toggleProps.onKeyDown = callAllEventHandlers(onKeyDown, toggleButtonHandleKeyDown);
2754
+ }
2755
+ }
2756
+ setGetterPropCallInfo('getToggleButtonProps', suppressRefError, refKey, toggleButtonRef);
2757
+ return toggleProps;
2758
+ }, [dispatch, elementIds, latest, mouseAndTouchTrackers, setGetterPropCallInfo, toggleButtonKeyDownHandlers]);
2759
+ var getItemProps = React.useCallback(function (_temp6) {
2760
+ var _extends4;
2761
+ var _ref6 = _temp6 === void 0 ? {} : _temp6,
2762
+ itemProp = _ref6.item,
2763
+ indexProp = _ref6.index,
2764
+ onMouseMove = _ref6.onMouseMove,
2765
+ onClick = _ref6.onClick,
2766
+ onMouseDown = _ref6.onMouseDown;
2767
+ _ref6.onPress;
2768
+ var _ref6$refKey = _ref6.refKey,
2769
+ refKey = _ref6$refKey === void 0 ? 'ref' : _ref6$refKey,
2770
+ disabledProp = _ref6.disabled,
2771
+ ref = _ref6.ref,
2772
+ rest = _objectWithoutPropertiesLoose(_ref6, _excluded4$1);
2773
+ if (disabledProp !== undefined) {
2774
+ console.warn('Passing "disabled" as an argument to getItemProps is not supported anymore. Please use the isItemDisabled prop from useSelect.');
2775
+ }
2776
+ var _latest$current = latest.current,
2777
+ latestState = _latest$current.state,
2778
+ latestProps = _latest$current.props;
2779
+ var _getItemAndIndex = getItemAndIndex(itemProp, indexProp, latestProps.items, 'Pass either item or index to getItemProps!'),
2780
+ item = _getItemAndIndex[0],
2781
+ index = _getItemAndIndex[1];
2782
+ var disabled = latestProps.isItemDisabled(item, index);
2783
+ var itemHandleMouseMove = function itemHandleMouseMove() {
2784
+ if (mouseAndTouchTrackers.isTouchEnd || index === latestState.highlightedIndex) {
2785
+ return;
2786
+ }
2787
+ shouldScrollRef.current = false;
2788
+ dispatch({
2789
+ type: ItemMouseMove$1,
2790
+ index: index,
2791
+ disabled: disabled
2792
+ });
2793
+ };
2794
+ var itemHandleClick = function itemHandleClick() {
2795
+ dispatch({
2796
+ type: ItemClick$1,
2797
+ index: index
2798
+ });
2799
+ };
2800
+ var itemHandleMouseDown = function itemHandleMouseDown(e) {
2801
+ return e.preventDefault();
2802
+ }; // keep focus on the toggle after item click select.
2803
+
2804
+ var itemProps = _extends((_extends4 = {}, _extends4[refKey] = handleRefs(ref, function (itemNode) {
2805
+ if (itemNode) {
2806
+ itemRefs.current[elementIds.getItemId(index)] = itemNode;
2807
+ }
2808
+ }), _extends4['aria-disabled'] = disabled, _extends4['aria-selected'] = item === latestState.selectedItem, _extends4.id = elementIds.getItemId(index), _extends4.role = 'option', _extends4), rest);
2809
+ if (!disabled) {
2810
+ /* istanbul ignore next (react-native) */
2811
+ {
2812
+ itemProps.onClick = callAllEventHandlers(onClick, itemHandleClick);
2813
+ }
2814
+ }
2815
+ itemProps.onMouseMove = callAllEventHandlers(onMouseMove, itemHandleMouseMove);
2816
+ itemProps.onMouseDown = callAllEventHandlers(onMouseDown, itemHandleMouseDown);
2817
+ return itemProps;
2818
+ }, [latest, elementIds, mouseAndTouchTrackers, shouldScrollRef, dispatch]);
2819
+ return {
2820
+ // prop getters.
2821
+ getToggleButtonProps: getToggleButtonProps,
2822
+ getLabelProps: getLabelProps,
2823
+ getMenuProps: getMenuProps,
2824
+ getItemProps: getItemProps,
2825
+ // actions.
2826
+ toggleMenu: toggleMenu,
2827
+ openMenu: openMenu,
2828
+ closeMenu: closeMenu,
2829
+ setHighlightedIndex: setHighlightedIndex,
2830
+ selectItem: selectItem,
2831
+ reset: reset,
2832
+ setInputValue: setInputValue,
2833
+ // state.
2834
+ highlightedIndex: highlightedIndex,
2835
+ isOpen: isOpen,
2836
+ selectedItem: selectedItem,
2837
+ inputValue: inputValue
2838
+ };
2839
+ }
2840
+
2841
+ var InputKeyDownArrowDown = process.env.NODE_ENV !== "production" ? '__input_keydown_arrow_down__' : 0;
2842
+ var InputKeyDownArrowUp = process.env.NODE_ENV !== "production" ? '__input_keydown_arrow_up__' : 1;
2843
+ var InputKeyDownEscape = process.env.NODE_ENV !== "production" ? '__input_keydown_escape__' : 2;
2844
+ var InputKeyDownHome = process.env.NODE_ENV !== "production" ? '__input_keydown_home__' : 3;
2845
+ var InputKeyDownEnd = process.env.NODE_ENV !== "production" ? '__input_keydown_end__' : 4;
2846
+ var InputKeyDownPageUp = process.env.NODE_ENV !== "production" ? '__input_keydown_page_up__' : 5;
2847
+ var InputKeyDownPageDown = process.env.NODE_ENV !== "production" ? '__input_keydown_page_down__' : 6;
2848
+ var InputKeyDownEnter = process.env.NODE_ENV !== "production" ? '__input_keydown_enter__' : 7;
2849
+ var InputChange = process.env.NODE_ENV !== "production" ? '__input_change__' : 8;
2850
+ var InputBlur = process.env.NODE_ENV !== "production" ? '__input_blur__' : 9;
2851
+ var InputClick = process.env.NODE_ENV !== "production" ? '__input_click__' : 10;
2852
+ var MenuMouseLeave = process.env.NODE_ENV !== "production" ? '__menu_mouse_leave__' : 11;
2853
+ var ItemMouseMove = process.env.NODE_ENV !== "production" ? '__item_mouse_move__' : 12;
2854
+ var ItemClick = process.env.NODE_ENV !== "production" ? '__item_click__' : 13;
2855
+ var ToggleButtonClick = process.env.NODE_ENV !== "production" ? '__togglebutton_click__' : 14;
2856
+ var FunctionToggleMenu = process.env.NODE_ENV !== "production" ? '__function_toggle_menu__' : 15;
2857
+ var FunctionOpenMenu = process.env.NODE_ENV !== "production" ? '__function_open_menu__' : 16;
2858
+ var FunctionCloseMenu = process.env.NODE_ENV !== "production" ? '__function_close_menu__' : 17;
2859
+ var FunctionSetHighlightedIndex = process.env.NODE_ENV !== "production" ? '__function_set_highlighted_index__' : 18;
2860
+ var FunctionSelectItem = process.env.NODE_ENV !== "production" ? '__function_select_item__' : 19;
2861
+ var FunctionSetInputValue = process.env.NODE_ENV !== "production" ? '__function_set_input_value__' : 20;
2862
+ var FunctionReset$1 = process.env.NODE_ENV !== "production" ? '__function_reset__' : 21;
2863
+ var ControlledPropUpdatedSelectedItem = process.env.NODE_ENV !== "production" ? '__controlled_prop_updated_selected_item__' : 22;
2864
+
2865
+ var stateChangeTypes$2 = /*#__PURE__*/Object.freeze({
2866
+ __proto__: null,
2867
+ ControlledPropUpdatedSelectedItem: ControlledPropUpdatedSelectedItem,
2868
+ FunctionCloseMenu: FunctionCloseMenu,
2869
+ FunctionOpenMenu: FunctionOpenMenu,
2870
+ FunctionReset: FunctionReset$1,
2871
+ FunctionSelectItem: FunctionSelectItem,
2872
+ FunctionSetHighlightedIndex: FunctionSetHighlightedIndex,
2873
+ FunctionSetInputValue: FunctionSetInputValue,
2874
+ FunctionToggleMenu: FunctionToggleMenu,
2875
+ InputBlur: InputBlur,
2876
+ InputChange: InputChange,
2877
+ InputClick: InputClick,
2878
+ InputKeyDownArrowDown: InputKeyDownArrowDown,
2879
+ InputKeyDownArrowUp: InputKeyDownArrowUp,
2880
+ InputKeyDownEnd: InputKeyDownEnd,
2881
+ InputKeyDownEnter: InputKeyDownEnter,
2882
+ InputKeyDownEscape: InputKeyDownEscape,
2883
+ InputKeyDownHome: InputKeyDownHome,
2884
+ InputKeyDownPageDown: InputKeyDownPageDown,
2885
+ InputKeyDownPageUp: InputKeyDownPageUp,
2886
+ ItemClick: ItemClick,
2887
+ ItemMouseMove: ItemMouseMove,
2888
+ MenuMouseLeave: MenuMouseLeave,
2889
+ ToggleButtonClick: ToggleButtonClick
2890
+ });
2891
+
2892
+ function getInitialState$2(props) {
2893
+ var initialState = getInitialState$3(props);
2894
+ var selectedItem = initialState.selectedItem;
2895
+ var inputValue = initialState.inputValue;
2896
+ if (inputValue === '' && selectedItem && props.defaultInputValue === undefined && props.initialInputValue === undefined && props.inputValue === undefined) {
2897
+ inputValue = props.itemToString(selectedItem);
2898
+ }
2899
+ return _extends({}, initialState, {
2900
+ inputValue: inputValue
2901
+ });
2902
+ }
2903
+ var propTypes$2 = _extends({}, propTypes$4, {
2904
+ items: PropTypes.array.isRequired,
2905
+ isItemDisabled: PropTypes.func,
2906
+ inputValue: PropTypes.string,
2907
+ defaultInputValue: PropTypes.string,
2908
+ initialInputValue: PropTypes.string,
2909
+ inputId: PropTypes.string,
2910
+ onInputValueChange: PropTypes.func
2911
+ });
2912
+
2913
+ /**
2914
+ * The useCombobox version of useControlledReducer, which also
2915
+ * checks if the controlled prop selectedItem changed between
2916
+ * renders. If so, it will also update inputValue with its
2917
+ * string equivalent. It uses the common useEnhancedReducer to
2918
+ * compute the rest of the state.
2919
+ *
2920
+ * @param {Function} reducer Reducer function from downshift.
2921
+ * @param {Object} props The hook props, also passed to createInitialState.
2922
+ * @param {Function} createInitialState Function that returns the initial state.
2923
+ * @param {Function} isStateEqual Function that checks if a previous state is equal to the next.
2924
+ * @returns {Array} An array with the state and an action dispatcher.
2925
+ */
2926
+ function useControlledReducer(reducer, props, createInitialState, isStateEqual) {
2927
+ var previousSelectedItemRef = React.useRef();
2928
+ var _useEnhancedReducer = useEnhancedReducer(reducer, props, createInitialState, isStateEqual),
2929
+ state = _useEnhancedReducer[0],
2930
+ dispatch = _useEnhancedReducer[1];
2931
+ var isInitialMount = useIsInitialMount();
2932
+ React.useEffect(function () {
2933
+ if (!isControlledProp(props, 'selectedItem')) {
2934
+ return;
2935
+ }
2936
+ if (!isInitialMount // on first mount we already have the proper inputValue for a initial selected item.
2937
+ ) {
2938
+ var shouldCallDispatch = props.itemToKey(props.selectedItem) !== props.itemToKey(previousSelectedItemRef.current);
2939
+ if (shouldCallDispatch) {
2940
+ dispatch({
2941
+ type: ControlledPropUpdatedSelectedItem,
2942
+ inputValue: props.itemToString(props.selectedItem)
2943
+ });
2944
+ }
2945
+ }
2946
+ previousSelectedItemRef.current = state.selectedItem === previousSelectedItemRef.current ? props.selectedItem : state.selectedItem;
2947
+ // eslint-disable-next-line react-hooks/exhaustive-deps
2948
+ }, [state.selectedItem, props.selectedItem]);
2949
+ return [getState(state, props), dispatch];
2950
+ }
2951
+ var defaultProps$1 = _extends({}, defaultProps$3, {
2952
+ isItemDisabled: function isItemDisabled() {
2953
+ return false;
2954
+ }
2955
+ });
2956
+
2957
+ /* eslint-disable complexity */
2958
+ function downshiftUseComboboxReducer(state, props, action) {
2959
+ var _props$items;
2960
+ var type = action.type,
2961
+ altKey = action.altKey;
2962
+ var changes;
2963
+ switch (type) {
2964
+ case ItemClick:
2965
+ changes = {
2966
+ isOpen: getDefaultValue$1(props, 'isOpen', defaultStateValues$1),
2967
+ highlightedIndex: getDefaultHighlightedIndex(props),
2968
+ selectedItem: props.items[action.index],
2969
+ inputValue: props.itemToString(props.items[action.index])
2970
+ };
2971
+ break;
2972
+ case InputKeyDownArrowDown:
2973
+ if (state.isOpen) {
2974
+ changes = {
2975
+ highlightedIndex: getHighlightedIndex(state.highlightedIndex, 1, props.items, props.isItemDisabled, true)
2976
+ };
2977
+ } else {
2978
+ changes = {
2979
+ highlightedIndex: altKey && state.selectedItem == null ? -1 : getHighlightedIndexOnOpen(props, state, 1),
2980
+ isOpen: props.items.length >= 0
2981
+ };
2982
+ }
2983
+ break;
2984
+ case InputKeyDownArrowUp:
2985
+ if (state.isOpen) {
2986
+ if (altKey) {
2987
+ changes = getChangesOnSelection(props, state.highlightedIndex);
2988
+ } else {
2989
+ changes = {
2990
+ highlightedIndex: getHighlightedIndex(state.highlightedIndex, -1, props.items, props.isItemDisabled, true)
2991
+ };
2992
+ }
2993
+ } else {
2994
+ changes = {
2995
+ highlightedIndex: getHighlightedIndexOnOpen(props, state, -1),
2996
+ isOpen: props.items.length >= 0
2997
+ };
2998
+ }
2999
+ break;
3000
+ case InputKeyDownEnter:
3001
+ changes = getChangesOnSelection(props, state.highlightedIndex);
3002
+ break;
3003
+ case InputKeyDownEscape:
3004
+ changes = _extends({
3005
+ isOpen: false,
3006
+ highlightedIndex: -1
3007
+ }, !state.isOpen && {
3008
+ selectedItem: null,
3009
+ inputValue: ''
3010
+ });
3011
+ break;
3012
+ case InputKeyDownPageUp:
3013
+ changes = {
3014
+ highlightedIndex: getHighlightedIndex(state.highlightedIndex, -10, props.items, props.isItemDisabled, true)
3015
+ };
3016
+ break;
3017
+ case InputKeyDownPageDown:
3018
+ changes = {
3019
+ highlightedIndex: getHighlightedIndex(state.highlightedIndex, 10, props.items, props.isItemDisabled, true)
3020
+ };
3021
+ break;
3022
+ case InputKeyDownHome:
3023
+ changes = {
3024
+ highlightedIndex: getNonDisabledIndex(0, false, props.items, props.isItemDisabled)
3025
+ };
3026
+ break;
3027
+ case InputKeyDownEnd:
3028
+ changes = {
3029
+ highlightedIndex: getNonDisabledIndex(props.items.length - 1, true, props.items, props.isItemDisabled)
3030
+ };
3031
+ break;
3032
+ case InputBlur:
3033
+ changes = _extends({
3034
+ isOpen: false,
3035
+ highlightedIndex: -1
3036
+ }, state.highlightedIndex >= 0 && ((_props$items = props.items) == null ? void 0 : _props$items.length) && action.selectItem && {
3037
+ selectedItem: props.items[state.highlightedIndex],
3038
+ inputValue: props.itemToString(props.items[state.highlightedIndex])
3039
+ });
3040
+ break;
3041
+ case InputChange:
3042
+ changes = {
3043
+ isOpen: true,
3044
+ highlightedIndex: getDefaultHighlightedIndex(props),
3045
+ inputValue: action.inputValue
3046
+ };
3047
+ break;
3048
+ case InputClick:
3049
+ changes = {
3050
+ isOpen: !state.isOpen,
3051
+ highlightedIndex: state.isOpen ? -1 : getHighlightedIndexOnOpen(props, state, 0)
3052
+ };
3053
+ break;
3054
+ case FunctionSelectItem:
3055
+ changes = {
3056
+ selectedItem: action.selectedItem,
3057
+ inputValue: props.itemToString(action.selectedItem)
3058
+ };
3059
+ break;
3060
+ case ControlledPropUpdatedSelectedItem:
3061
+ changes = {
3062
+ inputValue: action.inputValue
3063
+ };
3064
+ break;
3065
+ default:
3066
+ return downshiftCommonReducer(state, props, action, stateChangeTypes$2);
3067
+ }
3068
+ return _extends({}, state, changes);
3069
+ }
3070
+ /* eslint-enable complexity */
3071
+
3072
+ var _excluded$2 = ["onMouseLeave", "refKey", "ref"],
3073
+ _excluded2$2 = ["item", "index", "refKey", "ref", "onMouseMove", "onMouseDown", "onClick", "onPress", "disabled"],
3074
+ _excluded3$1 = ["onClick", "onPress", "refKey", "ref"],
3075
+ _excluded4 = ["onKeyDown", "onChange", "onInput", "onBlur", "onChangeText", "onClick", "refKey", "ref"];
3076
+ useCombobox.stateChangeTypes = stateChangeTypes$2;
3077
+ function useCombobox(userProps) {
3078
+ if (userProps === void 0) {
3079
+ userProps = {};
3080
+ }
3081
+ validatePropTypes$1(userProps, useCombobox, propTypes$2);
3082
+ // Props defaults and destructuring.
3083
+ var props = _extends({}, defaultProps$1, userProps);
3084
+ var items = props.items,
3085
+ scrollIntoView = props.scrollIntoView,
3086
+ environment = props.environment,
3087
+ getA11yStatusMessage = props.getA11yStatusMessage;
3088
+ // Initial state depending on controlled props.
3089
+ var _useControlledReducer = useControlledReducer(downshiftUseComboboxReducer, props, getInitialState$2, isDropdownsStateEqual),
3090
+ state = _useControlledReducer[0],
3091
+ dispatch = _useControlledReducer[1];
3092
+ var isOpen = state.isOpen,
3093
+ highlightedIndex = state.highlightedIndex,
3094
+ selectedItem = state.selectedItem,
3095
+ inputValue = state.inputValue;
3096
+
3097
+ // Element refs.
3098
+ var menuRef = React.useRef(null);
3099
+ var itemRefs = React.useRef({});
3100
+ var inputRef = React.useRef(null);
3101
+ var toggleButtonRef = React.useRef(null);
3102
+ var isInitialMount = useIsInitialMount();
3103
+
3104
+ // prevent id re-generation between renders.
3105
+ var elementIds = useElementIds$1(props);
3106
+ // used to keep track of how many items we had on previous cycle.
3107
+ var previousResultCountRef = React.useRef();
3108
+ // utility callback to get item element.
3109
+ var latest = useLatestRef({
3110
+ state: state,
3111
+ props: props
3112
+ });
3113
+ var getItemNodeFromIndex = React.useCallback(function (index) {
3114
+ return itemRefs.current[elementIds.getItemId(index)];
3115
+ }, [elementIds]);
3116
+
3117
+ // Effects.
3118
+ // Adds an a11y aria live status message if getA11yStatusMessage is passed.
3119
+ useA11yMessageStatus(getA11yStatusMessage, state, [isOpen, highlightedIndex, selectedItem, inputValue], environment);
3120
+ // Scroll on highlighted item if change comes from keyboard.
3121
+ var shouldScrollRef = useScrollIntoView({
3122
+ menuElement: menuRef.current,
3123
+ highlightedIndex: highlightedIndex,
3124
+ isOpen: isOpen,
3125
+ itemRefs: itemRefs,
3126
+ scrollIntoView: scrollIntoView,
3127
+ getItemNodeFromIndex: getItemNodeFromIndex
3128
+ });
3129
+ useControlPropsValidator({
3130
+ props: props,
3131
+ state: state
3132
+ });
3133
+ // Focus the input on first render if required.
3134
+ React.useEffect(function () {
3135
+ var focusOnOpen = getInitialValue$1(props, 'isOpen', defaultStateValues$1);
3136
+ if (focusOnOpen && inputRef.current) {
3137
+ inputRef.current.focus();
3138
+ }
3139
+ // eslint-disable-next-line react-hooks/exhaustive-deps
3140
+ }, []);
3141
+ React.useEffect(function () {
3142
+ if (!isInitialMount) {
3143
+ previousResultCountRef.current = items.length;
3144
+ }
3145
+ });
3146
+ var handleBlurInTracker = React.useCallback(function handleBlur() {
3147
+ if (latest.current.state.isOpen) {
3148
+ dispatch({
3149
+ type: InputBlur
3150
+ });
3151
+ }
3152
+ }, [dispatch, latest]);
3153
+ var downshiftRefs = React.useMemo(function () {
3154
+ return [menuRef, toggleButtonRef, inputRef];
3155
+ }, []);
3156
+ var mouseAndTouchTrackers = useMouseAndTouchTracker(environment, handleBlurInTracker, downshiftRefs);
3157
+ var setGetterPropCallInfo = useGetterPropsCalledChecker('getInputProps', 'getMenuProps');
3158
+ // Reset itemRefs on close.
3159
+ React.useEffect(function () {
3160
+ if (!isOpen) {
3161
+ itemRefs.current = {};
3162
+ }
3163
+ }, [isOpen]);
3164
+ // Reset itemRefs on close.
3165
+ React.useEffect(function () {
3166
+ var _inputRef$current;
3167
+ if (!isOpen || !(environment != null && environment.document) || !(inputRef != null && (_inputRef$current = inputRef.current) != null && _inputRef$current.focus)) {
3168
+ return;
3169
+ }
3170
+ if (environment.document.activeElement !== inputRef.current) {
3171
+ inputRef.current.focus();
3172
+ }
3173
+ }, [isOpen, environment]);
3174
+
3175
+ /* Event handler functions */
3176
+ var inputKeyDownHandlers = React.useMemo(function () {
3177
+ return {
3178
+ ArrowDown: function ArrowDown(event) {
3179
+ event.preventDefault();
3180
+ dispatch({
3181
+ type: InputKeyDownArrowDown,
3182
+ altKey: event.altKey
3183
+ });
3184
+ },
3185
+ ArrowUp: function ArrowUp(event) {
3186
+ event.preventDefault();
3187
+ dispatch({
3188
+ type: InputKeyDownArrowUp,
3189
+ altKey: event.altKey
3190
+ });
3191
+ },
3192
+ Home: function Home(event) {
3193
+ if (!latest.current.state.isOpen) {
3194
+ return;
3195
+ }
3196
+ event.preventDefault();
3197
+ dispatch({
3198
+ type: InputKeyDownHome
3199
+ });
3200
+ },
3201
+ End: function End(event) {
3202
+ if (!latest.current.state.isOpen) {
3203
+ return;
3204
+ }
3205
+ event.preventDefault();
3206
+ dispatch({
3207
+ type: InputKeyDownEnd
3208
+ });
3209
+ },
3210
+ Escape: function Escape(event) {
3211
+ var latestState = latest.current.state;
3212
+ if (latestState.isOpen || latestState.inputValue || latestState.selectedItem || latestState.highlightedIndex > -1) {
3213
+ event.preventDefault();
3214
+ dispatch({
3215
+ type: InputKeyDownEscape
3216
+ });
3217
+ }
3218
+ },
3219
+ Enter: function Enter(event) {
3220
+ var latestState = latest.current.state;
3221
+ // if closed or no highlighted index, do nothing.
3222
+ if (!latestState.isOpen || event.which === 229 // if IME composing, wait for next Enter keydown event.
3223
+ ) {
3224
+ return;
3225
+ }
3226
+ event.preventDefault();
3227
+ dispatch({
3228
+ type: InputKeyDownEnter
3229
+ });
3230
+ },
3231
+ PageUp: function PageUp(event) {
3232
+ if (latest.current.state.isOpen) {
3233
+ event.preventDefault();
3234
+ dispatch({
3235
+ type: InputKeyDownPageUp
3236
+ });
3237
+ }
3238
+ },
3239
+ PageDown: function PageDown(event) {
3240
+ if (latest.current.state.isOpen) {
3241
+ event.preventDefault();
3242
+ dispatch({
3243
+ type: InputKeyDownPageDown
3244
+ });
3245
+ }
3246
+ }
3247
+ };
3248
+ }, [dispatch, latest]);
3249
+
3250
+ // Getter props.
3251
+ var getLabelProps = React.useCallback(function (labelProps) {
3252
+ return _extends({
3253
+ id: elementIds.labelId,
3254
+ htmlFor: elementIds.inputId
3255
+ }, labelProps);
3256
+ }, [elementIds]);
3257
+ var getMenuProps = React.useCallback(function (_temp, _temp2) {
3258
+ var _extends2;
3259
+ var _ref = _temp === void 0 ? {} : _temp,
3260
+ onMouseLeave = _ref.onMouseLeave,
3261
+ _ref$refKey = _ref.refKey,
3262
+ refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
3263
+ ref = _ref.ref,
3264
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded$2);
3265
+ var _ref2 = _temp2 === void 0 ? {} : _temp2,
3266
+ _ref2$suppressRefErro = _ref2.suppressRefError,
3267
+ suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
3268
+ setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
3269
+ return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
3270
+ menuRef.current = menuNode;
3271
+ }), _extends2.id = elementIds.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = rest && rest['aria-label'] ? undefined : "" + elementIds.labelId, _extends2.onMouseLeave = callAllEventHandlers(onMouseLeave, function () {
3272
+ dispatch({
3273
+ type: MenuMouseLeave
3274
+ });
3275
+ }), _extends2), rest);
3276
+ }, [dispatch, setGetterPropCallInfo, elementIds]);
3277
+ var getItemProps = React.useCallback(function (_temp3) {
3278
+ var _extends3, _ref4;
3279
+ var _ref3 = _temp3 === void 0 ? {} : _temp3,
3280
+ itemProp = _ref3.item,
3281
+ indexProp = _ref3.index,
3282
+ _ref3$refKey = _ref3.refKey,
3283
+ refKey = _ref3$refKey === void 0 ? 'ref' : _ref3$refKey,
3284
+ ref = _ref3.ref,
3285
+ onMouseMove = _ref3.onMouseMove,
3286
+ onMouseDown = _ref3.onMouseDown,
3287
+ onClick = _ref3.onClick;
3288
+ _ref3.onPress;
3289
+ var disabledProp = _ref3.disabled,
3290
+ rest = _objectWithoutPropertiesLoose(_ref3, _excluded2$2);
3291
+ if (disabledProp !== undefined) {
3292
+ console.warn('Passing "disabled" as an argument to getItemProps is not supported anymore. Please use the isItemDisabled prop from useCombobox.');
3293
+ }
3294
+ var _latest$current = latest.current,
3295
+ latestProps = _latest$current.props,
3296
+ latestState = _latest$current.state;
3297
+ var _getItemAndIndex = getItemAndIndex(itemProp, indexProp, latestProps.items, 'Pass either item or index to getItemProps!'),
3298
+ item = _getItemAndIndex[0],
3299
+ index = _getItemAndIndex[1];
3300
+ var disabled = latestProps.isItemDisabled(item, index);
3301
+ var onSelectKey = 'onClick';
3302
+ var customClickHandler = onClick;
3303
+ var itemHandleMouseMove = function itemHandleMouseMove() {
3304
+ if (mouseAndTouchTrackers.isTouchEnd || index === latestState.highlightedIndex) {
3305
+ return;
3306
+ }
3307
+ shouldScrollRef.current = false;
3308
+ dispatch({
3309
+ type: ItemMouseMove,
3310
+ index: index,
3311
+ disabled: disabled
3312
+ });
3313
+ };
3314
+ var itemHandleClick = function itemHandleClick() {
3315
+ dispatch({
3316
+ type: ItemClick,
3317
+ index: index
3318
+ });
3319
+ };
3320
+ var itemHandleMouseDown = function itemHandleMouseDown(e) {
3321
+ return e.preventDefault();
3322
+ }; // keep focus on the input after item click select.
3323
+
3324
+ return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (itemNode) {
3325
+ if (itemNode) {
3326
+ itemRefs.current[elementIds.getItemId(index)] = itemNode;
3327
+ }
3328
+ }), _extends3['aria-disabled'] = disabled, _extends3['aria-selected'] = index === latestState.highlightedIndex, _extends3.id = elementIds.getItemId(index), _extends3.role = 'option', _extends3), !disabled && (_ref4 = {}, _ref4[onSelectKey] = callAllEventHandlers(customClickHandler, itemHandleClick), _ref4), {
3329
+ onMouseMove: callAllEventHandlers(onMouseMove, itemHandleMouseMove),
3330
+ onMouseDown: callAllEventHandlers(onMouseDown, itemHandleMouseDown)
3331
+ }, rest);
3332
+ }, [dispatch, elementIds, latest, mouseAndTouchTrackers, shouldScrollRef]);
3333
+ var getToggleButtonProps = React.useCallback(function (_temp4) {
3334
+ var _extends4;
3335
+ var _ref5 = _temp4 === void 0 ? {} : _temp4,
3336
+ onClick = _ref5.onClick;
3337
+ _ref5.onPress;
3338
+ var _ref5$refKey = _ref5.refKey,
3339
+ refKey = _ref5$refKey === void 0 ? 'ref' : _ref5$refKey,
3340
+ ref = _ref5.ref,
3341
+ rest = _objectWithoutPropertiesLoose(_ref5, _excluded3$1);
3342
+ var latestState = latest.current.state;
3343
+ var toggleButtonHandleClick = function toggleButtonHandleClick() {
3344
+ dispatch({
3345
+ type: ToggleButtonClick
3346
+ });
3347
+ };
3348
+ return _extends((_extends4 = {}, _extends4[refKey] = handleRefs(ref, function (toggleButtonNode) {
3349
+ toggleButtonRef.current = toggleButtonNode;
3350
+ }), _extends4['aria-controls'] = elementIds.menuId, _extends4['aria-expanded'] = latestState.isOpen, _extends4.id = elementIds.toggleButtonId, _extends4.tabIndex = -1, _extends4), !rest.disabled && _extends({}, {
3351
+ onClick: callAllEventHandlers(onClick, toggleButtonHandleClick)
3352
+ }), rest);
3353
+ }, [dispatch, latest, elementIds]);
3354
+ var getInputProps = React.useCallback(function (_temp5, _temp6) {
3355
+ var _extends5;
3356
+ var _ref6 = _temp5 === void 0 ? {} : _temp5,
3357
+ onKeyDown = _ref6.onKeyDown,
3358
+ onChange = _ref6.onChange,
3359
+ onInput = _ref6.onInput,
3360
+ onBlur = _ref6.onBlur;
3361
+ _ref6.onChangeText;
3362
+ var onClick = _ref6.onClick,
3363
+ _ref6$refKey = _ref6.refKey,
3364
+ refKey = _ref6$refKey === void 0 ? 'ref' : _ref6$refKey,
3365
+ ref = _ref6.ref,
3366
+ rest = _objectWithoutPropertiesLoose(_ref6, _excluded4);
3367
+ var _ref7 = _temp6 === void 0 ? {} : _temp6,
3368
+ _ref7$suppressRefErro = _ref7.suppressRefError,
3369
+ suppressRefError = _ref7$suppressRefErro === void 0 ? false : _ref7$suppressRefErro;
3370
+ setGetterPropCallInfo('getInputProps', suppressRefError, refKey, inputRef);
3371
+ var latestState = latest.current.state;
3372
+ var inputHandleKeyDown = function inputHandleKeyDown(event) {
3373
+ var key = normalizeArrowKey(event);
3374
+ if (key && inputKeyDownHandlers[key]) {
3375
+ inputKeyDownHandlers[key](event);
3376
+ }
3377
+ };
3378
+ var inputHandleChange = function inputHandleChange(event) {
3379
+ dispatch({
3380
+ type: InputChange,
3381
+ inputValue: event.target.value
3382
+ });
3383
+ };
3384
+ var inputHandleBlur = function inputHandleBlur(event) {
3385
+ /* istanbul ignore else */
3386
+ if (environment != null && environment.document && latestState.isOpen && !mouseAndTouchTrackers.isMouseDown) {
3387
+ var isBlurByTabChange = event.relatedTarget === null && environment.document.activeElement !== environment.document.body;
3388
+ dispatch({
3389
+ type: InputBlur,
3390
+ selectItem: !isBlurByTabChange
3391
+ });
3392
+ }
3393
+ };
3394
+ var inputHandleClick = function inputHandleClick() {
3395
+ dispatch({
3396
+ type: InputClick
3397
+ });
3398
+ };
3399
+
3400
+ /* istanbul ignore next (preact) */
3401
+ var onChangeKey = 'onInput' ;
3402
+ var eventHandlers = {};
3403
+ if (!rest.disabled) {
3404
+ var _eventHandlers;
3405
+ eventHandlers = (_eventHandlers = {}, _eventHandlers[onChangeKey] = callAllEventHandlers(onChange, onInput, inputHandleChange), _eventHandlers.onKeyDown = callAllEventHandlers(onKeyDown, inputHandleKeyDown), _eventHandlers.onBlur = callAllEventHandlers(onBlur, inputHandleBlur), _eventHandlers.onClick = callAllEventHandlers(onClick, inputHandleClick), _eventHandlers);
3406
+ }
3407
+ return _extends((_extends5 = {}, _extends5[refKey] = handleRefs(ref, function (inputNode) {
3408
+ inputRef.current = inputNode;
3409
+ }), _extends5['aria-activedescendant'] = latestState.isOpen && latestState.highlightedIndex > -1 ? elementIds.getItemId(latestState.highlightedIndex) : '', _extends5['aria-autocomplete'] = 'list', _extends5['aria-controls'] = elementIds.menuId, _extends5['aria-expanded'] = latestState.isOpen, _extends5['aria-labelledby'] = rest && rest['aria-label'] ? undefined : elementIds.labelId, _extends5.autoComplete = 'off', _extends5.id = elementIds.inputId, _extends5.role = 'combobox', _extends5.value = latestState.inputValue, _extends5), eventHandlers, rest);
3410
+ }, [dispatch, elementIds, environment, inputKeyDownHandlers, latest, mouseAndTouchTrackers, setGetterPropCallInfo]);
3411
+
3412
+ // returns
3413
+ var toggleMenu = React.useCallback(function () {
3414
+ dispatch({
3415
+ type: FunctionToggleMenu
3416
+ });
3417
+ }, [dispatch]);
3418
+ var closeMenu = React.useCallback(function () {
3419
+ dispatch({
3420
+ type: FunctionCloseMenu
3421
+ });
3422
+ }, [dispatch]);
3423
+ var openMenu = React.useCallback(function () {
3424
+ dispatch({
3425
+ type: FunctionOpenMenu
3426
+ });
3427
+ }, [dispatch]);
3428
+ var setHighlightedIndex = React.useCallback(function (newHighlightedIndex) {
3429
+ dispatch({
3430
+ type: FunctionSetHighlightedIndex,
3431
+ highlightedIndex: newHighlightedIndex
3432
+ });
3433
+ }, [dispatch]);
3434
+ var selectItem = React.useCallback(function (newSelectedItem) {
3435
+ dispatch({
3436
+ type: FunctionSelectItem,
3437
+ selectedItem: newSelectedItem
3438
+ });
3439
+ }, [dispatch]);
3440
+ var setInputValue = React.useCallback(function (newInputValue) {
3441
+ dispatch({
3442
+ type: FunctionSetInputValue,
3443
+ inputValue: newInputValue
3444
+ });
3445
+ }, [dispatch]);
3446
+ var reset = React.useCallback(function () {
3447
+ dispatch({
3448
+ type: FunctionReset$1
3449
+ });
3450
+ }, [dispatch]);
3451
+ return {
3452
+ // prop getters.
3453
+ getItemProps: getItemProps,
3454
+ getLabelProps: getLabelProps,
3455
+ getMenuProps: getMenuProps,
3456
+ getInputProps: getInputProps,
3457
+ getToggleButtonProps: getToggleButtonProps,
3458
+ // actions.
3459
+ toggleMenu: toggleMenu,
3460
+ openMenu: openMenu,
3461
+ closeMenu: closeMenu,
3462
+ setHighlightedIndex: setHighlightedIndex,
3463
+ setInputValue: setInputValue,
3464
+ selectItem: selectItem,
3465
+ reset: reset,
3466
+ // state.
3467
+ highlightedIndex: highlightedIndex,
3468
+ isOpen: isOpen,
3469
+ selectedItem: selectedItem,
3470
+ inputValue: inputValue
3471
+ };
3472
+ }
3473
+
3474
+ var defaultStateValues = {
3475
+ activeIndex: -1,
3476
+ selectedItems: []
3477
+ };
3478
+
3479
+ /**
3480
+ * Returns the initial value for a state key in the following order:
3481
+ * 1. controlled prop, 2. initial prop, 3. default prop, 4. default
3482
+ * value from Downshift.
3483
+ *
3484
+ * @param {Object} props Props passed to the hook.
3485
+ * @param {string} propKey Props key to generate the value for.
3486
+ * @returns {any} The initial value for that prop.
3487
+ */
3488
+ function getInitialValue(props, propKey) {
3489
+ return getInitialValue$1(props, propKey, defaultStateValues);
3490
+ }
3491
+
3492
+ /**
3493
+ * Returns the default value for a state key in the following order:
3494
+ * 1. controlled prop, 2. default prop, 3. default value from Downshift.
3495
+ *
3496
+ * @param {Object} props Props passed to the hook.
3497
+ * @param {string} propKey Props key to generate the value for.
3498
+ * @returns {any} The initial value for that prop.
3499
+ */
3500
+ function getDefaultValue(props, propKey) {
3501
+ return getDefaultValue$1(props, propKey, defaultStateValues);
3502
+ }
3503
+
3504
+ /**
3505
+ * Gets the initial state based on the provided props. It uses initial, default
3506
+ * and controlled props related to state in order to compute the initial value.
3507
+ *
3508
+ * @param {Object} props Props passed to the hook.
3509
+ * @returns {Object} The initial state.
3510
+ */
3511
+ function getInitialState$1(props) {
3512
+ var activeIndex = getInitialValue(props, 'activeIndex');
3513
+ var selectedItems = getInitialValue(props, 'selectedItems');
3514
+ return {
3515
+ activeIndex: activeIndex,
3516
+ selectedItems: selectedItems
3517
+ };
3518
+ }
3519
+
3520
+ /**
3521
+ * Returns true if dropdown keydown operation is permitted. Should not be
3522
+ * allowed on keydown with modifier keys (ctrl, alt, shift, meta), on
3523
+ * input element with text content that is either highlighted or selection
3524
+ * cursor is not at the starting position.
3525
+ *
3526
+ * @param {KeyboardEvent} event The event from keydown.
3527
+ * @returns {boolean} Whether the operation is allowed.
3528
+ */
3529
+ function isKeyDownOperationPermitted(event) {
3530
+ if (event.shiftKey || event.metaKey || event.ctrlKey || event.altKey) {
3531
+ return false;
3532
+ }
3533
+ var element = event.target;
3534
+ if (element instanceof HTMLInputElement &&
3535
+ // if element is a text input
3536
+ element.value !== '' && (
3537
+ // and we have text in it
3538
+ // and cursor is either not at the start or is currently highlighting text.
3539
+ element.selectionStart !== 0 || element.selectionEnd !== 0)) {
3540
+ return false;
3541
+ }
3542
+ return true;
3543
+ }
3544
+
3545
+ /**
3546
+ * Check if a state is equal for taglist, by comparing active index and selected items.
3547
+ * Used by useSelect and useCombobox.
3548
+ *
3549
+ * @param {Object} prevState
3550
+ * @param {Object} newState
3551
+ * @returns {boolean} Wheather the states are deeply equal.
3552
+ */
3553
+ function isStateEqual$1(prevState, newState) {
3554
+ return prevState.selectedItems === newState.selectedItems && prevState.activeIndex === newState.activeIndex;
3555
+ }
3556
+ var propTypes$1 = {
3557
+ stateReducer: propTypes$4.stateReducer,
3558
+ itemToKey: propTypes$4.itemToKey,
3559
+ environment: propTypes$4.environment,
3560
+ selectedItems: PropTypes.array,
3561
+ initialSelectedItems: PropTypes.array,
3562
+ defaultSelectedItems: PropTypes.array,
3563
+ getA11yStatusMessage: PropTypes.func,
3564
+ activeIndex: PropTypes.number,
3565
+ initialActiveIndex: PropTypes.number,
3566
+ defaultActiveIndex: PropTypes.number,
3567
+ onActiveIndexChange: PropTypes.func,
3568
+ onSelectedItemsChange: PropTypes.func,
3569
+ keyNavigationNext: PropTypes.string,
3570
+ keyNavigationPrevious: PropTypes.string
3571
+ };
3572
+ var defaultProps = {
3573
+ itemToKey: defaultProps$3.itemToKey,
3574
+ stateReducer: defaultProps$3.stateReducer,
3575
+ environment: defaultProps$3.environment,
3576
+ keyNavigationNext: 'ArrowRight',
3577
+ keyNavigationPrevious: 'ArrowLeft'
3578
+ };
3579
+
3580
+ // eslint-disable-next-line import/no-mutable-exports
3581
+ var validatePropTypes = noop;
3582
+ /* istanbul ignore next */
3583
+ if (process.env.NODE_ENV !== 'production') {
3584
+ validatePropTypes = function validatePropTypes(options, caller) {
3585
+ PropTypes.checkPropTypes(propTypes$1, options, 'prop', caller.name);
3586
+ };
3587
+ }
3588
+
3589
+ var SelectedItemClick = process.env.NODE_ENV !== "production" ? '__selected_item_click__' : 0;
3590
+ var SelectedItemKeyDownDelete = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_delete__' : 1;
3591
+ var SelectedItemKeyDownBackspace = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_backspace__' : 2;
3592
+ var SelectedItemKeyDownNavigationNext = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_navigation_next__' : 3;
3593
+ var SelectedItemKeyDownNavigationPrevious = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_navigation_previous__' : 4;
3594
+ var DropdownKeyDownNavigationPrevious = process.env.NODE_ENV !== "production" ? '__dropdown_keydown_navigation_previous__' : 5;
3595
+ var DropdownKeyDownBackspace = process.env.NODE_ENV !== "production" ? '__dropdown_keydown_backspace__' : 6;
3596
+ var DropdownClick = process.env.NODE_ENV !== "production" ? '__dropdown_click__' : 7;
3597
+ var FunctionAddSelectedItem = process.env.NODE_ENV !== "production" ? '__function_add_selected_item__' : 8;
3598
+ var FunctionRemoveSelectedItem = process.env.NODE_ENV !== "production" ? '__function_remove_selected_item__' : 9;
3599
+ var FunctionSetSelectedItems = process.env.NODE_ENV !== "production" ? '__function_set_selected_items__' : 10;
3600
+ var FunctionSetActiveIndex = process.env.NODE_ENV !== "production" ? '__function_set_active_index__' : 11;
3601
+ var FunctionReset = process.env.NODE_ENV !== "production" ? '__function_reset__' : 12;
3602
+
3603
+ var stateChangeTypes$1 = /*#__PURE__*/Object.freeze({
3604
+ __proto__: null,
3605
+ DropdownClick: DropdownClick,
3606
+ DropdownKeyDownBackspace: DropdownKeyDownBackspace,
3607
+ DropdownKeyDownNavigationPrevious: DropdownKeyDownNavigationPrevious,
3608
+ FunctionAddSelectedItem: FunctionAddSelectedItem,
3609
+ FunctionRemoveSelectedItem: FunctionRemoveSelectedItem,
3610
+ FunctionReset: FunctionReset,
3611
+ FunctionSetActiveIndex: FunctionSetActiveIndex,
3612
+ FunctionSetSelectedItems: FunctionSetSelectedItems,
3613
+ SelectedItemClick: SelectedItemClick,
3614
+ SelectedItemKeyDownBackspace: SelectedItemKeyDownBackspace,
3615
+ SelectedItemKeyDownDelete: SelectedItemKeyDownDelete,
3616
+ SelectedItemKeyDownNavigationNext: SelectedItemKeyDownNavigationNext,
3617
+ SelectedItemKeyDownNavigationPrevious: SelectedItemKeyDownNavigationPrevious
3618
+ });
3619
+
3620
+ /* eslint-disable complexity */
3621
+ function downshiftMultipleSelectionReducer(state, props, action) {
3622
+ var type = action.type,
3623
+ index = action.index,
3624
+ selectedItem = action.selectedItem;
3625
+ var activeIndex = state.activeIndex,
3626
+ selectedItems = state.selectedItems;
3627
+ var changes;
3628
+ switch (type) {
3629
+ case SelectedItemClick:
3630
+ changes = {
3631
+ activeIndex: index
3632
+ };
3633
+ break;
3634
+ case SelectedItemKeyDownNavigationPrevious:
3635
+ changes = {
3636
+ activeIndex: activeIndex - 1 < 0 ? 0 : activeIndex - 1
3637
+ };
3638
+ break;
3639
+ case SelectedItemKeyDownNavigationNext:
3640
+ changes = {
3641
+ activeIndex: activeIndex + 1 >= selectedItems.length ? -1 : activeIndex + 1
3642
+ };
3643
+ break;
3644
+ case SelectedItemKeyDownBackspace:
3645
+ case SelectedItemKeyDownDelete:
3646
+ {
3647
+ if (activeIndex < 0) {
3648
+ break;
3649
+ }
3650
+ var newActiveIndex = activeIndex;
3651
+ if (selectedItems.length === 1) {
3652
+ newActiveIndex = -1;
3653
+ } else if (activeIndex === selectedItems.length - 1) {
3654
+ newActiveIndex = selectedItems.length - 2;
3655
+ }
3656
+ changes = _extends({
3657
+ selectedItems: [].concat(selectedItems.slice(0, activeIndex), selectedItems.slice(activeIndex + 1))
3658
+ }, {
3659
+ activeIndex: newActiveIndex
3660
+ });
3661
+ break;
3662
+ }
3663
+ case DropdownKeyDownNavigationPrevious:
3664
+ changes = {
3665
+ activeIndex: selectedItems.length - 1
3666
+ };
3667
+ break;
3668
+ case DropdownKeyDownBackspace:
3669
+ changes = {
3670
+ selectedItems: selectedItems.slice(0, selectedItems.length - 1)
3671
+ };
3672
+ break;
3673
+ case FunctionAddSelectedItem:
3674
+ changes = {
3675
+ selectedItems: [].concat(selectedItems, [selectedItem])
3676
+ };
3677
+ break;
3678
+ case DropdownClick:
3679
+ changes = {
3680
+ activeIndex: -1
3681
+ };
3682
+ break;
3683
+ case FunctionRemoveSelectedItem:
3684
+ {
3685
+ var _newActiveIndex = activeIndex;
3686
+ var selectedItemIndex = selectedItems.findIndex(function (item) {
3687
+ return props.itemToKey(item) === props.itemToKey(selectedItem);
3688
+ });
3689
+ if (selectedItemIndex < 0) {
3690
+ break;
3691
+ }
3692
+ if (selectedItems.length === 1) {
3693
+ _newActiveIndex = -1;
3694
+ } else if (selectedItemIndex === selectedItems.length - 1) {
3695
+ _newActiveIndex = selectedItems.length - 2;
3696
+ }
3697
+ changes = {
3698
+ selectedItems: [].concat(selectedItems.slice(0, selectedItemIndex), selectedItems.slice(selectedItemIndex + 1)),
3699
+ activeIndex: _newActiveIndex
3700
+ };
3701
+ break;
3702
+ }
3703
+ case FunctionSetSelectedItems:
3704
+ {
3705
+ var newSelectedItems = action.selectedItems;
3706
+ changes = {
3707
+ selectedItems: newSelectedItems
3708
+ };
3709
+ break;
3710
+ }
3711
+ case FunctionSetActiveIndex:
3712
+ {
3713
+ var _newActiveIndex2 = action.activeIndex;
3714
+ changes = {
3715
+ activeIndex: _newActiveIndex2
3716
+ };
3717
+ break;
3718
+ }
3719
+ case FunctionReset:
3720
+ changes = {
3721
+ activeIndex: getDefaultValue(props, 'activeIndex'),
3722
+ selectedItems: getDefaultValue(props, 'selectedItems')
3723
+ };
3724
+ break;
3725
+ default:
3726
+ throw new Error('Reducer called without proper action type.');
3727
+ }
3728
+ return _extends({}, state, changes);
3729
+ }
3730
+
3731
+ var _excluded$1 = ["refKey", "ref", "onClick", "onKeyDown", "selectedItem", "index"],
3732
+ _excluded2$1 = ["refKey", "ref", "onKeyDown", "onClick", "preventKeyAction"];
3733
+ useMultipleSelection.stateChangeTypes = stateChangeTypes$1;
3734
+ function useMultipleSelection(userProps) {
3735
+ if (userProps === void 0) {
3736
+ userProps = {};
3737
+ }
3738
+ validatePropTypes(userProps, useMultipleSelection);
3739
+ // Props defaults and destructuring.
3740
+ var props = _extends({}, defaultProps, userProps);
3741
+ var getA11yStatusMessage = props.getA11yStatusMessage,
3742
+ environment = props.environment,
3743
+ keyNavigationNext = props.keyNavigationNext,
3744
+ keyNavigationPrevious = props.keyNavigationPrevious;
3745
+
3746
+ // Reducer init.
3747
+ var _useControlledReducer = useControlledReducer$1(downshiftMultipleSelectionReducer, props, getInitialState$1, isStateEqual$1),
3748
+ state = _useControlledReducer[0],
3749
+ dispatch = _useControlledReducer[1];
3750
+ var activeIndex = state.activeIndex,
3751
+ selectedItems = state.selectedItems;
3752
+
3753
+ // Refs.
3754
+ var isInitialMount = useIsInitialMount();
3755
+ var dropdownRef = React.useRef(null);
3756
+ var selectedItemRefs = React.useRef();
3757
+ selectedItemRefs.current = [];
3758
+ var latest = useLatestRef({
3759
+ state: state,
3760
+ props: props
3761
+ });
3762
+
3763
+ // Effects.
3764
+ // Adds an a11y aria live status message if getA11yStatusMessage is passed.
3765
+ useA11yMessageStatus(getA11yStatusMessage, state, [activeIndex, selectedItems], environment);
3766
+ // Sets focus on active item.
3767
+ React.useEffect(function () {
3768
+ if (isInitialMount) {
3769
+ return;
3770
+ }
3771
+ if (activeIndex === -1 && dropdownRef.current) {
3772
+ dropdownRef.current.focus();
3773
+ } else if (selectedItemRefs.current[activeIndex]) {
3774
+ selectedItemRefs.current[activeIndex].focus();
3775
+ }
3776
+ // eslint-disable-next-line react-hooks/exhaustive-deps
3777
+ }, [activeIndex]);
3778
+ useControlPropsValidator({
3779
+ props: props,
3780
+ state: state
3781
+ });
3782
+ var setGetterPropCallInfo = useGetterPropsCalledChecker('getDropdownProps');
3783
+
3784
+ // Event handler functions.
3785
+ var selectedItemKeyDownHandlers = React.useMemo(function () {
3786
+ var _ref;
3787
+ return _ref = {}, _ref[keyNavigationPrevious] = function () {
3788
+ dispatch({
3789
+ type: SelectedItemKeyDownNavigationPrevious
3790
+ });
3791
+ }, _ref[keyNavigationNext] = function () {
3792
+ dispatch({
3793
+ type: SelectedItemKeyDownNavigationNext
3794
+ });
3795
+ }, _ref.Delete = function Delete() {
3796
+ dispatch({
3797
+ type: SelectedItemKeyDownDelete
3798
+ });
3799
+ }, _ref.Backspace = function Backspace() {
3800
+ dispatch({
3801
+ type: SelectedItemKeyDownBackspace
3802
+ });
3803
+ }, _ref;
3804
+ }, [dispatch, keyNavigationNext, keyNavigationPrevious]);
3805
+ var dropdownKeyDownHandlers = React.useMemo(function () {
3806
+ var _ref2;
3807
+ return _ref2 = {}, _ref2[keyNavigationPrevious] = function (event) {
3808
+ if (isKeyDownOperationPermitted(event)) {
3809
+ dispatch({
3810
+ type: DropdownKeyDownNavigationPrevious
3811
+ });
3812
+ }
3813
+ }, _ref2.Backspace = function Backspace(event) {
3814
+ if (isKeyDownOperationPermitted(event)) {
3815
+ dispatch({
3816
+ type: DropdownKeyDownBackspace
3817
+ });
3818
+ }
3819
+ }, _ref2;
3820
+ }, [dispatch, keyNavigationPrevious]);
3821
+
3822
+ // Getter props.
3823
+ var getSelectedItemProps = React.useCallback(function (_temp) {
3824
+ var _extends2;
3825
+ var _ref3 = _temp === void 0 ? {} : _temp,
3826
+ _ref3$refKey = _ref3.refKey,
3827
+ refKey = _ref3$refKey === void 0 ? 'ref' : _ref3$refKey,
3828
+ ref = _ref3.ref,
3829
+ onClick = _ref3.onClick,
3830
+ onKeyDown = _ref3.onKeyDown,
3831
+ selectedItemProp = _ref3.selectedItem,
3832
+ indexProp = _ref3.index,
3833
+ rest = _objectWithoutPropertiesLoose(_ref3, _excluded$1);
3834
+ var latestState = latest.current.state;
3835
+ var _getItemAndIndex = getItemAndIndex(selectedItemProp, indexProp, latestState.selectedItems, 'Pass either item or index to getSelectedItemProps!'),
3836
+ index = _getItemAndIndex[1];
3837
+ var isFocusable = index > -1 && index === latestState.activeIndex;
3838
+ var selectedItemHandleClick = function selectedItemHandleClick() {
3839
+ dispatch({
3840
+ type: SelectedItemClick,
3841
+ index: index
3842
+ });
3843
+ };
3844
+ var selectedItemHandleKeyDown = function selectedItemHandleKeyDown(event) {
3845
+ var key = normalizeArrowKey(event);
3846
+ if (key && selectedItemKeyDownHandlers[key]) {
3847
+ selectedItemKeyDownHandlers[key](event);
3848
+ }
3849
+ };
3850
+ return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (selectedItemNode) {
3851
+ if (selectedItemNode) {
3852
+ selectedItemRefs.current.push(selectedItemNode);
3853
+ }
3854
+ }), _extends2.tabIndex = isFocusable ? 0 : -1, _extends2.onClick = callAllEventHandlers(onClick, selectedItemHandleClick), _extends2.onKeyDown = callAllEventHandlers(onKeyDown, selectedItemHandleKeyDown), _extends2), rest);
3855
+ }, [dispatch, latest, selectedItemKeyDownHandlers]);
3856
+ var getDropdownProps = React.useCallback(function (_temp2, _temp3) {
3857
+ var _extends3;
3858
+ var _ref4 = _temp2 === void 0 ? {} : _temp2,
3859
+ _ref4$refKey = _ref4.refKey,
3860
+ refKey = _ref4$refKey === void 0 ? 'ref' : _ref4$refKey,
3861
+ ref = _ref4.ref,
3862
+ onKeyDown = _ref4.onKeyDown,
3863
+ onClick = _ref4.onClick,
3864
+ _ref4$preventKeyActio = _ref4.preventKeyAction,
3865
+ preventKeyAction = _ref4$preventKeyActio === void 0 ? false : _ref4$preventKeyActio,
3866
+ rest = _objectWithoutPropertiesLoose(_ref4, _excluded2$1);
3867
+ var _ref5 = _temp3 === void 0 ? {} : _temp3,
3868
+ _ref5$suppressRefErro = _ref5.suppressRefError,
3869
+ suppressRefError = _ref5$suppressRefErro === void 0 ? false : _ref5$suppressRefErro;
3870
+ setGetterPropCallInfo('getDropdownProps', suppressRefError, refKey, dropdownRef);
3871
+ var dropdownHandleKeyDown = function dropdownHandleKeyDown(event) {
3872
+ var key = normalizeArrowKey(event);
3873
+ if (key && dropdownKeyDownHandlers[key]) {
3874
+ dropdownKeyDownHandlers[key](event);
3875
+ }
3876
+ };
3877
+ var dropdownHandleClick = function dropdownHandleClick() {
3878
+ dispatch({
3879
+ type: DropdownClick
3880
+ });
3881
+ };
3882
+ return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (dropdownNode) {
3883
+ if (dropdownNode) {
3884
+ dropdownRef.current = dropdownNode;
3885
+ }
3886
+ }), _extends3), !preventKeyAction && {
3887
+ onKeyDown: callAllEventHandlers(onKeyDown, dropdownHandleKeyDown),
3888
+ onClick: callAllEventHandlers(onClick, dropdownHandleClick)
3889
+ }, rest);
3890
+ }, [dispatch, dropdownKeyDownHandlers, setGetterPropCallInfo]);
3891
+
3892
+ // returns
3893
+ var addSelectedItem = React.useCallback(function (selectedItem) {
3894
+ dispatch({
3895
+ type: FunctionAddSelectedItem,
3896
+ selectedItem: selectedItem
3897
+ });
3898
+ }, [dispatch]);
3899
+ var removeSelectedItem = React.useCallback(function (selectedItem) {
3900
+ dispatch({
3901
+ type: FunctionRemoveSelectedItem,
3902
+ selectedItem: selectedItem
3903
+ });
3904
+ }, [dispatch]);
3905
+ var setSelectedItems = React.useCallback(function (newSelectedItems) {
3906
+ dispatch({
3907
+ type: FunctionSetSelectedItems,
3908
+ selectedItems: newSelectedItems
3909
+ });
3910
+ }, [dispatch]);
3911
+ var setActiveIndex = React.useCallback(function (newActiveIndex) {
3912
+ dispatch({
3913
+ type: FunctionSetActiveIndex,
3914
+ activeIndex: newActiveIndex
3915
+ });
3916
+ }, [dispatch]);
3917
+ var reset = React.useCallback(function () {
3918
+ dispatch({
3919
+ type: FunctionReset
3920
+ });
3921
+ }, [dispatch]);
3922
+ return {
3923
+ getSelectedItemProps: getSelectedItemProps,
3924
+ getDropdownProps: getDropdownProps,
3925
+ addSelectedItem: addSelectedItem,
3926
+ removeSelectedItem: removeSelectedItem,
3927
+ setSelectedItems: setSelectedItems,
3928
+ setActiveIndex: setActiveIndex,
3929
+ reset: reset,
3930
+ selectedItems: selectedItems,
3931
+ activeIndex: activeIndex
3932
+ };
3933
+ }
3934
+
3935
+ var TagClick = process.env.NODE_ENV !== "production" ? '__tag_click__' : 0;
3936
+ var TagGroupKeyDownArrowLeft = process.env.NODE_ENV !== "production" ? '__taggroup_keydown_arrowleft__' : 1;
3937
+ var TagGroupKeyDownArrowRight = process.env.NODE_ENV !== "production" ? '__taggroup_keydown_arrowright__' : 2;
3938
+ var TagGroupKeyDownDelete = process.env.NODE_ENV !== "production" ? '__taggroup_keydown_delete__' : 3;
3939
+ var TagGroupKeyDownBackspace = process.env.NODE_ENV !== "production" ? '__taggroup_keydown_backspace__' : 4;
3940
+ var TagRemoveClick = process.env.NODE_ENV !== "production" ? '__tagremove_click__' : 5;
3941
+ var FunctionAddItem = process.env.NODE_ENV !== "production" ? '__function_add_item__' : 6;
3942
+
3943
+ var stateChangeTypes = /*#__PURE__*/Object.freeze({
3944
+ __proto__: null,
3945
+ FunctionAddItem: FunctionAddItem,
3946
+ TagClick: TagClick,
3947
+ TagGroupKeyDownArrowLeft: TagGroupKeyDownArrowLeft,
3948
+ TagGroupKeyDownArrowRight: TagGroupKeyDownArrowRight,
3949
+ TagGroupKeyDownBackspace: TagGroupKeyDownBackspace,
3950
+ TagGroupKeyDownDelete: TagGroupKeyDownDelete,
3951
+ TagRemoveClick: TagRemoveClick
3952
+ });
3953
+
3954
+ function useTagGroupReducer(state, _props, action) {
3955
+ var type = action.type;
3956
+ var changes;
3957
+ switch (type) {
3958
+ case TagClick:
3959
+ changes = {
3960
+ activeIndex: action.index
3961
+ };
3962
+ break;
3963
+ case TagGroupKeyDownArrowLeft:
3964
+ changes = {
3965
+ activeIndex: state.activeIndex === 0 ? state.items.length - 1 : state.activeIndex - 1
3966
+ };
3967
+ break;
3968
+ case TagGroupKeyDownArrowRight:
3969
+ changes = {
3970
+ activeIndex: state.activeIndex === state.items.length - 1 ? 0 : state.activeIndex + 1
3971
+ };
3972
+ break;
3973
+ case TagGroupKeyDownBackspace:
3974
+ case TagGroupKeyDownDelete:
3975
+ {
3976
+ var newItems = [].concat(state.items.slice(0, state.activeIndex), state.items.slice(state.activeIndex + 1));
3977
+ var newActiveIndex = newItems.length === 0 ? -1 : newItems.length === state.activeIndex ? state.activeIndex - 1 : state.activeIndex;
3978
+ changes = {
3979
+ items: [].concat(state.items.slice(0, state.activeIndex), state.items.slice(state.activeIndex + 1)),
3980
+ activeIndex: newActiveIndex
3981
+ };
3982
+ break;
3983
+ }
3984
+ case TagRemoveClick:
3985
+ {
3986
+ var _newItems = [].concat(state.items.slice(0, action.index), state.items.slice(action.index + 1));
3987
+ var _newActiveIndex = _newItems.length === 0 ? -1 : _newItems.length === action.index ? action.index - 1 : action.index;
3988
+ changes = {
3989
+ items: _newItems,
3990
+ activeIndex: _newActiveIndex
3991
+ };
3992
+ }
3993
+ break;
3994
+ case FunctionAddItem:
3995
+ {
3996
+ var _newItems2 = [];
3997
+ if (action.index === undefined) {
3998
+ _newItems2 = [].concat(state.items, [action.item]);
3999
+ } else {
4000
+ _newItems2 = [].concat(state.items.slice(0, action.index), [action.item], state.items.slice(action.index));
4001
+ }
4002
+ var _newActiveIndex2 = state.activeIndex === -1 ? _newItems2.length - 1 : state.activeIndex;
4003
+ changes = {
4004
+ items: _newItems2,
4005
+ activeIndex: _newActiveIndex2
4006
+ };
4007
+ break;
4008
+ }
4009
+ default:
4010
+ throw new Error('Invalid useTagGroup reducer action.');
4011
+ }
4012
+ return _extends({}, state, changes);
4013
+ }
4014
+
4015
+ // istanbul ignore next
4016
+ var useElementIds = 'useId' in React__namespace // Avoid conditional useId call
4017
+ ? useElementIdsR18 : useElementIdsLegacy;
4018
+ function useElementIdsR18(_ref) {
4019
+ var id = _ref.id,
4020
+ tagGroupId = _ref.tagGroupId,
4021
+ getTagId = _ref.getTagId;
4022
+ // Avoid conditional useId call
4023
+ var reactId = "downshift-" + React__namespace.useId();
4024
+ if (!id) {
4025
+ id = reactId;
4026
+ }
4027
+ var elementIdsRef = React__namespace.useRef({
4028
+ tagGroupId: tagGroupId != null ? tagGroupId : id + "-tag-group",
4029
+ getTagId: getTagId != null ? getTagId : function (index) {
4030
+ return id + "-tag-" + index;
4031
+ }
4032
+ });
4033
+ return elementIdsRef.current;
4034
+ }
4035
+ function useElementIdsLegacy(_ref2) {
4036
+ var _ref2$id = _ref2.id,
4037
+ id = _ref2$id === void 0 ? "downshift-" + generateId() : _ref2$id,
4038
+ getTagId = _ref2.getTagId,
4039
+ tagGroupId = _ref2.tagGroupId;
4040
+ var elementIdsRef = React__namespace.useRef({
4041
+ tagGroupId: tagGroupId != null ? tagGroupId : id + "-tag-group",
4042
+ getTagId: getTagId != null ? getTagId : function (index) {
4043
+ return id + "-tag-" + index;
4044
+ }
4045
+ });
4046
+ return elementIdsRef.current;
4047
+ }
4048
+
4049
+ function getInitialState(props) {
4050
+ var _ref, _props$items, _ref2, _props$activeIndex;
4051
+ var items = (_ref = (_props$items = props.items) != null ? _props$items : props.initialItems) != null ? _ref : [];
4052
+ var activeIndex = (_ref2 = (_props$activeIndex = props.activeIndex) != null ? _props$activeIndex : props.initialActiveIndex) != null ? _ref2 : items.length === 0 ? -1 : 0;
4053
+ return {
4054
+ activeIndex: activeIndex,
4055
+ items: items
4056
+ };
4057
+ }
4058
+
4059
+ function isStateEqual(oldState, newState) {
4060
+ return oldState.activeIndex === newState.activeIndex && oldState.items === newState.items;
4061
+ }
4062
+
4063
+ var A11Y_DESCRIPTION_ELEMENT_ID = 'tag-group-a11y-description';
4064
+ function useAccessibleDescription(document, description) {
4065
+ React__namespace.useEffect(function () {
4066
+ if (!document) {
4067
+ return;
4068
+ }
4069
+ var accessibleDescriptionElement = document.createElement('div');
4070
+ accessibleDescriptionElement.setAttribute('id', A11Y_DESCRIPTION_ELEMENT_ID);
4071
+ accessibleDescriptionElement.style.display = 'none';
4072
+ accessibleDescriptionElement.textContent = description;
4073
+ document.body.appendChild(accessibleDescriptionElement);
4074
+ return function () {
4075
+ accessibleDescriptionElement.remove();
4076
+ };
4077
+ }, [description, document]);
4078
+ }
4079
+
4080
+ function getMergedProps(userProps) {
4081
+ return _extends({
4082
+ stateReducer: function stateReducer(_s, _ref) {
4083
+ var changes = _ref.changes;
4084
+ return changes;
4085
+ },
4086
+ environment: /* istanbul ignore next (ssr) */
4087
+ typeof window === 'undefined' || false ? undefined : window,
4088
+ removeElementDescription: 'Press Delete or Backspace to remove tag.'
4089
+ }, userProps);
4090
+ }
4091
+
4092
+ /**
4093
+ * Focuses the tag at activeIndex when it changes or when an item is removed.
4094
+ */
4095
+ function useRovingTagFocus(activeIndex, itemsLength, getTagId) {
4096
+ var itemRefs = React__namespace.useRef({});
4097
+ var previousActiveIndexRef = React__namespace.useRef(activeIndex);
4098
+ var previousItemsLengthRef = React__namespace.useRef(itemsLength);
4099
+ React__namespace.useEffect(function () {
4100
+ if (activeIndex !== -1 && previousActiveIndexRef.current !== -1 && activeIndex !== previousActiveIndexRef.current || previousItemsLengthRef.current === itemsLength + 1) {
4101
+ var _itemRefs$current$get;
4102
+ (_itemRefs$current$get = itemRefs.current[getTagId(activeIndex)]) == null || _itemRefs$current$get.focus();
4103
+ }
4104
+ previousActiveIndexRef.current = activeIndex;
4105
+ previousItemsLengthRef.current = itemsLength;
4106
+ }, [activeIndex, getTagId, itemsLength]);
4107
+ return itemRefs;
4108
+ }
4109
+
4110
+ var propTypes = {
4111
+ isItemDisabled: PropTypes.func
4112
+ };
4113
+
4114
+ var _excluded = ["onKeyDown"],
4115
+ _excluded2 = ["index", "refKey", "ref", "onClick"],
4116
+ _excluded3 = ["index", "onClick"];
4117
+ var _useTagGroup = function useTagGroup(userProps) {
4118
+ var _props$environment;
4119
+ if (userProps === void 0) {
4120
+ userProps = {};
4121
+ }
4122
+ /* State and Props */
4123
+
4124
+ validatePropTypes$1(userProps, _useTagGroup, propTypes);
4125
+ var props = getMergedProps(userProps);
4126
+ var _useControlledReducer = useControlledReducer$1(useTagGroupReducer, props, getInitialState, isStateEqual),
4127
+ state = _useControlledReducer[0],
4128
+ dispatch = _useControlledReducer[1];
4129
+ var activeIndex = state.activeIndex,
4130
+ items = state.items;
4131
+
4132
+ /* Refs */
4133
+
4134
+ var latest = useLatestRef({
4135
+ state: state,
4136
+ props: props
4137
+ });
4138
+ var elementIds = useElementIds({
4139
+ getTagId: props.getTagId,
4140
+ id: props.id,
4141
+ tagGroupId: props.tagGroupId
4142
+ });
4143
+
4144
+ /* Effects */
4145
+
4146
+ useAccessibleDescription((_props$environment = props.environment) == null ? void 0 : _props$environment.document, props.removeElementDescription);
4147
+ var itemRefs = useRovingTagFocus(activeIndex, items.length, elementIds.getTagId);
4148
+
4149
+ /* Getter functions */
4150
+
4151
+ var getTagGroupProps = React.useCallback(function (options) {
4152
+ var _ref = options != null ? options : {},
4153
+ onKeyDown = _ref.onKeyDown,
4154
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded);
4155
+ var handleKeyDown = function handleKeyDown(e) {
4156
+ switch (e.key) {
4157
+ case 'ArrowLeft':
4158
+ dispatch({
4159
+ type: TagGroupKeyDownArrowLeft
4160
+ });
4161
+ break;
4162
+ case 'ArrowRight':
4163
+ dispatch({
4164
+ type: TagGroupKeyDownArrowRight
4165
+ });
4166
+ break;
4167
+ case 'Delete':
4168
+ dispatch({
4169
+ type: TagGroupKeyDownDelete
4170
+ });
4171
+ break;
4172
+ case 'Backspace':
4173
+ dispatch({
4174
+ type: TagGroupKeyDownBackspace
4175
+ });
4176
+ break;
4177
+ }
4178
+ };
4179
+ var tagGroupProps = _extends({
4180
+ id: elementIds.tagGroupId,
4181
+ 'aria-live': 'polite',
4182
+ 'aria-atomic': 'false',
4183
+ 'aria-relevant': 'additions',
4184
+ role: 'listbox',
4185
+ onKeyDown: callAllEventHandlers$1(onKeyDown, handleKeyDown)
4186
+ }, rest);
4187
+ return tagGroupProps;
4188
+ }, [dispatch, elementIds.tagGroupId]);
4189
+ var getTagProps = React.useCallback(function (options) {
4190
+ var _extends2;
4191
+ var index = options.index,
4192
+ _options$refKey = options.refKey,
4193
+ refKey = _options$refKey === void 0 ? 'ref' : _options$refKey,
4194
+ ref = options.ref,
4195
+ onClick = options.onClick,
4196
+ rest = _objectWithoutPropertiesLoose(options, _excluded2);
4197
+ if (!Number.isInteger(index) || index < 0) {
4198
+ throw new Error('Pass correct item index to getTagProps!');
4199
+ }
4200
+ var latestState = latest.current.state;
4201
+ var handleClick = function handleClick() {
4202
+ dispatch({
4203
+ type: TagClick,
4204
+ index: index
4205
+ });
4206
+ };
4207
+ var tagId = elementIds.getTagId(index);
4208
+ return _extends((_extends2 = {
4209
+ 'aria-describedby': A11Y_DESCRIPTION_ELEMENT_ID
4210
+ }, _extends2[refKey] = handleRefs$1(ref, function (itemNode) {
4211
+ if (itemNode) {
4212
+ itemRefs.current[tagId] = itemNode;
4213
+ }
4214
+ }), _extends2['aria-labelledby'] = tagId, _extends2.role = 'option', _extends2.id = tagId, _extends2.onClick = callAllEventHandlers$1(onClick, handleClick), _extends2.tabIndex = latestState.activeIndex === index ? 0 : -1, _extends2), rest);
4215
+ }, [dispatch, elementIds, latest, itemRefs]);
4216
+ var getTagRemoveProps = React.useCallback(function (options) {
4217
+ var index = options.index,
4218
+ onClick = options.onClick,
4219
+ rest = _objectWithoutPropertiesLoose(options, _excluded3);
4220
+ if (!Number.isInteger(index) || index < 0) {
4221
+ throw new Error('Pass correct item index to getTagRemoveProps!');
4222
+ }
4223
+ var handleClick = function handleClick(event) {
4224
+ event.stopPropagation();
4225
+ dispatch({
4226
+ type: TagRemoveClick,
4227
+ index: index
4228
+ });
4229
+ };
4230
+ var tagId = elementIds.getTagId(index);
4231
+ var tagRemoveId = tagId + "-remove";
4232
+ return _extends({
4233
+ id: tagRemoveId,
4234
+ tabIndex: -1,
4235
+ 'aria-labelledby': tagRemoveId + " " + tagId,
4236
+ onClick: callAllEventHandlers$1(onClick, handleClick)
4237
+ }, rest);
4238
+ }, [elementIds, dispatch]);
4239
+
4240
+ /* Imperative Functions */
4241
+
4242
+ var addItem = React.useCallback(function (item, index) {
4243
+ dispatch({
4244
+ type: FunctionAddItem,
4245
+ item: item,
4246
+ index: index
4247
+ });
4248
+ }, [dispatch]);
4249
+ return {
4250
+ activeIndex: activeIndex,
4251
+ addItem: addItem,
4252
+ getTagGroupProps: getTagGroupProps,
4253
+ getTagProps: getTagProps,
4254
+ getTagRemoveProps: getTagRemoveProps,
4255
+ items: items
4256
+ };
4257
+ };
4258
+ _useTagGroup.stateChangeTypes = stateChangeTypes;
4259
+
4260
+ exports.default = Downshift;
4261
+ exports.resetIdCounter = resetIdCounter;
4262
+ exports.useCombobox = useCombobox;
4263
+ exports.useMultipleSelection = useMultipleSelection;
4264
+ exports.useSelect = useSelect;
4265
+ exports.useTagGroup = _useTagGroup;