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
|
@@ -8,55 +8,6 @@
|
|
|
8
8
|
|
|
9
9
|
var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
|
|
10
10
|
|
|
11
|
-
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
12
|
-
if (source == null) return {};
|
|
13
|
-
var target = {};
|
|
14
|
-
var sourceKeys = Object.keys(source);
|
|
15
|
-
var key, i;
|
|
16
|
-
for (i = 0; i < sourceKeys.length; i++) {
|
|
17
|
-
key = sourceKeys[i];
|
|
18
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
19
|
-
target[key] = source[key];
|
|
20
|
-
}
|
|
21
|
-
return target;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function _extends() {
|
|
25
|
-
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
26
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
27
|
-
var source = arguments[i];
|
|
28
|
-
for (var key in source) {
|
|
29
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
30
|
-
target[key] = source[key];
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return target;
|
|
35
|
-
};
|
|
36
|
-
return _extends.apply(this, arguments);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function _assertThisInitialized(self) {
|
|
40
|
-
if (self === void 0) {
|
|
41
|
-
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
42
|
-
}
|
|
43
|
-
return self;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function _setPrototypeOf(o, p) {
|
|
47
|
-
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
|
|
48
|
-
o.__proto__ = p;
|
|
49
|
-
return o;
|
|
50
|
-
};
|
|
51
|
-
return _setPrototypeOf(o, p);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function _inheritsLoose(subClass, superClass) {
|
|
55
|
-
subClass.prototype = Object.create(superClass.prototype);
|
|
56
|
-
subClass.prototype.constructor = subClass;
|
|
57
|
-
_setPrototypeOf(subClass, superClass);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
11
|
var reactIs = {exports: {}};
|
|
61
12
|
|
|
62
13
|
/** @license React v17.0.2
|
|
@@ -296,9 +247,9 @@
|
|
|
296
247
|
reactIs.exports = reactIs_development;
|
|
297
248
|
}
|
|
298
249
|
|
|
299
|
-
|
|
250
|
+
let e=e=>"object"==typeof e&&null!=e&&1===e.nodeType,t=(e,t)=>(!t||"hidden"!==e)&&("visible"!==e&&"clip"!==e),n=(e,n)=>{if(e.clientHeight<e.scrollHeight||e.clientWidth<e.scrollWidth){let l=getComputedStyle(e,null);return t(l.overflowY,n)||t(l.overflowX,n)||(e=>{let t=(e=>{if(!e.ownerDocument||!e.ownerDocument.defaultView)return null;try{return e.ownerDocument.defaultView.frameElement}catch(e){return null}})(e);return !!t&&(t.clientHeight<e.scrollHeight||t.clientWidth<e.scrollWidth)})(e)}return !1},l=(e,t,n,l,i,o,r,d)=>o<e&&r>t||o>e&&r<t?0:o<=e&&d<=n||r>=t&&d>=n?o-e-l:r>t&&d<n||o<e&&d>n?r-t+i:0,i=e=>{let t=e.parentElement;return null==t?e.getRootNode().host||null:t};var o=(t,o)=>{var r,d,h,f,u,s;if("undefined"==typeof document)return [];let{scrollMode:a,block:c,inline:g,boundary:m,skipOverflowHiddenElements:p}=o,w="function"==typeof m?m:e=>e!==m;if(!e(t))throw new TypeError("Invalid target");let W=document.scrollingElement||document.documentElement,H=[],b=t;for(;e(b)&&w(b);){if(b=i(b),b===W){H.push(b);break}null!=b&&b===document.body&&n(b)&&!n(document.documentElement)||null!=b&&n(b,p)&&H.push(b);}let v=null!=(d=null==(r=window.visualViewport)?void 0:r.width)?d:innerWidth,y=null!=(f=null==(h=window.visualViewport)?void 0:h.height)?f:innerHeight,E=null!=(u=window.scrollX)?u:pageXOffset,M=null!=(s=window.scrollY)?s:pageYOffset,{height:x,width:I,top:C,right:R,bottom:T,left:V}=t.getBoundingClientRect(),k="start"===c||"nearest"===c?C:"end"===c?T:C+x/2,B="center"===g?V+I/2:"end"===g?R:V,D=[];for(let e=0;e<H.length;e++){let t=H[e],{height:n,width:i,top:o,right:r,bottom:d,left:h}=t.getBoundingClientRect();if("if-needed"===a&&C>=0&&V>=0&&T<=y&&R<=v&&C>=o&&T<=d&&V>=h&&R<=r)return D;let f=getComputedStyle(t),u=parseInt(f.borderLeftWidth,10),s=parseInt(f.borderTopWidth,10),m=parseInt(f.borderRightWidth,10),p=parseInt(f.borderBottomWidth,10),w=0,b=0,O="offsetWidth"in t?t.offsetWidth-t.clientWidth-u-m:0,X="offsetHeight"in t?t.offsetHeight-t.clientHeight-s-p:0,Y="offsetWidth"in t?0===t.offsetWidth?0:i/t.offsetWidth:0,L="offsetHeight"in t?0===t.offsetHeight?0:n/t.offsetHeight:0;if(W===t)w="start"===c?k:"end"===c?k-y:"nearest"===c?l(M,M+y,y,s,p,M+k,M+k+x,x):k-y/2,b="start"===g?B:"center"===g?B-v/2:"end"===g?B-v:l(E,E+v,v,u,m,E+B,E+B+I,I),w=Math.max(0,w+M),b=Math.max(0,b+E);else {w="start"===c?k-o-s:"end"===c?k-d+p+X:"nearest"===c?l(o,d,n,s,p+X,k,k+x,x):k-(o+n/2)+X/2,b="start"===g?B-h-u:"center"===g?B-(h+i/2)+O/2:"end"===g?B-r+m+O:l(h,r,i,u,m+O,B,B+I,I);let{scrollLeft:e,scrollTop:f}=t;w=Math.max(0,Math.min(f+w/L,t.scrollHeight-n/L+X)),b=Math.max(0,Math.min(e+b/Y,t.scrollWidth-i/Y+O)),k+=f-w,B+=e-b;}D.push({el:t,top:w,left:b});}return D};
|
|
300
251
|
|
|
301
|
-
|
|
252
|
+
let idCounter = 0;
|
|
302
253
|
|
|
303
254
|
/**
|
|
304
255
|
* Accepts a parameter and returns it if it's a function
|
|
@@ -322,15 +273,17 @@
|
|
|
322
273
|
if (!node) {
|
|
323
274
|
return;
|
|
324
275
|
}
|
|
325
|
-
|
|
276
|
+
const actions = o(node, {
|
|
326
277
|
boundary: menuNode,
|
|
327
278
|
block: 'nearest',
|
|
328
279
|
scrollMode: 'if-needed'
|
|
329
280
|
});
|
|
330
|
-
actions.forEach(
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
281
|
+
actions.forEach(_ref => {
|
|
282
|
+
let {
|
|
283
|
+
el,
|
|
284
|
+
top,
|
|
285
|
+
left
|
|
286
|
+
} = _ref;
|
|
334
287
|
el.scrollTop = top;
|
|
335
288
|
el.scrollLeft = left;
|
|
336
289
|
});
|
|
@@ -343,7 +296,7 @@
|
|
|
343
296
|
* @return {Boolean} whether the parent is the child or the child is in the parent
|
|
344
297
|
*/
|
|
345
298
|
function isOrContainsNode(parent, child, environment) {
|
|
346
|
-
|
|
299
|
+
const result = parent === child || child instanceof environment.Node && parent.contains && parent.contains(child);
|
|
347
300
|
return result;
|
|
348
301
|
}
|
|
349
302
|
|
|
@@ -356,7 +309,7 @@
|
|
|
356
309
|
* @return {Function} the debounced function
|
|
357
310
|
*/
|
|
358
311
|
function debounce(fn, time) {
|
|
359
|
-
|
|
312
|
+
let timeoutId;
|
|
360
313
|
function cancel() {
|
|
361
314
|
if (timeoutId) {
|
|
362
315
|
clearTimeout(timeoutId);
|
|
@@ -367,9 +320,9 @@
|
|
|
367
320
|
args[_key] = arguments[_key];
|
|
368
321
|
}
|
|
369
322
|
cancel();
|
|
370
|
-
timeoutId = setTimeout(
|
|
323
|
+
timeoutId = setTimeout(() => {
|
|
371
324
|
timeoutId = null;
|
|
372
|
-
fn
|
|
325
|
+
fn(...args);
|
|
373
326
|
}, time);
|
|
374
327
|
}
|
|
375
328
|
wrapper.cancel = cancel;
|
|
@@ -391,9 +344,9 @@
|
|
|
391
344
|
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
392
345
|
args[_key3 - 1] = arguments[_key3];
|
|
393
346
|
}
|
|
394
|
-
return fns.some(
|
|
347
|
+
return fns.some(fn => {
|
|
395
348
|
if (fn) {
|
|
396
|
-
fn
|
|
349
|
+
fn(event, ...args);
|
|
397
350
|
}
|
|
398
351
|
return event.preventDownshiftDefault || event.hasOwnProperty('nativeEvent') && event.nativeEvent.preventDownshiftDefault;
|
|
399
352
|
});
|
|
@@ -403,8 +356,8 @@
|
|
|
403
356
|
for (var _len4 = arguments.length, refs = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
404
357
|
refs[_key4] = arguments[_key4];
|
|
405
358
|
}
|
|
406
|
-
return
|
|
407
|
-
refs.forEach(
|
|
359
|
+
return node => {
|
|
360
|
+
refs.forEach(ref => {
|
|
408
361
|
if (typeof ref === 'function') {
|
|
409
362
|
ref(node);
|
|
410
363
|
} else if (ref) {
|
|
@@ -438,9 +391,11 @@
|
|
|
438
391
|
* @return {String} the a11y status message
|
|
439
392
|
*/
|
|
440
393
|
function getA11yStatusMessage$1(_ref2) {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
394
|
+
let {
|
|
395
|
+
isOpen,
|
|
396
|
+
resultCount,
|
|
397
|
+
previousResultCount
|
|
398
|
+
} = _ref2;
|
|
444
399
|
if (!isOpen) {
|
|
445
400
|
return '';
|
|
446
401
|
}
|
|
@@ -448,7 +403,7 @@
|
|
|
448
403
|
return 'No results are available.';
|
|
449
404
|
}
|
|
450
405
|
if (resultCount !== previousResultCount) {
|
|
451
|
-
return resultCount
|
|
406
|
+
return `${resultCount} result${resultCount === 1 ? ' is' : 's are'} available, use up and down arrow keys to navigate. Press Enter key to select.`;
|
|
452
407
|
}
|
|
453
408
|
return '';
|
|
454
409
|
}
|
|
@@ -502,9 +457,9 @@
|
|
|
502
457
|
*/
|
|
503
458
|
function requiredProp(fnName, propName) {
|
|
504
459
|
// eslint-disable-next-line no-console
|
|
505
|
-
console.error(
|
|
460
|
+
console.error(`The property "${propName}" is required in "${fnName}"`);
|
|
506
461
|
}
|
|
507
|
-
|
|
462
|
+
const stateKeys = ['highlightedIndex', 'inputValue', 'isOpen', 'selectedItem', 'type'];
|
|
508
463
|
/**
|
|
509
464
|
* @param {Object} state the state object
|
|
510
465
|
* @return {Object} state that is relevant to downshift
|
|
@@ -513,8 +468,8 @@
|
|
|
513
468
|
if (state === void 0) {
|
|
514
469
|
state = {};
|
|
515
470
|
}
|
|
516
|
-
|
|
517
|
-
stateKeys.forEach(
|
|
471
|
+
const result = {};
|
|
472
|
+
stateKeys.forEach(k => {
|
|
518
473
|
if (state.hasOwnProperty(k)) {
|
|
519
474
|
result[k] = state[k];
|
|
520
475
|
}
|
|
@@ -534,7 +489,7 @@
|
|
|
534
489
|
* @returns {Object} The merged controlled state.
|
|
535
490
|
*/
|
|
536
491
|
function getState(state, props) {
|
|
537
|
-
return Object.keys(state).reduce(
|
|
492
|
+
return Object.keys(state).reduce((prevState, key) => {
|
|
538
493
|
prevState[key] = isControlledProp(props, key) ? props[key] : state[key];
|
|
539
494
|
return prevState;
|
|
540
495
|
}, {});
|
|
@@ -559,11 +514,13 @@
|
|
|
559
514
|
* @return {String} keyboard key
|
|
560
515
|
*/
|
|
561
516
|
function normalizeArrowKey(event) {
|
|
562
|
-
|
|
563
|
-
|
|
517
|
+
const {
|
|
518
|
+
key,
|
|
519
|
+
keyCode
|
|
520
|
+
} = event;
|
|
564
521
|
/* istanbul ignore next (ie) */
|
|
565
522
|
if (keyCode >= 37 && keyCode <= 40 && key.indexOf('Arrow') !== 0) {
|
|
566
|
-
return
|
|
523
|
+
return `Arrow${key}`;
|
|
567
524
|
}
|
|
568
525
|
return key;
|
|
569
526
|
}
|
|
@@ -595,17 +552,17 @@
|
|
|
595
552
|
if (itemCount === 0) {
|
|
596
553
|
return -1;
|
|
597
554
|
}
|
|
598
|
-
|
|
555
|
+
const itemsLastIndex = itemCount - 1;
|
|
599
556
|
if (typeof baseIndex !== 'number' || baseIndex < 0 || baseIndex >= itemCount) {
|
|
600
557
|
baseIndex = moveAmount > 0 ? -1 : itemsLastIndex + 1;
|
|
601
558
|
}
|
|
602
|
-
|
|
559
|
+
let newIndex = baseIndex + moveAmount;
|
|
603
560
|
if (newIndex < 0) {
|
|
604
561
|
newIndex = circular ? itemsLastIndex : 0;
|
|
605
562
|
} else if (newIndex > itemsLastIndex) {
|
|
606
563
|
newIndex = circular ? 0 : itemsLastIndex;
|
|
607
564
|
}
|
|
608
|
-
|
|
565
|
+
const nonDisabledNewIndex = getNextNonDisabledIndex(moveAmount, newIndex, itemCount, getItemNodeFromIndex, circular);
|
|
609
566
|
if (nonDisabledNewIndex === -1) {
|
|
610
567
|
return baseIndex >= itemCount ? -1 : baseIndex;
|
|
611
568
|
}
|
|
@@ -623,20 +580,20 @@
|
|
|
623
580
|
* @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.
|
|
624
581
|
*/
|
|
625
582
|
function getNextNonDisabledIndex(moveAmount, baseIndex, itemCount, getItemNodeFromIndex, circular) {
|
|
626
|
-
|
|
583
|
+
const currentElementNode = getItemNodeFromIndex(baseIndex);
|
|
627
584
|
if (!currentElementNode || !currentElementNode.hasAttribute('disabled')) {
|
|
628
585
|
return baseIndex;
|
|
629
586
|
}
|
|
630
587
|
if (moveAmount > 0) {
|
|
631
|
-
for (
|
|
588
|
+
for (let index = baseIndex + 1; index < itemCount; index++) {
|
|
632
589
|
if (!getItemNodeFromIndex(index).hasAttribute('disabled')) {
|
|
633
590
|
return index;
|
|
634
591
|
}
|
|
635
592
|
}
|
|
636
593
|
} else {
|
|
637
|
-
for (
|
|
638
|
-
if (!getItemNodeFromIndex(
|
|
639
|
-
return
|
|
594
|
+
for (let index = baseIndex - 1; index >= 0; index--) {
|
|
595
|
+
if (!getItemNodeFromIndex(index).hasAttribute('disabled')) {
|
|
596
|
+
return index;
|
|
640
597
|
}
|
|
641
598
|
}
|
|
642
599
|
}
|
|
@@ -660,30 +617,28 @@
|
|
|
660
617
|
if (checkActiveElement === void 0) {
|
|
661
618
|
checkActiveElement = true;
|
|
662
619
|
}
|
|
663
|
-
return downshiftElements.some(
|
|
664
|
-
return contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment));
|
|
665
|
-
});
|
|
620
|
+
return downshiftElements.some(contextNode => contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment)));
|
|
666
621
|
}
|
|
667
622
|
|
|
668
623
|
// eslint-disable-next-line import/no-mutable-exports
|
|
669
|
-
|
|
624
|
+
let validateControlledUnchanged = noop;
|
|
670
625
|
/* istanbul ignore next */
|
|
671
626
|
{
|
|
672
|
-
validateControlledUnchanged =
|
|
673
|
-
|
|
674
|
-
Object.keys(state).forEach(
|
|
627
|
+
validateControlledUnchanged = (state, prevProps, nextProps) => {
|
|
628
|
+
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`;
|
|
629
|
+
Object.keys(state).forEach(propKey => {
|
|
675
630
|
if (prevProps[propKey] !== undefined && nextProps[propKey] === undefined) {
|
|
676
631
|
// eslint-disable-next-line no-console
|
|
677
|
-
console.error(
|
|
632
|
+
console.error(`downshift: A component has changed the controlled prop "${propKey}" to be uncontrolled. ${warningDescription}`);
|
|
678
633
|
} else if (prevProps[propKey] === undefined && nextProps[propKey] !== undefined) {
|
|
679
634
|
// eslint-disable-next-line no-console
|
|
680
|
-
console.error(
|
|
635
|
+
console.error(`downshift: A component has changed the uncontrolled prop "${propKey}" to be controlled. ${warningDescription}`);
|
|
681
636
|
}
|
|
682
637
|
});
|
|
683
638
|
};
|
|
684
639
|
}
|
|
685
640
|
|
|
686
|
-
|
|
641
|
+
const cleanupStatus = debounce(documentProp => {
|
|
687
642
|
getStatusDiv(documentProp).textContent = '';
|
|
688
643
|
}, 500);
|
|
689
644
|
|
|
@@ -692,7 +647,7 @@
|
|
|
692
647
|
* @param {Object} documentProp document passed by the user.
|
|
693
648
|
*/
|
|
694
649
|
function setStatus(status, documentProp) {
|
|
695
|
-
|
|
650
|
+
const div = getStatusDiv(documentProp);
|
|
696
651
|
if (!status) {
|
|
697
652
|
return;
|
|
698
653
|
}
|
|
@@ -709,7 +664,7 @@
|
|
|
709
664
|
if (documentProp === void 0) {
|
|
710
665
|
documentProp = document;
|
|
711
666
|
}
|
|
712
|
-
|
|
667
|
+
let statusDiv = documentProp.getElementById('a11y-status-message');
|
|
713
668
|
if (statusDiv) {
|
|
714
669
|
return statusDiv;
|
|
715
670
|
}
|
|
@@ -732,23 +687,23 @@
|
|
|
732
687
|
return statusDiv;
|
|
733
688
|
}
|
|
734
689
|
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
690
|
+
const unknown = '__autocomplete_unknown__' ;
|
|
691
|
+
const mouseUp = '__autocomplete_mouseup__' ;
|
|
692
|
+
const itemMouseEnter = '__autocomplete_item_mouseenter__' ;
|
|
693
|
+
const keyDownArrowUp = '__autocomplete_keydown_arrow_up__' ;
|
|
694
|
+
const keyDownArrowDown = '__autocomplete_keydown_arrow_down__' ;
|
|
695
|
+
const keyDownEscape = '__autocomplete_keydown_escape__' ;
|
|
696
|
+
const keyDownEnter = '__autocomplete_keydown_enter__' ;
|
|
697
|
+
const keyDownHome = '__autocomplete_keydown_home__' ;
|
|
698
|
+
const keyDownEnd = '__autocomplete_keydown_end__' ;
|
|
699
|
+
const clickItem = '__autocomplete_click_item__' ;
|
|
700
|
+
const blurInput = '__autocomplete_blur_input__' ;
|
|
701
|
+
const changeInput = '__autocomplete_change_input__' ;
|
|
702
|
+
const keyDownSpaceButton = '__autocomplete_keydown_space_button__' ;
|
|
703
|
+
const clickButton = '__autocomplete_click_button__' ;
|
|
704
|
+
const blurButton = '__autocomplete_blur_button__' ;
|
|
705
|
+
const controlledPropUpdatedSelectedItem = '__autocomplete_controlled_prop_updated_selected_item__' ;
|
|
706
|
+
const touchEnd = '__autocomplete_touchend__' ;
|
|
752
707
|
|
|
753
708
|
var stateChangeTypes$3 = /*#__PURE__*/Object.freeze({
|
|
754
709
|
__proto__: null,
|
|
@@ -771,48 +726,37 @@
|
|
|
771
726
|
touchEnd: touchEnd
|
|
772
727
|
});
|
|
773
728
|
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
_excluded5 = ["onMouseMove", "onMouseDown", "onClick", "onPress", "index", "item"];
|
|
779
|
-
var Downshift = /*#__PURE__*/function () {
|
|
780
|
-
var Downshift = /*#__PURE__*/function (_Component) {
|
|
781
|
-
_inheritsLoose(Downshift, _Component);
|
|
782
|
-
function Downshift(_props) {
|
|
729
|
+
/* eslint camelcase:0 */
|
|
730
|
+
const Downshift = /*#__PURE__*/(() => {
|
|
731
|
+
class Downshift extends preact.Component {
|
|
732
|
+
constructor(_props) {
|
|
783
733
|
var _this;
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
_this.timeoutIds = [];
|
|
800
|
-
_this.internalSetTimeout = function (fn, time) {
|
|
801
|
-
var id = setTimeout(function () {
|
|
802
|
-
_this.timeoutIds = _this.timeoutIds.filter(function (i) {
|
|
803
|
-
return i !== id;
|
|
804
|
-
});
|
|
734
|
+
super(_props);
|
|
735
|
+
_this = this;
|
|
736
|
+
this.id = this.props.id || `downshift-${generateId()}`;
|
|
737
|
+
this.menuId = this.props.menuId || `${this.id}-menu`;
|
|
738
|
+
this.labelId = this.props.labelId || `${this.id}-label`;
|
|
739
|
+
this.inputId = this.props.inputId || `${this.id}-input`;
|
|
740
|
+
this.getItemId = this.props.getItemId || (index => `${this.id}-item-${index}`);
|
|
741
|
+
this.input = null;
|
|
742
|
+
this.items = [];
|
|
743
|
+
this.itemCount = null;
|
|
744
|
+
this.previousResultCount = 0;
|
|
745
|
+
this.timeoutIds = [];
|
|
746
|
+
this.internalSetTimeout = (fn, time) => {
|
|
747
|
+
const id = setTimeout(() => {
|
|
748
|
+
this.timeoutIds = this.timeoutIds.filter(i => i !== id);
|
|
805
749
|
fn();
|
|
806
750
|
}, time);
|
|
807
|
-
|
|
751
|
+
this.timeoutIds.push(id);
|
|
808
752
|
};
|
|
809
|
-
|
|
810
|
-
|
|
753
|
+
this.setItemCount = count => {
|
|
754
|
+
this.itemCount = count;
|
|
811
755
|
};
|
|
812
|
-
|
|
813
|
-
|
|
756
|
+
this.unsetItemCount = () => {
|
|
757
|
+
this.itemCount = null;
|
|
814
758
|
};
|
|
815
|
-
|
|
759
|
+
this.setHighlightedIndex = function (highlightedIndex, otherStateToSet) {
|
|
816
760
|
if (highlightedIndex === void 0) {
|
|
817
761
|
highlightedIndex = _this.props.defaultHighlightedIndex;
|
|
818
762
|
}
|
|
@@ -820,41 +764,43 @@
|
|
|
820
764
|
otherStateToSet = {};
|
|
821
765
|
}
|
|
822
766
|
otherStateToSet = pickState(otherStateToSet);
|
|
823
|
-
_this.internalSetState(_extends({
|
|
824
|
-
highlightedIndex: highlightedIndex
|
|
825
|
-
}, otherStateToSet));
|
|
826
|
-
};
|
|
827
|
-
_this.clearSelection = function (cb) {
|
|
828
767
|
_this.internalSetState({
|
|
768
|
+
highlightedIndex,
|
|
769
|
+
...otherStateToSet
|
|
770
|
+
});
|
|
771
|
+
};
|
|
772
|
+
this.clearSelection = cb => {
|
|
773
|
+
this.internalSetState({
|
|
829
774
|
selectedItem: null,
|
|
830
775
|
inputValue: '',
|
|
831
|
-
highlightedIndex:
|
|
832
|
-
isOpen:
|
|
776
|
+
highlightedIndex: this.props.defaultHighlightedIndex,
|
|
777
|
+
isOpen: this.props.defaultIsOpen
|
|
833
778
|
}, cb);
|
|
834
779
|
};
|
|
835
|
-
|
|
780
|
+
this.selectItem = (item, otherStateToSet, cb) => {
|
|
836
781
|
otherStateToSet = pickState(otherStateToSet);
|
|
837
|
-
|
|
838
|
-
isOpen:
|
|
839
|
-
highlightedIndex:
|
|
782
|
+
this.internalSetState({
|
|
783
|
+
isOpen: this.props.defaultIsOpen,
|
|
784
|
+
highlightedIndex: this.props.defaultHighlightedIndex,
|
|
840
785
|
selectedItem: item,
|
|
841
|
-
inputValue:
|
|
842
|
-
|
|
786
|
+
inputValue: this.props.itemToString(item),
|
|
787
|
+
...otherStateToSet
|
|
788
|
+
}, cb);
|
|
843
789
|
};
|
|
844
|
-
|
|
845
|
-
|
|
790
|
+
this.selectItemAtIndex = (itemIndex, otherStateToSet, cb) => {
|
|
791
|
+
const item = this.items[itemIndex];
|
|
846
792
|
if (item == null) {
|
|
847
793
|
return;
|
|
848
794
|
}
|
|
849
|
-
|
|
795
|
+
this.selectItem(item, otherStateToSet, cb);
|
|
850
796
|
};
|
|
851
|
-
|
|
852
|
-
return
|
|
797
|
+
this.selectHighlightedItem = (otherStateToSet, cb) => {
|
|
798
|
+
return this.selectItemAtIndex(this.getState().highlightedIndex, otherStateToSet, cb);
|
|
853
799
|
};
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
800
|
+
this.internalSetState = (stateToSet, cb) => {
|
|
801
|
+
let isItemSelected, onChangeArg;
|
|
802
|
+
const onStateChangeArg = {};
|
|
803
|
+
const isStateToSetFunction = typeof stateToSet === 'function';
|
|
858
804
|
|
|
859
805
|
// we want to call `onInputValueChange` before the `setState` call
|
|
860
806
|
// so someone controlling the `inputValue` state gets notified of
|
|
@@ -862,21 +808,24 @@
|
|
|
862
808
|
// preserving the cursor position.
|
|
863
809
|
// See https://github.com/downshift-js/downshift/issues/217 for more info.
|
|
864
810
|
if (!isStateToSetFunction && stateToSet.hasOwnProperty('inputValue')) {
|
|
865
|
-
|
|
811
|
+
this.props.onInputValueChange(stateToSet.inputValue, {
|
|
812
|
+
...this.getStateAndHelpers(),
|
|
813
|
+
...stateToSet
|
|
814
|
+
});
|
|
866
815
|
}
|
|
867
|
-
return
|
|
868
|
-
state =
|
|
869
|
-
|
|
816
|
+
return this.setState(state => {
|
|
817
|
+
state = this.getState(state);
|
|
818
|
+
let newStateToSet = isStateToSetFunction ? stateToSet(state) : stateToSet;
|
|
870
819
|
|
|
871
820
|
// Your own function that could modify the state that will be set.
|
|
872
|
-
newStateToSet =
|
|
821
|
+
newStateToSet = this.props.stateReducer(state, newStateToSet);
|
|
873
822
|
|
|
874
823
|
// checks if an item is selected, regardless of if it's different from
|
|
875
824
|
// what was selected before
|
|
876
825
|
// used to determine if onSelect and onChange callbacks should be called
|
|
877
826
|
isItemSelected = newStateToSet.hasOwnProperty('selectedItem');
|
|
878
827
|
// this keeps track of the object we want to call with setState
|
|
879
|
-
|
|
828
|
+
const nextState = {};
|
|
880
829
|
// we need to call on change if the outside world is controlling any of our state
|
|
881
830
|
// and we're trying to update that state. OR if the selection has changed and we're
|
|
882
831
|
// trying to update the selection
|
|
@@ -884,7 +833,7 @@
|
|
|
884
833
|
onChangeArg = newStateToSet.selectedItem;
|
|
885
834
|
}
|
|
886
835
|
newStateToSet.type = newStateToSet.type || unknown;
|
|
887
|
-
Object.keys(newStateToSet).forEach(
|
|
836
|
+
Object.keys(newStateToSet).forEach(key => {
|
|
888
837
|
// onStateChangeArg should only have the state that is
|
|
889
838
|
// actually changing
|
|
890
839
|
if (state[key] !== newStateToSet[key]) {
|
|
@@ -901,7 +850,7 @@
|
|
|
901
850
|
}
|
|
902
851
|
newStateToSet[key];
|
|
903
852
|
// if it's coming from props, then we don't care to set it internally
|
|
904
|
-
if (!isControlledProp(
|
|
853
|
+
if (!isControlledProp(this.props, key)) {
|
|
905
854
|
nextState[key] = newStateToSet[key];
|
|
906
855
|
}
|
|
907
856
|
});
|
|
@@ -909,58 +858,66 @@
|
|
|
909
858
|
// if stateToSet is a function, then we weren't able to call onInputValueChange
|
|
910
859
|
// earlier, so we'll call it now that we know what the inputValue state will be.
|
|
911
860
|
if (isStateToSetFunction && newStateToSet.hasOwnProperty('inputValue')) {
|
|
912
|
-
|
|
861
|
+
this.props.onInputValueChange(newStateToSet.inputValue, {
|
|
862
|
+
...this.getStateAndHelpers(),
|
|
863
|
+
...newStateToSet
|
|
864
|
+
});
|
|
913
865
|
}
|
|
914
866
|
return nextState;
|
|
915
|
-
},
|
|
867
|
+
}, () => {
|
|
916
868
|
// call the provided callback if it's a function
|
|
917
869
|
cbToCb(cb)();
|
|
918
870
|
|
|
919
871
|
// only call the onStateChange and onChange callbacks if
|
|
920
872
|
// we have relevant information to pass them.
|
|
921
|
-
|
|
873
|
+
const hasMoreStateThanType = Object.keys(onStateChangeArg).length > 1;
|
|
922
874
|
if (hasMoreStateThanType) {
|
|
923
|
-
|
|
875
|
+
this.props.onStateChange(onStateChangeArg, this.getStateAndHelpers());
|
|
924
876
|
}
|
|
925
877
|
if (isItemSelected) {
|
|
926
|
-
|
|
878
|
+
this.props.onSelect(stateToSet.selectedItem, this.getStateAndHelpers());
|
|
927
879
|
}
|
|
928
880
|
if (onChangeArg !== undefined) {
|
|
929
|
-
|
|
881
|
+
this.props.onChange(onChangeArg, this.getStateAndHelpers());
|
|
930
882
|
}
|
|
931
883
|
// this is currently undocumented and therefore subject to change
|
|
932
884
|
// We'll try to not break it, but just be warned.
|
|
933
|
-
|
|
885
|
+
this.props.onUserAction(onStateChangeArg, this.getStateAndHelpers());
|
|
934
886
|
});
|
|
935
887
|
};
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
var _ref2 = _temp2 === void 0 ? {} : _temp2,
|
|
947
|
-
_ref2$suppressRefErro = _ref2.suppressRefError,
|
|
948
|
-
suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
|
|
888
|
+
this.rootRef = node => this._rootNode = node;
|
|
889
|
+
this.getRootProps = function (_temp, _temp2) {
|
|
890
|
+
let {
|
|
891
|
+
refKey = 'ref',
|
|
892
|
+
ref,
|
|
893
|
+
...rest
|
|
894
|
+
} = _temp === void 0 ? {} : _temp;
|
|
895
|
+
let {
|
|
896
|
+
suppressRefError = false
|
|
897
|
+
} = _temp2 === void 0 ? {} : _temp2;
|
|
949
898
|
// this is used in the render to know whether the user has called getRootProps.
|
|
950
899
|
// It uses that to know whether to apply the props automatically
|
|
951
900
|
_this.getRootProps.called = true;
|
|
952
901
|
_this.getRootProps.refKey = refKey;
|
|
953
902
|
_this.getRootProps.suppressRefError = suppressRefError;
|
|
954
|
-
|
|
955
|
-
isOpen
|
|
956
|
-
|
|
903
|
+
const {
|
|
904
|
+
isOpen
|
|
905
|
+
} = _this.getState();
|
|
906
|
+
return {
|
|
907
|
+
[refKey]: handleRefs(ref, _this.rootRef),
|
|
908
|
+
role: 'combobox',
|
|
909
|
+
'aria-expanded': isOpen,
|
|
910
|
+
'aria-haspopup': 'listbox',
|
|
911
|
+
'aria-owns': isOpen ? _this.menuId : null,
|
|
912
|
+
'aria-labelledby': _this.labelId,
|
|
913
|
+
...rest
|
|
914
|
+
};
|
|
957
915
|
};
|
|
958
|
-
|
|
959
|
-
ArrowDown
|
|
960
|
-
var _this2 = this;
|
|
916
|
+
this.keyDownHandlers = {
|
|
917
|
+
ArrowDown(event) {
|
|
961
918
|
event.preventDefault();
|
|
962
919
|
if (this.getState().isOpen) {
|
|
963
|
-
|
|
920
|
+
const amount = event.shiftKey ? 5 : 1;
|
|
964
921
|
this.moveHighlightedIndex(amount, {
|
|
965
922
|
type: keyDownArrowDown
|
|
966
923
|
});
|
|
@@ -968,26 +925,24 @@
|
|
|
968
925
|
this.internalSetState({
|
|
969
926
|
isOpen: true,
|
|
970
927
|
type: keyDownArrowDown
|
|
971
|
-
},
|
|
972
|
-
|
|
928
|
+
}, () => {
|
|
929
|
+
const itemCount = this.getItemCount();
|
|
973
930
|
if (itemCount > 0) {
|
|
974
|
-
|
|
975
|
-
highlightedIndex
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
_this2.setHighlightedIndex(nextHighlightedIndex, {
|
|
931
|
+
const {
|
|
932
|
+
highlightedIndex
|
|
933
|
+
} = this.getState();
|
|
934
|
+
const nextHighlightedIndex = getNextWrappingIndex(1, highlightedIndex, itemCount, index => this.getItemNodeFromIndex(index));
|
|
935
|
+
this.setHighlightedIndex(nextHighlightedIndex, {
|
|
980
936
|
type: keyDownArrowDown
|
|
981
937
|
});
|
|
982
938
|
}
|
|
983
939
|
});
|
|
984
940
|
}
|
|
985
941
|
},
|
|
986
|
-
ArrowUp
|
|
987
|
-
var _this3 = this;
|
|
942
|
+
ArrowUp(event) {
|
|
988
943
|
event.preventDefault();
|
|
989
944
|
if (this.getState().isOpen) {
|
|
990
|
-
|
|
945
|
+
const amount = event.shiftKey ? -5 : -1;
|
|
991
946
|
this.moveHighlightedIndex(amount, {
|
|
992
947
|
type: keyDownArrowUp
|
|
993
948
|
});
|
|
@@ -995,32 +950,32 @@
|
|
|
995
950
|
this.internalSetState({
|
|
996
951
|
isOpen: true,
|
|
997
952
|
type: keyDownArrowUp
|
|
998
|
-
},
|
|
999
|
-
|
|
953
|
+
}, () => {
|
|
954
|
+
const itemCount = this.getItemCount();
|
|
1000
955
|
if (itemCount > 0) {
|
|
1001
|
-
|
|
1002
|
-
highlightedIndex
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
_this3.setHighlightedIndex(nextHighlightedIndex, {
|
|
956
|
+
const {
|
|
957
|
+
highlightedIndex
|
|
958
|
+
} = this.getState();
|
|
959
|
+
const nextHighlightedIndex = getNextWrappingIndex(-1, highlightedIndex, itemCount, index => this.getItemNodeFromIndex(index));
|
|
960
|
+
this.setHighlightedIndex(nextHighlightedIndex, {
|
|
1007
961
|
type: keyDownArrowUp
|
|
1008
962
|
});
|
|
1009
963
|
}
|
|
1010
964
|
});
|
|
1011
965
|
}
|
|
1012
966
|
},
|
|
1013
|
-
Enter
|
|
967
|
+
Enter(event) {
|
|
1014
968
|
if (event.which === 229) {
|
|
1015
969
|
return;
|
|
1016
970
|
}
|
|
1017
|
-
|
|
1018
|
-
isOpen
|
|
1019
|
-
highlightedIndex
|
|
971
|
+
const {
|
|
972
|
+
isOpen,
|
|
973
|
+
highlightedIndex
|
|
974
|
+
} = this.getState();
|
|
1020
975
|
if (isOpen && highlightedIndex != null) {
|
|
1021
976
|
event.preventDefault();
|
|
1022
|
-
|
|
1023
|
-
|
|
977
|
+
const item = this.items[highlightedIndex];
|
|
978
|
+
const itemNode = this.getItemNodeFromIndex(highlightedIndex);
|
|
1024
979
|
if (item == null || itemNode && itemNode.hasAttribute('disabled')) {
|
|
1025
980
|
return;
|
|
1026
981
|
}
|
|
@@ -1029,109 +984,112 @@
|
|
|
1029
984
|
});
|
|
1030
985
|
}
|
|
1031
986
|
},
|
|
1032
|
-
Escape
|
|
987
|
+
Escape(event) {
|
|
1033
988
|
event.preventDefault();
|
|
1034
|
-
this.reset(
|
|
1035
|
-
type: keyDownEscape
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
989
|
+
this.reset({
|
|
990
|
+
type: keyDownEscape,
|
|
991
|
+
...(!this.state.isOpen && {
|
|
992
|
+
selectedItem: null,
|
|
993
|
+
inputValue: ''
|
|
994
|
+
})
|
|
995
|
+
});
|
|
1040
996
|
}
|
|
1041
997
|
};
|
|
1042
|
-
|
|
1043
|
-
|
|
998
|
+
this.buttonKeyDownHandlers = {
|
|
999
|
+
...this.keyDownHandlers,
|
|
1000
|
+
' '(event) {
|
|
1044
1001
|
event.preventDefault();
|
|
1045
1002
|
this.toggleMenu({
|
|
1046
1003
|
type: keyDownSpaceButton
|
|
1047
1004
|
});
|
|
1048
1005
|
}
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
isOpen
|
|
1006
|
+
};
|
|
1007
|
+
this.inputKeyDownHandlers = {
|
|
1008
|
+
...this.keyDownHandlers,
|
|
1009
|
+
Home(event) {
|
|
1010
|
+
const {
|
|
1011
|
+
isOpen
|
|
1012
|
+
} = this.getState();
|
|
1055
1013
|
if (!isOpen) {
|
|
1056
1014
|
return;
|
|
1057
1015
|
}
|
|
1058
1016
|
event.preventDefault();
|
|
1059
|
-
|
|
1017
|
+
const itemCount = this.getItemCount();
|
|
1060
1018
|
if (itemCount <= 0 || !isOpen) {
|
|
1061
1019
|
return;
|
|
1062
1020
|
}
|
|
1063
1021
|
|
|
1064
1022
|
// get next non-disabled starting downwards from 0 if that's disabled.
|
|
1065
|
-
|
|
1066
|
-
return _this4.getItemNodeFromIndex(index);
|
|
1067
|
-
}, false);
|
|
1023
|
+
const newHighlightedIndex = getNextNonDisabledIndex(1, 0, itemCount, index => this.getItemNodeFromIndex(index), false);
|
|
1068
1024
|
this.setHighlightedIndex(newHighlightedIndex, {
|
|
1069
1025
|
type: keyDownHome
|
|
1070
1026
|
});
|
|
1071
1027
|
},
|
|
1072
|
-
End
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1028
|
+
End(event) {
|
|
1029
|
+
const {
|
|
1030
|
+
isOpen
|
|
1031
|
+
} = this.getState();
|
|
1076
1032
|
if (!isOpen) {
|
|
1077
1033
|
return;
|
|
1078
1034
|
}
|
|
1079
1035
|
event.preventDefault();
|
|
1080
|
-
|
|
1036
|
+
const itemCount = this.getItemCount();
|
|
1081
1037
|
if (itemCount <= 0 || !isOpen) {
|
|
1082
1038
|
return;
|
|
1083
1039
|
}
|
|
1084
1040
|
|
|
1085
1041
|
// get next non-disabled starting upwards from last index if that's disabled.
|
|
1086
|
-
|
|
1087
|
-
return _this5.getItemNodeFromIndex(index);
|
|
1088
|
-
}, false);
|
|
1042
|
+
const newHighlightedIndex = getNextNonDisabledIndex(-1, itemCount - 1, itemCount, index => this.getItemNodeFromIndex(index), false);
|
|
1089
1043
|
this.setHighlightedIndex(newHighlightedIndex, {
|
|
1090
1044
|
type: keyDownEnd
|
|
1091
1045
|
});
|
|
1092
1046
|
}
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
onClick
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
onKeyUp
|
|
1100
|
-
onBlur
|
|
1101
|
-
rest
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1047
|
+
};
|
|
1048
|
+
this.getToggleButtonProps = function (_temp3) {
|
|
1049
|
+
let {
|
|
1050
|
+
onClick,
|
|
1051
|
+
onPress,
|
|
1052
|
+
onKeyDown,
|
|
1053
|
+
onKeyUp,
|
|
1054
|
+
onBlur,
|
|
1055
|
+
...rest
|
|
1056
|
+
} = _temp3 === void 0 ? {} : _temp3;
|
|
1057
|
+
const {
|
|
1058
|
+
isOpen
|
|
1059
|
+
} = _this.getState();
|
|
1060
|
+
const enabledEventHandlers = {
|
|
1105
1061
|
onClick: callAllEventHandlers(onClick, _this.buttonHandleClick),
|
|
1106
1062
|
onKeyDown: callAllEventHandlers(onKeyDown, _this.buttonHandleKeyDown),
|
|
1107
1063
|
onKeyUp: callAllEventHandlers(onKeyUp, _this.buttonHandleKeyUp),
|
|
1108
1064
|
onBlur: callAllEventHandlers(onBlur, _this.buttonHandleBlur)
|
|
1109
1065
|
};
|
|
1110
|
-
|
|
1111
|
-
return
|
|
1066
|
+
const eventHandlers = rest.disabled ? {} : enabledEventHandlers;
|
|
1067
|
+
return {
|
|
1112
1068
|
type: 'button',
|
|
1113
1069
|
role: 'button',
|
|
1114
1070
|
'aria-label': isOpen ? 'close menu' : 'open menu',
|
|
1115
1071
|
'aria-haspopup': true,
|
|
1116
|
-
'data-toggle': true
|
|
1117
|
-
|
|
1072
|
+
'data-toggle': true,
|
|
1073
|
+
...eventHandlers,
|
|
1074
|
+
...rest
|
|
1075
|
+
};
|
|
1118
1076
|
};
|
|
1119
|
-
|
|
1077
|
+
this.buttonHandleKeyUp = event => {
|
|
1120
1078
|
// Prevent click event from emitting in Firefox
|
|
1121
1079
|
event.preventDefault();
|
|
1122
1080
|
};
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
if (
|
|
1126
|
-
|
|
1081
|
+
this.buttonHandleKeyDown = event => {
|
|
1082
|
+
const key = normalizeArrowKey(event);
|
|
1083
|
+
if (this.buttonKeyDownHandlers[key]) {
|
|
1084
|
+
this.buttonKeyDownHandlers[key].call(this, event);
|
|
1127
1085
|
}
|
|
1128
1086
|
};
|
|
1129
|
-
|
|
1087
|
+
this.buttonHandleClick = event => {
|
|
1130
1088
|
event.preventDefault();
|
|
1131
1089
|
// handle odd case for Safari and Firefox which
|
|
1132
1090
|
// don't give the button the focus properly.
|
|
1133
1091
|
/* istanbul ignore if (can't reasonably test this) */
|
|
1134
|
-
if (
|
|
1092
|
+
if (this.props.environment.document.activeElement === this.props.environment.document.body) {
|
|
1135
1093
|
event.target.focus();
|
|
1136
1094
|
}
|
|
1137
1095
|
// to simplify testing components that use downshift, we'll not wrap this in a setTimeout
|
|
@@ -1139,55 +1097,59 @@
|
|
|
1139
1097
|
// when building for production and should therefore have no impact on production code.
|
|
1140
1098
|
{
|
|
1141
1099
|
// Ensure that toggle of menu occurs after the potential blur event in iOS
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
});
|
|
1146
|
-
});
|
|
1100
|
+
this.internalSetTimeout(() => this.toggleMenu({
|
|
1101
|
+
type: clickButton
|
|
1102
|
+
}));
|
|
1147
1103
|
}
|
|
1148
1104
|
};
|
|
1149
|
-
|
|
1150
|
-
|
|
1105
|
+
this.buttonHandleBlur = event => {
|
|
1106
|
+
const blurTarget = event.target; // Save blur target for comparison with activeElement later
|
|
1151
1107
|
// Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not body element
|
|
1152
|
-
|
|
1153
|
-
if (!
|
|
1108
|
+
this.internalSetTimeout(() => {
|
|
1109
|
+
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)
|
|
1154
1110
|
) {
|
|
1155
|
-
|
|
1111
|
+
this.reset({
|
|
1156
1112
|
type: blurButton
|
|
1157
1113
|
});
|
|
1158
1114
|
}
|
|
1159
1115
|
});
|
|
1160
1116
|
};
|
|
1161
|
-
|
|
1162
|
-
return
|
|
1163
|
-
htmlFor:
|
|
1164
|
-
id:
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1117
|
+
this.getLabelProps = props => {
|
|
1118
|
+
return {
|
|
1119
|
+
htmlFor: this.inputId,
|
|
1120
|
+
id: this.labelId,
|
|
1121
|
+
...props
|
|
1122
|
+
};
|
|
1123
|
+
};
|
|
1124
|
+
this.getInputProps = function (_temp4) {
|
|
1125
|
+
let {
|
|
1126
|
+
onKeyDown,
|
|
1127
|
+
onBlur,
|
|
1128
|
+
onChange,
|
|
1129
|
+
onInput,
|
|
1130
|
+
onChangeText,
|
|
1131
|
+
...rest
|
|
1132
|
+
} = _temp4 === void 0 ? {} : _temp4;
|
|
1133
|
+
let onChangeKey;
|
|
1134
|
+
let eventHandlers = {};
|
|
1177
1135
|
|
|
1178
1136
|
/* istanbul ignore next (preact) */
|
|
1179
1137
|
{
|
|
1180
1138
|
onChangeKey = 'onInput';
|
|
1181
1139
|
}
|
|
1182
|
-
|
|
1183
|
-
inputValue
|
|
1184
|
-
isOpen
|
|
1185
|
-
highlightedIndex
|
|
1140
|
+
const {
|
|
1141
|
+
inputValue,
|
|
1142
|
+
isOpen,
|
|
1143
|
+
highlightedIndex
|
|
1144
|
+
} = _this.getState();
|
|
1186
1145
|
if (!rest.disabled) {
|
|
1187
|
-
|
|
1188
|
-
|
|
1146
|
+
eventHandlers = {
|
|
1147
|
+
[onChangeKey]: callAllEventHandlers(onChange, onInput, _this.inputHandleChange),
|
|
1148
|
+
onKeyDown: callAllEventHandlers(onKeyDown, _this.inputHandleKeyDown),
|
|
1149
|
+
onBlur: callAllEventHandlers(onBlur, _this.inputHandleBlur)
|
|
1150
|
+
};
|
|
1189
1151
|
}
|
|
1190
|
-
return
|
|
1152
|
+
return {
|
|
1191
1153
|
'aria-autocomplete': 'list',
|
|
1192
1154
|
'aria-activedescendant': isOpen && typeof highlightedIndex === 'number' && highlightedIndex >= 0 ? _this.getItemId(highlightedIndex) : null,
|
|
1193
1155
|
'aria-controls': isOpen ? _this.menuId : null,
|
|
@@ -1196,76 +1158,82 @@
|
|
|
1196
1158
|
// revert back since autocomplete="nope" is ignored on latest Chrome and Opera
|
|
1197
1159
|
autoComplete: 'off',
|
|
1198
1160
|
value: inputValue,
|
|
1199
|
-
id: _this.inputId
|
|
1200
|
-
|
|
1161
|
+
id: _this.inputId,
|
|
1162
|
+
...eventHandlers,
|
|
1163
|
+
...rest
|
|
1164
|
+
};
|
|
1201
1165
|
};
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
if (key &&
|
|
1205
|
-
|
|
1166
|
+
this.inputHandleKeyDown = event => {
|
|
1167
|
+
const key = normalizeArrowKey(event);
|
|
1168
|
+
if (key && this.inputKeyDownHandlers[key]) {
|
|
1169
|
+
this.inputKeyDownHandlers[key].call(this, event);
|
|
1206
1170
|
}
|
|
1207
1171
|
};
|
|
1208
|
-
|
|
1209
|
-
|
|
1172
|
+
this.inputHandleChange = event => {
|
|
1173
|
+
this.internalSetState({
|
|
1210
1174
|
type: changeInput,
|
|
1211
1175
|
isOpen: true,
|
|
1212
1176
|
inputValue: event.target.value,
|
|
1213
|
-
highlightedIndex:
|
|
1177
|
+
highlightedIndex: this.props.defaultHighlightedIndex
|
|
1214
1178
|
});
|
|
1215
1179
|
};
|
|
1216
|
-
|
|
1180
|
+
this.inputHandleBlur = () => {
|
|
1217
1181
|
// Need setTimeout, so that when the user presses Tab, the activeElement is the next focused element, not the body element
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
if (!
|
|
1221
|
-
|
|
1182
|
+
this.internalSetTimeout(() => {
|
|
1183
|
+
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);
|
|
1184
|
+
if (!this.isMouseDown && !downshiftButtonIsActive) {
|
|
1185
|
+
this.reset({
|
|
1222
1186
|
type: blurInput
|
|
1223
1187
|
});
|
|
1224
1188
|
}
|
|
1225
1189
|
});
|
|
1226
1190
|
};
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
};
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
suppressRefError = _ref6$suppressRefErro === void 0 ? false : _ref6$suppressRefErro;
|
|
1191
|
+
this.menuRef = node => {
|
|
1192
|
+
this._menuNode = node;
|
|
1193
|
+
};
|
|
1194
|
+
this.getMenuProps = function (_temp5, _temp6) {
|
|
1195
|
+
let {
|
|
1196
|
+
refKey = 'ref',
|
|
1197
|
+
ref,
|
|
1198
|
+
...props
|
|
1199
|
+
} = _temp5 === void 0 ? {} : _temp5;
|
|
1200
|
+
let {
|
|
1201
|
+
suppressRefError = false
|
|
1202
|
+
} = _temp6 === void 0 ? {} : _temp6;
|
|
1240
1203
|
_this.getMenuProps.called = true;
|
|
1241
1204
|
_this.getMenuProps.refKey = refKey;
|
|
1242
1205
|
_this.getMenuProps.suppressRefError = suppressRefError;
|
|
1243
|
-
return
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1206
|
+
return {
|
|
1207
|
+
[refKey]: handleRefs(ref, _this.menuRef),
|
|
1208
|
+
role: 'listbox',
|
|
1209
|
+
'aria-labelledby': props && props['aria-label'] ? null : _this.labelId,
|
|
1210
|
+
id: _this.menuId,
|
|
1211
|
+
...props
|
|
1212
|
+
};
|
|
1213
|
+
};
|
|
1214
|
+
this.getItemProps = function (_temp7) {
|
|
1215
|
+
let {
|
|
1216
|
+
onMouseMove,
|
|
1217
|
+
onMouseDown,
|
|
1218
|
+
onClick,
|
|
1219
|
+
onPress,
|
|
1220
|
+
index,
|
|
1221
|
+
item = requiredProp('getItemProps', 'item'),
|
|
1222
|
+
...rest
|
|
1223
|
+
} = _temp7 === void 0 ? {} : _temp7;
|
|
1256
1224
|
if (index === undefined) {
|
|
1257
1225
|
_this.items.push(item);
|
|
1258
1226
|
index = _this.items.indexOf(item);
|
|
1259
1227
|
} else {
|
|
1260
1228
|
_this.items[index] = item;
|
|
1261
1229
|
}
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1230
|
+
const onSelectKey = 'onClick';
|
|
1231
|
+
const customClickHandler = onClick;
|
|
1232
|
+
const enabledEventHandlers = {
|
|
1265
1233
|
// onMouseMove is used over onMouseEnter here. onMouseMove
|
|
1266
1234
|
// is only triggered on actual mouse movement while onMouseEnter
|
|
1267
1235
|
// can fire on DOM changes, interrupting keyboard navigation
|
|
1268
|
-
onMouseMove: callAllEventHandlers(onMouseMove,
|
|
1236
|
+
onMouseMove: callAllEventHandlers(onMouseMove, () => {
|
|
1269
1237
|
if (index === _this.getState().highlightedIndex) {
|
|
1270
1238
|
return;
|
|
1271
1239
|
}
|
|
@@ -1278,66 +1246,75 @@
|
|
|
1278
1246
|
// from under the user which is currently scrolling/moving the
|
|
1279
1247
|
// cursor
|
|
1280
1248
|
_this.avoidScrolling = true;
|
|
1281
|
-
_this.internalSetTimeout(
|
|
1282
|
-
return _this.avoidScrolling = false;
|
|
1283
|
-
}, 250);
|
|
1249
|
+
_this.internalSetTimeout(() => _this.avoidScrolling = false, 250);
|
|
1284
1250
|
}),
|
|
1285
|
-
onMouseDown: callAllEventHandlers(onMouseDown,
|
|
1251
|
+
onMouseDown: callAllEventHandlers(onMouseDown, event => {
|
|
1286
1252
|
// This prevents the activeElement from being changed
|
|
1287
1253
|
// to the item so it can remain with the current activeElement
|
|
1288
1254
|
// which is a more common use case.
|
|
1289
1255
|
event.preventDefault();
|
|
1256
|
+
}),
|
|
1257
|
+
[onSelectKey]: callAllEventHandlers(customClickHandler, () => {
|
|
1258
|
+
_this.selectItemAtIndex(index, {
|
|
1259
|
+
type: clickItem
|
|
1260
|
+
});
|
|
1290
1261
|
})
|
|
1291
|
-
}
|
|
1292
|
-
_this.selectItemAtIndex(index, {
|
|
1293
|
-
type: clickItem
|
|
1294
|
-
});
|
|
1295
|
-
}), _enabledEventHandlers);
|
|
1262
|
+
};
|
|
1296
1263
|
|
|
1297
1264
|
// Passing down the onMouseDown handler to prevent redirect
|
|
1298
1265
|
// of the activeElement if clicking on disabled items
|
|
1299
|
-
|
|
1266
|
+
const eventHandlers = rest.disabled ? {
|
|
1300
1267
|
onMouseDown: enabledEventHandlers.onMouseDown
|
|
1301
1268
|
} : enabledEventHandlers;
|
|
1302
|
-
return
|
|
1269
|
+
return {
|
|
1303
1270
|
id: _this.getItemId(index),
|
|
1304
1271
|
role: 'option',
|
|
1305
|
-
'aria-selected': _this.getState().highlightedIndex === index
|
|
1306
|
-
|
|
1272
|
+
'aria-selected': _this.getState().highlightedIndex === index,
|
|
1273
|
+
...eventHandlers,
|
|
1274
|
+
...rest
|
|
1275
|
+
};
|
|
1307
1276
|
};
|
|
1308
|
-
|
|
1309
|
-
|
|
1277
|
+
this.clearItems = () => {
|
|
1278
|
+
this.items = [];
|
|
1310
1279
|
};
|
|
1311
|
-
|
|
1280
|
+
this.reset = function (otherStateToSet, cb) {
|
|
1312
1281
|
if (otherStateToSet === void 0) {
|
|
1313
1282
|
otherStateToSet = {};
|
|
1314
1283
|
}
|
|
1315
1284
|
otherStateToSet = pickState(otherStateToSet);
|
|
1316
|
-
_this.internalSetState(
|
|
1317
|
-
|
|
1318
|
-
|
|
1285
|
+
_this.internalSetState(_ref => {
|
|
1286
|
+
let {
|
|
1287
|
+
selectedItem
|
|
1288
|
+
} = _ref;
|
|
1289
|
+
return {
|
|
1319
1290
|
isOpen: _this.props.defaultIsOpen,
|
|
1320
1291
|
highlightedIndex: _this.props.defaultHighlightedIndex,
|
|
1321
|
-
inputValue: _this.props.itemToString(selectedItem)
|
|
1322
|
-
|
|
1292
|
+
inputValue: _this.props.itemToString(selectedItem),
|
|
1293
|
+
...otherStateToSet
|
|
1294
|
+
};
|
|
1323
1295
|
}, cb);
|
|
1324
1296
|
};
|
|
1325
|
-
|
|
1297
|
+
this.toggleMenu = function (otherStateToSet, cb) {
|
|
1326
1298
|
if (otherStateToSet === void 0) {
|
|
1327
1299
|
otherStateToSet = {};
|
|
1328
1300
|
}
|
|
1329
1301
|
otherStateToSet = pickState(otherStateToSet);
|
|
1330
|
-
_this.internalSetState(
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1302
|
+
_this.internalSetState(_ref2 => {
|
|
1303
|
+
let {
|
|
1304
|
+
isOpen
|
|
1305
|
+
} = _ref2;
|
|
1306
|
+
return {
|
|
1307
|
+
isOpen: !isOpen,
|
|
1308
|
+
...(isOpen && {
|
|
1309
|
+
highlightedIndex: _this.props.defaultHighlightedIndex
|
|
1310
|
+
}),
|
|
1311
|
+
...otherStateToSet
|
|
1312
|
+
};
|
|
1313
|
+
}, () => {
|
|
1314
|
+
const {
|
|
1315
|
+
isOpen,
|
|
1316
|
+
highlightedIndex
|
|
1317
|
+
} = _this.getState();
|
|
1341
1318
|
if (isOpen) {
|
|
1342
1319
|
if (_this.getItemCount() > 0 && typeof highlightedIndex === 'number') {
|
|
1343
1320
|
_this.setHighlightedIndex(highlightedIndex, otherStateToSet);
|
|
@@ -1346,58 +1323,57 @@
|
|
|
1346
1323
|
cbToCb(cb)();
|
|
1347
1324
|
});
|
|
1348
1325
|
};
|
|
1349
|
-
|
|
1350
|
-
|
|
1326
|
+
this.openMenu = cb => {
|
|
1327
|
+
this.internalSetState({
|
|
1351
1328
|
isOpen: true
|
|
1352
1329
|
}, cb);
|
|
1353
1330
|
};
|
|
1354
|
-
|
|
1355
|
-
|
|
1331
|
+
this.closeMenu = cb => {
|
|
1332
|
+
this.internalSetState({
|
|
1356
1333
|
isOpen: false
|
|
1357
1334
|
}, cb);
|
|
1358
1335
|
};
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
itemToString:
|
|
1365
|
-
previousResultCount:
|
|
1366
|
-
resultCount
|
|
1367
|
-
highlightedItem: item
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1336
|
+
this.updateStatus = debounce(() => {
|
|
1337
|
+
const state = this.getState();
|
|
1338
|
+
const item = this.items[state.highlightedIndex];
|
|
1339
|
+
const resultCount = this.getItemCount();
|
|
1340
|
+
const status = this.props.getA11yStatusMessage({
|
|
1341
|
+
itemToString: this.props.itemToString,
|
|
1342
|
+
previousResultCount: this.previousResultCount,
|
|
1343
|
+
resultCount,
|
|
1344
|
+
highlightedItem: item,
|
|
1345
|
+
...state
|
|
1346
|
+
});
|
|
1347
|
+
this.previousResultCount = resultCount;
|
|
1348
|
+
setStatus(status, this.props.environment.document);
|
|
1371
1349
|
}, 200);
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
_inputValue =
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1350
|
+
// fancy destructuring + defaults + aliases
|
|
1351
|
+
// this basically says each value of state should either be set to
|
|
1352
|
+
// the initial value or the default value if the initial value is not provided
|
|
1353
|
+
const {
|
|
1354
|
+
defaultHighlightedIndex,
|
|
1355
|
+
initialHighlightedIndex: _highlightedIndex = defaultHighlightedIndex,
|
|
1356
|
+
defaultIsOpen,
|
|
1357
|
+
initialIsOpen: _isOpen = defaultIsOpen,
|
|
1358
|
+
initialInputValue: _inputValue = '',
|
|
1359
|
+
initialSelectedItem: _selectedItem = null
|
|
1360
|
+
} = this.props;
|
|
1361
|
+
const _state = this.getState({
|
|
1384
1362
|
highlightedIndex: _highlightedIndex,
|
|
1385
1363
|
isOpen: _isOpen,
|
|
1386
1364
|
inputValue: _inputValue,
|
|
1387
1365
|
selectedItem: _selectedItem
|
|
1388
1366
|
});
|
|
1389
|
-
if (_state.selectedItem != null &&
|
|
1390
|
-
_state.inputValue =
|
|
1367
|
+
if (_state.selectedItem != null && this.props.initialInputValue === undefined) {
|
|
1368
|
+
_state.inputValue = this.props.itemToString(_state.selectedItem);
|
|
1391
1369
|
}
|
|
1392
|
-
|
|
1393
|
-
return _this;
|
|
1370
|
+
this.state = _state;
|
|
1394
1371
|
}
|
|
1395
|
-
var _proto = Downshift.prototype;
|
|
1396
1372
|
/**
|
|
1397
1373
|
* Clear all running timeouts
|
|
1398
1374
|
*/
|
|
1399
|
-
|
|
1400
|
-
this.timeoutIds.forEach(
|
|
1375
|
+
internalClearTimeouts() {
|
|
1376
|
+
this.timeoutIds.forEach(id => {
|
|
1401
1377
|
clearTimeout(id);
|
|
1402
1378
|
});
|
|
1403
1379
|
this.timeoutIds = [];
|
|
@@ -1411,113 +1387,117 @@
|
|
|
1411
1387
|
*
|
|
1412
1388
|
* @param {Object} stateToMerge defaults to this.state
|
|
1413
1389
|
* @return {Object} the state
|
|
1414
|
-
|
|
1415
|
-
|
|
1390
|
+
*/
|
|
1391
|
+
getState(stateToMerge) {
|
|
1416
1392
|
if (stateToMerge === void 0) {
|
|
1417
1393
|
stateToMerge = this.state;
|
|
1418
1394
|
}
|
|
1419
1395
|
return getState(stateToMerge, this.props);
|
|
1420
|
-
}
|
|
1421
|
-
|
|
1396
|
+
}
|
|
1397
|
+
getItemCount() {
|
|
1422
1398
|
// things read better this way. They're in priority order:
|
|
1423
1399
|
// 1. `this.itemCount`
|
|
1424
1400
|
// 2. `this.props.itemCount`
|
|
1425
1401
|
// 3. `this.items.length`
|
|
1426
|
-
|
|
1402
|
+
let itemCount = this.items.length;
|
|
1427
1403
|
if (this.itemCount != null) {
|
|
1428
1404
|
itemCount = this.itemCount;
|
|
1429
1405
|
} else if (this.props.itemCount !== undefined) {
|
|
1430
1406
|
itemCount = this.props.itemCount;
|
|
1431
1407
|
}
|
|
1432
1408
|
return itemCount;
|
|
1433
|
-
}
|
|
1434
|
-
|
|
1409
|
+
}
|
|
1410
|
+
getItemNodeFromIndex(index) {
|
|
1435
1411
|
return this.props.environment.document.getElementById(this.getItemId(index));
|
|
1436
|
-
}
|
|
1437
|
-
|
|
1412
|
+
}
|
|
1413
|
+
scrollHighlightedItemIntoView() {
|
|
1438
1414
|
/* istanbul ignore else (react-native) */
|
|
1439
1415
|
{
|
|
1440
|
-
|
|
1416
|
+
const node = this.getItemNodeFromIndex(this.getState().highlightedIndex);
|
|
1441
1417
|
this.props.scrollIntoView(node, this._menuNode);
|
|
1442
1418
|
}
|
|
1443
|
-
}
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1419
|
+
}
|
|
1420
|
+
moveHighlightedIndex(amount, otherStateToSet) {
|
|
1421
|
+
const itemCount = this.getItemCount();
|
|
1422
|
+
const {
|
|
1423
|
+
highlightedIndex
|
|
1424
|
+
} = this.getState();
|
|
1449
1425
|
if (itemCount > 0) {
|
|
1450
|
-
|
|
1451
|
-
return _this6.getItemNodeFromIndex(index);
|
|
1452
|
-
});
|
|
1426
|
+
const nextHighlightedIndex = getNextWrappingIndex(amount, highlightedIndex, itemCount, index => this.getItemNodeFromIndex(index));
|
|
1453
1427
|
this.setHighlightedIndex(nextHighlightedIndex, otherStateToSet);
|
|
1454
1428
|
}
|
|
1455
|
-
}
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
highlightedIndex
|
|
1459
|
-
inputValue
|
|
1460
|
-
selectedItem
|
|
1461
|
-
isOpen
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1429
|
+
}
|
|
1430
|
+
getStateAndHelpers() {
|
|
1431
|
+
const {
|
|
1432
|
+
highlightedIndex,
|
|
1433
|
+
inputValue,
|
|
1434
|
+
selectedItem,
|
|
1435
|
+
isOpen
|
|
1436
|
+
} = this.getState();
|
|
1437
|
+
const {
|
|
1438
|
+
itemToString
|
|
1439
|
+
} = this.props;
|
|
1440
|
+
const {
|
|
1441
|
+
id
|
|
1442
|
+
} = this;
|
|
1443
|
+
const {
|
|
1444
|
+
getRootProps,
|
|
1445
|
+
getToggleButtonProps,
|
|
1446
|
+
getLabelProps,
|
|
1447
|
+
getMenuProps,
|
|
1448
|
+
getInputProps,
|
|
1449
|
+
getItemProps,
|
|
1450
|
+
openMenu,
|
|
1451
|
+
closeMenu,
|
|
1452
|
+
toggleMenu,
|
|
1453
|
+
selectItem,
|
|
1454
|
+
selectItemAtIndex,
|
|
1455
|
+
selectHighlightedItem,
|
|
1456
|
+
setHighlightedIndex,
|
|
1457
|
+
clearSelection,
|
|
1458
|
+
clearItems,
|
|
1459
|
+
reset,
|
|
1460
|
+
setItemCount,
|
|
1461
|
+
unsetItemCount,
|
|
1462
|
+
internalSetState: setState
|
|
1463
|
+
} = this;
|
|
1483
1464
|
return {
|
|
1484
1465
|
// prop getters
|
|
1485
|
-
getRootProps
|
|
1486
|
-
getToggleButtonProps
|
|
1487
|
-
getLabelProps
|
|
1488
|
-
getMenuProps
|
|
1489
|
-
getInputProps
|
|
1490
|
-
getItemProps
|
|
1466
|
+
getRootProps,
|
|
1467
|
+
getToggleButtonProps,
|
|
1468
|
+
getLabelProps,
|
|
1469
|
+
getMenuProps,
|
|
1470
|
+
getInputProps,
|
|
1471
|
+
getItemProps,
|
|
1491
1472
|
// actions
|
|
1492
|
-
reset
|
|
1493
|
-
openMenu
|
|
1494
|
-
closeMenu
|
|
1495
|
-
toggleMenu
|
|
1496
|
-
selectItem
|
|
1497
|
-
selectItemAtIndex
|
|
1498
|
-
selectHighlightedItem
|
|
1499
|
-
setHighlightedIndex
|
|
1500
|
-
clearSelection
|
|
1501
|
-
clearItems
|
|
1502
|
-
setItemCount
|
|
1503
|
-
unsetItemCount
|
|
1504
|
-
setState
|
|
1473
|
+
reset,
|
|
1474
|
+
openMenu,
|
|
1475
|
+
closeMenu,
|
|
1476
|
+
toggleMenu,
|
|
1477
|
+
selectItem,
|
|
1478
|
+
selectItemAtIndex,
|
|
1479
|
+
selectHighlightedItem,
|
|
1480
|
+
setHighlightedIndex,
|
|
1481
|
+
clearSelection,
|
|
1482
|
+
clearItems,
|
|
1483
|
+
setItemCount,
|
|
1484
|
+
unsetItemCount,
|
|
1485
|
+
setState,
|
|
1505
1486
|
// props
|
|
1506
|
-
itemToString
|
|
1487
|
+
itemToString,
|
|
1507
1488
|
// derived
|
|
1508
|
-
id
|
|
1489
|
+
id,
|
|
1509
1490
|
// state
|
|
1510
|
-
highlightedIndex
|
|
1511
|
-
inputValue
|
|
1512
|
-
isOpen
|
|
1513
|
-
selectedItem
|
|
1491
|
+
highlightedIndex,
|
|
1492
|
+
inputValue,
|
|
1493
|
+
isOpen,
|
|
1494
|
+
selectedItem
|
|
1514
1495
|
};
|
|
1515
1496
|
}
|
|
1516
1497
|
|
|
1517
1498
|
//////////////////////////// ROOT
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
var _this7 = this;
|
|
1499
|
+
|
|
1500
|
+
componentDidMount() {
|
|
1521
1501
|
/* istanbul ignore if (react-native) */
|
|
1522
1502
|
if (this.getMenuProps.called && !this.getMenuProps.suppressRefError) {
|
|
1523
1503
|
validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
|
|
@@ -1530,20 +1510,18 @@
|
|
|
1530
1510
|
// down long enough for the list to disappear (because the blur event fires on the input)
|
|
1531
1511
|
// this.isMouseDown is used in the blur handler on the input to determine whether the blur event should
|
|
1532
1512
|
// trigger hiding the menu.
|
|
1533
|
-
|
|
1534
|
-
|
|
1513
|
+
const onMouseDown = () => {
|
|
1514
|
+
this.isMouseDown = true;
|
|
1535
1515
|
};
|
|
1536
|
-
|
|
1537
|
-
|
|
1516
|
+
const onMouseUp = event => {
|
|
1517
|
+
this.isMouseDown = false;
|
|
1538
1518
|
// if the target element or the activeElement is within a downshift node
|
|
1539
1519
|
// then we don't want to reset downshift
|
|
1540
|
-
|
|
1541
|
-
if (!contextWithinDownshift &&
|
|
1542
|
-
|
|
1520
|
+
const contextWithinDownshift = targetWithinDownshift(event.target, [this._rootNode, this._menuNode], this.props.environment);
|
|
1521
|
+
if (!contextWithinDownshift && this.getState().isOpen) {
|
|
1522
|
+
this.reset({
|
|
1543
1523
|
type: mouseUp
|
|
1544
|
-
},
|
|
1545
|
-
return _this7.props.onOuterClick(_this7.getStateAndHelpers());
|
|
1546
|
-
});
|
|
1524
|
+
}, () => this.props.onOuterClick(this.getStateAndHelpers()));
|
|
1547
1525
|
}
|
|
1548
1526
|
};
|
|
1549
1527
|
// Touching an element in iOS gives focus and hover states, but touching out of
|
|
@@ -1552,31 +1530,31 @@
|
|
|
1552
1530
|
// this.isTouchMove helps us track whether the user is tapping or swiping on a touch screen.
|
|
1553
1531
|
// If the user taps outside of Downshift, the component should be reset,
|
|
1554
1532
|
// but not if the user is swiping
|
|
1555
|
-
|
|
1556
|
-
|
|
1533
|
+
const onTouchStart = () => {
|
|
1534
|
+
this.isTouchMove = false;
|
|
1557
1535
|
};
|
|
1558
|
-
|
|
1559
|
-
|
|
1536
|
+
const onTouchMove = () => {
|
|
1537
|
+
this.isTouchMove = true;
|
|
1560
1538
|
};
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
if (!
|
|
1564
|
-
|
|
1539
|
+
const onTouchEnd = event => {
|
|
1540
|
+
const contextWithinDownshift = targetWithinDownshift(event.target, [this._rootNode, this._menuNode], this.props.environment, false);
|
|
1541
|
+
if (!this.isTouchMove && !contextWithinDownshift && this.getState().isOpen) {
|
|
1542
|
+
this.reset({
|
|
1565
1543
|
type: touchEnd
|
|
1566
|
-
},
|
|
1567
|
-
return _this7.props.onOuterClick(_this7.getStateAndHelpers());
|
|
1568
|
-
});
|
|
1544
|
+
}, () => this.props.onOuterClick(this.getStateAndHelpers()));
|
|
1569
1545
|
}
|
|
1570
1546
|
};
|
|
1571
|
-
|
|
1547
|
+
const {
|
|
1548
|
+
environment
|
|
1549
|
+
} = this.props;
|
|
1572
1550
|
environment.addEventListener('mousedown', onMouseDown);
|
|
1573
1551
|
environment.addEventListener('mouseup', onMouseUp);
|
|
1574
1552
|
environment.addEventListener('touchstart', onTouchStart);
|
|
1575
1553
|
environment.addEventListener('touchmove', onTouchMove);
|
|
1576
1554
|
environment.addEventListener('touchend', onTouchEnd);
|
|
1577
|
-
this.cleanup =
|
|
1578
|
-
|
|
1579
|
-
|
|
1555
|
+
this.cleanup = () => {
|
|
1556
|
+
this.internalClearTimeouts();
|
|
1557
|
+
this.updateStatus.cancel();
|
|
1580
1558
|
environment.removeEventListener('mousedown', onMouseDown);
|
|
1581
1559
|
environment.removeEventListener('mouseup', onMouseUp);
|
|
1582
1560
|
environment.removeEventListener('touchstart', onTouchStart);
|
|
@@ -1584,17 +1562,19 @@
|
|
|
1584
1562
|
environment.removeEventListener('touchend', onTouchEnd);
|
|
1585
1563
|
};
|
|
1586
1564
|
}
|
|
1587
|
-
}
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
currentHighlightedIndex
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1565
|
+
}
|
|
1566
|
+
shouldScroll(prevState, prevProps) {
|
|
1567
|
+
const {
|
|
1568
|
+
highlightedIndex: currentHighlightedIndex
|
|
1569
|
+
} = this.props.highlightedIndex === undefined ? this.getState() : this.props;
|
|
1570
|
+
const {
|
|
1571
|
+
highlightedIndex: prevHighlightedIndex
|
|
1572
|
+
} = prevProps.highlightedIndex === undefined ? prevState : prevProps;
|
|
1573
|
+
const scrollWhenOpen = currentHighlightedIndex && this.getState().isOpen && !prevState.isOpen;
|
|
1574
|
+
const scrollWhenNavigating = currentHighlightedIndex !== prevHighlightedIndex;
|
|
1595
1575
|
return scrollWhenOpen || scrollWhenNavigating;
|
|
1596
|
-
}
|
|
1597
|
-
|
|
1576
|
+
}
|
|
1577
|
+
componentDidUpdate(prevProps, prevState) {
|
|
1598
1578
|
{
|
|
1599
1579
|
validateControlledUnchanged(this.state, prevProps, this.props);
|
|
1600
1580
|
/* istanbul ignore if (react-native) */
|
|
@@ -1616,12 +1596,13 @@
|
|
|
1616
1596
|
{
|
|
1617
1597
|
this.updateStatus();
|
|
1618
1598
|
}
|
|
1619
|
-
}
|
|
1620
|
-
|
|
1599
|
+
}
|
|
1600
|
+
componentWillUnmount() {
|
|
1621
1601
|
this.cleanup(); // avoids memory leak
|
|
1622
|
-
}
|
|
1623
|
-
|
|
1624
|
-
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
render() {
|
|
1605
|
+
const children = unwrapArray(this.props.children, noop);
|
|
1625
1606
|
// because the items are rerendered every time we call the children
|
|
1626
1607
|
// we clear this out each render and it will be populated again as
|
|
1627
1608
|
// getItemProps is called.
|
|
@@ -1641,7 +1622,7 @@
|
|
|
1641
1622
|
this.getLabelProps.called = false;
|
|
1642
1623
|
// and something similar for getInputProps
|
|
1643
1624
|
this.getInputProps.called = false;
|
|
1644
|
-
|
|
1625
|
+
const element = unwrapArray(children(this.getStateAndHelpers()));
|
|
1645
1626
|
if (!element) {
|
|
1646
1627
|
return null;
|
|
1647
1628
|
}
|
|
@@ -1663,14 +1644,13 @@
|
|
|
1663
1644
|
|
|
1664
1645
|
throw new Error('downshift: If you return a non-DOM element, you must apply the getRootProps function');
|
|
1665
1646
|
}
|
|
1666
|
-
}
|
|
1667
|
-
|
|
1668
|
-
}(preact.Component);
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1669
1649
|
Downshift.defaultProps = {
|
|
1670
1650
|
defaultHighlightedIndex: null,
|
|
1671
1651
|
defaultIsOpen: false,
|
|
1672
1652
|
getA11yStatusMessage: getA11yStatusMessage$1,
|
|
1673
|
-
itemToString:
|
|
1653
|
+
itemToString: i => {
|
|
1674
1654
|
if (i == null) {
|
|
1675
1655
|
return '';
|
|
1676
1656
|
}
|
|
@@ -1686,76 +1666,81 @@
|
|
|
1686
1666
|
onChange: noop,
|
|
1687
1667
|
onSelect: noop,
|
|
1688
1668
|
onOuterClick: noop,
|
|
1689
|
-
selectedItemChanged:
|
|
1690
|
-
return prevItem !== item;
|
|
1691
|
-
},
|
|
1669
|
+
selectedItemChanged: (prevItem, item) => prevItem !== item,
|
|
1692
1670
|
environment: /* istanbul ignore next (ssr) */
|
|
1693
1671
|
typeof window === 'undefined' ? {} : window,
|
|
1694
|
-
stateReducer:
|
|
1695
|
-
return stateToSet;
|
|
1696
|
-
},
|
|
1672
|
+
stateReducer: (state, stateToSet) => stateToSet,
|
|
1697
1673
|
suppressRefError: false,
|
|
1698
|
-
scrollIntoView
|
|
1674
|
+
scrollIntoView
|
|
1699
1675
|
};
|
|
1700
1676
|
Downshift.stateChangeTypes = stateChangeTypes$3;
|
|
1701
1677
|
return Downshift;
|
|
1702
|
-
}();
|
|
1678
|
+
})();
|
|
1703
1679
|
var Downshift$1 = Downshift;
|
|
1704
|
-
function validateGetMenuPropsCalledCorrectly(node,
|
|
1705
|
-
|
|
1680
|
+
function validateGetMenuPropsCalledCorrectly(node, _ref3) {
|
|
1681
|
+
let {
|
|
1682
|
+
refKey
|
|
1683
|
+
} = _ref3;
|
|
1706
1684
|
if (!node) {
|
|
1707
1685
|
// eslint-disable-next-line no-console
|
|
1708
|
-
console.error(
|
|
1686
|
+
console.error(`downshift: The ref prop "${refKey}" from getMenuProps was not applied correctly on your menu element.`);
|
|
1709
1687
|
}
|
|
1710
1688
|
}
|
|
1711
|
-
function validateGetRootPropsCalledCorrectly(element,
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1689
|
+
function validateGetRootPropsCalledCorrectly(element, _ref4) {
|
|
1690
|
+
let {
|
|
1691
|
+
refKey
|
|
1692
|
+
} = _ref4;
|
|
1693
|
+
const refKeySpecified = refKey !== 'ref';
|
|
1694
|
+
const isComposite = !isDOMElement(element);
|
|
1715
1695
|
if (isComposite && !refKeySpecified && !reactIs.exports.isForwardRef(element)) {
|
|
1716
1696
|
// eslint-disable-next-line no-console
|
|
1717
1697
|
console.error('downshift: You returned a non-DOM element. You must specify a refKey in getRootProps');
|
|
1718
1698
|
} else if (!isComposite && refKeySpecified) {
|
|
1719
1699
|
// eslint-disable-next-line no-console
|
|
1720
|
-
console.error(
|
|
1700
|
+
console.error(`downshift: You returned a DOM element. You should not specify a refKey in getRootProps. You specified "${refKey}"`);
|
|
1721
1701
|
}
|
|
1722
1702
|
if (!reactIs.exports.isForwardRef(element) && !getElementProps(element)[refKey]) {
|
|
1723
1703
|
// eslint-disable-next-line no-console
|
|
1724
|
-
console.error(
|
|
1704
|
+
console.error(`downshift: You must apply the ref prop "${refKey}" from getRootProps onto your root element.`);
|
|
1725
1705
|
}
|
|
1726
1706
|
}
|
|
1727
1707
|
|
|
1728
|
-
|
|
1729
|
-
var dropdownDefaultStateValues = {
|
|
1708
|
+
const dropdownDefaultStateValues = {
|
|
1730
1709
|
highlightedIndex: -1,
|
|
1731
1710
|
isOpen: false,
|
|
1732
1711
|
selectedItem: null,
|
|
1733
1712
|
inputValue: ''
|
|
1734
1713
|
};
|
|
1735
1714
|
function callOnChangeProps(action, state, newState) {
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1715
|
+
const {
|
|
1716
|
+
props,
|
|
1717
|
+
type
|
|
1718
|
+
} = action;
|
|
1719
|
+
const changes = {};
|
|
1720
|
+
Object.keys(state).forEach(key => {
|
|
1740
1721
|
invokeOnChangeHandler(key, action, state, newState);
|
|
1741
1722
|
if (newState[key] !== state[key]) {
|
|
1742
1723
|
changes[key] = newState[key];
|
|
1743
1724
|
}
|
|
1744
1725
|
});
|
|
1745
1726
|
if (props.onStateChange && Object.keys(changes).length) {
|
|
1746
|
-
props.onStateChange(
|
|
1747
|
-
type
|
|
1748
|
-
|
|
1727
|
+
props.onStateChange({
|
|
1728
|
+
type,
|
|
1729
|
+
...changes
|
|
1730
|
+
});
|
|
1749
1731
|
}
|
|
1750
1732
|
}
|
|
1751
1733
|
function invokeOnChangeHandler(key, action, state, newState) {
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1734
|
+
const {
|
|
1735
|
+
props,
|
|
1736
|
+
type
|
|
1737
|
+
} = action;
|
|
1738
|
+
const handler = `on${capitalizeString(key)}Change`;
|
|
1755
1739
|
if (props[handler] && newState[key] !== undefined && newState[key] !== state[key]) {
|
|
1756
|
-
props[handler](
|
|
1757
|
-
type
|
|
1758
|
-
|
|
1740
|
+
props[handler]({
|
|
1741
|
+
type,
|
|
1742
|
+
...newState
|
|
1743
|
+
});
|
|
1759
1744
|
}
|
|
1760
1745
|
}
|
|
1761
1746
|
|
|
@@ -1777,36 +1762,37 @@
|
|
|
1777
1762
|
* @returns {string} The a11y message.
|
|
1778
1763
|
*/
|
|
1779
1764
|
function getA11ySelectionMessage(selectionParameters) {
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1765
|
+
const {
|
|
1766
|
+
selectedItem,
|
|
1767
|
+
itemToString: itemToStringLocal
|
|
1768
|
+
} = selectionParameters;
|
|
1769
|
+
return selectedItem ? `${itemToStringLocal(selectedItem)} has been selected.` : '';
|
|
1783
1770
|
}
|
|
1784
1771
|
|
|
1785
1772
|
/**
|
|
1786
1773
|
* Debounced call for updating the a11y message.
|
|
1787
1774
|
*/
|
|
1788
|
-
|
|
1775
|
+
const updateA11yStatus = debounce((getA11yMessage, document) => {
|
|
1789
1776
|
setStatus(getA11yMessage(), document);
|
|
1790
1777
|
}, 200);
|
|
1791
1778
|
|
|
1792
1779
|
// istanbul ignore next
|
|
1793
|
-
|
|
1780
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined' ? preact.useLayoutEffect : preact.useEffect;
|
|
1794
1781
|
function useElementIds(_ref) {
|
|
1795
|
-
|
|
1796
|
-
id =
|
|
1797
|
-
labelId
|
|
1798
|
-
menuId
|
|
1799
|
-
getItemId
|
|
1800
|
-
toggleButtonId
|
|
1801
|
-
inputId
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
}
|
|
1808
|
-
|
|
1809
|
-
inputId: inputId || id + "-input"
|
|
1782
|
+
let {
|
|
1783
|
+
id = `downshift-${generateId()}`,
|
|
1784
|
+
labelId,
|
|
1785
|
+
menuId,
|
|
1786
|
+
getItemId,
|
|
1787
|
+
toggleButtonId,
|
|
1788
|
+
inputId
|
|
1789
|
+
} = _ref;
|
|
1790
|
+
const elementIdsRef = preact.useRef({
|
|
1791
|
+
labelId: labelId || `${id}-label`,
|
|
1792
|
+
menuId: menuId || `${id}-menu`,
|
|
1793
|
+
getItemId: getItemId || (index => `${id}-item-${index}`),
|
|
1794
|
+
toggleButtonId: toggleButtonId || `${id}-toggle-button`,
|
|
1795
|
+
inputId: inputId || `${id}-input`
|
|
1810
1796
|
});
|
|
1811
1797
|
return elementIdsRef.current;
|
|
1812
1798
|
}
|
|
@@ -1826,10 +1812,10 @@
|
|
|
1826
1812
|
return /^\S{1}$/.test(key);
|
|
1827
1813
|
}
|
|
1828
1814
|
function capitalizeString(string) {
|
|
1829
|
-
return
|
|
1815
|
+
return `${string.slice(0, 1).toUpperCase()}${string.slice(1)}`;
|
|
1830
1816
|
}
|
|
1831
1817
|
function useLatestRef(val) {
|
|
1832
|
-
|
|
1818
|
+
const ref = preact.useRef(val);
|
|
1833
1819
|
// technically this is not "concurrent mode safe" because we're manipulating
|
|
1834
1820
|
// the value during render (so it's not idempotent). However, the places this
|
|
1835
1821
|
// hook is used is to support memoizing callbacks which will be called
|
|
@@ -1850,28 +1836,26 @@
|
|
|
1850
1836
|
* @returns {Array} An array with the state and an action dispatcher.
|
|
1851
1837
|
*/
|
|
1852
1838
|
function useEnhancedReducer(reducer, initialState, props) {
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1839
|
+
const prevStateRef = preact.useRef();
|
|
1840
|
+
const actionRef = preact.useRef();
|
|
1841
|
+
const enhancedReducer = preact.useCallback((state, action) => {
|
|
1856
1842
|
actionRef.current = action;
|
|
1857
1843
|
state = getState(state, action.props);
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1844
|
+
const changes = reducer(state, action);
|
|
1845
|
+
const newState = action.props.stateReducer(state, {
|
|
1846
|
+
...action,
|
|
1847
|
+
changes
|
|
1848
|
+
});
|
|
1862
1849
|
return newState;
|
|
1863
1850
|
}, [reducer]);
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
}, [propsRef]);
|
|
1873
|
-
var action = actionRef.current;
|
|
1874
|
-
preact.useEffect(function () {
|
|
1851
|
+
const [state, dispatch] = preact.useReducer(enhancedReducer, initialState);
|
|
1852
|
+
const propsRef = useLatestRef(props);
|
|
1853
|
+
const dispatchWithProps = preact.useCallback(action => dispatch({
|
|
1854
|
+
props: propsRef.current,
|
|
1855
|
+
...action
|
|
1856
|
+
}), [propsRef]);
|
|
1857
|
+
const action = actionRef.current;
|
|
1858
|
+
preact.useEffect(() => {
|
|
1875
1859
|
if (action && prevStateRef.current && prevStateRef.current !== state) {
|
|
1876
1860
|
callOnChangeProps(action, getState(prevStateRef.current, action.props), state);
|
|
1877
1861
|
}
|
|
@@ -1890,16 +1874,14 @@
|
|
|
1890
1874
|
* @returns {Array} An array with the state and an action dispatcher.
|
|
1891
1875
|
*/
|
|
1892
1876
|
function useControlledReducer$1(reducer, initialState, props) {
|
|
1893
|
-
|
|
1894
|
-
state = _useEnhancedReducer[0],
|
|
1895
|
-
dispatch = _useEnhancedReducer[1];
|
|
1877
|
+
const [state, dispatch] = useEnhancedReducer(reducer, initialState, props);
|
|
1896
1878
|
return [getState(state, props), dispatch];
|
|
1897
1879
|
}
|
|
1898
|
-
|
|
1899
|
-
itemToString
|
|
1900
|
-
stateReducer
|
|
1901
|
-
getA11ySelectionMessage
|
|
1902
|
-
scrollIntoView
|
|
1880
|
+
const defaultProps$3 = {
|
|
1881
|
+
itemToString,
|
|
1882
|
+
stateReducer,
|
|
1883
|
+
getA11ySelectionMessage,
|
|
1884
|
+
scrollIntoView,
|
|
1903
1885
|
environment: /* istanbul ignore next (ssr) */
|
|
1904
1886
|
typeof window === 'undefined' ? {} : window
|
|
1905
1887
|
};
|
|
@@ -1907,7 +1889,7 @@
|
|
|
1907
1889
|
if (defaultStateValues === void 0) {
|
|
1908
1890
|
defaultStateValues = dropdownDefaultStateValues;
|
|
1909
1891
|
}
|
|
1910
|
-
|
|
1892
|
+
const defaultValue = props[`default${capitalizeString(propKey)}`];
|
|
1911
1893
|
if (defaultValue !== undefined) {
|
|
1912
1894
|
return defaultValue;
|
|
1913
1895
|
}
|
|
@@ -1917,34 +1899,38 @@
|
|
|
1917
1899
|
if (defaultStateValues === void 0) {
|
|
1918
1900
|
defaultStateValues = dropdownDefaultStateValues;
|
|
1919
1901
|
}
|
|
1920
|
-
|
|
1902
|
+
const value = props[propKey];
|
|
1921
1903
|
if (value !== undefined) {
|
|
1922
1904
|
return value;
|
|
1923
1905
|
}
|
|
1924
|
-
|
|
1906
|
+
const initialValue = props[`initial${capitalizeString(propKey)}`];
|
|
1925
1907
|
if (initialValue !== undefined) {
|
|
1926
1908
|
return initialValue;
|
|
1927
1909
|
}
|
|
1928
1910
|
return getDefaultValue$1(props, propKey, defaultStateValues);
|
|
1929
1911
|
}
|
|
1930
1912
|
function getInitialState$2(props) {
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1913
|
+
const selectedItem = getInitialValue$1(props, 'selectedItem');
|
|
1914
|
+
const isOpen = getInitialValue$1(props, 'isOpen');
|
|
1915
|
+
const highlightedIndex = getInitialValue$1(props, 'highlightedIndex');
|
|
1916
|
+
const inputValue = getInitialValue$1(props, 'inputValue');
|
|
1935
1917
|
return {
|
|
1936
1918
|
highlightedIndex: highlightedIndex < 0 && selectedItem && isOpen ? props.items.indexOf(selectedItem) : highlightedIndex,
|
|
1937
|
-
isOpen
|
|
1938
|
-
selectedItem
|
|
1939
|
-
inputValue
|
|
1919
|
+
isOpen,
|
|
1920
|
+
selectedItem,
|
|
1921
|
+
inputValue
|
|
1940
1922
|
};
|
|
1941
1923
|
}
|
|
1942
1924
|
function getHighlightedIndexOnOpen(props, state, offset) {
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1925
|
+
const {
|
|
1926
|
+
items,
|
|
1927
|
+
initialHighlightedIndex,
|
|
1928
|
+
defaultHighlightedIndex
|
|
1929
|
+
} = props;
|
|
1930
|
+
const {
|
|
1931
|
+
selectedItem,
|
|
1932
|
+
highlightedIndex
|
|
1933
|
+
} = state;
|
|
1948
1934
|
if (items.length === 0) {
|
|
1949
1935
|
return -1;
|
|
1950
1936
|
}
|
|
@@ -1975,34 +1961,30 @@
|
|
|
1975
1961
|
* @returns {Object} Ref containing whether mouseDown or touchMove event is happening
|
|
1976
1962
|
*/
|
|
1977
1963
|
function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, handleBlur) {
|
|
1978
|
-
|
|
1964
|
+
const mouseAndTouchTrackersRef = preact.useRef({
|
|
1979
1965
|
isMouseDown: false,
|
|
1980
1966
|
isTouchMove: false
|
|
1981
1967
|
});
|
|
1982
|
-
preact.useEffect(
|
|
1968
|
+
preact.useEffect(() => {
|
|
1983
1969
|
// The same strategy for checking if a click occurred inside or outside downsift
|
|
1984
1970
|
// as in downshift.js.
|
|
1985
|
-
|
|
1971
|
+
const onMouseDown = () => {
|
|
1986
1972
|
mouseAndTouchTrackersRef.current.isMouseDown = true;
|
|
1987
1973
|
};
|
|
1988
|
-
|
|
1974
|
+
const onMouseUp = event => {
|
|
1989
1975
|
mouseAndTouchTrackersRef.current.isMouseDown = false;
|
|
1990
|
-
if (isOpen && !targetWithinDownshift(event.target, downshiftElementRefs.map(
|
|
1991
|
-
return ref.current;
|
|
1992
|
-
}), environment)) {
|
|
1976
|
+
if (isOpen && !targetWithinDownshift(event.target, downshiftElementRefs.map(ref => ref.current), environment)) {
|
|
1993
1977
|
handleBlur();
|
|
1994
1978
|
}
|
|
1995
1979
|
};
|
|
1996
|
-
|
|
1980
|
+
const onTouchStart = () => {
|
|
1997
1981
|
mouseAndTouchTrackersRef.current.isTouchMove = false;
|
|
1998
1982
|
};
|
|
1999
|
-
|
|
1983
|
+
const onTouchMove = () => {
|
|
2000
1984
|
mouseAndTouchTrackersRef.current.isTouchMove = true;
|
|
2001
1985
|
};
|
|
2002
|
-
|
|
2003
|
-
if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, downshiftElementRefs.map(
|
|
2004
|
-
return ref.current;
|
|
2005
|
-
}), environment, false)) {
|
|
1986
|
+
const onTouchEnd = event => {
|
|
1987
|
+
if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, downshiftElementRefs.map(ref => ref.current), environment, false)) {
|
|
2006
1988
|
handleBlur();
|
|
2007
1989
|
}
|
|
2008
1990
|
};
|
|
@@ -2025,9 +2007,7 @@
|
|
|
2025
2007
|
|
|
2026
2008
|
/* istanbul ignore next */
|
|
2027
2009
|
// eslint-disable-next-line import/no-mutable-exports
|
|
2028
|
-
|
|
2029
|
-
return noop;
|
|
2030
|
-
};
|
|
2010
|
+
let useGetterPropsCalledChecker = () => noop;
|
|
2031
2011
|
/**
|
|
2032
2012
|
* Custom hook that checks if getter props are called correctly.
|
|
2033
2013
|
*
|
|
@@ -2036,77 +2016,82 @@
|
|
|
2036
2016
|
*/
|
|
2037
2017
|
/* istanbul ignore next */
|
|
2038
2018
|
{
|
|
2039
|
-
useGetterPropsCalledChecker = function
|
|
2040
|
-
|
|
2019
|
+
useGetterPropsCalledChecker = function () {
|
|
2020
|
+
const isInitialMountRef = preact.useRef(true);
|
|
2041
2021
|
for (var _len = arguments.length, propKeys = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2042
2022
|
propKeys[_key] = arguments[_key];
|
|
2043
2023
|
}
|
|
2044
|
-
|
|
2024
|
+
const getterPropsCalledRef = preact.useRef(propKeys.reduce((acc, propKey) => {
|
|
2045
2025
|
acc[propKey] = {};
|
|
2046
2026
|
return acc;
|
|
2047
2027
|
}, {}));
|
|
2048
|
-
preact.useEffect(
|
|
2049
|
-
Object.keys(getterPropsCalledRef.current).forEach(
|
|
2050
|
-
|
|
2028
|
+
preact.useEffect(() => {
|
|
2029
|
+
Object.keys(getterPropsCalledRef.current).forEach(propKey => {
|
|
2030
|
+
const propCallInfo = getterPropsCalledRef.current[propKey];
|
|
2051
2031
|
if (isInitialMountRef.current) {
|
|
2052
2032
|
if (!Object.keys(propCallInfo).length) {
|
|
2053
2033
|
// eslint-disable-next-line no-console
|
|
2054
|
-
console.error(
|
|
2034
|
+
console.error(`downshift: You forgot to call the ${propKey} getter function on your component / element.`);
|
|
2055
2035
|
return;
|
|
2056
2036
|
}
|
|
2057
2037
|
}
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2038
|
+
const {
|
|
2039
|
+
suppressRefError,
|
|
2040
|
+
refKey,
|
|
2041
|
+
elementRef
|
|
2042
|
+
} = propCallInfo;
|
|
2061
2043
|
if ((!elementRef || !elementRef.current) && !suppressRefError) {
|
|
2062
2044
|
// eslint-disable-next-line no-console
|
|
2063
|
-
console.error(
|
|
2045
|
+
console.error(`downshift: The ref prop "${refKey}" from ${propKey} was not applied correctly on your element.`);
|
|
2064
2046
|
}
|
|
2065
2047
|
});
|
|
2066
2048
|
isInitialMountRef.current = false;
|
|
2067
2049
|
});
|
|
2068
|
-
|
|
2050
|
+
const setGetterPropCallInfo = preact.useCallback((propKey, suppressRefError, refKey, elementRef) => {
|
|
2069
2051
|
getterPropsCalledRef.current[propKey] = {
|
|
2070
|
-
suppressRefError
|
|
2071
|
-
refKey
|
|
2072
|
-
elementRef
|
|
2052
|
+
suppressRefError,
|
|
2053
|
+
refKey,
|
|
2054
|
+
elementRef
|
|
2073
2055
|
};
|
|
2074
2056
|
}, []);
|
|
2075
2057
|
return setGetterPropCallInfo;
|
|
2076
2058
|
};
|
|
2077
2059
|
}
|
|
2078
2060
|
function useA11yMessageSetter(getA11yMessage, dependencyArray, _ref2) {
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2061
|
+
let {
|
|
2062
|
+
isInitialMount,
|
|
2063
|
+
highlightedIndex,
|
|
2064
|
+
items,
|
|
2065
|
+
environment,
|
|
2066
|
+
...rest
|
|
2067
|
+
} = _ref2;
|
|
2084
2068
|
// Sets a11y status message on changes in state.
|
|
2085
|
-
preact.useEffect(
|
|
2069
|
+
preact.useEffect(() => {
|
|
2086
2070
|
if (isInitialMount || false) {
|
|
2087
2071
|
return;
|
|
2088
2072
|
}
|
|
2089
|
-
updateA11yStatus(
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
}, environment.document);
|
|
2073
|
+
updateA11yStatus(() => getA11yMessage({
|
|
2074
|
+
highlightedIndex,
|
|
2075
|
+
highlightedItem: items[highlightedIndex],
|
|
2076
|
+
resultCount: items.length,
|
|
2077
|
+
...rest
|
|
2078
|
+
}), environment.document);
|
|
2096
2079
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2097
2080
|
}, dependencyArray);
|
|
2098
2081
|
}
|
|
2099
2082
|
function useScrollIntoView(_ref3) {
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2083
|
+
let {
|
|
2084
|
+
highlightedIndex,
|
|
2085
|
+
isOpen,
|
|
2086
|
+
itemRefs,
|
|
2087
|
+
getItemNodeFromIndex,
|
|
2088
|
+
menuElement,
|
|
2089
|
+
scrollIntoView: scrollIntoViewProp
|
|
2090
|
+
} = _ref3;
|
|
2106
2091
|
// used not to scroll on highlight by mouse.
|
|
2107
|
-
|
|
2092
|
+
const shouldScrollRef = preact.useRef(true);
|
|
2108
2093
|
// Scroll on highlighted item if change comes from keyboard.
|
|
2109
|
-
useIsomorphicLayoutEffect(
|
|
2094
|
+
useIsomorphicLayoutEffect(() => {
|
|
2110
2095
|
if (highlightedIndex < 0 || !isOpen || !Object.keys(itemRefs.current).length) {
|
|
2111
2096
|
return;
|
|
2112
2097
|
}
|
|
@@ -2121,16 +2106,18 @@
|
|
|
2121
2106
|
}
|
|
2122
2107
|
|
|
2123
2108
|
// eslint-disable-next-line import/no-mutable-exports
|
|
2124
|
-
|
|
2109
|
+
let useControlPropsValidator = noop;
|
|
2125
2110
|
/* istanbul ignore next */
|
|
2126
2111
|
{
|
|
2127
|
-
useControlPropsValidator =
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2112
|
+
useControlPropsValidator = _ref4 => {
|
|
2113
|
+
let {
|
|
2114
|
+
isInitialMount,
|
|
2115
|
+
props,
|
|
2116
|
+
state
|
|
2117
|
+
} = _ref4;
|
|
2131
2118
|
// used for checking when props are moving from controlled to uncontrolled.
|
|
2132
|
-
|
|
2133
|
-
preact.useEffect(
|
|
2119
|
+
const prevPropsRef = preact.useRef(props);
|
|
2120
|
+
preact.useEffect(() => {
|
|
2134
2121
|
if (isInitialMount) {
|
|
2135
2122
|
return;
|
|
2136
2123
|
}
|
|
@@ -2141,9 +2128,11 @@
|
|
|
2141
2128
|
}
|
|
2142
2129
|
|
|
2143
2130
|
function downshiftCommonReducer(state, action, stateChangeTypes) {
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2131
|
+
const {
|
|
2132
|
+
type,
|
|
2133
|
+
props
|
|
2134
|
+
} = action;
|
|
2135
|
+
let changes;
|
|
2147
2136
|
switch (type) {
|
|
2148
2137
|
case stateChangeTypes.ItemMouseMove:
|
|
2149
2138
|
changes = {
|
|
@@ -2194,7 +2183,10 @@
|
|
|
2194
2183
|
default:
|
|
2195
2184
|
throw new Error('Reducer called without proper action type.');
|
|
2196
2185
|
}
|
|
2197
|
-
return
|
|
2186
|
+
return {
|
|
2187
|
+
...state,
|
|
2188
|
+
...changes
|
|
2189
|
+
};
|
|
2198
2190
|
}
|
|
2199
2191
|
/* eslint-enable complexity */
|
|
2200
2192
|
|
|
@@ -2306,28 +2298,28 @@
|
|
|
2306
2298
|
};
|
|
2307
2299
|
}
|
|
2308
2300
|
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2301
|
+
const ToggleButtonClick$1 = '__togglebutton_click__' ;
|
|
2302
|
+
const ToggleButtonKeyDownArrowDown = '__togglebutton_keydown_arrow_down__' ;
|
|
2303
|
+
const ToggleButtonKeyDownArrowUp = '__togglebutton_keydown_arrow_up__' ;
|
|
2304
|
+
const ToggleButtonKeyDownCharacter = '__togglebutton_keydown_character__' ;
|
|
2305
|
+
const ToggleButtonKeyDownEscape = '__togglebutton_keydown_escape__' ;
|
|
2306
|
+
const ToggleButtonKeyDownHome = '__togglebutton_keydown_home__' ;
|
|
2307
|
+
const ToggleButtonKeyDownEnd = '__togglebutton_keydown_end__' ;
|
|
2308
|
+
const ToggleButtonKeyDownEnter = '__togglebutton_keydown_enter__' ;
|
|
2309
|
+
const ToggleButtonKeyDownSpaceButton = '__togglebutton_keydown_space_button__' ;
|
|
2310
|
+
const ToggleButtonKeyDownPageUp = '__togglebutton_keydown_page_up__' ;
|
|
2311
|
+
const ToggleButtonKeyDownPageDown = '__togglebutton_keydown_page_down__' ;
|
|
2312
|
+
const ToggleButtonBlur = '__togglebutton_blur__' ;
|
|
2313
|
+
const MenuMouseLeave$1 = '__menu_mouse_leave__' ;
|
|
2314
|
+
const ItemMouseMove$1 = '__item_mouse_move__' ;
|
|
2315
|
+
const ItemClick$1 = '__item_click__' ;
|
|
2316
|
+
const FunctionToggleMenu$1 = '__function_toggle_menu__' ;
|
|
2317
|
+
const FunctionOpenMenu$1 = '__function_open_menu__' ;
|
|
2318
|
+
const FunctionCloseMenu$1 = '__function_close_menu__' ;
|
|
2319
|
+
const FunctionSetHighlightedIndex$1 = '__function_set_highlighted_index__' ;
|
|
2320
|
+
const FunctionSelectItem$1 = '__function_select_item__' ;
|
|
2321
|
+
const FunctionSetInputValue$1 = '__function_set_input_value__' ;
|
|
2322
|
+
const FunctionReset$2 = '__function_reset__' ;
|
|
2331
2323
|
|
|
2332
2324
|
var stateChangeTypes$2 = /*#__PURE__*/Object.freeze({
|
|
2333
2325
|
__proto__: null,
|
|
@@ -2357,10 +2349,12 @@
|
|
|
2357
2349
|
|
|
2358
2350
|
/* eslint-disable complexity */
|
|
2359
2351
|
function downshiftSelectReducer(state, action) {
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2352
|
+
const {
|
|
2353
|
+
type,
|
|
2354
|
+
props,
|
|
2355
|
+
altKey
|
|
2356
|
+
} = action;
|
|
2357
|
+
let changes;
|
|
2364
2358
|
switch (type) {
|
|
2365
2359
|
case ItemClick$1:
|
|
2366
2360
|
changes = {
|
|
@@ -2371,10 +2365,10 @@
|
|
|
2371
2365
|
break;
|
|
2372
2366
|
case ToggleButtonKeyDownCharacter:
|
|
2373
2367
|
{
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2368
|
+
const lowercasedKey = action.key;
|
|
2369
|
+
const inputValue = `${state.inputValue}${lowercasedKey}`;
|
|
2370
|
+
const prevHighlightedIndex = !state.isOpen && state.selectedItem ? props.items.indexOf(state.selectedItem) : state.highlightedIndex;
|
|
2371
|
+
const highlightedIndex = getItemIndexByCharacterKey({
|
|
2378
2372
|
keysSoFar: inputValue,
|
|
2379
2373
|
highlightedIndex: prevHighlightedIndex,
|
|
2380
2374
|
items: props.items,
|
|
@@ -2382,33 +2376,34 @@
|
|
|
2382
2376
|
getItemNodeFromIndex: action.getItemNodeFromIndex
|
|
2383
2377
|
});
|
|
2384
2378
|
changes = {
|
|
2385
|
-
inputValue
|
|
2386
|
-
highlightedIndex
|
|
2379
|
+
inputValue,
|
|
2380
|
+
highlightedIndex,
|
|
2387
2381
|
isOpen: true
|
|
2388
2382
|
};
|
|
2389
2383
|
}
|
|
2390
2384
|
break;
|
|
2391
2385
|
case ToggleButtonKeyDownArrowDown:
|
|
2392
2386
|
{
|
|
2393
|
-
|
|
2387
|
+
const highlightedIndex = state.isOpen ? getNextWrappingIndex(1, state.highlightedIndex, props.items.length, action.getItemNodeFromIndex, false) : altKey && state.selectedItem == null ? -1 : getHighlightedIndexOnOpen(props, state, 1);
|
|
2394
2388
|
changes = {
|
|
2395
|
-
highlightedIndex
|
|
2389
|
+
highlightedIndex,
|
|
2396
2390
|
isOpen: true
|
|
2397
2391
|
};
|
|
2398
2392
|
}
|
|
2399
2393
|
break;
|
|
2400
2394
|
case ToggleButtonKeyDownArrowUp:
|
|
2401
2395
|
if (state.isOpen && altKey) {
|
|
2402
|
-
changes =
|
|
2396
|
+
changes = {
|
|
2403
2397
|
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
2404
|
-
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2398
|
+
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
|
2399
|
+
...(state.highlightedIndex >= 0 && {
|
|
2400
|
+
selectedItem: props.items[state.highlightedIndex]
|
|
2401
|
+
})
|
|
2402
|
+
};
|
|
2408
2403
|
} else {
|
|
2409
|
-
|
|
2404
|
+
const highlightedIndex = state.isOpen ? getNextWrappingIndex(-1, state.highlightedIndex, props.items.length, action.getItemNodeFromIndex, false) : getHighlightedIndexOnOpen(props, state, -1);
|
|
2410
2405
|
changes = {
|
|
2411
|
-
highlightedIndex
|
|
2406
|
+
highlightedIndex,
|
|
2412
2407
|
isOpen: true
|
|
2413
2408
|
};
|
|
2414
2409
|
}
|
|
@@ -2416,12 +2411,13 @@
|
|
|
2416
2411
|
// only triggered when menu is open.
|
|
2417
2412
|
case ToggleButtonKeyDownEnter:
|
|
2418
2413
|
case ToggleButtonKeyDownSpaceButton:
|
|
2419
|
-
changes =
|
|
2414
|
+
changes = {
|
|
2420
2415
|
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
2421
|
-
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2416
|
+
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
|
2417
|
+
...(state.highlightedIndex >= 0 && {
|
|
2418
|
+
selectedItem: props.items[state.highlightedIndex]
|
|
2419
|
+
})
|
|
2420
|
+
};
|
|
2425
2421
|
break;
|
|
2426
2422
|
case ToggleButtonKeyDownHome:
|
|
2427
2423
|
changes = {
|
|
@@ -2452,12 +2448,13 @@
|
|
|
2452
2448
|
};
|
|
2453
2449
|
break;
|
|
2454
2450
|
case ToggleButtonBlur:
|
|
2455
|
-
changes =
|
|
2451
|
+
changes = {
|
|
2456
2452
|
isOpen: false,
|
|
2457
|
-
highlightedIndex: -1
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2453
|
+
highlightedIndex: -1,
|
|
2454
|
+
...(state.highlightedIndex >= 0 && {
|
|
2455
|
+
selectedItem: props.items[state.highlightedIndex]
|
|
2456
|
+
})
|
|
2457
|
+
};
|
|
2461
2458
|
break;
|
|
2462
2459
|
case FunctionSelectItem$1:
|
|
2463
2460
|
changes = {
|
|
@@ -2467,13 +2464,13 @@
|
|
|
2467
2464
|
default:
|
|
2468
2465
|
return downshiftCommonReducer(state, action, stateChangeTypes$2);
|
|
2469
2466
|
}
|
|
2470
|
-
return
|
|
2467
|
+
return {
|
|
2468
|
+
...state,
|
|
2469
|
+
...changes
|
|
2470
|
+
};
|
|
2471
2471
|
}
|
|
2472
2472
|
/* eslint-enable complexity */
|
|
2473
2473
|
|
|
2474
|
-
var _excluded$2 = ["onMouseLeave", "refKey", "onKeyDown", "onBlur", "ref"],
|
|
2475
|
-
_excluded2$2 = ["onBlur", "onClick", "onKeyDown", "refKey", "ref"],
|
|
2476
|
-
_excluded3$1 = ["item", "index", "onMouseMove", "onClick", "refKey", "ref", "disabled"];
|
|
2477
2474
|
useSelect.stateChangeTypes = stateChangeTypes$2;
|
|
2478
2475
|
function useSelect(userProps) {
|
|
2479
2476
|
if (userProps === void 0) {
|
|
@@ -2481,76 +2478,81 @@
|
|
|
2481
2478
|
}
|
|
2482
2479
|
validatePropTypes$2(userProps, useSelect);
|
|
2483
2480
|
// Props defaults and destructuring.
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2481
|
+
const props = {
|
|
2482
|
+
...defaultProps$2,
|
|
2483
|
+
...userProps
|
|
2484
|
+
};
|
|
2485
|
+
const {
|
|
2486
|
+
items,
|
|
2487
|
+
scrollIntoView,
|
|
2488
|
+
environment,
|
|
2489
|
+
itemToString,
|
|
2490
|
+
getA11ySelectionMessage,
|
|
2491
|
+
getA11yStatusMessage
|
|
2492
|
+
} = props;
|
|
2491
2493
|
// Initial state depending on controlled props.
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2494
|
+
const initialState = getInitialState$2(props);
|
|
2495
|
+
const [state, dispatch] = useControlledReducer$1(downshiftSelectReducer, initialState, props);
|
|
2496
|
+
const {
|
|
2497
|
+
isOpen,
|
|
2498
|
+
highlightedIndex,
|
|
2499
|
+
selectedItem,
|
|
2500
|
+
inputValue
|
|
2501
|
+
} = state;
|
|
2500
2502
|
|
|
2501
2503
|
// Element efs.
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2504
|
+
const toggleButtonRef = preact.useRef(null);
|
|
2505
|
+
const menuRef = preact.useRef(null);
|
|
2506
|
+
const itemRefs = preact.useRef({});
|
|
2505
2507
|
// used to keep the inputValue clearTimeout object between renders.
|
|
2506
|
-
|
|
2508
|
+
const clearTimeoutRef = preact.useRef(null);
|
|
2507
2509
|
// prevent id re-generation between renders.
|
|
2508
|
-
|
|
2510
|
+
const elementIds = useElementIds(props);
|
|
2509
2511
|
// used to keep track of how many items we had on previous cycle.
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
+
const previousResultCountRef = preact.useRef();
|
|
2513
|
+
const isInitialMountRef = preact.useRef(true);
|
|
2512
2514
|
// utility callback to get item element.
|
|
2513
|
-
|
|
2514
|
-
state
|
|
2515
|
-
props
|
|
2515
|
+
const latest = useLatestRef({
|
|
2516
|
+
state,
|
|
2517
|
+
props
|
|
2516
2518
|
});
|
|
2517
2519
|
|
|
2518
2520
|
// Some utils.
|
|
2519
|
-
|
|
2520
|
-
return itemRefs.current[elementIds.getItemId(index)];
|
|
2521
|
-
}, [elementIds]);
|
|
2521
|
+
const getItemNodeFromIndex = preact.useCallback(index => itemRefs.current[elementIds.getItemId(index)], [elementIds]);
|
|
2522
2522
|
|
|
2523
2523
|
// Effects.
|
|
2524
2524
|
// Sets a11y status message on changes in state.
|
|
2525
|
-
useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items],
|
|
2525
|
+
useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items], {
|
|
2526
2526
|
isInitialMount: isInitialMountRef.current,
|
|
2527
2527
|
previousResultCount: previousResultCountRef.current,
|
|
2528
|
-
items
|
|
2529
|
-
environment
|
|
2530
|
-
itemToString
|
|
2531
|
-
|
|
2528
|
+
items,
|
|
2529
|
+
environment,
|
|
2530
|
+
itemToString,
|
|
2531
|
+
...state
|
|
2532
|
+
});
|
|
2532
2533
|
// Sets a11y status message on changes in selectedItem.
|
|
2533
|
-
useA11yMessageSetter(getA11ySelectionMessage, [selectedItem],
|
|
2534
|
+
useA11yMessageSetter(getA11ySelectionMessage, [selectedItem], {
|
|
2534
2535
|
isInitialMount: isInitialMountRef.current,
|
|
2535
2536
|
previousResultCount: previousResultCountRef.current,
|
|
2536
|
-
items
|
|
2537
|
-
environment
|
|
2538
|
-
itemToString
|
|
2539
|
-
|
|
2537
|
+
items,
|
|
2538
|
+
environment,
|
|
2539
|
+
itemToString,
|
|
2540
|
+
...state
|
|
2541
|
+
});
|
|
2540
2542
|
// Scroll on highlighted item if change comes from keyboard.
|
|
2541
|
-
|
|
2543
|
+
const shouldScrollRef = useScrollIntoView({
|
|
2542
2544
|
menuElement: menuRef.current,
|
|
2543
|
-
highlightedIndex
|
|
2544
|
-
isOpen
|
|
2545
|
-
itemRefs
|
|
2546
|
-
scrollIntoView
|
|
2547
|
-
getItemNodeFromIndex
|
|
2545
|
+
highlightedIndex,
|
|
2546
|
+
isOpen,
|
|
2547
|
+
itemRefs,
|
|
2548
|
+
scrollIntoView,
|
|
2549
|
+
getItemNodeFromIndex
|
|
2548
2550
|
});
|
|
2549
2551
|
|
|
2550
2552
|
// Sets cleanup for the keysSoFar callback, debounded after 500ms.
|
|
2551
|
-
preact.useEffect(
|
|
2553
|
+
preact.useEffect(() => {
|
|
2552
2554
|
// init the clean function here as we need access to dispatch.
|
|
2553
|
-
clearTimeoutRef.current = debounce(
|
|
2555
|
+
clearTimeoutRef.current = debounce(outerDispatch => {
|
|
2554
2556
|
outerDispatch({
|
|
2555
2557
|
type: FunctionSetInputValue$1,
|
|
2556
2558
|
inputValue: ''
|
|
@@ -2558,13 +2560,13 @@
|
|
|
2558
2560
|
}, 500);
|
|
2559
2561
|
|
|
2560
2562
|
// Cancel any pending debounced calls on mount
|
|
2561
|
-
return
|
|
2563
|
+
return () => {
|
|
2562
2564
|
clearTimeoutRef.current.cancel();
|
|
2563
2565
|
};
|
|
2564
2566
|
}, []);
|
|
2565
2567
|
|
|
2566
2568
|
// Invokes the keysSoFar callback set up above.
|
|
2567
|
-
preact.useEffect(
|
|
2569
|
+
preact.useEffect(() => {
|
|
2568
2570
|
if (!inputValue) {
|
|
2569
2571
|
return;
|
|
2570
2572
|
}
|
|
@@ -2572,234 +2574,249 @@
|
|
|
2572
2574
|
}, [dispatch, inputValue]);
|
|
2573
2575
|
useControlPropsValidator({
|
|
2574
2576
|
isInitialMount: isInitialMountRef.current,
|
|
2575
|
-
props
|
|
2576
|
-
state
|
|
2577
|
+
props,
|
|
2578
|
+
state
|
|
2577
2579
|
});
|
|
2578
|
-
preact.useEffect(
|
|
2580
|
+
preact.useEffect(() => {
|
|
2579
2581
|
if (isInitialMountRef.current) {
|
|
2580
2582
|
return;
|
|
2581
2583
|
}
|
|
2582
2584
|
previousResultCountRef.current = items.length;
|
|
2583
2585
|
});
|
|
2584
2586
|
// Add mouse/touch events to document.
|
|
2585
|
-
|
|
2587
|
+
const mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [menuRef, toggleButtonRef], environment, () => {
|
|
2586
2588
|
dispatch({
|
|
2587
2589
|
type: ToggleButtonBlur
|
|
2588
2590
|
});
|
|
2589
2591
|
});
|
|
2590
|
-
|
|
2592
|
+
const setGetterPropCallInfo = useGetterPropsCalledChecker('getMenuProps', 'getToggleButtonProps');
|
|
2591
2593
|
// Make initial ref false.
|
|
2592
|
-
preact.useEffect(
|
|
2594
|
+
preact.useEffect(() => {
|
|
2593
2595
|
isInitialMountRef.current = false;
|
|
2594
|
-
return
|
|
2596
|
+
return () => {
|
|
2595
2597
|
isInitialMountRef.current = true;
|
|
2596
2598
|
};
|
|
2597
2599
|
}, []);
|
|
2598
2600
|
// Reset itemRefs on close.
|
|
2599
|
-
preact.useEffect(
|
|
2601
|
+
preact.useEffect(() => {
|
|
2600
2602
|
if (!isOpen) {
|
|
2601
2603
|
itemRefs.current = {};
|
|
2602
2604
|
}
|
|
2603
2605
|
}, [isOpen]);
|
|
2604
2606
|
|
|
2605
2607
|
// Event handler functions.
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2608
|
+
const toggleButtonKeyDownHandlers = preact.useMemo(() => ({
|
|
2609
|
+
ArrowDown(event) {
|
|
2610
|
+
event.preventDefault();
|
|
2611
|
+
dispatch({
|
|
2612
|
+
type: ToggleButtonKeyDownArrowDown,
|
|
2613
|
+
getItemNodeFromIndex,
|
|
2614
|
+
altKey: event.altKey
|
|
2615
|
+
});
|
|
2616
|
+
},
|
|
2617
|
+
ArrowUp(event) {
|
|
2618
|
+
event.preventDefault();
|
|
2619
|
+
dispatch({
|
|
2620
|
+
type: ToggleButtonKeyDownArrowUp,
|
|
2621
|
+
getItemNodeFromIndex,
|
|
2622
|
+
altKey: event.altKey
|
|
2623
|
+
});
|
|
2624
|
+
},
|
|
2625
|
+
Home(event) {
|
|
2626
|
+
event.preventDefault();
|
|
2627
|
+
dispatch({
|
|
2628
|
+
type: ToggleButtonKeyDownHome,
|
|
2629
|
+
getItemNodeFromIndex
|
|
2630
|
+
});
|
|
2631
|
+
},
|
|
2632
|
+
End(event) {
|
|
2633
|
+
event.preventDefault();
|
|
2634
|
+
dispatch({
|
|
2635
|
+
type: ToggleButtonKeyDownEnd,
|
|
2636
|
+
getItemNodeFromIndex
|
|
2637
|
+
});
|
|
2638
|
+
},
|
|
2639
|
+
Escape() {
|
|
2640
|
+
if (latest.current.state.isOpen) {
|
|
2610
2641
|
dispatch({
|
|
2611
|
-
type:
|
|
2612
|
-
getItemNodeFromIndex: getItemNodeFromIndex,
|
|
2613
|
-
altKey: event.altKey
|
|
2642
|
+
type: ToggleButtonKeyDownEscape
|
|
2614
2643
|
});
|
|
2615
|
-
}
|
|
2616
|
-
|
|
2644
|
+
}
|
|
2645
|
+
},
|
|
2646
|
+
Enter(event) {
|
|
2647
|
+
event.preventDefault();
|
|
2648
|
+
dispatch({
|
|
2649
|
+
type: latest.current.state.isOpen ? ToggleButtonKeyDownEnter : ToggleButtonClick$1
|
|
2650
|
+
});
|
|
2651
|
+
},
|
|
2652
|
+
PageUp(event) {
|
|
2653
|
+
if (latest.current.state.isOpen) {
|
|
2617
2654
|
event.preventDefault();
|
|
2618
2655
|
dispatch({
|
|
2619
|
-
type:
|
|
2620
|
-
getItemNodeFromIndex
|
|
2621
|
-
altKey: event.altKey
|
|
2656
|
+
type: ToggleButtonKeyDownPageUp,
|
|
2657
|
+
getItemNodeFromIndex
|
|
2622
2658
|
});
|
|
2623
|
-
}
|
|
2624
|
-
|
|
2659
|
+
}
|
|
2660
|
+
},
|
|
2661
|
+
PageDown(event) {
|
|
2662
|
+
if (latest.current.state.isOpen) {
|
|
2625
2663
|
event.preventDefault();
|
|
2626
2664
|
dispatch({
|
|
2627
|
-
type:
|
|
2628
|
-
getItemNodeFromIndex
|
|
2665
|
+
type: ToggleButtonKeyDownPageDown,
|
|
2666
|
+
getItemNodeFromIndex
|
|
2629
2667
|
});
|
|
2630
|
-
}
|
|
2631
|
-
|
|
2632
|
-
|
|
2668
|
+
}
|
|
2669
|
+
},
|
|
2670
|
+
' '(event) {
|
|
2671
|
+
event.preventDefault();
|
|
2672
|
+
const currentState = latest.current.state;
|
|
2673
|
+
if (!currentState.isOpen) {
|
|
2633
2674
|
dispatch({
|
|
2634
|
-
type:
|
|
2635
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
2675
|
+
type: ToggleButtonClick$1
|
|
2636
2676
|
});
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
dispatch({
|
|
2641
|
-
type: ToggleButtonKeyDownEscape
|
|
2642
|
-
});
|
|
2643
|
-
}
|
|
2644
|
-
},
|
|
2645
|
-
Enter: function Enter(event) {
|
|
2646
|
-
event.preventDefault();
|
|
2677
|
+
return;
|
|
2678
|
+
}
|
|
2679
|
+
if (currentState.inputValue) {
|
|
2647
2680
|
dispatch({
|
|
2648
|
-
type:
|
|
2681
|
+
type: ToggleButtonKeyDownCharacter,
|
|
2682
|
+
key: ' ',
|
|
2683
|
+
getItemNodeFromIndex
|
|
2684
|
+
});
|
|
2685
|
+
} else {
|
|
2686
|
+
dispatch({
|
|
2687
|
+
type: ToggleButtonKeyDownSpaceButton
|
|
2649
2688
|
});
|
|
2650
|
-
},
|
|
2651
|
-
PageUp: function PageUp(event) {
|
|
2652
|
-
if (latest.current.state.isOpen) {
|
|
2653
|
-
event.preventDefault();
|
|
2654
|
-
dispatch({
|
|
2655
|
-
type: ToggleButtonKeyDownPageUp,
|
|
2656
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
2657
|
-
});
|
|
2658
|
-
}
|
|
2659
|
-
},
|
|
2660
|
-
PageDown: function PageDown(event) {
|
|
2661
|
-
if (latest.current.state.isOpen) {
|
|
2662
|
-
event.preventDefault();
|
|
2663
|
-
dispatch({
|
|
2664
|
-
type: ToggleButtonKeyDownPageDown,
|
|
2665
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
2666
|
-
});
|
|
2667
|
-
}
|
|
2668
|
-
},
|
|
2669
|
-
' ': function _(event) {
|
|
2670
|
-
event.preventDefault();
|
|
2671
|
-
var currentState = latest.current.state;
|
|
2672
|
-
if (!currentState.isOpen) {
|
|
2673
|
-
dispatch({
|
|
2674
|
-
type: ToggleButtonClick$1
|
|
2675
|
-
});
|
|
2676
|
-
return;
|
|
2677
|
-
}
|
|
2678
|
-
if (currentState.inputValue) {
|
|
2679
|
-
dispatch({
|
|
2680
|
-
type: ToggleButtonKeyDownCharacter,
|
|
2681
|
-
key: ' ',
|
|
2682
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
2683
|
-
});
|
|
2684
|
-
} else {
|
|
2685
|
-
dispatch({
|
|
2686
|
-
type: ToggleButtonKeyDownSpaceButton
|
|
2687
|
-
});
|
|
2688
|
-
}
|
|
2689
2689
|
}
|
|
2690
|
-
}
|
|
2691
|
-
}, [dispatch, getItemNodeFromIndex, latest]);
|
|
2690
|
+
}
|
|
2691
|
+
}), [dispatch, getItemNodeFromIndex, latest]);
|
|
2692
2692
|
|
|
2693
2693
|
// Action functions.
|
|
2694
|
-
|
|
2694
|
+
const toggleMenu = preact.useCallback(() => {
|
|
2695
2695
|
dispatch({
|
|
2696
2696
|
type: FunctionToggleMenu$1
|
|
2697
2697
|
});
|
|
2698
2698
|
}, [dispatch]);
|
|
2699
|
-
|
|
2699
|
+
const closeMenu = preact.useCallback(() => {
|
|
2700
2700
|
dispatch({
|
|
2701
2701
|
type: FunctionCloseMenu$1
|
|
2702
2702
|
});
|
|
2703
2703
|
}, [dispatch]);
|
|
2704
|
-
|
|
2704
|
+
const openMenu = preact.useCallback(() => {
|
|
2705
2705
|
dispatch({
|
|
2706
2706
|
type: FunctionOpenMenu$1
|
|
2707
2707
|
});
|
|
2708
2708
|
}, [dispatch]);
|
|
2709
|
-
|
|
2709
|
+
const setHighlightedIndex = preact.useCallback(newHighlightedIndex => {
|
|
2710
2710
|
dispatch({
|
|
2711
2711
|
type: FunctionSetHighlightedIndex$1,
|
|
2712
2712
|
highlightedIndex: newHighlightedIndex
|
|
2713
2713
|
});
|
|
2714
2714
|
}, [dispatch]);
|
|
2715
|
-
|
|
2715
|
+
const selectItem = preact.useCallback(newSelectedItem => {
|
|
2716
2716
|
dispatch({
|
|
2717
2717
|
type: FunctionSelectItem$1,
|
|
2718
2718
|
selectedItem: newSelectedItem
|
|
2719
2719
|
});
|
|
2720
2720
|
}, [dispatch]);
|
|
2721
|
-
|
|
2721
|
+
const reset = preact.useCallback(() => {
|
|
2722
2722
|
dispatch({
|
|
2723
2723
|
type: FunctionReset$2
|
|
2724
2724
|
});
|
|
2725
2725
|
}, [dispatch]);
|
|
2726
|
-
|
|
2726
|
+
const setInputValue = preact.useCallback(newInputValue => {
|
|
2727
2727
|
dispatch({
|
|
2728
2728
|
type: FunctionSetInputValue$1,
|
|
2729
2729
|
inputValue: newInputValue
|
|
2730
2730
|
});
|
|
2731
2731
|
}, [dispatch]);
|
|
2732
2732
|
// Getter functions.
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
|
|
2752
|
-
var menuHandleMouseLeave = function menuHandleMouseLeave() {
|
|
2733
|
+
const getLabelProps = preact.useCallback(labelProps => ({
|
|
2734
|
+
id: elementIds.labelId,
|
|
2735
|
+
htmlFor: elementIds.toggleButtonId,
|
|
2736
|
+
...labelProps
|
|
2737
|
+
}), [elementIds]);
|
|
2738
|
+
const getMenuProps = preact.useCallback(function (_temp, _temp2) {
|
|
2739
|
+
let {
|
|
2740
|
+
onMouseLeave,
|
|
2741
|
+
refKey = 'ref',
|
|
2742
|
+
onKeyDown,
|
|
2743
|
+
onBlur,
|
|
2744
|
+
ref,
|
|
2745
|
+
...rest
|
|
2746
|
+
} = _temp === void 0 ? {} : _temp;
|
|
2747
|
+
let {
|
|
2748
|
+
suppressRefError = false
|
|
2749
|
+
} = _temp2 === void 0 ? {} : _temp2;
|
|
2750
|
+
const menuHandleMouseLeave = () => {
|
|
2753
2751
|
dispatch({
|
|
2754
2752
|
type: MenuMouseLeave$1
|
|
2755
2753
|
});
|
|
2756
2754
|
};
|
|
2757
2755
|
setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
|
|
2758
|
-
return
|
|
2759
|
-
|
|
2760
|
-
|
|
2756
|
+
return {
|
|
2757
|
+
[refKey]: handleRefs(ref, menuNode => {
|
|
2758
|
+
menuRef.current = menuNode;
|
|
2759
|
+
}),
|
|
2760
|
+
id: elementIds.menuId,
|
|
2761
|
+
role: 'listbox',
|
|
2762
|
+
'aria-labelledby': elementIds.labelId,
|
|
2763
|
+
tabIndex: -1,
|
|
2764
|
+
onMouseLeave: callAllEventHandlers(onMouseLeave, menuHandleMouseLeave),
|
|
2765
|
+
...rest
|
|
2766
|
+
};
|
|
2761
2767
|
}, [dispatch, setGetterPropCallInfo, elementIds]);
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
var toggleButtonHandleClick = function toggleButtonHandleClick() {
|
|
2768
|
+
const getToggleButtonProps = preact.useCallback(function (_temp3, _temp4) {
|
|
2769
|
+
let {
|
|
2770
|
+
onBlur,
|
|
2771
|
+
onClick,
|
|
2772
|
+
onKeyDown,
|
|
2773
|
+
refKey = 'ref',
|
|
2774
|
+
ref,
|
|
2775
|
+
...rest
|
|
2776
|
+
} = _temp3 === void 0 ? {} : _temp3;
|
|
2777
|
+
let {
|
|
2778
|
+
suppressRefError = false
|
|
2779
|
+
} = _temp4 === void 0 ? {} : _temp4;
|
|
2780
|
+
const latestState = latest.current.state;
|
|
2781
|
+
const toggleButtonHandleClick = () => {
|
|
2777
2782
|
dispatch({
|
|
2778
2783
|
type: ToggleButtonClick$1
|
|
2779
2784
|
});
|
|
2780
2785
|
};
|
|
2781
|
-
|
|
2786
|
+
const toggleButtonHandleBlur = () => {
|
|
2782
2787
|
if (latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
|
|
2783
2788
|
dispatch({
|
|
2784
2789
|
type: ToggleButtonBlur
|
|
2785
2790
|
});
|
|
2786
2791
|
}
|
|
2787
2792
|
};
|
|
2788
|
-
|
|
2789
|
-
|
|
2793
|
+
const toggleButtonHandleKeyDown = event => {
|
|
2794
|
+
const key = normalizeArrowKey(event);
|
|
2790
2795
|
if (key && toggleButtonKeyDownHandlers[key]) {
|
|
2791
2796
|
toggleButtonKeyDownHandlers[key](event);
|
|
2792
2797
|
} else if (isAcceptedCharacterKey(key)) {
|
|
2793
2798
|
dispatch({
|
|
2794
2799
|
type: ToggleButtonKeyDownCharacter,
|
|
2795
|
-
key
|
|
2796
|
-
getItemNodeFromIndex
|
|
2800
|
+
key,
|
|
2801
|
+
getItemNodeFromIndex
|
|
2797
2802
|
});
|
|
2798
2803
|
}
|
|
2799
2804
|
};
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2805
|
+
const toggleProps = {
|
|
2806
|
+
[refKey]: handleRefs(ref, toggleButtonNode => {
|
|
2807
|
+
toggleButtonRef.current = toggleButtonNode;
|
|
2808
|
+
}),
|
|
2809
|
+
'aria-activedescendant': latestState.isOpen && latestState.highlightedIndex > -1 ? elementIds.getItemId(latestState.highlightedIndex) : '',
|
|
2810
|
+
'aria-controls': elementIds.menuId,
|
|
2811
|
+
'aria-expanded': latest.current.state.isOpen,
|
|
2812
|
+
'aria-haspopup': 'listbox',
|
|
2813
|
+
'aria-labelledby': `${elementIds.labelId}`,
|
|
2814
|
+
id: elementIds.toggleButtonId,
|
|
2815
|
+
role: 'combobox',
|
|
2816
|
+
tabIndex: 0,
|
|
2817
|
+
onBlur: callAllEventHandlers(onBlur, toggleButtonHandleBlur),
|
|
2818
|
+
...rest
|
|
2819
|
+
};
|
|
2803
2820
|
if (!rest.disabled) {
|
|
2804
2821
|
toggleProps.onClick = callAllEventHandlers(onClick, toggleButtonHandleClick);
|
|
2805
2822
|
toggleProps.onKeyDown = callAllEventHandlers(onKeyDown, toggleButtonHandleKeyDown);
|
|
@@ -2807,54 +2824,56 @@
|
|
|
2807
2824
|
setGetterPropCallInfo('getToggleButtonProps', suppressRefError, refKey, toggleButtonRef);
|
|
2808
2825
|
return toggleProps;
|
|
2809
2826
|
}, [latest, elementIds, setGetterPropCallInfo, dispatch, mouseAndTouchTrackersRef, toggleButtonKeyDownHandlers, getItemNodeFromIndex]);
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2827
|
+
const getItemProps = preact.useCallback(function (_temp5) {
|
|
2828
|
+
let {
|
|
2829
|
+
item: itemProp,
|
|
2830
|
+
index: indexProp,
|
|
2831
|
+
onMouseMove,
|
|
2832
|
+
onClick,
|
|
2833
|
+
refKey = 'ref',
|
|
2834
|
+
ref,
|
|
2835
|
+
disabled,
|
|
2836
|
+
...rest
|
|
2837
|
+
} = _temp5 === void 0 ? {} : _temp5;
|
|
2838
|
+
const {
|
|
2839
|
+
state: latestState,
|
|
2840
|
+
props: latestProps
|
|
2841
|
+
} = latest.current;
|
|
2842
|
+
const item = itemProp ?? items[indexProp];
|
|
2843
|
+
const index = getItemIndex(indexProp, item, latestProps.items);
|
|
2844
|
+
const itemHandleMouseMove = () => {
|
|
2828
2845
|
if (index === latestState.highlightedIndex) {
|
|
2829
2846
|
return;
|
|
2830
2847
|
}
|
|
2831
2848
|
shouldScrollRef.current = false;
|
|
2832
2849
|
dispatch({
|
|
2833
2850
|
type: ItemMouseMove$1,
|
|
2834
|
-
index
|
|
2835
|
-
disabled
|
|
2851
|
+
index,
|
|
2852
|
+
disabled
|
|
2836
2853
|
});
|
|
2837
2854
|
};
|
|
2838
|
-
|
|
2855
|
+
const itemHandleClick = () => {
|
|
2839
2856
|
dispatch({
|
|
2840
2857
|
type: ItemClick$1,
|
|
2841
|
-
index
|
|
2858
|
+
index
|
|
2842
2859
|
});
|
|
2843
2860
|
};
|
|
2844
|
-
|
|
2861
|
+
const itemIndex = getItemIndex(index, item, latestProps.items);
|
|
2845
2862
|
if (itemIndex < 0) {
|
|
2846
2863
|
throw new Error('Pass either item or item index in getItemProps!');
|
|
2847
2864
|
}
|
|
2848
|
-
|
|
2849
|
-
disabled
|
|
2865
|
+
const itemProps = {
|
|
2866
|
+
disabled,
|
|
2850
2867
|
role: 'option',
|
|
2851
|
-
'aria-selected':
|
|
2852
|
-
id: elementIds.getItemId(itemIndex)
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2868
|
+
'aria-selected': `${item === selectedItem}`,
|
|
2869
|
+
id: elementIds.getItemId(itemIndex),
|
|
2870
|
+
[refKey]: handleRefs(ref, itemNode => {
|
|
2871
|
+
if (itemNode) {
|
|
2872
|
+
itemRefs.current[elementIds.getItemId(itemIndex)] = itemNode;
|
|
2873
|
+
}
|
|
2874
|
+
}),
|
|
2875
|
+
...rest
|
|
2876
|
+
};
|
|
2858
2877
|
if (!disabled) {
|
|
2859
2878
|
itemProps.onClick = callAllEventHandlers(onClick, itemHandleClick);
|
|
2860
2879
|
}
|
|
@@ -2863,49 +2882,49 @@
|
|
|
2863
2882
|
}, [latest, items, selectedItem, elementIds, shouldScrollRef, dispatch]);
|
|
2864
2883
|
return {
|
|
2865
2884
|
// prop getters.
|
|
2866
|
-
getToggleButtonProps
|
|
2867
|
-
getLabelProps
|
|
2868
|
-
getMenuProps
|
|
2869
|
-
getItemProps
|
|
2885
|
+
getToggleButtonProps,
|
|
2886
|
+
getLabelProps,
|
|
2887
|
+
getMenuProps,
|
|
2888
|
+
getItemProps,
|
|
2870
2889
|
// actions.
|
|
2871
|
-
toggleMenu
|
|
2872
|
-
openMenu
|
|
2873
|
-
closeMenu
|
|
2874
|
-
setHighlightedIndex
|
|
2875
|
-
selectItem
|
|
2876
|
-
reset
|
|
2877
|
-
setInputValue
|
|
2890
|
+
toggleMenu,
|
|
2891
|
+
openMenu,
|
|
2892
|
+
closeMenu,
|
|
2893
|
+
setHighlightedIndex,
|
|
2894
|
+
selectItem,
|
|
2895
|
+
reset,
|
|
2896
|
+
setInputValue,
|
|
2878
2897
|
// state.
|
|
2879
|
-
highlightedIndex
|
|
2880
|
-
isOpen
|
|
2881
|
-
selectedItem
|
|
2882
|
-
inputValue
|
|
2898
|
+
highlightedIndex,
|
|
2899
|
+
isOpen,
|
|
2900
|
+
selectedItem,
|
|
2901
|
+
inputValue
|
|
2883
2902
|
};
|
|
2884
2903
|
}
|
|
2885
2904
|
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2905
|
+
const InputKeyDownArrowDown = '__input_keydown_arrow_down__' ;
|
|
2906
|
+
const InputKeyDownArrowUp = '__input_keydown_arrow_up__' ;
|
|
2907
|
+
const InputKeyDownEscape = '__input_keydown_escape__' ;
|
|
2908
|
+
const InputKeyDownHome = '__input_keydown_home__' ;
|
|
2909
|
+
const InputKeyDownEnd = '__input_keydown_end__' ;
|
|
2910
|
+
const InputKeyDownPageUp = '__input_keydown_page_up__' ;
|
|
2911
|
+
const InputKeyDownPageDown = '__input_keydown_page_down__' ;
|
|
2912
|
+
const InputKeyDownEnter = '__input_keydown_enter__' ;
|
|
2913
|
+
const InputChange = '__input_change__' ;
|
|
2914
|
+
const InputBlur = '__input_blur__' ;
|
|
2915
|
+
const InputFocus = '__input_focus__' ;
|
|
2916
|
+
const MenuMouseLeave = '__menu_mouse_leave__' ;
|
|
2917
|
+
const ItemMouseMove = '__item_mouse_move__' ;
|
|
2918
|
+
const ItemClick = '__item_click__' ;
|
|
2919
|
+
const ToggleButtonClick = '__togglebutton_click__' ;
|
|
2920
|
+
const FunctionToggleMenu = '__function_toggle_menu__' ;
|
|
2921
|
+
const FunctionOpenMenu = '__function_open_menu__' ;
|
|
2922
|
+
const FunctionCloseMenu = '__function_close_menu__' ;
|
|
2923
|
+
const FunctionSetHighlightedIndex = '__function_set_highlighted_index__' ;
|
|
2924
|
+
const FunctionSelectItem = '__function_select_item__' ;
|
|
2925
|
+
const FunctionSetInputValue = '__function_set_input_value__' ;
|
|
2926
|
+
const FunctionReset$1 = '__function_reset__' ;
|
|
2927
|
+
const ControlledPropUpdatedSelectedItem = '__controlled_prop_updated_selected_item__' ;
|
|
2909
2928
|
|
|
2910
2929
|
var stateChangeTypes$1 = /*#__PURE__*/Object.freeze({
|
|
2911
2930
|
__proto__: null,
|
|
@@ -2935,17 +2954,22 @@
|
|
|
2935
2954
|
});
|
|
2936
2955
|
|
|
2937
2956
|
function getInitialState$1(props) {
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2957
|
+
const initialState = getInitialState$2(props);
|
|
2958
|
+
const {
|
|
2959
|
+
selectedItem
|
|
2960
|
+
} = initialState;
|
|
2961
|
+
let {
|
|
2962
|
+
inputValue
|
|
2963
|
+
} = initialState;
|
|
2941
2964
|
if (inputValue === '' && selectedItem && props.defaultInputValue === undefined && props.initialInputValue === undefined && props.inputValue === undefined) {
|
|
2942
2965
|
inputValue = props.itemToString(selectedItem);
|
|
2943
2966
|
}
|
|
2944
|
-
return
|
|
2945
|
-
|
|
2946
|
-
|
|
2967
|
+
return {
|
|
2968
|
+
...initialState,
|
|
2969
|
+
inputValue
|
|
2970
|
+
};
|
|
2947
2971
|
}
|
|
2948
|
-
|
|
2972
|
+
const propTypes$1 = {
|
|
2949
2973
|
items: PropTypes__default["default"].array.isRequired,
|
|
2950
2974
|
itemToString: PropTypes__default["default"].func,
|
|
2951
2975
|
getA11yStatusMessage: PropTypes__default["default"].func,
|
|
@@ -2998,13 +3022,11 @@
|
|
|
2998
3022
|
* @returns {Array} An array with the state and an action dispatcher.
|
|
2999
3023
|
*/
|
|
3000
3024
|
function useControlledReducer(reducer, initialState, props) {
|
|
3001
|
-
|
|
3002
|
-
|
|
3003
|
-
state = _useEnhancedReducer[0],
|
|
3004
|
-
dispatch = _useEnhancedReducer[1];
|
|
3025
|
+
const previousSelectedItemRef = preact.useRef();
|
|
3026
|
+
const [state, dispatch] = useEnhancedReducer(reducer, initialState, props);
|
|
3005
3027
|
|
|
3006
3028
|
// ToDo: if needed, make same approach as selectedItemChanged from Downshift.
|
|
3007
|
-
preact.useEffect(
|
|
3029
|
+
preact.useEffect(() => {
|
|
3008
3030
|
if (isControlledProp(props, 'selectedItem')) {
|
|
3009
3031
|
if (previousSelectedItemRef.current !== props.selectedItem) {
|
|
3010
3032
|
dispatch({
|
|
@@ -3019,23 +3041,26 @@
|
|
|
3019
3041
|
}
|
|
3020
3042
|
|
|
3021
3043
|
// eslint-disable-next-line import/no-mutable-exports
|
|
3022
|
-
|
|
3044
|
+
let validatePropTypes$1 = noop;
|
|
3023
3045
|
/* istanbul ignore next */
|
|
3024
3046
|
{
|
|
3025
|
-
validatePropTypes$1 =
|
|
3047
|
+
validatePropTypes$1 = (options, caller) => {
|
|
3026
3048
|
PropTypes__default["default"].checkPropTypes(propTypes$1, options, 'prop', caller.name);
|
|
3027
3049
|
};
|
|
3028
3050
|
}
|
|
3029
|
-
|
|
3051
|
+
const defaultProps$1 = {
|
|
3052
|
+
...defaultProps$3,
|
|
3030
3053
|
getA11yStatusMessage: getA11yStatusMessage$1
|
|
3031
|
-
}
|
|
3054
|
+
};
|
|
3032
3055
|
|
|
3033
3056
|
/* eslint-disable complexity */
|
|
3034
3057
|
function downshiftUseComboboxReducer(state, action) {
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3058
|
+
const {
|
|
3059
|
+
type,
|
|
3060
|
+
props,
|
|
3061
|
+
altKey
|
|
3062
|
+
} = action;
|
|
3063
|
+
let changes;
|
|
3039
3064
|
switch (type) {
|
|
3040
3065
|
case ItemClick:
|
|
3041
3066
|
changes = {
|
|
@@ -3060,13 +3085,14 @@
|
|
|
3060
3085
|
case InputKeyDownArrowUp:
|
|
3061
3086
|
if (state.isOpen) {
|
|
3062
3087
|
if (altKey) {
|
|
3063
|
-
changes =
|
|
3088
|
+
changes = {
|
|
3064
3089
|
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
3065
|
-
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3090
|
+
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
|
3091
|
+
...(state.highlightedIndex >= 0 && {
|
|
3092
|
+
selectedItem: props.items[state.highlightedIndex],
|
|
3093
|
+
inputValue: props.itemToString(props.items[state.highlightedIndex])
|
|
3094
|
+
})
|
|
3095
|
+
};
|
|
3070
3096
|
} else {
|
|
3071
3097
|
changes = {
|
|
3072
3098
|
highlightedIndex: getNextWrappingIndex(-1, state.highlightedIndex, props.items.length, action.getItemNodeFromIndex, true)
|
|
@@ -3080,22 +3106,24 @@
|
|
|
3080
3106
|
}
|
|
3081
3107
|
break;
|
|
3082
3108
|
case InputKeyDownEnter:
|
|
3083
|
-
changes =
|
|
3109
|
+
changes = {
|
|
3084
3110
|
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
3085
|
-
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3111
|
+
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
|
3112
|
+
...(state.highlightedIndex >= 0 && {
|
|
3113
|
+
selectedItem: props.items[state.highlightedIndex],
|
|
3114
|
+
inputValue: props.itemToString(props.items[state.highlightedIndex])
|
|
3115
|
+
})
|
|
3116
|
+
};
|
|
3090
3117
|
break;
|
|
3091
3118
|
case InputKeyDownEscape:
|
|
3092
|
-
changes =
|
|
3119
|
+
changes = {
|
|
3093
3120
|
isOpen: false,
|
|
3094
|
-
highlightedIndex: -1
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3121
|
+
highlightedIndex: -1,
|
|
3122
|
+
...(!state.isOpen && {
|
|
3123
|
+
selectedItem: null,
|
|
3124
|
+
inputValue: ''
|
|
3125
|
+
})
|
|
3126
|
+
};
|
|
3099
3127
|
break;
|
|
3100
3128
|
case InputKeyDownPageUp:
|
|
3101
3129
|
changes = {
|
|
@@ -3118,13 +3146,14 @@
|
|
|
3118
3146
|
};
|
|
3119
3147
|
break;
|
|
3120
3148
|
case InputBlur:
|
|
3121
|
-
changes =
|
|
3149
|
+
changes = {
|
|
3122
3150
|
isOpen: false,
|
|
3123
|
-
highlightedIndex: -1
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3151
|
+
highlightedIndex: -1,
|
|
3152
|
+
...(state.highlightedIndex >= 0 && action.selectItem && {
|
|
3153
|
+
selectedItem: props.items[state.highlightedIndex],
|
|
3154
|
+
inputValue: props.itemToString(props.items[state.highlightedIndex])
|
|
3155
|
+
})
|
|
3156
|
+
};
|
|
3128
3157
|
break;
|
|
3129
3158
|
case InputChange:
|
|
3130
3159
|
changes = {
|
|
@@ -3153,14 +3182,14 @@
|
|
|
3153
3182
|
default:
|
|
3154
3183
|
return downshiftCommonReducer(state, action, stateChangeTypes$1);
|
|
3155
3184
|
}
|
|
3156
|
-
return
|
|
3185
|
+
return {
|
|
3186
|
+
...state,
|
|
3187
|
+
...changes
|
|
3188
|
+
};
|
|
3157
3189
|
}
|
|
3158
3190
|
/* eslint-enable complexity */
|
|
3159
3191
|
|
|
3160
|
-
|
|
3161
|
-
_excluded2$1 = ["item", "index", "refKey", "ref", "onMouseMove", "onMouseDown", "onClick", "onPress", "disabled"],
|
|
3162
|
-
_excluded3 = ["onClick", "onPress", "refKey", "ref"],
|
|
3163
|
-
_excluded4 = ["onKeyDown", "onChange", "onInput", "onFocus", "onBlur", "onChangeText", "refKey", "ref"];
|
|
3192
|
+
/* eslint-disable max-statements */
|
|
3164
3193
|
useCombobox.stateChangeTypes = stateChangeTypes$1;
|
|
3165
3194
|
function useCombobox(userProps) {
|
|
3166
3195
|
if (userProps === void 0) {
|
|
@@ -3168,327 +3197,349 @@
|
|
|
3168
3197
|
}
|
|
3169
3198
|
validatePropTypes$1(userProps, useCombobox);
|
|
3170
3199
|
// Props defaults and destructuring.
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3200
|
+
const props = {
|
|
3201
|
+
...defaultProps$1,
|
|
3202
|
+
...userProps
|
|
3203
|
+
};
|
|
3204
|
+
const {
|
|
3205
|
+
initialIsOpen,
|
|
3206
|
+
defaultIsOpen,
|
|
3207
|
+
items,
|
|
3208
|
+
scrollIntoView,
|
|
3209
|
+
environment,
|
|
3210
|
+
getA11yStatusMessage,
|
|
3211
|
+
getA11ySelectionMessage,
|
|
3212
|
+
itemToString
|
|
3213
|
+
} = props;
|
|
3180
3214
|
// Initial state depending on controlled props.
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3215
|
+
const initialState = getInitialState$1(props);
|
|
3216
|
+
const [state, dispatch] = useControlledReducer(downshiftUseComboboxReducer, initialState, props);
|
|
3217
|
+
const {
|
|
3218
|
+
isOpen,
|
|
3219
|
+
highlightedIndex,
|
|
3220
|
+
selectedItem,
|
|
3221
|
+
inputValue
|
|
3222
|
+
} = state;
|
|
3189
3223
|
|
|
3190
3224
|
// Element refs.
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3225
|
+
const menuRef = preact.useRef(null);
|
|
3226
|
+
const itemRefs = preact.useRef({});
|
|
3227
|
+
const inputRef = preact.useRef(null);
|
|
3228
|
+
const toggleButtonRef = preact.useRef(null);
|
|
3229
|
+
const isInitialMountRef = preact.useRef(true);
|
|
3196
3230
|
// prevent id re-generation between renders.
|
|
3197
|
-
|
|
3231
|
+
const elementIds = useElementIds(props);
|
|
3198
3232
|
// used to keep track of how many items we had on previous cycle.
|
|
3199
|
-
|
|
3233
|
+
const previousResultCountRef = preact.useRef();
|
|
3200
3234
|
// utility callback to get item element.
|
|
3201
|
-
|
|
3202
|
-
state
|
|
3203
|
-
props
|
|
3235
|
+
const latest = useLatestRef({
|
|
3236
|
+
state,
|
|
3237
|
+
props
|
|
3204
3238
|
});
|
|
3205
|
-
|
|
3206
|
-
return itemRefs.current[elementIds.getItemId(index)];
|
|
3207
|
-
}, [elementIds]);
|
|
3239
|
+
const getItemNodeFromIndex = preact.useCallback(index => itemRefs.current[elementIds.getItemId(index)], [elementIds]);
|
|
3208
3240
|
|
|
3209
3241
|
// Effects.
|
|
3210
3242
|
// Sets a11y status message on changes in state.
|
|
3211
|
-
useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items],
|
|
3243
|
+
useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items], {
|
|
3212
3244
|
isInitialMount: isInitialMountRef.current,
|
|
3213
3245
|
previousResultCount: previousResultCountRef.current,
|
|
3214
|
-
items
|
|
3215
|
-
environment
|
|
3216
|
-
itemToString
|
|
3217
|
-
|
|
3246
|
+
items,
|
|
3247
|
+
environment,
|
|
3248
|
+
itemToString,
|
|
3249
|
+
...state
|
|
3250
|
+
});
|
|
3218
3251
|
// Sets a11y status message on changes in selectedItem.
|
|
3219
|
-
useA11yMessageSetter(getA11ySelectionMessage, [selectedItem],
|
|
3252
|
+
useA11yMessageSetter(getA11ySelectionMessage, [selectedItem], {
|
|
3220
3253
|
isInitialMount: isInitialMountRef.current,
|
|
3221
3254
|
previousResultCount: previousResultCountRef.current,
|
|
3222
|
-
items
|
|
3223
|
-
environment
|
|
3224
|
-
itemToString
|
|
3225
|
-
|
|
3255
|
+
items,
|
|
3256
|
+
environment,
|
|
3257
|
+
itemToString,
|
|
3258
|
+
...state
|
|
3259
|
+
});
|
|
3226
3260
|
// Scroll on highlighted item if change comes from keyboard.
|
|
3227
|
-
|
|
3261
|
+
const shouldScrollRef = useScrollIntoView({
|
|
3228
3262
|
menuElement: menuRef.current,
|
|
3229
|
-
highlightedIndex
|
|
3230
|
-
isOpen
|
|
3231
|
-
itemRefs
|
|
3232
|
-
scrollIntoView
|
|
3233
|
-
getItemNodeFromIndex
|
|
3263
|
+
highlightedIndex,
|
|
3264
|
+
isOpen,
|
|
3265
|
+
itemRefs,
|
|
3266
|
+
scrollIntoView,
|
|
3267
|
+
getItemNodeFromIndex
|
|
3234
3268
|
});
|
|
3235
3269
|
useControlPropsValidator({
|
|
3236
3270
|
isInitialMount: isInitialMountRef.current,
|
|
3237
|
-
props
|
|
3238
|
-
state
|
|
3271
|
+
props,
|
|
3272
|
+
state
|
|
3239
3273
|
});
|
|
3240
3274
|
// Focus the input on first render if required.
|
|
3241
|
-
preact.useEffect(
|
|
3242
|
-
|
|
3275
|
+
preact.useEffect(() => {
|
|
3276
|
+
const focusOnOpen = initialIsOpen || defaultIsOpen || isOpen;
|
|
3243
3277
|
if (focusOnOpen && inputRef.current) {
|
|
3244
3278
|
inputRef.current.focus();
|
|
3245
3279
|
}
|
|
3246
3280
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
3247
3281
|
}, []);
|
|
3248
|
-
preact.useEffect(
|
|
3282
|
+
preact.useEffect(() => {
|
|
3249
3283
|
if (isInitialMountRef.current) {
|
|
3250
3284
|
return;
|
|
3251
3285
|
}
|
|
3252
3286
|
previousResultCountRef.current = items.length;
|
|
3253
3287
|
});
|
|
3254
3288
|
// Add mouse/touch events to document.
|
|
3255
|
-
|
|
3289
|
+
const mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [inputRef, menuRef, toggleButtonRef], environment, () => {
|
|
3256
3290
|
dispatch({
|
|
3257
3291
|
type: InputBlur,
|
|
3258
3292
|
selectItem: false
|
|
3259
3293
|
});
|
|
3260
3294
|
});
|
|
3261
|
-
|
|
3295
|
+
const setGetterPropCallInfo = useGetterPropsCalledChecker('getInputProps', 'getMenuProps');
|
|
3262
3296
|
// Make initial ref false.
|
|
3263
|
-
preact.useEffect(
|
|
3297
|
+
preact.useEffect(() => {
|
|
3264
3298
|
isInitialMountRef.current = false;
|
|
3265
|
-
return
|
|
3299
|
+
return () => {
|
|
3266
3300
|
isInitialMountRef.current = true;
|
|
3267
3301
|
};
|
|
3268
3302
|
}, []);
|
|
3269
3303
|
// Reset itemRefs on close.
|
|
3270
|
-
preact.useEffect(
|
|
3304
|
+
preact.useEffect(() => {
|
|
3271
3305
|
if (!isOpen) {
|
|
3272
3306
|
itemRefs.current = {};
|
|
3273
3307
|
} else if (document.activeElement !== inputRef.current) {
|
|
3274
|
-
|
|
3275
|
-
inputRef == null ? void 0 : (_inputRef$current = inputRef.current) == null ? void 0 : _inputRef$current.focus();
|
|
3308
|
+
inputRef?.current?.focus();
|
|
3276
3309
|
}
|
|
3277
3310
|
}, [isOpen]);
|
|
3278
3311
|
|
|
3279
3312
|
/* Event handler functions */
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
3289
|
-
|
|
3290
|
-
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3313
|
+
const inputKeyDownHandlers = preact.useMemo(() => ({
|
|
3314
|
+
ArrowDown(event) {
|
|
3315
|
+
event.preventDefault();
|
|
3316
|
+
dispatch({
|
|
3317
|
+
type: InputKeyDownArrowDown,
|
|
3318
|
+
altKey: event.altKey,
|
|
3319
|
+
getItemNodeFromIndex
|
|
3320
|
+
});
|
|
3321
|
+
},
|
|
3322
|
+
ArrowUp(event) {
|
|
3323
|
+
event.preventDefault();
|
|
3324
|
+
dispatch({
|
|
3325
|
+
type: InputKeyDownArrowUp,
|
|
3326
|
+
altKey: event.altKey,
|
|
3327
|
+
getItemNodeFromIndex
|
|
3328
|
+
});
|
|
3329
|
+
},
|
|
3330
|
+
Home(event) {
|
|
3331
|
+
if (!latest.current.state.isOpen) {
|
|
3332
|
+
return;
|
|
3333
|
+
}
|
|
3334
|
+
event.preventDefault();
|
|
3335
|
+
dispatch({
|
|
3336
|
+
type: InputKeyDownHome,
|
|
3337
|
+
getItemNodeFromIndex
|
|
3338
|
+
});
|
|
3339
|
+
},
|
|
3340
|
+
End(event) {
|
|
3341
|
+
if (!latest.current.state.isOpen) {
|
|
3342
|
+
return;
|
|
3343
|
+
}
|
|
3344
|
+
event.preventDefault();
|
|
3345
|
+
dispatch({
|
|
3346
|
+
type: InputKeyDownEnd,
|
|
3347
|
+
getItemNodeFromIndex
|
|
3348
|
+
});
|
|
3349
|
+
},
|
|
3350
|
+
Escape(event) {
|
|
3351
|
+
const latestState = latest.current.state;
|
|
3352
|
+
if (latestState.isOpen || latestState.inputValue || latestState.selectedItem || latestState.highlightedIndex > -1) {
|
|
3302
3353
|
event.preventDefault();
|
|
3303
3354
|
dispatch({
|
|
3304
|
-
type:
|
|
3305
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
3355
|
+
type: InputKeyDownEscape
|
|
3306
3356
|
});
|
|
3307
|
-
}
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3357
|
+
}
|
|
3358
|
+
},
|
|
3359
|
+
Enter(event) {
|
|
3360
|
+
const latestState = latest.current.state;
|
|
3361
|
+
// if closed or no highlighted index, do nothing.
|
|
3362
|
+
if (!latestState.isOpen || event.which === 229 // if IME composing, wait for next Enter keydown event.
|
|
3363
|
+
) {
|
|
3364
|
+
return;
|
|
3365
|
+
}
|
|
3366
|
+
event.preventDefault();
|
|
3367
|
+
dispatch({
|
|
3368
|
+
type: InputKeyDownEnter,
|
|
3369
|
+
getItemNodeFromIndex
|
|
3370
|
+
});
|
|
3371
|
+
},
|
|
3372
|
+
PageUp(event) {
|
|
3373
|
+
if (latest.current.state.isOpen) {
|
|
3312
3374
|
event.preventDefault();
|
|
3313
3375
|
dispatch({
|
|
3314
|
-
type:
|
|
3315
|
-
getItemNodeFromIndex
|
|
3376
|
+
type: InputKeyDownPageUp,
|
|
3377
|
+
getItemNodeFromIndex
|
|
3316
3378
|
});
|
|
3317
|
-
}
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
event.preventDefault();
|
|
3322
|
-
dispatch({
|
|
3323
|
-
type: InputKeyDownEscape
|
|
3324
|
-
});
|
|
3325
|
-
}
|
|
3326
|
-
},
|
|
3327
|
-
Enter: function Enter(event) {
|
|
3328
|
-
var latestState = latest.current.state;
|
|
3329
|
-
// if closed or no highlighted index, do nothing.
|
|
3330
|
-
if (!latestState.isOpen || event.which === 229 // if IME composing, wait for next Enter keydown event.
|
|
3331
|
-
) {
|
|
3332
|
-
return;
|
|
3333
|
-
}
|
|
3379
|
+
}
|
|
3380
|
+
},
|
|
3381
|
+
PageDown(event) {
|
|
3382
|
+
if (latest.current.state.isOpen) {
|
|
3334
3383
|
event.preventDefault();
|
|
3335
3384
|
dispatch({
|
|
3336
|
-
type:
|
|
3337
|
-
getItemNodeFromIndex
|
|
3385
|
+
type: InputKeyDownPageDown,
|
|
3386
|
+
getItemNodeFromIndex
|
|
3338
3387
|
});
|
|
3339
|
-
},
|
|
3340
|
-
PageUp: function PageUp(event) {
|
|
3341
|
-
if (latest.current.state.isOpen) {
|
|
3342
|
-
event.preventDefault();
|
|
3343
|
-
dispatch({
|
|
3344
|
-
type: InputKeyDownPageUp,
|
|
3345
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
3346
|
-
});
|
|
3347
|
-
}
|
|
3348
|
-
},
|
|
3349
|
-
PageDown: function PageDown(event) {
|
|
3350
|
-
if (latest.current.state.isOpen) {
|
|
3351
|
-
event.preventDefault();
|
|
3352
|
-
dispatch({
|
|
3353
|
-
type: InputKeyDownPageDown,
|
|
3354
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
3355
|
-
});
|
|
3356
|
-
}
|
|
3357
3388
|
}
|
|
3358
|
-
}
|
|
3359
|
-
}, [dispatch, latest, getItemNodeFromIndex]);
|
|
3389
|
+
}
|
|
3390
|
+
}), [dispatch, latest, getItemNodeFromIndex]);
|
|
3360
3391
|
|
|
3361
3392
|
// Getter props.
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
_ref2$suppressRefErro = _ref2.suppressRefError,
|
|
3378
|
-
suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
|
|
3393
|
+
const getLabelProps = preact.useCallback(labelProps => ({
|
|
3394
|
+
id: elementIds.labelId,
|
|
3395
|
+
htmlFor: elementIds.inputId,
|
|
3396
|
+
...labelProps
|
|
3397
|
+
}), [elementIds]);
|
|
3398
|
+
const getMenuProps = preact.useCallback(function (_temp, _temp2) {
|
|
3399
|
+
let {
|
|
3400
|
+
onMouseLeave,
|
|
3401
|
+
refKey = 'ref',
|
|
3402
|
+
ref,
|
|
3403
|
+
...rest
|
|
3404
|
+
} = _temp === void 0 ? {} : _temp;
|
|
3405
|
+
let {
|
|
3406
|
+
suppressRefError = false
|
|
3407
|
+
} = _temp2 === void 0 ? {} : _temp2;
|
|
3379
3408
|
setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
|
|
3380
|
-
return
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3409
|
+
return {
|
|
3410
|
+
[refKey]: handleRefs(ref, menuNode => {
|
|
3411
|
+
menuRef.current = menuNode;
|
|
3412
|
+
}),
|
|
3413
|
+
id: elementIds.menuId,
|
|
3414
|
+
role: 'listbox',
|
|
3415
|
+
'aria-labelledby': elementIds.labelId,
|
|
3416
|
+
onMouseLeave: callAllEventHandlers(onMouseLeave, () => {
|
|
3417
|
+
dispatch({
|
|
3418
|
+
type: MenuMouseLeave
|
|
3419
|
+
});
|
|
3420
|
+
}),
|
|
3421
|
+
...rest
|
|
3422
|
+
};
|
|
3387
3423
|
}, [dispatch, setGetterPropCallInfo, elementIds]);
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3424
|
+
const getItemProps = preact.useCallback(function (_temp3) {
|
|
3425
|
+
let {
|
|
3426
|
+
item,
|
|
3427
|
+
index,
|
|
3428
|
+
refKey = 'ref',
|
|
3429
|
+
ref,
|
|
3430
|
+
onMouseMove,
|
|
3431
|
+
onMouseDown,
|
|
3432
|
+
onClick,
|
|
3433
|
+
onPress,
|
|
3434
|
+
disabled,
|
|
3435
|
+
...rest
|
|
3436
|
+
} = _temp3 === void 0 ? {} : _temp3;
|
|
3437
|
+
const {
|
|
3438
|
+
props: latestProps,
|
|
3439
|
+
state: latestState
|
|
3440
|
+
} = latest.current;
|
|
3441
|
+
const itemIndex = getItemIndex(index, item, latestProps.items);
|
|
3406
3442
|
if (itemIndex < 0) {
|
|
3407
3443
|
throw new Error('Pass either item or item index in getItemProps!');
|
|
3408
3444
|
}
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3445
|
+
const onSelectKey = 'onClick';
|
|
3446
|
+
const customClickHandler = onClick;
|
|
3447
|
+
const itemHandleMouseMove = () => {
|
|
3412
3448
|
if (index === latestState.highlightedIndex) {
|
|
3413
3449
|
return;
|
|
3414
3450
|
}
|
|
3415
3451
|
shouldScrollRef.current = false;
|
|
3416
3452
|
dispatch({
|
|
3417
3453
|
type: ItemMouseMove,
|
|
3418
|
-
index
|
|
3419
|
-
disabled
|
|
3454
|
+
index,
|
|
3455
|
+
disabled
|
|
3420
3456
|
});
|
|
3421
3457
|
};
|
|
3422
|
-
|
|
3458
|
+
const itemHandleClick = () => {
|
|
3423
3459
|
dispatch({
|
|
3424
3460
|
type: ItemClick,
|
|
3425
|
-
index
|
|
3461
|
+
index
|
|
3426
3462
|
});
|
|
3427
3463
|
};
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
3431
|
-
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
}
|
|
3435
|
-
|
|
3464
|
+
const itemHandleMouseDown = e => e.preventDefault();
|
|
3465
|
+
return {
|
|
3466
|
+
[refKey]: handleRefs(ref, itemNode => {
|
|
3467
|
+
if (itemNode) {
|
|
3468
|
+
itemRefs.current[elementIds.getItemId(itemIndex)] = itemNode;
|
|
3469
|
+
}
|
|
3470
|
+
}),
|
|
3471
|
+
disabled,
|
|
3472
|
+
role: 'option',
|
|
3473
|
+
'aria-selected': `${itemIndex === latestState.highlightedIndex}`,
|
|
3474
|
+
id: elementIds.getItemId(itemIndex),
|
|
3475
|
+
...(!disabled && {
|
|
3476
|
+
[onSelectKey]: callAllEventHandlers(customClickHandler, itemHandleClick)
|
|
3477
|
+
}),
|
|
3436
3478
|
onMouseMove: callAllEventHandlers(onMouseMove, itemHandleMouseMove),
|
|
3437
|
-
onMouseDown: callAllEventHandlers(onMouseDown, itemHandleMouseDown)
|
|
3438
|
-
|
|
3479
|
+
onMouseDown: callAllEventHandlers(onMouseDown, itemHandleMouseDown),
|
|
3480
|
+
...rest
|
|
3481
|
+
};
|
|
3439
3482
|
}, [dispatch, latest, shouldScrollRef, elementIds]);
|
|
3440
|
-
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
var toggleButtonHandleClick = function toggleButtonHandleClick() {
|
|
3483
|
+
const getToggleButtonProps = preact.useCallback(function (_temp4) {
|
|
3484
|
+
let {
|
|
3485
|
+
onClick,
|
|
3486
|
+
onPress,
|
|
3487
|
+
refKey = 'ref',
|
|
3488
|
+
ref,
|
|
3489
|
+
...rest
|
|
3490
|
+
} = _temp4 === void 0 ? {} : _temp4;
|
|
3491
|
+
const latestState = latest.current.state;
|
|
3492
|
+
const toggleButtonHandleClick = () => {
|
|
3451
3493
|
dispatch({
|
|
3452
3494
|
type: ToggleButtonClick
|
|
3453
3495
|
});
|
|
3454
3496
|
};
|
|
3455
|
-
return
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3497
|
+
return {
|
|
3498
|
+
[refKey]: handleRefs(ref, toggleButtonNode => {
|
|
3499
|
+
toggleButtonRef.current = toggleButtonNode;
|
|
3500
|
+
}),
|
|
3501
|
+
'aria-controls': elementIds.menuId,
|
|
3502
|
+
'aria-expanded': latestState.isOpen,
|
|
3503
|
+
id: elementIds.toggleButtonId,
|
|
3504
|
+
tabIndex: -1,
|
|
3505
|
+
...(!rest.disabled && {
|
|
3506
|
+
...({
|
|
3507
|
+
onClick: callAllEventHandlers(onClick, toggleButtonHandleClick)
|
|
3508
|
+
})
|
|
3509
|
+
}),
|
|
3510
|
+
...rest
|
|
3511
|
+
};
|
|
3460
3512
|
}, [dispatch, latest, elementIds]);
|
|
3461
|
-
|
|
3462
|
-
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
suppressRefError = _ref7$suppressRefErro === void 0 ? false : _ref7$suppressRefErro;
|
|
3513
|
+
const getInputProps = preact.useCallback(function (_temp5, _temp6) {
|
|
3514
|
+
let {
|
|
3515
|
+
onKeyDown,
|
|
3516
|
+
onChange,
|
|
3517
|
+
onInput,
|
|
3518
|
+
onFocus,
|
|
3519
|
+
onBlur,
|
|
3520
|
+
onChangeText,
|
|
3521
|
+
refKey = 'ref',
|
|
3522
|
+
ref,
|
|
3523
|
+
...rest
|
|
3524
|
+
} = _temp5 === void 0 ? {} : _temp5;
|
|
3525
|
+
let {
|
|
3526
|
+
suppressRefError = false
|
|
3527
|
+
} = _temp6 === void 0 ? {} : _temp6;
|
|
3477
3528
|
setGetterPropCallInfo('getInputProps', suppressRefError, refKey, inputRef);
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3529
|
+
const latestState = latest.current.state;
|
|
3530
|
+
const inputHandleKeyDown = event => {
|
|
3531
|
+
const key = normalizeArrowKey(event);
|
|
3481
3532
|
if (key && inputKeyDownHandlers[key]) {
|
|
3482
3533
|
inputKeyDownHandlers[key](event);
|
|
3483
3534
|
}
|
|
3484
3535
|
};
|
|
3485
|
-
|
|
3536
|
+
const inputHandleChange = event => {
|
|
3486
3537
|
dispatch({
|
|
3487
3538
|
type: InputChange,
|
|
3488
3539
|
inputValue: event.target.value
|
|
3489
3540
|
});
|
|
3490
3541
|
};
|
|
3491
|
-
|
|
3542
|
+
const inputHandleBlur = () => {
|
|
3492
3543
|
/* istanbul ignore else */
|
|
3493
3544
|
if (latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
|
|
3494
3545
|
dispatch({
|
|
@@ -3497,7 +3548,7 @@
|
|
|
3497
3548
|
});
|
|
3498
3549
|
}
|
|
3499
3550
|
};
|
|
3500
|
-
|
|
3551
|
+
const inputHandleFocus = () => {
|
|
3501
3552
|
if (!latestState.isOpen) {
|
|
3502
3553
|
dispatch({
|
|
3503
3554
|
type: InputFocus
|
|
@@ -3506,80 +3557,99 @@
|
|
|
3506
3557
|
};
|
|
3507
3558
|
|
|
3508
3559
|
/* istanbul ignore next (preact) */
|
|
3509
|
-
|
|
3510
|
-
|
|
3560
|
+
const onChangeKey = 'onInput' ;
|
|
3561
|
+
let eventHandlers = {};
|
|
3511
3562
|
if (!rest.disabled) {
|
|
3512
|
-
|
|
3513
|
-
|
|
3563
|
+
eventHandlers = {
|
|
3564
|
+
[onChangeKey]: callAllEventHandlers(onChange, onInput, inputHandleChange),
|
|
3565
|
+
onKeyDown: callAllEventHandlers(onKeyDown, inputHandleKeyDown),
|
|
3566
|
+
onBlur: callAllEventHandlers(onBlur, inputHandleBlur),
|
|
3567
|
+
onFocus: callAllEventHandlers(onFocus, inputHandleFocus)
|
|
3568
|
+
};
|
|
3514
3569
|
}
|
|
3515
|
-
return
|
|
3516
|
-
|
|
3517
|
-
|
|
3570
|
+
return {
|
|
3571
|
+
[refKey]: handleRefs(ref, inputNode => {
|
|
3572
|
+
inputRef.current = inputNode;
|
|
3573
|
+
}),
|
|
3574
|
+
'aria-activedescendant': latestState.isOpen && latestState.highlightedIndex > -1 ? elementIds.getItemId(latestState.highlightedIndex) : '',
|
|
3575
|
+
'aria-autocomplete': 'list',
|
|
3576
|
+
'aria-controls': elementIds.menuId,
|
|
3577
|
+
'aria-expanded': latestState.isOpen,
|
|
3578
|
+
'aria-labelledby': elementIds.labelId,
|
|
3579
|
+
// https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
|
|
3580
|
+
// revert back since autocomplete="nope" is ignored on latest Chrome and Opera
|
|
3581
|
+
autoComplete: 'off',
|
|
3582
|
+
id: elementIds.inputId,
|
|
3583
|
+
role: 'combobox',
|
|
3584
|
+
value: latestState.inputValue,
|
|
3585
|
+
...eventHandlers,
|
|
3586
|
+
...rest
|
|
3587
|
+
};
|
|
3518
3588
|
}, [dispatch, inputKeyDownHandlers, latest, mouseAndTouchTrackersRef, setGetterPropCallInfo, elementIds]);
|
|
3519
3589
|
|
|
3520
3590
|
// returns
|
|
3521
|
-
|
|
3591
|
+
const toggleMenu = preact.useCallback(() => {
|
|
3522
3592
|
dispatch({
|
|
3523
3593
|
type: FunctionToggleMenu
|
|
3524
3594
|
});
|
|
3525
3595
|
}, [dispatch]);
|
|
3526
|
-
|
|
3596
|
+
const closeMenu = preact.useCallback(() => {
|
|
3527
3597
|
dispatch({
|
|
3528
3598
|
type: FunctionCloseMenu
|
|
3529
3599
|
});
|
|
3530
3600
|
}, [dispatch]);
|
|
3531
|
-
|
|
3601
|
+
const openMenu = preact.useCallback(() => {
|
|
3532
3602
|
dispatch({
|
|
3533
3603
|
type: FunctionOpenMenu
|
|
3534
3604
|
});
|
|
3535
3605
|
}, [dispatch]);
|
|
3536
|
-
|
|
3606
|
+
const setHighlightedIndex = preact.useCallback(newHighlightedIndex => {
|
|
3537
3607
|
dispatch({
|
|
3538
3608
|
type: FunctionSetHighlightedIndex,
|
|
3539
3609
|
highlightedIndex: newHighlightedIndex
|
|
3540
3610
|
});
|
|
3541
3611
|
}, [dispatch]);
|
|
3542
|
-
|
|
3612
|
+
const selectItem = preact.useCallback(newSelectedItem => {
|
|
3543
3613
|
dispatch({
|
|
3544
3614
|
type: FunctionSelectItem,
|
|
3545
3615
|
selectedItem: newSelectedItem
|
|
3546
3616
|
});
|
|
3547
3617
|
}, [dispatch]);
|
|
3548
|
-
|
|
3618
|
+
const setInputValue = preact.useCallback(newInputValue => {
|
|
3549
3619
|
dispatch({
|
|
3550
3620
|
type: FunctionSetInputValue,
|
|
3551
3621
|
inputValue: newInputValue
|
|
3552
3622
|
});
|
|
3553
3623
|
}, [dispatch]);
|
|
3554
|
-
|
|
3624
|
+
const reset = preact.useCallback(() => {
|
|
3555
3625
|
dispatch({
|
|
3556
3626
|
type: FunctionReset$1
|
|
3557
3627
|
});
|
|
3558
3628
|
}, [dispatch]);
|
|
3559
3629
|
return {
|
|
3560
3630
|
// prop getters.
|
|
3561
|
-
getItemProps
|
|
3562
|
-
getLabelProps
|
|
3563
|
-
getMenuProps
|
|
3564
|
-
getInputProps
|
|
3565
|
-
getToggleButtonProps
|
|
3631
|
+
getItemProps,
|
|
3632
|
+
getLabelProps,
|
|
3633
|
+
getMenuProps,
|
|
3634
|
+
getInputProps,
|
|
3635
|
+
getToggleButtonProps,
|
|
3566
3636
|
// actions.
|
|
3567
|
-
toggleMenu
|
|
3568
|
-
openMenu
|
|
3569
|
-
closeMenu
|
|
3570
|
-
setHighlightedIndex
|
|
3571
|
-
setInputValue
|
|
3572
|
-
selectItem
|
|
3573
|
-
reset
|
|
3637
|
+
toggleMenu,
|
|
3638
|
+
openMenu,
|
|
3639
|
+
closeMenu,
|
|
3640
|
+
setHighlightedIndex,
|
|
3641
|
+
setInputValue,
|
|
3642
|
+
selectItem,
|
|
3643
|
+
reset,
|
|
3574
3644
|
// state.
|
|
3575
|
-
highlightedIndex
|
|
3576
|
-
isOpen
|
|
3577
|
-
selectedItem
|
|
3578
|
-
inputValue
|
|
3645
|
+
highlightedIndex,
|
|
3646
|
+
isOpen,
|
|
3647
|
+
selectedItem,
|
|
3648
|
+
inputValue
|
|
3579
3649
|
};
|
|
3580
3650
|
}
|
|
3581
3651
|
|
|
3582
|
-
|
|
3652
|
+
const defaultStateValues = {
|
|
3583
3653
|
activeIndex: -1,
|
|
3584
3654
|
selectedItems: []
|
|
3585
3655
|
};
|
|
@@ -3617,11 +3687,11 @@
|
|
|
3617
3687
|
* @returns {Object} The initial state.
|
|
3618
3688
|
*/
|
|
3619
3689
|
function getInitialState(props) {
|
|
3620
|
-
|
|
3621
|
-
|
|
3690
|
+
const activeIndex = getInitialValue(props, 'activeIndex');
|
|
3691
|
+
const selectedItems = getInitialValue(props, 'selectedItems');
|
|
3622
3692
|
return {
|
|
3623
|
-
activeIndex
|
|
3624
|
-
selectedItems
|
|
3693
|
+
activeIndex,
|
|
3694
|
+
selectedItems
|
|
3625
3695
|
};
|
|
3626
3696
|
}
|
|
3627
3697
|
|
|
@@ -3638,7 +3708,7 @@
|
|
|
3638
3708
|
if (event.shiftKey || event.metaKey || event.ctrlKey || event.altKey) {
|
|
3639
3709
|
return false;
|
|
3640
3710
|
}
|
|
3641
|
-
|
|
3711
|
+
const element = event.target;
|
|
3642
3712
|
if (element instanceof HTMLInputElement &&
|
|
3643
3713
|
// if element is a text input
|
|
3644
3714
|
element.value !== '' && (
|
|
@@ -3657,11 +3727,13 @@
|
|
|
3657
3727
|
* @returns {string} The a11y message.
|
|
3658
3728
|
*/
|
|
3659
3729
|
function getA11yRemovalMessage(selectionParameters) {
|
|
3660
|
-
|
|
3661
|
-
|
|
3662
|
-
|
|
3730
|
+
const {
|
|
3731
|
+
removedSelectedItem,
|
|
3732
|
+
itemToString: itemToStringLocal
|
|
3733
|
+
} = selectionParameters;
|
|
3734
|
+
return `${itemToStringLocal(removedSelectedItem)} has been removed.`;
|
|
3663
3735
|
}
|
|
3664
|
-
|
|
3736
|
+
const propTypes = {
|
|
3665
3737
|
selectedItems: PropTypes__default["default"].array,
|
|
3666
3738
|
initialSelectedItems: PropTypes__default["default"].array,
|
|
3667
3739
|
defaultSelectedItems: PropTypes__default["default"].array,
|
|
@@ -3685,37 +3757,37 @@
|
|
|
3685
3757
|
})
|
|
3686
3758
|
})
|
|
3687
3759
|
};
|
|
3688
|
-
|
|
3760
|
+
const defaultProps = {
|
|
3689
3761
|
itemToString: defaultProps$3.itemToString,
|
|
3690
3762
|
stateReducer: defaultProps$3.stateReducer,
|
|
3691
3763
|
environment: defaultProps$3.environment,
|
|
3692
|
-
getA11yRemovalMessage
|
|
3764
|
+
getA11yRemovalMessage,
|
|
3693
3765
|
keyNavigationNext: 'ArrowRight',
|
|
3694
3766
|
keyNavigationPrevious: 'ArrowLeft'
|
|
3695
3767
|
};
|
|
3696
3768
|
|
|
3697
3769
|
// eslint-disable-next-line import/no-mutable-exports
|
|
3698
|
-
|
|
3770
|
+
let validatePropTypes = noop;
|
|
3699
3771
|
/* istanbul ignore next */
|
|
3700
3772
|
{
|
|
3701
|
-
validatePropTypes =
|
|
3773
|
+
validatePropTypes = (options, caller) => {
|
|
3702
3774
|
PropTypes__default["default"].checkPropTypes(propTypes, options, 'prop', caller.name);
|
|
3703
3775
|
};
|
|
3704
3776
|
}
|
|
3705
3777
|
|
|
3706
|
-
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
|
|
3710
|
-
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3778
|
+
const SelectedItemClick = '__selected_item_click__' ;
|
|
3779
|
+
const SelectedItemKeyDownDelete = '__selected_item_keydown_delete__' ;
|
|
3780
|
+
const SelectedItemKeyDownBackspace = '__selected_item_keydown_backspace__' ;
|
|
3781
|
+
const SelectedItemKeyDownNavigationNext = '__selected_item_keydown_navigation_next__' ;
|
|
3782
|
+
const SelectedItemKeyDownNavigationPrevious = '__selected_item_keydown_navigation_previous__' ;
|
|
3783
|
+
const DropdownKeyDownNavigationPrevious = '__dropdown_keydown_navigation_previous__' ;
|
|
3784
|
+
const DropdownKeyDownBackspace = '__dropdown_keydown_backspace__' ;
|
|
3785
|
+
const DropdownClick = '__dropdown_click__' ;
|
|
3786
|
+
const FunctionAddSelectedItem = '__function_add_selected_item__' ;
|
|
3787
|
+
const FunctionRemoveSelectedItem = '__function_remove_selected_item__' ;
|
|
3788
|
+
const FunctionSetSelectedItems = '__function_set_selected_items__' ;
|
|
3789
|
+
const FunctionSetActiveIndex = '__function_set_active_index__' ;
|
|
3790
|
+
const FunctionReset = '__function_reset__' ;
|
|
3719
3791
|
|
|
3720
3792
|
var stateChangeTypes = /*#__PURE__*/Object.freeze({
|
|
3721
3793
|
__proto__: null,
|
|
@@ -3736,13 +3808,17 @@
|
|
|
3736
3808
|
|
|
3737
3809
|
/* eslint-disable complexity */
|
|
3738
3810
|
function downshiftMultipleSelectionReducer(state, action) {
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3811
|
+
const {
|
|
3812
|
+
type,
|
|
3813
|
+
index,
|
|
3814
|
+
props,
|
|
3815
|
+
selectedItem
|
|
3816
|
+
} = action;
|
|
3817
|
+
const {
|
|
3818
|
+
activeIndex,
|
|
3819
|
+
selectedItems
|
|
3820
|
+
} = state;
|
|
3821
|
+
let changes;
|
|
3746
3822
|
switch (type) {
|
|
3747
3823
|
case SelectedItemClick:
|
|
3748
3824
|
changes = {
|
|
@@ -3765,17 +3841,18 @@
|
|
|
3765
3841
|
if (activeIndex < 0) {
|
|
3766
3842
|
break;
|
|
3767
3843
|
}
|
|
3768
|
-
|
|
3844
|
+
let newActiveIndex = activeIndex;
|
|
3769
3845
|
if (selectedItems.length === 1) {
|
|
3770
3846
|
newActiveIndex = -1;
|
|
3771
3847
|
} else if (activeIndex === selectedItems.length - 1) {
|
|
3772
3848
|
newActiveIndex = selectedItems.length - 2;
|
|
3773
3849
|
}
|
|
3774
|
-
changes =
|
|
3775
|
-
selectedItems: [
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3850
|
+
changes = {
|
|
3851
|
+
selectedItems: [...selectedItems.slice(0, activeIndex), ...selectedItems.slice(activeIndex + 1)],
|
|
3852
|
+
...{
|
|
3853
|
+
activeIndex: newActiveIndex
|
|
3854
|
+
}
|
|
3855
|
+
};
|
|
3779
3856
|
break;
|
|
3780
3857
|
}
|
|
3781
3858
|
case DropdownKeyDownNavigationPrevious:
|
|
@@ -3790,7 +3867,7 @@
|
|
|
3790
3867
|
break;
|
|
3791
3868
|
case FunctionAddSelectedItem:
|
|
3792
3869
|
changes = {
|
|
3793
|
-
selectedItems: [
|
|
3870
|
+
selectedItems: [...selectedItems, selectedItem]
|
|
3794
3871
|
};
|
|
3795
3872
|
break;
|
|
3796
3873
|
case DropdownClick:
|
|
@@ -3800,25 +3877,27 @@
|
|
|
3800
3877
|
break;
|
|
3801
3878
|
case FunctionRemoveSelectedItem:
|
|
3802
3879
|
{
|
|
3803
|
-
|
|
3804
|
-
|
|
3880
|
+
let newActiveIndex = activeIndex;
|
|
3881
|
+
const selectedItemIndex = selectedItems.indexOf(selectedItem);
|
|
3805
3882
|
if (selectedItemIndex < 0) {
|
|
3806
3883
|
break;
|
|
3807
3884
|
}
|
|
3808
3885
|
if (selectedItems.length === 1) {
|
|
3809
|
-
|
|
3886
|
+
newActiveIndex = -1;
|
|
3810
3887
|
} else if (selectedItemIndex === selectedItems.length - 1) {
|
|
3811
|
-
|
|
3888
|
+
newActiveIndex = selectedItems.length - 2;
|
|
3812
3889
|
}
|
|
3813
3890
|
changes = {
|
|
3814
|
-
selectedItems: [
|
|
3815
|
-
activeIndex:
|
|
3891
|
+
selectedItems: [...selectedItems.slice(0, selectedItemIndex), ...selectedItems.slice(selectedItemIndex + 1)],
|
|
3892
|
+
activeIndex: newActiveIndex
|
|
3816
3893
|
};
|
|
3817
3894
|
break;
|
|
3818
3895
|
}
|
|
3819
3896
|
case FunctionSetSelectedItems:
|
|
3820
3897
|
{
|
|
3821
|
-
|
|
3898
|
+
const {
|
|
3899
|
+
selectedItems: newSelectedItems
|
|
3900
|
+
} = action;
|
|
3822
3901
|
changes = {
|
|
3823
3902
|
selectedItems: newSelectedItems
|
|
3824
3903
|
};
|
|
@@ -3826,9 +3905,11 @@
|
|
|
3826
3905
|
}
|
|
3827
3906
|
case FunctionSetActiveIndex:
|
|
3828
3907
|
{
|
|
3829
|
-
|
|
3908
|
+
const {
|
|
3909
|
+
activeIndex: newActiveIndex
|
|
3910
|
+
} = action;
|
|
3830
3911
|
changes = {
|
|
3831
|
-
activeIndex:
|
|
3912
|
+
activeIndex: newActiveIndex
|
|
3832
3913
|
};
|
|
3833
3914
|
break;
|
|
3834
3915
|
}
|
|
@@ -3841,11 +3922,12 @@
|
|
|
3841
3922
|
default:
|
|
3842
3923
|
throw new Error('Reducer called without proper action type.');
|
|
3843
3924
|
}
|
|
3844
|
-
return
|
|
3925
|
+
return {
|
|
3926
|
+
...state,
|
|
3927
|
+
...changes
|
|
3928
|
+
};
|
|
3845
3929
|
}
|
|
3846
3930
|
|
|
3847
|
-
var _excluded = ["refKey", "ref", "onClick", "onKeyDown", "selectedItem", "index"],
|
|
3848
|
-
_excluded2 = ["refKey", "ref", "onKeyDown", "onClick", "preventKeyAction"];
|
|
3849
3931
|
useMultipleSelection.stateChangeTypes = stateChangeTypes;
|
|
3850
3932
|
function useMultipleSelection(userProps) {
|
|
3851
3933
|
if (userProps === void 0) {
|
|
@@ -3853,46 +3935,49 @@
|
|
|
3853
3935
|
}
|
|
3854
3936
|
validatePropTypes(userProps, useMultipleSelection);
|
|
3855
3937
|
// Props defaults and destructuring.
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
|
|
3938
|
+
const props = {
|
|
3939
|
+
...defaultProps,
|
|
3940
|
+
...userProps
|
|
3941
|
+
};
|
|
3942
|
+
const {
|
|
3943
|
+
getA11yRemovalMessage,
|
|
3944
|
+
itemToString,
|
|
3945
|
+
environment,
|
|
3946
|
+
keyNavigationNext,
|
|
3947
|
+
keyNavigationPrevious
|
|
3948
|
+
} = props;
|
|
3862
3949
|
|
|
3863
3950
|
// Reducer init.
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3951
|
+
const [state, dispatch] = useControlledReducer$1(downshiftMultipleSelectionReducer, getInitialState(props), props);
|
|
3952
|
+
const {
|
|
3953
|
+
activeIndex,
|
|
3954
|
+
selectedItems
|
|
3955
|
+
} = state;
|
|
3869
3956
|
|
|
3870
3957
|
// Refs.
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3958
|
+
const isInitialMountRef = preact.useRef(true);
|
|
3959
|
+
const dropdownRef = preact.useRef(null);
|
|
3960
|
+
const previousSelectedItemsRef = preact.useRef(selectedItems);
|
|
3961
|
+
const selectedItemRefs = preact.useRef();
|
|
3875
3962
|
selectedItemRefs.current = [];
|
|
3876
|
-
|
|
3877
|
-
state
|
|
3878
|
-
props
|
|
3963
|
+
const latest = useLatestRef({
|
|
3964
|
+
state,
|
|
3965
|
+
props
|
|
3879
3966
|
});
|
|
3880
3967
|
|
|
3881
3968
|
// Effects.
|
|
3882
3969
|
/* Sets a11y status message on changes in selectedItem. */
|
|
3883
|
-
preact.useEffect(
|
|
3970
|
+
preact.useEffect(() => {
|
|
3884
3971
|
if (isInitialMountRef.current) {
|
|
3885
3972
|
return;
|
|
3886
3973
|
}
|
|
3887
3974
|
if (selectedItems.length < previousSelectedItemsRef.current.length) {
|
|
3888
|
-
|
|
3889
|
-
return selectedItems.indexOf(item) < 0;
|
|
3890
|
-
});
|
|
3975
|
+
const removedSelectedItem = previousSelectedItemsRef.current.find(item => selectedItems.indexOf(item) < 0);
|
|
3891
3976
|
setStatus(getA11yRemovalMessage({
|
|
3892
|
-
itemToString
|
|
3977
|
+
itemToString,
|
|
3893
3978
|
resultCount: selectedItems.length,
|
|
3894
|
-
removedSelectedItem
|
|
3895
|
-
activeIndex
|
|
3979
|
+
removedSelectedItem,
|
|
3980
|
+
activeIndex,
|
|
3896
3981
|
activeSelectedItem: selectedItems[activeIndex]
|
|
3897
3982
|
}), environment.document);
|
|
3898
3983
|
}
|
|
@@ -3901,7 +3986,7 @@
|
|
|
3901
3986
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
3902
3987
|
}, [selectedItems.length]);
|
|
3903
3988
|
// Sets focus on active item.
|
|
3904
|
-
preact.useEffect(
|
|
3989
|
+
preact.useEffect(() => {
|
|
3905
3990
|
if (isInitialMountRef.current) {
|
|
3906
3991
|
return;
|
|
3907
3992
|
}
|
|
@@ -3913,167 +3998,178 @@
|
|
|
3913
3998
|
}, [activeIndex]);
|
|
3914
3999
|
useControlPropsValidator({
|
|
3915
4000
|
isInitialMount: isInitialMountRef.current,
|
|
3916
|
-
props
|
|
3917
|
-
state
|
|
4001
|
+
props,
|
|
4002
|
+
state
|
|
3918
4003
|
});
|
|
3919
|
-
|
|
4004
|
+
const setGetterPropCallInfo = useGetterPropsCalledChecker('getDropdownProps');
|
|
3920
4005
|
// Make initial ref false.
|
|
3921
|
-
preact.useEffect(
|
|
4006
|
+
preact.useEffect(() => {
|
|
3922
4007
|
isInitialMountRef.current = false;
|
|
3923
|
-
return
|
|
4008
|
+
return () => {
|
|
3924
4009
|
isInitialMountRef.current = true;
|
|
3925
4010
|
};
|
|
3926
4011
|
}, []);
|
|
3927
4012
|
|
|
3928
4013
|
// Event handler functions.
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
return _ref = {}, _ref[keyNavigationPrevious] = function () {
|
|
4014
|
+
const selectedItemKeyDownHandlers = preact.useMemo(() => ({
|
|
4015
|
+
[keyNavigationPrevious]() {
|
|
3932
4016
|
dispatch({
|
|
3933
4017
|
type: SelectedItemKeyDownNavigationPrevious
|
|
3934
4018
|
});
|
|
3935
|
-
},
|
|
4019
|
+
},
|
|
4020
|
+
[keyNavigationNext]() {
|
|
3936
4021
|
dispatch({
|
|
3937
4022
|
type: SelectedItemKeyDownNavigationNext
|
|
3938
4023
|
});
|
|
3939
|
-
},
|
|
4024
|
+
},
|
|
4025
|
+
Delete() {
|
|
3940
4026
|
dispatch({
|
|
3941
4027
|
type: SelectedItemKeyDownDelete
|
|
3942
4028
|
});
|
|
3943
|
-
},
|
|
4029
|
+
},
|
|
4030
|
+
Backspace() {
|
|
3944
4031
|
dispatch({
|
|
3945
4032
|
type: SelectedItemKeyDownBackspace
|
|
3946
4033
|
});
|
|
3947
|
-
}
|
|
3948
|
-
}, [dispatch, keyNavigationNext, keyNavigationPrevious]);
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
return _ref2 = {}, _ref2[keyNavigationPrevious] = function (event) {
|
|
4034
|
+
}
|
|
4035
|
+
}), [dispatch, keyNavigationNext, keyNavigationPrevious]);
|
|
4036
|
+
const dropdownKeyDownHandlers = preact.useMemo(() => ({
|
|
4037
|
+
[keyNavigationPrevious](event) {
|
|
3952
4038
|
if (isKeyDownOperationPermitted(event)) {
|
|
3953
4039
|
dispatch({
|
|
3954
4040
|
type: DropdownKeyDownNavigationPrevious
|
|
3955
4041
|
});
|
|
3956
4042
|
}
|
|
3957
|
-
},
|
|
4043
|
+
},
|
|
4044
|
+
Backspace(event) {
|
|
3958
4045
|
if (isKeyDownOperationPermitted(event)) {
|
|
3959
4046
|
dispatch({
|
|
3960
4047
|
type: DropdownKeyDownBackspace
|
|
3961
4048
|
});
|
|
3962
4049
|
}
|
|
3963
|
-
}
|
|
3964
|
-
}, [dispatch, keyNavigationPrevious]);
|
|
4050
|
+
}
|
|
4051
|
+
}), [dispatch, keyNavigationPrevious]);
|
|
3965
4052
|
|
|
3966
4053
|
// Getter props.
|
|
3967
|
-
|
|
3968
|
-
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
4054
|
+
const getSelectedItemProps = preact.useCallback(function (_temp) {
|
|
4055
|
+
let {
|
|
4056
|
+
refKey = 'ref',
|
|
4057
|
+
ref,
|
|
4058
|
+
onClick,
|
|
4059
|
+
onKeyDown,
|
|
4060
|
+
selectedItem,
|
|
4061
|
+
index,
|
|
4062
|
+
...rest
|
|
4063
|
+
} = _temp === void 0 ? {} : _temp;
|
|
4064
|
+
const {
|
|
4065
|
+
state: latestState
|
|
4066
|
+
} = latest.current;
|
|
4067
|
+
const itemIndex = getItemIndex(index, selectedItem, latestState.selectedItems);
|
|
3980
4068
|
if (itemIndex < 0) {
|
|
3981
4069
|
throw new Error('Pass either selectedItem or index in getSelectedItemProps!');
|
|
3982
4070
|
}
|
|
3983
|
-
|
|
4071
|
+
const selectedItemHandleClick = () => {
|
|
3984
4072
|
dispatch({
|
|
3985
4073
|
type: SelectedItemClick,
|
|
3986
|
-
index
|
|
4074
|
+
index
|
|
3987
4075
|
});
|
|
3988
4076
|
};
|
|
3989
|
-
|
|
3990
|
-
|
|
4077
|
+
const selectedItemHandleKeyDown = event => {
|
|
4078
|
+
const key = normalizeArrowKey(event);
|
|
3991
4079
|
if (key && selectedItemKeyDownHandlers[key]) {
|
|
3992
4080
|
selectedItemKeyDownHandlers[key](event);
|
|
3993
4081
|
}
|
|
3994
4082
|
};
|
|
3995
|
-
return
|
|
3996
|
-
|
|
3997
|
-
|
|
3998
|
-
|
|
3999
|
-
|
|
4083
|
+
return {
|
|
4084
|
+
[refKey]: handleRefs(ref, selectedItemNode => {
|
|
4085
|
+
if (selectedItemNode) {
|
|
4086
|
+
selectedItemRefs.current.push(selectedItemNode);
|
|
4087
|
+
}
|
|
4088
|
+
}),
|
|
4089
|
+
tabIndex: index === latestState.activeIndex ? 0 : -1,
|
|
4090
|
+
onClick: callAllEventHandlers(onClick, selectedItemHandleClick),
|
|
4091
|
+
onKeyDown: callAllEventHandlers(onKeyDown, selectedItemHandleKeyDown),
|
|
4092
|
+
...rest
|
|
4093
|
+
};
|
|
4000
4094
|
}, [dispatch, latest, selectedItemKeyDownHandlers]);
|
|
4001
|
-
|
|
4002
|
-
|
|
4003
|
-
|
|
4004
|
-
|
|
4005
|
-
|
|
4006
|
-
|
|
4007
|
-
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
_ref5$suppressRefErro = _ref5.suppressRefError,
|
|
4014
|
-
suppressRefError = _ref5$suppressRefErro === void 0 ? false : _ref5$suppressRefErro;
|
|
4095
|
+
const getDropdownProps = preact.useCallback(function (_temp2, _temp3) {
|
|
4096
|
+
let {
|
|
4097
|
+
refKey = 'ref',
|
|
4098
|
+
ref,
|
|
4099
|
+
onKeyDown,
|
|
4100
|
+
onClick,
|
|
4101
|
+
preventKeyAction = false,
|
|
4102
|
+
...rest
|
|
4103
|
+
} = _temp2 === void 0 ? {} : _temp2;
|
|
4104
|
+
let {
|
|
4105
|
+
suppressRefError = false
|
|
4106
|
+
} = _temp3 === void 0 ? {} : _temp3;
|
|
4015
4107
|
setGetterPropCallInfo('getDropdownProps', suppressRefError, refKey, dropdownRef);
|
|
4016
|
-
|
|
4017
|
-
|
|
4108
|
+
const dropdownHandleKeyDown = event => {
|
|
4109
|
+
const key = normalizeArrowKey(event);
|
|
4018
4110
|
if (key && dropdownKeyDownHandlers[key]) {
|
|
4019
4111
|
dropdownKeyDownHandlers[key](event);
|
|
4020
4112
|
}
|
|
4021
4113
|
};
|
|
4022
|
-
|
|
4114
|
+
const dropdownHandleClick = () => {
|
|
4023
4115
|
dispatch({
|
|
4024
4116
|
type: DropdownClick
|
|
4025
4117
|
});
|
|
4026
4118
|
};
|
|
4027
|
-
return
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4119
|
+
return {
|
|
4120
|
+
[refKey]: handleRefs(ref, dropdownNode => {
|
|
4121
|
+
if (dropdownNode) {
|
|
4122
|
+
dropdownRef.current = dropdownNode;
|
|
4123
|
+
}
|
|
4124
|
+
}),
|
|
4125
|
+
...(!preventKeyAction && {
|
|
4126
|
+
onKeyDown: callAllEventHandlers(onKeyDown, dropdownHandleKeyDown),
|
|
4127
|
+
onClick: callAllEventHandlers(onClick, dropdownHandleClick)
|
|
4128
|
+
}),
|
|
4129
|
+
...rest
|
|
4130
|
+
};
|
|
4035
4131
|
}, [dispatch, dropdownKeyDownHandlers, setGetterPropCallInfo]);
|
|
4036
4132
|
|
|
4037
4133
|
// returns
|
|
4038
|
-
|
|
4134
|
+
const addSelectedItem = preact.useCallback(selectedItem => {
|
|
4039
4135
|
dispatch({
|
|
4040
4136
|
type: FunctionAddSelectedItem,
|
|
4041
|
-
selectedItem
|
|
4137
|
+
selectedItem
|
|
4042
4138
|
});
|
|
4043
4139
|
}, [dispatch]);
|
|
4044
|
-
|
|
4140
|
+
const removeSelectedItem = preact.useCallback(selectedItem => {
|
|
4045
4141
|
dispatch({
|
|
4046
4142
|
type: FunctionRemoveSelectedItem,
|
|
4047
|
-
selectedItem
|
|
4143
|
+
selectedItem
|
|
4048
4144
|
});
|
|
4049
4145
|
}, [dispatch]);
|
|
4050
|
-
|
|
4146
|
+
const setSelectedItems = preact.useCallback(newSelectedItems => {
|
|
4051
4147
|
dispatch({
|
|
4052
4148
|
type: FunctionSetSelectedItems,
|
|
4053
4149
|
selectedItems: newSelectedItems
|
|
4054
4150
|
});
|
|
4055
4151
|
}, [dispatch]);
|
|
4056
|
-
|
|
4152
|
+
const setActiveIndex = preact.useCallback(newActiveIndex => {
|
|
4057
4153
|
dispatch({
|
|
4058
4154
|
type: FunctionSetActiveIndex,
|
|
4059
4155
|
activeIndex: newActiveIndex
|
|
4060
4156
|
});
|
|
4061
4157
|
}, [dispatch]);
|
|
4062
|
-
|
|
4158
|
+
const reset = preact.useCallback(() => {
|
|
4063
4159
|
dispatch({
|
|
4064
4160
|
type: FunctionReset
|
|
4065
4161
|
});
|
|
4066
4162
|
}, [dispatch]);
|
|
4067
4163
|
return {
|
|
4068
|
-
getSelectedItemProps
|
|
4069
|
-
getDropdownProps
|
|
4070
|
-
addSelectedItem
|
|
4071
|
-
removeSelectedItem
|
|
4072
|
-
setSelectedItems
|
|
4073
|
-
setActiveIndex
|
|
4074
|
-
reset
|
|
4075
|
-
selectedItems
|
|
4076
|
-
activeIndex
|
|
4164
|
+
getSelectedItemProps,
|
|
4165
|
+
getDropdownProps,
|
|
4166
|
+
addSelectedItem,
|
|
4167
|
+
removeSelectedItem,
|
|
4168
|
+
setSelectedItems,
|
|
4169
|
+
setActiveIndex,
|
|
4170
|
+
reset,
|
|
4171
|
+
selectedItems,
|
|
4172
|
+
activeIndex
|
|
4077
4173
|
};
|
|
4078
4174
|
}
|
|
4079
4175
|
|