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