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