downshift 6.1.6 → 6.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -9
- package/dist/downshift.cjs.js +1703 -1637
- package/dist/downshift.esm.js +1577 -1507
- package/dist/downshift.native.cjs.js +1688 -1620
- package/dist/downshift.umd.js +1614 -1574
- package/dist/downshift.umd.js.map +1 -1
- package/dist/downshift.umd.min.js +1 -21
- package/dist/downshift.umd.min.js.map +1 -1
- package/package.json +25 -11
- package/preact/dist/downshift.cjs.js +1667 -1601
- package/preact/dist/downshift.esm.js +1577 -1507
- package/preact/dist/downshift.umd.js +1667 -1649
- package/preact/dist/downshift.umd.js.map +1 -1
- package/preact/dist/downshift.umd.min.js +1 -23
- package/preact/dist/downshift.umd.min.js.map +1 -1
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import
|
|
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
|
-
import { cloneElement, Component, useRef, useEffect, useCallback, useLayoutEffect, useReducer, useMemo } from 'preact';
|
|
1
|
+
import { Component, cloneElement, useRef, useEffect, useCallback, useLayoutEffect, useReducer, useMemo } from 'preact';
|
|
6
2
|
import { isForwardRef } from 'react-is';
|
|
7
3
|
import computeScrollIntoView from 'compute-scroll-into-view';
|
|
8
4
|
import { __assign } from 'tslib';
|
|
9
5
|
import PropTypes from 'prop-types';
|
|
10
6
|
|
|
11
|
-
|
|
7
|
+
let idCounter = 0;
|
|
12
8
|
/**
|
|
13
9
|
* Accepts a parameter and returns it if it's a function
|
|
14
10
|
* or a noop function if it's not. This allows us to
|
|
@@ -35,15 +31,17 @@ function scrollIntoView(node, menuNode) {
|
|
|
35
31
|
return;
|
|
36
32
|
}
|
|
37
33
|
|
|
38
|
-
|
|
34
|
+
const actions = computeScrollIntoView(node, {
|
|
39
35
|
boundary: menuNode,
|
|
40
36
|
block: 'nearest',
|
|
41
37
|
scrollMode: 'if-needed'
|
|
42
38
|
});
|
|
43
|
-
actions.forEach(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
actions.forEach(_ref => {
|
|
40
|
+
let {
|
|
41
|
+
el,
|
|
42
|
+
top,
|
|
43
|
+
left
|
|
44
|
+
} = _ref;
|
|
47
45
|
el.scrollTop = top;
|
|
48
46
|
el.scrollLeft = left;
|
|
49
47
|
});
|
|
@@ -57,7 +55,7 @@ function scrollIntoView(node, menuNode) {
|
|
|
57
55
|
|
|
58
56
|
|
|
59
57
|
function isOrContainsNode(parent, child, environment) {
|
|
60
|
-
|
|
58
|
+
const result = parent === child || child instanceof environment.Node && parent.contains && parent.contains(child);
|
|
61
59
|
return result;
|
|
62
60
|
}
|
|
63
61
|
/**
|
|
@@ -71,7 +69,7 @@ function isOrContainsNode(parent, child, environment) {
|
|
|
71
69
|
|
|
72
70
|
|
|
73
71
|
function debounce(fn, time) {
|
|
74
|
-
|
|
72
|
+
let timeoutId;
|
|
75
73
|
|
|
76
74
|
function cancel() {
|
|
77
75
|
if (timeoutId) {
|
|
@@ -85,9 +83,9 @@ function debounce(fn, time) {
|
|
|
85
83
|
}
|
|
86
84
|
|
|
87
85
|
cancel();
|
|
88
|
-
timeoutId = setTimeout(
|
|
86
|
+
timeoutId = setTimeout(() => {
|
|
89
87
|
timeoutId = null;
|
|
90
|
-
fn
|
|
88
|
+
fn(...args);
|
|
91
89
|
}, time);
|
|
92
90
|
}
|
|
93
91
|
|
|
@@ -113,9 +111,9 @@ function callAllEventHandlers() {
|
|
|
113
111
|
args[_key3 - 1] = arguments[_key3];
|
|
114
112
|
}
|
|
115
113
|
|
|
116
|
-
return fns.some(
|
|
114
|
+
return fns.some(fn => {
|
|
117
115
|
if (fn) {
|
|
118
|
-
fn
|
|
116
|
+
fn(event, ...args);
|
|
119
117
|
}
|
|
120
118
|
|
|
121
119
|
return event.preventDownshiftDefault || event.hasOwnProperty('nativeEvent') && event.nativeEvent.preventDownshiftDefault;
|
|
@@ -128,8 +126,8 @@ function handleRefs() {
|
|
|
128
126
|
refs[_key4] = arguments[_key4];
|
|
129
127
|
}
|
|
130
128
|
|
|
131
|
-
return
|
|
132
|
-
refs.forEach(
|
|
129
|
+
return node => {
|
|
130
|
+
refs.forEach(ref => {
|
|
133
131
|
if (typeof ref === 'function') {
|
|
134
132
|
ref(node);
|
|
135
133
|
} else if (ref) {
|
|
@@ -166,9 +164,11 @@ function resetIdCounter() {
|
|
|
166
164
|
|
|
167
165
|
|
|
168
166
|
function getA11yStatusMessage$1(_ref2) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
167
|
+
let {
|
|
168
|
+
isOpen,
|
|
169
|
+
resultCount,
|
|
170
|
+
previousResultCount
|
|
171
|
+
} = _ref2;
|
|
172
172
|
|
|
173
173
|
if (!isOpen) {
|
|
174
174
|
return '';
|
|
@@ -244,7 +244,7 @@ function requiredProp(fnName, propName) {
|
|
|
244
244
|
console.error("The property \"" + propName + "\" is required in \"" + fnName + "\"");
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
-
|
|
247
|
+
const stateKeys = ['highlightedIndex', 'inputValue', 'isOpen', 'selectedItem', 'type'];
|
|
248
248
|
/**
|
|
249
249
|
* @param {Object} state the state object
|
|
250
250
|
* @return {Object} state that is relevant to downshift
|
|
@@ -255,8 +255,8 @@ function pickState(state) {
|
|
|
255
255
|
state = {};
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
|
|
259
|
-
stateKeys.forEach(
|
|
258
|
+
const result = {};
|
|
259
|
+
stateKeys.forEach(k => {
|
|
260
260
|
if (state.hasOwnProperty(k)) {
|
|
261
261
|
result[k] = state[k];
|
|
262
262
|
}
|
|
@@ -277,7 +277,7 @@ function pickState(state) {
|
|
|
277
277
|
|
|
278
278
|
|
|
279
279
|
function getState(state, props) {
|
|
280
|
-
return Object.keys(state).reduce(
|
|
280
|
+
return Object.keys(state).reduce((prevState, key) => {
|
|
281
281
|
prevState[key] = isControlledProp(props, key) ? props[key] : state[key];
|
|
282
282
|
return prevState;
|
|
283
283
|
}, {});
|
|
@@ -304,8 +304,10 @@ function isControlledProp(props, key) {
|
|
|
304
304
|
|
|
305
305
|
|
|
306
306
|
function normalizeArrowKey(event) {
|
|
307
|
-
|
|
308
|
-
|
|
307
|
+
const {
|
|
308
|
+
key,
|
|
309
|
+
keyCode
|
|
310
|
+
} = event;
|
|
309
311
|
/* istanbul ignore next (ie) */
|
|
310
312
|
|
|
311
313
|
if (keyCode >= 37 && keyCode <= 40 && key.indexOf('Arrow') !== 0) {
|
|
@@ -346,13 +348,13 @@ function getNextWrappingIndex(moveAmount, baseIndex, itemCount, getItemNodeFromI
|
|
|
346
348
|
return -1;
|
|
347
349
|
}
|
|
348
350
|
|
|
349
|
-
|
|
351
|
+
const itemsLastIndex = itemCount - 1;
|
|
350
352
|
|
|
351
353
|
if (typeof baseIndex !== 'number' || baseIndex < 0 || baseIndex >= itemCount) {
|
|
352
354
|
baseIndex = moveAmount > 0 ? -1 : itemsLastIndex + 1;
|
|
353
355
|
}
|
|
354
356
|
|
|
355
|
-
|
|
357
|
+
let newIndex = baseIndex + moveAmount;
|
|
356
358
|
|
|
357
359
|
if (newIndex < 0) {
|
|
358
360
|
newIndex = circular ? itemsLastIndex : 0;
|
|
@@ -360,7 +362,7 @@ function getNextWrappingIndex(moveAmount, baseIndex, itemCount, getItemNodeFromI
|
|
|
360
362
|
newIndex = circular ? 0 : itemsLastIndex;
|
|
361
363
|
}
|
|
362
364
|
|
|
363
|
-
|
|
365
|
+
const nonDisabledNewIndex = getNextNonDisabledIndex(moveAmount, newIndex, itemCount, getItemNodeFromIndex, circular);
|
|
364
366
|
|
|
365
367
|
if (nonDisabledNewIndex === -1) {
|
|
366
368
|
return baseIndex >= itemCount ? -1 : baseIndex;
|
|
@@ -381,22 +383,22 @@ function getNextWrappingIndex(moveAmount, baseIndex, itemCount, getItemNodeFromI
|
|
|
381
383
|
|
|
382
384
|
|
|
383
385
|
function getNextNonDisabledIndex(moveAmount, baseIndex, itemCount, getItemNodeFromIndex, circular) {
|
|
384
|
-
|
|
386
|
+
const currentElementNode = getItemNodeFromIndex(baseIndex);
|
|
385
387
|
|
|
386
388
|
if (!currentElementNode || !currentElementNode.hasAttribute('disabled')) {
|
|
387
389
|
return baseIndex;
|
|
388
390
|
}
|
|
389
391
|
|
|
390
392
|
if (moveAmount > 0) {
|
|
391
|
-
for (
|
|
393
|
+
for (let index = baseIndex + 1; index < itemCount; index++) {
|
|
392
394
|
if (!getItemNodeFromIndex(index).hasAttribute('disabled')) {
|
|
393
395
|
return index;
|
|
394
396
|
}
|
|
395
397
|
}
|
|
396
398
|
} else {
|
|
397
|
-
for (
|
|
398
|
-
if (!getItemNodeFromIndex(
|
|
399
|
-
return
|
|
399
|
+
for (let index = baseIndex - 1; index >= 0; index--) {
|
|
400
|
+
if (!getItemNodeFromIndex(index).hasAttribute('disabled')) {
|
|
401
|
+
return index;
|
|
400
402
|
}
|
|
401
403
|
}
|
|
402
404
|
}
|
|
@@ -424,19 +426,17 @@ function targetWithinDownshift(target, downshiftElements, environment, checkActi
|
|
|
424
426
|
checkActiveElement = true;
|
|
425
427
|
}
|
|
426
428
|
|
|
427
|
-
return downshiftElements.some(
|
|
428
|
-
return contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment));
|
|
429
|
-
});
|
|
429
|
+
return downshiftElements.some(contextNode => contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment)));
|
|
430
430
|
} // eslint-disable-next-line import/no-mutable-exports
|
|
431
431
|
|
|
432
432
|
|
|
433
|
-
|
|
433
|
+
let validateControlledUnchanged = noop;
|
|
434
434
|
/* istanbul ignore next */
|
|
435
435
|
|
|
436
436
|
if (process.env.NODE_ENV !== 'production') {
|
|
437
|
-
validateControlledUnchanged =
|
|
438
|
-
|
|
439
|
-
Object.keys(state).forEach(
|
|
437
|
+
validateControlledUnchanged = (state, prevProps, nextProps) => {
|
|
438
|
+
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";
|
|
439
|
+
Object.keys(state).forEach(propKey => {
|
|
440
440
|
if (prevProps[propKey] !== undefined && nextProps[propKey] === undefined) {
|
|
441
441
|
// eslint-disable-next-line no-console
|
|
442
442
|
console.error("downshift: A component has changed the controlled prop \"" + propKey + "\" to be uncontrolled. " + warningDescription);
|
|
@@ -448,7 +448,7 @@ if (process.env.NODE_ENV !== 'production') {
|
|
|
448
448
|
};
|
|
449
449
|
}
|
|
450
450
|
|
|
451
|
-
|
|
451
|
+
const cleanupStatus = debounce(documentProp => {
|
|
452
452
|
getStatusDiv(documentProp).textContent = '';
|
|
453
453
|
}, 500);
|
|
454
454
|
/**
|
|
@@ -457,7 +457,7 @@ var cleanupStatus = debounce(function (documentProp) {
|
|
|
457
457
|
*/
|
|
458
458
|
|
|
459
459
|
function setStatus(status, documentProp) {
|
|
460
|
-
|
|
460
|
+
const div = getStatusDiv(documentProp);
|
|
461
461
|
|
|
462
462
|
if (!status) {
|
|
463
463
|
return;
|
|
@@ -478,7 +478,7 @@ function getStatusDiv(documentProp) {
|
|
|
478
478
|
documentProp = document;
|
|
479
479
|
}
|
|
480
480
|
|
|
481
|
-
|
|
481
|
+
let statusDiv = documentProp.getElementById('a11y-status-message');
|
|
482
482
|
|
|
483
483
|
if (statusDiv) {
|
|
484
484
|
return statusDiv;
|
|
@@ -503,23 +503,23 @@ function getStatusDiv(documentProp) {
|
|
|
503
503
|
return statusDiv;
|
|
504
504
|
}
|
|
505
505
|
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
506
|
+
const unknown = process.env.NODE_ENV !== "production" ? '__autocomplete_unknown__' : 0;
|
|
507
|
+
const mouseUp = process.env.NODE_ENV !== "production" ? '__autocomplete_mouseup__' : 1;
|
|
508
|
+
const itemMouseEnter = process.env.NODE_ENV !== "production" ? '__autocomplete_item_mouseenter__' : 2;
|
|
509
|
+
const keyDownArrowUp = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_arrow_up__' : 3;
|
|
510
|
+
const keyDownArrowDown = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_arrow_down__' : 4;
|
|
511
|
+
const keyDownEscape = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_escape__' : 5;
|
|
512
|
+
const keyDownEnter = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_enter__' : 6;
|
|
513
|
+
const keyDownHome = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_home__' : 7;
|
|
514
|
+
const keyDownEnd = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_end__' : 8;
|
|
515
|
+
const clickItem = process.env.NODE_ENV !== "production" ? '__autocomplete_click_item__' : 9;
|
|
516
|
+
const blurInput = process.env.NODE_ENV !== "production" ? '__autocomplete_blur_input__' : 10;
|
|
517
|
+
const changeInput = process.env.NODE_ENV !== "production" ? '__autocomplete_change_input__' : 11;
|
|
518
|
+
const keyDownSpaceButton = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_space_button__' : 12;
|
|
519
|
+
const clickButton = process.env.NODE_ENV !== "production" ? '__autocomplete_click_button__' : 13;
|
|
520
|
+
const blurButton = process.env.NODE_ENV !== "production" ? '__autocomplete_blur_button__' : 14;
|
|
521
|
+
const controlledPropUpdatedSelectedItem = process.env.NODE_ENV !== "production" ? '__autocomplete_controlled_prop_updated_selected_item__' : 15;
|
|
522
|
+
const touchEnd = process.env.NODE_ENV !== "production" ? '__autocomplete_touchend__' : 16;
|
|
523
523
|
|
|
524
524
|
var stateChangeTypes$3 = /*#__PURE__*/Object.freeze({
|
|
525
525
|
__proto__: null,
|
|
@@ -542,58 +542,45 @@ var stateChangeTypes$3 = /*#__PURE__*/Object.freeze({
|
|
|
542
542
|
touchEnd: touchEnd
|
|
543
543
|
});
|
|
544
544
|
|
|
545
|
-
|
|
546
|
-
_excluded2$3 = ["onClick", "onPress", "onKeyDown", "onKeyUp", "onBlur"],
|
|
547
|
-
_excluded3$2 = ["onKeyDown", "onBlur", "onChange", "onInput", "onChangeText"],
|
|
548
|
-
_excluded4$1 = ["refKey", "ref"],
|
|
549
|
-
_excluded5$1 = ["onMouseMove", "onMouseDown", "onClick", "onPress", "index", "item"];
|
|
545
|
+
/* eslint camelcase:0 */
|
|
550
546
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
function Downshift(_props) {
|
|
547
|
+
const Downshift = /*#__PURE__*/(() => {
|
|
548
|
+
class Downshift extends Component {
|
|
549
|
+
constructor(_props) {
|
|
556
550
|
var _this;
|
|
557
551
|
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
_this.labelId = _this.props.labelId || _this.id + "-label";
|
|
565
|
-
_this.inputId = _this.props.inputId || _this.id + "-input";
|
|
552
|
+
super(_props);
|
|
553
|
+
_this = this;
|
|
554
|
+
this.id = this.props.id || "downshift-" + generateId();
|
|
555
|
+
this.menuId = this.props.menuId || this.id + "-menu";
|
|
556
|
+
this.labelId = this.props.labelId || this.id + "-label";
|
|
557
|
+
this.inputId = this.props.inputId || this.id + "-input";
|
|
566
558
|
|
|
567
|
-
|
|
568
|
-
return _this.id + "-item-" + index;
|
|
569
|
-
};
|
|
559
|
+
this.getItemId = this.props.getItemId || (index => this.id + "-item-" + index);
|
|
570
560
|
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
561
|
+
this.input = null;
|
|
562
|
+
this.items = [];
|
|
563
|
+
this.itemCount = null;
|
|
564
|
+
this.previousResultCount = 0;
|
|
565
|
+
this.timeoutIds = [];
|
|
576
566
|
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
return i !== id;
|
|
581
|
-
});
|
|
567
|
+
this.internalSetTimeout = (fn, time) => {
|
|
568
|
+
const id = setTimeout(() => {
|
|
569
|
+
this.timeoutIds = this.timeoutIds.filter(i => i !== id);
|
|
582
570
|
fn();
|
|
583
571
|
}, time);
|
|
584
|
-
|
|
585
|
-
_this.timeoutIds.push(id);
|
|
572
|
+
this.timeoutIds.push(id);
|
|
586
573
|
};
|
|
587
574
|
|
|
588
|
-
|
|
589
|
-
|
|
575
|
+
this.setItemCount = count => {
|
|
576
|
+
this.itemCount = count;
|
|
590
577
|
};
|
|
591
578
|
|
|
592
|
-
|
|
593
|
-
|
|
579
|
+
this.unsetItemCount = () => {
|
|
580
|
+
this.itemCount = null;
|
|
594
581
|
};
|
|
595
582
|
|
|
596
|
-
|
|
583
|
+
this.setHighlightedIndex = function (highlightedIndex, otherStateToSet) {
|
|
597
584
|
if (highlightedIndex === void 0) {
|
|
598
585
|
highlightedIndex = _this.props.defaultHighlightedIndex;
|
|
599
586
|
}
|
|
@@ -604,71 +591,72 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
604
591
|
|
|
605
592
|
otherStateToSet = pickState(otherStateToSet);
|
|
606
593
|
|
|
607
|
-
_this.internalSetState(
|
|
608
|
-
highlightedIndex
|
|
609
|
-
|
|
594
|
+
_this.internalSetState({
|
|
595
|
+
highlightedIndex,
|
|
596
|
+
...otherStateToSet
|
|
597
|
+
});
|
|
610
598
|
};
|
|
611
599
|
|
|
612
|
-
|
|
613
|
-
|
|
600
|
+
this.clearSelection = cb => {
|
|
601
|
+
this.internalSetState({
|
|
614
602
|
selectedItem: null,
|
|
615
603
|
inputValue: '',
|
|
616
|
-
highlightedIndex:
|
|
617
|
-
isOpen:
|
|
604
|
+
highlightedIndex: this.props.defaultHighlightedIndex,
|
|
605
|
+
isOpen: this.props.defaultIsOpen
|
|
618
606
|
}, cb);
|
|
619
607
|
};
|
|
620
608
|
|
|
621
|
-
|
|
609
|
+
this.selectItem = (item, otherStateToSet, cb) => {
|
|
622
610
|
otherStateToSet = pickState(otherStateToSet);
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
highlightedIndex: _this.props.defaultHighlightedIndex,
|
|
611
|
+
this.internalSetState({
|
|
612
|
+
isOpen: this.props.defaultIsOpen,
|
|
613
|
+
highlightedIndex: this.props.defaultHighlightedIndex,
|
|
627
614
|
selectedItem: item,
|
|
628
|
-
inputValue:
|
|
629
|
-
|
|
615
|
+
inputValue: this.props.itemToString(item),
|
|
616
|
+
...otherStateToSet
|
|
617
|
+
}, cb);
|
|
630
618
|
};
|
|
631
619
|
|
|
632
|
-
|
|
633
|
-
|
|
620
|
+
this.selectItemAtIndex = (itemIndex, otherStateToSet, cb) => {
|
|
621
|
+
const item = this.items[itemIndex];
|
|
634
622
|
|
|
635
623
|
if (item == null) {
|
|
636
624
|
return;
|
|
637
625
|
}
|
|
638
626
|
|
|
639
|
-
|
|
627
|
+
this.selectItem(item, otherStateToSet, cb);
|
|
640
628
|
};
|
|
641
629
|
|
|
642
|
-
|
|
643
|
-
return
|
|
630
|
+
this.selectHighlightedItem = (otherStateToSet, cb) => {
|
|
631
|
+
return this.selectItemAtIndex(this.getState().highlightedIndex, otherStateToSet, cb);
|
|
644
632
|
};
|
|
645
633
|
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
634
|
+
this.internalSetState = (stateToSet, cb) => {
|
|
635
|
+
let isItemSelected, onChangeArg;
|
|
636
|
+
const onStateChangeArg = {};
|
|
637
|
+
const isStateToSetFunction = typeof stateToSet === 'function'; // we want to call `onInputValueChange` before the `setState` call
|
|
650
638
|
// so someone controlling the `inputValue` state gets notified of
|
|
651
639
|
// the input change as soon as possible. This avoids issues with
|
|
652
640
|
// preserving the cursor position.
|
|
653
641
|
// See https://github.com/downshift-js/downshift/issues/217 for more info.
|
|
654
642
|
|
|
655
643
|
if (!isStateToSetFunction && stateToSet.hasOwnProperty('inputValue')) {
|
|
656
|
-
|
|
644
|
+
this.props.onInputValueChange(stateToSet.inputValue, { ...this.getStateAndHelpers(),
|
|
645
|
+
...stateToSet
|
|
646
|
+
});
|
|
657
647
|
}
|
|
658
648
|
|
|
659
|
-
return
|
|
660
|
-
state =
|
|
661
|
-
|
|
649
|
+
return this.setState(state => {
|
|
650
|
+
state = this.getState(state);
|
|
651
|
+
let newStateToSet = isStateToSetFunction ? stateToSet(state) : stateToSet; // Your own function that could modify the state that will be set.
|
|
662
652
|
|
|
663
|
-
newStateToSet =
|
|
653
|
+
newStateToSet = this.props.stateReducer(state, newStateToSet); // checks if an item is selected, regardless of if it's different from
|
|
664
654
|
// what was selected before
|
|
665
655
|
// used to determine if onSelect and onChange callbacks should be called
|
|
666
656
|
|
|
667
657
|
isItemSelected = newStateToSet.hasOwnProperty('selectedItem'); // this keeps track of the object we want to call with setState
|
|
668
658
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
var nextFullState = {}; // we need to call on change if the outside world is controlling any of our state
|
|
659
|
+
const nextState = {}; // this is just used to tell whether the state changed
|
|
672
660
|
// and we're trying to update that state. OR if the selection has changed and we're
|
|
673
661
|
// trying to update the selection
|
|
674
662
|
|
|
@@ -677,7 +665,7 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
677
665
|
}
|
|
678
666
|
|
|
679
667
|
newStateToSet.type = newStateToSet.type || unknown;
|
|
680
|
-
Object.keys(newStateToSet).forEach(
|
|
668
|
+
Object.keys(newStateToSet).forEach(key => {
|
|
681
669
|
// onStateChangeArg should only have the state that is
|
|
682
670
|
// actually changing
|
|
683
671
|
if (state[key] !== newStateToSet[key]) {
|
|
@@ -694,81 +682,84 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
694
682
|
return;
|
|
695
683
|
}
|
|
696
684
|
|
|
697
|
-
|
|
685
|
+
newStateToSet[key]; // if it's coming from props, then we don't care to set it internally
|
|
698
686
|
|
|
699
|
-
if (!isControlledProp(
|
|
687
|
+
if (!isControlledProp(this.props, key)) {
|
|
700
688
|
nextState[key] = newStateToSet[key];
|
|
701
689
|
}
|
|
702
690
|
}); // if stateToSet is a function, then we weren't able to call onInputValueChange
|
|
703
691
|
// earlier, so we'll call it now that we know what the inputValue state will be.
|
|
704
692
|
|
|
705
693
|
if (isStateToSetFunction && newStateToSet.hasOwnProperty('inputValue')) {
|
|
706
|
-
|
|
694
|
+
this.props.onInputValueChange(newStateToSet.inputValue, { ...this.getStateAndHelpers(),
|
|
695
|
+
...newStateToSet
|
|
696
|
+
});
|
|
707
697
|
}
|
|
708
698
|
|
|
709
699
|
return nextState;
|
|
710
|
-
},
|
|
700
|
+
}, () => {
|
|
711
701
|
// call the provided callback if it's a function
|
|
712
702
|
cbToCb(cb)(); // only call the onStateChange and onChange callbacks if
|
|
713
703
|
// we have relevant information to pass them.
|
|
714
704
|
|
|
715
|
-
|
|
705
|
+
const hasMoreStateThanType = Object.keys(onStateChangeArg).length > 1;
|
|
716
706
|
|
|
717
707
|
if (hasMoreStateThanType) {
|
|
718
|
-
|
|
708
|
+
this.props.onStateChange(onStateChangeArg, this.getStateAndHelpers());
|
|
719
709
|
}
|
|
720
710
|
|
|
721
711
|
if (isItemSelected) {
|
|
722
|
-
|
|
712
|
+
this.props.onSelect(stateToSet.selectedItem, this.getStateAndHelpers());
|
|
723
713
|
}
|
|
724
714
|
|
|
725
715
|
if (onChangeArg !== undefined) {
|
|
726
|
-
|
|
716
|
+
this.props.onChange(onChangeArg, this.getStateAndHelpers());
|
|
727
717
|
} // this is currently undocumented and therefore subject to change
|
|
728
718
|
// We'll try to not break it, but just be warned.
|
|
729
719
|
|
|
730
720
|
|
|
731
|
-
|
|
721
|
+
this.props.onUserAction(onStateChangeArg, this.getStateAndHelpers());
|
|
732
722
|
});
|
|
733
723
|
};
|
|
734
724
|
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded$4);
|
|
747
|
-
|
|
748
|
-
var _ref2 = _temp2 === void 0 ? {} : _temp2,
|
|
749
|
-
_ref2$suppressRefErro = _ref2.suppressRefError,
|
|
750
|
-
suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
|
|
751
|
-
|
|
725
|
+
this.rootRef = node => this._rootNode = node;
|
|
726
|
+
|
|
727
|
+
this.getRootProps = function (_temp, _temp2) {
|
|
728
|
+
let {
|
|
729
|
+
refKey = 'ref',
|
|
730
|
+
ref,
|
|
731
|
+
...rest
|
|
732
|
+
} = _temp === void 0 ? {} : _temp;
|
|
733
|
+
let {
|
|
734
|
+
suppressRefError = false
|
|
735
|
+
} = _temp2 === void 0 ? {} : _temp2;
|
|
752
736
|
// this is used in the render to know whether the user has called getRootProps.
|
|
753
737
|
// It uses that to know whether to apply the props automatically
|
|
754
738
|
_this.getRootProps.called = true;
|
|
755
739
|
_this.getRootProps.refKey = refKey;
|
|
756
740
|
_this.getRootProps.suppressRefError = suppressRefError;
|
|
757
741
|
|
|
758
|
-
|
|
759
|
-
|
|
742
|
+
const {
|
|
743
|
+
isOpen
|
|
744
|
+
} = _this.getState();
|
|
760
745
|
|
|
761
|
-
return
|
|
746
|
+
return {
|
|
747
|
+
[refKey]: handleRefs(ref, _this.rootRef),
|
|
748
|
+
role: 'combobox',
|
|
749
|
+
'aria-expanded': isOpen,
|
|
750
|
+
'aria-haspopup': 'listbox',
|
|
751
|
+
'aria-owns': isOpen ? _this.menuId : null,
|
|
752
|
+
'aria-labelledby': _this.labelId,
|
|
753
|
+
...rest
|
|
754
|
+
};
|
|
762
755
|
};
|
|
763
756
|
|
|
764
|
-
|
|
765
|
-
ArrowDown
|
|
766
|
-
var _this2 = this;
|
|
767
|
-
|
|
757
|
+
this.keyDownHandlers = {
|
|
758
|
+
ArrowDown(event) {
|
|
768
759
|
event.preventDefault();
|
|
769
760
|
|
|
770
761
|
if (this.getState().isOpen) {
|
|
771
|
-
|
|
762
|
+
const amount = event.shiftKey ? 5 : 1;
|
|
772
763
|
this.moveHighlightedIndex(amount, {
|
|
773
764
|
type: keyDownArrowDown
|
|
774
765
|
});
|
|
@@ -776,31 +767,27 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
776
767
|
this.internalSetState({
|
|
777
768
|
isOpen: true,
|
|
778
769
|
type: keyDownArrowDown
|
|
779
|
-
},
|
|
780
|
-
|
|
770
|
+
}, () => {
|
|
771
|
+
const itemCount = this.getItemCount();
|
|
781
772
|
|
|
782
773
|
if (itemCount > 0) {
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
});
|
|
789
|
-
|
|
790
|
-
_this2.setHighlightedIndex(nextHighlightedIndex, {
|
|
774
|
+
const {
|
|
775
|
+
highlightedIndex
|
|
776
|
+
} = this.getState();
|
|
777
|
+
const nextHighlightedIndex = getNextWrappingIndex(1, highlightedIndex, itemCount, index => this.getItemNodeFromIndex(index));
|
|
778
|
+
this.setHighlightedIndex(nextHighlightedIndex, {
|
|
791
779
|
type: keyDownArrowDown
|
|
792
780
|
});
|
|
793
781
|
}
|
|
794
782
|
});
|
|
795
783
|
}
|
|
796
784
|
},
|
|
797
|
-
ArrowUp: function ArrowUp(event) {
|
|
798
|
-
var _this3 = this;
|
|
799
785
|
|
|
786
|
+
ArrowUp(event) {
|
|
800
787
|
event.preventDefault();
|
|
801
788
|
|
|
802
789
|
if (this.getState().isOpen) {
|
|
803
|
-
|
|
790
|
+
const amount = event.shiftKey ? -5 : -1;
|
|
804
791
|
this.moveHighlightedIndex(amount, {
|
|
805
792
|
type: keyDownArrowUp
|
|
806
793
|
});
|
|
@@ -808,37 +795,36 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
808
795
|
this.internalSetState({
|
|
809
796
|
isOpen: true,
|
|
810
797
|
type: keyDownArrowUp
|
|
811
|
-
},
|
|
812
|
-
|
|
798
|
+
}, () => {
|
|
799
|
+
const itemCount = this.getItemCount();
|
|
813
800
|
|
|
814
801
|
if (itemCount > 0) {
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
});
|
|
821
|
-
|
|
822
|
-
_this3.setHighlightedIndex(nextHighlightedIndex, {
|
|
802
|
+
const {
|
|
803
|
+
highlightedIndex
|
|
804
|
+
} = this.getState();
|
|
805
|
+
const nextHighlightedIndex = getNextWrappingIndex(-1, highlightedIndex, itemCount, index => this.getItemNodeFromIndex(index));
|
|
806
|
+
this.setHighlightedIndex(nextHighlightedIndex, {
|
|
823
807
|
type: keyDownArrowUp
|
|
824
808
|
});
|
|
825
809
|
}
|
|
826
810
|
});
|
|
827
811
|
}
|
|
828
812
|
},
|
|
829
|
-
|
|
813
|
+
|
|
814
|
+
Enter(event) {
|
|
830
815
|
if (event.which === 229) {
|
|
831
816
|
return;
|
|
832
817
|
}
|
|
833
818
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
819
|
+
const {
|
|
820
|
+
isOpen,
|
|
821
|
+
highlightedIndex
|
|
822
|
+
} = this.getState();
|
|
837
823
|
|
|
838
824
|
if (isOpen && highlightedIndex != null) {
|
|
839
825
|
event.preventDefault();
|
|
840
|
-
|
|
841
|
-
|
|
826
|
+
const item = this.items[highlightedIndex];
|
|
827
|
+
const itemNode = this.getItemNodeFromIndex(highlightedIndex);
|
|
842
828
|
|
|
843
829
|
if (item == null || itemNode && itemNode.hasAttribute('disabled')) {
|
|
844
830
|
return;
|
|
@@ -849,125 +835,131 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
849
835
|
});
|
|
850
836
|
}
|
|
851
837
|
},
|
|
852
|
-
|
|
838
|
+
|
|
839
|
+
Escape(event) {
|
|
853
840
|
event.preventDefault();
|
|
854
|
-
this.reset(
|
|
855
|
-
type: keyDownEscape
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
841
|
+
this.reset({
|
|
842
|
+
type: keyDownEscape,
|
|
843
|
+
...(!this.state.isOpen && {
|
|
844
|
+
selectedItem: null,
|
|
845
|
+
inputValue: ''
|
|
846
|
+
})
|
|
847
|
+
});
|
|
860
848
|
}
|
|
849
|
+
|
|
861
850
|
};
|
|
862
|
-
|
|
863
|
-
|
|
851
|
+
this.buttonKeyDownHandlers = { ...this.keyDownHandlers,
|
|
852
|
+
|
|
853
|
+
' '(event) {
|
|
864
854
|
event.preventDefault();
|
|
865
855
|
this.toggleMenu({
|
|
866
856
|
type: keyDownSpaceButton
|
|
867
857
|
});
|
|
868
858
|
}
|
|
869
|
-
});
|
|
870
|
-
_this.inputKeyDownHandlers = _extends({}, _this.keyDownHandlers, {
|
|
871
|
-
Home: function Home(event) {
|
|
872
|
-
var _this4 = this;
|
|
873
859
|
|
|
874
|
-
|
|
875
|
-
|
|
860
|
+
};
|
|
861
|
+
this.inputKeyDownHandlers = { ...this.keyDownHandlers,
|
|
862
|
+
|
|
863
|
+
Home(event) {
|
|
864
|
+
const {
|
|
865
|
+
isOpen
|
|
866
|
+
} = this.getState();
|
|
876
867
|
|
|
877
868
|
if (!isOpen) {
|
|
878
869
|
return;
|
|
879
870
|
}
|
|
880
871
|
|
|
881
872
|
event.preventDefault();
|
|
882
|
-
|
|
873
|
+
const itemCount = this.getItemCount();
|
|
883
874
|
|
|
884
875
|
if (itemCount <= 0 || !isOpen) {
|
|
885
876
|
return;
|
|
886
877
|
} // get next non-disabled starting downwards from 0 if that's disabled.
|
|
887
878
|
|
|
888
879
|
|
|
889
|
-
|
|
890
|
-
return _this4.getItemNodeFromIndex(index);
|
|
891
|
-
}, false);
|
|
880
|
+
const newHighlightedIndex = getNextNonDisabledIndex(1, 0, itemCount, index => this.getItemNodeFromIndex(index), false);
|
|
892
881
|
this.setHighlightedIndex(newHighlightedIndex, {
|
|
893
882
|
type: keyDownHome
|
|
894
883
|
});
|
|
895
884
|
},
|
|
896
|
-
End: function End(event) {
|
|
897
|
-
var _this5 = this;
|
|
898
885
|
|
|
899
|
-
|
|
900
|
-
|
|
886
|
+
End(event) {
|
|
887
|
+
const {
|
|
888
|
+
isOpen
|
|
889
|
+
} = this.getState();
|
|
901
890
|
|
|
902
891
|
if (!isOpen) {
|
|
903
892
|
return;
|
|
904
893
|
}
|
|
905
894
|
|
|
906
895
|
event.preventDefault();
|
|
907
|
-
|
|
896
|
+
const itemCount = this.getItemCount();
|
|
908
897
|
|
|
909
898
|
if (itemCount <= 0 || !isOpen) {
|
|
910
899
|
return;
|
|
911
900
|
} // get next non-disabled starting upwards from last index if that's disabled.
|
|
912
901
|
|
|
913
902
|
|
|
914
|
-
|
|
915
|
-
return _this5.getItemNodeFromIndex(index);
|
|
916
|
-
}, false);
|
|
903
|
+
const newHighlightedIndex = getNextNonDisabledIndex(-1, itemCount - 1, itemCount, index => this.getItemNodeFromIndex(index), false);
|
|
917
904
|
this.setHighlightedIndex(newHighlightedIndex, {
|
|
918
905
|
type: keyDownEnd
|
|
919
906
|
});
|
|
920
907
|
}
|
|
921
|
-
});
|
|
922
908
|
|
|
923
|
-
|
|
924
|
-
var _ref3 = _temp3 === void 0 ? {} : _temp3,
|
|
925
|
-
onClick = _ref3.onClick;
|
|
926
|
-
_ref3.onPress;
|
|
927
|
-
var onKeyDown = _ref3.onKeyDown,
|
|
928
|
-
onKeyUp = _ref3.onKeyUp,
|
|
929
|
-
onBlur = _ref3.onBlur,
|
|
930
|
-
rest = _objectWithoutPropertiesLoose(_ref3, _excluded2$3);
|
|
931
|
-
|
|
932
|
-
var _this$getState5 = _this.getState(),
|
|
933
|
-
isOpen = _this$getState5.isOpen;
|
|
909
|
+
};
|
|
934
910
|
|
|
935
|
-
|
|
911
|
+
this.getToggleButtonProps = function (_temp3) {
|
|
912
|
+
let {
|
|
913
|
+
onClick,
|
|
914
|
+
onPress,
|
|
915
|
+
onKeyDown,
|
|
916
|
+
onKeyUp,
|
|
917
|
+
onBlur,
|
|
918
|
+
...rest
|
|
919
|
+
} = _temp3 === void 0 ? {} : _temp3;
|
|
920
|
+
|
|
921
|
+
const {
|
|
922
|
+
isOpen
|
|
923
|
+
} = _this.getState();
|
|
924
|
+
|
|
925
|
+
const enabledEventHandlers = {
|
|
936
926
|
onClick: callAllEventHandlers(onClick, _this.buttonHandleClick),
|
|
937
927
|
onKeyDown: callAllEventHandlers(onKeyDown, _this.buttonHandleKeyDown),
|
|
938
928
|
onKeyUp: callAllEventHandlers(onKeyUp, _this.buttonHandleKeyUp),
|
|
939
929
|
onBlur: callAllEventHandlers(onBlur, _this.buttonHandleBlur)
|
|
940
930
|
};
|
|
941
|
-
|
|
942
|
-
return
|
|
931
|
+
const eventHandlers = rest.disabled ? {} : enabledEventHandlers;
|
|
932
|
+
return {
|
|
943
933
|
type: 'button',
|
|
944
934
|
role: 'button',
|
|
945
935
|
'aria-label': isOpen ? 'close menu' : 'open menu',
|
|
946
936
|
'aria-haspopup': true,
|
|
947
|
-
'data-toggle': true
|
|
948
|
-
|
|
937
|
+
'data-toggle': true,
|
|
938
|
+
...eventHandlers,
|
|
939
|
+
...rest
|
|
940
|
+
};
|
|
949
941
|
};
|
|
950
942
|
|
|
951
|
-
|
|
943
|
+
this.buttonHandleKeyUp = event => {
|
|
952
944
|
// Prevent click event from emitting in Firefox
|
|
953
945
|
event.preventDefault();
|
|
954
946
|
};
|
|
955
947
|
|
|
956
|
-
|
|
957
|
-
|
|
948
|
+
this.buttonHandleKeyDown = event => {
|
|
949
|
+
const key = normalizeArrowKey(event);
|
|
958
950
|
|
|
959
|
-
if (
|
|
960
|
-
|
|
951
|
+
if (this.buttonKeyDownHandlers[key]) {
|
|
952
|
+
this.buttonKeyDownHandlers[key].call(this, event);
|
|
961
953
|
}
|
|
962
954
|
};
|
|
963
955
|
|
|
964
|
-
|
|
956
|
+
this.buttonHandleClick = event => {
|
|
965
957
|
event.preventDefault(); // handle odd case for Safari and Firefox which
|
|
966
958
|
// don't give the button the focus properly.
|
|
967
959
|
|
|
968
960
|
/* istanbul ignore if (can't reasonably test this) */
|
|
969
961
|
|
|
970
|
-
if (
|
|
962
|
+
if (this.props.environment.document.activeElement === this.props.environment.document.body) {
|
|
971
963
|
event.target.focus();
|
|
972
964
|
} // to simplify testing components that use downshift, we'll not wrap this in a setTimeout
|
|
973
965
|
// if the NODE_ENV is test. With the proper build system, this should be dead code eliminated
|
|
@@ -975,69 +967,71 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
975
967
|
|
|
976
968
|
|
|
977
969
|
if (process.env.NODE_ENV === 'test') {
|
|
978
|
-
|
|
970
|
+
this.toggleMenu({
|
|
979
971
|
type: clickButton
|
|
980
972
|
});
|
|
981
973
|
} else {
|
|
982
974
|
// Ensure that toggle of menu occurs after the potential blur event in iOS
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
});
|
|
987
|
-
});
|
|
975
|
+
this.internalSetTimeout(() => this.toggleMenu({
|
|
976
|
+
type: clickButton
|
|
977
|
+
}));
|
|
988
978
|
}
|
|
989
979
|
};
|
|
990
980
|
|
|
991
|
-
|
|
992
|
-
|
|
981
|
+
this.buttonHandleBlur = event => {
|
|
982
|
+
const blurTarget = event.target; // Save blur target for comparison with activeElement later
|
|
993
983
|
// Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not body element
|
|
994
984
|
|
|
995
|
-
|
|
996
|
-
if (!
|
|
985
|
+
this.internalSetTimeout(() => {
|
|
986
|
+
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)
|
|
997
987
|
) {
|
|
998
|
-
|
|
988
|
+
this.reset({
|
|
999
989
|
type: blurButton
|
|
1000
990
|
});
|
|
1001
991
|
}
|
|
1002
992
|
});
|
|
1003
993
|
};
|
|
1004
994
|
|
|
1005
|
-
|
|
1006
|
-
return
|
|
1007
|
-
htmlFor:
|
|
1008
|
-
id:
|
|
1009
|
-
|
|
995
|
+
this.getLabelProps = props => {
|
|
996
|
+
return {
|
|
997
|
+
htmlFor: this.inputId,
|
|
998
|
+
id: this.labelId,
|
|
999
|
+
...props
|
|
1000
|
+
};
|
|
1010
1001
|
};
|
|
1011
1002
|
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1003
|
+
this.getInputProps = function (_temp4) {
|
|
1004
|
+
let {
|
|
1005
|
+
onKeyDown,
|
|
1006
|
+
onBlur,
|
|
1007
|
+
onChange,
|
|
1008
|
+
onInput,
|
|
1009
|
+
onChangeText,
|
|
1010
|
+
...rest
|
|
1011
|
+
} = _temp4 === void 0 ? {} : _temp4;
|
|
1012
|
+
let onChangeKey;
|
|
1013
|
+
let eventHandlers = {};
|
|
1023
1014
|
/* istanbul ignore next (preact) */
|
|
1024
1015
|
|
|
1025
1016
|
{
|
|
1026
1017
|
onChangeKey = 'onInput';
|
|
1027
1018
|
}
|
|
1028
1019
|
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1020
|
+
const {
|
|
1021
|
+
inputValue,
|
|
1022
|
+
isOpen,
|
|
1023
|
+
highlightedIndex
|
|
1024
|
+
} = _this.getState();
|
|
1033
1025
|
|
|
1034
1026
|
if (!rest.disabled) {
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1027
|
+
eventHandlers = {
|
|
1028
|
+
[onChangeKey]: callAllEventHandlers(onChange, onInput, _this.inputHandleChange),
|
|
1029
|
+
onKeyDown: callAllEventHandlers(onKeyDown, _this.inputHandleKeyDown),
|
|
1030
|
+
onBlur: callAllEventHandlers(onBlur, _this.inputHandleBlur)
|
|
1031
|
+
};
|
|
1038
1032
|
}
|
|
1039
1033
|
|
|
1040
|
-
return
|
|
1034
|
+
return {
|
|
1041
1035
|
'aria-autocomplete': 'list',
|
|
1042
1036
|
'aria-activedescendant': isOpen && typeof highlightedIndex === 'number' && highlightedIndex >= 0 ? _this.getItemId(highlightedIndex) : null,
|
|
1043
1037
|
'aria-controls': isOpen ? _this.menuId : null,
|
|
@@ -1046,77 +1040,79 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1046
1040
|
// revert back since autocomplete="nope" is ignored on latest Chrome and Opera
|
|
1047
1041
|
autoComplete: 'off',
|
|
1048
1042
|
value: inputValue,
|
|
1049
|
-
id: _this.inputId
|
|
1050
|
-
|
|
1043
|
+
id: _this.inputId,
|
|
1044
|
+
...eventHandlers,
|
|
1045
|
+
...rest
|
|
1046
|
+
};
|
|
1051
1047
|
};
|
|
1052
1048
|
|
|
1053
|
-
|
|
1054
|
-
|
|
1049
|
+
this.inputHandleKeyDown = event => {
|
|
1050
|
+
const key = normalizeArrowKey(event);
|
|
1055
1051
|
|
|
1056
|
-
if (key &&
|
|
1057
|
-
|
|
1052
|
+
if (key && this.inputKeyDownHandlers[key]) {
|
|
1053
|
+
this.inputKeyDownHandlers[key].call(this, event);
|
|
1058
1054
|
}
|
|
1059
1055
|
};
|
|
1060
1056
|
|
|
1061
|
-
|
|
1062
|
-
|
|
1057
|
+
this.inputHandleChange = event => {
|
|
1058
|
+
this.internalSetState({
|
|
1063
1059
|
type: changeInput,
|
|
1064
1060
|
isOpen: true,
|
|
1065
1061
|
inputValue: event.target.value,
|
|
1066
|
-
highlightedIndex:
|
|
1062
|
+
highlightedIndex: this.props.defaultHighlightedIndex
|
|
1067
1063
|
});
|
|
1068
1064
|
};
|
|
1069
1065
|
|
|
1070
|
-
|
|
1066
|
+
this.inputHandleBlur = () => {
|
|
1071
1067
|
// Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not the body element
|
|
1072
|
-
|
|
1073
|
-
|
|
1068
|
+
this.internalSetTimeout(() => {
|
|
1069
|
+
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);
|
|
1074
1070
|
|
|
1075
|
-
if (!
|
|
1076
|
-
|
|
1071
|
+
if (!this.isMouseDown && !downshiftButtonIsActive) {
|
|
1072
|
+
this.reset({
|
|
1077
1073
|
type: blurInput
|
|
1078
1074
|
});
|
|
1079
1075
|
}
|
|
1080
1076
|
});
|
|
1081
1077
|
};
|
|
1082
1078
|
|
|
1083
|
-
|
|
1084
|
-
|
|
1079
|
+
this.menuRef = node => {
|
|
1080
|
+
this._menuNode = node;
|
|
1085
1081
|
};
|
|
1086
1082
|
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
var _ref6 = _temp6 === void 0 ? {} : _temp6,
|
|
1097
|
-
_ref6$suppressRefErro = _ref6.suppressRefError,
|
|
1098
|
-
suppressRefError = _ref6$suppressRefErro === void 0 ? false : _ref6$suppressRefErro;
|
|
1099
|
-
|
|
1083
|
+
this.getMenuProps = function (_temp5, _temp6) {
|
|
1084
|
+
let {
|
|
1085
|
+
refKey = 'ref',
|
|
1086
|
+
ref,
|
|
1087
|
+
...props
|
|
1088
|
+
} = _temp5 === void 0 ? {} : _temp5;
|
|
1089
|
+
let {
|
|
1090
|
+
suppressRefError = false
|
|
1091
|
+
} = _temp6 === void 0 ? {} : _temp6;
|
|
1100
1092
|
_this.getMenuProps.called = true;
|
|
1101
1093
|
_this.getMenuProps.refKey = refKey;
|
|
1102
1094
|
_this.getMenuProps.suppressRefError = suppressRefError;
|
|
1103
|
-
return
|
|
1095
|
+
return {
|
|
1096
|
+
[refKey]: handleRefs(ref, _this.menuRef),
|
|
1097
|
+
role: 'listbox',
|
|
1098
|
+
'aria-labelledby': props && props['aria-label'] ? null : _this.labelId,
|
|
1099
|
+
id: _this.menuId,
|
|
1100
|
+
...props
|
|
1101
|
+
};
|
|
1104
1102
|
};
|
|
1105
1103
|
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
undefined : requiredProp('getItemProps', 'item') : _ref7$item,
|
|
1119
|
-
rest = _objectWithoutPropertiesLoose(_ref7, _excluded5$1);
|
|
1104
|
+
this.getItemProps = function (_temp7) {
|
|
1105
|
+
let {
|
|
1106
|
+
onMouseMove,
|
|
1107
|
+
onMouseDown,
|
|
1108
|
+
onClick,
|
|
1109
|
+
onPress,
|
|
1110
|
+
index,
|
|
1111
|
+
item = process.env.NODE_ENV === 'production' ?
|
|
1112
|
+
/* istanbul ignore next */
|
|
1113
|
+
undefined : requiredProp('getItemProps', 'item'),
|
|
1114
|
+
...rest
|
|
1115
|
+
} = _temp7 === void 0 ? {} : _temp7;
|
|
1120
1116
|
|
|
1121
1117
|
if (index === undefined) {
|
|
1122
1118
|
_this.items.push(item);
|
|
@@ -1126,13 +1122,13 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1126
1122
|
_this.items[index] = item;
|
|
1127
1123
|
}
|
|
1128
1124
|
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1125
|
+
const onSelectKey = 'onClick';
|
|
1126
|
+
const customClickHandler = onClick;
|
|
1127
|
+
const enabledEventHandlers = {
|
|
1132
1128
|
// onMouseMove is used over onMouseEnter here. onMouseMove
|
|
1133
1129
|
// is only triggered on actual mouse movement while onMouseEnter
|
|
1134
1130
|
// can fire on DOM changes, interrupting keyboard navigation
|
|
1135
|
-
onMouseMove: callAllEventHandlers(onMouseMove,
|
|
1131
|
+
onMouseMove: callAllEventHandlers(onMouseMove, () => {
|
|
1136
1132
|
if (index === _this.getState().highlightedIndex) {
|
|
1137
1133
|
return;
|
|
1138
1134
|
}
|
|
@@ -1147,72 +1143,81 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1147
1143
|
|
|
1148
1144
|
_this.avoidScrolling = true;
|
|
1149
1145
|
|
|
1150
|
-
_this.internalSetTimeout(
|
|
1151
|
-
return _this.avoidScrolling = false;
|
|
1152
|
-
}, 250);
|
|
1146
|
+
_this.internalSetTimeout(() => _this.avoidScrolling = false, 250);
|
|
1153
1147
|
}),
|
|
1154
|
-
onMouseDown: callAllEventHandlers(onMouseDown,
|
|
1148
|
+
onMouseDown: callAllEventHandlers(onMouseDown, event => {
|
|
1155
1149
|
// This prevents the activeElement from being changed
|
|
1156
1150
|
// to the item so it can remain with the current activeElement
|
|
1157
1151
|
// which is a more common use case.
|
|
1158
1152
|
event.preventDefault();
|
|
1153
|
+
}),
|
|
1154
|
+
[onSelectKey]: callAllEventHandlers(customClickHandler, () => {
|
|
1155
|
+
_this.selectItemAtIndex(index, {
|
|
1156
|
+
type: clickItem
|
|
1157
|
+
});
|
|
1159
1158
|
})
|
|
1160
|
-
}
|
|
1161
|
-
_this.selectItemAtIndex(index, {
|
|
1162
|
-
type: clickItem
|
|
1163
|
-
});
|
|
1164
|
-
}), _enabledEventHandlers); // Passing down the onMouseDown handler to prevent redirect
|
|
1159
|
+
}; // Passing down the onMouseDown handler to prevent redirect
|
|
1165
1160
|
// of the activeElement if clicking on disabled items
|
|
1166
1161
|
|
|
1167
|
-
|
|
1162
|
+
const eventHandlers = rest.disabled ? {
|
|
1168
1163
|
onMouseDown: enabledEventHandlers.onMouseDown
|
|
1169
1164
|
} : enabledEventHandlers;
|
|
1170
|
-
return
|
|
1165
|
+
return {
|
|
1171
1166
|
id: _this.getItemId(index),
|
|
1172
1167
|
role: 'option',
|
|
1173
|
-
'aria-selected': _this.getState().highlightedIndex === index
|
|
1174
|
-
|
|
1168
|
+
'aria-selected': _this.getState().highlightedIndex === index,
|
|
1169
|
+
...eventHandlers,
|
|
1170
|
+
...rest
|
|
1171
|
+
};
|
|
1175
1172
|
};
|
|
1176
1173
|
|
|
1177
|
-
|
|
1178
|
-
|
|
1174
|
+
this.clearItems = () => {
|
|
1175
|
+
this.items = [];
|
|
1179
1176
|
};
|
|
1180
1177
|
|
|
1181
|
-
|
|
1178
|
+
this.reset = function (otherStateToSet, cb) {
|
|
1182
1179
|
if (otherStateToSet === void 0) {
|
|
1183
1180
|
otherStateToSet = {};
|
|
1184
1181
|
}
|
|
1185
1182
|
|
|
1186
1183
|
otherStateToSet = pickState(otherStateToSet);
|
|
1187
1184
|
|
|
1188
|
-
_this.internalSetState(
|
|
1189
|
-
|
|
1190
|
-
|
|
1185
|
+
_this.internalSetState(_ref => {
|
|
1186
|
+
let {
|
|
1187
|
+
selectedItem
|
|
1188
|
+
} = _ref;
|
|
1189
|
+
return {
|
|
1191
1190
|
isOpen: _this.props.defaultIsOpen,
|
|
1192
1191
|
highlightedIndex: _this.props.defaultHighlightedIndex,
|
|
1193
|
-
inputValue: _this.props.itemToString(selectedItem)
|
|
1194
|
-
|
|
1192
|
+
inputValue: _this.props.itemToString(selectedItem),
|
|
1193
|
+
...otherStateToSet
|
|
1194
|
+
};
|
|
1195
1195
|
}, cb);
|
|
1196
1196
|
};
|
|
1197
1197
|
|
|
1198
|
-
|
|
1198
|
+
this.toggleMenu = function (otherStateToSet, cb) {
|
|
1199
1199
|
if (otherStateToSet === void 0) {
|
|
1200
1200
|
otherStateToSet = {};
|
|
1201
1201
|
}
|
|
1202
1202
|
|
|
1203
1203
|
otherStateToSet = pickState(otherStateToSet);
|
|
1204
1204
|
|
|
1205
|
-
_this.internalSetState(
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1205
|
+
_this.internalSetState(_ref2 => {
|
|
1206
|
+
let {
|
|
1207
|
+
isOpen
|
|
1208
|
+
} = _ref2;
|
|
1209
|
+
return {
|
|
1210
|
+
isOpen: !isOpen,
|
|
1211
|
+
...(isOpen && {
|
|
1212
|
+
highlightedIndex: _this.props.defaultHighlightedIndex
|
|
1213
|
+
}),
|
|
1214
|
+
...otherStateToSet
|
|
1215
|
+
};
|
|
1216
|
+
}, () => {
|
|
1217
|
+
const {
|
|
1218
|
+
isOpen,
|
|
1219
|
+
highlightedIndex
|
|
1220
|
+
} = _this.getState();
|
|
1216
1221
|
|
|
1217
1222
|
if (isOpen) {
|
|
1218
1223
|
if (_this.getItemCount() > 0 && typeof highlightedIndex === 'number') {
|
|
@@ -1224,70 +1229,63 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1224
1229
|
});
|
|
1225
1230
|
};
|
|
1226
1231
|
|
|
1227
|
-
|
|
1228
|
-
|
|
1232
|
+
this.openMenu = cb => {
|
|
1233
|
+
this.internalSetState({
|
|
1229
1234
|
isOpen: true
|
|
1230
1235
|
}, cb);
|
|
1231
1236
|
};
|
|
1232
1237
|
|
|
1233
|
-
|
|
1234
|
-
|
|
1238
|
+
this.closeMenu = cb => {
|
|
1239
|
+
this.internalSetState({
|
|
1235
1240
|
isOpen: false
|
|
1236
1241
|
}, cb);
|
|
1237
1242
|
};
|
|
1238
1243
|
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
_this.previousResultCount = resultCount;
|
|
1254
|
-
setStatus(status, _this.props.environment.document);
|
|
1244
|
+
this.updateStatus = debounce(() => {
|
|
1245
|
+
const state = this.getState();
|
|
1246
|
+
const item = this.items[state.highlightedIndex];
|
|
1247
|
+
const resultCount = this.getItemCount();
|
|
1248
|
+
const status = this.props.getA11yStatusMessage({
|
|
1249
|
+
itemToString: this.props.itemToString,
|
|
1250
|
+
previousResultCount: this.previousResultCount,
|
|
1251
|
+
resultCount,
|
|
1252
|
+
highlightedItem: item,
|
|
1253
|
+
...state
|
|
1254
|
+
});
|
|
1255
|
+
this.previousResultCount = resultCount;
|
|
1256
|
+
setStatus(status, this.props.environment.document);
|
|
1255
1257
|
}, 200);
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
var _state = _this.getState({
|
|
1258
|
+
// fancy destructuring + defaults + aliases
|
|
1259
|
+
// this basically says each value of state should either be set to
|
|
1260
|
+
// the initial value or the default value if the initial value is not provided
|
|
1261
|
+
const {
|
|
1262
|
+
defaultHighlightedIndex,
|
|
1263
|
+
initialHighlightedIndex: _highlightedIndex = defaultHighlightedIndex,
|
|
1264
|
+
defaultIsOpen,
|
|
1265
|
+
initialIsOpen: _isOpen = defaultIsOpen,
|
|
1266
|
+
initialInputValue: _inputValue = '',
|
|
1267
|
+
initialSelectedItem: _selectedItem = null
|
|
1268
|
+
} = this.props;
|
|
1269
|
+
|
|
1270
|
+
const _state = this.getState({
|
|
1270
1271
|
highlightedIndex: _highlightedIndex,
|
|
1271
1272
|
isOpen: _isOpen,
|
|
1272
1273
|
inputValue: _inputValue,
|
|
1273
1274
|
selectedItem: _selectedItem
|
|
1274
1275
|
});
|
|
1275
1276
|
|
|
1276
|
-
if (_state.selectedItem != null &&
|
|
1277
|
-
_state.inputValue =
|
|
1277
|
+
if (_state.selectedItem != null && this.props.initialInputValue === undefined) {
|
|
1278
|
+
_state.inputValue = this.props.itemToString(_state.selectedItem);
|
|
1278
1279
|
}
|
|
1279
1280
|
|
|
1280
|
-
|
|
1281
|
-
return _this;
|
|
1281
|
+
this.state = _state;
|
|
1282
1282
|
}
|
|
1283
1283
|
|
|
1284
|
-
var _proto = Downshift.prototype;
|
|
1285
|
-
|
|
1286
1284
|
/**
|
|
1287
1285
|
* Clear all running timeouts
|
|
1288
1286
|
*/
|
|
1289
|
-
|
|
1290
|
-
this.timeoutIds.forEach(
|
|
1287
|
+
internalClearTimeouts() {
|
|
1288
|
+
this.timeoutIds.forEach(id => {
|
|
1291
1289
|
clearTimeout(id);
|
|
1292
1290
|
});
|
|
1293
1291
|
this.timeoutIds = [];
|
|
@@ -1301,22 +1299,22 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1301
1299
|
* @param {Object} stateToMerge defaults to this.state
|
|
1302
1300
|
* @return {Object} the state
|
|
1303
1301
|
*/
|
|
1304
|
-
;
|
|
1305
1302
|
|
|
1306
|
-
|
|
1303
|
+
|
|
1304
|
+
getState(stateToMerge) {
|
|
1307
1305
|
if (stateToMerge === void 0) {
|
|
1308
1306
|
stateToMerge = this.state;
|
|
1309
1307
|
}
|
|
1310
1308
|
|
|
1311
1309
|
return getState(stateToMerge, this.props);
|
|
1312
|
-
}
|
|
1310
|
+
}
|
|
1313
1311
|
|
|
1314
|
-
|
|
1312
|
+
getItemCount() {
|
|
1315
1313
|
// things read better this way. They're in priority order:
|
|
1316
1314
|
// 1. `this.itemCount`
|
|
1317
1315
|
// 2. `this.props.itemCount`
|
|
1318
1316
|
// 3. `this.items.length`
|
|
1319
|
-
|
|
1317
|
+
let itemCount = this.items.length;
|
|
1320
1318
|
|
|
1321
1319
|
if (this.itemCount != null) {
|
|
1322
1320
|
itemCount = this.itemCount;
|
|
@@ -1325,102 +1323,102 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1325
1323
|
}
|
|
1326
1324
|
|
|
1327
1325
|
return itemCount;
|
|
1328
|
-
}
|
|
1326
|
+
}
|
|
1329
1327
|
|
|
1330
|
-
|
|
1328
|
+
getItemNodeFromIndex(index) {
|
|
1331
1329
|
return this.props.environment.document.getElementById(this.getItemId(index));
|
|
1332
|
-
}
|
|
1330
|
+
}
|
|
1333
1331
|
|
|
1334
|
-
|
|
1332
|
+
scrollHighlightedItemIntoView() {
|
|
1335
1333
|
/* istanbul ignore else (react-native) */
|
|
1336
1334
|
{
|
|
1337
|
-
|
|
1335
|
+
const node = this.getItemNodeFromIndex(this.getState().highlightedIndex);
|
|
1338
1336
|
this.props.scrollIntoView(node, this._menuNode);
|
|
1339
1337
|
}
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
_proto.moveHighlightedIndex = function moveHighlightedIndex(amount, otherStateToSet) {
|
|
1343
|
-
var _this6 = this;
|
|
1344
|
-
|
|
1345
|
-
var itemCount = this.getItemCount();
|
|
1338
|
+
}
|
|
1346
1339
|
|
|
1347
|
-
|
|
1348
|
-
|
|
1340
|
+
moveHighlightedIndex(amount, otherStateToSet) {
|
|
1341
|
+
const itemCount = this.getItemCount();
|
|
1342
|
+
const {
|
|
1343
|
+
highlightedIndex
|
|
1344
|
+
} = this.getState();
|
|
1349
1345
|
|
|
1350
1346
|
if (itemCount > 0) {
|
|
1351
|
-
|
|
1352
|
-
return _this6.getItemNodeFromIndex(index);
|
|
1353
|
-
});
|
|
1347
|
+
const nextHighlightedIndex = getNextWrappingIndex(amount, highlightedIndex, itemCount, index => this.getItemNodeFromIndex(index));
|
|
1354
1348
|
this.setHighlightedIndex(nextHighlightedIndex, otherStateToSet);
|
|
1355
1349
|
}
|
|
1356
|
-
}
|
|
1350
|
+
}
|
|
1357
1351
|
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1352
|
+
getStateAndHelpers() {
|
|
1353
|
+
const {
|
|
1354
|
+
highlightedIndex,
|
|
1355
|
+
inputValue,
|
|
1356
|
+
selectedItem,
|
|
1357
|
+
isOpen
|
|
1358
|
+
} = this.getState();
|
|
1359
|
+
const {
|
|
1360
|
+
itemToString
|
|
1361
|
+
} = this.props;
|
|
1362
|
+
const {
|
|
1363
|
+
id
|
|
1364
|
+
} = this;
|
|
1365
|
+
const {
|
|
1366
|
+
getRootProps,
|
|
1367
|
+
getToggleButtonProps,
|
|
1368
|
+
getLabelProps,
|
|
1369
|
+
getMenuProps,
|
|
1370
|
+
getInputProps,
|
|
1371
|
+
getItemProps,
|
|
1372
|
+
openMenu,
|
|
1373
|
+
closeMenu,
|
|
1374
|
+
toggleMenu,
|
|
1375
|
+
selectItem,
|
|
1376
|
+
selectItemAtIndex,
|
|
1377
|
+
selectHighlightedItem,
|
|
1378
|
+
setHighlightedIndex,
|
|
1379
|
+
clearSelection,
|
|
1380
|
+
clearItems,
|
|
1381
|
+
reset,
|
|
1382
|
+
setItemCount,
|
|
1383
|
+
unsetItemCount,
|
|
1384
|
+
internalSetState: setState
|
|
1385
|
+
} = this;
|
|
1386
1386
|
return {
|
|
1387
1387
|
// prop getters
|
|
1388
|
-
getRootProps
|
|
1389
|
-
getToggleButtonProps
|
|
1390
|
-
getLabelProps
|
|
1391
|
-
getMenuProps
|
|
1392
|
-
getInputProps
|
|
1393
|
-
getItemProps
|
|
1388
|
+
getRootProps,
|
|
1389
|
+
getToggleButtonProps,
|
|
1390
|
+
getLabelProps,
|
|
1391
|
+
getMenuProps,
|
|
1392
|
+
getInputProps,
|
|
1393
|
+
getItemProps,
|
|
1394
1394
|
// actions
|
|
1395
|
-
reset
|
|
1396
|
-
openMenu
|
|
1397
|
-
closeMenu
|
|
1398
|
-
toggleMenu
|
|
1399
|
-
selectItem
|
|
1400
|
-
selectItemAtIndex
|
|
1401
|
-
selectHighlightedItem
|
|
1402
|
-
setHighlightedIndex
|
|
1403
|
-
clearSelection
|
|
1404
|
-
clearItems
|
|
1405
|
-
setItemCount
|
|
1406
|
-
unsetItemCount
|
|
1407
|
-
setState
|
|
1395
|
+
reset,
|
|
1396
|
+
openMenu,
|
|
1397
|
+
closeMenu,
|
|
1398
|
+
toggleMenu,
|
|
1399
|
+
selectItem,
|
|
1400
|
+
selectItemAtIndex,
|
|
1401
|
+
selectHighlightedItem,
|
|
1402
|
+
setHighlightedIndex,
|
|
1403
|
+
clearSelection,
|
|
1404
|
+
clearItems,
|
|
1405
|
+
setItemCount,
|
|
1406
|
+
unsetItemCount,
|
|
1407
|
+
setState,
|
|
1408
1408
|
// props
|
|
1409
|
-
itemToString
|
|
1409
|
+
itemToString,
|
|
1410
1410
|
// derived
|
|
1411
|
-
id
|
|
1411
|
+
id,
|
|
1412
1412
|
// state
|
|
1413
|
-
highlightedIndex
|
|
1414
|
-
inputValue
|
|
1415
|
-
isOpen
|
|
1416
|
-
selectedItem
|
|
1413
|
+
highlightedIndex,
|
|
1414
|
+
inputValue,
|
|
1415
|
+
isOpen,
|
|
1416
|
+
selectedItem
|
|
1417
1417
|
};
|
|
1418
1418
|
} //////////////////////////// ROOT
|
|
1419
|
-
;
|
|
1420
1419
|
|
|
1421
|
-
_proto.componentDidMount = function componentDidMount() {
|
|
1422
|
-
var _this7 = this;
|
|
1423
1420
|
|
|
1421
|
+
componentDidMount() {
|
|
1424
1422
|
/* istanbul ignore if (react-native) */
|
|
1425
1423
|
if (process.env.NODE_ENV !== 'production' && !false && this.getMenuProps.called && !this.getMenuProps.suppressRefError) {
|
|
1426
1424
|
validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
|
|
@@ -1434,22 +1432,20 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1434
1432
|
// down long enough for the list to disappear (because the blur event fires on the input)
|
|
1435
1433
|
// this.isMouseDown is used in the blur handler on the input to determine whether the blur event should
|
|
1436
1434
|
// trigger hiding the menu.
|
|
1437
|
-
|
|
1438
|
-
|
|
1435
|
+
const onMouseDown = () => {
|
|
1436
|
+
this.isMouseDown = true;
|
|
1439
1437
|
};
|
|
1440
1438
|
|
|
1441
|
-
|
|
1442
|
-
|
|
1439
|
+
const onMouseUp = event => {
|
|
1440
|
+
this.isMouseDown = false; // if the target element or the activeElement is within a downshift node
|
|
1443
1441
|
// then we don't want to reset downshift
|
|
1444
1442
|
|
|
1445
|
-
|
|
1443
|
+
const contextWithinDownshift = targetWithinDownshift(event.target, [this._rootNode, this._menuNode], this.props.environment);
|
|
1446
1444
|
|
|
1447
|
-
if (!contextWithinDownshift &&
|
|
1448
|
-
|
|
1445
|
+
if (!contextWithinDownshift && this.getState().isOpen) {
|
|
1446
|
+
this.reset({
|
|
1449
1447
|
type: mouseUp
|
|
1450
|
-
},
|
|
1451
|
-
return _this7.props.onOuterClick(_this7.getStateAndHelpers());
|
|
1452
|
-
});
|
|
1448
|
+
}, () => this.props.onOuterClick(this.getStateAndHelpers()));
|
|
1453
1449
|
}
|
|
1454
1450
|
}; // Touching an element in iOS gives focus and hover states, but touching out of
|
|
1455
1451
|
// the element will remove hover, and persist the focus state, resulting in the
|
|
@@ -1459,38 +1455,36 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1459
1455
|
// but not if the user is swiping
|
|
1460
1456
|
|
|
1461
1457
|
|
|
1462
|
-
|
|
1463
|
-
|
|
1458
|
+
const onTouchStart = () => {
|
|
1459
|
+
this.isTouchMove = false;
|
|
1464
1460
|
};
|
|
1465
1461
|
|
|
1466
|
-
|
|
1467
|
-
|
|
1462
|
+
const onTouchMove = () => {
|
|
1463
|
+
this.isTouchMove = true;
|
|
1468
1464
|
};
|
|
1469
1465
|
|
|
1470
|
-
|
|
1471
|
-
|
|
1466
|
+
const onTouchEnd = event => {
|
|
1467
|
+
const contextWithinDownshift = targetWithinDownshift(event.target, [this._rootNode, this._menuNode], this.props.environment, false);
|
|
1472
1468
|
|
|
1473
|
-
if (!
|
|
1474
|
-
|
|
1469
|
+
if (!this.isTouchMove && !contextWithinDownshift && this.getState().isOpen) {
|
|
1470
|
+
this.reset({
|
|
1475
1471
|
type: touchEnd
|
|
1476
|
-
},
|
|
1477
|
-
return _this7.props.onOuterClick(_this7.getStateAndHelpers());
|
|
1478
|
-
});
|
|
1472
|
+
}, () => this.props.onOuterClick(this.getStateAndHelpers()));
|
|
1479
1473
|
}
|
|
1480
1474
|
};
|
|
1481
1475
|
|
|
1482
|
-
|
|
1476
|
+
const {
|
|
1477
|
+
environment
|
|
1478
|
+
} = this.props;
|
|
1483
1479
|
environment.addEventListener('mousedown', onMouseDown);
|
|
1484
1480
|
environment.addEventListener('mouseup', onMouseUp);
|
|
1485
1481
|
environment.addEventListener('touchstart', onTouchStart);
|
|
1486
1482
|
environment.addEventListener('touchmove', onTouchMove);
|
|
1487
1483
|
environment.addEventListener('touchend', onTouchEnd);
|
|
1488
1484
|
|
|
1489
|
-
this.cleanup =
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
_this7.updateStatus.cancel();
|
|
1493
|
-
|
|
1485
|
+
this.cleanup = () => {
|
|
1486
|
+
this.internalClearTimeouts();
|
|
1487
|
+
this.updateStatus.cancel();
|
|
1494
1488
|
environment.removeEventListener('mousedown', onMouseDown);
|
|
1495
1489
|
environment.removeEventListener('mouseup', onMouseUp);
|
|
1496
1490
|
environment.removeEventListener('touchstart', onTouchStart);
|
|
@@ -1498,21 +1492,21 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1498
1492
|
environment.removeEventListener('touchend', onTouchEnd);
|
|
1499
1493
|
};
|
|
1500
1494
|
}
|
|
1501
|
-
}
|
|
1502
|
-
|
|
1503
|
-
_proto.shouldScroll = function shouldScroll(prevState, prevProps) {
|
|
1504
|
-
var _ref10 = this.props.highlightedIndex === undefined ? this.getState() : this.props,
|
|
1505
|
-
currentHighlightedIndex = _ref10.highlightedIndex;
|
|
1506
|
-
|
|
1507
|
-
var _ref11 = prevProps.highlightedIndex === undefined ? prevState : prevProps,
|
|
1508
|
-
prevHighlightedIndex = _ref11.highlightedIndex;
|
|
1495
|
+
}
|
|
1509
1496
|
|
|
1510
|
-
|
|
1511
|
-
|
|
1497
|
+
shouldScroll(prevState, prevProps) {
|
|
1498
|
+
const {
|
|
1499
|
+
highlightedIndex: currentHighlightedIndex
|
|
1500
|
+
} = this.props.highlightedIndex === undefined ? this.getState() : this.props;
|
|
1501
|
+
const {
|
|
1502
|
+
highlightedIndex: prevHighlightedIndex
|
|
1503
|
+
} = prevProps.highlightedIndex === undefined ? prevState : prevProps;
|
|
1504
|
+
const scrollWhenOpen = currentHighlightedIndex && this.getState().isOpen && !prevState.isOpen;
|
|
1505
|
+
const scrollWhenNavigating = currentHighlightedIndex !== prevHighlightedIndex;
|
|
1512
1506
|
return scrollWhenOpen || scrollWhenNavigating;
|
|
1513
|
-
}
|
|
1507
|
+
}
|
|
1514
1508
|
|
|
1515
|
-
|
|
1509
|
+
componentDidUpdate(prevProps, prevState) {
|
|
1516
1510
|
if (process.env.NODE_ENV !== 'production') {
|
|
1517
1511
|
validateControlledUnchanged(this.state, prevProps, this.props);
|
|
1518
1512
|
/* istanbul ignore if (react-native) */
|
|
@@ -1538,14 +1532,14 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1538
1532
|
{
|
|
1539
1533
|
this.updateStatus();
|
|
1540
1534
|
}
|
|
1541
|
-
}
|
|
1535
|
+
}
|
|
1542
1536
|
|
|
1543
|
-
|
|
1537
|
+
componentWillUnmount() {
|
|
1544
1538
|
this.cleanup(); // avoids memory leak
|
|
1545
|
-
}
|
|
1539
|
+
}
|
|
1546
1540
|
|
|
1547
|
-
|
|
1548
|
-
|
|
1541
|
+
render() {
|
|
1542
|
+
const children = unwrapArray(this.props.children, noop); // because the items are rerendered every time we call the children
|
|
1549
1543
|
// we clear this out each render and it will be populated again as
|
|
1550
1544
|
// getItemProps is called.
|
|
1551
1545
|
|
|
@@ -1565,7 +1559,7 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1565
1559
|
this.getLabelProps.called = false; // and something similar for getInputProps
|
|
1566
1560
|
|
|
1567
1561
|
this.getInputProps.called = false;
|
|
1568
|
-
|
|
1562
|
+
const element = unwrapArray(children(this.getStateAndHelpers()));
|
|
1569
1563
|
|
|
1570
1564
|
if (!element) {
|
|
1571
1565
|
return null;
|
|
@@ -1594,16 +1588,15 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1594
1588
|
|
|
1595
1589
|
|
|
1596
1590
|
return undefined;
|
|
1597
|
-
}
|
|
1591
|
+
}
|
|
1598
1592
|
|
|
1599
|
-
|
|
1600
|
-
}(Component);
|
|
1593
|
+
}
|
|
1601
1594
|
|
|
1602
1595
|
Downshift.defaultProps = {
|
|
1603
1596
|
defaultHighlightedIndex: null,
|
|
1604
1597
|
defaultIsOpen: false,
|
|
1605
1598
|
getA11yStatusMessage: getA11yStatusMessage$1,
|
|
1606
|
-
itemToString:
|
|
1599
|
+
itemToString: i => {
|
|
1607
1600
|
if (i == null) {
|
|
1608
1601
|
return '';
|
|
1609
1602
|
}
|
|
@@ -1621,26 +1614,24 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1621
1614
|
onChange: noop,
|
|
1622
1615
|
onSelect: noop,
|
|
1623
1616
|
onOuterClick: noop,
|
|
1624
|
-
selectedItemChanged:
|
|
1625
|
-
return prevItem !== item;
|
|
1626
|
-
},
|
|
1617
|
+
selectedItemChanged: (prevItem, item) => prevItem !== item,
|
|
1627
1618
|
environment:
|
|
1628
1619
|
/* istanbul ignore next (ssr) */
|
|
1629
1620
|
typeof window === 'undefined' ? {} : window,
|
|
1630
|
-
stateReducer:
|
|
1631
|
-
return stateToSet;
|
|
1632
|
-
},
|
|
1621
|
+
stateReducer: (state, stateToSet) => stateToSet,
|
|
1633
1622
|
suppressRefError: false,
|
|
1634
|
-
scrollIntoView
|
|
1623
|
+
scrollIntoView
|
|
1635
1624
|
};
|
|
1636
1625
|
Downshift.stateChangeTypes = stateChangeTypes$3;
|
|
1637
1626
|
return Downshift;
|
|
1638
|
-
}();
|
|
1627
|
+
})();
|
|
1639
1628
|
|
|
1640
1629
|
var Downshift$1 = Downshift;
|
|
1641
1630
|
|
|
1642
|
-
function validateGetMenuPropsCalledCorrectly(node,
|
|
1643
|
-
|
|
1631
|
+
function validateGetMenuPropsCalledCorrectly(node, _ref3) {
|
|
1632
|
+
let {
|
|
1633
|
+
refKey
|
|
1634
|
+
} = _ref3;
|
|
1644
1635
|
|
|
1645
1636
|
if (!node) {
|
|
1646
1637
|
// eslint-disable-next-line no-console
|
|
@@ -1648,10 +1639,12 @@ function validateGetMenuPropsCalledCorrectly(node, _ref12) {
|
|
|
1648
1639
|
}
|
|
1649
1640
|
}
|
|
1650
1641
|
|
|
1651
|
-
function validateGetRootPropsCalledCorrectly(element,
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1642
|
+
function validateGetRootPropsCalledCorrectly(element, _ref4) {
|
|
1643
|
+
let {
|
|
1644
|
+
refKey
|
|
1645
|
+
} = _ref4;
|
|
1646
|
+
const refKeySpecified = refKey !== 'ref';
|
|
1647
|
+
const isComposite = !isDOMElement(element);
|
|
1655
1648
|
|
|
1656
1649
|
if (isComposite && !refKeySpecified && !isForwardRef(element)) {
|
|
1657
1650
|
// eslint-disable-next-line no-console
|
|
@@ -1667,8 +1660,7 @@ function validateGetRootPropsCalledCorrectly(element, _ref13) {
|
|
|
1667
1660
|
}
|
|
1668
1661
|
}
|
|
1669
1662
|
|
|
1670
|
-
|
|
1671
|
-
var dropdownDefaultStateValues = {
|
|
1663
|
+
const dropdownDefaultStateValues = {
|
|
1672
1664
|
highlightedIndex: -1,
|
|
1673
1665
|
isOpen: false,
|
|
1674
1666
|
selectedItem: null,
|
|
@@ -1676,10 +1668,12 @@ var dropdownDefaultStateValues = {
|
|
|
1676
1668
|
};
|
|
1677
1669
|
|
|
1678
1670
|
function callOnChangeProps(action, state, newState) {
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1671
|
+
const {
|
|
1672
|
+
props,
|
|
1673
|
+
type
|
|
1674
|
+
} = action;
|
|
1675
|
+
const changes = {};
|
|
1676
|
+
Object.keys(state).forEach(key => {
|
|
1683
1677
|
invokeOnChangeHandler(key, action, state, newState);
|
|
1684
1678
|
|
|
1685
1679
|
if (newState[key] !== state[key]) {
|
|
@@ -1688,21 +1682,25 @@ function callOnChangeProps(action, state, newState) {
|
|
|
1688
1682
|
});
|
|
1689
1683
|
|
|
1690
1684
|
if (props.onStateChange && Object.keys(changes).length) {
|
|
1691
|
-
props.onStateChange(
|
|
1692
|
-
type
|
|
1693
|
-
|
|
1685
|
+
props.onStateChange({
|
|
1686
|
+
type,
|
|
1687
|
+
...changes
|
|
1688
|
+
});
|
|
1694
1689
|
}
|
|
1695
1690
|
}
|
|
1696
1691
|
|
|
1697
1692
|
function invokeOnChangeHandler(key, action, state, newState) {
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1693
|
+
const {
|
|
1694
|
+
props,
|
|
1695
|
+
type
|
|
1696
|
+
} = action;
|
|
1697
|
+
const handler = "on" + capitalizeString(key) + "Change";
|
|
1701
1698
|
|
|
1702
1699
|
if (props[handler] && newState[key] !== undefined && newState[key] !== state[key]) {
|
|
1703
|
-
props[handler](
|
|
1704
|
-
type
|
|
1705
|
-
|
|
1700
|
+
props[handler]({
|
|
1701
|
+
type,
|
|
1702
|
+
...newState
|
|
1703
|
+
});
|
|
1706
1704
|
}
|
|
1707
1705
|
}
|
|
1708
1706
|
/**
|
|
@@ -1726,8 +1724,10 @@ function stateReducer(s, a) {
|
|
|
1726
1724
|
|
|
1727
1725
|
|
|
1728
1726
|
function getA11ySelectionMessage(selectionParameters) {
|
|
1729
|
-
|
|
1730
|
-
|
|
1727
|
+
const {
|
|
1728
|
+
selectedItem,
|
|
1729
|
+
itemToString: itemToStringLocal
|
|
1730
|
+
} = selectionParameters;
|
|
1731
1731
|
return selectedItem ? itemToStringLocal(selectedItem) + " has been selected." : '';
|
|
1732
1732
|
}
|
|
1733
1733
|
/**
|
|
@@ -1735,26 +1735,25 @@ function getA11ySelectionMessage(selectionParameters) {
|
|
|
1735
1735
|
*/
|
|
1736
1736
|
|
|
1737
1737
|
|
|
1738
|
-
|
|
1738
|
+
const updateA11yStatus = debounce((getA11yMessage, document) => {
|
|
1739
1739
|
setStatus(getA11yMessage(), document);
|
|
1740
1740
|
}, 200); // istanbul ignore next
|
|
1741
1741
|
|
|
1742
|
-
|
|
1742
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined' ? useLayoutEffect : useEffect;
|
|
1743
1743
|
|
|
1744
1744
|
function useElementIds(_ref) {
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1745
|
+
let {
|
|
1746
|
+
id = "downshift-" + generateId(),
|
|
1747
|
+
labelId,
|
|
1748
|
+
menuId,
|
|
1749
|
+
getItemId,
|
|
1750
|
+
toggleButtonId,
|
|
1751
|
+
inputId
|
|
1752
|
+
} = _ref;
|
|
1753
|
+
const elementIdsRef = useRef({
|
|
1753
1754
|
labelId: labelId || id + "-label",
|
|
1754
1755
|
menuId: menuId || id + "-menu",
|
|
1755
|
-
getItemId: getItemId ||
|
|
1756
|
-
return id + "-item-" + index;
|
|
1757
|
-
},
|
|
1756
|
+
getItemId: getItemId || (index => id + "-item-" + index),
|
|
1758
1757
|
toggleButtonId: toggleButtonId || id + "-toggle-button",
|
|
1759
1758
|
inputId: inputId || id + "-input"
|
|
1760
1759
|
});
|
|
@@ -1786,7 +1785,7 @@ function capitalizeString(string) {
|
|
|
1786
1785
|
}
|
|
1787
1786
|
|
|
1788
1787
|
function useLatestRef(val) {
|
|
1789
|
-
|
|
1788
|
+
const ref = useRef(val); // technically this is not "concurrent mode safe" because we're manipulating
|
|
1790
1789
|
// the value during render (so it's not idempotent). However, the places this
|
|
1791
1790
|
// hook is used is to support memoizing callbacks which will be called
|
|
1792
1791
|
// *during* render, so we need the latest values *during* render.
|
|
@@ -1808,30 +1807,25 @@ function useLatestRef(val) {
|
|
|
1808
1807
|
|
|
1809
1808
|
|
|
1810
1809
|
function useEnhancedReducer(reducer, initialState, props) {
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1810
|
+
const prevStateRef = useRef();
|
|
1811
|
+
const actionRef = useRef();
|
|
1812
|
+
const enhancedReducer = useCallback((state, action) => {
|
|
1814
1813
|
actionRef.current = action;
|
|
1815
1814
|
state = getState(state, action.props);
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
changes
|
|
1819
|
-
})
|
|
1815
|
+
const changes = reducer(state, action);
|
|
1816
|
+
const newState = action.props.stateReducer(state, { ...action,
|
|
1817
|
+
changes
|
|
1818
|
+
});
|
|
1820
1819
|
return newState;
|
|
1821
1820
|
}, [reducer]);
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
props: propsRef.current
|
|
1831
|
-
}, action));
|
|
1832
|
-
}, [propsRef]);
|
|
1833
|
-
var action = actionRef.current;
|
|
1834
|
-
useEffect(function () {
|
|
1821
|
+
const [state, dispatch] = useReducer(enhancedReducer, initialState);
|
|
1822
|
+
const propsRef = useLatestRef(props);
|
|
1823
|
+
const dispatchWithProps = useCallback(action => dispatch({
|
|
1824
|
+
props: propsRef.current,
|
|
1825
|
+
...action
|
|
1826
|
+
}), [propsRef]);
|
|
1827
|
+
const action = actionRef.current;
|
|
1828
|
+
useEffect(() => {
|
|
1835
1829
|
if (action && prevStateRef.current && prevStateRef.current !== state) {
|
|
1836
1830
|
callOnChangeProps(action, getState(prevStateRef.current, action.props), state);
|
|
1837
1831
|
}
|
|
@@ -1852,18 +1846,15 @@ function useEnhancedReducer(reducer, initialState, props) {
|
|
|
1852
1846
|
|
|
1853
1847
|
|
|
1854
1848
|
function useControlledReducer$1(reducer, initialState, props) {
|
|
1855
|
-
|
|
1856
|
-
state = _useEnhancedReducer[0],
|
|
1857
|
-
dispatch = _useEnhancedReducer[1];
|
|
1858
|
-
|
|
1849
|
+
const [state, dispatch] = useEnhancedReducer(reducer, initialState, props);
|
|
1859
1850
|
return [getState(state, props), dispatch];
|
|
1860
1851
|
}
|
|
1861
1852
|
|
|
1862
|
-
|
|
1863
|
-
itemToString
|
|
1864
|
-
stateReducer
|
|
1865
|
-
getA11ySelectionMessage
|
|
1866
|
-
scrollIntoView
|
|
1853
|
+
const defaultProps$3 = {
|
|
1854
|
+
itemToString,
|
|
1855
|
+
stateReducer,
|
|
1856
|
+
getA11ySelectionMessage,
|
|
1857
|
+
scrollIntoView,
|
|
1867
1858
|
circularNavigation: false,
|
|
1868
1859
|
environment:
|
|
1869
1860
|
/* istanbul ignore next (ssr) */
|
|
@@ -1875,10 +1866,10 @@ function getDefaultValue$1(props, propKey, defaultStateValues) {
|
|
|
1875
1866
|
defaultStateValues = dropdownDefaultStateValues;
|
|
1876
1867
|
}
|
|
1877
1868
|
|
|
1878
|
-
|
|
1869
|
+
const defaultValue = props["default" + capitalizeString(propKey)];
|
|
1879
1870
|
|
|
1880
|
-
if (
|
|
1881
|
-
return
|
|
1871
|
+
if (defaultValue !== undefined) {
|
|
1872
|
+
return defaultValue;
|
|
1882
1873
|
}
|
|
1883
1874
|
|
|
1884
1875
|
return defaultStateValues[propKey];
|
|
@@ -1889,38 +1880,44 @@ function getInitialValue$1(props, propKey, defaultStateValues) {
|
|
|
1889
1880
|
defaultStateValues = dropdownDefaultStateValues;
|
|
1890
1881
|
}
|
|
1891
1882
|
|
|
1892
|
-
|
|
1893
|
-
|
|
1883
|
+
const value = props[propKey];
|
|
1884
|
+
|
|
1885
|
+
if (value !== undefined) {
|
|
1886
|
+
return value;
|
|
1894
1887
|
}
|
|
1895
1888
|
|
|
1896
|
-
|
|
1889
|
+
const initialValue = props["initial" + capitalizeString(propKey)];
|
|
1897
1890
|
|
|
1898
|
-
if (
|
|
1899
|
-
return
|
|
1891
|
+
if (initialValue !== undefined) {
|
|
1892
|
+
return initialValue;
|
|
1900
1893
|
}
|
|
1901
1894
|
|
|
1902
1895
|
return getDefaultValue$1(props, propKey, defaultStateValues);
|
|
1903
1896
|
}
|
|
1904
1897
|
|
|
1905
1898
|
function getInitialState$2(props) {
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1899
|
+
const selectedItem = getInitialValue$1(props, 'selectedItem');
|
|
1900
|
+
const isOpen = getInitialValue$1(props, 'isOpen');
|
|
1901
|
+
const highlightedIndex = getInitialValue$1(props, 'highlightedIndex');
|
|
1902
|
+
const inputValue = getInitialValue$1(props, 'inputValue');
|
|
1910
1903
|
return {
|
|
1911
1904
|
highlightedIndex: highlightedIndex < 0 && selectedItem && isOpen ? props.items.indexOf(selectedItem) : highlightedIndex,
|
|
1912
|
-
isOpen
|
|
1913
|
-
selectedItem
|
|
1914
|
-
inputValue
|
|
1905
|
+
isOpen,
|
|
1906
|
+
selectedItem,
|
|
1907
|
+
inputValue
|
|
1915
1908
|
};
|
|
1916
1909
|
}
|
|
1917
1910
|
|
|
1918
1911
|
function getHighlightedIndexOnOpen(props, state, offset, getItemNodeFromIndex) {
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1912
|
+
const {
|
|
1913
|
+
items,
|
|
1914
|
+
initialHighlightedIndex,
|
|
1915
|
+
defaultHighlightedIndex
|
|
1916
|
+
} = props;
|
|
1917
|
+
const {
|
|
1918
|
+
selectedItem,
|
|
1919
|
+
highlightedIndex
|
|
1920
|
+
} = state;
|
|
1924
1921
|
|
|
1925
1922
|
if (items.length === 0) {
|
|
1926
1923
|
return -1;
|
|
@@ -1961,39 +1958,35 @@ function getHighlightedIndexOnOpen(props, state, offset, getItemNodeFromIndex) {
|
|
|
1961
1958
|
|
|
1962
1959
|
|
|
1963
1960
|
function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, handleBlur) {
|
|
1964
|
-
|
|
1961
|
+
const mouseAndTouchTrackersRef = useRef({
|
|
1965
1962
|
isMouseDown: false,
|
|
1966
1963
|
isTouchMove: false
|
|
1967
1964
|
});
|
|
1968
|
-
useEffect(
|
|
1965
|
+
useEffect(() => {
|
|
1969
1966
|
// The same strategy for checking if a click occurred inside or outside downsift
|
|
1970
1967
|
// as in downshift.js.
|
|
1971
|
-
|
|
1968
|
+
const onMouseDown = () => {
|
|
1972
1969
|
mouseAndTouchTrackersRef.current.isMouseDown = true;
|
|
1973
1970
|
};
|
|
1974
1971
|
|
|
1975
|
-
|
|
1972
|
+
const onMouseUp = event => {
|
|
1976
1973
|
mouseAndTouchTrackersRef.current.isMouseDown = false;
|
|
1977
1974
|
|
|
1978
|
-
if (isOpen && !targetWithinDownshift(event.target, downshiftElementRefs.map(
|
|
1979
|
-
return ref.current;
|
|
1980
|
-
}), environment)) {
|
|
1975
|
+
if (isOpen && !targetWithinDownshift(event.target, downshiftElementRefs.map(ref => ref.current), environment)) {
|
|
1981
1976
|
handleBlur();
|
|
1982
1977
|
}
|
|
1983
1978
|
};
|
|
1984
1979
|
|
|
1985
|
-
|
|
1980
|
+
const onTouchStart = () => {
|
|
1986
1981
|
mouseAndTouchTrackersRef.current.isTouchMove = false;
|
|
1987
1982
|
};
|
|
1988
1983
|
|
|
1989
|
-
|
|
1984
|
+
const onTouchMove = () => {
|
|
1990
1985
|
mouseAndTouchTrackersRef.current.isTouchMove = true;
|
|
1991
1986
|
};
|
|
1992
1987
|
|
|
1993
|
-
|
|
1994
|
-
if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, downshiftElementRefs.map(
|
|
1995
|
-
return ref.current;
|
|
1996
|
-
}), environment, false)) {
|
|
1988
|
+
const onTouchEnd = event => {
|
|
1989
|
+
if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, downshiftElementRefs.map(ref => ref.current), environment, false)) {
|
|
1997
1990
|
handleBlur();
|
|
1998
1991
|
}
|
|
1999
1992
|
};
|
|
@@ -2017,9 +2010,7 @@ function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, hand
|
|
|
2017
2010
|
// eslint-disable-next-line import/no-mutable-exports
|
|
2018
2011
|
|
|
2019
2012
|
|
|
2020
|
-
|
|
2021
|
-
return noop;
|
|
2022
|
-
};
|
|
2013
|
+
let useGetterPropsCalledChecker = () => noop;
|
|
2023
2014
|
/**
|
|
2024
2015
|
* Custom hook that checks if getter props are called correctly.
|
|
2025
2016
|
*
|
|
@@ -2031,20 +2022,20 @@ var useGetterPropsCalledChecker = function useGetterPropsCalledChecker() {
|
|
|
2031
2022
|
|
|
2032
2023
|
|
|
2033
2024
|
if (process.env.NODE_ENV !== 'production') {
|
|
2034
|
-
useGetterPropsCalledChecker = function
|
|
2035
|
-
|
|
2025
|
+
useGetterPropsCalledChecker = function () {
|
|
2026
|
+
const isInitialMountRef = useRef(true);
|
|
2036
2027
|
|
|
2037
2028
|
for (var _len = arguments.length, propKeys = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2038
2029
|
propKeys[_key] = arguments[_key];
|
|
2039
2030
|
}
|
|
2040
2031
|
|
|
2041
|
-
|
|
2032
|
+
const getterPropsCalledRef = useRef(propKeys.reduce((acc, propKey) => {
|
|
2042
2033
|
acc[propKey] = {};
|
|
2043
2034
|
return acc;
|
|
2044
2035
|
}, {}));
|
|
2045
|
-
useEffect(
|
|
2046
|
-
Object.keys(getterPropsCalledRef.current).forEach(
|
|
2047
|
-
|
|
2036
|
+
useEffect(() => {
|
|
2037
|
+
Object.keys(getterPropsCalledRef.current).forEach(propKey => {
|
|
2038
|
+
const propCallInfo = getterPropsCalledRef.current[propKey];
|
|
2048
2039
|
|
|
2049
2040
|
if (isInitialMountRef.current) {
|
|
2050
2041
|
if (!Object.keys(propCallInfo).length) {
|
|
@@ -2054,9 +2045,11 @@ if (process.env.NODE_ENV !== 'production') {
|
|
|
2054
2045
|
}
|
|
2055
2046
|
}
|
|
2056
2047
|
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2048
|
+
const {
|
|
2049
|
+
suppressRefError,
|
|
2050
|
+
refKey,
|
|
2051
|
+
elementRef
|
|
2052
|
+
} = propCallInfo;
|
|
2060
2053
|
|
|
2061
2054
|
if ((!elementRef || !elementRef.current) && !suppressRefError) {
|
|
2062
2055
|
// eslint-disable-next-line no-console
|
|
@@ -2065,11 +2058,11 @@ if (process.env.NODE_ENV !== 'production') {
|
|
|
2065
2058
|
});
|
|
2066
2059
|
isInitialMountRef.current = false;
|
|
2067
2060
|
});
|
|
2068
|
-
|
|
2061
|
+
const setGetterPropCallInfo = useCallback((propKey, suppressRefError, refKey, elementRef) => {
|
|
2069
2062
|
getterPropsCalledRef.current[propKey] = {
|
|
2070
|
-
suppressRefError
|
|
2071
|
-
refKey
|
|
2072
|
-
elementRef
|
|
2063
|
+
suppressRefError,
|
|
2064
|
+
refKey,
|
|
2065
|
+
elementRef
|
|
2073
2066
|
};
|
|
2074
2067
|
}, []);
|
|
2075
2068
|
return setGetterPropCallInfo;
|
|
@@ -2077,39 +2070,41 @@ if (process.env.NODE_ENV !== 'production') {
|
|
|
2077
2070
|
}
|
|
2078
2071
|
|
|
2079
2072
|
function useA11yMessageSetter(getA11yMessage, dependencyArray, _ref2) {
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2073
|
+
let {
|
|
2074
|
+
isInitialMount,
|
|
2075
|
+
highlightedIndex,
|
|
2076
|
+
items,
|
|
2077
|
+
environment,
|
|
2078
|
+
...rest
|
|
2079
|
+
} = _ref2;
|
|
2086
2080
|
// Sets a11y status message on changes in state.
|
|
2087
|
-
useEffect(
|
|
2081
|
+
useEffect(() => {
|
|
2088
2082
|
if (isInitialMount || false) {
|
|
2089
2083
|
return;
|
|
2090
2084
|
}
|
|
2091
2085
|
|
|
2092
|
-
updateA11yStatus(
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
}, environment.document); // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2086
|
+
updateA11yStatus(() => getA11yMessage({
|
|
2087
|
+
highlightedIndex,
|
|
2088
|
+
highlightedItem: items[highlightedIndex],
|
|
2089
|
+
resultCount: items.length,
|
|
2090
|
+
...rest
|
|
2091
|
+
}), environment.document); // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2099
2092
|
}, dependencyArray);
|
|
2100
2093
|
}
|
|
2101
2094
|
|
|
2102
2095
|
function useScrollIntoView(_ref3) {
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2096
|
+
let {
|
|
2097
|
+
highlightedIndex,
|
|
2098
|
+
isOpen,
|
|
2099
|
+
itemRefs,
|
|
2100
|
+
getItemNodeFromIndex,
|
|
2101
|
+
menuElement,
|
|
2102
|
+
scrollIntoView: scrollIntoViewProp
|
|
2103
|
+
} = _ref3;
|
|
2109
2104
|
// used not to scroll on highlight by mouse.
|
|
2110
|
-
|
|
2105
|
+
const shouldScrollRef = useRef(true); // Scroll on highlighted item if change comes from keyboard.
|
|
2111
2106
|
|
|
2112
|
-
useIsomorphicLayoutEffect(
|
|
2107
|
+
useIsomorphicLayoutEffect(() => {
|
|
2113
2108
|
if (highlightedIndex < 0 || !isOpen || !Object.keys(itemRefs.current).length) {
|
|
2114
2109
|
return;
|
|
2115
2110
|
}
|
|
@@ -2125,17 +2120,19 @@ function useScrollIntoView(_ref3) {
|
|
|
2125
2120
|
} // eslint-disable-next-line import/no-mutable-exports
|
|
2126
2121
|
|
|
2127
2122
|
|
|
2128
|
-
|
|
2123
|
+
let useControlPropsValidator = noop;
|
|
2129
2124
|
/* istanbul ignore next */
|
|
2130
2125
|
|
|
2131
2126
|
if (process.env.NODE_ENV !== 'production') {
|
|
2132
|
-
useControlPropsValidator =
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2127
|
+
useControlPropsValidator = _ref4 => {
|
|
2128
|
+
let {
|
|
2129
|
+
isInitialMount,
|
|
2130
|
+
props,
|
|
2131
|
+
state
|
|
2132
|
+
} = _ref4;
|
|
2136
2133
|
// used for checking when props are moving from controlled to uncontrolled.
|
|
2137
|
-
|
|
2138
|
-
useEffect(
|
|
2134
|
+
const prevPropsRef = useRef(props);
|
|
2135
|
+
useEffect(() => {
|
|
2139
2136
|
if (isInitialMount) {
|
|
2140
2137
|
return;
|
|
2141
2138
|
}
|
|
@@ -2149,9 +2146,11 @@ if (process.env.NODE_ENV !== 'production') {
|
|
|
2149
2146
|
/* eslint-disable complexity */
|
|
2150
2147
|
|
|
2151
2148
|
function downshiftCommonReducer(state, action, stateChangeTypes) {
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2149
|
+
const {
|
|
2150
|
+
type,
|
|
2151
|
+
props
|
|
2152
|
+
} = action;
|
|
2153
|
+
let changes;
|
|
2155
2154
|
|
|
2156
2155
|
switch (type) {
|
|
2157
2156
|
case stateChangeTypes.ItemMouseMove:
|
|
@@ -2212,7 +2211,9 @@ function downshiftCommonReducer(state, action, stateChangeTypes) {
|
|
|
2212
2211
|
throw new Error('Reducer called without proper action type.');
|
|
2213
2212
|
}
|
|
2214
2213
|
|
|
2215
|
-
return
|
|
2214
|
+
return { ...state,
|
|
2215
|
+
...changes
|
|
2216
|
+
};
|
|
2216
2217
|
}
|
|
2217
2218
|
/* eslint-enable complexity */
|
|
2218
2219
|
|
|
@@ -2286,7 +2287,7 @@ function getA11yStatusMessage(_a) {
|
|
|
2286
2287
|
return 'No results are available.';
|
|
2287
2288
|
}
|
|
2288
2289
|
if (resultCount !== previousResultCount) {
|
|
2289
|
-
return resultCount
|
|
2290
|
+
return "".concat(resultCount, " result").concat(resultCount === 1 ? ' is' : 's are', " available, use up and down arrow keys to navigate. Press Enter or Space Bar keys to select.");
|
|
2290
2291
|
}
|
|
2291
2292
|
return '';
|
|
2292
2293
|
}
|
|
@@ -2300,29 +2301,29 @@ if (process.env.NODE_ENV !== 'production') {
|
|
|
2300
2301
|
};
|
|
2301
2302
|
}
|
|
2302
2303
|
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2304
|
+
const MenuKeyDownArrowDown = process.env.NODE_ENV !== "production" ? '__menu_keydown_arrow_down__' : 0;
|
|
2305
|
+
const MenuKeyDownArrowUp = process.env.NODE_ENV !== "production" ? '__menu_keydown_arrow_up__' : 1;
|
|
2306
|
+
const MenuKeyDownEscape = process.env.NODE_ENV !== "production" ? '__menu_keydown_escape__' : 2;
|
|
2307
|
+
const MenuKeyDownHome = process.env.NODE_ENV !== "production" ? '__menu_keydown_home__' : 3;
|
|
2308
|
+
const MenuKeyDownEnd = process.env.NODE_ENV !== "production" ? '__menu_keydown_end__' : 4;
|
|
2309
|
+
const MenuKeyDownEnter = process.env.NODE_ENV !== "production" ? '__menu_keydown_enter__' : 5;
|
|
2310
|
+
const MenuKeyDownSpaceButton = process.env.NODE_ENV !== "production" ? '__menu_keydown_space_button__' : 6;
|
|
2311
|
+
const MenuKeyDownCharacter = process.env.NODE_ENV !== "production" ? '__menu_keydown_character__' : 7;
|
|
2312
|
+
const MenuBlur = process.env.NODE_ENV !== "production" ? '__menu_blur__' : 8;
|
|
2313
|
+
const MenuMouseLeave$1 = process.env.NODE_ENV !== "production" ? '__menu_mouse_leave__' : 9;
|
|
2314
|
+
const ItemMouseMove$1 = process.env.NODE_ENV !== "production" ? '__item_mouse_move__' : 10;
|
|
2315
|
+
const ItemClick$1 = process.env.NODE_ENV !== "production" ? '__item_click__' : 11;
|
|
2316
|
+
const ToggleButtonClick$1 = process.env.NODE_ENV !== "production" ? '__togglebutton_click__' : 12;
|
|
2317
|
+
const ToggleButtonKeyDownArrowDown = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_arrow_down__' : 13;
|
|
2318
|
+
const ToggleButtonKeyDownArrowUp = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_arrow_up__' : 14;
|
|
2319
|
+
const ToggleButtonKeyDownCharacter = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_character__' : 15;
|
|
2320
|
+
const FunctionToggleMenu$1 = process.env.NODE_ENV !== "production" ? '__function_toggle_menu__' : 16;
|
|
2321
|
+
const FunctionOpenMenu$1 = process.env.NODE_ENV !== "production" ? '__function_open_menu__' : 17;
|
|
2322
|
+
const FunctionCloseMenu$1 = process.env.NODE_ENV !== "production" ? '__function_close_menu__' : 18;
|
|
2323
|
+
const FunctionSetHighlightedIndex$1 = process.env.NODE_ENV !== "production" ? '__function_set_highlighted_index__' : 19;
|
|
2324
|
+
const FunctionSelectItem$1 = process.env.NODE_ENV !== "production" ? '__function_select_item__' : 20;
|
|
2325
|
+
const FunctionSetInputValue$1 = process.env.NODE_ENV !== "production" ? '__function_set_input_value__' : 21;
|
|
2326
|
+
const FunctionReset$2 = process.env.NODE_ENV !== "production" ? '__function_reset__' : 22;
|
|
2326
2327
|
|
|
2327
2328
|
var stateChangeTypes$2 = /*#__PURE__*/Object.freeze({
|
|
2328
2329
|
__proto__: null,
|
|
@@ -2354,10 +2355,12 @@ var stateChangeTypes$2 = /*#__PURE__*/Object.freeze({
|
|
|
2354
2355
|
/* eslint-disable complexity */
|
|
2355
2356
|
|
|
2356
2357
|
function downshiftSelectReducer(state, action) {
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2358
|
+
const {
|
|
2359
|
+
type,
|
|
2360
|
+
props,
|
|
2361
|
+
shiftKey
|
|
2362
|
+
} = action;
|
|
2363
|
+
let changes;
|
|
2361
2364
|
|
|
2362
2365
|
switch (type) {
|
|
2363
2366
|
case ItemClick$1:
|
|
@@ -2370,20 +2373,21 @@ function downshiftSelectReducer(state, action) {
|
|
|
2370
2373
|
|
|
2371
2374
|
case ToggleButtonKeyDownCharacter:
|
|
2372
2375
|
{
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
+
const lowercasedKey = action.key;
|
|
2377
|
+
const inputValue = "" + state.inputValue + lowercasedKey;
|
|
2378
|
+
const itemIndex = getItemIndexByCharacterKey({
|
|
2376
2379
|
keysSoFar: inputValue,
|
|
2377
2380
|
highlightedIndex: state.selectedItem ? props.items.indexOf(state.selectedItem) : -1,
|
|
2378
2381
|
items: props.items,
|
|
2379
2382
|
itemToString: props.itemToString,
|
|
2380
2383
|
getItemNodeFromIndex: action.getItemNodeFromIndex
|
|
2381
2384
|
});
|
|
2382
|
-
changes =
|
|
2383
|
-
inputValue
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2385
|
+
changes = {
|
|
2386
|
+
inputValue,
|
|
2387
|
+
...(itemIndex >= 0 && {
|
|
2388
|
+
selectedItem: props.items[itemIndex]
|
|
2389
|
+
})
|
|
2390
|
+
};
|
|
2387
2391
|
}
|
|
2388
2392
|
break;
|
|
2389
2393
|
|
|
@@ -2403,12 +2407,13 @@ function downshiftSelectReducer(state, action) {
|
|
|
2403
2407
|
|
|
2404
2408
|
case MenuKeyDownEnter:
|
|
2405
2409
|
case MenuKeyDownSpaceButton:
|
|
2406
|
-
changes =
|
|
2410
|
+
changes = {
|
|
2407
2411
|
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
2408
|
-
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
+
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
|
2413
|
+
...(state.highlightedIndex >= 0 && {
|
|
2414
|
+
selectedItem: props.items[state.highlightedIndex]
|
|
2415
|
+
})
|
|
2416
|
+
};
|
|
2412
2417
|
break;
|
|
2413
2418
|
|
|
2414
2419
|
case MenuKeyDownHome:
|
|
@@ -2439,22 +2444,21 @@ function downshiftSelectReducer(state, action) {
|
|
|
2439
2444
|
|
|
2440
2445
|
case MenuKeyDownCharacter:
|
|
2441
2446
|
{
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
var highlightedIndex = getItemIndexByCharacterKey({
|
|
2447
|
-
keysSoFar: _inputValue,
|
|
2447
|
+
const lowercasedKey = action.key;
|
|
2448
|
+
const inputValue = "" + state.inputValue + lowercasedKey;
|
|
2449
|
+
const highlightedIndex = getItemIndexByCharacterKey({
|
|
2450
|
+
keysSoFar: inputValue,
|
|
2448
2451
|
highlightedIndex: state.highlightedIndex,
|
|
2449
2452
|
items: props.items,
|
|
2450
2453
|
itemToString: props.itemToString,
|
|
2451
2454
|
getItemNodeFromIndex: action.getItemNodeFromIndex
|
|
2452
2455
|
});
|
|
2453
|
-
changes =
|
|
2454
|
-
inputValue
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2456
|
+
changes = {
|
|
2457
|
+
inputValue,
|
|
2458
|
+
...(highlightedIndex >= 0 && {
|
|
2459
|
+
highlightedIndex
|
|
2460
|
+
})
|
|
2461
|
+
};
|
|
2458
2462
|
}
|
|
2459
2463
|
break;
|
|
2460
2464
|
|
|
@@ -2480,13 +2484,13 @@ function downshiftSelectReducer(state, action) {
|
|
|
2480
2484
|
return downshiftCommonReducer(state, action, stateChangeTypes$2);
|
|
2481
2485
|
}
|
|
2482
2486
|
|
|
2483
|
-
return
|
|
2487
|
+
return { ...state,
|
|
2488
|
+
...changes
|
|
2489
|
+
};
|
|
2484
2490
|
}
|
|
2485
2491
|
/* eslint-enable complexity */
|
|
2486
2492
|
|
|
2487
|
-
|
|
2488
|
-
_excluded2$2 = ["onClick", "onKeyDown", "refKey", "ref"],
|
|
2489
|
-
_excluded3$1 = ["item", "index", "onMouseMove", "onClick", "refKey", "ref"];
|
|
2493
|
+
/* eslint-disable max-statements */
|
|
2490
2494
|
useSelect.stateChangeTypes = stateChangeTypes$2;
|
|
2491
2495
|
|
|
2492
2496
|
function useSelect(userProps) {
|
|
@@ -2496,91 +2500,92 @@ function useSelect(userProps) {
|
|
|
2496
2500
|
|
|
2497
2501
|
validatePropTypes$2(userProps, useSelect); // Props defaults and destructuring.
|
|
2498
2502
|
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2503
|
+
const props = { ...defaultProps$2,
|
|
2504
|
+
...userProps
|
|
2505
|
+
};
|
|
2506
|
+
const {
|
|
2507
|
+
items,
|
|
2508
|
+
scrollIntoView,
|
|
2509
|
+
environment,
|
|
2510
|
+
initialIsOpen,
|
|
2511
|
+
defaultIsOpen,
|
|
2512
|
+
itemToString,
|
|
2513
|
+
getA11ySelectionMessage,
|
|
2514
|
+
getA11yStatusMessage
|
|
2515
|
+
} = props; // Initial state depending on controlled props.
|
|
2516
|
+
|
|
2517
|
+
const initialState = getInitialState$2(props);
|
|
2518
|
+
const [state, dispatch] = useControlledReducer$1(downshiftSelectReducer, initialState, props);
|
|
2519
|
+
const {
|
|
2520
|
+
isOpen,
|
|
2521
|
+
highlightedIndex,
|
|
2522
|
+
selectedItem,
|
|
2523
|
+
inputValue
|
|
2524
|
+
} = state; // Element efs.
|
|
2525
|
+
|
|
2526
|
+
const toggleButtonRef = useRef(null);
|
|
2527
|
+
const menuRef = useRef(null);
|
|
2528
|
+
const itemRefs = useRef({}); // used not to trigger menu blur action in some scenarios.
|
|
2529
|
+
|
|
2530
|
+
const shouldBlurRef = useRef(true); // used to keep the inputValue clearTimeout object between renders.
|
|
2531
|
+
|
|
2532
|
+
const clearTimeoutRef = useRef(null); // prevent id re-generation between renders.
|
|
2533
|
+
|
|
2534
|
+
const elementIds = useElementIds(props); // used to keep track of how many items we had on previous cycle.
|
|
2535
|
+
|
|
2536
|
+
const previousResultCountRef = useRef();
|
|
2537
|
+
const isInitialMountRef = useRef(true); // utility callback to get item element.
|
|
2538
|
+
|
|
2539
|
+
const latest = useLatestRef({
|
|
2540
|
+
state,
|
|
2541
|
+
props
|
|
2537
2542
|
}); // Some utils.
|
|
2538
2543
|
|
|
2539
|
-
|
|
2540
|
-
return itemRefs.current[elementIds.getItemId(index)];
|
|
2541
|
-
}, [elementIds]); // Effects.
|
|
2544
|
+
const getItemNodeFromIndex = useCallback(index => itemRefs.current[elementIds.getItemId(index)], [elementIds]); // Effects.
|
|
2542
2545
|
// Sets a11y status message on changes in state.
|
|
2543
2546
|
|
|
2544
|
-
useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items],
|
|
2547
|
+
useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items], {
|
|
2545
2548
|
isInitialMount: isInitialMountRef.current,
|
|
2546
2549
|
previousResultCount: previousResultCountRef.current,
|
|
2547
|
-
items
|
|
2548
|
-
environment
|
|
2549
|
-
itemToString
|
|
2550
|
-
|
|
2550
|
+
items,
|
|
2551
|
+
environment,
|
|
2552
|
+
itemToString,
|
|
2553
|
+
...state
|
|
2554
|
+
}); // Sets a11y status message on changes in selectedItem.
|
|
2551
2555
|
|
|
2552
|
-
useA11yMessageSetter(getA11ySelectionMessage, [selectedItem],
|
|
2556
|
+
useA11yMessageSetter(getA11ySelectionMessage, [selectedItem], {
|
|
2553
2557
|
isInitialMount: isInitialMountRef.current,
|
|
2554
2558
|
previousResultCount: previousResultCountRef.current,
|
|
2555
|
-
items
|
|
2556
|
-
environment
|
|
2557
|
-
itemToString
|
|
2558
|
-
|
|
2559
|
+
items,
|
|
2560
|
+
environment,
|
|
2561
|
+
itemToString,
|
|
2562
|
+
...state
|
|
2563
|
+
}); // Scroll on highlighted item if change comes from keyboard.
|
|
2559
2564
|
|
|
2560
|
-
|
|
2565
|
+
const shouldScrollRef = useScrollIntoView({
|
|
2561
2566
|
menuElement: menuRef.current,
|
|
2562
|
-
highlightedIndex
|
|
2563
|
-
isOpen
|
|
2564
|
-
itemRefs
|
|
2565
|
-
scrollIntoView
|
|
2566
|
-
getItemNodeFromIndex
|
|
2567
|
+
highlightedIndex,
|
|
2568
|
+
isOpen,
|
|
2569
|
+
itemRefs,
|
|
2570
|
+
scrollIntoView,
|
|
2571
|
+
getItemNodeFromIndex
|
|
2567
2572
|
}); // Sets cleanup for the keysSoFar callback, debounded after 500ms.
|
|
2568
2573
|
|
|
2569
|
-
useEffect(
|
|
2574
|
+
useEffect(() => {
|
|
2570
2575
|
// init the clean function here as we need access to dispatch.
|
|
2571
|
-
clearTimeoutRef.current = debounce(
|
|
2576
|
+
clearTimeoutRef.current = debounce(outerDispatch => {
|
|
2572
2577
|
outerDispatch({
|
|
2573
2578
|
type: FunctionSetInputValue$1,
|
|
2574
2579
|
inputValue: ''
|
|
2575
2580
|
});
|
|
2576
2581
|
}, 500); // Cancel any pending debounced calls on mount
|
|
2577
2582
|
|
|
2578
|
-
return
|
|
2583
|
+
return () => {
|
|
2579
2584
|
clearTimeoutRef.current.cancel();
|
|
2580
2585
|
};
|
|
2581
2586
|
}, []); // Invokes the keysSoFar callback set up above.
|
|
2582
2587
|
|
|
2583
|
-
useEffect(
|
|
2588
|
+
useEffect(() => {
|
|
2584
2589
|
if (!inputValue) {
|
|
2585
2590
|
return;
|
|
2586
2591
|
}
|
|
@@ -2589,12 +2594,12 @@ function useSelect(userProps) {
|
|
|
2589
2594
|
}, [dispatch, inputValue]);
|
|
2590
2595
|
useControlPropsValidator({
|
|
2591
2596
|
isInitialMount: isInitialMountRef.current,
|
|
2592
|
-
props
|
|
2593
|
-
state
|
|
2597
|
+
props,
|
|
2598
|
+
state
|
|
2594
2599
|
});
|
|
2595
2600
|
/* Controls the focus on the menu or the toggle button. */
|
|
2596
2601
|
|
|
2597
|
-
useEffect(
|
|
2602
|
+
useEffect(() => {
|
|
2598
2603
|
// Don't focus menu on first render.
|
|
2599
2604
|
if (isInitialMountRef.current) {
|
|
2600
2605
|
// Unless it was initialised as open.
|
|
@@ -2625,7 +2630,7 @@ function useSelect(userProps) {
|
|
|
2625
2630
|
} // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2626
2631
|
|
|
2627
2632
|
}, [isOpen]);
|
|
2628
|
-
useEffect(
|
|
2633
|
+
useEffect(() => {
|
|
2629
2634
|
if (isInitialMountRef.current) {
|
|
2630
2635
|
return;
|
|
2631
2636
|
}
|
|
@@ -2633,180 +2638,180 @@ function useSelect(userProps) {
|
|
|
2633
2638
|
previousResultCountRef.current = items.length;
|
|
2634
2639
|
}); // Add mouse/touch events to document.
|
|
2635
2640
|
|
|
2636
|
-
|
|
2641
|
+
const mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [menuRef, toggleButtonRef], environment, () => {
|
|
2637
2642
|
dispatch({
|
|
2638
2643
|
type: MenuBlur
|
|
2639
2644
|
});
|
|
2640
2645
|
});
|
|
2641
|
-
|
|
2646
|
+
const setGetterPropCallInfo = useGetterPropsCalledChecker('getMenuProps', 'getToggleButtonProps'); // Make initial ref false.
|
|
2642
2647
|
|
|
2643
|
-
useEffect(
|
|
2648
|
+
useEffect(() => {
|
|
2644
2649
|
isInitialMountRef.current = false;
|
|
2645
2650
|
}, []); // Reset itemRefs on close.
|
|
2646
2651
|
|
|
2647
|
-
useEffect(
|
|
2652
|
+
useEffect(() => {
|
|
2648
2653
|
if (!isOpen) {
|
|
2649
2654
|
itemRefs.current = {};
|
|
2650
2655
|
}
|
|
2651
2656
|
}, [isOpen]); // Event handler functions.
|
|
2652
2657
|
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
},
|
|
2663
|
-
ArrowUp: function ArrowUp(event) {
|
|
2664
|
-
event.preventDefault();
|
|
2665
|
-
dispatch({
|
|
2666
|
-
type: ToggleButtonKeyDownArrowUp,
|
|
2667
|
-
getItemNodeFromIndex: getItemNodeFromIndex,
|
|
2668
|
-
shiftKey: event.shiftKey
|
|
2669
|
-
});
|
|
2670
|
-
}
|
|
2671
|
-
};
|
|
2672
|
-
}, [dispatch, getItemNodeFromIndex]);
|
|
2673
|
-
var menuKeyDownHandlers = useMemo(function () {
|
|
2674
|
-
return {
|
|
2675
|
-
ArrowDown: function ArrowDown(event) {
|
|
2676
|
-
event.preventDefault();
|
|
2677
|
-
dispatch({
|
|
2678
|
-
type: MenuKeyDownArrowDown,
|
|
2679
|
-
getItemNodeFromIndex: getItemNodeFromIndex,
|
|
2680
|
-
shiftKey: event.shiftKey
|
|
2681
|
-
});
|
|
2682
|
-
},
|
|
2683
|
-
ArrowUp: function ArrowUp(event) {
|
|
2684
|
-
event.preventDefault();
|
|
2685
|
-
dispatch({
|
|
2686
|
-
type: MenuKeyDownArrowUp,
|
|
2687
|
-
getItemNodeFromIndex: getItemNodeFromIndex,
|
|
2688
|
-
shiftKey: event.shiftKey
|
|
2689
|
-
});
|
|
2690
|
-
},
|
|
2691
|
-
Home: function Home(event) {
|
|
2692
|
-
event.preventDefault();
|
|
2693
|
-
dispatch({
|
|
2694
|
-
type: MenuKeyDownHome,
|
|
2695
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
2696
|
-
});
|
|
2697
|
-
},
|
|
2698
|
-
End: function End(event) {
|
|
2699
|
-
event.preventDefault();
|
|
2700
|
-
dispatch({
|
|
2701
|
-
type: MenuKeyDownEnd,
|
|
2702
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
2703
|
-
});
|
|
2704
|
-
},
|
|
2705
|
-
Escape: function Escape() {
|
|
2706
|
-
dispatch({
|
|
2707
|
-
type: MenuKeyDownEscape
|
|
2708
|
-
});
|
|
2709
|
-
},
|
|
2710
|
-
Enter: function Enter(event) {
|
|
2711
|
-
event.preventDefault();
|
|
2712
|
-
dispatch({
|
|
2713
|
-
type: MenuKeyDownEnter
|
|
2714
|
-
});
|
|
2715
|
-
},
|
|
2716
|
-
' ': function _(event) {
|
|
2717
|
-
event.preventDefault();
|
|
2718
|
-
dispatch({
|
|
2719
|
-
type: MenuKeyDownSpaceButton
|
|
2720
|
-
});
|
|
2721
|
-
}
|
|
2722
|
-
};
|
|
2723
|
-
}, [dispatch, getItemNodeFromIndex]); // Action functions.
|
|
2658
|
+
const toggleButtonKeyDownHandlers = useMemo(() => ({
|
|
2659
|
+
ArrowDown(event) {
|
|
2660
|
+
event.preventDefault();
|
|
2661
|
+
dispatch({
|
|
2662
|
+
type: ToggleButtonKeyDownArrowDown,
|
|
2663
|
+
getItemNodeFromIndex,
|
|
2664
|
+
shiftKey: event.shiftKey
|
|
2665
|
+
});
|
|
2666
|
+
},
|
|
2724
2667
|
|
|
2725
|
-
|
|
2668
|
+
ArrowUp(event) {
|
|
2669
|
+
event.preventDefault();
|
|
2670
|
+
dispatch({
|
|
2671
|
+
type: ToggleButtonKeyDownArrowUp,
|
|
2672
|
+
getItemNodeFromIndex,
|
|
2673
|
+
shiftKey: event.shiftKey
|
|
2674
|
+
});
|
|
2675
|
+
}
|
|
2676
|
+
|
|
2677
|
+
}), [dispatch, getItemNodeFromIndex]);
|
|
2678
|
+
const menuKeyDownHandlers = useMemo(() => ({
|
|
2679
|
+
ArrowDown(event) {
|
|
2680
|
+
event.preventDefault();
|
|
2681
|
+
dispatch({
|
|
2682
|
+
type: MenuKeyDownArrowDown,
|
|
2683
|
+
getItemNodeFromIndex,
|
|
2684
|
+
shiftKey: event.shiftKey
|
|
2685
|
+
});
|
|
2686
|
+
},
|
|
2687
|
+
|
|
2688
|
+
ArrowUp(event) {
|
|
2689
|
+
event.preventDefault();
|
|
2690
|
+
dispatch({
|
|
2691
|
+
type: MenuKeyDownArrowUp,
|
|
2692
|
+
getItemNodeFromIndex,
|
|
2693
|
+
shiftKey: event.shiftKey
|
|
2694
|
+
});
|
|
2695
|
+
},
|
|
2696
|
+
|
|
2697
|
+
Home(event) {
|
|
2698
|
+
event.preventDefault();
|
|
2699
|
+
dispatch({
|
|
2700
|
+
type: MenuKeyDownHome,
|
|
2701
|
+
getItemNodeFromIndex
|
|
2702
|
+
});
|
|
2703
|
+
},
|
|
2704
|
+
|
|
2705
|
+
End(event) {
|
|
2706
|
+
event.preventDefault();
|
|
2707
|
+
dispatch({
|
|
2708
|
+
type: MenuKeyDownEnd,
|
|
2709
|
+
getItemNodeFromIndex
|
|
2710
|
+
});
|
|
2711
|
+
},
|
|
2712
|
+
|
|
2713
|
+
Escape() {
|
|
2714
|
+
dispatch({
|
|
2715
|
+
type: MenuKeyDownEscape
|
|
2716
|
+
});
|
|
2717
|
+
},
|
|
2718
|
+
|
|
2719
|
+
Enter(event) {
|
|
2720
|
+
event.preventDefault();
|
|
2721
|
+
dispatch({
|
|
2722
|
+
type: MenuKeyDownEnter
|
|
2723
|
+
});
|
|
2724
|
+
},
|
|
2725
|
+
|
|
2726
|
+
' '(event) {
|
|
2727
|
+
event.preventDefault();
|
|
2728
|
+
dispatch({
|
|
2729
|
+
type: MenuKeyDownSpaceButton
|
|
2730
|
+
});
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
}), [dispatch, getItemNodeFromIndex]); // Action functions.
|
|
2734
|
+
|
|
2735
|
+
const toggleMenu = useCallback(() => {
|
|
2726
2736
|
dispatch({
|
|
2727
2737
|
type: FunctionToggleMenu$1
|
|
2728
2738
|
});
|
|
2729
2739
|
}, [dispatch]);
|
|
2730
|
-
|
|
2740
|
+
const closeMenu = useCallback(() => {
|
|
2731
2741
|
dispatch({
|
|
2732
2742
|
type: FunctionCloseMenu$1
|
|
2733
2743
|
});
|
|
2734
2744
|
}, [dispatch]);
|
|
2735
|
-
|
|
2745
|
+
const openMenu = useCallback(() => {
|
|
2736
2746
|
dispatch({
|
|
2737
2747
|
type: FunctionOpenMenu$1
|
|
2738
2748
|
});
|
|
2739
2749
|
}, [dispatch]);
|
|
2740
|
-
|
|
2750
|
+
const setHighlightedIndex = useCallback(newHighlightedIndex => {
|
|
2741
2751
|
dispatch({
|
|
2742
2752
|
type: FunctionSetHighlightedIndex$1,
|
|
2743
2753
|
highlightedIndex: newHighlightedIndex
|
|
2744
2754
|
});
|
|
2745
2755
|
}, [dispatch]);
|
|
2746
|
-
|
|
2756
|
+
const selectItem = useCallback(newSelectedItem => {
|
|
2747
2757
|
dispatch({
|
|
2748
2758
|
type: FunctionSelectItem$1,
|
|
2749
2759
|
selectedItem: newSelectedItem
|
|
2750
2760
|
});
|
|
2751
2761
|
}, [dispatch]);
|
|
2752
|
-
|
|
2762
|
+
const reset = useCallback(() => {
|
|
2753
2763
|
dispatch({
|
|
2754
2764
|
type: FunctionReset$2
|
|
2755
2765
|
});
|
|
2756
2766
|
}, [dispatch]);
|
|
2757
|
-
|
|
2767
|
+
const setInputValue = useCallback(newInputValue => {
|
|
2758
2768
|
dispatch({
|
|
2759
2769
|
type: FunctionSetInputValue$1,
|
|
2760
2770
|
inputValue: newInputValue
|
|
2761
2771
|
});
|
|
2762
2772
|
}, [dispatch]); // Getter functions.
|
|
2763
2773
|
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
var latestState = latest.current.state;
|
|
2787
|
-
|
|
2788
|
-
var menuHandleKeyDown = function menuHandleKeyDown(event) {
|
|
2789
|
-
var key = normalizeArrowKey(event);
|
|
2774
|
+
const getLabelProps = useCallback(labelProps => ({
|
|
2775
|
+
id: elementIds.labelId,
|
|
2776
|
+
htmlFor: elementIds.toggleButtonId,
|
|
2777
|
+
...labelProps
|
|
2778
|
+
}), [elementIds]);
|
|
2779
|
+
const getMenuProps = useCallback(function (_temp, _temp2) {
|
|
2780
|
+
let {
|
|
2781
|
+
onMouseLeave,
|
|
2782
|
+
refKey = 'ref',
|
|
2783
|
+
onKeyDown,
|
|
2784
|
+
onBlur,
|
|
2785
|
+
ref,
|
|
2786
|
+
...rest
|
|
2787
|
+
} = _temp === void 0 ? {} : _temp;
|
|
2788
|
+
let {
|
|
2789
|
+
suppressRefError = false
|
|
2790
|
+
} = _temp2 === void 0 ? {} : _temp2;
|
|
2791
|
+
const latestState = latest.current.state;
|
|
2792
|
+
|
|
2793
|
+
const menuHandleKeyDown = event => {
|
|
2794
|
+
const key = normalizeArrowKey(event);
|
|
2790
2795
|
|
|
2791
2796
|
if (key && menuKeyDownHandlers[key]) {
|
|
2792
2797
|
menuKeyDownHandlers[key](event);
|
|
2793
2798
|
} else if (isAcceptedCharacterKey(key)) {
|
|
2794
2799
|
dispatch({
|
|
2795
2800
|
type: MenuKeyDownCharacter,
|
|
2796
|
-
key
|
|
2797
|
-
getItemNodeFromIndex
|
|
2801
|
+
key,
|
|
2802
|
+
getItemNodeFromIndex
|
|
2798
2803
|
});
|
|
2799
2804
|
}
|
|
2800
2805
|
};
|
|
2801
2806
|
|
|
2802
|
-
|
|
2807
|
+
const menuHandleBlur = () => {
|
|
2803
2808
|
// if the blur was a result of selection, we don't trigger this action.
|
|
2804
2809
|
if (shouldBlurRef.current === false) {
|
|
2805
2810
|
shouldBlurRef.current = true;
|
|
2806
2811
|
return;
|
|
2807
2812
|
}
|
|
2808
2813
|
|
|
2809
|
-
|
|
2814
|
+
const shouldBlur = !mouseAndTouchTrackersRef.current.isMouseDown;
|
|
2810
2815
|
/* istanbul ignore else */
|
|
2811
2816
|
|
|
2812
2817
|
if (shouldBlur) {
|
|
@@ -2816,61 +2821,72 @@ function useSelect(userProps) {
|
|
|
2816
2821
|
}
|
|
2817
2822
|
};
|
|
2818
2823
|
|
|
2819
|
-
|
|
2824
|
+
const menuHandleMouseLeave = () => {
|
|
2820
2825
|
dispatch({
|
|
2821
2826
|
type: MenuMouseLeave$1
|
|
2822
2827
|
});
|
|
2823
2828
|
};
|
|
2824
2829
|
|
|
2825
2830
|
setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
|
|
2826
|
-
return
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
+
return {
|
|
2832
|
+
[refKey]: handleRefs(ref, menuNode => {
|
|
2833
|
+
menuRef.current = menuNode;
|
|
2834
|
+
}),
|
|
2835
|
+
id: elementIds.menuId,
|
|
2836
|
+
role: 'listbox',
|
|
2837
|
+
'aria-labelledby': elementIds.labelId,
|
|
2838
|
+
tabIndex: -1,
|
|
2839
|
+
...(latestState.isOpen && latestState.highlightedIndex > -1 && {
|
|
2840
|
+
'aria-activedescendant': elementIds.getItemId(latestState.highlightedIndex)
|
|
2841
|
+
}),
|
|
2831
2842
|
onMouseLeave: callAllEventHandlers(onMouseLeave, menuHandleMouseLeave),
|
|
2832
2843
|
onKeyDown: callAllEventHandlers(onKeyDown, menuHandleKeyDown),
|
|
2833
|
-
onBlur: callAllEventHandlers(onBlur, menuHandleBlur)
|
|
2834
|
-
|
|
2844
|
+
onBlur: callAllEventHandlers(onBlur, menuHandleBlur),
|
|
2845
|
+
...rest
|
|
2846
|
+
};
|
|
2835
2847
|
}, [dispatch, latest, menuKeyDownHandlers, mouseAndTouchTrackersRef, setGetterPropCallInfo, elementIds, getItemNodeFromIndex]);
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
suppressRefError = _ref4$suppressRefErro === void 0 ? false : _ref4$suppressRefErro;
|
|
2850
|
-
|
|
2851
|
-
var toggleButtonHandleClick = function toggleButtonHandleClick() {
|
|
2848
|
+
const getToggleButtonProps = useCallback(function (_temp3, _temp4) {
|
|
2849
|
+
let {
|
|
2850
|
+
onClick,
|
|
2851
|
+
onKeyDown,
|
|
2852
|
+
refKey = 'ref',
|
|
2853
|
+
ref,
|
|
2854
|
+
...rest
|
|
2855
|
+
} = _temp3 === void 0 ? {} : _temp3;
|
|
2856
|
+
let {
|
|
2857
|
+
suppressRefError = false
|
|
2858
|
+
} = _temp4 === void 0 ? {} : _temp4;
|
|
2859
|
+
|
|
2860
|
+
const toggleButtonHandleClick = () => {
|
|
2852
2861
|
dispatch({
|
|
2853
2862
|
type: ToggleButtonClick$1
|
|
2854
2863
|
});
|
|
2855
2864
|
};
|
|
2856
2865
|
|
|
2857
|
-
|
|
2858
|
-
|
|
2866
|
+
const toggleButtonHandleKeyDown = event => {
|
|
2867
|
+
const key = normalizeArrowKey(event);
|
|
2859
2868
|
|
|
2860
2869
|
if (key && toggleButtonKeyDownHandlers[key]) {
|
|
2861
2870
|
toggleButtonKeyDownHandlers[key](event);
|
|
2862
2871
|
} else if (isAcceptedCharacterKey(key)) {
|
|
2863
2872
|
dispatch({
|
|
2864
2873
|
type: ToggleButtonKeyDownCharacter,
|
|
2865
|
-
key
|
|
2866
|
-
getItemNodeFromIndex
|
|
2874
|
+
key,
|
|
2875
|
+
getItemNodeFromIndex
|
|
2867
2876
|
});
|
|
2868
2877
|
}
|
|
2869
2878
|
};
|
|
2870
2879
|
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2880
|
+
const toggleProps = {
|
|
2881
|
+
[refKey]: handleRefs(ref, toggleButtonNode => {
|
|
2882
|
+
toggleButtonRef.current = toggleButtonNode;
|
|
2883
|
+
}),
|
|
2884
|
+
id: elementIds.toggleButtonId,
|
|
2885
|
+
'aria-haspopup': 'listbox',
|
|
2886
|
+
'aria-expanded': latest.current.state.isOpen,
|
|
2887
|
+
'aria-labelledby': elementIds.labelId + " " + elementIds.toggleButtonId,
|
|
2888
|
+
...rest
|
|
2889
|
+
};
|
|
2874
2890
|
|
|
2875
2891
|
if (!rest.disabled) {
|
|
2876
2892
|
toggleProps.onClick = callAllEventHandlers(onClick, toggleButtonHandleClick);
|
|
@@ -2880,24 +2896,22 @@ function useSelect(userProps) {
|
|
|
2880
2896
|
setGetterPropCallInfo('getToggleButtonProps', suppressRefError, refKey, toggleButtonRef);
|
|
2881
2897
|
return toggleProps;
|
|
2882
2898
|
}, [dispatch, latest, toggleButtonKeyDownHandlers, setGetterPropCallInfo, elementIds, getItemNodeFromIndex]);
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
var itemHandleMouseMove = function itemHandleMouseMove() {
|
|
2899
|
+
const getItemProps = useCallback(function (_temp5) {
|
|
2900
|
+
let {
|
|
2901
|
+
item,
|
|
2902
|
+
index,
|
|
2903
|
+
onMouseMove,
|
|
2904
|
+
onClick,
|
|
2905
|
+
refKey = 'ref',
|
|
2906
|
+
ref,
|
|
2907
|
+
...rest
|
|
2908
|
+
} = _temp5 === void 0 ? {} : _temp5;
|
|
2909
|
+
const {
|
|
2910
|
+
state: latestState,
|
|
2911
|
+
props: latestProps
|
|
2912
|
+
} = latest.current;
|
|
2913
|
+
|
|
2914
|
+
const itemHandleMouseMove = () => {
|
|
2901
2915
|
if (index === latestState.highlightedIndex) {
|
|
2902
2916
|
return;
|
|
2903
2917
|
}
|
|
@@ -2905,32 +2919,34 @@ function useSelect(userProps) {
|
|
|
2905
2919
|
shouldScrollRef.current = false;
|
|
2906
2920
|
dispatch({
|
|
2907
2921
|
type: ItemMouseMove$1,
|
|
2908
|
-
index
|
|
2922
|
+
index
|
|
2909
2923
|
});
|
|
2910
2924
|
};
|
|
2911
2925
|
|
|
2912
|
-
|
|
2926
|
+
const itemHandleClick = () => {
|
|
2913
2927
|
dispatch({
|
|
2914
2928
|
type: ItemClick$1,
|
|
2915
|
-
index
|
|
2929
|
+
index
|
|
2916
2930
|
});
|
|
2917
2931
|
};
|
|
2918
2932
|
|
|
2919
|
-
|
|
2933
|
+
const itemIndex = getItemIndex(index, item, latestProps.items);
|
|
2920
2934
|
|
|
2921
2935
|
if (itemIndex < 0) {
|
|
2922
2936
|
throw new Error('Pass either item or item index in getItemProps!');
|
|
2923
2937
|
}
|
|
2924
2938
|
|
|
2925
|
-
|
|
2939
|
+
const itemProps = {
|
|
2926
2940
|
role: 'option',
|
|
2927
2941
|
'aria-selected': "" + (itemIndex === latestState.highlightedIndex),
|
|
2928
|
-
id: elementIds.getItemId(itemIndex)
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2942
|
+
id: elementIds.getItemId(itemIndex),
|
|
2943
|
+
[refKey]: handleRefs(ref, itemNode => {
|
|
2944
|
+
if (itemNode) {
|
|
2945
|
+
itemRefs.current[elementIds.getItemId(itemIndex)] = itemNode;
|
|
2946
|
+
}
|
|
2947
|
+
}),
|
|
2948
|
+
...rest
|
|
2949
|
+
};
|
|
2934
2950
|
|
|
2935
2951
|
if (!rest.disabled) {
|
|
2936
2952
|
itemProps.onMouseMove = callAllEventHandlers(onMouseMove, itemHandleMouseMove);
|
|
@@ -2941,46 +2957,46 @@ function useSelect(userProps) {
|
|
|
2941
2957
|
}, [dispatch, latest, shouldScrollRef, elementIds]);
|
|
2942
2958
|
return {
|
|
2943
2959
|
// prop getters.
|
|
2944
|
-
getToggleButtonProps
|
|
2945
|
-
getLabelProps
|
|
2946
|
-
getMenuProps
|
|
2947
|
-
getItemProps
|
|
2960
|
+
getToggleButtonProps,
|
|
2961
|
+
getLabelProps,
|
|
2962
|
+
getMenuProps,
|
|
2963
|
+
getItemProps,
|
|
2948
2964
|
// actions.
|
|
2949
|
-
toggleMenu
|
|
2950
|
-
openMenu
|
|
2951
|
-
closeMenu
|
|
2952
|
-
setHighlightedIndex
|
|
2953
|
-
selectItem
|
|
2954
|
-
reset
|
|
2955
|
-
setInputValue
|
|
2965
|
+
toggleMenu,
|
|
2966
|
+
openMenu,
|
|
2967
|
+
closeMenu,
|
|
2968
|
+
setHighlightedIndex,
|
|
2969
|
+
selectItem,
|
|
2970
|
+
reset,
|
|
2971
|
+
setInputValue,
|
|
2956
2972
|
// state.
|
|
2957
|
-
highlightedIndex
|
|
2958
|
-
isOpen
|
|
2959
|
-
selectedItem
|
|
2960
|
-
inputValue
|
|
2973
|
+
highlightedIndex,
|
|
2974
|
+
isOpen,
|
|
2975
|
+
selectedItem,
|
|
2976
|
+
inputValue
|
|
2961
2977
|
};
|
|
2962
2978
|
}
|
|
2963
2979
|
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2980
|
+
const InputKeyDownArrowDown = process.env.NODE_ENV !== "production" ? '__input_keydown_arrow_down__' : 0;
|
|
2981
|
+
const InputKeyDownArrowUp = process.env.NODE_ENV !== "production" ? '__input_keydown_arrow_up__' : 1;
|
|
2982
|
+
const InputKeyDownEscape = process.env.NODE_ENV !== "production" ? '__input_keydown_escape__' : 2;
|
|
2983
|
+
const InputKeyDownHome = process.env.NODE_ENV !== "production" ? '__input_keydown_home__' : 3;
|
|
2984
|
+
const InputKeyDownEnd = process.env.NODE_ENV !== "production" ? '__input_keydown_end__' : 4;
|
|
2985
|
+
const InputKeyDownEnter = process.env.NODE_ENV !== "production" ? '__input_keydown_enter__' : 5;
|
|
2986
|
+
const InputChange = process.env.NODE_ENV !== "production" ? '__input_change__' : 6;
|
|
2987
|
+
const InputBlur = process.env.NODE_ENV !== "production" ? '__input_blur__' : 7;
|
|
2988
|
+
const MenuMouseLeave = process.env.NODE_ENV !== "production" ? '__menu_mouse_leave__' : 8;
|
|
2989
|
+
const ItemMouseMove = process.env.NODE_ENV !== "production" ? '__item_mouse_move__' : 9;
|
|
2990
|
+
const ItemClick = process.env.NODE_ENV !== "production" ? '__item_click__' : 10;
|
|
2991
|
+
const ToggleButtonClick = process.env.NODE_ENV !== "production" ? '__togglebutton_click__' : 11;
|
|
2992
|
+
const FunctionToggleMenu = process.env.NODE_ENV !== "production" ? '__function_toggle_menu__' : 12;
|
|
2993
|
+
const FunctionOpenMenu = process.env.NODE_ENV !== "production" ? '__function_open_menu__' : 13;
|
|
2994
|
+
const FunctionCloseMenu = process.env.NODE_ENV !== "production" ? '__function_close_menu__' : 14;
|
|
2995
|
+
const FunctionSetHighlightedIndex = process.env.NODE_ENV !== "production" ? '__function_set_highlighted_index__' : 15;
|
|
2996
|
+
const FunctionSelectItem = process.env.NODE_ENV !== "production" ? '__function_select_item__' : 16;
|
|
2997
|
+
const FunctionSetInputValue = process.env.NODE_ENV !== "production" ? '__function_set_input_value__' : 17;
|
|
2998
|
+
const FunctionReset$1 = process.env.NODE_ENV !== "production" ? '__function_reset__' : 18;
|
|
2999
|
+
const ControlledPropUpdatedSelectedItem = process.env.NODE_ENV !== "production" ? '__controlled_prop_updated_selected_item__' : 19;
|
|
2984
3000
|
|
|
2985
3001
|
var stateChangeTypes$1 = /*#__PURE__*/Object.freeze({
|
|
2986
3002
|
__proto__: null,
|
|
@@ -3007,20 +3023,24 @@ var stateChangeTypes$1 = /*#__PURE__*/Object.freeze({
|
|
|
3007
3023
|
});
|
|
3008
3024
|
|
|
3009
3025
|
function getInitialState$1(props) {
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3026
|
+
const initialState = getInitialState$2(props);
|
|
3027
|
+
const {
|
|
3028
|
+
selectedItem
|
|
3029
|
+
} = initialState;
|
|
3030
|
+
let {
|
|
3031
|
+
inputValue
|
|
3032
|
+
} = initialState;
|
|
3013
3033
|
|
|
3014
3034
|
if (inputValue === '' && selectedItem && props.defaultInputValue === undefined && props.initialInputValue === undefined && props.inputValue === undefined) {
|
|
3015
3035
|
inputValue = props.itemToString(selectedItem);
|
|
3016
3036
|
}
|
|
3017
3037
|
|
|
3018
|
-
return
|
|
3019
|
-
inputValue
|
|
3020
|
-
}
|
|
3038
|
+
return { ...initialState,
|
|
3039
|
+
inputValue
|
|
3040
|
+
};
|
|
3021
3041
|
}
|
|
3022
3042
|
|
|
3023
|
-
|
|
3043
|
+
const propTypes$1 = {
|
|
3024
3044
|
items: PropTypes.array.isRequired,
|
|
3025
3045
|
itemToString: PropTypes.func,
|
|
3026
3046
|
getA11yStatusMessage: PropTypes.func,
|
|
@@ -3074,14 +3094,10 @@ var propTypes$1 = {
|
|
|
3074
3094
|
*/
|
|
3075
3095
|
|
|
3076
3096
|
function useControlledReducer(reducer, initialState, props) {
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
var _useEnhancedReducer = useEnhancedReducer(reducer, initialState, props),
|
|
3080
|
-
state = _useEnhancedReducer[0],
|
|
3081
|
-
dispatch = _useEnhancedReducer[1]; // ToDo: if needed, make same approach as selectedItemChanged from Downshift.
|
|
3097
|
+
const previousSelectedItemRef = useRef();
|
|
3098
|
+
const [state, dispatch] = useEnhancedReducer(reducer, initialState, props); // ToDo: if needed, make same approach as selectedItemChanged from Downshift.
|
|
3082
3099
|
|
|
3083
|
-
|
|
3084
|
-
useEffect(function () {
|
|
3100
|
+
useEffect(() => {
|
|
3085
3101
|
if (isControlledProp(props, 'selectedItem')) {
|
|
3086
3102
|
if (previousSelectedItemRef.current !== props.selectedItem) {
|
|
3087
3103
|
dispatch({
|
|
@@ -3097,27 +3113,29 @@ function useControlledReducer(reducer, initialState, props) {
|
|
|
3097
3113
|
} // eslint-disable-next-line import/no-mutable-exports
|
|
3098
3114
|
|
|
3099
3115
|
|
|
3100
|
-
|
|
3116
|
+
let validatePropTypes$1 = noop;
|
|
3101
3117
|
/* istanbul ignore next */
|
|
3102
3118
|
|
|
3103
3119
|
if (process.env.NODE_ENV !== 'production') {
|
|
3104
|
-
validatePropTypes$1 =
|
|
3120
|
+
validatePropTypes$1 = (options, caller) => {
|
|
3105
3121
|
PropTypes.checkPropTypes(propTypes$1, options, 'prop', caller.name);
|
|
3106
3122
|
};
|
|
3107
3123
|
}
|
|
3108
3124
|
|
|
3109
|
-
|
|
3125
|
+
const defaultProps$1 = { ...defaultProps$3,
|
|
3110
3126
|
getA11yStatusMessage: getA11yStatusMessage$1,
|
|
3111
3127
|
circularNavigation: true
|
|
3112
|
-
}
|
|
3128
|
+
};
|
|
3113
3129
|
|
|
3114
3130
|
/* eslint-disable complexity */
|
|
3115
3131
|
|
|
3116
3132
|
function downshiftUseComboboxReducer(state, action) {
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3133
|
+
const {
|
|
3134
|
+
type,
|
|
3135
|
+
props,
|
|
3136
|
+
shiftKey
|
|
3137
|
+
} = action;
|
|
3138
|
+
let changes;
|
|
3121
3139
|
|
|
3122
3140
|
switch (type) {
|
|
3123
3141
|
case ItemClick:
|
|
@@ -3158,22 +3176,24 @@ function downshiftUseComboboxReducer(state, action) {
|
|
|
3158
3176
|
break;
|
|
3159
3177
|
|
|
3160
3178
|
case InputKeyDownEnter:
|
|
3161
|
-
changes =
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3179
|
+
changes = { ...(state.isOpen && state.highlightedIndex >= 0 && {
|
|
3180
|
+
selectedItem: props.items[state.highlightedIndex],
|
|
3181
|
+
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
3182
|
+
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
|
3183
|
+
inputValue: props.itemToString(props.items[state.highlightedIndex])
|
|
3184
|
+
})
|
|
3185
|
+
};
|
|
3167
3186
|
break;
|
|
3168
3187
|
|
|
3169
3188
|
case InputKeyDownEscape:
|
|
3170
|
-
changes =
|
|
3189
|
+
changes = {
|
|
3171
3190
|
isOpen: false,
|
|
3172
|
-
highlightedIndex: -1
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3191
|
+
highlightedIndex: -1,
|
|
3192
|
+
...(!state.isOpen && {
|
|
3193
|
+
selectedItem: null,
|
|
3194
|
+
inputValue: ''
|
|
3195
|
+
})
|
|
3196
|
+
};
|
|
3177
3197
|
break;
|
|
3178
3198
|
|
|
3179
3199
|
case InputKeyDownHome:
|
|
@@ -3189,13 +3209,14 @@ function downshiftUseComboboxReducer(state, action) {
|
|
|
3189
3209
|
break;
|
|
3190
3210
|
|
|
3191
3211
|
case InputBlur:
|
|
3192
|
-
changes =
|
|
3212
|
+
changes = {
|
|
3193
3213
|
isOpen: false,
|
|
3194
|
-
highlightedIndex: -1
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3214
|
+
highlightedIndex: -1,
|
|
3215
|
+
...(state.highlightedIndex >= 0 && action.selectItem && {
|
|
3216
|
+
selectedItem: props.items[state.highlightedIndex],
|
|
3217
|
+
inputValue: props.itemToString(props.items[state.highlightedIndex])
|
|
3218
|
+
})
|
|
3219
|
+
};
|
|
3199
3220
|
break;
|
|
3200
3221
|
|
|
3201
3222
|
case InputChange:
|
|
@@ -3223,15 +3244,13 @@ function downshiftUseComboboxReducer(state, action) {
|
|
|
3223
3244
|
return downshiftCommonReducer(state, action, stateChangeTypes$1);
|
|
3224
3245
|
}
|
|
3225
3246
|
|
|
3226
|
-
return
|
|
3247
|
+
return { ...state,
|
|
3248
|
+
...changes
|
|
3249
|
+
};
|
|
3227
3250
|
}
|
|
3228
3251
|
/* eslint-enable complexity */
|
|
3229
3252
|
|
|
3230
|
-
|
|
3231
|
-
_excluded2$1 = ["item", "index", "refKey", "ref", "onMouseMove", "onClick", "onPress"],
|
|
3232
|
-
_excluded3 = ["onClick", "onPress", "refKey", "ref"],
|
|
3233
|
-
_excluded4 = ["onKeyDown", "onChange", "onInput", "onBlur", "onChangeText", "refKey", "ref"],
|
|
3234
|
-
_excluded5 = ["refKey", "ref"];
|
|
3253
|
+
/* eslint-disable max-statements */
|
|
3235
3254
|
useCombobox.stateChangeTypes = stateChangeTypes$1;
|
|
3236
3255
|
|
|
3237
3256
|
function useCombobox(userProps) {
|
|
@@ -3241,87 +3260,88 @@ function useCombobox(userProps) {
|
|
|
3241
3260
|
|
|
3242
3261
|
validatePropTypes$1(userProps, useCombobox); // Props defaults and destructuring.
|
|
3243
3262
|
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3263
|
+
const props = { ...defaultProps$1,
|
|
3264
|
+
...userProps
|
|
3265
|
+
};
|
|
3266
|
+
const {
|
|
3267
|
+
initialIsOpen,
|
|
3268
|
+
defaultIsOpen,
|
|
3269
|
+
items,
|
|
3270
|
+
scrollIntoView,
|
|
3271
|
+
environment,
|
|
3272
|
+
getA11yStatusMessage,
|
|
3273
|
+
getA11ySelectionMessage,
|
|
3274
|
+
itemToString
|
|
3275
|
+
} = props; // Initial state depending on controlled props.
|
|
3276
|
+
|
|
3277
|
+
const initialState = getInitialState$1(props);
|
|
3278
|
+
const [state, dispatch] = useControlledReducer(downshiftUseComboboxReducer, initialState, props);
|
|
3279
|
+
const {
|
|
3280
|
+
isOpen,
|
|
3281
|
+
highlightedIndex,
|
|
3282
|
+
selectedItem,
|
|
3283
|
+
inputValue
|
|
3284
|
+
} = state; // Element refs.
|
|
3285
|
+
|
|
3286
|
+
const menuRef = useRef(null);
|
|
3287
|
+
const itemRefs = useRef({});
|
|
3288
|
+
const inputRef = useRef(null);
|
|
3289
|
+
const toggleButtonRef = useRef(null);
|
|
3290
|
+
const comboboxRef = useRef(null);
|
|
3291
|
+
const isInitialMountRef = useRef(true); // prevent id re-generation between renders.
|
|
3292
|
+
|
|
3293
|
+
const elementIds = useElementIds(props); // used to keep track of how many items we had on previous cycle.
|
|
3294
|
+
|
|
3295
|
+
const previousResultCountRef = useRef(); // utility callback to get item element.
|
|
3296
|
+
|
|
3297
|
+
const latest = useLatestRef({
|
|
3298
|
+
state,
|
|
3299
|
+
props
|
|
3280
3300
|
});
|
|
3281
|
-
|
|
3282
|
-
return itemRefs.current[elementIds.getItemId(index)];
|
|
3283
|
-
}, [elementIds]); // Effects.
|
|
3301
|
+
const getItemNodeFromIndex = useCallback(index => itemRefs.current[elementIds.getItemId(index)], [elementIds]); // Effects.
|
|
3284
3302
|
// Sets a11y status message on changes in state.
|
|
3285
3303
|
|
|
3286
|
-
useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items],
|
|
3304
|
+
useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items], {
|
|
3287
3305
|
isInitialMount: isInitialMountRef.current,
|
|
3288
3306
|
previousResultCount: previousResultCountRef.current,
|
|
3289
|
-
items
|
|
3290
|
-
environment
|
|
3291
|
-
itemToString
|
|
3292
|
-
|
|
3307
|
+
items,
|
|
3308
|
+
environment,
|
|
3309
|
+
itemToString,
|
|
3310
|
+
...state
|
|
3311
|
+
}); // Sets a11y status message on changes in selectedItem.
|
|
3293
3312
|
|
|
3294
|
-
useA11yMessageSetter(getA11ySelectionMessage, [selectedItem],
|
|
3313
|
+
useA11yMessageSetter(getA11ySelectionMessage, [selectedItem], {
|
|
3295
3314
|
isInitialMount: isInitialMountRef.current,
|
|
3296
3315
|
previousResultCount: previousResultCountRef.current,
|
|
3297
|
-
items
|
|
3298
|
-
environment
|
|
3299
|
-
itemToString
|
|
3300
|
-
|
|
3316
|
+
items,
|
|
3317
|
+
environment,
|
|
3318
|
+
itemToString,
|
|
3319
|
+
...state
|
|
3320
|
+
}); // Scroll on highlighted item if change comes from keyboard.
|
|
3301
3321
|
|
|
3302
|
-
|
|
3322
|
+
const shouldScrollRef = useScrollIntoView({
|
|
3303
3323
|
menuElement: menuRef.current,
|
|
3304
|
-
highlightedIndex
|
|
3305
|
-
isOpen
|
|
3306
|
-
itemRefs
|
|
3307
|
-
scrollIntoView
|
|
3308
|
-
getItemNodeFromIndex
|
|
3324
|
+
highlightedIndex,
|
|
3325
|
+
isOpen,
|
|
3326
|
+
itemRefs,
|
|
3327
|
+
scrollIntoView,
|
|
3328
|
+
getItemNodeFromIndex
|
|
3309
3329
|
});
|
|
3310
3330
|
useControlPropsValidator({
|
|
3311
3331
|
isInitialMount: isInitialMountRef.current,
|
|
3312
|
-
props
|
|
3313
|
-
state
|
|
3332
|
+
props,
|
|
3333
|
+
state
|
|
3314
3334
|
}); // Focus the input on first render if required.
|
|
3315
3335
|
|
|
3316
|
-
useEffect(
|
|
3317
|
-
|
|
3336
|
+
useEffect(() => {
|
|
3337
|
+
const focusOnOpen = initialIsOpen || defaultIsOpen || isOpen;
|
|
3318
3338
|
|
|
3319
3339
|
if (focusOnOpen && inputRef.current) {
|
|
3320
3340
|
inputRef.current.focus();
|
|
3321
3341
|
} // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
3322
3342
|
|
|
3323
3343
|
}, []);
|
|
3324
|
-
useEffect(
|
|
3344
|
+
useEffect(() => {
|
|
3325
3345
|
if (isInitialMountRef.current) {
|
|
3326
3346
|
return;
|
|
3327
3347
|
}
|
|
@@ -3329,147 +3349,151 @@ function useCombobox(userProps) {
|
|
|
3329
3349
|
previousResultCountRef.current = items.length;
|
|
3330
3350
|
}); // Add mouse/touch events to document.
|
|
3331
3351
|
|
|
3332
|
-
|
|
3352
|
+
const mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [comboboxRef, menuRef, toggleButtonRef], environment, () => {
|
|
3333
3353
|
dispatch({
|
|
3334
3354
|
type: InputBlur,
|
|
3335
3355
|
selectItem: false
|
|
3336
3356
|
});
|
|
3337
3357
|
});
|
|
3338
|
-
|
|
3358
|
+
const setGetterPropCallInfo = useGetterPropsCalledChecker('getInputProps', 'getComboboxProps', 'getMenuProps'); // Make initial ref false.
|
|
3339
3359
|
|
|
3340
|
-
useEffect(
|
|
3360
|
+
useEffect(() => {
|
|
3341
3361
|
isInitialMountRef.current = false;
|
|
3342
3362
|
}, []); // Reset itemRefs on close.
|
|
3343
3363
|
|
|
3344
|
-
useEffect(
|
|
3364
|
+
useEffect(() => {
|
|
3345
3365
|
if (!isOpen) {
|
|
3346
3366
|
itemRefs.current = {};
|
|
3347
3367
|
}
|
|
3348
3368
|
}, [isOpen]);
|
|
3349
3369
|
/* Event handler functions */
|
|
3350
3370
|
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
},
|
|
3361
|
-
ArrowUp: function ArrowUp(event) {
|
|
3362
|
-
event.preventDefault();
|
|
3363
|
-
dispatch({
|
|
3364
|
-
type: InputKeyDownArrowUp,
|
|
3365
|
-
shiftKey: event.shiftKey,
|
|
3366
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
3367
|
-
});
|
|
3368
|
-
},
|
|
3369
|
-
Home: function Home(event) {
|
|
3370
|
-
if (!latest.current.state.isOpen) {
|
|
3371
|
-
return;
|
|
3372
|
-
}
|
|
3371
|
+
const inputKeyDownHandlers = useMemo(() => ({
|
|
3372
|
+
ArrowDown(event) {
|
|
3373
|
+
event.preventDefault();
|
|
3374
|
+
dispatch({
|
|
3375
|
+
type: InputKeyDownArrowDown,
|
|
3376
|
+
shiftKey: event.shiftKey,
|
|
3377
|
+
getItemNodeFromIndex
|
|
3378
|
+
});
|
|
3379
|
+
},
|
|
3373
3380
|
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
return;
|
|
3383
|
-
}
|
|
3381
|
+
ArrowUp(event) {
|
|
3382
|
+
event.preventDefault();
|
|
3383
|
+
dispatch({
|
|
3384
|
+
type: InputKeyDownArrowUp,
|
|
3385
|
+
shiftKey: event.shiftKey,
|
|
3386
|
+
getItemNodeFromIndex
|
|
3387
|
+
});
|
|
3388
|
+
},
|
|
3384
3389
|
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
});
|
|
3390
|
-
},
|
|
3391
|
-
Escape: function Escape() {
|
|
3392
|
-
var latestState = latest.current.state;
|
|
3390
|
+
Home(event) {
|
|
3391
|
+
if (!latest.current.state.isOpen) {
|
|
3392
|
+
return;
|
|
3393
|
+
}
|
|
3393
3394
|
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
Enter: function Enter(event) {
|
|
3401
|
-
var latestState = latest.current.state; // if closed or no highlighted index, do nothing.
|
|
3395
|
+
event.preventDefault();
|
|
3396
|
+
dispatch({
|
|
3397
|
+
type: InputKeyDownHome,
|
|
3398
|
+
getItemNodeFromIndex
|
|
3399
|
+
});
|
|
3400
|
+
},
|
|
3402
3401
|
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3402
|
+
End(event) {
|
|
3403
|
+
if (!latest.current.state.isOpen) {
|
|
3404
|
+
return;
|
|
3405
|
+
}
|
|
3407
3406
|
|
|
3408
|
-
|
|
3407
|
+
event.preventDefault();
|
|
3408
|
+
dispatch({
|
|
3409
|
+
type: InputKeyDownEnd,
|
|
3410
|
+
getItemNodeFromIndex
|
|
3411
|
+
});
|
|
3412
|
+
},
|
|
3413
|
+
|
|
3414
|
+
Escape() {
|
|
3415
|
+
const latestState = latest.current.state;
|
|
3416
|
+
|
|
3417
|
+
if (latestState.isOpen || latestState.inputValue || latestState.selectedItem || latestState.highlightedIndex > -1) {
|
|
3409
3418
|
dispatch({
|
|
3410
|
-
type:
|
|
3411
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
3419
|
+
type: InputKeyDownEscape
|
|
3412
3420
|
});
|
|
3413
3421
|
}
|
|
3414
|
-
}
|
|
3415
|
-
}, [dispatch, latest, getItemNodeFromIndex]); // Getter props.
|
|
3416
|
-
|
|
3417
|
-
var getLabelProps = useCallback(function (labelProps) {
|
|
3418
|
-
return _extends({
|
|
3419
|
-
id: elementIds.labelId,
|
|
3420
|
-
htmlFor: elementIds.inputId
|
|
3421
|
-
}, labelProps);
|
|
3422
|
-
}, [elementIds]);
|
|
3423
|
-
var getMenuProps = useCallback(function (_temp, _temp2) {
|
|
3424
|
-
var _extends2;
|
|
3425
|
-
|
|
3426
|
-
var _ref = _temp === void 0 ? {} : _temp,
|
|
3427
|
-
onMouseLeave = _ref.onMouseLeave,
|
|
3428
|
-
_ref$refKey = _ref.refKey,
|
|
3429
|
-
refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
|
|
3430
|
-
ref = _ref.ref,
|
|
3431
|
-
rest = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
|
3432
|
-
|
|
3433
|
-
var _ref2 = _temp2 === void 0 ? {} : _temp2,
|
|
3434
|
-
_ref2$suppressRefErro = _ref2.suppressRefError,
|
|
3435
|
-
suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
|
|
3422
|
+
},
|
|
3436
3423
|
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
3424
|
+
Enter(event) {
|
|
3425
|
+
const latestState = latest.current.state; // if closed or no highlighted index, do nothing.
|
|
3426
|
+
|
|
3427
|
+
if (!latestState.isOpen || latestState.highlightedIndex < 0 || event.which === 229 // if IME composing, wait for next Enter keydown event.
|
|
3428
|
+
) {
|
|
3429
|
+
return;
|
|
3430
|
+
}
|
|
3431
|
+
|
|
3432
|
+
event.preventDefault();
|
|
3441
3433
|
dispatch({
|
|
3442
|
-
type:
|
|
3434
|
+
type: InputKeyDownEnter,
|
|
3435
|
+
getItemNodeFromIndex
|
|
3443
3436
|
});
|
|
3444
|
-
}
|
|
3437
|
+
}
|
|
3438
|
+
|
|
3439
|
+
}), [dispatch, latest, getItemNodeFromIndex]); // Getter props.
|
|
3440
|
+
|
|
3441
|
+
const getLabelProps = useCallback(labelProps => ({
|
|
3442
|
+
id: elementIds.labelId,
|
|
3443
|
+
htmlFor: elementIds.inputId,
|
|
3444
|
+
...labelProps
|
|
3445
|
+
}), [elementIds]);
|
|
3446
|
+
const getMenuProps = useCallback(function (_temp, _temp2) {
|
|
3447
|
+
let {
|
|
3448
|
+
onMouseLeave,
|
|
3449
|
+
refKey = 'ref',
|
|
3450
|
+
ref,
|
|
3451
|
+
...rest
|
|
3452
|
+
} = _temp === void 0 ? {} : _temp;
|
|
3453
|
+
let {
|
|
3454
|
+
suppressRefError = false
|
|
3455
|
+
} = _temp2 === void 0 ? {} : _temp2;
|
|
3456
|
+
setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
|
|
3457
|
+
return {
|
|
3458
|
+
[refKey]: handleRefs(ref, menuNode => {
|
|
3459
|
+
menuRef.current = menuNode;
|
|
3460
|
+
}),
|
|
3461
|
+
id: elementIds.menuId,
|
|
3462
|
+
role: 'listbox',
|
|
3463
|
+
'aria-labelledby': elementIds.labelId,
|
|
3464
|
+
onMouseLeave: callAllEventHandlers(onMouseLeave, () => {
|
|
3465
|
+
dispatch({
|
|
3466
|
+
type: MenuMouseLeave
|
|
3467
|
+
});
|
|
3468
|
+
}),
|
|
3469
|
+
...rest
|
|
3470
|
+
};
|
|
3445
3471
|
}, [dispatch, setGetterPropCallInfo, elementIds]);
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
latestState = _latest$current.state;
|
|
3463
|
-
var itemIndex = getItemIndex(index, item, latestProps.items);
|
|
3472
|
+
const getItemProps = useCallback(function (_temp3) {
|
|
3473
|
+
let {
|
|
3474
|
+
item,
|
|
3475
|
+
index,
|
|
3476
|
+
refKey = 'ref',
|
|
3477
|
+
ref,
|
|
3478
|
+
onMouseMove,
|
|
3479
|
+
onClick,
|
|
3480
|
+
onPress,
|
|
3481
|
+
...rest
|
|
3482
|
+
} = _temp3 === void 0 ? {} : _temp3;
|
|
3483
|
+
const {
|
|
3484
|
+
props: latestProps,
|
|
3485
|
+
state: latestState
|
|
3486
|
+
} = latest.current;
|
|
3487
|
+
const itemIndex = getItemIndex(index, item, latestProps.items);
|
|
3464
3488
|
|
|
3465
3489
|
if (itemIndex < 0) {
|
|
3466
3490
|
throw new Error('Pass either item or item index in getItemProps!');
|
|
3467
3491
|
}
|
|
3468
3492
|
|
|
3469
|
-
|
|
3470
|
-
|
|
3493
|
+
const onSelectKey = 'onClick';
|
|
3494
|
+
const customClickHandler = onClick;
|
|
3471
3495
|
|
|
3472
|
-
|
|
3496
|
+
const itemHandleMouseMove = () => {
|
|
3473
3497
|
if (index === latestState.highlightedIndex) {
|
|
3474
3498
|
return;
|
|
3475
3499
|
}
|
|
@@ -3477,14 +3501,14 @@ function useCombobox(userProps) {
|
|
|
3477
3501
|
shouldScrollRef.current = false;
|
|
3478
3502
|
dispatch({
|
|
3479
3503
|
type: ItemMouseMove,
|
|
3480
|
-
index
|
|
3504
|
+
index
|
|
3481
3505
|
});
|
|
3482
3506
|
};
|
|
3483
3507
|
|
|
3484
|
-
|
|
3508
|
+
const itemHandleClick = () => {
|
|
3485
3509
|
dispatch({
|
|
3486
3510
|
type: ItemClick,
|
|
3487
|
-
index
|
|
3511
|
+
index
|
|
3488
3512
|
});
|
|
3489
3513
|
|
|
3490
3514
|
if (inputRef.current) {
|
|
@@ -3492,26 +3516,32 @@ function useCombobox(userProps) {
|
|
|
3492
3516
|
}
|
|
3493
3517
|
};
|
|
3494
3518
|
|
|
3495
|
-
return
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3501
|
-
|
|
3519
|
+
return {
|
|
3520
|
+
[refKey]: handleRefs(ref, itemNode => {
|
|
3521
|
+
if (itemNode) {
|
|
3522
|
+
itemRefs.current[elementIds.getItemId(itemIndex)] = itemNode;
|
|
3523
|
+
}
|
|
3524
|
+
}),
|
|
3525
|
+
role: 'option',
|
|
3526
|
+
'aria-selected': "" + (itemIndex === latestState.highlightedIndex),
|
|
3527
|
+
id: elementIds.getItemId(itemIndex),
|
|
3528
|
+
...(!rest.disabled && {
|
|
3529
|
+
onMouseMove: callAllEventHandlers(onMouseMove, itemHandleMouseMove),
|
|
3530
|
+
[onSelectKey]: callAllEventHandlers(customClickHandler, itemHandleClick)
|
|
3531
|
+
}),
|
|
3532
|
+
...rest
|
|
3533
|
+
};
|
|
3502
3534
|
}, [dispatch, latest, shouldScrollRef, elementIds]);
|
|
3503
|
-
|
|
3504
|
-
|
|
3505
|
-
|
|
3506
|
-
|
|
3507
|
-
|
|
3508
|
-
|
|
3509
|
-
|
|
3510
|
-
|
|
3511
|
-
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
var toggleButtonHandleClick = function toggleButtonHandleClick() {
|
|
3535
|
+
const getToggleButtonProps = useCallback(function (_temp4) {
|
|
3536
|
+
let {
|
|
3537
|
+
onClick,
|
|
3538
|
+
onPress,
|
|
3539
|
+
refKey = 'ref',
|
|
3540
|
+
ref,
|
|
3541
|
+
...rest
|
|
3542
|
+
} = _temp4 === void 0 ? {} : _temp4;
|
|
3543
|
+
|
|
3544
|
+
const toggleButtonHandleClick = () => {
|
|
3515
3545
|
dispatch({
|
|
3516
3546
|
type: ToggleButtonClick
|
|
3517
3547
|
});
|
|
@@ -3521,49 +3551,52 @@ function useCombobox(userProps) {
|
|
|
3521
3551
|
}
|
|
3522
3552
|
};
|
|
3523
3553
|
|
|
3524
|
-
return
|
|
3525
|
-
|
|
3526
|
-
|
|
3527
|
-
|
|
3528
|
-
|
|
3554
|
+
return {
|
|
3555
|
+
[refKey]: handleRefs(ref, toggleButtonNode => {
|
|
3556
|
+
toggleButtonRef.current = toggleButtonNode;
|
|
3557
|
+
}),
|
|
3558
|
+
id: elementIds.toggleButtonId,
|
|
3559
|
+
tabIndex: -1,
|
|
3560
|
+
...(!rest.disabled && { ...({
|
|
3561
|
+
onClick: callAllEventHandlers(onClick, toggleButtonHandleClick)
|
|
3562
|
+
})
|
|
3563
|
+
}),
|
|
3564
|
+
...rest
|
|
3565
|
+
};
|
|
3529
3566
|
}, [dispatch, latest, elementIds]);
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
var _ref7 = _temp6 === void 0 ? {} : _temp6,
|
|
3545
|
-
_ref7$suppressRefErro = _ref7.suppressRefError,
|
|
3546
|
-
suppressRefError = _ref7$suppressRefErro === void 0 ? false : _ref7$suppressRefErro;
|
|
3547
|
-
|
|
3567
|
+
const getInputProps = useCallback(function (_temp5, _temp6) {
|
|
3568
|
+
let {
|
|
3569
|
+
onKeyDown,
|
|
3570
|
+
onChange,
|
|
3571
|
+
onInput,
|
|
3572
|
+
onBlur,
|
|
3573
|
+
onChangeText,
|
|
3574
|
+
refKey = 'ref',
|
|
3575
|
+
ref,
|
|
3576
|
+
...rest
|
|
3577
|
+
} = _temp5 === void 0 ? {} : _temp5;
|
|
3578
|
+
let {
|
|
3579
|
+
suppressRefError = false
|
|
3580
|
+
} = _temp6 === void 0 ? {} : _temp6;
|
|
3548
3581
|
setGetterPropCallInfo('getInputProps', suppressRefError, refKey, inputRef);
|
|
3549
|
-
|
|
3582
|
+
const latestState = latest.current.state;
|
|
3550
3583
|
|
|
3551
|
-
|
|
3552
|
-
|
|
3584
|
+
const inputHandleKeyDown = event => {
|
|
3585
|
+
const key = normalizeArrowKey(event);
|
|
3553
3586
|
|
|
3554
3587
|
if (key && inputKeyDownHandlers[key]) {
|
|
3555
3588
|
inputKeyDownHandlers[key](event);
|
|
3556
3589
|
}
|
|
3557
3590
|
};
|
|
3558
3591
|
|
|
3559
|
-
|
|
3592
|
+
const inputHandleChange = event => {
|
|
3560
3593
|
dispatch({
|
|
3561
3594
|
type: InputChange,
|
|
3562
3595
|
inputValue: event.target.value
|
|
3563
3596
|
});
|
|
3564
3597
|
};
|
|
3565
3598
|
|
|
3566
|
-
|
|
3599
|
+
const inputHandleBlur = () => {
|
|
3567
3600
|
/* istanbul ignore else */
|
|
3568
3601
|
if (latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
|
|
3569
3602
|
dispatch({
|
|
@@ -3575,109 +3608,121 @@ function useCombobox(userProps) {
|
|
|
3575
3608
|
/* istanbul ignore next (preact) */
|
|
3576
3609
|
|
|
3577
3610
|
|
|
3578
|
-
|
|
3579
|
-
|
|
3611
|
+
const onChangeKey = 'onInput' ;
|
|
3612
|
+
let eventHandlers = {};
|
|
3580
3613
|
|
|
3581
3614
|
if (!rest.disabled) {
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3615
|
+
eventHandlers = {
|
|
3616
|
+
[onChangeKey]: callAllEventHandlers(onChange, onInput, inputHandleChange),
|
|
3617
|
+
onKeyDown: callAllEventHandlers(onKeyDown, inputHandleKeyDown),
|
|
3618
|
+
onBlur: callAllEventHandlers(onBlur, inputHandleBlur)
|
|
3619
|
+
};
|
|
3585
3620
|
}
|
|
3586
3621
|
|
|
3587
|
-
return
|
|
3588
|
-
|
|
3589
|
-
|
|
3590
|
-
|
|
3591
|
-
|
|
3622
|
+
return {
|
|
3623
|
+
[refKey]: handleRefs(ref, inputNode => {
|
|
3624
|
+
inputRef.current = inputNode;
|
|
3625
|
+
}),
|
|
3626
|
+
id: elementIds.inputId,
|
|
3627
|
+
'aria-autocomplete': 'list',
|
|
3628
|
+
'aria-controls': elementIds.menuId,
|
|
3629
|
+
...(latestState.isOpen && latestState.highlightedIndex > -1 && {
|
|
3630
|
+
'aria-activedescendant': elementIds.getItemId(latestState.highlightedIndex)
|
|
3631
|
+
}),
|
|
3592
3632
|
'aria-labelledby': elementIds.labelId,
|
|
3593
3633
|
// https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
|
|
3594
3634
|
// revert back since autocomplete="nope" is ignored on latest Chrome and Opera
|
|
3595
3635
|
autoComplete: 'off',
|
|
3596
|
-
value: latestState.inputValue
|
|
3597
|
-
|
|
3636
|
+
value: latestState.inputValue,
|
|
3637
|
+
...eventHandlers,
|
|
3638
|
+
...rest
|
|
3639
|
+
};
|
|
3598
3640
|
}, [dispatch, inputKeyDownHandlers, latest, mouseAndTouchTrackersRef, setGetterPropCallInfo, elementIds]);
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
var _ref9 = _temp8 === void 0 ? {} : _temp8,
|
|
3609
|
-
_ref9$suppressRefErro = _ref9.suppressRefError,
|
|
3610
|
-
suppressRefError = _ref9$suppressRefErro === void 0 ? false : _ref9$suppressRefErro;
|
|
3611
|
-
|
|
3641
|
+
const getComboboxProps = useCallback(function (_temp7, _temp8) {
|
|
3642
|
+
let {
|
|
3643
|
+
refKey = 'ref',
|
|
3644
|
+
ref,
|
|
3645
|
+
...rest
|
|
3646
|
+
} = _temp7 === void 0 ? {} : _temp7;
|
|
3647
|
+
let {
|
|
3648
|
+
suppressRefError = false
|
|
3649
|
+
} = _temp8 === void 0 ? {} : _temp8;
|
|
3612
3650
|
setGetterPropCallInfo('getComboboxProps', suppressRefError, refKey, comboboxRef);
|
|
3613
|
-
return
|
|
3614
|
-
|
|
3615
|
-
|
|
3651
|
+
return {
|
|
3652
|
+
[refKey]: handleRefs(ref, comboboxNode => {
|
|
3653
|
+
comboboxRef.current = comboboxNode;
|
|
3654
|
+
}),
|
|
3655
|
+
role: 'combobox',
|
|
3656
|
+
'aria-haspopup': 'listbox',
|
|
3657
|
+
'aria-owns': elementIds.menuId,
|
|
3658
|
+
'aria-expanded': latest.current.state.isOpen,
|
|
3659
|
+
...rest
|
|
3660
|
+
};
|
|
3616
3661
|
}, [latest, setGetterPropCallInfo, elementIds]); // returns
|
|
3617
3662
|
|
|
3618
|
-
|
|
3663
|
+
const toggleMenu = useCallback(() => {
|
|
3619
3664
|
dispatch({
|
|
3620
3665
|
type: FunctionToggleMenu
|
|
3621
3666
|
});
|
|
3622
3667
|
}, [dispatch]);
|
|
3623
|
-
|
|
3668
|
+
const closeMenu = useCallback(() => {
|
|
3624
3669
|
dispatch({
|
|
3625
3670
|
type: FunctionCloseMenu
|
|
3626
3671
|
});
|
|
3627
3672
|
}, [dispatch]);
|
|
3628
|
-
|
|
3673
|
+
const openMenu = useCallback(() => {
|
|
3629
3674
|
dispatch({
|
|
3630
3675
|
type: FunctionOpenMenu
|
|
3631
3676
|
});
|
|
3632
3677
|
}, [dispatch]);
|
|
3633
|
-
|
|
3678
|
+
const setHighlightedIndex = useCallback(newHighlightedIndex => {
|
|
3634
3679
|
dispatch({
|
|
3635
3680
|
type: FunctionSetHighlightedIndex,
|
|
3636
3681
|
highlightedIndex: newHighlightedIndex
|
|
3637
3682
|
});
|
|
3638
3683
|
}, [dispatch]);
|
|
3639
|
-
|
|
3684
|
+
const selectItem = useCallback(newSelectedItem => {
|
|
3640
3685
|
dispatch({
|
|
3641
3686
|
type: FunctionSelectItem,
|
|
3642
3687
|
selectedItem: newSelectedItem
|
|
3643
3688
|
});
|
|
3644
3689
|
}, [dispatch]);
|
|
3645
|
-
|
|
3690
|
+
const setInputValue = useCallback(newInputValue => {
|
|
3646
3691
|
dispatch({
|
|
3647
3692
|
type: FunctionSetInputValue,
|
|
3648
3693
|
inputValue: newInputValue
|
|
3649
3694
|
});
|
|
3650
3695
|
}, [dispatch]);
|
|
3651
|
-
|
|
3696
|
+
const reset = useCallback(() => {
|
|
3652
3697
|
dispatch({
|
|
3653
3698
|
type: FunctionReset$1
|
|
3654
3699
|
});
|
|
3655
3700
|
}, [dispatch]);
|
|
3656
3701
|
return {
|
|
3657
3702
|
// prop getters.
|
|
3658
|
-
getItemProps
|
|
3659
|
-
getLabelProps
|
|
3660
|
-
getMenuProps
|
|
3661
|
-
getInputProps
|
|
3662
|
-
getComboboxProps
|
|
3663
|
-
getToggleButtonProps
|
|
3703
|
+
getItemProps,
|
|
3704
|
+
getLabelProps,
|
|
3705
|
+
getMenuProps,
|
|
3706
|
+
getInputProps,
|
|
3707
|
+
getComboboxProps,
|
|
3708
|
+
getToggleButtonProps,
|
|
3664
3709
|
// actions.
|
|
3665
|
-
toggleMenu
|
|
3666
|
-
openMenu
|
|
3667
|
-
closeMenu
|
|
3668
|
-
setHighlightedIndex
|
|
3669
|
-
setInputValue
|
|
3670
|
-
selectItem
|
|
3671
|
-
reset
|
|
3710
|
+
toggleMenu,
|
|
3711
|
+
openMenu,
|
|
3712
|
+
closeMenu,
|
|
3713
|
+
setHighlightedIndex,
|
|
3714
|
+
setInputValue,
|
|
3715
|
+
selectItem,
|
|
3716
|
+
reset,
|
|
3672
3717
|
// state.
|
|
3673
|
-
highlightedIndex
|
|
3674
|
-
isOpen
|
|
3675
|
-
selectedItem
|
|
3676
|
-
inputValue
|
|
3718
|
+
highlightedIndex,
|
|
3719
|
+
isOpen,
|
|
3720
|
+
selectedItem,
|
|
3721
|
+
inputValue
|
|
3677
3722
|
};
|
|
3678
3723
|
}
|
|
3679
3724
|
|
|
3680
|
-
|
|
3725
|
+
const defaultStateValues = {
|
|
3681
3726
|
activeIndex: -1,
|
|
3682
3727
|
selectedItems: []
|
|
3683
3728
|
};
|
|
@@ -3717,11 +3762,11 @@ function getDefaultValue(props, propKey) {
|
|
|
3717
3762
|
|
|
3718
3763
|
|
|
3719
3764
|
function getInitialState(props) {
|
|
3720
|
-
|
|
3721
|
-
|
|
3765
|
+
const activeIndex = getInitialValue(props, 'activeIndex');
|
|
3766
|
+
const selectedItems = getInitialValue(props, 'selectedItems');
|
|
3722
3767
|
return {
|
|
3723
|
-
activeIndex
|
|
3724
|
-
selectedItems
|
|
3768
|
+
activeIndex,
|
|
3769
|
+
selectedItems
|
|
3725
3770
|
};
|
|
3726
3771
|
}
|
|
3727
3772
|
/**
|
|
@@ -3740,10 +3785,12 @@ function isKeyDownOperationPermitted(event) {
|
|
|
3740
3785
|
return false;
|
|
3741
3786
|
}
|
|
3742
3787
|
|
|
3743
|
-
|
|
3788
|
+
const element = event.target;
|
|
3744
3789
|
|
|
3745
3790
|
if (element instanceof HTMLInputElement && // if element is a text input
|
|
3746
|
-
element.value !== '' && (
|
|
3791
|
+
element.value !== '' && ( // and we have text in it
|
|
3792
|
+
// and cursor is either not at the start or is currently highlighting text.
|
|
3793
|
+
element.selectionStart !== 0 || element.selectionEnd !== 0)) {
|
|
3747
3794
|
return false;
|
|
3748
3795
|
}
|
|
3749
3796
|
|
|
@@ -3758,12 +3805,14 @@ function isKeyDownOperationPermitted(event) {
|
|
|
3758
3805
|
|
|
3759
3806
|
|
|
3760
3807
|
function getA11yRemovalMessage(selectionParameters) {
|
|
3761
|
-
|
|
3762
|
-
|
|
3808
|
+
const {
|
|
3809
|
+
removedSelectedItem,
|
|
3810
|
+
itemToString: itemToStringLocal
|
|
3811
|
+
} = selectionParameters;
|
|
3763
3812
|
return itemToStringLocal(removedSelectedItem) + " has been removed.";
|
|
3764
3813
|
}
|
|
3765
3814
|
|
|
3766
|
-
|
|
3815
|
+
const propTypes = {
|
|
3767
3816
|
selectedItems: PropTypes.array,
|
|
3768
3817
|
initialSelectedItems: PropTypes.array,
|
|
3769
3818
|
defaultSelectedItems: PropTypes.array,
|
|
@@ -3787,37 +3836,37 @@ var propTypes = {
|
|
|
3787
3836
|
})
|
|
3788
3837
|
})
|
|
3789
3838
|
};
|
|
3790
|
-
|
|
3839
|
+
const defaultProps = {
|
|
3791
3840
|
itemToString: defaultProps$3.itemToString,
|
|
3792
3841
|
stateReducer: defaultProps$3.stateReducer,
|
|
3793
3842
|
environment: defaultProps$3.environment,
|
|
3794
|
-
getA11yRemovalMessage
|
|
3843
|
+
getA11yRemovalMessage,
|
|
3795
3844
|
keyNavigationNext: 'ArrowRight',
|
|
3796
3845
|
keyNavigationPrevious: 'ArrowLeft'
|
|
3797
3846
|
}; // eslint-disable-next-line import/no-mutable-exports
|
|
3798
3847
|
|
|
3799
|
-
|
|
3848
|
+
let validatePropTypes = noop;
|
|
3800
3849
|
/* istanbul ignore next */
|
|
3801
3850
|
|
|
3802
3851
|
if (process.env.NODE_ENV !== 'production') {
|
|
3803
|
-
validatePropTypes =
|
|
3852
|
+
validatePropTypes = (options, caller) => {
|
|
3804
3853
|
PropTypes.checkPropTypes(propTypes, options, 'prop', caller.name);
|
|
3805
3854
|
};
|
|
3806
3855
|
}
|
|
3807
3856
|
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3857
|
+
const SelectedItemClick = process.env.NODE_ENV !== "production" ? '__selected_item_click__' : 0;
|
|
3858
|
+
const SelectedItemKeyDownDelete = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_delete__' : 1;
|
|
3859
|
+
const SelectedItemKeyDownBackspace = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_backspace__' : 2;
|
|
3860
|
+
const SelectedItemKeyDownNavigationNext = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_navigation_next__' : 3;
|
|
3861
|
+
const SelectedItemKeyDownNavigationPrevious = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_navigation_previous__' : 4;
|
|
3862
|
+
const DropdownKeyDownNavigationPrevious = process.env.NODE_ENV !== "production" ? '__dropdown_keydown_navigation_previous__' : 5;
|
|
3863
|
+
const DropdownKeyDownBackspace = process.env.NODE_ENV !== "production" ? '__dropdown_keydown_backspace__' : 6;
|
|
3864
|
+
const DropdownClick = process.env.NODE_ENV !== "production" ? '__dropdown_click__' : 7;
|
|
3865
|
+
const FunctionAddSelectedItem = process.env.NODE_ENV !== "production" ? '__function_add_selected_item__' : 8;
|
|
3866
|
+
const FunctionRemoveSelectedItem = process.env.NODE_ENV !== "production" ? '__function_remove_selected_item__' : 9;
|
|
3867
|
+
const FunctionSetSelectedItems = process.env.NODE_ENV !== "production" ? '__function_set_selected_items__' : 10;
|
|
3868
|
+
const FunctionSetActiveIndex = process.env.NODE_ENV !== "production" ? '__function_set_active_index__' : 11;
|
|
3869
|
+
const FunctionReset = process.env.NODE_ENV !== "production" ? '__function_reset__' : 12;
|
|
3821
3870
|
|
|
3822
3871
|
var stateChangeTypes = /*#__PURE__*/Object.freeze({
|
|
3823
3872
|
__proto__: null,
|
|
@@ -3839,13 +3888,17 @@ var stateChangeTypes = /*#__PURE__*/Object.freeze({
|
|
|
3839
3888
|
/* eslint-disable complexity */
|
|
3840
3889
|
|
|
3841
3890
|
function downshiftMultipleSelectionReducer(state, action) {
|
|
3842
|
-
|
|
3843
|
-
|
|
3844
|
-
|
|
3845
|
-
|
|
3846
|
-
|
|
3847
|
-
|
|
3848
|
-
|
|
3891
|
+
const {
|
|
3892
|
+
type,
|
|
3893
|
+
index,
|
|
3894
|
+
props,
|
|
3895
|
+
selectedItem
|
|
3896
|
+
} = action;
|
|
3897
|
+
const {
|
|
3898
|
+
activeIndex,
|
|
3899
|
+
selectedItems
|
|
3900
|
+
} = state;
|
|
3901
|
+
let changes;
|
|
3849
3902
|
|
|
3850
3903
|
switch (type) {
|
|
3851
3904
|
case SelectedItemClick:
|
|
@@ -3869,7 +3922,7 @@ function downshiftMultipleSelectionReducer(state, action) {
|
|
|
3869
3922
|
case SelectedItemKeyDownBackspace:
|
|
3870
3923
|
case SelectedItemKeyDownDelete:
|
|
3871
3924
|
{
|
|
3872
|
-
|
|
3925
|
+
let newActiveIndex = activeIndex;
|
|
3873
3926
|
|
|
3874
3927
|
if (selectedItems.length === 1) {
|
|
3875
3928
|
newActiveIndex = -1;
|
|
@@ -3877,11 +3930,12 @@ function downshiftMultipleSelectionReducer(state, action) {
|
|
|
3877
3930
|
newActiveIndex = selectedItems.length - 2;
|
|
3878
3931
|
}
|
|
3879
3932
|
|
|
3880
|
-
changes =
|
|
3881
|
-
selectedItems: [
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
|
|
3933
|
+
changes = {
|
|
3934
|
+
selectedItems: [...selectedItems.slice(0, activeIndex), ...selectedItems.slice(activeIndex + 1)],
|
|
3935
|
+
...{
|
|
3936
|
+
activeIndex: newActiveIndex
|
|
3937
|
+
}
|
|
3938
|
+
};
|
|
3885
3939
|
break;
|
|
3886
3940
|
}
|
|
3887
3941
|
|
|
@@ -3899,7 +3953,7 @@ function downshiftMultipleSelectionReducer(state, action) {
|
|
|
3899
3953
|
|
|
3900
3954
|
case FunctionAddSelectedItem:
|
|
3901
3955
|
changes = {
|
|
3902
|
-
selectedItems: [
|
|
3956
|
+
selectedItems: [...selectedItems, selectedItem]
|
|
3903
3957
|
};
|
|
3904
3958
|
break;
|
|
3905
3959
|
|
|
@@ -3911,26 +3965,30 @@ function downshiftMultipleSelectionReducer(state, action) {
|
|
|
3911
3965
|
|
|
3912
3966
|
case FunctionRemoveSelectedItem:
|
|
3913
3967
|
{
|
|
3914
|
-
|
|
3915
|
-
|
|
3968
|
+
let newActiveIndex = activeIndex;
|
|
3969
|
+
const selectedItemIndex = selectedItems.indexOf(selectedItem);
|
|
3970
|
+
|
|
3971
|
+
if (selectedItemIndex >= 0) {
|
|
3972
|
+
if (selectedItems.length === 1) {
|
|
3973
|
+
newActiveIndex = -1;
|
|
3974
|
+
} else if (selectedItemIndex === selectedItems.length - 1) {
|
|
3975
|
+
newActiveIndex = selectedItems.length - 2;
|
|
3976
|
+
}
|
|
3916
3977
|
|
|
3917
|
-
|
|
3918
|
-
|
|
3919
|
-
|
|
3920
|
-
|
|
3978
|
+
changes = {
|
|
3979
|
+
selectedItems: [...selectedItems.slice(0, selectedItemIndex), ...selectedItems.slice(selectedItemIndex + 1)],
|
|
3980
|
+
activeIndex: newActiveIndex
|
|
3981
|
+
};
|
|
3921
3982
|
}
|
|
3922
3983
|
|
|
3923
|
-
changes = _extends({
|
|
3924
|
-
selectedItems: [].concat(selectedItems.slice(0, selectedItemIndex), selectedItems.slice(selectedItemIndex + 1))
|
|
3925
|
-
}, {
|
|
3926
|
-
activeIndex: _newActiveIndex
|
|
3927
|
-
});
|
|
3928
3984
|
break;
|
|
3929
3985
|
}
|
|
3930
3986
|
|
|
3931
3987
|
case FunctionSetSelectedItems:
|
|
3932
3988
|
{
|
|
3933
|
-
|
|
3989
|
+
const {
|
|
3990
|
+
selectedItems: newSelectedItems
|
|
3991
|
+
} = action;
|
|
3934
3992
|
changes = {
|
|
3935
3993
|
selectedItems: newSelectedItems
|
|
3936
3994
|
};
|
|
@@ -3939,9 +3997,11 @@ function downshiftMultipleSelectionReducer(state, action) {
|
|
|
3939
3997
|
|
|
3940
3998
|
case FunctionSetActiveIndex:
|
|
3941
3999
|
{
|
|
3942
|
-
|
|
4000
|
+
const {
|
|
4001
|
+
activeIndex: newActiveIndex
|
|
4002
|
+
} = action;
|
|
3943
4003
|
changes = {
|
|
3944
|
-
activeIndex:
|
|
4004
|
+
activeIndex: newActiveIndex
|
|
3945
4005
|
};
|
|
3946
4006
|
break;
|
|
3947
4007
|
}
|
|
@@ -3957,11 +4017,11 @@ function downshiftMultipleSelectionReducer(state, action) {
|
|
|
3957
4017
|
throw new Error('Reducer called without proper action type.');
|
|
3958
4018
|
}
|
|
3959
4019
|
|
|
3960
|
-
return
|
|
4020
|
+
return { ...state,
|
|
4021
|
+
...changes
|
|
4022
|
+
};
|
|
3961
4023
|
}
|
|
3962
4024
|
|
|
3963
|
-
var _excluded = ["refKey", "ref", "onClick", "onKeyDown", "selectedItem", "index"],
|
|
3964
|
-
_excluded2 = ["refKey", "ref", "onKeyDown", "onClick", "preventKeyAction"];
|
|
3965
4025
|
useMultipleSelection.stateChangeTypes = stateChangeTypes;
|
|
3966
4026
|
|
|
3967
4027
|
function useMultipleSelection(userProps) {
|
|
@@ -3971,47 +4031,47 @@ function useMultipleSelection(userProps) {
|
|
|
3971
4031
|
|
|
3972
4032
|
validatePropTypes(userProps, useMultipleSelection); // Props defaults and destructuring.
|
|
3973
4033
|
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
|
|
3985
|
-
|
|
3986
|
-
|
|
3987
|
-
|
|
3988
|
-
|
|
3989
|
-
|
|
3990
|
-
|
|
3991
|
-
|
|
3992
|
-
|
|
4034
|
+
const props = { ...defaultProps,
|
|
4035
|
+
...userProps
|
|
4036
|
+
};
|
|
4037
|
+
const {
|
|
4038
|
+
getA11yRemovalMessage,
|
|
4039
|
+
itemToString,
|
|
4040
|
+
environment,
|
|
4041
|
+
keyNavigationNext,
|
|
4042
|
+
keyNavigationPrevious
|
|
4043
|
+
} = props; // Reducer init.
|
|
4044
|
+
|
|
4045
|
+
const [state, dispatch] = useControlledReducer$1(downshiftMultipleSelectionReducer, getInitialState(props), props);
|
|
4046
|
+
const {
|
|
4047
|
+
activeIndex,
|
|
4048
|
+
selectedItems
|
|
4049
|
+
} = state; // Refs.
|
|
4050
|
+
|
|
4051
|
+
const isInitialMountRef = useRef(true);
|
|
4052
|
+
const dropdownRef = useRef(null);
|
|
4053
|
+
const previousSelectedItemsRef = useRef(selectedItems);
|
|
4054
|
+
const selectedItemRefs = useRef();
|
|
3993
4055
|
selectedItemRefs.current = [];
|
|
3994
|
-
|
|
3995
|
-
state
|
|
3996
|
-
props
|
|
4056
|
+
const latest = useLatestRef({
|
|
4057
|
+
state,
|
|
4058
|
+
props
|
|
3997
4059
|
}); // Effects.
|
|
3998
4060
|
|
|
3999
4061
|
/* Sets a11y status message on changes in selectedItem. */
|
|
4000
4062
|
|
|
4001
|
-
useEffect(
|
|
4063
|
+
useEffect(() => {
|
|
4002
4064
|
if (isInitialMountRef.current) {
|
|
4003
4065
|
return;
|
|
4004
4066
|
}
|
|
4005
4067
|
|
|
4006
4068
|
if (selectedItems.length < previousSelectedItemsRef.current.length) {
|
|
4007
|
-
|
|
4008
|
-
return selectedItems.indexOf(item) < 0;
|
|
4009
|
-
});
|
|
4069
|
+
const removedSelectedItem = previousSelectedItemsRef.current.find(item => selectedItems.indexOf(item) < 0);
|
|
4010
4070
|
setStatus(getA11yRemovalMessage({
|
|
4011
|
-
itemToString
|
|
4071
|
+
itemToString,
|
|
4012
4072
|
resultCount: selectedItems.length,
|
|
4013
|
-
removedSelectedItem
|
|
4014
|
-
activeIndex
|
|
4073
|
+
removedSelectedItem,
|
|
4074
|
+
activeIndex,
|
|
4015
4075
|
activeSelectedItem: selectedItems[activeIndex]
|
|
4016
4076
|
}), environment.document);
|
|
4017
4077
|
}
|
|
@@ -4019,7 +4079,7 @@ function useMultipleSelection(userProps) {
|
|
|
4019
4079
|
previousSelectedItemsRef.current = selectedItems; // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
4020
4080
|
}, [selectedItems.length]); // Sets focus on active item.
|
|
4021
4081
|
|
|
4022
|
-
useEffect(
|
|
4082
|
+
useEffect(() => {
|
|
4023
4083
|
if (isInitialMountRef.current) {
|
|
4024
4084
|
return;
|
|
4025
4085
|
}
|
|
@@ -4032,177 +4092,187 @@ function useMultipleSelection(userProps) {
|
|
|
4032
4092
|
}, [activeIndex]);
|
|
4033
4093
|
useControlPropsValidator({
|
|
4034
4094
|
isInitialMount: isInitialMountRef.current,
|
|
4035
|
-
props
|
|
4036
|
-
state
|
|
4095
|
+
props,
|
|
4096
|
+
state
|
|
4037
4097
|
});
|
|
4038
|
-
|
|
4098
|
+
const setGetterPropCallInfo = useGetterPropsCalledChecker('getDropdownProps'); // Make initial ref false.
|
|
4039
4099
|
|
|
4040
|
-
useEffect(
|
|
4100
|
+
useEffect(() => {
|
|
4041
4101
|
isInitialMountRef.current = false;
|
|
4042
4102
|
}, []); // Event handler functions.
|
|
4043
4103
|
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
return _ref = {}, _ref[keyNavigationPrevious] = function () {
|
|
4104
|
+
const selectedItemKeyDownHandlers = useMemo(() => ({
|
|
4105
|
+
[keyNavigationPrevious]() {
|
|
4048
4106
|
dispatch({
|
|
4049
4107
|
type: SelectedItemKeyDownNavigationPrevious
|
|
4050
4108
|
});
|
|
4051
|
-
},
|
|
4109
|
+
},
|
|
4110
|
+
|
|
4111
|
+
[keyNavigationNext]() {
|
|
4052
4112
|
dispatch({
|
|
4053
4113
|
type: SelectedItemKeyDownNavigationNext
|
|
4054
4114
|
});
|
|
4055
|
-
},
|
|
4115
|
+
},
|
|
4116
|
+
|
|
4117
|
+
Delete() {
|
|
4056
4118
|
dispatch({
|
|
4057
4119
|
type: SelectedItemKeyDownDelete
|
|
4058
4120
|
});
|
|
4059
|
-
},
|
|
4121
|
+
},
|
|
4122
|
+
|
|
4123
|
+
Backspace() {
|
|
4060
4124
|
dispatch({
|
|
4061
4125
|
type: SelectedItemKeyDownBackspace
|
|
4062
4126
|
});
|
|
4063
|
-
}
|
|
4064
|
-
}, [dispatch, keyNavigationNext, keyNavigationPrevious]);
|
|
4065
|
-
var dropdownKeyDownHandlers = useMemo(function () {
|
|
4066
|
-
var _ref2;
|
|
4127
|
+
}
|
|
4067
4128
|
|
|
4068
|
-
|
|
4129
|
+
}), [dispatch, keyNavigationNext, keyNavigationPrevious]);
|
|
4130
|
+
const dropdownKeyDownHandlers = useMemo(() => ({
|
|
4131
|
+
[keyNavigationPrevious](event) {
|
|
4069
4132
|
if (isKeyDownOperationPermitted(event)) {
|
|
4070
4133
|
dispatch({
|
|
4071
4134
|
type: DropdownKeyDownNavigationPrevious
|
|
4072
4135
|
});
|
|
4073
4136
|
}
|
|
4074
|
-
},
|
|
4137
|
+
},
|
|
4138
|
+
|
|
4139
|
+
Backspace(event) {
|
|
4075
4140
|
if (isKeyDownOperationPermitted(event)) {
|
|
4076
4141
|
dispatch({
|
|
4077
4142
|
type: DropdownKeyDownBackspace
|
|
4078
4143
|
});
|
|
4079
4144
|
}
|
|
4080
|
-
}
|
|
4081
|
-
}, [dispatch, keyNavigationPrevious]); // Getter props.
|
|
4082
|
-
|
|
4083
|
-
var getSelectedItemProps = useCallback(function (_temp) {
|
|
4084
|
-
var _extends2;
|
|
4085
|
-
|
|
4086
|
-
var _ref3 = _temp === void 0 ? {} : _temp,
|
|
4087
|
-
_ref3$refKey = _ref3.refKey,
|
|
4088
|
-
refKey = _ref3$refKey === void 0 ? 'ref' : _ref3$refKey,
|
|
4089
|
-
ref = _ref3.ref,
|
|
4090
|
-
onClick = _ref3.onClick,
|
|
4091
|
-
onKeyDown = _ref3.onKeyDown,
|
|
4092
|
-
selectedItem = _ref3.selectedItem,
|
|
4093
|
-
index = _ref3.index,
|
|
4094
|
-
rest = _objectWithoutPropertiesLoose(_ref3, _excluded);
|
|
4145
|
+
}
|
|
4095
4146
|
|
|
4096
|
-
|
|
4097
|
-
|
|
4147
|
+
}), [dispatch, keyNavigationPrevious]); // Getter props.
|
|
4148
|
+
|
|
4149
|
+
const getSelectedItemProps = useCallback(function (_temp) {
|
|
4150
|
+
let {
|
|
4151
|
+
refKey = 'ref',
|
|
4152
|
+
ref,
|
|
4153
|
+
onClick,
|
|
4154
|
+
onKeyDown,
|
|
4155
|
+
selectedItem,
|
|
4156
|
+
index,
|
|
4157
|
+
...rest
|
|
4158
|
+
} = _temp === void 0 ? {} : _temp;
|
|
4159
|
+
const {
|
|
4160
|
+
state: latestState
|
|
4161
|
+
} = latest.current;
|
|
4162
|
+
const itemIndex = getItemIndex(index, selectedItem, latestState.selectedItems);
|
|
4098
4163
|
|
|
4099
4164
|
if (itemIndex < 0) {
|
|
4100
4165
|
throw new Error('Pass either selectedItem or index in getSelectedItemProps!');
|
|
4101
4166
|
}
|
|
4102
4167
|
|
|
4103
|
-
|
|
4168
|
+
const selectedItemHandleClick = () => {
|
|
4104
4169
|
dispatch({
|
|
4105
4170
|
type: SelectedItemClick,
|
|
4106
|
-
index
|
|
4171
|
+
index
|
|
4107
4172
|
});
|
|
4108
4173
|
};
|
|
4109
4174
|
|
|
4110
|
-
|
|
4111
|
-
|
|
4175
|
+
const selectedItemHandleKeyDown = event => {
|
|
4176
|
+
const key = normalizeArrowKey(event);
|
|
4112
4177
|
|
|
4113
4178
|
if (key && selectedItemKeyDownHandlers[key]) {
|
|
4114
4179
|
selectedItemKeyDownHandlers[key](event);
|
|
4115
4180
|
}
|
|
4116
4181
|
};
|
|
4117
4182
|
|
|
4118
|
-
return
|
|
4119
|
-
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4183
|
+
return {
|
|
4184
|
+
[refKey]: handleRefs(ref, selectedItemNode => {
|
|
4185
|
+
if (selectedItemNode) {
|
|
4186
|
+
selectedItemRefs.current.push(selectedItemNode);
|
|
4187
|
+
}
|
|
4188
|
+
}),
|
|
4189
|
+
tabIndex: index === latestState.activeIndex ? 0 : -1,
|
|
4190
|
+
onClick: callAllEventHandlers(onClick, selectedItemHandleClick),
|
|
4191
|
+
onKeyDown: callAllEventHandlers(onKeyDown, selectedItemHandleKeyDown),
|
|
4192
|
+
...rest
|
|
4193
|
+
};
|
|
4123
4194
|
}, [dispatch, latest, selectedItemKeyDownHandlers]);
|
|
4124
|
-
|
|
4125
|
-
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
var _ref5 = _temp3 === void 0 ? {} : _temp3,
|
|
4138
|
-
_ref5$suppressRefErro = _ref5.suppressRefError,
|
|
4139
|
-
suppressRefError = _ref5$suppressRefErro === void 0 ? false : _ref5$suppressRefErro;
|
|
4140
|
-
|
|
4195
|
+
const getDropdownProps = useCallback(function (_temp2, _temp3) {
|
|
4196
|
+
let {
|
|
4197
|
+
refKey = 'ref',
|
|
4198
|
+
ref,
|
|
4199
|
+
onKeyDown,
|
|
4200
|
+
onClick,
|
|
4201
|
+
preventKeyAction = false,
|
|
4202
|
+
...rest
|
|
4203
|
+
} = _temp2 === void 0 ? {} : _temp2;
|
|
4204
|
+
let {
|
|
4205
|
+
suppressRefError = false
|
|
4206
|
+
} = _temp3 === void 0 ? {} : _temp3;
|
|
4141
4207
|
setGetterPropCallInfo('getDropdownProps', suppressRefError, refKey, dropdownRef);
|
|
4142
4208
|
|
|
4143
|
-
|
|
4144
|
-
|
|
4209
|
+
const dropdownHandleKeyDown = event => {
|
|
4210
|
+
const key = normalizeArrowKey(event);
|
|
4145
4211
|
|
|
4146
4212
|
if (key && dropdownKeyDownHandlers[key]) {
|
|
4147
4213
|
dropdownKeyDownHandlers[key](event);
|
|
4148
4214
|
}
|
|
4149
4215
|
};
|
|
4150
4216
|
|
|
4151
|
-
|
|
4217
|
+
const dropdownHandleClick = () => {
|
|
4152
4218
|
dispatch({
|
|
4153
4219
|
type: DropdownClick
|
|
4154
4220
|
});
|
|
4155
4221
|
};
|
|
4156
4222
|
|
|
4157
|
-
return
|
|
4158
|
-
|
|
4159
|
-
|
|
4160
|
-
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
|
|
4164
|
-
|
|
4223
|
+
return {
|
|
4224
|
+
[refKey]: handleRefs(ref, dropdownNode => {
|
|
4225
|
+
if (dropdownNode) {
|
|
4226
|
+
dropdownRef.current = dropdownNode;
|
|
4227
|
+
}
|
|
4228
|
+
}),
|
|
4229
|
+
...(!preventKeyAction && {
|
|
4230
|
+
onKeyDown: callAllEventHandlers(onKeyDown, dropdownHandleKeyDown),
|
|
4231
|
+
onClick: callAllEventHandlers(onClick, dropdownHandleClick)
|
|
4232
|
+
}),
|
|
4233
|
+
...rest
|
|
4234
|
+
};
|
|
4165
4235
|
}, [dispatch, dropdownKeyDownHandlers, setGetterPropCallInfo]); // returns
|
|
4166
4236
|
|
|
4167
|
-
|
|
4237
|
+
const addSelectedItem = useCallback(selectedItem => {
|
|
4168
4238
|
dispatch({
|
|
4169
4239
|
type: FunctionAddSelectedItem,
|
|
4170
|
-
selectedItem
|
|
4240
|
+
selectedItem
|
|
4171
4241
|
});
|
|
4172
4242
|
}, [dispatch]);
|
|
4173
|
-
|
|
4243
|
+
const removeSelectedItem = useCallback(selectedItem => {
|
|
4174
4244
|
dispatch({
|
|
4175
4245
|
type: FunctionRemoveSelectedItem,
|
|
4176
|
-
selectedItem
|
|
4246
|
+
selectedItem
|
|
4177
4247
|
});
|
|
4178
4248
|
}, [dispatch]);
|
|
4179
|
-
|
|
4249
|
+
const setSelectedItems = useCallback(newSelectedItems => {
|
|
4180
4250
|
dispatch({
|
|
4181
4251
|
type: FunctionSetSelectedItems,
|
|
4182
4252
|
selectedItems: newSelectedItems
|
|
4183
4253
|
});
|
|
4184
4254
|
}, [dispatch]);
|
|
4185
|
-
|
|
4255
|
+
const setActiveIndex = useCallback(newActiveIndex => {
|
|
4186
4256
|
dispatch({
|
|
4187
4257
|
type: FunctionSetActiveIndex,
|
|
4188
4258
|
activeIndex: newActiveIndex
|
|
4189
4259
|
});
|
|
4190
4260
|
}, [dispatch]);
|
|
4191
|
-
|
|
4261
|
+
const reset = useCallback(() => {
|
|
4192
4262
|
dispatch({
|
|
4193
4263
|
type: FunctionReset
|
|
4194
4264
|
});
|
|
4195
4265
|
}, [dispatch]);
|
|
4196
4266
|
return {
|
|
4197
|
-
getSelectedItemProps
|
|
4198
|
-
getDropdownProps
|
|
4199
|
-
addSelectedItem
|
|
4200
|
-
removeSelectedItem
|
|
4201
|
-
setSelectedItems
|
|
4202
|
-
setActiveIndex
|
|
4203
|
-
reset
|
|
4204
|
-
selectedItems
|
|
4205
|
-
activeIndex
|
|
4267
|
+
getSelectedItemProps,
|
|
4268
|
+
getDropdownProps,
|
|
4269
|
+
addSelectedItem,
|
|
4270
|
+
removeSelectedItem,
|
|
4271
|
+
setSelectedItems,
|
|
4272
|
+
setActiveIndex,
|
|
4273
|
+
reset,
|
|
4274
|
+
selectedItems,
|
|
4275
|
+
activeIndex
|
|
4206
4276
|
};
|
|
4207
4277
|
}
|
|
4208
4278
|
|