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