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