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