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