1 | import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/esm/objectWithoutPropertiesLoose';
|
2 | import _extends from '@babel/runtime/helpers/esm/extends';
|
3 | import _assertThisInitialized from '@babel/runtime/helpers/esm/assertThisInitialized';
|
4 | import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose';
|
5 | import PropTypes from 'prop-types';
|
6 | import { cloneElement, Component, useRef, useEffect, useCallback, useLayoutEffect, useReducer, useMemo } from 'react';
|
7 | import { isForwardRef } from 'react-is';
|
8 | import computeScrollIntoView from 'compute-scroll-into-view';
|
9 | import { __assign } from 'tslib';
|
10 |
|
11 | var idCounter = 0;
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function cbToCb(cb) {
|
22 | return typeof cb === 'function' ? cb : noop;
|
23 | }
|
24 |
|
25 | function noop() {}
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function scrollIntoView(node, menuNode) {
|
34 | if (!node) {
|
35 | return;
|
36 | }
|
37 |
|
38 | var actions = computeScrollIntoView(node, {
|
39 | boundary: menuNode,
|
40 | block: 'nearest',
|
41 | scrollMode: 'if-needed'
|
42 | });
|
43 | actions.forEach(function (_ref) {
|
44 | var el = _ref.el,
|
45 | top = _ref.top,
|
46 | left = _ref.left;
|
47 | el.scrollTop = top;
|
48 | el.scrollLeft = left;
|
49 | });
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function isOrContainsNode(parent, child, environment) {
|
60 | var result = parent === child || child instanceof environment.Node && parent.contains && parent.contains(child);
|
61 | return result;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function debounce(fn, time) {
|
74 | var timeoutId;
|
75 |
|
76 | function cancel() {
|
77 | if (timeoutId) {
|
78 | clearTimeout(timeoutId);
|
79 | }
|
80 | }
|
81 |
|
82 | function wrapper() {
|
83 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
84 | args[_key] = arguments[_key];
|
85 | }
|
86 |
|
87 | cancel();
|
88 | timeoutId = setTimeout(function () {
|
89 | timeoutId = null;
|
90 | fn.apply(void 0, args);
|
91 | }, time);
|
92 | }
|
93 |
|
94 | wrapper.cancel = cancel;
|
95 | return wrapper;
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | function callAllEventHandlers() {
|
107 | for (var _len2 = arguments.length, fns = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
108 | fns[_key2] = arguments[_key2];
|
109 | }
|
110 |
|
111 | return function (event) {
|
112 | for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
113 | args[_key3 - 1] = arguments[_key3];
|
114 | }
|
115 |
|
116 | return fns.some(function (fn) {
|
117 | if (fn) {
|
118 | fn.apply(void 0, [event].concat(args));
|
119 | }
|
120 |
|
121 | return event.preventDownshiftDefault || event.hasOwnProperty('nativeEvent') && event.nativeEvent.preventDownshiftDefault;
|
122 | });
|
123 | };
|
124 | }
|
125 |
|
126 | function handleRefs() {
|
127 | for (var _len4 = arguments.length, refs = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
128 | refs[_key4] = arguments[_key4];
|
129 | }
|
130 |
|
131 | return function (node) {
|
132 | refs.forEach(function (ref) {
|
133 | if (typeof ref === 'function') {
|
134 | ref(node);
|
135 | } else if (ref) {
|
136 | ref.current = node;
|
137 | }
|
138 | });
|
139 | };
|
140 | }
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | function generateId() {
|
148 | return String(idCounter++);
|
149 | }
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | function resetIdCounter() {
|
156 | idCounter = 0;
|
157 | }
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | function getA11yStatusMessage$1(_ref2) {
|
169 | var isOpen = _ref2.isOpen,
|
170 | resultCount = _ref2.resultCount,
|
171 | previousResultCount = _ref2.previousResultCount;
|
172 |
|
173 | if (!isOpen) {
|
174 | return '';
|
175 | }
|
176 |
|
177 | if (!resultCount) {
|
178 | return 'No results are available.';
|
179 | }
|
180 |
|
181 | if (resultCount !== previousResultCount) {
|
182 | return resultCount + " result" + (resultCount === 1 ? ' is' : 's are') + " available, use up and down arrow keys to navigate. Press Enter key to select.";
|
183 | }
|
184 |
|
185 | return '';
|
186 | }
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | function unwrapArray(arg, defaultValue) {
|
197 | arg = Array.isArray(arg) ?
|
198 |
|
199 | arg[0] : arg;
|
200 |
|
201 | if (!arg && defaultValue) {
|
202 | return defaultValue;
|
203 | } else {
|
204 | return arg;
|
205 | }
|
206 | }
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | function isDOMElement(element) {
|
214 |
|
215 |
|
216 | return typeof element.type === 'string';
|
217 | }
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | function getElementProps(element) {
|
225 |
|
226 | return element.props;
|
227 | }
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | function requiredProp(fnName, propName) {
|
237 |
|
238 | console.error("The property \"" + propName + "\" is required in \"" + fnName + "\"");
|
239 | }
|
240 |
|
241 | var stateKeys = ['highlightedIndex', 'inputValue', 'isOpen', 'selectedItem', 'type'];
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | function pickState(state) {
|
248 | if (state === void 0) {
|
249 | state = {};
|
250 | }
|
251 |
|
252 | var result = {};
|
253 | stateKeys.forEach(function (k) {
|
254 | if (state.hasOwnProperty(k)) {
|
255 | result[k] = state[k];
|
256 | }
|
257 | });
|
258 | return result;
|
259 | }
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 | function getState(state, props) {
|
274 | return Object.keys(state).reduce(function (prevState, key) {
|
275 | prevState[key] = isControlledProp(props, key) ? props[key] : state[key];
|
276 | return prevState;
|
277 | }, {});
|
278 | }
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | function isControlledProp(props, key) {
|
291 | return props[key] !== undefined;
|
292 | }
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | function normalizeArrowKey(event) {
|
301 | var key = event.key,
|
302 | keyCode = event.keyCode;
|
303 |
|
304 |
|
305 | if (keyCode >= 37 && keyCode <= 40 && key.indexOf('Arrow') !== 0) {
|
306 | return "Arrow" + key;
|
307 | }
|
308 |
|
309 | return key;
|
310 | }
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | function isPlainObject(obj) {
|
319 | return Object.prototype.toString.call(obj) === '[object Object]';
|
320 | }
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 | function getNextWrappingIndex(moveAmount, baseIndex, itemCount, getItemNodeFromIndex, circular) {
|
335 | if (circular === void 0) {
|
336 | circular = true;
|
337 | }
|
338 |
|
339 | if (itemCount === 0) {
|
340 | return -1;
|
341 | }
|
342 |
|
343 | var itemsLastIndex = itemCount - 1;
|
344 |
|
345 | if (typeof baseIndex !== 'number' || baseIndex < 0 || baseIndex >= itemCount) {
|
346 | baseIndex = moveAmount > 0 ? -1 : itemsLastIndex + 1;
|
347 | }
|
348 |
|
349 | var newIndex = baseIndex + moveAmount;
|
350 |
|
351 | if (newIndex < 0) {
|
352 | newIndex = circular ? itemsLastIndex : 0;
|
353 | } else if (newIndex > itemsLastIndex) {
|
354 | newIndex = circular ? 0 : itemsLastIndex;
|
355 | }
|
356 |
|
357 | var nonDisabledNewIndex = getNextNonDisabledIndex(moveAmount, newIndex, itemCount, getItemNodeFromIndex, circular);
|
358 |
|
359 | if (nonDisabledNewIndex === -1) {
|
360 | return baseIndex >= itemCount ? -1 : baseIndex;
|
361 | }
|
362 |
|
363 | return nonDisabledNewIndex;
|
364 | }
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 | function getNextNonDisabledIndex(moveAmount, baseIndex, itemCount, getItemNodeFromIndex, circular) {
|
378 | var currentElementNode = getItemNodeFromIndex(baseIndex);
|
379 |
|
380 | if (!currentElementNode || !currentElementNode.hasAttribute('disabled')) {
|
381 | return baseIndex;
|
382 | }
|
383 |
|
384 | if (moveAmount > 0) {
|
385 | for (var index = baseIndex + 1; index < itemCount; index++) {
|
386 | if (!getItemNodeFromIndex(index).hasAttribute('disabled')) {
|
387 | return index;
|
388 | }
|
389 | }
|
390 | } else {
|
391 | for (var _index = baseIndex - 1; _index >= 0; _index--) {
|
392 | if (!getItemNodeFromIndex(_index).hasAttribute('disabled')) {
|
393 | return _index;
|
394 | }
|
395 | }
|
396 | }
|
397 |
|
398 | if (circular) {
|
399 | return moveAmount > 0 ? getNextNonDisabledIndex(1, 0, itemCount, getItemNodeFromIndex, false) : getNextNonDisabledIndex(-1, itemCount - 1, itemCount, getItemNodeFromIndex, false);
|
400 | }
|
401 |
|
402 | return -1;
|
403 | }
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 | function targetWithinDownshift(target, downshiftElements, environment, checkActiveElement) {
|
417 | if (checkActiveElement === void 0) {
|
418 | checkActiveElement = true;
|
419 | }
|
420 |
|
421 | return downshiftElements.some(function (contextNode) {
|
422 | return contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment));
|
423 | });
|
424 | }
|
425 |
|
426 |
|
427 | var validateControlledUnchanged = noop;
|
428 |
|
429 |
|
430 | if (process.env.NODE_ENV !== 'production') {
|
431 | validateControlledUnchanged = function validateControlledUnchanged(state, prevProps, nextProps) {
|
432 | var 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";
|
433 | Object.keys(state).forEach(function (propKey) {
|
434 | if (prevProps[propKey] !== undefined && nextProps[propKey] === undefined) {
|
435 |
|
436 | console.error("downshift: A component has changed the controlled prop \"" + propKey + "\" to be uncontrolled. " + warningDescription);
|
437 | } else if (prevProps[propKey] === undefined && nextProps[propKey] !== undefined) {
|
438 |
|
439 | console.error("downshift: A component has changed the uncontrolled prop \"" + propKey + "\" to be controlled. " + warningDescription);
|
440 | }
|
441 | });
|
442 | };
|
443 | }
|
444 |
|
445 | var cleanupStatus = debounce(function (documentProp) {
|
446 | getStatusDiv(documentProp).textContent = '';
|
447 | }, 500);
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 | function setStatus(status, documentProp) {
|
454 | var div = getStatusDiv(documentProp);
|
455 |
|
456 | if (!status) {
|
457 | return;
|
458 | }
|
459 |
|
460 | div.textContent = status;
|
461 | cleanupStatus(documentProp);
|
462 | }
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 | function getStatusDiv(documentProp) {
|
471 | if (documentProp === void 0) {
|
472 | documentProp = document;
|
473 | }
|
474 |
|
475 | var statusDiv = documentProp.getElementById('a11y-status-message');
|
476 |
|
477 | if (statusDiv) {
|
478 | return statusDiv;
|
479 | }
|
480 |
|
481 | statusDiv = documentProp.createElement('div');
|
482 | statusDiv.setAttribute('id', 'a11y-status-message');
|
483 | statusDiv.setAttribute('role', 'status');
|
484 | statusDiv.setAttribute('aria-live', 'polite');
|
485 | statusDiv.setAttribute('aria-relevant', 'additions text');
|
486 | Object.assign(statusDiv.style, {
|
487 | border: '0',
|
488 | clip: 'rect(0 0 0 0)',
|
489 | height: '1px',
|
490 | margin: '-1px',
|
491 | overflow: 'hidden',
|
492 | padding: '0',
|
493 | position: 'absolute',
|
494 | width: '1px'
|
495 | });
|
496 | documentProp.body.appendChild(statusDiv);
|
497 | return statusDiv;
|
498 | }
|
499 |
|
500 | var unknown = process.env.NODE_ENV !== "production" ? '__autocomplete_unknown__' : 0;
|
501 | var mouseUp = process.env.NODE_ENV !== "production" ? '__autocomplete_mouseup__' : 1;
|
502 | var itemMouseEnter = process.env.NODE_ENV !== "production" ? '__autocomplete_item_mouseenter__' : 2;
|
503 | var keyDownArrowUp = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_arrow_up__' : 3;
|
504 | var keyDownArrowDown = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_arrow_down__' : 4;
|
505 | var keyDownEscape = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_escape__' : 5;
|
506 | var keyDownEnter = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_enter__' : 6;
|
507 | var keyDownHome = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_home__' : 7;
|
508 | var keyDownEnd = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_end__' : 8;
|
509 | var clickItem = process.env.NODE_ENV !== "production" ? '__autocomplete_click_item__' : 9;
|
510 | var blurInput = process.env.NODE_ENV !== "production" ? '__autocomplete_blur_input__' : 10;
|
511 | var changeInput = process.env.NODE_ENV !== "production" ? '__autocomplete_change_input__' : 11;
|
512 | var keyDownSpaceButton = process.env.NODE_ENV !== "production" ? '__autocomplete_keydown_space_button__' : 12;
|
513 | var clickButton = process.env.NODE_ENV !== "production" ? '__autocomplete_click_button__' : 13;
|
514 | var blurButton = process.env.NODE_ENV !== "production" ? '__autocomplete_blur_button__' : 14;
|
515 | var controlledPropUpdatedSelectedItem = process.env.NODE_ENV !== "production" ? '__autocomplete_controlled_prop_updated_selected_item__' : 15;
|
516 | var touchEnd = process.env.NODE_ENV !== "production" ? '__autocomplete_touchend__' : 16;
|
517 |
|
518 | var stateChangeTypes$3 = Object.freeze({
|
519 | __proto__: null,
|
520 | unknown: unknown,
|
521 | mouseUp: mouseUp,
|
522 | itemMouseEnter: itemMouseEnter,
|
523 | keyDownArrowUp: keyDownArrowUp,
|
524 | keyDownArrowDown: keyDownArrowDown,
|
525 | keyDownEscape: keyDownEscape,
|
526 | keyDownEnter: keyDownEnter,
|
527 | keyDownHome: keyDownHome,
|
528 | keyDownEnd: keyDownEnd,
|
529 | clickItem: clickItem,
|
530 | blurInput: blurInput,
|
531 | changeInput: changeInput,
|
532 | keyDownSpaceButton: keyDownSpaceButton,
|
533 | clickButton: clickButton,
|
534 | blurButton: blurButton,
|
535 | controlledPropUpdatedSelectedItem: controlledPropUpdatedSelectedItem,
|
536 | touchEnd: touchEnd
|
537 | });
|
538 |
|
539 | var _excluded$4 = ["refKey", "ref"],
|
540 | _excluded2$3 = ["onClick", "onPress", "onKeyDown", "onKeyUp", "onBlur"],
|
541 | _excluded3$2 = ["onKeyDown", "onBlur", "onChange", "onInput", "onChangeText"],
|
542 | _excluded4$1 = ["refKey", "ref"],
|
543 | _excluded5$1 = ["onMouseMove", "onMouseDown", "onClick", "onPress", "index", "item"];
|
544 |
|
545 | var Downshift = function () {
|
546 | var Downshift = function (_Component) {
|
547 | _inheritsLoose(Downshift, _Component);
|
548 |
|
549 | function Downshift(_props) {
|
550 | var _this;
|
551 |
|
552 | _this = _Component.call(this, _props) || this;
|
553 |
|
554 |
|
555 |
|
556 | _this.id = _this.props.id || "downshift-" + generateId();
|
557 | _this.menuId = _this.props.menuId || _this.id + "-menu";
|
558 | _this.labelId = _this.props.labelId || _this.id + "-label";
|
559 | _this.inputId = _this.props.inputId || _this.id + "-input";
|
560 |
|
561 | _this.getItemId = _this.props.getItemId || function (index) {
|
562 | return _this.id + "-item-" + index;
|
563 | };
|
564 |
|
565 | _this.input = null;
|
566 | _this.items = [];
|
567 | _this.itemCount = null;
|
568 | _this.previousResultCount = 0;
|
569 | _this.timeoutIds = [];
|
570 |
|
571 | _this.internalSetTimeout = function (fn, time) {
|
572 | var id = setTimeout(function () {
|
573 | _this.timeoutIds = _this.timeoutIds.filter(function (i) {
|
574 | return i !== id;
|
575 | });
|
576 | fn();
|
577 | }, time);
|
578 |
|
579 | _this.timeoutIds.push(id);
|
580 | };
|
581 |
|
582 | _this.setItemCount = function (count) {
|
583 | _this.itemCount = count;
|
584 | };
|
585 |
|
586 | _this.unsetItemCount = function () {
|
587 | _this.itemCount = null;
|
588 | };
|
589 |
|
590 | _this.setHighlightedIndex = function (highlightedIndex, otherStateToSet) {
|
591 | if (highlightedIndex === void 0) {
|
592 | highlightedIndex = _this.props.defaultHighlightedIndex;
|
593 | }
|
594 |
|
595 | if (otherStateToSet === void 0) {
|
596 | otherStateToSet = {};
|
597 | }
|
598 |
|
599 | otherStateToSet = pickState(otherStateToSet);
|
600 |
|
601 | _this.internalSetState(_extends({
|
602 | highlightedIndex: highlightedIndex
|
603 | }, otherStateToSet));
|
604 | };
|
605 |
|
606 | _this.clearSelection = function (cb) {
|
607 | _this.internalSetState({
|
608 | selectedItem: null,
|
609 | inputValue: '',
|
610 | highlightedIndex: _this.props.defaultHighlightedIndex,
|
611 | isOpen: _this.props.defaultIsOpen
|
612 | }, cb);
|
613 | };
|
614 |
|
615 | _this.selectItem = function (item, otherStateToSet, cb) {
|
616 | otherStateToSet = pickState(otherStateToSet);
|
617 |
|
618 | _this.internalSetState(_extends({
|
619 | isOpen: _this.props.defaultIsOpen,
|
620 | highlightedIndex: _this.props.defaultHighlightedIndex,
|
621 | selectedItem: item,
|
622 | inputValue: _this.props.itemToString(item)
|
623 | }, otherStateToSet), cb);
|
624 | };
|
625 |
|
626 | _this.selectItemAtIndex = function (itemIndex, otherStateToSet, cb) {
|
627 | var item = _this.items[itemIndex];
|
628 |
|
629 | if (item == null) {
|
630 | return;
|
631 | }
|
632 |
|
633 | _this.selectItem(item, otherStateToSet, cb);
|
634 | };
|
635 |
|
636 | _this.selectHighlightedItem = function (otherStateToSet, cb) {
|
637 | return _this.selectItemAtIndex(_this.getState().highlightedIndex, otherStateToSet, cb);
|
638 | };
|
639 |
|
640 | _this.internalSetState = function (stateToSet, cb) {
|
641 | var isItemSelected, onChangeArg;
|
642 | var onStateChangeArg = {};
|
643 | var isStateToSetFunction = typeof stateToSet === 'function';
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 | if (!isStateToSetFunction && stateToSet.hasOwnProperty('inputValue')) {
|
650 | _this.props.onInputValueChange(stateToSet.inputValue, _extends({}, _this.getStateAndHelpers(), stateToSet));
|
651 | }
|
652 |
|
653 | return _this.setState(function (state) {
|
654 | state = _this.getState(state);
|
655 | var newStateToSet = isStateToSetFunction ? stateToSet(state) : stateToSet;
|
656 |
|
657 | newStateToSet = _this.props.stateReducer(state, newStateToSet);
|
658 |
|
659 |
|
660 |
|
661 | isItemSelected = newStateToSet.hasOwnProperty('selectedItem');
|
662 |
|
663 | var nextState = {};
|
664 |
|
665 | var nextFullState = {};
|
666 |
|
667 |
|
668 |
|
669 | if (isItemSelected && newStateToSet.selectedItem !== state.selectedItem) {
|
670 | onChangeArg = newStateToSet.selectedItem;
|
671 | }
|
672 |
|
673 | newStateToSet.type = newStateToSet.type || unknown;
|
674 | Object.keys(newStateToSet).forEach(function (key) {
|
675 |
|
676 |
|
677 | if (state[key] !== newStateToSet[key]) {
|
678 | onStateChangeArg[key] = newStateToSet[key];
|
679 | }
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 | if (key === 'type') {
|
688 | return;
|
689 | }
|
690 |
|
691 | nextFullState[key] = newStateToSet[key];
|
692 |
|
693 | if (!isControlledProp(_this.props, key)) {
|
694 | nextState[key] = newStateToSet[key];
|
695 | }
|
696 | });
|
697 |
|
698 |
|
699 | if (isStateToSetFunction && newStateToSet.hasOwnProperty('inputValue')) {
|
700 | _this.props.onInputValueChange(newStateToSet.inputValue, _extends({}, _this.getStateAndHelpers(), newStateToSet));
|
701 | }
|
702 |
|
703 | return nextState;
|
704 | }, function () {
|
705 |
|
706 | cbToCb(cb)();
|
707 |
|
708 |
|
709 | var hasMoreStateThanType = Object.keys(onStateChangeArg).length > 1;
|
710 |
|
711 | if (hasMoreStateThanType) {
|
712 | _this.props.onStateChange(onStateChangeArg, _this.getStateAndHelpers());
|
713 | }
|
714 |
|
715 | if (isItemSelected) {
|
716 | _this.props.onSelect(stateToSet.selectedItem, _this.getStateAndHelpers());
|
717 | }
|
718 |
|
719 | if (onChangeArg !== undefined) {
|
720 | _this.props.onChange(onChangeArg, _this.getStateAndHelpers());
|
721 | }
|
722 |
|
723 |
|
724 |
|
725 | _this.props.onUserAction(onStateChangeArg, _this.getStateAndHelpers());
|
726 | });
|
727 | };
|
728 |
|
729 | _this.rootRef = function (node) {
|
730 | return _this._rootNode = node;
|
731 | };
|
732 |
|
733 | _this.getRootProps = function (_temp, _temp2) {
|
734 | var _extends2;
|
735 |
|
736 | var _ref = _temp === void 0 ? {} : _temp,
|
737 | _ref$refKey = _ref.refKey,
|
738 | refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
|
739 | ref = _ref.ref,
|
740 | rest = _objectWithoutPropertiesLoose(_ref, _excluded$4);
|
741 |
|
742 | var _ref2 = _temp2 === void 0 ? {} : _temp2,
|
743 | _ref2$suppressRefErro = _ref2.suppressRefError,
|
744 | suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
|
745 |
|
746 |
|
747 |
|
748 | _this.getRootProps.called = true;
|
749 | _this.getRootProps.refKey = refKey;
|
750 | _this.getRootProps.suppressRefError = suppressRefError;
|
751 |
|
752 | var _this$getState = _this.getState(),
|
753 | isOpen = _this$getState.isOpen;
|
754 |
|
755 | return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, _this.rootRef), _extends2.role = 'combobox', _extends2['aria-expanded'] = isOpen, _extends2['aria-haspopup'] = 'listbox', _extends2['aria-owns'] = isOpen ? _this.menuId : null, _extends2['aria-labelledby'] = _this.labelId, _extends2), rest);
|
756 | };
|
757 |
|
758 | _this.keyDownHandlers = {
|
759 | ArrowDown: function ArrowDown(event) {
|
760 | var _this2 = this;
|
761 |
|
762 | event.preventDefault();
|
763 |
|
764 | if (this.getState().isOpen) {
|
765 | var amount = event.shiftKey ? 5 : 1;
|
766 | this.moveHighlightedIndex(amount, {
|
767 | type: keyDownArrowDown
|
768 | });
|
769 | } else {
|
770 | this.internalSetState({
|
771 | isOpen: true,
|
772 | type: keyDownArrowDown
|
773 | }, function () {
|
774 | var itemCount = _this2.getItemCount();
|
775 |
|
776 | if (itemCount > 0) {
|
777 | var _this2$getState = _this2.getState(),
|
778 | highlightedIndex = _this2$getState.highlightedIndex;
|
779 |
|
780 | var nextHighlightedIndex = getNextWrappingIndex(1, highlightedIndex, itemCount, function (index) {
|
781 | return _this2.getItemNodeFromIndex(index);
|
782 | });
|
783 |
|
784 | _this2.setHighlightedIndex(nextHighlightedIndex, {
|
785 | type: keyDownArrowDown
|
786 | });
|
787 | }
|
788 | });
|
789 | }
|
790 | },
|
791 | ArrowUp: function ArrowUp(event) {
|
792 | var _this3 = this;
|
793 |
|
794 | event.preventDefault();
|
795 |
|
796 | if (this.getState().isOpen) {
|
797 | var amount = event.shiftKey ? -5 : -1;
|
798 | this.moveHighlightedIndex(amount, {
|
799 | type: keyDownArrowUp
|
800 | });
|
801 | } else {
|
802 | this.internalSetState({
|
803 | isOpen: true,
|
804 | type: keyDownArrowUp
|
805 | }, function () {
|
806 | var itemCount = _this3.getItemCount();
|
807 |
|
808 | if (itemCount > 0) {
|
809 | var _this3$getState = _this3.getState(),
|
810 | highlightedIndex = _this3$getState.highlightedIndex;
|
811 |
|
812 | var nextHighlightedIndex = getNextWrappingIndex(-1, highlightedIndex, itemCount, function (index) {
|
813 | return _this3.getItemNodeFromIndex(index);
|
814 | });
|
815 |
|
816 | _this3.setHighlightedIndex(nextHighlightedIndex, {
|
817 | type: keyDownArrowUp
|
818 | });
|
819 | }
|
820 | });
|
821 | }
|
822 | },
|
823 | Enter: function Enter(event) {
|
824 | if (event.which === 229) {
|
825 | return;
|
826 | }
|
827 |
|
828 | var _this$getState2 = this.getState(),
|
829 | isOpen = _this$getState2.isOpen,
|
830 | highlightedIndex = _this$getState2.highlightedIndex;
|
831 |
|
832 | if (isOpen && highlightedIndex != null) {
|
833 | event.preventDefault();
|
834 | var item = this.items[highlightedIndex];
|
835 | var itemNode = this.getItemNodeFromIndex(highlightedIndex);
|
836 |
|
837 | if (item == null || itemNode && itemNode.hasAttribute('disabled')) {
|
838 | return;
|
839 | }
|
840 |
|
841 | this.selectHighlightedItem({
|
842 | type: keyDownEnter
|
843 | });
|
844 | }
|
845 | },
|
846 | Escape: function Escape(event) {
|
847 | event.preventDefault();
|
848 | this.reset(_extends({
|
849 | type: keyDownEscape
|
850 | }, !this.state.isOpen && {
|
851 | selectedItem: null,
|
852 | inputValue: ''
|
853 | }));
|
854 | }
|
855 | };
|
856 | _this.buttonKeyDownHandlers = _extends({}, _this.keyDownHandlers, {
|
857 | ' ': function _(event) {
|
858 | event.preventDefault();
|
859 | this.toggleMenu({
|
860 | type: keyDownSpaceButton
|
861 | });
|
862 | }
|
863 | });
|
864 | _this.inputKeyDownHandlers = _extends({}, _this.keyDownHandlers, {
|
865 | Home: function Home(event) {
|
866 | var _this4 = this;
|
867 |
|
868 | var _this$getState3 = this.getState(),
|
869 | isOpen = _this$getState3.isOpen;
|
870 |
|
871 | if (!isOpen) {
|
872 | return;
|
873 | }
|
874 |
|
875 | event.preventDefault();
|
876 | var itemCount = this.getItemCount();
|
877 |
|
878 | if (itemCount <= 0 || !isOpen) {
|
879 | return;
|
880 | }
|
881 |
|
882 |
|
883 | var newHighlightedIndex = getNextNonDisabledIndex(1, 0, itemCount, function (index) {
|
884 | return _this4.getItemNodeFromIndex(index);
|
885 | }, false);
|
886 | this.setHighlightedIndex(newHighlightedIndex, {
|
887 | type: keyDownHome
|
888 | });
|
889 | },
|
890 | End: function End(event) {
|
891 | var _this5 = this;
|
892 |
|
893 | var _this$getState4 = this.getState(),
|
894 | isOpen = _this$getState4.isOpen;
|
895 |
|
896 | if (!isOpen) {
|
897 | return;
|
898 | }
|
899 |
|
900 | event.preventDefault();
|
901 | var itemCount = this.getItemCount();
|
902 |
|
903 | if (itemCount <= 0 || !isOpen) {
|
904 | return;
|
905 | }
|
906 |
|
907 |
|
908 | var newHighlightedIndex = getNextNonDisabledIndex(-1, itemCount - 1, itemCount, function (index) {
|
909 | return _this5.getItemNodeFromIndex(index);
|
910 | }, false);
|
911 | this.setHighlightedIndex(newHighlightedIndex, {
|
912 | type: keyDownEnd
|
913 | });
|
914 | }
|
915 | });
|
916 |
|
917 | _this.getToggleButtonProps = function (_temp3) {
|
918 | var _ref3 = _temp3 === void 0 ? {} : _temp3,
|
919 | onClick = _ref3.onClick;
|
920 | _ref3.onPress;
|
921 | var onKeyDown = _ref3.onKeyDown,
|
922 | onKeyUp = _ref3.onKeyUp,
|
923 | onBlur = _ref3.onBlur,
|
924 | rest = _objectWithoutPropertiesLoose(_ref3, _excluded2$3);
|
925 |
|
926 | var _this$getState5 = _this.getState(),
|
927 | isOpen = _this$getState5.isOpen;
|
928 |
|
929 | var enabledEventHandlers = {
|
930 | onClick: callAllEventHandlers(onClick, _this.buttonHandleClick),
|
931 | onKeyDown: callAllEventHandlers(onKeyDown, _this.buttonHandleKeyDown),
|
932 | onKeyUp: callAllEventHandlers(onKeyUp, _this.buttonHandleKeyUp),
|
933 | onBlur: callAllEventHandlers(onBlur, _this.buttonHandleBlur)
|
934 | };
|
935 | var eventHandlers = rest.disabled ? {} : enabledEventHandlers;
|
936 | return _extends({
|
937 | type: 'button',
|
938 | role: 'button',
|
939 | 'aria-label': isOpen ? 'close menu' : 'open menu',
|
940 | 'aria-haspopup': true,
|
941 | 'data-toggle': true
|
942 | }, eventHandlers, rest);
|
943 | };
|
944 |
|
945 | _this.buttonHandleKeyUp = function (event) {
|
946 |
|
947 | event.preventDefault();
|
948 | };
|
949 |
|
950 | _this.buttonHandleKeyDown = function (event) {
|
951 | var key = normalizeArrowKey(event);
|
952 |
|
953 | if (_this.buttonKeyDownHandlers[key]) {
|
954 | _this.buttonKeyDownHandlers[key].call(_assertThisInitialized(_this), event);
|
955 | }
|
956 | };
|
957 |
|
958 | _this.buttonHandleClick = function (event) {
|
959 | event.preventDefault();
|
960 |
|
961 |
|
962 |
|
963 |
|
964 | if (_this.props.environment.document.activeElement === _this.props.environment.document.body) {
|
965 | event.target.focus();
|
966 | }
|
967 |
|
968 |
|
969 |
|
970 |
|
971 | if (process.env.NODE_ENV === 'test') {
|
972 | _this.toggleMenu({
|
973 | type: clickButton
|
974 | });
|
975 | } else {
|
976 |
|
977 | _this.internalSetTimeout(function () {
|
978 | return _this.toggleMenu({
|
979 | type: clickButton
|
980 | });
|
981 | });
|
982 | }
|
983 | };
|
984 |
|
985 | _this.buttonHandleBlur = function (event) {
|
986 | var blurTarget = event.target;
|
987 |
|
988 |
|
989 | _this.internalSetTimeout(function () {
|
990 | if (!_this.isMouseDown && (_this.props.environment.document.activeElement == null || _this.props.environment.document.activeElement.id !== _this.inputId) && _this.props.environment.document.activeElement !== blurTarget
|
991 | ) {
|
992 | _this.reset({
|
993 | type: blurButton
|
994 | });
|
995 | }
|
996 | });
|
997 | };
|
998 |
|
999 | _this.getLabelProps = function (props) {
|
1000 | return _extends({
|
1001 | htmlFor: _this.inputId,
|
1002 | id: _this.labelId
|
1003 | }, props);
|
1004 | };
|
1005 |
|
1006 | _this.getInputProps = function (_temp4) {
|
1007 | var _ref4 = _temp4 === void 0 ? {} : _temp4,
|
1008 | onKeyDown = _ref4.onKeyDown,
|
1009 | onBlur = _ref4.onBlur,
|
1010 | onChange = _ref4.onChange,
|
1011 | onInput = _ref4.onInput;
|
1012 | _ref4.onChangeText;
|
1013 | var rest = _objectWithoutPropertiesLoose(_ref4, _excluded3$2);
|
1014 |
|
1015 | var onChangeKey;
|
1016 | var eventHandlers = {};
|
1017 |
|
1018 |
|
1019 | {
|
1020 | onChangeKey = 'onChange';
|
1021 | }
|
1022 |
|
1023 | var _this$getState6 = _this.getState(),
|
1024 | inputValue = _this$getState6.inputValue,
|
1025 | isOpen = _this$getState6.isOpen,
|
1026 | highlightedIndex = _this$getState6.highlightedIndex;
|
1027 |
|
1028 | if (!rest.disabled) {
|
1029 | var _eventHandlers;
|
1030 |
|
1031 | eventHandlers = (_eventHandlers = {}, _eventHandlers[onChangeKey] = callAllEventHandlers(onChange, onInput, _this.inputHandleChange), _eventHandlers.onKeyDown = callAllEventHandlers(onKeyDown, _this.inputHandleKeyDown), _eventHandlers.onBlur = callAllEventHandlers(onBlur, _this.inputHandleBlur), _eventHandlers);
|
1032 | }
|
1033 |
|
1034 | return _extends({
|
1035 | 'aria-autocomplete': 'list',
|
1036 | 'aria-activedescendant': isOpen && typeof highlightedIndex === 'number' && highlightedIndex >= 0 ? _this.getItemId(highlightedIndex) : null,
|
1037 | 'aria-controls': isOpen ? _this.menuId : null,
|
1038 | 'aria-labelledby': _this.labelId,
|
1039 |
|
1040 |
|
1041 | autoComplete: 'off',
|
1042 | value: inputValue,
|
1043 | id: _this.inputId
|
1044 | }, eventHandlers, rest);
|
1045 | };
|
1046 |
|
1047 | _this.inputHandleKeyDown = function (event) {
|
1048 | var key = normalizeArrowKey(event);
|
1049 |
|
1050 | if (key && _this.inputKeyDownHandlers[key]) {
|
1051 | _this.inputKeyDownHandlers[key].call(_assertThisInitialized(_this), event);
|
1052 | }
|
1053 | };
|
1054 |
|
1055 | _this.inputHandleChange = function (event) {
|
1056 | _this.internalSetState({
|
1057 | type: changeInput,
|
1058 | isOpen: true,
|
1059 | inputValue: event.target.value,
|
1060 | highlightedIndex: _this.props.defaultHighlightedIndex
|
1061 | });
|
1062 | };
|
1063 |
|
1064 | _this.inputHandleBlur = function () {
|
1065 |
|
1066 | _this.internalSetTimeout(function () {
|
1067 | var 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);
|
1068 |
|
1069 | if (!_this.isMouseDown && !downshiftButtonIsActive) {
|
1070 | _this.reset({
|
1071 | type: blurInput
|
1072 | });
|
1073 | }
|
1074 | });
|
1075 | };
|
1076 |
|
1077 | _this.menuRef = function (node) {
|
1078 | _this._menuNode = node;
|
1079 | };
|
1080 |
|
1081 | _this.getMenuProps = function (_temp5, _temp6) {
|
1082 | var _extends3;
|
1083 |
|
1084 | var _ref5 = _temp5 === void 0 ? {} : _temp5,
|
1085 | _ref5$refKey = _ref5.refKey,
|
1086 | refKey = _ref5$refKey === void 0 ? 'ref' : _ref5$refKey,
|
1087 | ref = _ref5.ref,
|
1088 | props = _objectWithoutPropertiesLoose(_ref5, _excluded4$1);
|
1089 |
|
1090 | var _ref6 = _temp6 === void 0 ? {} : _temp6,
|
1091 | _ref6$suppressRefErro = _ref6.suppressRefError,
|
1092 | suppressRefError = _ref6$suppressRefErro === void 0 ? false : _ref6$suppressRefErro;
|
1093 |
|
1094 | _this.getMenuProps.called = true;
|
1095 | _this.getMenuProps.refKey = refKey;
|
1096 | _this.getMenuProps.suppressRefError = suppressRefError;
|
1097 | return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, _this.menuRef), _extends3.role = 'listbox', _extends3['aria-labelledby'] = props && props['aria-label'] ? null : _this.labelId, _extends3.id = _this.menuId, _extends3), props);
|
1098 | };
|
1099 |
|
1100 | _this.getItemProps = function (_temp7) {
|
1101 | var _enabledEventHandlers;
|
1102 |
|
1103 | var _ref7 = _temp7 === void 0 ? {} : _temp7,
|
1104 | onMouseMove = _ref7.onMouseMove,
|
1105 | onMouseDown = _ref7.onMouseDown,
|
1106 | onClick = _ref7.onClick;
|
1107 | _ref7.onPress;
|
1108 | var index = _ref7.index,
|
1109 | _ref7$item = _ref7.item,
|
1110 | item = _ref7$item === void 0 ? process.env.NODE_ENV === 'production' ?
|
1111 |
|
1112 | undefined : requiredProp('getItemProps', 'item') : _ref7$item,
|
1113 | rest = _objectWithoutPropertiesLoose(_ref7, _excluded5$1);
|
1114 |
|
1115 | if (index === undefined) {
|
1116 | _this.items.push(item);
|
1117 |
|
1118 | index = _this.items.indexOf(item);
|
1119 | } else {
|
1120 | _this.items[index] = item;
|
1121 | }
|
1122 |
|
1123 | var onSelectKey = 'onClick';
|
1124 | var customClickHandler = onClick;
|
1125 | var enabledEventHandlers = (_enabledEventHandlers = {
|
1126 |
|
1127 |
|
1128 |
|
1129 | onMouseMove: callAllEventHandlers(onMouseMove, function () {
|
1130 | if (index === _this.getState().highlightedIndex) {
|
1131 | return;
|
1132 | }
|
1133 |
|
1134 | _this.setHighlightedIndex(index, {
|
1135 | type: itemMouseEnter
|
1136 | });
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 |
|
1142 | _this.avoidScrolling = true;
|
1143 |
|
1144 | _this.internalSetTimeout(function () {
|
1145 | return _this.avoidScrolling = false;
|
1146 | }, 250);
|
1147 | }),
|
1148 | onMouseDown: callAllEventHandlers(onMouseDown, function (event) {
|
1149 |
|
1150 |
|
1151 |
|
1152 | event.preventDefault();
|
1153 | })
|
1154 | }, _enabledEventHandlers[onSelectKey] = callAllEventHandlers(customClickHandler, function () {
|
1155 | _this.selectItemAtIndex(index, {
|
1156 | type: clickItem
|
1157 | });
|
1158 | }), _enabledEventHandlers);
|
1159 |
|
1160 |
|
1161 | var eventHandlers = rest.disabled ? {
|
1162 | onMouseDown: enabledEventHandlers.onMouseDown
|
1163 | } : enabledEventHandlers;
|
1164 | return _extends({
|
1165 | id: _this.getItemId(index),
|
1166 | role: 'option',
|
1167 | 'aria-selected': _this.getState().highlightedIndex === index
|
1168 | }, eventHandlers, rest);
|
1169 | };
|
1170 |
|
1171 | _this.clearItems = function () {
|
1172 | _this.items = [];
|
1173 | };
|
1174 |
|
1175 | _this.reset = function (otherStateToSet, cb) {
|
1176 | if (otherStateToSet === void 0) {
|
1177 | otherStateToSet = {};
|
1178 | }
|
1179 |
|
1180 | otherStateToSet = pickState(otherStateToSet);
|
1181 |
|
1182 | _this.internalSetState(function (_ref8) {
|
1183 | var selectedItem = _ref8.selectedItem;
|
1184 | return _extends({
|
1185 | isOpen: _this.props.defaultIsOpen,
|
1186 | highlightedIndex: _this.props.defaultHighlightedIndex,
|
1187 | inputValue: _this.props.itemToString(selectedItem)
|
1188 | }, otherStateToSet);
|
1189 | }, cb);
|
1190 | };
|
1191 |
|
1192 | _this.toggleMenu = function (otherStateToSet, cb) {
|
1193 | if (otherStateToSet === void 0) {
|
1194 | otherStateToSet = {};
|
1195 | }
|
1196 |
|
1197 | otherStateToSet = pickState(otherStateToSet);
|
1198 |
|
1199 | _this.internalSetState(function (_ref9) {
|
1200 | var isOpen = _ref9.isOpen;
|
1201 | return _extends({
|
1202 | isOpen: !isOpen
|
1203 | }, isOpen && {
|
1204 | highlightedIndex: _this.props.defaultHighlightedIndex
|
1205 | }, otherStateToSet);
|
1206 | }, function () {
|
1207 | var _this$getState7 = _this.getState(),
|
1208 | isOpen = _this$getState7.isOpen,
|
1209 | highlightedIndex = _this$getState7.highlightedIndex;
|
1210 |
|
1211 | if (isOpen) {
|
1212 | if (_this.getItemCount() > 0 && typeof highlightedIndex === 'number') {
|
1213 | _this.setHighlightedIndex(highlightedIndex, otherStateToSet);
|
1214 | }
|
1215 | }
|
1216 |
|
1217 | cbToCb(cb)();
|
1218 | });
|
1219 | };
|
1220 |
|
1221 | _this.openMenu = function (cb) {
|
1222 | _this.internalSetState({
|
1223 | isOpen: true
|
1224 | }, cb);
|
1225 | };
|
1226 |
|
1227 | _this.closeMenu = function (cb) {
|
1228 | _this.internalSetState({
|
1229 | isOpen: false
|
1230 | }, cb);
|
1231 | };
|
1232 |
|
1233 | _this.updateStatus = debounce(function () {
|
1234 | var state = _this.getState();
|
1235 |
|
1236 | var item = _this.items[state.highlightedIndex];
|
1237 |
|
1238 | var resultCount = _this.getItemCount();
|
1239 |
|
1240 | var status = _this.props.getA11yStatusMessage(_extends({
|
1241 | itemToString: _this.props.itemToString,
|
1242 | previousResultCount: _this.previousResultCount,
|
1243 | resultCount: resultCount,
|
1244 | highlightedItem: item
|
1245 | }, state));
|
1246 |
|
1247 | _this.previousResultCount = resultCount;
|
1248 | setStatus(status, _this.props.environment.document);
|
1249 | }, 200);
|
1250 |
|
1251 | var _this$props = _this.props,
|
1252 | defaultHighlightedIndex = _this$props.defaultHighlightedIndex,
|
1253 | _this$props$initialHi = _this$props.initialHighlightedIndex,
|
1254 | _highlightedIndex = _this$props$initialHi === void 0 ? defaultHighlightedIndex : _this$props$initialHi,
|
1255 | defaultIsOpen = _this$props.defaultIsOpen,
|
1256 | _this$props$initialIs = _this$props.initialIsOpen,
|
1257 | _isOpen = _this$props$initialIs === void 0 ? defaultIsOpen : _this$props$initialIs,
|
1258 | _this$props$initialIn = _this$props.initialInputValue,
|
1259 | _inputValue = _this$props$initialIn === void 0 ? '' : _this$props$initialIn,
|
1260 | _this$props$initialSe = _this$props.initialSelectedItem,
|
1261 | _selectedItem = _this$props$initialSe === void 0 ? null : _this$props$initialSe;
|
1262 |
|
1263 | var _state = _this.getState({
|
1264 | highlightedIndex: _highlightedIndex,
|
1265 | isOpen: _isOpen,
|
1266 | inputValue: _inputValue,
|
1267 | selectedItem: _selectedItem
|
1268 | });
|
1269 |
|
1270 | if (_state.selectedItem != null && _this.props.initialInputValue === undefined) {
|
1271 | _state.inputValue = _this.props.itemToString(_state.selectedItem);
|
1272 | }
|
1273 |
|
1274 | _this.state = _state;
|
1275 | return _this;
|
1276 | }
|
1277 |
|
1278 | var _proto = Downshift.prototype;
|
1279 |
|
1280 | |
1281 |
|
1282 |
|
1283 | _proto.internalClearTimeouts = function internalClearTimeouts() {
|
1284 | this.timeoutIds.forEach(function (id) {
|
1285 | clearTimeout(id);
|
1286 | });
|
1287 | this.timeoutIds = [];
|
1288 | }
|
1289 | |
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 |
|
1295 |
|
1296 |
|
1297 |
|
1298 | ;
|
1299 |
|
1300 | _proto.getState = function getState$1(stateToMerge) {
|
1301 | if (stateToMerge === void 0) {
|
1302 | stateToMerge = this.state;
|
1303 | }
|
1304 |
|
1305 | return getState(stateToMerge, this.props);
|
1306 | };
|
1307 |
|
1308 | _proto.getItemCount = function getItemCount() {
|
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 | var itemCount = this.items.length;
|
1314 |
|
1315 | if (this.itemCount != null) {
|
1316 | itemCount = this.itemCount;
|
1317 | } else if (this.props.itemCount !== undefined) {
|
1318 | itemCount = this.props.itemCount;
|
1319 | }
|
1320 |
|
1321 | return itemCount;
|
1322 | };
|
1323 |
|
1324 | _proto.getItemNodeFromIndex = function getItemNodeFromIndex(index) {
|
1325 | return this.props.environment.document.getElementById(this.getItemId(index));
|
1326 | };
|
1327 |
|
1328 | _proto.scrollHighlightedItemIntoView = function scrollHighlightedItemIntoView() {
|
1329 |
|
1330 | {
|
1331 | var node = this.getItemNodeFromIndex(this.getState().highlightedIndex);
|
1332 | this.props.scrollIntoView(node, this._menuNode);
|
1333 | }
|
1334 | };
|
1335 |
|
1336 | _proto.moveHighlightedIndex = function moveHighlightedIndex(amount, otherStateToSet) {
|
1337 | var _this6 = this;
|
1338 |
|
1339 | var itemCount = this.getItemCount();
|
1340 |
|
1341 | var _this$getState8 = this.getState(),
|
1342 | highlightedIndex = _this$getState8.highlightedIndex;
|
1343 |
|
1344 | if (itemCount > 0) {
|
1345 | var nextHighlightedIndex = getNextWrappingIndex(amount, highlightedIndex, itemCount, function (index) {
|
1346 | return _this6.getItemNodeFromIndex(index);
|
1347 | });
|
1348 | this.setHighlightedIndex(nextHighlightedIndex, otherStateToSet);
|
1349 | }
|
1350 | };
|
1351 |
|
1352 | _proto.getStateAndHelpers = function getStateAndHelpers() {
|
1353 | var _this$getState9 = this.getState(),
|
1354 | highlightedIndex = _this$getState9.highlightedIndex,
|
1355 | inputValue = _this$getState9.inputValue,
|
1356 | selectedItem = _this$getState9.selectedItem,
|
1357 | isOpen = _this$getState9.isOpen;
|
1358 |
|
1359 | var itemToString = this.props.itemToString;
|
1360 | var id = this.id;
|
1361 | var getRootProps = this.getRootProps,
|
1362 | getToggleButtonProps = this.getToggleButtonProps,
|
1363 | getLabelProps = this.getLabelProps,
|
1364 | getMenuProps = this.getMenuProps,
|
1365 | getInputProps = this.getInputProps,
|
1366 | getItemProps = this.getItemProps,
|
1367 | openMenu = this.openMenu,
|
1368 | closeMenu = this.closeMenu,
|
1369 | toggleMenu = this.toggleMenu,
|
1370 | selectItem = this.selectItem,
|
1371 | selectItemAtIndex = this.selectItemAtIndex,
|
1372 | selectHighlightedItem = this.selectHighlightedItem,
|
1373 | setHighlightedIndex = this.setHighlightedIndex,
|
1374 | clearSelection = this.clearSelection,
|
1375 | clearItems = this.clearItems,
|
1376 | reset = this.reset,
|
1377 | setItemCount = this.setItemCount,
|
1378 | unsetItemCount = this.unsetItemCount,
|
1379 | setState = this.internalSetState;
|
1380 | return {
|
1381 |
|
1382 | getRootProps: getRootProps,
|
1383 | getToggleButtonProps: getToggleButtonProps,
|
1384 | getLabelProps: getLabelProps,
|
1385 | getMenuProps: getMenuProps,
|
1386 | getInputProps: getInputProps,
|
1387 | getItemProps: getItemProps,
|
1388 |
|
1389 | reset: reset,
|
1390 | openMenu: openMenu,
|
1391 | closeMenu: closeMenu,
|
1392 | toggleMenu: toggleMenu,
|
1393 | selectItem: selectItem,
|
1394 | selectItemAtIndex: selectItemAtIndex,
|
1395 | selectHighlightedItem: selectHighlightedItem,
|
1396 | setHighlightedIndex: setHighlightedIndex,
|
1397 | clearSelection: clearSelection,
|
1398 | clearItems: clearItems,
|
1399 | setItemCount: setItemCount,
|
1400 | unsetItemCount: unsetItemCount,
|
1401 | setState: setState,
|
1402 |
|
1403 | itemToString: itemToString,
|
1404 |
|
1405 | id: id,
|
1406 |
|
1407 | highlightedIndex: highlightedIndex,
|
1408 | inputValue: inputValue,
|
1409 | isOpen: isOpen,
|
1410 | selectedItem: selectedItem
|
1411 | };
|
1412 | }
|
1413 | ;
|
1414 |
|
1415 | _proto.componentDidMount = function componentDidMount() {
|
1416 | var _this7 = this;
|
1417 |
|
1418 |
|
1419 | if (process.env.NODE_ENV !== 'production' && !false && this.getMenuProps.called && !this.getMenuProps.suppressRefError) {
|
1420 | validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
|
1421 | }
|
1422 |
|
1423 |
|
1424 |
|
1425 | {
|
1426 |
|
1427 |
|
1428 |
|
1429 |
|
1430 |
|
1431 | var onMouseDown = function onMouseDown() {
|
1432 | _this7.isMouseDown = true;
|
1433 | };
|
1434 |
|
1435 | var onMouseUp = function onMouseUp(event) {
|
1436 | _this7.isMouseDown = false;
|
1437 |
|
1438 |
|
1439 | var contextWithinDownshift = targetWithinDownshift(event.target, [_this7._rootNode, _this7._menuNode], _this7.props.environment);
|
1440 |
|
1441 | if (!contextWithinDownshift && _this7.getState().isOpen) {
|
1442 | _this7.reset({
|
1443 | type: mouseUp
|
1444 | }, function () {
|
1445 | return _this7.props.onOuterClick(_this7.getStateAndHelpers());
|
1446 | });
|
1447 | }
|
1448 | };
|
1449 |
|
1450 |
|
1451 |
|
1452 |
|
1453 |
|
1454 |
|
1455 |
|
1456 | var onTouchStart = function onTouchStart() {
|
1457 | _this7.isTouchMove = false;
|
1458 | };
|
1459 |
|
1460 | var onTouchMove = function onTouchMove() {
|
1461 | _this7.isTouchMove = true;
|
1462 | };
|
1463 |
|
1464 | var onTouchEnd = function onTouchEnd(event) {
|
1465 | var contextWithinDownshift = targetWithinDownshift(event.target, [_this7._rootNode, _this7._menuNode], _this7.props.environment, false);
|
1466 |
|
1467 | if (!_this7.isTouchMove && !contextWithinDownshift && _this7.getState().isOpen) {
|
1468 | _this7.reset({
|
1469 | type: touchEnd
|
1470 | }, function () {
|
1471 | return _this7.props.onOuterClick(_this7.getStateAndHelpers());
|
1472 | });
|
1473 | }
|
1474 | };
|
1475 |
|
1476 | var environment = this.props.environment;
|
1477 | environment.addEventListener('mousedown', onMouseDown);
|
1478 | environment.addEventListener('mouseup', onMouseUp);
|
1479 | environment.addEventListener('touchstart', onTouchStart);
|
1480 | environment.addEventListener('touchmove', onTouchMove);
|
1481 | environment.addEventListener('touchend', onTouchEnd);
|
1482 |
|
1483 | this.cleanup = function () {
|
1484 | _this7.internalClearTimeouts();
|
1485 |
|
1486 | _this7.updateStatus.cancel();
|
1487 |
|
1488 | environment.removeEventListener('mousedown', onMouseDown);
|
1489 | environment.removeEventListener('mouseup', onMouseUp);
|
1490 | environment.removeEventListener('touchstart', onTouchStart);
|
1491 | environment.removeEventListener('touchmove', onTouchMove);
|
1492 | environment.removeEventListener('touchend', onTouchEnd);
|
1493 | };
|
1494 | }
|
1495 | };
|
1496 |
|
1497 | _proto.shouldScroll = function shouldScroll(prevState, prevProps) {
|
1498 | var _ref10 = this.props.highlightedIndex === undefined ? this.getState() : this.props,
|
1499 | currentHighlightedIndex = _ref10.highlightedIndex;
|
1500 |
|
1501 | var _ref11 = prevProps.highlightedIndex === undefined ? prevState : prevProps,
|
1502 | prevHighlightedIndex = _ref11.highlightedIndex;
|
1503 |
|
1504 | var scrollWhenOpen = currentHighlightedIndex && this.getState().isOpen && !prevState.isOpen;
|
1505 | var scrollWhenNavigating = currentHighlightedIndex !== prevHighlightedIndex;
|
1506 | return scrollWhenOpen || scrollWhenNavigating;
|
1507 | };
|
1508 |
|
1509 | _proto.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
|
1510 | if (process.env.NODE_ENV !== 'production') {
|
1511 | validateControlledUnchanged(this.state, prevProps, this.props);
|
1512 |
|
1513 |
|
1514 | if (this.getMenuProps.called && !this.getMenuProps.suppressRefError) {
|
1515 | validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
|
1516 | }
|
1517 | }
|
1518 |
|
1519 | if (isControlledProp(this.props, 'selectedItem') && this.props.selectedItemChanged(prevProps.selectedItem, this.props.selectedItem)) {
|
1520 | this.internalSetState({
|
1521 | type: controlledPropUpdatedSelectedItem,
|
1522 | inputValue: this.props.itemToString(this.props.selectedItem)
|
1523 | });
|
1524 | }
|
1525 |
|
1526 | if (!this.avoidScrolling && this.shouldScroll(prevState, prevProps)) {
|
1527 | this.scrollHighlightedItemIntoView();
|
1528 | }
|
1529 |
|
1530 |
|
1531 |
|
1532 | {
|
1533 | this.updateStatus();
|
1534 | }
|
1535 | };
|
1536 |
|
1537 | _proto.componentWillUnmount = function componentWillUnmount() {
|
1538 | this.cleanup();
|
1539 | };
|
1540 |
|
1541 | _proto.render = function render() {
|
1542 | var children = unwrapArray(this.props.children, noop);
|
1543 |
|
1544 |
|
1545 |
|
1546 | this.clearItems();
|
1547 |
|
1548 |
|
1549 |
|
1550 |
|
1551 | this.getRootProps.called = false;
|
1552 | this.getRootProps.refKey = undefined;
|
1553 | this.getRootProps.suppressRefError = undefined;
|
1554 |
|
1555 | this.getMenuProps.called = false;
|
1556 | this.getMenuProps.refKey = undefined;
|
1557 | this.getMenuProps.suppressRefError = undefined;
|
1558 |
|
1559 | this.getLabelProps.called = false;
|
1560 |
|
1561 | this.getInputProps.called = false;
|
1562 | var element = unwrapArray(children(this.getStateAndHelpers()));
|
1563 |
|
1564 | if (!element) {
|
1565 | return null;
|
1566 | }
|
1567 |
|
1568 | if (this.getRootProps.called || this.props.suppressRefError) {
|
1569 | if (process.env.NODE_ENV !== 'production' && !this.getRootProps.suppressRefError && !this.props.suppressRefError) {
|
1570 | validateGetRootPropsCalledCorrectly(element, this.getRootProps);
|
1571 | }
|
1572 |
|
1573 | return element;
|
1574 | } else if (isDOMElement(element)) {
|
1575 |
|
1576 |
|
1577 | return cloneElement(element, this.getRootProps(getElementProps(element)));
|
1578 | }
|
1579 |
|
1580 |
|
1581 |
|
1582 | if (process.env.NODE_ENV !== 'production') {
|
1583 |
|
1584 |
|
1585 | throw new Error('downshift: If you return a non-DOM element, you must apply the getRootProps function');
|
1586 | }
|
1587 |
|
1588 |
|
1589 |
|
1590 | return undefined;
|
1591 | };
|
1592 |
|
1593 | return Downshift;
|
1594 | }(Component);
|
1595 |
|
1596 | Downshift.defaultProps = {
|
1597 | defaultHighlightedIndex: null,
|
1598 | defaultIsOpen: false,
|
1599 | getA11yStatusMessage: getA11yStatusMessage$1,
|
1600 | itemToString: function itemToString(i) {
|
1601 | if (i == null) {
|
1602 | return '';
|
1603 | }
|
1604 |
|
1605 | if (process.env.NODE_ENV !== 'production' && isPlainObject(i) && !i.hasOwnProperty('toString')) {
|
1606 |
|
1607 | console.warn('downshift: An object was passed to the default implementation of `itemToString`. You should probably provide your own `itemToString` implementation. Please refer to the `itemToString` API documentation.', 'The object that was passed:', i);
|
1608 | }
|
1609 |
|
1610 | return String(i);
|
1611 | },
|
1612 | onStateChange: noop,
|
1613 | onInputValueChange: noop,
|
1614 | onUserAction: noop,
|
1615 | onChange: noop,
|
1616 | onSelect: noop,
|
1617 | onOuterClick: noop,
|
1618 | selectedItemChanged: function selectedItemChanged(prevItem, item) {
|
1619 | return prevItem !== item;
|
1620 | },
|
1621 | environment:
|
1622 |
|
1623 | typeof window === 'undefined' ? {} : window,
|
1624 | stateReducer: function stateReducer(state, stateToSet) {
|
1625 | return stateToSet;
|
1626 | },
|
1627 | suppressRefError: false,
|
1628 | scrollIntoView: scrollIntoView
|
1629 | };
|
1630 | Downshift.stateChangeTypes = stateChangeTypes$3;
|
1631 | return Downshift;
|
1632 | }();
|
1633 |
|
1634 | process.env.NODE_ENV !== "production" ? Downshift.propTypes = {
|
1635 | children: PropTypes.func,
|
1636 | defaultHighlightedIndex: PropTypes.number,
|
1637 | defaultIsOpen: PropTypes.bool,
|
1638 | initialHighlightedIndex: PropTypes.number,
|
1639 | initialSelectedItem: PropTypes.any,
|
1640 | initialInputValue: PropTypes.string,
|
1641 | initialIsOpen: PropTypes.bool,
|
1642 | getA11yStatusMessage: PropTypes.func,
|
1643 | itemToString: PropTypes.func,
|
1644 | onChange: PropTypes.func,
|
1645 | onSelect: PropTypes.func,
|
1646 | onStateChange: PropTypes.func,
|
1647 | onInputValueChange: PropTypes.func,
|
1648 | onUserAction: PropTypes.func,
|
1649 | onOuterClick: PropTypes.func,
|
1650 | selectedItemChanged: PropTypes.func,
|
1651 | stateReducer: PropTypes.func,
|
1652 | itemCount: PropTypes.number,
|
1653 | id: PropTypes.string,
|
1654 | environment: PropTypes.shape({
|
1655 | addEventListener: PropTypes.func,
|
1656 | removeEventListener: PropTypes.func,
|
1657 | document: PropTypes.shape({
|
1658 | getElementById: PropTypes.func,
|
1659 | activeElement: PropTypes.any,
|
1660 | body: PropTypes.any
|
1661 | })
|
1662 | }),
|
1663 | suppressRefError: PropTypes.bool,
|
1664 | scrollIntoView: PropTypes.func,
|
1665 |
|
1666 |
|
1667 |
|
1668 |
|
1669 | selectedItem: PropTypes.any,
|
1670 | isOpen: PropTypes.bool,
|
1671 | inputValue: PropTypes.string,
|
1672 | highlightedIndex: PropTypes.number,
|
1673 | labelId: PropTypes.string,
|
1674 | inputId: PropTypes.string,
|
1675 | menuId: PropTypes.string,
|
1676 | getItemId: PropTypes.func
|
1677 |
|
1678 |
|
1679 | } : void 0;
|
1680 | var Downshift$1 = Downshift;
|
1681 |
|
1682 | function validateGetMenuPropsCalledCorrectly(node, _ref12) {
|
1683 | var refKey = _ref12.refKey;
|
1684 |
|
1685 | if (!node) {
|
1686 |
|
1687 | console.error("downshift: The ref prop \"" + refKey + "\" from getMenuProps was not applied correctly on your menu element.");
|
1688 | }
|
1689 | }
|
1690 |
|
1691 | function validateGetRootPropsCalledCorrectly(element, _ref13) {
|
1692 | var refKey = _ref13.refKey;
|
1693 | var refKeySpecified = refKey !== 'ref';
|
1694 | var isComposite = !isDOMElement(element);
|
1695 |
|
1696 | if (isComposite && !refKeySpecified && !isForwardRef(element)) {
|
1697 |
|
1698 | console.error('downshift: You returned a non-DOM element. You must specify a refKey in getRootProps');
|
1699 | } else if (!isComposite && refKeySpecified) {
|
1700 |
|
1701 | console.error("downshift: You returned a DOM element. You should not specify a refKey in getRootProps. You specified \"" + refKey + "\"");
|
1702 | }
|
1703 |
|
1704 | if (!isForwardRef(element) && !getElementProps(element)[refKey]) {
|
1705 |
|
1706 | console.error("downshift: You must apply the ref prop \"" + refKey + "\" from getRootProps onto your root element.");
|
1707 | }
|
1708 | }
|
1709 |
|
1710 | var _excluded$3 = ["isInitialMount", "highlightedIndex", "items", "environment"];
|
1711 | var dropdownDefaultStateValues = {
|
1712 | highlightedIndex: -1,
|
1713 | isOpen: false,
|
1714 | selectedItem: null,
|
1715 | inputValue: ''
|
1716 | };
|
1717 |
|
1718 | function callOnChangeProps(action, state, newState) {
|
1719 | var props = action.props,
|
1720 | type = action.type;
|
1721 | var changes = {};
|
1722 | Object.keys(state).forEach(function (key) {
|
1723 | invokeOnChangeHandler(key, action, state, newState);
|
1724 |
|
1725 | if (newState[key] !== state[key]) {
|
1726 | changes[key] = newState[key];
|
1727 | }
|
1728 | });
|
1729 |
|
1730 | if (props.onStateChange && Object.keys(changes).length) {
|
1731 | props.onStateChange(_extends({
|
1732 | type: type
|
1733 | }, changes));
|
1734 | }
|
1735 | }
|
1736 |
|
1737 | function invokeOnChangeHandler(key, action, state, newState) {
|
1738 | var props = action.props,
|
1739 | type = action.type;
|
1740 | var handler = "on" + capitalizeString(key) + "Change";
|
1741 |
|
1742 | if (props[handler] && newState[key] !== undefined && newState[key] !== state[key]) {
|
1743 | props[handler](_extends({
|
1744 | type: type
|
1745 | }, newState));
|
1746 | }
|
1747 | }
|
1748 |
|
1749 |
|
1750 |
|
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 | function stateReducer(s, a) {
|
1758 | return a.changes;
|
1759 | }
|
1760 |
|
1761 |
|
1762 |
|
1763 |
|
1764 |
|
1765 |
|
1766 |
|
1767 |
|
1768 | function getA11ySelectionMessage(selectionParameters) {
|
1769 | var selectedItem = selectionParameters.selectedItem,
|
1770 | itemToStringLocal = selectionParameters.itemToString;
|
1771 | return selectedItem ? itemToStringLocal(selectedItem) + " has been selected." : '';
|
1772 | }
|
1773 |
|
1774 |
|
1775 |
|
1776 |
|
1777 |
|
1778 | var updateA11yStatus = debounce(function (getA11yMessage, document) {
|
1779 | setStatus(getA11yMessage(), document);
|
1780 | }, 200);
|
1781 |
|
1782 | var useIsomorphicLayoutEffect = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined' ? useLayoutEffect : useEffect;
|
1783 |
|
1784 | function useElementIds(_ref) {
|
1785 | var _ref$id = _ref.id,
|
1786 | id = _ref$id === void 0 ? "downshift-" + generateId() : _ref$id,
|
1787 | labelId = _ref.labelId,
|
1788 | menuId = _ref.menuId,
|
1789 | getItemId = _ref.getItemId,
|
1790 | toggleButtonId = _ref.toggleButtonId,
|
1791 | inputId = _ref.inputId;
|
1792 | var elementIdsRef = useRef({
|
1793 | labelId: labelId || id + "-label",
|
1794 | menuId: menuId || id + "-menu",
|
1795 | getItemId: getItemId || function (index) {
|
1796 | return id + "-item-" + index;
|
1797 | },
|
1798 | toggleButtonId: toggleButtonId || id + "-toggle-button",
|
1799 | inputId: inputId || id + "-input"
|
1800 | });
|
1801 | return elementIdsRef.current;
|
1802 | }
|
1803 |
|
1804 | function getItemIndex(index, item, items) {
|
1805 | if (index !== undefined) {
|
1806 | return index;
|
1807 | }
|
1808 |
|
1809 | if (items.length === 0) {
|
1810 | return -1;
|
1811 | }
|
1812 |
|
1813 | return items.indexOf(item);
|
1814 | }
|
1815 |
|
1816 | function itemToString(item) {
|
1817 | return item ? String(item) : '';
|
1818 | }
|
1819 |
|
1820 | function isAcceptedCharacterKey(key) {
|
1821 | return /^\S{1}$/.test(key);
|
1822 | }
|
1823 |
|
1824 | function capitalizeString(string) {
|
1825 | return "" + string.slice(0, 1).toUpperCase() + string.slice(1);
|
1826 | }
|
1827 |
|
1828 | function useLatestRef(val) {
|
1829 | var ref = useRef(val);
|
1830 |
|
1831 |
|
1832 |
|
1833 |
|
1834 |
|
1835 | ref.current = val;
|
1836 | return ref;
|
1837 | }
|
1838 |
|
1839 |
|
1840 |
|
1841 |
|
1842 |
|
1843 |
|
1844 |
|
1845 |
|
1846 |
|
1847 |
|
1848 |
|
1849 |
|
1850 | function useEnhancedReducer(reducer, initialState, props) {
|
1851 | var prevStateRef = useRef();
|
1852 | var actionRef = useRef();
|
1853 | var enhancedReducer = useCallback(function (state, action) {
|
1854 | actionRef.current = action;
|
1855 | state = getState(state, action.props);
|
1856 | var changes = reducer(state, action);
|
1857 | var newState = action.props.stateReducer(state, _extends({}, action, {
|
1858 | changes: changes
|
1859 | }));
|
1860 | return newState;
|
1861 | }, [reducer]);
|
1862 |
|
1863 | var _useReducer = useReducer(enhancedReducer, initialState),
|
1864 | state = _useReducer[0],
|
1865 | dispatch = _useReducer[1];
|
1866 |
|
1867 | var propsRef = useLatestRef(props);
|
1868 | var dispatchWithProps = useCallback(function (action) {
|
1869 | return dispatch(_extends({
|
1870 | props: propsRef.current
|
1871 | }, action));
|
1872 | }, [propsRef]);
|
1873 | var action = actionRef.current;
|
1874 | useEffect(function () {
|
1875 | if (action && prevStateRef.current && prevStateRef.current !== state) {
|
1876 | callOnChangeProps(action, getState(prevStateRef.current, action.props), state);
|
1877 | }
|
1878 |
|
1879 | prevStateRef.current = state;
|
1880 | }, [state, props, action]);
|
1881 | return [state, dispatchWithProps];
|
1882 | }
|
1883 |
|
1884 |
|
1885 |
|
1886 |
|
1887 |
|
1888 |
|
1889 |
|
1890 |
|
1891 |
|
1892 |
|
1893 |
|
1894 | function useControlledReducer$1(reducer, initialState, props) {
|
1895 | var _useEnhancedReducer = useEnhancedReducer(reducer, initialState, props),
|
1896 | state = _useEnhancedReducer[0],
|
1897 | dispatch = _useEnhancedReducer[1];
|
1898 |
|
1899 | return [getState(state, props), dispatch];
|
1900 | }
|
1901 |
|
1902 | var defaultProps$3 = {
|
1903 | itemToString: itemToString,
|
1904 | stateReducer: stateReducer,
|
1905 | getA11ySelectionMessage: getA11ySelectionMessage,
|
1906 | scrollIntoView: scrollIntoView,
|
1907 | circularNavigation: false,
|
1908 | environment:
|
1909 |
|
1910 | typeof window === 'undefined' ? {} : window
|
1911 | };
|
1912 |
|
1913 | function getDefaultValue$1(props, propKey, defaultStateValues) {
|
1914 | if (defaultStateValues === void 0) {
|
1915 | defaultStateValues = dropdownDefaultStateValues;
|
1916 | }
|
1917 |
|
1918 | var defaultPropKey = "default" + capitalizeString(propKey);
|
1919 |
|
1920 | if (defaultPropKey in props) {
|
1921 | return props[defaultPropKey];
|
1922 | }
|
1923 |
|
1924 | return defaultStateValues[propKey];
|
1925 | }
|
1926 |
|
1927 | function getInitialValue$1(props, propKey, defaultStateValues) {
|
1928 | if (defaultStateValues === void 0) {
|
1929 | defaultStateValues = dropdownDefaultStateValues;
|
1930 | }
|
1931 |
|
1932 | if (propKey in props) {
|
1933 | return props[propKey];
|
1934 | }
|
1935 |
|
1936 | var initialPropKey = "initial" + capitalizeString(propKey);
|
1937 |
|
1938 | if (initialPropKey in props) {
|
1939 | return props[initialPropKey];
|
1940 | }
|
1941 |
|
1942 | return getDefaultValue$1(props, propKey, defaultStateValues);
|
1943 | }
|
1944 |
|
1945 | function getInitialState$2(props) {
|
1946 | var selectedItem = getInitialValue$1(props, 'selectedItem');
|
1947 | var isOpen = getInitialValue$1(props, 'isOpen');
|
1948 | var highlightedIndex = getInitialValue$1(props, 'highlightedIndex');
|
1949 | var inputValue = getInitialValue$1(props, 'inputValue');
|
1950 | return {
|
1951 | highlightedIndex: highlightedIndex < 0 && selectedItem && isOpen ? props.items.indexOf(selectedItem) : highlightedIndex,
|
1952 | isOpen: isOpen,
|
1953 | selectedItem: selectedItem,
|
1954 | inputValue: inputValue
|
1955 | };
|
1956 | }
|
1957 |
|
1958 | function getHighlightedIndexOnOpen(props, state, offset, getItemNodeFromIndex) {
|
1959 | var items = props.items,
|
1960 | initialHighlightedIndex = props.initialHighlightedIndex,
|
1961 | defaultHighlightedIndex = props.defaultHighlightedIndex;
|
1962 | var selectedItem = state.selectedItem,
|
1963 | highlightedIndex = state.highlightedIndex;
|
1964 |
|
1965 | if (items.length === 0) {
|
1966 | return -1;
|
1967 | }
|
1968 |
|
1969 |
|
1970 | if (initialHighlightedIndex !== undefined && highlightedIndex === initialHighlightedIndex) {
|
1971 | return initialHighlightedIndex;
|
1972 | }
|
1973 |
|
1974 | if (defaultHighlightedIndex !== undefined) {
|
1975 | return defaultHighlightedIndex;
|
1976 | }
|
1977 |
|
1978 | if (selectedItem) {
|
1979 | if (offset === 0) {
|
1980 | return items.indexOf(selectedItem);
|
1981 | }
|
1982 |
|
1983 | return getNextWrappingIndex(offset, items.indexOf(selectedItem), items.length, getItemNodeFromIndex, false);
|
1984 | }
|
1985 |
|
1986 | if (offset === 0) {
|
1987 | return -1;
|
1988 | }
|
1989 |
|
1990 | return offset < 0 ? items.length - 1 : 0;
|
1991 | }
|
1992 |
|
1993 |
|
1994 |
|
1995 |
|
1996 |
|
1997 |
|
1998 |
|
1999 |
|
2000 |
|
2001 |
|
2002 |
|
2003 | function useMouseAndTouchTracker(isOpen, downshiftElementRefs, environment, handleBlur) {
|
2004 | var mouseAndTouchTrackersRef = useRef({
|
2005 | isMouseDown: false,
|
2006 | isTouchMove: false
|
2007 | });
|
2008 | useEffect(function () {
|
2009 |
|
2010 |
|
2011 | var onMouseDown = function onMouseDown() {
|
2012 | mouseAndTouchTrackersRef.current.isMouseDown = true;
|
2013 | };
|
2014 |
|
2015 | var onMouseUp = function onMouseUp(event) {
|
2016 | mouseAndTouchTrackersRef.current.isMouseDown = false;
|
2017 |
|
2018 | if (isOpen && !targetWithinDownshift(event.target, downshiftElementRefs.map(function (ref) {
|
2019 | return ref.current;
|
2020 | }), environment)) {
|
2021 | handleBlur();
|
2022 | }
|
2023 | };
|
2024 |
|
2025 | var onTouchStart = function onTouchStart() {
|
2026 | mouseAndTouchTrackersRef.current.isTouchMove = false;
|
2027 | };
|
2028 |
|
2029 | var onTouchMove = function onTouchMove() {
|
2030 | mouseAndTouchTrackersRef.current.isTouchMove = true;
|
2031 | };
|
2032 |
|
2033 | var onTouchEnd = function onTouchEnd(event) {
|
2034 | if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, downshiftElementRefs.map(function (ref) {
|
2035 | return ref.current;
|
2036 | }), environment, false)) {
|
2037 | handleBlur();
|
2038 | }
|
2039 | };
|
2040 |
|
2041 | environment.addEventListener('mousedown', onMouseDown);
|
2042 | environment.addEventListener('mouseup', onMouseUp);
|
2043 | environment.addEventListener('touchstart', onTouchStart);
|
2044 | environment.addEventListener('touchmove', onTouchMove);
|
2045 | environment.addEventListener('touchend', onTouchEnd);
|
2046 | return function cleanup() {
|
2047 | environment.removeEventListener('mousedown', onMouseDown);
|
2048 | environment.removeEventListener('mouseup', onMouseUp);
|
2049 | environment.removeEventListener('touchstart', onTouchStart);
|
2050 | environment.removeEventListener('touchmove', onTouchMove);
|
2051 | environment.removeEventListener('touchend', onTouchEnd);
|
2052 | };
|
2053 | }, [isOpen, environment]);
|
2054 | return mouseAndTouchTrackersRef;
|
2055 | }
|
2056 |
|
2057 |
|
2058 |
|
2059 |
|
2060 | var useGetterPropsCalledChecker = function useGetterPropsCalledChecker() {
|
2061 | return noop;
|
2062 | };
|
2063 |
|
2064 |
|
2065 |
|
2066 |
|
2067 |
|
2068 |
|
2069 |
|
2070 |
|
2071 |
|
2072 |
|
2073 | if (process.env.NODE_ENV !== 'production') {
|
2074 | useGetterPropsCalledChecker = function useGetterPropsCalledChecker() {
|
2075 | var isInitialMountRef = useRef(true);
|
2076 |
|
2077 | for (var _len = arguments.length, propKeys = new Array(_len), _key = 0; _key < _len; _key++) {
|
2078 | propKeys[_key] = arguments[_key];
|
2079 | }
|
2080 |
|
2081 | var getterPropsCalledRef = useRef(propKeys.reduce(function (acc, propKey) {
|
2082 | acc[propKey] = {};
|
2083 | return acc;
|
2084 | }, {}));
|
2085 | useEffect(function () {
|
2086 | Object.keys(getterPropsCalledRef.current).forEach(function (propKey) {
|
2087 | var propCallInfo = getterPropsCalledRef.current[propKey];
|
2088 |
|
2089 | if (isInitialMountRef.current) {
|
2090 | if (!Object.keys(propCallInfo).length) {
|
2091 |
|
2092 | console.error("downshift: You forgot to call the " + propKey + " getter function on your component / element.");
|
2093 | return;
|
2094 | }
|
2095 | }
|
2096 |
|
2097 | var suppressRefError = propCallInfo.suppressRefError,
|
2098 | refKey = propCallInfo.refKey,
|
2099 | elementRef = propCallInfo.elementRef;
|
2100 |
|
2101 | if ((!elementRef || !elementRef.current) && !suppressRefError) {
|
2102 |
|
2103 | console.error("downshift: The ref prop \"" + refKey + "\" from " + propKey + " was not applied correctly on your element.");
|
2104 | }
|
2105 | });
|
2106 | isInitialMountRef.current = false;
|
2107 | });
|
2108 | var setGetterPropCallInfo = useCallback(function (propKey, suppressRefError, refKey, elementRef) {
|
2109 | getterPropsCalledRef.current[propKey] = {
|
2110 | suppressRefError: suppressRefError,
|
2111 | refKey: refKey,
|
2112 | elementRef: elementRef
|
2113 | };
|
2114 | }, []);
|
2115 | return setGetterPropCallInfo;
|
2116 | };
|
2117 | }
|
2118 |
|
2119 | function useA11yMessageSetter(getA11yMessage, dependencyArray, _ref2) {
|
2120 | var isInitialMount = _ref2.isInitialMount,
|
2121 | highlightedIndex = _ref2.highlightedIndex,
|
2122 | items = _ref2.items,
|
2123 | environment = _ref2.environment,
|
2124 | rest = _objectWithoutPropertiesLoose(_ref2, _excluded$3);
|
2125 |
|
2126 |
|
2127 | useEffect(function () {
|
2128 | if (isInitialMount || false) {
|
2129 | return;
|
2130 | }
|
2131 |
|
2132 | updateA11yStatus(function () {
|
2133 | return getA11yMessage(_extends({
|
2134 | highlightedIndex: highlightedIndex,
|
2135 | highlightedItem: items[highlightedIndex],
|
2136 | resultCount: items.length
|
2137 | }, rest));
|
2138 | }, environment.document);
|
2139 | }, dependencyArray);
|
2140 | }
|
2141 |
|
2142 | function useScrollIntoView(_ref3) {
|
2143 | var highlightedIndex = _ref3.highlightedIndex,
|
2144 | isOpen = _ref3.isOpen,
|
2145 | itemRefs = _ref3.itemRefs,
|
2146 | getItemNodeFromIndex = _ref3.getItemNodeFromIndex,
|
2147 | menuElement = _ref3.menuElement,
|
2148 | scrollIntoViewProp = _ref3.scrollIntoView;
|
2149 |
|
2150 | var shouldScrollRef = useRef(true);
|
2151 |
|
2152 | useIsomorphicLayoutEffect(function () {
|
2153 | if (highlightedIndex < 0 || !isOpen || !Object.keys(itemRefs.current).length) {
|
2154 | return;
|
2155 | }
|
2156 |
|
2157 | if (shouldScrollRef.current === false) {
|
2158 | shouldScrollRef.current = true;
|
2159 | } else {
|
2160 | scrollIntoViewProp(getItemNodeFromIndex(highlightedIndex), menuElement);
|
2161 | }
|
2162 |
|
2163 | }, [highlightedIndex]);
|
2164 | return shouldScrollRef;
|
2165 | }
|
2166 |
|
2167 |
|
2168 | var useControlPropsValidator = noop;
|
2169 |
|
2170 |
|
2171 | if (process.env.NODE_ENV !== 'production') {
|
2172 | useControlPropsValidator = function useControlPropsValidator(_ref4) {
|
2173 | var isInitialMount = _ref4.isInitialMount,
|
2174 | props = _ref4.props,
|
2175 | state = _ref4.state;
|
2176 |
|
2177 | var prevPropsRef = useRef(props);
|
2178 | useEffect(function () {
|
2179 | if (isInitialMount) {
|
2180 | return;
|
2181 | }
|
2182 |
|
2183 | validateControlledUnchanged(state, prevPropsRef.current, props);
|
2184 | prevPropsRef.current = props;
|
2185 | }, [state, props, isInitialMount]);
|
2186 | };
|
2187 | }
|
2188 |
|
2189 |
|
2190 |
|
2191 | function downshiftCommonReducer(state, action, stateChangeTypes) {
|
2192 | var type = action.type,
|
2193 | props = action.props;
|
2194 | var changes;
|
2195 |
|
2196 | switch (type) {
|
2197 | case stateChangeTypes.ItemMouseMove:
|
2198 | changes = {
|
2199 | highlightedIndex: action.index
|
2200 | };
|
2201 | break;
|
2202 |
|
2203 | case stateChangeTypes.MenuMouseLeave:
|
2204 | changes = {
|
2205 | highlightedIndex: -1
|
2206 | };
|
2207 | break;
|
2208 |
|
2209 | case stateChangeTypes.ToggleButtonClick:
|
2210 | case stateChangeTypes.FunctionToggleMenu:
|
2211 | changes = {
|
2212 | isOpen: !state.isOpen,
|
2213 | highlightedIndex: state.isOpen ? -1 : getHighlightedIndexOnOpen(props, state, 0)
|
2214 | };
|
2215 | break;
|
2216 |
|
2217 | case stateChangeTypes.FunctionOpenMenu:
|
2218 | changes = {
|
2219 | isOpen: true,
|
2220 | highlightedIndex: getHighlightedIndexOnOpen(props, state, 0)
|
2221 | };
|
2222 | break;
|
2223 |
|
2224 | case stateChangeTypes.FunctionCloseMenu:
|
2225 | changes = {
|
2226 | isOpen: false
|
2227 | };
|
2228 | break;
|
2229 |
|
2230 | case stateChangeTypes.FunctionSetHighlightedIndex:
|
2231 | changes = {
|
2232 | highlightedIndex: action.highlightedIndex
|
2233 | };
|
2234 | break;
|
2235 |
|
2236 | case stateChangeTypes.FunctionSetInputValue:
|
2237 | changes = {
|
2238 | inputValue: action.inputValue
|
2239 | };
|
2240 | break;
|
2241 |
|
2242 | case stateChangeTypes.FunctionReset:
|
2243 | changes = {
|
2244 | highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
2245 | isOpen: getDefaultValue$1(props, 'isOpen'),
|
2246 | selectedItem: getDefaultValue$1(props, 'selectedItem'),
|
2247 | inputValue: getDefaultValue$1(props, 'inputValue')
|
2248 | };
|
2249 | break;
|
2250 |
|
2251 | default:
|
2252 | throw new Error('Reducer called without proper action type.');
|
2253 | }
|
2254 |
|
2255 | return _extends({}, state, changes);
|
2256 | }
|
2257 |
|
2258 |
|
2259 | function getItemIndexByCharacterKey(_a) {
|
2260 | var keysSoFar = _a.keysSoFar, highlightedIndex = _a.highlightedIndex, items = _a.items, itemToString = _a.itemToString, getItemNodeFromIndex = _a.getItemNodeFromIndex;
|
2261 | var lowerCasedKeysSoFar = keysSoFar.toLowerCase();
|
2262 | for (var index = 0; index < items.length; index++) {
|
2263 | var offsetIndex = (index + highlightedIndex + 1) % items.length;
|
2264 | var item = items[offsetIndex];
|
2265 | if (item !== undefined &&
|
2266 | itemToString(item)
|
2267 | .toLowerCase()
|
2268 | .startsWith(lowerCasedKeysSoFar)) {
|
2269 | var element = getItemNodeFromIndex(offsetIndex);
|
2270 | if (!(element === null || element === void 0 ? void 0 : element.hasAttribute('disabled'))) {
|
2271 | return offsetIndex;
|
2272 | }
|
2273 | }
|
2274 | }
|
2275 | return highlightedIndex;
|
2276 | }
|
2277 | var propTypes$2 = {
|
2278 | items: PropTypes.array.isRequired,
|
2279 | itemToString: PropTypes.func,
|
2280 | getA11yStatusMessage: PropTypes.func,
|
2281 | getA11ySelectionMessage: PropTypes.func,
|
2282 | circularNavigation: PropTypes.bool,
|
2283 | highlightedIndex: PropTypes.number,
|
2284 | defaultHighlightedIndex: PropTypes.number,
|
2285 | initialHighlightedIndex: PropTypes.number,
|
2286 | isOpen: PropTypes.bool,
|
2287 | defaultIsOpen: PropTypes.bool,
|
2288 | initialIsOpen: PropTypes.bool,
|
2289 | selectedItem: PropTypes.any,
|
2290 | initialSelectedItem: PropTypes.any,
|
2291 | defaultSelectedItem: PropTypes.any,
|
2292 | id: PropTypes.string,
|
2293 | labelId: PropTypes.string,
|
2294 | menuId: PropTypes.string,
|
2295 | getItemId: PropTypes.func,
|
2296 | toggleButtonId: PropTypes.string,
|
2297 | stateReducer: PropTypes.func,
|
2298 | onSelectedItemChange: PropTypes.func,
|
2299 | onHighlightedIndexChange: PropTypes.func,
|
2300 | onStateChange: PropTypes.func,
|
2301 | onIsOpenChange: PropTypes.func,
|
2302 | environment: PropTypes.shape({
|
2303 | addEventListener: PropTypes.func,
|
2304 | removeEventListener: PropTypes.func,
|
2305 | document: PropTypes.shape({
|
2306 | getElementById: PropTypes.func,
|
2307 | activeElement: PropTypes.any,
|
2308 | body: PropTypes.any
|
2309 | })
|
2310 | })
|
2311 | };
|
2312 |
|
2313 |
|
2314 |
|
2315 |
|
2316 |
|
2317 |
|
2318 |
|
2319 |
|
2320 | function getA11yStatusMessage(_a) {
|
2321 | var isOpen = _a.isOpen, resultCount = _a.resultCount, previousResultCount = _a.previousResultCount;
|
2322 | if (!isOpen) {
|
2323 | return '';
|
2324 | }
|
2325 | if (!resultCount) {
|
2326 | return 'No results are available.';
|
2327 | }
|
2328 | if (resultCount !== previousResultCount) {
|
2329 | return resultCount + " result" + (resultCount === 1 ? ' is' : 's are') + " available, use up and down arrow keys to navigate. Press Enter or Space Bar keys to select.";
|
2330 | }
|
2331 | return '';
|
2332 | }
|
2333 | var defaultProps$2 = __assign(__assign({}, defaultProps$3), { getA11yStatusMessage: getA11yStatusMessage });
|
2334 |
|
2335 | var validatePropTypes$2 = noop;
|
2336 |
|
2337 | if (process.env.NODE_ENV !== 'production') {
|
2338 | validatePropTypes$2 = function (options, caller) {
|
2339 | PropTypes.checkPropTypes(propTypes$2, options, 'prop', caller.name);
|
2340 | };
|
2341 | }
|
2342 |
|
2343 | var MenuKeyDownArrowDown = process.env.NODE_ENV !== "production" ? '__menu_keydown_arrow_down__' : 0;
|
2344 | var MenuKeyDownArrowUp = process.env.NODE_ENV !== "production" ? '__menu_keydown_arrow_up__' : 1;
|
2345 | var MenuKeyDownEscape = process.env.NODE_ENV !== "production" ? '__menu_keydown_escape__' : 2;
|
2346 | var MenuKeyDownHome = process.env.NODE_ENV !== "production" ? '__menu_keydown_home__' : 3;
|
2347 | var MenuKeyDownEnd = process.env.NODE_ENV !== "production" ? '__menu_keydown_end__' : 4;
|
2348 | var MenuKeyDownEnter = process.env.NODE_ENV !== "production" ? '__menu_keydown_enter__' : 5;
|
2349 | var MenuKeyDownSpaceButton = process.env.NODE_ENV !== "production" ? '__menu_keydown_space_button__' : 6;
|
2350 | var MenuKeyDownCharacter = process.env.NODE_ENV !== "production" ? '__menu_keydown_character__' : 7;
|
2351 | var MenuBlur = process.env.NODE_ENV !== "production" ? '__menu_blur__' : 8;
|
2352 | var MenuMouseLeave$1 = process.env.NODE_ENV !== "production" ? '__menu_mouse_leave__' : 9;
|
2353 | var ItemMouseMove$1 = process.env.NODE_ENV !== "production" ? '__item_mouse_move__' : 10;
|
2354 | var ItemClick$1 = process.env.NODE_ENV !== "production" ? '__item_click__' : 11;
|
2355 | var ToggleButtonClick$1 = process.env.NODE_ENV !== "production" ? '__togglebutton_click__' : 12;
|
2356 | var ToggleButtonKeyDownArrowDown = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_arrow_down__' : 13;
|
2357 | var ToggleButtonKeyDownArrowUp = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_arrow_up__' : 14;
|
2358 | var ToggleButtonKeyDownCharacter = process.env.NODE_ENV !== "production" ? '__togglebutton_keydown_character__' : 15;
|
2359 | var FunctionToggleMenu$1 = process.env.NODE_ENV !== "production" ? '__function_toggle_menu__' : 16;
|
2360 | var FunctionOpenMenu$1 = process.env.NODE_ENV !== "production" ? '__function_open_menu__' : 17;
|
2361 | var FunctionCloseMenu$1 = process.env.NODE_ENV !== "production" ? '__function_close_menu__' : 18;
|
2362 | var FunctionSetHighlightedIndex$1 = process.env.NODE_ENV !== "production" ? '__function_set_highlighted_index__' : 19;
|
2363 | var FunctionSelectItem$1 = process.env.NODE_ENV !== "production" ? '__function_select_item__' : 20;
|
2364 | var FunctionSetInputValue$1 = process.env.NODE_ENV !== "production" ? '__function_set_input_value__' : 21;
|
2365 | var FunctionReset$2 = process.env.NODE_ENV !== "production" ? '__function_reset__' : 22;
|
2366 |
|
2367 | var stateChangeTypes$2 = Object.freeze({
|
2368 | __proto__: null,
|
2369 | MenuKeyDownArrowDown: MenuKeyDownArrowDown,
|
2370 | MenuKeyDownArrowUp: MenuKeyDownArrowUp,
|
2371 | MenuKeyDownEscape: MenuKeyDownEscape,
|
2372 | MenuKeyDownHome: MenuKeyDownHome,
|
2373 | MenuKeyDownEnd: MenuKeyDownEnd,
|
2374 | MenuKeyDownEnter: MenuKeyDownEnter,
|
2375 | MenuKeyDownSpaceButton: MenuKeyDownSpaceButton,
|
2376 | MenuKeyDownCharacter: MenuKeyDownCharacter,
|
2377 | MenuBlur: MenuBlur,
|
2378 | MenuMouseLeave: MenuMouseLeave$1,
|
2379 | ItemMouseMove: ItemMouseMove$1,
|
2380 | ItemClick: ItemClick$1,
|
2381 | ToggleButtonClick: ToggleButtonClick$1,
|
2382 | ToggleButtonKeyDownArrowDown: ToggleButtonKeyDownArrowDown,
|
2383 | ToggleButtonKeyDownArrowUp: ToggleButtonKeyDownArrowUp,
|
2384 | ToggleButtonKeyDownCharacter: ToggleButtonKeyDownCharacter,
|
2385 | FunctionToggleMenu: FunctionToggleMenu$1,
|
2386 | FunctionOpenMenu: FunctionOpenMenu$1,
|
2387 | FunctionCloseMenu: FunctionCloseMenu$1,
|
2388 | FunctionSetHighlightedIndex: FunctionSetHighlightedIndex$1,
|
2389 | FunctionSelectItem: FunctionSelectItem$1,
|
2390 | FunctionSetInputValue: FunctionSetInputValue$1,
|
2391 | FunctionReset: FunctionReset$2
|
2392 | });
|
2393 |
|
2394 |
|
2395 |
|
2396 | function downshiftSelectReducer(state, action) {
|
2397 | var type = action.type,
|
2398 | props = action.props,
|
2399 | shiftKey = action.shiftKey;
|
2400 | var changes;
|
2401 |
|
2402 | switch (type) {
|
2403 | case ItemClick$1:
|
2404 | changes = {
|
2405 | isOpen: getDefaultValue$1(props, 'isOpen'),
|
2406 | highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
2407 | selectedItem: props.items[action.index]
|
2408 | };
|
2409 | break;
|
2410 |
|
2411 | case ToggleButtonKeyDownCharacter:
|
2412 | {
|
2413 | var lowercasedKey = action.key;
|
2414 | var inputValue = "" + state.inputValue + lowercasedKey;
|
2415 | var itemIndex = getItemIndexByCharacterKey({
|
2416 | keysSoFar: inputValue,
|
2417 | highlightedIndex: state.selectedItem ? props.items.indexOf(state.selectedItem) : -1,
|
2418 | items: props.items,
|
2419 | itemToString: props.itemToString,
|
2420 | getItemNodeFromIndex: action.getItemNodeFromIndex
|
2421 | });
|
2422 | changes = _extends({
|
2423 | inputValue: inputValue
|
2424 | }, itemIndex >= 0 && {
|
2425 | selectedItem: props.items[itemIndex]
|
2426 | });
|
2427 | }
|
2428 | break;
|
2429 |
|
2430 | case ToggleButtonKeyDownArrowDown:
|
2431 | changes = {
|
2432 | highlightedIndex: getHighlightedIndexOnOpen(props, state, 1, action.getItemNodeFromIndex),
|
2433 | isOpen: true
|
2434 | };
|
2435 | break;
|
2436 |
|
2437 | case ToggleButtonKeyDownArrowUp:
|
2438 | changes = {
|
2439 | highlightedIndex: getHighlightedIndexOnOpen(props, state, -1, action.getItemNodeFromIndex),
|
2440 | isOpen: true
|
2441 | };
|
2442 | break;
|
2443 |
|
2444 | case MenuKeyDownEnter:
|
2445 | case MenuKeyDownSpaceButton:
|
2446 | changes = _extends({
|
2447 | isOpen: getDefaultValue$1(props, 'isOpen'),
|
2448 | highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
2449 | }, state.highlightedIndex >= 0 && {
|
2450 | selectedItem: props.items[state.highlightedIndex]
|
2451 | });
|
2452 | break;
|
2453 |
|
2454 | case MenuKeyDownHome:
|
2455 | changes = {
|
2456 | highlightedIndex: getNextNonDisabledIndex(1, 0, props.items.length, action.getItemNodeFromIndex, false)
|
2457 | };
|
2458 | break;
|
2459 |
|
2460 | case MenuKeyDownEnd:
|
2461 | changes = {
|
2462 | highlightedIndex: getNextNonDisabledIndex(-1, props.items.length - 1, props.items.length, action.getItemNodeFromIndex, false)
|
2463 | };
|
2464 | break;
|
2465 |
|
2466 | case MenuKeyDownEscape:
|
2467 | changes = {
|
2468 | isOpen: false,
|
2469 | highlightedIndex: -1
|
2470 | };
|
2471 | break;
|
2472 |
|
2473 | case MenuBlur:
|
2474 | changes = {
|
2475 | isOpen: false,
|
2476 | highlightedIndex: -1
|
2477 | };
|
2478 | break;
|
2479 |
|
2480 | case MenuKeyDownCharacter:
|
2481 | {
|
2482 | var _lowercasedKey = action.key;
|
2483 |
|
2484 | var _inputValue = "" + state.inputValue + _lowercasedKey;
|
2485 |
|
2486 | var highlightedIndex = getItemIndexByCharacterKey({
|
2487 | keysSoFar: _inputValue,
|
2488 | highlightedIndex: state.highlightedIndex,
|
2489 | items: props.items,
|
2490 | itemToString: props.itemToString,
|
2491 | getItemNodeFromIndex: action.getItemNodeFromIndex
|
2492 | });
|
2493 | changes = _extends({
|
2494 | inputValue: _inputValue
|
2495 | }, highlightedIndex >= 0 && {
|
2496 | highlightedIndex: highlightedIndex
|
2497 | });
|
2498 | }
|
2499 | break;
|
2500 |
|
2501 | case MenuKeyDownArrowDown:
|
2502 | changes = {
|
2503 | highlightedIndex: getNextWrappingIndex(shiftKey ? 5 : 1, state.highlightedIndex, props.items.length, action.getItemNodeFromIndex, props.circularNavigation)
|
2504 | };
|
2505 | break;
|
2506 |
|
2507 | case MenuKeyDownArrowUp:
|
2508 | changes = {
|
2509 | highlightedIndex: getNextWrappingIndex(shiftKey ? -5 : -1, state.highlightedIndex, props.items.length, action.getItemNodeFromIndex, props.circularNavigation)
|
2510 | };
|
2511 | break;
|
2512 |
|
2513 | case FunctionSelectItem$1:
|
2514 | changes = {
|
2515 | selectedItem: action.selectedItem
|
2516 | };
|
2517 | break;
|
2518 |
|
2519 | default:
|
2520 | return downshiftCommonReducer(state, action, stateChangeTypes$2);
|
2521 | }
|
2522 |
|
2523 | return _extends({}, state, changes);
|
2524 | }
|
2525 |
|
2526 |
|
2527 | var _excluded$2 = ["onMouseLeave", "refKey", "onKeyDown", "onBlur", "ref"],
|
2528 | _excluded2$2 = ["onClick", "onKeyDown", "refKey", "ref"],
|
2529 | _excluded3$1 = ["item", "index", "onMouseMove", "onClick", "refKey", "ref"];
|
2530 | useSelect.stateChangeTypes = stateChangeTypes$2;
|
2531 |
|
2532 | function useSelect(userProps) {
|
2533 | if (userProps === void 0) {
|
2534 | userProps = {};
|
2535 | }
|
2536 |
|
2537 | validatePropTypes$2(userProps, useSelect);
|
2538 |
|
2539 | var props = _extends({}, defaultProps$2, userProps);
|
2540 |
|
2541 | var items = props.items,
|
2542 | scrollIntoView = props.scrollIntoView,
|
2543 | environment = props.environment,
|
2544 | initialIsOpen = props.initialIsOpen,
|
2545 | defaultIsOpen = props.defaultIsOpen,
|
2546 | itemToString = props.itemToString,
|
2547 | getA11ySelectionMessage = props.getA11ySelectionMessage,
|
2548 | getA11yStatusMessage = props.getA11yStatusMessage;
|
2549 |
|
2550 | var initialState = getInitialState$2(props);
|
2551 |
|
2552 | var _useControlledReducer = useControlledReducer$1(downshiftSelectReducer, initialState, props),
|
2553 | state = _useControlledReducer[0],
|
2554 | dispatch = _useControlledReducer[1];
|
2555 |
|
2556 | var isOpen = state.isOpen,
|
2557 | highlightedIndex = state.highlightedIndex,
|
2558 | selectedItem = state.selectedItem,
|
2559 | inputValue = state.inputValue;
|
2560 |
|
2561 | var toggleButtonRef = useRef(null);
|
2562 | var menuRef = useRef(null);
|
2563 | var itemRefs = useRef({});
|
2564 |
|
2565 | var shouldBlurRef = useRef(true);
|
2566 |
|
2567 | var clearTimeoutRef = useRef(null);
|
2568 |
|
2569 | var elementIds = useElementIds(props);
|
2570 |
|
2571 | var previousResultCountRef = useRef();
|
2572 | var isInitialMountRef = useRef(true);
|
2573 |
|
2574 | var latest = useLatestRef({
|
2575 | state: state,
|
2576 | props: props
|
2577 | });
|
2578 |
|
2579 | var getItemNodeFromIndex = useCallback(function (index) {
|
2580 | return itemRefs.current[elementIds.getItemId(index)];
|
2581 | }, [elementIds]);
|
2582 |
|
2583 |
|
2584 | useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items], _extends({
|
2585 | isInitialMount: isInitialMountRef.current,
|
2586 | previousResultCount: previousResultCountRef.current,
|
2587 | items: items,
|
2588 | environment: environment,
|
2589 | itemToString: itemToString
|
2590 | }, state));
|
2591 |
|
2592 | useA11yMessageSetter(getA11ySelectionMessage, [selectedItem], _extends({
|
2593 | isInitialMount: isInitialMountRef.current,
|
2594 | previousResultCount: previousResultCountRef.current,
|
2595 | items: items,
|
2596 | environment: environment,
|
2597 | itemToString: itemToString
|
2598 | }, state));
|
2599 |
|
2600 | var shouldScrollRef = useScrollIntoView({
|
2601 | menuElement: menuRef.current,
|
2602 | highlightedIndex: highlightedIndex,
|
2603 | isOpen: isOpen,
|
2604 | itemRefs: itemRefs,
|
2605 | scrollIntoView: scrollIntoView,
|
2606 | getItemNodeFromIndex: getItemNodeFromIndex
|
2607 | });
|
2608 |
|
2609 | useEffect(function () {
|
2610 |
|
2611 | clearTimeoutRef.current = debounce(function (outerDispatch) {
|
2612 | outerDispatch({
|
2613 | type: FunctionSetInputValue$1,
|
2614 | inputValue: ''
|
2615 | });
|
2616 | }, 500);
|
2617 |
|
2618 | return function () {
|
2619 | clearTimeoutRef.current.cancel();
|
2620 | };
|
2621 | }, []);
|
2622 |
|
2623 | useEffect(function () {
|
2624 | if (!inputValue) {
|
2625 | return;
|
2626 | }
|
2627 |
|
2628 | clearTimeoutRef.current(dispatch);
|
2629 | }, [dispatch, inputValue]);
|
2630 | useControlPropsValidator({
|
2631 | isInitialMount: isInitialMountRef.current,
|
2632 | props: props,
|
2633 | state: state
|
2634 | });
|
2635 |
|
2636 |
|
2637 | useEffect(function () {
|
2638 |
|
2639 | if (isInitialMountRef.current) {
|
2640 |
|
2641 | if ((initialIsOpen || defaultIsOpen || isOpen) && menuRef.current) {
|
2642 | menuRef.current.focus();
|
2643 | }
|
2644 |
|
2645 | return;
|
2646 | }
|
2647 |
|
2648 |
|
2649 | if (isOpen) {
|
2650 |
|
2651 | if (menuRef.current) {
|
2652 | menuRef.current.focus();
|
2653 | }
|
2654 |
|
2655 | return;
|
2656 | }
|
2657 |
|
2658 |
|
2659 | if (environment.document.activeElement === menuRef.current) {
|
2660 |
|
2661 | if (toggleButtonRef.current) {
|
2662 | shouldBlurRef.current = false;
|
2663 | toggleButtonRef.current.focus();
|
2664 | }
|
2665 | }
|
2666 |
|
2667 | }, [isOpen]);
|
2668 | useEffect(function () {
|
2669 | if (isInitialMountRef.current) {
|
2670 | return;
|
2671 | }
|
2672 |
|
2673 | previousResultCountRef.current = items.length;
|
2674 | });
|
2675 |
|
2676 | var mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [menuRef, toggleButtonRef], environment, function () {
|
2677 | dispatch({
|
2678 | type: MenuBlur
|
2679 | });
|
2680 | });
|
2681 | var setGetterPropCallInfo = useGetterPropsCalledChecker('getMenuProps', 'getToggleButtonProps');
|
2682 |
|
2683 | useEffect(function () {
|
2684 | isInitialMountRef.current = false;
|
2685 | }, []);
|
2686 |
|
2687 | useEffect(function () {
|
2688 | if (!isOpen) {
|
2689 | itemRefs.current = {};
|
2690 | }
|
2691 | }, [isOpen]);
|
2692 |
|
2693 | var toggleButtonKeyDownHandlers = useMemo(function () {
|
2694 | return {
|
2695 | ArrowDown: function ArrowDown(event) {
|
2696 | event.preventDefault();
|
2697 | dispatch({
|
2698 | type: ToggleButtonKeyDownArrowDown,
|
2699 | getItemNodeFromIndex: getItemNodeFromIndex,
|
2700 | shiftKey: event.shiftKey
|
2701 | });
|
2702 | },
|
2703 | ArrowUp: function ArrowUp(event) {
|
2704 | event.preventDefault();
|
2705 | dispatch({
|
2706 | type: ToggleButtonKeyDownArrowUp,
|
2707 | getItemNodeFromIndex: getItemNodeFromIndex,
|
2708 | shiftKey: event.shiftKey
|
2709 | });
|
2710 | }
|
2711 | };
|
2712 | }, [dispatch, getItemNodeFromIndex]);
|
2713 | var menuKeyDownHandlers = useMemo(function () {
|
2714 | return {
|
2715 | ArrowDown: function ArrowDown(event) {
|
2716 | event.preventDefault();
|
2717 | dispatch({
|
2718 | type: MenuKeyDownArrowDown,
|
2719 | getItemNodeFromIndex: getItemNodeFromIndex,
|
2720 | shiftKey: event.shiftKey
|
2721 | });
|
2722 | },
|
2723 | ArrowUp: function ArrowUp(event) {
|
2724 | event.preventDefault();
|
2725 | dispatch({
|
2726 | type: MenuKeyDownArrowUp,
|
2727 | getItemNodeFromIndex: getItemNodeFromIndex,
|
2728 | shiftKey: event.shiftKey
|
2729 | });
|
2730 | },
|
2731 | Home: function Home(event) {
|
2732 | event.preventDefault();
|
2733 | dispatch({
|
2734 | type: MenuKeyDownHome,
|
2735 | getItemNodeFromIndex: getItemNodeFromIndex
|
2736 | });
|
2737 | },
|
2738 | End: function End(event) {
|
2739 | event.preventDefault();
|
2740 | dispatch({
|
2741 | type: MenuKeyDownEnd,
|
2742 | getItemNodeFromIndex: getItemNodeFromIndex
|
2743 | });
|
2744 | },
|
2745 | Escape: function Escape() {
|
2746 | dispatch({
|
2747 | type: MenuKeyDownEscape
|
2748 | });
|
2749 | },
|
2750 | Enter: function Enter(event) {
|
2751 | event.preventDefault();
|
2752 | dispatch({
|
2753 | type: MenuKeyDownEnter
|
2754 | });
|
2755 | },
|
2756 | ' ': function _(event) {
|
2757 | event.preventDefault();
|
2758 | dispatch({
|
2759 | type: MenuKeyDownSpaceButton
|
2760 | });
|
2761 | }
|
2762 | };
|
2763 | }, [dispatch, getItemNodeFromIndex]);
|
2764 |
|
2765 | var toggleMenu = useCallback(function () {
|
2766 | dispatch({
|
2767 | type: FunctionToggleMenu$1
|
2768 | });
|
2769 | }, [dispatch]);
|
2770 | var closeMenu = useCallback(function () {
|
2771 | dispatch({
|
2772 | type: FunctionCloseMenu$1
|
2773 | });
|
2774 | }, [dispatch]);
|
2775 | var openMenu = useCallback(function () {
|
2776 | dispatch({
|
2777 | type: FunctionOpenMenu$1
|
2778 | });
|
2779 | }, [dispatch]);
|
2780 | var setHighlightedIndex = useCallback(function (newHighlightedIndex) {
|
2781 | dispatch({
|
2782 | type: FunctionSetHighlightedIndex$1,
|
2783 | highlightedIndex: newHighlightedIndex
|
2784 | });
|
2785 | }, [dispatch]);
|
2786 | var selectItem = useCallback(function (newSelectedItem) {
|
2787 | dispatch({
|
2788 | type: FunctionSelectItem$1,
|
2789 | selectedItem: newSelectedItem
|
2790 | });
|
2791 | }, [dispatch]);
|
2792 | var reset = useCallback(function () {
|
2793 | dispatch({
|
2794 | type: FunctionReset$2
|
2795 | });
|
2796 | }, [dispatch]);
|
2797 | var setInputValue = useCallback(function (newInputValue) {
|
2798 | dispatch({
|
2799 | type: FunctionSetInputValue$1,
|
2800 | inputValue: newInputValue
|
2801 | });
|
2802 | }, [dispatch]);
|
2803 |
|
2804 | var getLabelProps = useCallback(function (labelProps) {
|
2805 | return _extends({
|
2806 | id: elementIds.labelId,
|
2807 | htmlFor: elementIds.toggleButtonId
|
2808 | }, labelProps);
|
2809 | }, [elementIds]);
|
2810 | var getMenuProps = useCallback(function (_temp, _temp2) {
|
2811 | var _extends2;
|
2812 |
|
2813 | var _ref = _temp === void 0 ? {} : _temp,
|
2814 | onMouseLeave = _ref.onMouseLeave,
|
2815 | _ref$refKey = _ref.refKey,
|
2816 | refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
|
2817 | onKeyDown = _ref.onKeyDown,
|
2818 | onBlur = _ref.onBlur,
|
2819 | ref = _ref.ref,
|
2820 | rest = _objectWithoutPropertiesLoose(_ref, _excluded$2);
|
2821 |
|
2822 | var _ref2 = _temp2 === void 0 ? {} : _temp2,
|
2823 | _ref2$suppressRefErro = _ref2.suppressRefError,
|
2824 | suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
|
2825 |
|
2826 | var latestState = latest.current.state;
|
2827 |
|
2828 | var menuHandleKeyDown = function menuHandleKeyDown(event) {
|
2829 | var key = normalizeArrowKey(event);
|
2830 |
|
2831 | if (key && menuKeyDownHandlers[key]) {
|
2832 | menuKeyDownHandlers[key](event);
|
2833 | } else if (isAcceptedCharacterKey(key)) {
|
2834 | dispatch({
|
2835 | type: MenuKeyDownCharacter,
|
2836 | key: key,
|
2837 | getItemNodeFromIndex: getItemNodeFromIndex
|
2838 | });
|
2839 | }
|
2840 | };
|
2841 |
|
2842 | var menuHandleBlur = function menuHandleBlur() {
|
2843 |
|
2844 | if (shouldBlurRef.current === false) {
|
2845 | shouldBlurRef.current = true;
|
2846 | return;
|
2847 | }
|
2848 |
|
2849 | var shouldBlur = !mouseAndTouchTrackersRef.current.isMouseDown;
|
2850 |
|
2851 |
|
2852 | if (shouldBlur) {
|
2853 | dispatch({
|
2854 | type: MenuBlur
|
2855 | });
|
2856 | }
|
2857 | };
|
2858 |
|
2859 | var menuHandleMouseLeave = function menuHandleMouseLeave() {
|
2860 | dispatch({
|
2861 | type: MenuMouseLeave$1
|
2862 | });
|
2863 | };
|
2864 |
|
2865 | setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
|
2866 | return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
|
2867 | menuRef.current = menuNode;
|
2868 | }), _extends2.id = elementIds.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = elementIds.labelId, _extends2.tabIndex = -1, _extends2), latestState.isOpen && latestState.highlightedIndex > -1 && {
|
2869 | 'aria-activedescendant': elementIds.getItemId(latestState.highlightedIndex)
|
2870 | }, {
|
2871 | onMouseLeave: callAllEventHandlers(onMouseLeave, menuHandleMouseLeave),
|
2872 | onKeyDown: callAllEventHandlers(onKeyDown, menuHandleKeyDown),
|
2873 | onBlur: callAllEventHandlers(onBlur, menuHandleBlur)
|
2874 | }, rest);
|
2875 | }, [dispatch, latest, menuKeyDownHandlers, mouseAndTouchTrackersRef, setGetterPropCallInfo, elementIds, getItemNodeFromIndex]);
|
2876 | var getToggleButtonProps = useCallback(function (_temp3, _temp4) {
|
2877 | var _extends3;
|
2878 |
|
2879 | var _ref3 = _temp3 === void 0 ? {} : _temp3,
|
2880 | onClick = _ref3.onClick,
|
2881 | onKeyDown = _ref3.onKeyDown,
|
2882 | _ref3$refKey = _ref3.refKey,
|
2883 | refKey = _ref3$refKey === void 0 ? 'ref' : _ref3$refKey,
|
2884 | ref = _ref3.ref,
|
2885 | rest = _objectWithoutPropertiesLoose(_ref3, _excluded2$2);
|
2886 |
|
2887 | var _ref4 = _temp4 === void 0 ? {} : _temp4,
|
2888 | _ref4$suppressRefErro = _ref4.suppressRefError,
|
2889 | suppressRefError = _ref4$suppressRefErro === void 0 ? false : _ref4$suppressRefErro;
|
2890 |
|
2891 | var toggleButtonHandleClick = function toggleButtonHandleClick() {
|
2892 | dispatch({
|
2893 | type: ToggleButtonClick$1
|
2894 | });
|
2895 | };
|
2896 |
|
2897 | var toggleButtonHandleKeyDown = function toggleButtonHandleKeyDown(event) {
|
2898 | var key = normalizeArrowKey(event);
|
2899 |
|
2900 | if (key && toggleButtonKeyDownHandlers[key]) {
|
2901 | toggleButtonKeyDownHandlers[key](event);
|
2902 | } else if (isAcceptedCharacterKey(key)) {
|
2903 | dispatch({
|
2904 | type: ToggleButtonKeyDownCharacter,
|
2905 | key: key,
|
2906 | getItemNodeFromIndex: getItemNodeFromIndex
|
2907 | });
|
2908 | }
|
2909 | };
|
2910 |
|
2911 | var toggleProps = _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (toggleButtonNode) {
|
2912 | toggleButtonRef.current = toggleButtonNode;
|
2913 | }), _extends3.id = elementIds.toggleButtonId, _extends3['aria-haspopup'] = 'listbox', _extends3['aria-expanded'] = latest.current.state.isOpen, _extends3['aria-labelledby'] = elementIds.labelId + " " + elementIds.toggleButtonId, _extends3), rest);
|
2914 |
|
2915 | if (!rest.disabled) {
|
2916 | toggleProps.onClick = callAllEventHandlers(onClick, toggleButtonHandleClick);
|
2917 | toggleProps.onKeyDown = callAllEventHandlers(onKeyDown, toggleButtonHandleKeyDown);
|
2918 | }
|
2919 |
|
2920 | setGetterPropCallInfo('getToggleButtonProps', suppressRefError, refKey, toggleButtonRef);
|
2921 | return toggleProps;
|
2922 | }, [dispatch, latest, toggleButtonKeyDownHandlers, setGetterPropCallInfo, elementIds, getItemNodeFromIndex]);
|
2923 | var getItemProps = useCallback(function (_temp5) {
|
2924 | var _extends4;
|
2925 |
|
2926 | var _ref5 = _temp5 === void 0 ? {} : _temp5,
|
2927 | item = _ref5.item,
|
2928 | index = _ref5.index,
|
2929 | onMouseMove = _ref5.onMouseMove,
|
2930 | onClick = _ref5.onClick,
|
2931 | _ref5$refKey = _ref5.refKey,
|
2932 | refKey = _ref5$refKey === void 0 ? 'ref' : _ref5$refKey,
|
2933 | ref = _ref5.ref,
|
2934 | rest = _objectWithoutPropertiesLoose(_ref5, _excluded3$1);
|
2935 |
|
2936 | var _latest$current = latest.current,
|
2937 | latestState = _latest$current.state,
|
2938 | latestProps = _latest$current.props;
|
2939 |
|
2940 | var itemHandleMouseMove = function itemHandleMouseMove() {
|
2941 | if (index === latestState.highlightedIndex) {
|
2942 | return;
|
2943 | }
|
2944 |
|
2945 | shouldScrollRef.current = false;
|
2946 | dispatch({
|
2947 | type: ItemMouseMove$1,
|
2948 | index: index
|
2949 | });
|
2950 | };
|
2951 |
|
2952 | var itemHandleClick = function itemHandleClick() {
|
2953 | dispatch({
|
2954 | type: ItemClick$1,
|
2955 | index: index
|
2956 | });
|
2957 | };
|
2958 |
|
2959 | var itemIndex = getItemIndex(index, item, latestProps.items);
|
2960 |
|
2961 | if (itemIndex < 0) {
|
2962 | throw new Error('Pass either item or item index in getItemProps!');
|
2963 | }
|
2964 |
|
2965 | var itemProps = _extends((_extends4 = {
|
2966 | role: 'option',
|
2967 | 'aria-selected': "" + (itemIndex === latestState.highlightedIndex),
|
2968 | id: elementIds.getItemId(itemIndex)
|
2969 | }, _extends4[refKey] = handleRefs(ref, function (itemNode) {
|
2970 | if (itemNode) {
|
2971 | itemRefs.current[elementIds.getItemId(itemIndex)] = itemNode;
|
2972 | }
|
2973 | }), _extends4), rest);
|
2974 |
|
2975 | if (!rest.disabled) {
|
2976 | itemProps.onMouseMove = callAllEventHandlers(onMouseMove, itemHandleMouseMove);
|
2977 | itemProps.onClick = callAllEventHandlers(onClick, itemHandleClick);
|
2978 | }
|
2979 |
|
2980 | return itemProps;
|
2981 | }, [dispatch, latest, shouldScrollRef, elementIds]);
|
2982 | return {
|
2983 |
|
2984 | getToggleButtonProps: getToggleButtonProps,
|
2985 | getLabelProps: getLabelProps,
|
2986 | getMenuProps: getMenuProps,
|
2987 | getItemProps: getItemProps,
|
2988 |
|
2989 | toggleMenu: toggleMenu,
|
2990 | openMenu: openMenu,
|
2991 | closeMenu: closeMenu,
|
2992 | setHighlightedIndex: setHighlightedIndex,
|
2993 | selectItem: selectItem,
|
2994 | reset: reset,
|
2995 | setInputValue: setInputValue,
|
2996 |
|
2997 | highlightedIndex: highlightedIndex,
|
2998 | isOpen: isOpen,
|
2999 | selectedItem: selectedItem,
|
3000 | inputValue: inputValue
|
3001 | };
|
3002 | }
|
3003 |
|
3004 | var InputKeyDownArrowDown = process.env.NODE_ENV !== "production" ? '__input_keydown_arrow_down__' : 0;
|
3005 | var InputKeyDownArrowUp = process.env.NODE_ENV !== "production" ? '__input_keydown_arrow_up__' : 1;
|
3006 | var InputKeyDownEscape = process.env.NODE_ENV !== "production" ? '__input_keydown_escape__' : 2;
|
3007 | var InputKeyDownHome = process.env.NODE_ENV !== "production" ? '__input_keydown_home__' : 3;
|
3008 | var InputKeyDownEnd = process.env.NODE_ENV !== "production" ? '__input_keydown_end__' : 4;
|
3009 | var InputKeyDownEnter = process.env.NODE_ENV !== "production" ? '__input_keydown_enter__' : 5;
|
3010 | var InputChange = process.env.NODE_ENV !== "production" ? '__input_change__' : 6;
|
3011 | var InputBlur = process.env.NODE_ENV !== "production" ? '__input_blur__' : 7;
|
3012 | var MenuMouseLeave = process.env.NODE_ENV !== "production" ? '__menu_mouse_leave__' : 8;
|
3013 | var ItemMouseMove = process.env.NODE_ENV !== "production" ? '__item_mouse_move__' : 9;
|
3014 | var ItemClick = process.env.NODE_ENV !== "production" ? '__item_click__' : 10;
|
3015 | var ToggleButtonClick = process.env.NODE_ENV !== "production" ? '__togglebutton_click__' : 11;
|
3016 | var FunctionToggleMenu = process.env.NODE_ENV !== "production" ? '__function_toggle_menu__' : 12;
|
3017 | var FunctionOpenMenu = process.env.NODE_ENV !== "production" ? '__function_open_menu__' : 13;
|
3018 | var FunctionCloseMenu = process.env.NODE_ENV !== "production" ? '__function_close_menu__' : 14;
|
3019 | var FunctionSetHighlightedIndex = process.env.NODE_ENV !== "production" ? '__function_set_highlighted_index__' : 15;
|
3020 | var FunctionSelectItem = process.env.NODE_ENV !== "production" ? '__function_select_item__' : 16;
|
3021 | var FunctionSetInputValue = process.env.NODE_ENV !== "production" ? '__function_set_input_value__' : 17;
|
3022 | var FunctionReset$1 = process.env.NODE_ENV !== "production" ? '__function_reset__' : 18;
|
3023 | var ControlledPropUpdatedSelectedItem = process.env.NODE_ENV !== "production" ? '__controlled_prop_updated_selected_item__' : 19;
|
3024 |
|
3025 | var stateChangeTypes$1 = Object.freeze({
|
3026 | __proto__: null,
|
3027 | InputKeyDownArrowDown: InputKeyDownArrowDown,
|
3028 | InputKeyDownArrowUp: InputKeyDownArrowUp,
|
3029 | InputKeyDownEscape: InputKeyDownEscape,
|
3030 | InputKeyDownHome: InputKeyDownHome,
|
3031 | InputKeyDownEnd: InputKeyDownEnd,
|
3032 | InputKeyDownEnter: InputKeyDownEnter,
|
3033 | InputChange: InputChange,
|
3034 | InputBlur: InputBlur,
|
3035 | MenuMouseLeave: MenuMouseLeave,
|
3036 | ItemMouseMove: ItemMouseMove,
|
3037 | ItemClick: ItemClick,
|
3038 | ToggleButtonClick: ToggleButtonClick,
|
3039 | FunctionToggleMenu: FunctionToggleMenu,
|
3040 | FunctionOpenMenu: FunctionOpenMenu,
|
3041 | FunctionCloseMenu: FunctionCloseMenu,
|
3042 | FunctionSetHighlightedIndex: FunctionSetHighlightedIndex,
|
3043 | FunctionSelectItem: FunctionSelectItem,
|
3044 | FunctionSetInputValue: FunctionSetInputValue,
|
3045 | FunctionReset: FunctionReset$1,
|
3046 | ControlledPropUpdatedSelectedItem: ControlledPropUpdatedSelectedItem
|
3047 | });
|
3048 |
|
3049 | function getInitialState$1(props) {
|
3050 | var initialState = getInitialState$2(props);
|
3051 | var selectedItem = initialState.selectedItem;
|
3052 | var inputValue = initialState.inputValue;
|
3053 |
|
3054 | if (inputValue === '' && selectedItem && props.defaultInputValue === undefined && props.initialInputValue === undefined && props.inputValue === undefined) {
|
3055 | inputValue = props.itemToString(selectedItem);
|
3056 | }
|
3057 |
|
3058 | return _extends({}, initialState, {
|
3059 | inputValue: inputValue
|
3060 | });
|
3061 | }
|
3062 |
|
3063 | var propTypes$1 = {
|
3064 | items: PropTypes.array.isRequired,
|
3065 | itemToString: PropTypes.func,
|
3066 | getA11yStatusMessage: PropTypes.func,
|
3067 | getA11ySelectionMessage: PropTypes.func,
|
3068 | circularNavigation: PropTypes.bool,
|
3069 | highlightedIndex: PropTypes.number,
|
3070 | defaultHighlightedIndex: PropTypes.number,
|
3071 | initialHighlightedIndex: PropTypes.number,
|
3072 | isOpen: PropTypes.bool,
|
3073 | defaultIsOpen: PropTypes.bool,
|
3074 | initialIsOpen: PropTypes.bool,
|
3075 | selectedItem: PropTypes.any,
|
3076 | initialSelectedItem: PropTypes.any,
|
3077 | defaultSelectedItem: PropTypes.any,
|
3078 | inputValue: PropTypes.string,
|
3079 | defaultInputValue: PropTypes.string,
|
3080 | initialInputValue: PropTypes.string,
|
3081 | id: PropTypes.string,
|
3082 | labelId: PropTypes.string,
|
3083 | menuId: PropTypes.string,
|
3084 | getItemId: PropTypes.func,
|
3085 | inputId: PropTypes.string,
|
3086 | toggleButtonId: PropTypes.string,
|
3087 | stateReducer: PropTypes.func,
|
3088 | onSelectedItemChange: PropTypes.func,
|
3089 | onHighlightedIndexChange: PropTypes.func,
|
3090 | onStateChange: PropTypes.func,
|
3091 | onIsOpenChange: PropTypes.func,
|
3092 | onInputValueChange: PropTypes.func,
|
3093 | environment: PropTypes.shape({
|
3094 | addEventListener: PropTypes.func,
|
3095 | removeEventListener: PropTypes.func,
|
3096 | document: PropTypes.shape({
|
3097 | getElementById: PropTypes.func,
|
3098 | activeElement: PropTypes.any,
|
3099 | body: PropTypes.any
|
3100 | })
|
3101 | })
|
3102 | };
|
3103 |
|
3104 |
|
3105 |
|
3106 |
|
3107 |
|
3108 |
|
3109 |
|
3110 |
|
3111 |
|
3112 |
|
3113 |
|
3114 |
|
3115 |
|
3116 | function useControlledReducer(reducer, initialState, props) {
|
3117 | var previousSelectedItemRef = useRef();
|
3118 |
|
3119 | var _useEnhancedReducer = useEnhancedReducer(reducer, initialState, props),
|
3120 | state = _useEnhancedReducer[0],
|
3121 | dispatch = _useEnhancedReducer[1];
|
3122 |
|
3123 |
|
3124 | useEffect(function () {
|
3125 | if (isControlledProp(props, 'selectedItem')) {
|
3126 | if (previousSelectedItemRef.current !== props.selectedItem) {
|
3127 | dispatch({
|
3128 | type: ControlledPropUpdatedSelectedItem,
|
3129 | inputValue: props.itemToString(props.selectedItem)
|
3130 | });
|
3131 | }
|
3132 |
|
3133 | previousSelectedItemRef.current = state.selectedItem === previousSelectedItemRef.current ? props.selectedItem : state.selectedItem;
|
3134 | }
|
3135 | });
|
3136 | return [getState(state, props), dispatch];
|
3137 | }
|
3138 |
|
3139 |
|
3140 | var validatePropTypes$1 = noop;
|
3141 |
|
3142 |
|
3143 | if (process.env.NODE_ENV !== 'production') {
|
3144 | validatePropTypes$1 = function validatePropTypes(options, caller) {
|
3145 | PropTypes.checkPropTypes(propTypes$1, options, 'prop', caller.name);
|
3146 | };
|
3147 | }
|
3148 |
|
3149 | var defaultProps$1 = _extends({}, defaultProps$3, {
|
3150 | getA11yStatusMessage: getA11yStatusMessage$1,
|
3151 | circularNavigation: true
|
3152 | });
|
3153 |
|
3154 |
|
3155 |
|
3156 | function downshiftUseComboboxReducer(state, action) {
|
3157 | var type = action.type,
|
3158 | props = action.props,
|
3159 | shiftKey = action.shiftKey;
|
3160 | var changes;
|
3161 |
|
3162 | switch (type) {
|
3163 | case ItemClick:
|
3164 | changes = {
|
3165 | isOpen: getDefaultValue$1(props, 'isOpen'),
|
3166 | highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
3167 | selectedItem: props.items[action.index],
|
3168 | inputValue: props.itemToString(props.items[action.index])
|
3169 | };
|
3170 | break;
|
3171 |
|
3172 | case InputKeyDownArrowDown:
|
3173 | if (state.isOpen) {
|
3174 | changes = {
|
3175 | highlightedIndex: getNextWrappingIndex(shiftKey ? 5 : 1, state.highlightedIndex, props.items.length, action.getItemNodeFromIndex, props.circularNavigation)
|
3176 | };
|
3177 | } else {
|
3178 | changes = {
|
3179 | highlightedIndex: getHighlightedIndexOnOpen(props, state, 1, action.getItemNodeFromIndex),
|
3180 | isOpen: props.items.length >= 0
|
3181 | };
|
3182 | }
|
3183 |
|
3184 | break;
|
3185 |
|
3186 | case InputKeyDownArrowUp:
|
3187 | if (state.isOpen) {
|
3188 | changes = {
|
3189 | highlightedIndex: getNextWrappingIndex(shiftKey ? -5 : -1, state.highlightedIndex, props.items.length, action.getItemNodeFromIndex, props.circularNavigation)
|
3190 | };
|
3191 | } else {
|
3192 | changes = {
|
3193 | highlightedIndex: getHighlightedIndexOnOpen(props, state, -1, action.getItemNodeFromIndex),
|
3194 | isOpen: props.items.length >= 0
|
3195 | };
|
3196 | }
|
3197 |
|
3198 | break;
|
3199 |
|
3200 | case InputKeyDownEnter:
|
3201 | changes = _extends({}, state.isOpen && state.highlightedIndex >= 0 && {
|
3202 | selectedItem: props.items[state.highlightedIndex],
|
3203 | isOpen: getDefaultValue$1(props, 'isOpen'),
|
3204 | highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
3205 | inputValue: props.itemToString(props.items[state.highlightedIndex])
|
3206 | });
|
3207 | break;
|
3208 |
|
3209 | case InputKeyDownEscape:
|
3210 | changes = _extends({
|
3211 | isOpen: false,
|
3212 | highlightedIndex: -1
|
3213 | }, !state.isOpen && {
|
3214 | selectedItem: null,
|
3215 | inputValue: ''
|
3216 | });
|
3217 | break;
|
3218 |
|
3219 | case InputKeyDownHome:
|
3220 | changes = {
|
3221 | highlightedIndex: getNextNonDisabledIndex(1, 0, props.items.length, action.getItemNodeFromIndex, false)
|
3222 | };
|
3223 | break;
|
3224 |
|
3225 | case InputKeyDownEnd:
|
3226 | changes = {
|
3227 | highlightedIndex: getNextNonDisabledIndex(-1, props.items.length - 1, props.items.length, action.getItemNodeFromIndex, false)
|
3228 | };
|
3229 | break;
|
3230 |
|
3231 | case InputBlur:
|
3232 | changes = _extends({
|
3233 | isOpen: false,
|
3234 | highlightedIndex: -1
|
3235 | }, state.highlightedIndex >= 0 && action.selectItem && {
|
3236 | selectedItem: props.items[state.highlightedIndex],
|
3237 | inputValue: props.itemToString(props.items[state.highlightedIndex])
|
3238 | });
|
3239 | break;
|
3240 |
|
3241 | case InputChange:
|
3242 | changes = {
|
3243 | isOpen: true,
|
3244 | highlightedIndex: getDefaultValue$1(props, 'highlightedIndex'),
|
3245 | inputValue: action.inputValue
|
3246 | };
|
3247 | break;
|
3248 |
|
3249 | case FunctionSelectItem:
|
3250 | changes = {
|
3251 | selectedItem: action.selectedItem,
|
3252 | inputValue: props.itemToString(action.selectedItem)
|
3253 | };
|
3254 | break;
|
3255 |
|
3256 | case ControlledPropUpdatedSelectedItem:
|
3257 | changes = {
|
3258 | inputValue: action.inputValue
|
3259 | };
|
3260 | break;
|
3261 |
|
3262 | default:
|
3263 | return downshiftCommonReducer(state, action, stateChangeTypes$1);
|
3264 | }
|
3265 |
|
3266 | return _extends({}, state, changes);
|
3267 | }
|
3268 |
|
3269 |
|
3270 | var _excluded$1 = ["onMouseLeave", "refKey", "ref"],
|
3271 | _excluded2$1 = ["item", "index", "refKey", "ref", "onMouseMove", "onClick", "onPress"],
|
3272 | _excluded3 = ["onClick", "onPress", "refKey", "ref"],
|
3273 | _excluded4 = ["onKeyDown", "onChange", "onInput", "onBlur", "onChangeText", "refKey", "ref"],
|
3274 | _excluded5 = ["refKey", "ref"];
|
3275 | useCombobox.stateChangeTypes = stateChangeTypes$1;
|
3276 |
|
3277 | function useCombobox(userProps) {
|
3278 | if (userProps === void 0) {
|
3279 | userProps = {};
|
3280 | }
|
3281 |
|
3282 | validatePropTypes$1(userProps, useCombobox);
|
3283 |
|
3284 | var props = _extends({}, defaultProps$1, userProps);
|
3285 |
|
3286 | var initialIsOpen = props.initialIsOpen,
|
3287 | defaultIsOpen = props.defaultIsOpen,
|
3288 | items = props.items,
|
3289 | scrollIntoView = props.scrollIntoView,
|
3290 | environment = props.environment,
|
3291 | getA11yStatusMessage = props.getA11yStatusMessage,
|
3292 | getA11ySelectionMessage = props.getA11ySelectionMessage,
|
3293 | itemToString = props.itemToString;
|
3294 |
|
3295 | var initialState = getInitialState$1(props);
|
3296 |
|
3297 | var _useControlledReducer = useControlledReducer(downshiftUseComboboxReducer, initialState, props),
|
3298 | state = _useControlledReducer[0],
|
3299 | dispatch = _useControlledReducer[1];
|
3300 |
|
3301 | var isOpen = state.isOpen,
|
3302 | highlightedIndex = state.highlightedIndex,
|
3303 | selectedItem = state.selectedItem,
|
3304 | inputValue = state.inputValue;
|
3305 |
|
3306 | var menuRef = useRef(null);
|
3307 | var itemRefs = useRef({});
|
3308 | var inputRef = useRef(null);
|
3309 | var toggleButtonRef = useRef(null);
|
3310 | var comboboxRef = useRef(null);
|
3311 | var isInitialMountRef = useRef(true);
|
3312 |
|
3313 | var elementIds = useElementIds(props);
|
3314 |
|
3315 | var previousResultCountRef = useRef();
|
3316 |
|
3317 | var latest = useLatestRef({
|
3318 | state: state,
|
3319 | props: props
|
3320 | });
|
3321 | var getItemNodeFromIndex = useCallback(function (index) {
|
3322 | return itemRefs.current[elementIds.getItemId(index)];
|
3323 | }, [elementIds]);
|
3324 |
|
3325 |
|
3326 | useA11yMessageSetter(getA11yStatusMessage, [isOpen, highlightedIndex, inputValue, items], _extends({
|
3327 | isInitialMount: isInitialMountRef.current,
|
3328 | previousResultCount: previousResultCountRef.current,
|
3329 | items: items,
|
3330 | environment: environment,
|
3331 | itemToString: itemToString
|
3332 | }, state));
|
3333 |
|
3334 | useA11yMessageSetter(getA11ySelectionMessage, [selectedItem], _extends({
|
3335 | isInitialMount: isInitialMountRef.current,
|
3336 | previousResultCount: previousResultCountRef.current,
|
3337 | items: items,
|
3338 | environment: environment,
|
3339 | itemToString: itemToString
|
3340 | }, state));
|
3341 |
|
3342 | var shouldScrollRef = useScrollIntoView({
|
3343 | menuElement: menuRef.current,
|
3344 | highlightedIndex: highlightedIndex,
|
3345 | isOpen: isOpen,
|
3346 | itemRefs: itemRefs,
|
3347 | scrollIntoView: scrollIntoView,
|
3348 | getItemNodeFromIndex: getItemNodeFromIndex
|
3349 | });
|
3350 | useControlPropsValidator({
|
3351 | isInitialMount: isInitialMountRef.current,
|
3352 | props: props,
|
3353 | state: state
|
3354 | });
|
3355 |
|
3356 | useEffect(function () {
|
3357 | var focusOnOpen = initialIsOpen || defaultIsOpen || isOpen;
|
3358 |
|
3359 | if (focusOnOpen && inputRef.current) {
|
3360 | inputRef.current.focus();
|
3361 | }
|
3362 |
|
3363 | }, []);
|
3364 | useEffect(function () {
|
3365 | if (isInitialMountRef.current) {
|
3366 | return;
|
3367 | }
|
3368 |
|
3369 | previousResultCountRef.current = items.length;
|
3370 | });
|
3371 |
|
3372 | var mouseAndTouchTrackersRef = useMouseAndTouchTracker(isOpen, [comboboxRef, menuRef, toggleButtonRef], environment, function () {
|
3373 | dispatch({
|
3374 | type: InputBlur,
|
3375 | selectItem: false
|
3376 | });
|
3377 | });
|
3378 | var setGetterPropCallInfo = useGetterPropsCalledChecker('getInputProps', 'getComboboxProps', 'getMenuProps');
|
3379 |
|
3380 | useEffect(function () {
|
3381 | isInitialMountRef.current = false;
|
3382 | }, []);
|
3383 |
|
3384 | useEffect(function () {
|
3385 | if (!isOpen) {
|
3386 | itemRefs.current = {};
|
3387 | }
|
3388 | }, [isOpen]);
|
3389 |
|
3390 |
|
3391 | var inputKeyDownHandlers = useMemo(function () {
|
3392 | return {
|
3393 | ArrowDown: function ArrowDown(event) {
|
3394 | event.preventDefault();
|
3395 | dispatch({
|
3396 | type: InputKeyDownArrowDown,
|
3397 | shiftKey: event.shiftKey,
|
3398 | getItemNodeFromIndex: getItemNodeFromIndex
|
3399 | });
|
3400 | },
|
3401 | ArrowUp: function ArrowUp(event) {
|
3402 | event.preventDefault();
|
3403 | dispatch({
|
3404 | type: InputKeyDownArrowUp,
|
3405 | shiftKey: event.shiftKey,
|
3406 | getItemNodeFromIndex: getItemNodeFromIndex
|
3407 | });
|
3408 | },
|
3409 | Home: function Home(event) {
|
3410 | if (!latest.current.state.isOpen) {
|
3411 | return;
|
3412 | }
|
3413 |
|
3414 | event.preventDefault();
|
3415 | dispatch({
|
3416 | type: InputKeyDownHome,
|
3417 | getItemNodeFromIndex: getItemNodeFromIndex
|
3418 | });
|
3419 | },
|
3420 | End: function End(event) {
|
3421 | if (!latest.current.state.isOpen) {
|
3422 | return;
|
3423 | }
|
3424 |
|
3425 | event.preventDefault();
|
3426 | dispatch({
|
3427 | type: InputKeyDownEnd,
|
3428 | getItemNodeFromIndex: getItemNodeFromIndex
|
3429 | });
|
3430 | },
|
3431 | Escape: function Escape() {
|
3432 | var latestState = latest.current.state;
|
3433 |
|
3434 | if (latestState.isOpen || latestState.inputValue || latestState.selectedItem || latestState.highlightedIndex > -1) {
|
3435 | dispatch({
|
3436 | type: InputKeyDownEscape
|
3437 | });
|
3438 | }
|
3439 | },
|
3440 | Enter: function Enter(event) {
|
3441 | var latestState = latest.current.state;
|
3442 |
|
3443 | if (!latestState.isOpen || latestState.highlightedIndex < 0 || event.which === 229
|
3444 | ) {
|
3445 | return;
|
3446 | }
|
3447 |
|
3448 | event.preventDefault();
|
3449 | dispatch({
|
3450 | type: InputKeyDownEnter,
|
3451 | getItemNodeFromIndex: getItemNodeFromIndex
|
3452 | });
|
3453 | }
|
3454 | };
|
3455 | }, [dispatch, latest, getItemNodeFromIndex]);
|
3456 |
|
3457 | var getLabelProps = useCallback(function (labelProps) {
|
3458 | return _extends({
|
3459 | id: elementIds.labelId,
|
3460 | htmlFor: elementIds.inputId
|
3461 | }, labelProps);
|
3462 | }, [elementIds]);
|
3463 | var getMenuProps = useCallback(function (_temp, _temp2) {
|
3464 | var _extends2;
|
3465 |
|
3466 | var _ref = _temp === void 0 ? {} : _temp,
|
3467 | onMouseLeave = _ref.onMouseLeave,
|
3468 | _ref$refKey = _ref.refKey,
|
3469 | refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
|
3470 | ref = _ref.ref,
|
3471 | rest = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
3472 |
|
3473 | var _ref2 = _temp2 === void 0 ? {} : _temp2,
|
3474 | _ref2$suppressRefErro = _ref2.suppressRefError,
|
3475 | suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
|
3476 |
|
3477 | setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
|
3478 | return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
|
3479 | menuRef.current = menuNode;
|
3480 | }), _extends2.id = elementIds.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = elementIds.labelId, _extends2.onMouseLeave = callAllEventHandlers(onMouseLeave, function () {
|
3481 | dispatch({
|
3482 | type: MenuMouseLeave
|
3483 | });
|
3484 | }), _extends2), rest);
|
3485 | }, [dispatch, setGetterPropCallInfo, elementIds]);
|
3486 | var getItemProps = useCallback(function (_temp3) {
|
3487 | var _extends3, _ref4;
|
3488 |
|
3489 | var _ref3 = _temp3 === void 0 ? {} : _temp3,
|
3490 | item = _ref3.item,
|
3491 | index = _ref3.index,
|
3492 | _ref3$refKey = _ref3.refKey,
|
3493 | refKey = _ref3$refKey === void 0 ? 'ref' : _ref3$refKey,
|
3494 | ref = _ref3.ref,
|
3495 | onMouseMove = _ref3.onMouseMove,
|
3496 | onClick = _ref3.onClick;
|
3497 | _ref3.onPress;
|
3498 | var rest = _objectWithoutPropertiesLoose(_ref3, _excluded2$1);
|
3499 |
|
3500 | var _latest$current = latest.current,
|
3501 | latestProps = _latest$current.props,
|
3502 | latestState = _latest$current.state;
|
3503 | var itemIndex = getItemIndex(index, item, latestProps.items);
|
3504 |
|
3505 | if (itemIndex < 0) {
|
3506 | throw new Error('Pass either item or item index in getItemProps!');
|
3507 | }
|
3508 |
|
3509 | var onSelectKey = 'onClick';
|
3510 | var customClickHandler = onClick;
|
3511 |
|
3512 | var itemHandleMouseMove = function itemHandleMouseMove() {
|
3513 | if (index === latestState.highlightedIndex) {
|
3514 | return;
|
3515 | }
|
3516 |
|
3517 | shouldScrollRef.current = false;
|
3518 | dispatch({
|
3519 | type: ItemMouseMove,
|
3520 | index: index
|
3521 | });
|
3522 | };
|
3523 |
|
3524 | var itemHandleClick = function itemHandleClick() {
|
3525 | dispatch({
|
3526 | type: ItemClick,
|
3527 | index: index
|
3528 | });
|
3529 |
|
3530 | if (inputRef.current) {
|
3531 | inputRef.current.focus();
|
3532 | }
|
3533 | };
|
3534 |
|
3535 | return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (itemNode) {
|
3536 | if (itemNode) {
|
3537 | itemRefs.current[elementIds.getItemId(itemIndex)] = itemNode;
|
3538 | }
|
3539 | }), _extends3.role = 'option', _extends3['aria-selected'] = "" + (itemIndex === latestState.highlightedIndex), _extends3.id = elementIds.getItemId(itemIndex), _extends3), !rest.disabled && (_ref4 = {
|
3540 | onMouseMove: callAllEventHandlers(onMouseMove, itemHandleMouseMove)
|
3541 | }, _ref4[onSelectKey] = callAllEventHandlers(customClickHandler, itemHandleClick), _ref4), rest);
|
3542 | }, [dispatch, latest, shouldScrollRef, elementIds]);
|
3543 | var getToggleButtonProps = useCallback(function (_temp4) {
|
3544 | var _extends4;
|
3545 |
|
3546 | var _ref5 = _temp4 === void 0 ? {} : _temp4,
|
3547 | onClick = _ref5.onClick;
|
3548 | _ref5.onPress;
|
3549 | var _ref5$refKey = _ref5.refKey,
|
3550 | refKey = _ref5$refKey === void 0 ? 'ref' : _ref5$refKey,
|
3551 | ref = _ref5.ref,
|
3552 | rest = _objectWithoutPropertiesLoose(_ref5, _excluded3);
|
3553 |
|
3554 | var toggleButtonHandleClick = function toggleButtonHandleClick() {
|
3555 | dispatch({
|
3556 | type: ToggleButtonClick
|
3557 | });
|
3558 |
|
3559 | if (!latest.current.state.isOpen && inputRef.current) {
|
3560 | inputRef.current.focus();
|
3561 | }
|
3562 | };
|
3563 |
|
3564 | return _extends((_extends4 = {}, _extends4[refKey] = handleRefs(ref, function (toggleButtonNode) {
|
3565 | toggleButtonRef.current = toggleButtonNode;
|
3566 | }), _extends4.id = elementIds.toggleButtonId, _extends4.tabIndex = -1, _extends4), !rest.disabled && _extends({}, {
|
3567 | onClick: callAllEventHandlers(onClick, toggleButtonHandleClick)
|
3568 | }), rest);
|
3569 | }, [dispatch, latest, elementIds]);
|
3570 | var getInputProps = useCallback(function (_temp5, _temp6) {
|
3571 | var _extends5;
|
3572 |
|
3573 | var _ref6 = _temp5 === void 0 ? {} : _temp5,
|
3574 | onKeyDown = _ref6.onKeyDown,
|
3575 | onChange = _ref6.onChange,
|
3576 | onInput = _ref6.onInput,
|
3577 | onBlur = _ref6.onBlur;
|
3578 | _ref6.onChangeText;
|
3579 | var _ref6$refKey = _ref6.refKey,
|
3580 | refKey = _ref6$refKey === void 0 ? 'ref' : _ref6$refKey,
|
3581 | ref = _ref6.ref,
|
3582 | rest = _objectWithoutPropertiesLoose(_ref6, _excluded4);
|
3583 |
|
3584 | var _ref7 = _temp6 === void 0 ? {} : _temp6,
|
3585 | _ref7$suppressRefErro = _ref7.suppressRefError,
|
3586 | suppressRefError = _ref7$suppressRefErro === void 0 ? false : _ref7$suppressRefErro;
|
3587 |
|
3588 | setGetterPropCallInfo('getInputProps', suppressRefError, refKey, inputRef);
|
3589 | var latestState = latest.current.state;
|
3590 |
|
3591 | var inputHandleKeyDown = function inputHandleKeyDown(event) {
|
3592 | var key = normalizeArrowKey(event);
|
3593 |
|
3594 | if (key && inputKeyDownHandlers[key]) {
|
3595 | inputKeyDownHandlers[key](event);
|
3596 | }
|
3597 | };
|
3598 |
|
3599 | var inputHandleChange = function inputHandleChange(event) {
|
3600 | dispatch({
|
3601 | type: InputChange,
|
3602 | inputValue: event.target.value
|
3603 | });
|
3604 | };
|
3605 |
|
3606 | var inputHandleBlur = function inputHandleBlur() {
|
3607 |
|
3608 | if (latestState.isOpen && !mouseAndTouchTrackersRef.current.isMouseDown) {
|
3609 | dispatch({
|
3610 | type: InputBlur,
|
3611 | selectItem: true
|
3612 | });
|
3613 | }
|
3614 | };
|
3615 |
|
3616 |
|
3617 |
|
3618 | var onChangeKey = 'onChange';
|
3619 | var eventHandlers = {};
|
3620 |
|
3621 | if (!rest.disabled) {
|
3622 | var _eventHandlers;
|
3623 |
|
3624 | eventHandlers = (_eventHandlers = {}, _eventHandlers[onChangeKey] = callAllEventHandlers(onChange, onInput, inputHandleChange), _eventHandlers.onKeyDown = callAllEventHandlers(onKeyDown, inputHandleKeyDown), _eventHandlers.onBlur = callAllEventHandlers(onBlur, inputHandleBlur), _eventHandlers);
|
3625 | }
|
3626 |
|
3627 | return _extends((_extends5 = {}, _extends5[refKey] = handleRefs(ref, function (inputNode) {
|
3628 | inputRef.current = inputNode;
|
3629 | }), _extends5.id = elementIds.inputId, _extends5['aria-autocomplete'] = 'list', _extends5['aria-controls'] = elementIds.menuId, _extends5), latestState.isOpen && latestState.highlightedIndex > -1 && {
|
3630 | 'aria-activedescendant': elementIds.getItemId(latestState.highlightedIndex)
|
3631 | }, {
|
3632 | 'aria-labelledby': elementIds.labelId,
|
3633 |
|
3634 |
|
3635 | autoComplete: 'off',
|
3636 | value: latestState.inputValue
|
3637 | }, eventHandlers, rest);
|
3638 | }, [dispatch, inputKeyDownHandlers, latest, mouseAndTouchTrackersRef, setGetterPropCallInfo, elementIds]);
|
3639 | var getComboboxProps = useCallback(function (_temp7, _temp8) {
|
3640 | var _extends6;
|
3641 |
|
3642 | var _ref8 = _temp7 === void 0 ? {} : _temp7,
|
3643 | _ref8$refKey = _ref8.refKey,
|
3644 | refKey = _ref8$refKey === void 0 ? 'ref' : _ref8$refKey,
|
3645 | ref = _ref8.ref,
|
3646 | rest = _objectWithoutPropertiesLoose(_ref8, _excluded5);
|
3647 |
|
3648 | var _ref9 = _temp8 === void 0 ? {} : _temp8,
|
3649 | _ref9$suppressRefErro = _ref9.suppressRefError,
|
3650 | suppressRefError = _ref9$suppressRefErro === void 0 ? false : _ref9$suppressRefErro;
|
3651 |
|
3652 | setGetterPropCallInfo('getComboboxProps', suppressRefError, refKey, comboboxRef);
|
3653 | return _extends((_extends6 = {}, _extends6[refKey] = handleRefs(ref, function (comboboxNode) {
|
3654 | comboboxRef.current = comboboxNode;
|
3655 | }), _extends6.role = 'combobox', _extends6['aria-haspopup'] = 'listbox', _extends6['aria-owns'] = elementIds.menuId, _extends6['aria-expanded'] = latest.current.state.isOpen, _extends6), rest);
|
3656 | }, [latest, setGetterPropCallInfo, elementIds]);
|
3657 |
|
3658 | var toggleMenu = useCallback(function () {
|
3659 | dispatch({
|
3660 | type: FunctionToggleMenu
|
3661 | });
|
3662 | }, [dispatch]);
|
3663 | var closeMenu = useCallback(function () {
|
3664 | dispatch({
|
3665 | type: FunctionCloseMenu
|
3666 | });
|
3667 | }, [dispatch]);
|
3668 | var openMenu = useCallback(function () {
|
3669 | dispatch({
|
3670 | type: FunctionOpenMenu
|
3671 | });
|
3672 | }, [dispatch]);
|
3673 | var setHighlightedIndex = useCallback(function (newHighlightedIndex) {
|
3674 | dispatch({
|
3675 | type: FunctionSetHighlightedIndex,
|
3676 | highlightedIndex: newHighlightedIndex
|
3677 | });
|
3678 | }, [dispatch]);
|
3679 | var selectItem = useCallback(function (newSelectedItem) {
|
3680 | dispatch({
|
3681 | type: FunctionSelectItem,
|
3682 | selectedItem: newSelectedItem
|
3683 | });
|
3684 | }, [dispatch]);
|
3685 | var setInputValue = useCallback(function (newInputValue) {
|
3686 | dispatch({
|
3687 | type: FunctionSetInputValue,
|
3688 | inputValue: newInputValue
|
3689 | });
|
3690 | }, [dispatch]);
|
3691 | var reset = useCallback(function () {
|
3692 | dispatch({
|
3693 | type: FunctionReset$1
|
3694 | });
|
3695 | }, [dispatch]);
|
3696 | return {
|
3697 |
|
3698 | getItemProps: getItemProps,
|
3699 | getLabelProps: getLabelProps,
|
3700 | getMenuProps: getMenuProps,
|
3701 | getInputProps: getInputProps,
|
3702 | getComboboxProps: getComboboxProps,
|
3703 | getToggleButtonProps: getToggleButtonProps,
|
3704 |
|
3705 | toggleMenu: toggleMenu,
|
3706 | openMenu: openMenu,
|
3707 | closeMenu: closeMenu,
|
3708 | setHighlightedIndex: setHighlightedIndex,
|
3709 | setInputValue: setInputValue,
|
3710 | selectItem: selectItem,
|
3711 | reset: reset,
|
3712 |
|
3713 | highlightedIndex: highlightedIndex,
|
3714 | isOpen: isOpen,
|
3715 | selectedItem: selectedItem,
|
3716 | inputValue: inputValue
|
3717 | };
|
3718 | }
|
3719 |
|
3720 | var defaultStateValues = {
|
3721 | activeIndex: -1,
|
3722 | selectedItems: []
|
3723 | };
|
3724 |
|
3725 |
|
3726 |
|
3727 |
|
3728 |
|
3729 |
|
3730 |
|
3731 |
|
3732 |
|
3733 |
|
3734 | function getInitialValue(props, propKey) {
|
3735 | return getInitialValue$1(props, propKey, defaultStateValues);
|
3736 | }
|
3737 |
|
3738 |
|
3739 |
|
3740 |
|
3741 |
|
3742 |
|
3743 |
|
3744 |
|
3745 |
|
3746 |
|
3747 | function getDefaultValue(props, propKey) {
|
3748 | return getDefaultValue$1(props, propKey, defaultStateValues);
|
3749 | }
|
3750 |
|
3751 |
|
3752 |
|
3753 |
|
3754 |
|
3755 |
|
3756 |
|
3757 |
|
3758 |
|
3759 | function getInitialState(props) {
|
3760 | var activeIndex = getInitialValue(props, 'activeIndex');
|
3761 | var selectedItems = getInitialValue(props, 'selectedItems');
|
3762 | return {
|
3763 | activeIndex: activeIndex,
|
3764 | selectedItems: selectedItems
|
3765 | };
|
3766 | }
|
3767 |
|
3768 |
|
3769 |
|
3770 |
|
3771 |
|
3772 |
|
3773 |
|
3774 |
|
3775 |
|
3776 |
|
3777 |
|
3778 | function isKeyDownOperationPermitted(event) {
|
3779 | if (event.shiftKey || event.metaKey || event.ctrlKey || event.altKey) {
|
3780 | return false;
|
3781 | }
|
3782 |
|
3783 | var element = event.target;
|
3784 |
|
3785 | if (element instanceof HTMLInputElement &&
|
3786 | element.value !== '' && (element.selectionStart !== 0 || element.selectionEnd !== 0)) {
|
3787 | return false;
|
3788 | }
|
3789 |
|
3790 | return true;
|
3791 | }
|
3792 |
|
3793 |
|
3794 |
|
3795 |
|
3796 |
|
3797 |
|
3798 |
|
3799 |
|
3800 | function getA11yRemovalMessage(selectionParameters) {
|
3801 | var removedSelectedItem = selectionParameters.removedSelectedItem,
|
3802 | itemToStringLocal = selectionParameters.itemToString;
|
3803 | return itemToStringLocal(removedSelectedItem) + " has been removed.";
|
3804 | }
|
3805 |
|
3806 | var propTypes = {
|
3807 | selectedItems: PropTypes.array,
|
3808 | initialSelectedItems: PropTypes.array,
|
3809 | defaultSelectedItems: PropTypes.array,
|
3810 | itemToString: PropTypes.func,
|
3811 | getA11yRemovalMessage: PropTypes.func,
|
3812 | stateReducer: PropTypes.func,
|
3813 | activeIndex: PropTypes.number,
|
3814 | initialActiveIndex: PropTypes.number,
|
3815 | defaultActiveIndex: PropTypes.number,
|
3816 | onActiveIndexChange: PropTypes.func,
|
3817 | onSelectedItemsChange: PropTypes.func,
|
3818 | keyNavigationNext: PropTypes.string,
|
3819 | keyNavigationPrevious: PropTypes.string,
|
3820 | environment: PropTypes.shape({
|
3821 | addEventListener: PropTypes.func,
|
3822 | removeEventListener: PropTypes.func,
|
3823 | document: PropTypes.shape({
|
3824 | getElementById: PropTypes.func,
|
3825 | activeElement: PropTypes.any,
|
3826 | body: PropTypes.any
|
3827 | })
|
3828 | })
|
3829 | };
|
3830 | var defaultProps = {
|
3831 | itemToString: defaultProps$3.itemToString,
|
3832 | stateReducer: defaultProps$3.stateReducer,
|
3833 | environment: defaultProps$3.environment,
|
3834 | getA11yRemovalMessage: getA11yRemovalMessage,
|
3835 | keyNavigationNext: 'ArrowRight',
|
3836 | keyNavigationPrevious: 'ArrowLeft'
|
3837 | };
|
3838 |
|
3839 | var validatePropTypes = noop;
|
3840 |
|
3841 |
|
3842 | if (process.env.NODE_ENV !== 'production') {
|
3843 | validatePropTypes = function validatePropTypes(options, caller) {
|
3844 | PropTypes.checkPropTypes(propTypes, options, 'prop', caller.name);
|
3845 | };
|
3846 | }
|
3847 |
|
3848 | var SelectedItemClick = process.env.NODE_ENV !== "production" ? '__selected_item_click__' : 0;
|
3849 | var SelectedItemKeyDownDelete = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_delete__' : 1;
|
3850 | var SelectedItemKeyDownBackspace = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_backspace__' : 2;
|
3851 | var SelectedItemKeyDownNavigationNext = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_navigation_next__' : 3;
|
3852 | var SelectedItemKeyDownNavigationPrevious = process.env.NODE_ENV !== "production" ? '__selected_item_keydown_navigation_previous__' : 4;
|
3853 | var DropdownKeyDownNavigationPrevious = process.env.NODE_ENV !== "production" ? '__dropdown_keydown_navigation_previous__' : 5;
|
3854 | var DropdownKeyDownBackspace = process.env.NODE_ENV !== "production" ? '__dropdown_keydown_backspace__' : 6;
|
3855 | var DropdownClick = process.env.NODE_ENV !== "production" ? '__dropdown_click__' : 7;
|
3856 | var FunctionAddSelectedItem = process.env.NODE_ENV !== "production" ? '__function_add_selected_item__' : 8;
|
3857 | var FunctionRemoveSelectedItem = process.env.NODE_ENV !== "production" ? '__function_remove_selected_item__' : 9;
|
3858 | var FunctionSetSelectedItems = process.env.NODE_ENV !== "production" ? '__function_set_selected_items__' : 10;
|
3859 | var FunctionSetActiveIndex = process.env.NODE_ENV !== "production" ? '__function_set_active_index__' : 11;
|
3860 | var FunctionReset = process.env.NODE_ENV !== "production" ? '__function_reset__' : 12;
|
3861 |
|
3862 | var stateChangeTypes = Object.freeze({
|
3863 | __proto__: null,
|
3864 | SelectedItemClick: SelectedItemClick,
|
3865 | SelectedItemKeyDownDelete: SelectedItemKeyDownDelete,
|
3866 | SelectedItemKeyDownBackspace: SelectedItemKeyDownBackspace,
|
3867 | SelectedItemKeyDownNavigationNext: SelectedItemKeyDownNavigationNext,
|
3868 | SelectedItemKeyDownNavigationPrevious: SelectedItemKeyDownNavigationPrevious,
|
3869 | DropdownKeyDownNavigationPrevious: DropdownKeyDownNavigationPrevious,
|
3870 | DropdownKeyDownBackspace: DropdownKeyDownBackspace,
|
3871 | DropdownClick: DropdownClick,
|
3872 | FunctionAddSelectedItem: FunctionAddSelectedItem,
|
3873 | FunctionRemoveSelectedItem: FunctionRemoveSelectedItem,
|
3874 | FunctionSetSelectedItems: FunctionSetSelectedItems,
|
3875 | FunctionSetActiveIndex: FunctionSetActiveIndex,
|
3876 | FunctionReset: FunctionReset
|
3877 | });
|
3878 |
|
3879 |
|
3880 |
|
3881 | function downshiftMultipleSelectionReducer(state, action) {
|
3882 | var type = action.type,
|
3883 | index = action.index,
|
3884 | props = action.props,
|
3885 | selectedItem = action.selectedItem;
|
3886 | var activeIndex = state.activeIndex,
|
3887 | selectedItems = state.selectedItems;
|
3888 | var changes;
|
3889 |
|
3890 | switch (type) {
|
3891 | case SelectedItemClick:
|
3892 | changes = {
|
3893 | activeIndex: index
|
3894 | };
|
3895 | break;
|
3896 |
|
3897 | case SelectedItemKeyDownNavigationPrevious:
|
3898 | changes = {
|
3899 | activeIndex: activeIndex - 1 < 0 ? 0 : activeIndex - 1
|
3900 | };
|
3901 | break;
|
3902 |
|
3903 | case SelectedItemKeyDownNavigationNext:
|
3904 | changes = {
|
3905 | activeIndex: activeIndex + 1 >= selectedItems.length ? -1 : activeIndex + 1
|
3906 | };
|
3907 | break;
|
3908 |
|
3909 | case SelectedItemKeyDownBackspace:
|
3910 | case SelectedItemKeyDownDelete:
|
3911 | {
|
3912 | var newActiveIndex = activeIndex;
|
3913 |
|
3914 | if (selectedItems.length === 1) {
|
3915 | newActiveIndex = -1;
|
3916 | } else if (activeIndex === selectedItems.length - 1) {
|
3917 | newActiveIndex = selectedItems.length - 2;
|
3918 | }
|
3919 |
|
3920 | changes = _extends({
|
3921 | selectedItems: [].concat(selectedItems.slice(0, activeIndex), selectedItems.slice(activeIndex + 1))
|
3922 | }, {
|
3923 | activeIndex: newActiveIndex
|
3924 | });
|
3925 | break;
|
3926 | }
|
3927 |
|
3928 | case DropdownKeyDownNavigationPrevious:
|
3929 | changes = {
|
3930 | activeIndex: selectedItems.length - 1
|
3931 | };
|
3932 | break;
|
3933 |
|
3934 | case DropdownKeyDownBackspace:
|
3935 | changes = {
|
3936 | selectedItems: selectedItems.slice(0, selectedItems.length - 1)
|
3937 | };
|
3938 | break;
|
3939 |
|
3940 | case FunctionAddSelectedItem:
|
3941 | changes = {
|
3942 | selectedItems: [].concat(selectedItems, [selectedItem])
|
3943 | };
|
3944 | break;
|
3945 |
|
3946 | case DropdownClick:
|
3947 | changes = {
|
3948 | activeIndex: -1
|
3949 | };
|
3950 | break;
|
3951 |
|
3952 | case FunctionRemoveSelectedItem:
|
3953 | {
|
3954 | var _newActiveIndex = activeIndex;
|
3955 | var selectedItemIndex = selectedItems.indexOf(selectedItem);
|
3956 |
|
3957 | if (selectedItems.length === 1) {
|
3958 | _newActiveIndex = -1;
|
3959 | } else if (selectedItemIndex === selectedItems.length - 1) {
|
3960 | _newActiveIndex = selectedItems.length - 2;
|
3961 | }
|
3962 |
|
3963 | changes = _extends({
|
3964 | selectedItems: [].concat(selectedItems.slice(0, selectedItemIndex), selectedItems.slice(selectedItemIndex + 1))
|
3965 | }, {
|
3966 | activeIndex: _newActiveIndex
|
3967 | });
|
3968 | break;
|
3969 | }
|
3970 |
|
3971 | case FunctionSetSelectedItems:
|
3972 | {
|
3973 | var newSelectedItems = action.selectedItems;
|
3974 | changes = {
|
3975 | selectedItems: newSelectedItems
|
3976 | };
|
3977 | break;
|
3978 | }
|
3979 |
|
3980 | case FunctionSetActiveIndex:
|
3981 | {
|
3982 | var _newActiveIndex2 = action.activeIndex;
|
3983 | changes = {
|
3984 | activeIndex: _newActiveIndex2
|
3985 | };
|
3986 | break;
|
3987 | }
|
3988 |
|
3989 | case FunctionReset:
|
3990 | changes = {
|
3991 | activeIndex: getDefaultValue(props, 'activeIndex'),
|
3992 | selectedItems: getDefaultValue(props, 'selectedItems')
|
3993 | };
|
3994 | break;
|
3995 |
|
3996 | default:
|
3997 | throw new Error('Reducer called without proper action type.');
|
3998 | }
|
3999 |
|
4000 | return _extends({}, state, changes);
|
4001 | }
|
4002 |
|
4003 | var _excluded = ["refKey", "ref", "onClick", "onKeyDown", "selectedItem", "index"],
|
4004 | _excluded2 = ["refKey", "ref", "onKeyDown", "onClick", "preventKeyAction"];
|
4005 | useMultipleSelection.stateChangeTypes = stateChangeTypes;
|
4006 |
|
4007 | function useMultipleSelection(userProps) {
|
4008 | if (userProps === void 0) {
|
4009 | userProps = {};
|
4010 | }
|
4011 |
|
4012 | validatePropTypes(userProps, useMultipleSelection);
|
4013 |
|
4014 | var props = _extends({}, defaultProps, userProps);
|
4015 |
|
4016 | var getA11yRemovalMessage = props.getA11yRemovalMessage,
|
4017 | itemToString = props.itemToString,
|
4018 | environment = props.environment,
|
4019 | keyNavigationNext = props.keyNavigationNext,
|
4020 | keyNavigationPrevious = props.keyNavigationPrevious;
|
4021 |
|
4022 | var _useControlledReducer = useControlledReducer$1(downshiftMultipleSelectionReducer, getInitialState(props), props),
|
4023 | state = _useControlledReducer[0],
|
4024 | dispatch = _useControlledReducer[1];
|
4025 |
|
4026 | var activeIndex = state.activeIndex,
|
4027 | selectedItems = state.selectedItems;
|
4028 |
|
4029 | var isInitialMountRef = useRef(true);
|
4030 | var dropdownRef = useRef(null);
|
4031 | var previousSelectedItemsRef = useRef(selectedItems);
|
4032 | var selectedItemRefs = useRef();
|
4033 | selectedItemRefs.current = [];
|
4034 | var latest = useLatestRef({
|
4035 | state: state,
|
4036 | props: props
|
4037 | });
|
4038 |
|
4039 |
|
4040 |
|
4041 | useEffect(function () {
|
4042 | if (isInitialMountRef.current) {
|
4043 | return;
|
4044 | }
|
4045 |
|
4046 | if (selectedItems.length < previousSelectedItemsRef.current.length) {
|
4047 | var removedSelectedItem = previousSelectedItemsRef.current.find(function (item) {
|
4048 | return selectedItems.indexOf(item) < 0;
|
4049 | });
|
4050 | setStatus(getA11yRemovalMessage({
|
4051 | itemToString: itemToString,
|
4052 | resultCount: selectedItems.length,
|
4053 | removedSelectedItem: removedSelectedItem,
|
4054 | activeIndex: activeIndex,
|
4055 | activeSelectedItem: selectedItems[activeIndex]
|
4056 | }), environment.document);
|
4057 | }
|
4058 |
|
4059 | previousSelectedItemsRef.current = selectedItems;
|
4060 | }, [selectedItems.length]);
|
4061 |
|
4062 | useEffect(function () {
|
4063 | if (isInitialMountRef.current) {
|
4064 | return;
|
4065 | }
|
4066 |
|
4067 | if (activeIndex === -1 && dropdownRef.current) {
|
4068 | dropdownRef.current.focus();
|
4069 | } else if (selectedItemRefs.current[activeIndex]) {
|
4070 | selectedItemRefs.current[activeIndex].focus();
|
4071 | }
|
4072 | }, [activeIndex]);
|
4073 | useControlPropsValidator({
|
4074 | isInitialMount: isInitialMountRef.current,
|
4075 | props: props,
|
4076 | state: state
|
4077 | });
|
4078 | var setGetterPropCallInfo = useGetterPropsCalledChecker('getDropdownProps');
|
4079 |
|
4080 | useEffect(function () {
|
4081 | isInitialMountRef.current = false;
|
4082 | }, []);
|
4083 |
|
4084 | var selectedItemKeyDownHandlers = useMemo(function () {
|
4085 | var _ref;
|
4086 |
|
4087 | return _ref = {}, _ref[keyNavigationPrevious] = function () {
|
4088 | dispatch({
|
4089 | type: SelectedItemKeyDownNavigationPrevious
|
4090 | });
|
4091 | }, _ref[keyNavigationNext] = function () {
|
4092 | dispatch({
|
4093 | type: SelectedItemKeyDownNavigationNext
|
4094 | });
|
4095 | }, _ref.Delete = function Delete() {
|
4096 | dispatch({
|
4097 | type: SelectedItemKeyDownDelete
|
4098 | });
|
4099 | }, _ref.Backspace = function Backspace() {
|
4100 | dispatch({
|
4101 | type: SelectedItemKeyDownBackspace
|
4102 | });
|
4103 | }, _ref;
|
4104 | }, [dispatch, keyNavigationNext, keyNavigationPrevious]);
|
4105 | var dropdownKeyDownHandlers = useMemo(function () {
|
4106 | var _ref2;
|
4107 |
|
4108 | return _ref2 = {}, _ref2[keyNavigationPrevious] = function (event) {
|
4109 | if (isKeyDownOperationPermitted(event)) {
|
4110 | dispatch({
|
4111 | type: DropdownKeyDownNavigationPrevious
|
4112 | });
|
4113 | }
|
4114 | }, _ref2.Backspace = function Backspace(event) {
|
4115 | if (isKeyDownOperationPermitted(event)) {
|
4116 | dispatch({
|
4117 | type: DropdownKeyDownBackspace
|
4118 | });
|
4119 | }
|
4120 | }, _ref2;
|
4121 | }, [dispatch, keyNavigationPrevious]);
|
4122 |
|
4123 | var getSelectedItemProps = useCallback(function (_temp) {
|
4124 | var _extends2;
|
4125 |
|
4126 | var _ref3 = _temp === void 0 ? {} : _temp,
|
4127 | _ref3$refKey = _ref3.refKey,
|
4128 | refKey = _ref3$refKey === void 0 ? 'ref' : _ref3$refKey,
|
4129 | ref = _ref3.ref,
|
4130 | onClick = _ref3.onClick,
|
4131 | onKeyDown = _ref3.onKeyDown,
|
4132 | selectedItem = _ref3.selectedItem,
|
4133 | index = _ref3.index,
|
4134 | rest = _objectWithoutPropertiesLoose(_ref3, _excluded);
|
4135 |
|
4136 | var latestState = latest.current.state;
|
4137 | var itemIndex = getItemIndex(index, selectedItem, latestState.selectedItems);
|
4138 |
|
4139 | if (itemIndex < 0) {
|
4140 | throw new Error('Pass either selectedItem or index in getSelectedItemProps!');
|
4141 | }
|
4142 |
|
4143 | var selectedItemHandleClick = function selectedItemHandleClick() {
|
4144 | dispatch({
|
4145 | type: SelectedItemClick,
|
4146 | index: index
|
4147 | });
|
4148 | };
|
4149 |
|
4150 | var selectedItemHandleKeyDown = function selectedItemHandleKeyDown(event) {
|
4151 | var key = normalizeArrowKey(event);
|
4152 |
|
4153 | if (key && selectedItemKeyDownHandlers[key]) {
|
4154 | selectedItemKeyDownHandlers[key](event);
|
4155 | }
|
4156 | };
|
4157 |
|
4158 | return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (selectedItemNode) {
|
4159 | if (selectedItemNode) {
|
4160 | selectedItemRefs.current.push(selectedItemNode);
|
4161 | }
|
4162 | }), _extends2.tabIndex = index === latestState.activeIndex ? 0 : -1, _extends2.onClick = callAllEventHandlers(onClick, selectedItemHandleClick), _extends2.onKeyDown = callAllEventHandlers(onKeyDown, selectedItemHandleKeyDown), _extends2), rest);
|
4163 | }, [dispatch, latest, selectedItemKeyDownHandlers]);
|
4164 | var getDropdownProps = useCallback(function (_temp2, _temp3) {
|
4165 | var _extends3;
|
4166 |
|
4167 | var _ref4 = _temp2 === void 0 ? {} : _temp2,
|
4168 | _ref4$refKey = _ref4.refKey,
|
4169 | refKey = _ref4$refKey === void 0 ? 'ref' : _ref4$refKey,
|
4170 | ref = _ref4.ref,
|
4171 | onKeyDown = _ref4.onKeyDown,
|
4172 | onClick = _ref4.onClick,
|
4173 | _ref4$preventKeyActio = _ref4.preventKeyAction,
|
4174 | preventKeyAction = _ref4$preventKeyActio === void 0 ? false : _ref4$preventKeyActio,
|
4175 | rest = _objectWithoutPropertiesLoose(_ref4, _excluded2);
|
4176 |
|
4177 | var _ref5 = _temp3 === void 0 ? {} : _temp3,
|
4178 | _ref5$suppressRefErro = _ref5.suppressRefError,
|
4179 | suppressRefError = _ref5$suppressRefErro === void 0 ? false : _ref5$suppressRefErro;
|
4180 |
|
4181 | setGetterPropCallInfo('getDropdownProps', suppressRefError, refKey, dropdownRef);
|
4182 |
|
4183 | var dropdownHandleKeyDown = function dropdownHandleKeyDown(event) {
|
4184 | var key = normalizeArrowKey(event);
|
4185 |
|
4186 | if (key && dropdownKeyDownHandlers[key]) {
|
4187 | dropdownKeyDownHandlers[key](event);
|
4188 | }
|
4189 | };
|
4190 |
|
4191 | var dropdownHandleClick = function dropdownHandleClick() {
|
4192 | dispatch({
|
4193 | type: DropdownClick
|
4194 | });
|
4195 | };
|
4196 |
|
4197 | return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (dropdownNode) {
|
4198 | if (dropdownNode) {
|
4199 | dropdownRef.current = dropdownNode;
|
4200 | }
|
4201 | }), _extends3), !preventKeyAction && {
|
4202 | onKeyDown: callAllEventHandlers(onKeyDown, dropdownHandleKeyDown),
|
4203 | onClick: callAllEventHandlers(onClick, dropdownHandleClick)
|
4204 | }, rest);
|
4205 | }, [dispatch, dropdownKeyDownHandlers, setGetterPropCallInfo]);
|
4206 |
|
4207 | var addSelectedItem = useCallback(function (selectedItem) {
|
4208 | dispatch({
|
4209 | type: FunctionAddSelectedItem,
|
4210 | selectedItem: selectedItem
|
4211 | });
|
4212 | }, [dispatch]);
|
4213 | var removeSelectedItem = useCallback(function (selectedItem) {
|
4214 | dispatch({
|
4215 | type: FunctionRemoveSelectedItem,
|
4216 | selectedItem: selectedItem
|
4217 | });
|
4218 | }, [dispatch]);
|
4219 | var setSelectedItems = useCallback(function (newSelectedItems) {
|
4220 | dispatch({
|
4221 | type: FunctionSetSelectedItems,
|
4222 | selectedItems: newSelectedItems
|
4223 | });
|
4224 | }, [dispatch]);
|
4225 | var setActiveIndex = useCallback(function (newActiveIndex) {
|
4226 | dispatch({
|
4227 | type: FunctionSetActiveIndex,
|
4228 | activeIndex: newActiveIndex
|
4229 | });
|
4230 | }, [dispatch]);
|
4231 | var reset = useCallback(function () {
|
4232 | dispatch({
|
4233 | type: FunctionReset
|
4234 | });
|
4235 | }, [dispatch]);
|
4236 | return {
|
4237 | getSelectedItemProps: getSelectedItemProps,
|
4238 | getDropdownProps: getDropdownProps,
|
4239 | addSelectedItem: addSelectedItem,
|
4240 | removeSelectedItem: removeSelectedItem,
|
4241 | setSelectedItems: setSelectedItems,
|
4242 | setActiveIndex: setActiveIndex,
|
4243 | reset: reset,
|
4244 | selectedItems: selectedItems,
|
4245 | activeIndex: activeIndex
|
4246 | };
|
4247 | }
|
4248 |
|
4249 | export { Downshift$1 as default, resetIdCounter, useCombobox, useMultipleSelection, useSelect };
|