downshift 9.3.2 → 9.4.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/downshift.cjs.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
|
@@ -101,7 +101,7 @@ function useLatestRef(val) {
|
|
|
101
101
|
return ref;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
function handleRefs
|
|
104
|
+
function handleRefs() {
|
|
105
105
|
for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
106
106
|
refs[_key] = arguments[_key];
|
|
107
107
|
}
|
|
@@ -123,7 +123,7 @@ function handleRefs$1() {
|
|
|
123
123
|
* @param fns the event handler functions
|
|
124
124
|
* @return the event handler to add to an element
|
|
125
125
|
*/
|
|
126
|
-
function callAllEventHandlers
|
|
126
|
+
function callAllEventHandlers() {
|
|
127
127
|
for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
128
128
|
fns[_key] = arguments[_key];
|
|
129
129
|
}
|
|
@@ -145,7 +145,7 @@ function callAllEventHandlers$1() {
|
|
|
145
145
|
* function once after the time given has passed since
|
|
146
146
|
* it was last called.
|
|
147
147
|
*/
|
|
148
|
-
function debounce
|
|
148
|
+
function debounce(fn, time) {
|
|
149
149
|
var timeoutId;
|
|
150
150
|
function cancel() {
|
|
151
151
|
if (timeoutId) {
|
|
@@ -166,7 +166,7 @@ function debounce$1(fn, time) {
|
|
|
166
166
|
return wrapper;
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
var cleanupStatus = debounce
|
|
169
|
+
var cleanupStatus = debounce(function (document) {
|
|
170
170
|
getStatusDiv(document).textContent = '';
|
|
171
171
|
}, 500);
|
|
172
172
|
|
|
@@ -247,6 +247,8 @@ function getState(state, props) {
|
|
|
247
247
|
}
|
|
248
248
|
var keys = Object.keys(state);
|
|
249
249
|
return keys.reduce(function (newState, key) {
|
|
250
|
+
// state keys could be in props, but with value undefined, which means they should be ignored.
|
|
251
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
250
252
|
if (props[key] !== undefined) {
|
|
251
253
|
newState[key] = props[key];
|
|
252
254
|
}
|
|
@@ -278,93 +280,149 @@ function scrollIntoView(node, menuNode) {
|
|
|
278
280
|
}
|
|
279
281
|
|
|
280
282
|
/**
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
* passed.
|
|
285
|
-
* @param {Function} cb the callback
|
|
286
|
-
* @return {Function} a function
|
|
287
|
-
*/
|
|
288
|
-
function cbToCb(cb) {
|
|
289
|
-
return typeof cb === 'function' ? cb : noop;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* @param {HTMLElement} parent the parent node
|
|
294
|
-
* @param {HTMLElement} child the child node
|
|
295
|
-
* @param {Window} environment The window context where downshift renders.
|
|
296
|
-
* @return {Boolean} whether the parent is the child or the child is in the parent
|
|
283
|
+
* Normalizes the 'key' property of a KeyboardEvent in IE/Edge
|
|
284
|
+
* @param event a keyboardEvent object
|
|
285
|
+
* @return keyboard key
|
|
297
286
|
*/
|
|
298
|
-
function
|
|
299
|
-
var
|
|
300
|
-
|
|
287
|
+
function normalizeArrowKey(event) {
|
|
288
|
+
var key = event.key,
|
|
289
|
+
keyCode = event.keyCode;
|
|
290
|
+
/* istanbul ignore next (ie) */
|
|
291
|
+
if (keyCode >= 37 && keyCode <= 40 && !key.startsWith('Arrow')) {
|
|
292
|
+
return "Arrow" + key;
|
|
293
|
+
}
|
|
294
|
+
return key;
|
|
301
295
|
}
|
|
302
296
|
|
|
303
297
|
/**
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
* @param
|
|
308
|
-
* @param
|
|
309
|
-
* @
|
|
298
|
+
* Returns the next non-disabled highlightedIndex value.
|
|
299
|
+
*
|
|
300
|
+
* @param start The current highlightedIndex.
|
|
301
|
+
* @param backwards If true, it will search backwards from the start.
|
|
302
|
+
* @param items The items array.
|
|
303
|
+
* @param isItemDisabled Function that tells if an item is disabled or not.
|
|
304
|
+
* @param circular If the search reaches the end, if it can search again starting from the other end.
|
|
305
|
+
* @returns The next non-disabled index.
|
|
310
306
|
*/
|
|
311
|
-
function
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if (timeoutId) {
|
|
315
|
-
clearTimeout(timeoutId);
|
|
316
|
-
}
|
|
307
|
+
function getNonDisabledIndex(start, backwards, items, isItemDisabled, circular) {
|
|
308
|
+
if (circular === void 0) {
|
|
309
|
+
circular = false;
|
|
317
310
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
311
|
+
var count = items.length;
|
|
312
|
+
if (backwards) {
|
|
313
|
+
for (var index = start; index >= 0; index--) {
|
|
314
|
+
if (!isItemDisabled(items[index], index)) {
|
|
315
|
+
return index;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
} else {
|
|
319
|
+
for (var _index = start; _index < count; _index++) {
|
|
320
|
+
if (!isItemDisabled(items[_index], _index)) {
|
|
321
|
+
return _index;
|
|
322
|
+
}
|
|
321
323
|
}
|
|
322
|
-
cancel();
|
|
323
|
-
timeoutId = setTimeout(function () {
|
|
324
|
-
timeoutId = null;
|
|
325
|
-
fn.apply(void 0, args);
|
|
326
|
-
}, time);
|
|
327
324
|
}
|
|
328
|
-
|
|
329
|
-
|
|
325
|
+
if (circular) {
|
|
326
|
+
return getNonDisabledIndex(backwards ? count - 1 : 0, backwards, items, isItemDisabled);
|
|
327
|
+
}
|
|
328
|
+
return -1;
|
|
330
329
|
}
|
|
331
330
|
|
|
332
331
|
/**
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
* @param
|
|
337
|
-
* @
|
|
332
|
+
* Returns the next non-disabled highlightedIndex value.
|
|
333
|
+
*
|
|
334
|
+
* @param start The current highlightedIndex.
|
|
335
|
+
* @param offset The offset from the current highlightedIndex to start searching.
|
|
336
|
+
* @param items The items array.
|
|
337
|
+
* @param isItemDisabled Function that tells if an item is disabled or not.
|
|
338
|
+
* @param circular If the search reaches the end, if it can search again starting from the other end.
|
|
339
|
+
* @returns The next highlightedIndex.
|
|
338
340
|
*/
|
|
339
|
-
function
|
|
340
|
-
|
|
341
|
-
|
|
341
|
+
function getHighlightedIndex(start, offset, items, isItemDisabled, circular) {
|
|
342
|
+
if (circular === void 0) {
|
|
343
|
+
circular = false;
|
|
342
344
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
345
|
+
var count = items.length;
|
|
346
|
+
if (count === 0) {
|
|
347
|
+
return -1;
|
|
348
|
+
}
|
|
349
|
+
var itemsLastIndex = count - 1;
|
|
350
|
+
if (typeof start !== 'number' || start < 0 || start > itemsLastIndex) {
|
|
351
|
+
start = offset > 0 ? -1 : itemsLastIndex + 1;
|
|
352
|
+
}
|
|
353
|
+
var current = start + offset;
|
|
354
|
+
if (current < 0) {
|
|
355
|
+
current = circular ? itemsLastIndex : 0;
|
|
356
|
+
} else if (current > itemsLastIndex) {
|
|
357
|
+
current = circular ? 0 : itemsLastIndex;
|
|
358
|
+
}
|
|
359
|
+
var highlightedIndex = getNonDisabledIndex(current, offset < 0, items, isItemDisabled, circular);
|
|
360
|
+
if (highlightedIndex === -1) {
|
|
361
|
+
return start >= count ? -1 : start;
|
|
362
|
+
}
|
|
363
|
+
return highlightedIndex;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
367
|
+
|
|
368
|
+
// eslint-disable-next-line import/no-mutable-exports
|
|
369
|
+
var validateControlledUnchanged = noop;
|
|
370
|
+
/* istanbul ignore next */
|
|
371
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
372
|
+
validateControlledUnchanged = function validateControlledUnchanged(state, prevProps, nextProps) {
|
|
373
|
+
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";
|
|
374
|
+
Object.keys(state).forEach(function (propKey) {
|
|
375
|
+
if (prevProps[propKey] !== undefined && nextProps[propKey] === undefined) {
|
|
376
|
+
// eslint-disable-next-line no-console
|
|
377
|
+
console.error("downshift: A component has changed the controlled prop \"" + propKey + "\" to be uncontrolled. " + warningDescription);
|
|
378
|
+
} else if (prevProps[propKey] === undefined && nextProps[propKey] !== undefined) {
|
|
379
|
+
// eslint-disable-next-line no-console
|
|
380
|
+
console.error("downshift: A component has changed the uncontrolled prop \"" + propKey + "\" to be controlled. " + warningDescription);
|
|
350
381
|
}
|
|
351
|
-
return event.preventDownshiftDefault || event.hasOwnProperty('nativeEvent') && event.nativeEvent.preventDownshiftDefault;
|
|
352
382
|
});
|
|
353
383
|
};
|
|
354
384
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Checks if event target is within the downshift elements.
|
|
388
|
+
*
|
|
389
|
+
* @param target Target to check.
|
|
390
|
+
* @param downshiftElements The elements that form downshift (list, toggle button etc).
|
|
391
|
+
* @param environment The window context where downshift renders.
|
|
392
|
+
* @param checkActiveElement Whether to also check activeElement.
|
|
393
|
+
*
|
|
394
|
+
* @returns Whether or not the target is within downshift elements.
|
|
395
|
+
*/
|
|
396
|
+
function targetWithinDownshift(target, downshiftElements, environment, checkActiveElement) {
|
|
397
|
+
if (checkActiveElement === void 0) {
|
|
398
|
+
checkActiveElement = true;
|
|
358
399
|
}
|
|
359
|
-
return function (
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
400
|
+
return !!environment && downshiftElements.some(function (contextNode) {
|
|
401
|
+
return contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment));
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* @param parent the parent node
|
|
407
|
+
* @param child the child node
|
|
408
|
+
* @param environment The window context where downshift renders.
|
|
409
|
+
* @return Whether the parent is the child or the child is in the parent
|
|
410
|
+
*/
|
|
411
|
+
function isOrContainsNode(parent, child, environment) {
|
|
412
|
+
var result = parent === child || child instanceof environment.Node && parent.contains(child);
|
|
413
|
+
return result;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Accepts a parameter and returns it if it's a function
|
|
418
|
+
* or a noop function if it's not. This allows us to
|
|
419
|
+
* accept a callback, but not worry about it if it's not
|
|
420
|
+
* passed.
|
|
421
|
+
* @param {Function} cb the callback
|
|
422
|
+
* @return {Function} a function
|
|
423
|
+
*/
|
|
424
|
+
function cbToCb(cb) {
|
|
425
|
+
return typeof cb === 'function' ? cb : noop;
|
|
368
426
|
}
|
|
369
427
|
|
|
370
428
|
/**
|
|
@@ -460,34 +518,6 @@ function pickState(state) {
|
|
|
460
518
|
return result;
|
|
461
519
|
}
|
|
462
520
|
|
|
463
|
-
/**
|
|
464
|
-
* This determines whether a prop is a "controlled prop" meaning it is
|
|
465
|
-
* state which is controlled by the outside of this component rather
|
|
466
|
-
* than within this component.
|
|
467
|
-
*
|
|
468
|
-
* @param {Object} props The props that may contain controlled values.
|
|
469
|
-
* @param {String} key the key to check
|
|
470
|
-
* @return {Boolean} whether it is a controlled controlled prop
|
|
471
|
-
*/
|
|
472
|
-
function isControlledProp(props, key) {
|
|
473
|
-
return props[key] !== undefined;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
/**
|
|
477
|
-
* Normalizes the 'key' property of a KeyboardEvent in IE/Edge
|
|
478
|
-
* @param {Object} event a keyboardEvent object
|
|
479
|
-
* @return {String} keyboard key
|
|
480
|
-
*/
|
|
481
|
-
function normalizeArrowKey(event) {
|
|
482
|
-
var key = event.key,
|
|
483
|
-
keyCode = event.keyCode;
|
|
484
|
-
/* istanbul ignore next (ie) */
|
|
485
|
-
if (keyCode >= 37 && keyCode <= 40 && key.indexOf('Arrow') !== 0) {
|
|
486
|
-
return "Arrow" + key;
|
|
487
|
-
}
|
|
488
|
-
return key;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
521
|
/**
|
|
492
522
|
* Simple check if the value passed is object literal
|
|
493
523
|
* @param {*} obj any things
|
|
@@ -497,112 +527,6 @@ function isPlainObject(obj) {
|
|
|
497
527
|
return Object.prototype.toString.call(obj) === '[object Object]';
|
|
498
528
|
}
|
|
499
529
|
|
|
500
|
-
/**
|
|
501
|
-
* Returns the next non-disabled highlightedIndex value.
|
|
502
|
-
*
|
|
503
|
-
* @param {number} start The current highlightedIndex.
|
|
504
|
-
* @param {number} offset The offset from the current highlightedIndex to start searching.
|
|
505
|
-
* @param {unknown[]} items The items array.
|
|
506
|
-
* @param {(item: unknown, index: number) => boolean} isItemDisabled Function that tells if an item is disabled or not.
|
|
507
|
-
* @param {boolean?} circular If the search reaches the end, if it can search again starting from the other end.
|
|
508
|
-
* @returns {number} The next highlightedIndex.
|
|
509
|
-
*/
|
|
510
|
-
function getHighlightedIndex(start, offset, items, isItemDisabled, circular) {
|
|
511
|
-
if (circular === void 0) {
|
|
512
|
-
circular = false;
|
|
513
|
-
}
|
|
514
|
-
var count = items.length;
|
|
515
|
-
if (count === 0) {
|
|
516
|
-
return -1;
|
|
517
|
-
}
|
|
518
|
-
var itemsLastIndex = count - 1;
|
|
519
|
-
if (typeof start !== 'number' || start < 0 || start > itemsLastIndex) {
|
|
520
|
-
start = offset > 0 ? -1 : itemsLastIndex + 1;
|
|
521
|
-
}
|
|
522
|
-
var current = start + offset;
|
|
523
|
-
if (current < 0) {
|
|
524
|
-
current = circular ? itemsLastIndex : 0;
|
|
525
|
-
} else if (current > itemsLastIndex) {
|
|
526
|
-
current = circular ? 0 : itemsLastIndex;
|
|
527
|
-
}
|
|
528
|
-
var highlightedIndex = getNonDisabledIndex(current, offset < 0, items, isItemDisabled, circular);
|
|
529
|
-
if (highlightedIndex === -1) {
|
|
530
|
-
return start >= count ? -1 : start;
|
|
531
|
-
}
|
|
532
|
-
return highlightedIndex;
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* Returns the next non-disabled highlightedIndex value.
|
|
537
|
-
*
|
|
538
|
-
* @param {number} start The current highlightedIndex.
|
|
539
|
-
* @param {boolean} backwards If true, it will search backwards from the start.
|
|
540
|
-
* @param {unknown[]} items The items array.
|
|
541
|
-
* @param {(item: unknown, index: number) => boolean} isItemDisabled Function that tells if an item is disabled or not.
|
|
542
|
-
* @param {boolean} circular If the search reaches the end, if it can search again starting from the other end.
|
|
543
|
-
* @returns {number} The next non-disabled index.
|
|
544
|
-
*/
|
|
545
|
-
function getNonDisabledIndex(start, backwards, items, isItemDisabled, circular) {
|
|
546
|
-
if (circular === void 0) {
|
|
547
|
-
circular = false;
|
|
548
|
-
}
|
|
549
|
-
var count = items.length;
|
|
550
|
-
if (backwards) {
|
|
551
|
-
for (var index = start; index >= 0; index--) {
|
|
552
|
-
if (!isItemDisabled(items[index], index)) {
|
|
553
|
-
return index;
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
} else {
|
|
557
|
-
for (var _index = start; _index < count; _index++) {
|
|
558
|
-
if (!isItemDisabled(items[_index], _index)) {
|
|
559
|
-
return _index;
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
if (circular) {
|
|
564
|
-
return getNonDisabledIndex(backwards ? count - 1 : 0, backwards, items, isItemDisabled);
|
|
565
|
-
}
|
|
566
|
-
return -1;
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
/**
|
|
570
|
-
* Checks if event target is within the downshift elements.
|
|
571
|
-
*
|
|
572
|
-
* @param {EventTarget} target Target to check.
|
|
573
|
-
* @param {HTMLElement[]} downshiftElements The elements that form downshift (list, toggle button etc).
|
|
574
|
-
* @param {Window} environment The window context where downshift renders.
|
|
575
|
-
* @param {boolean} checkActiveElement Whether to also check activeElement.
|
|
576
|
-
*
|
|
577
|
-
* @returns {boolean} Whether or not the target is within downshift elements.
|
|
578
|
-
*/
|
|
579
|
-
function targetWithinDownshift(target, downshiftElements, environment, checkActiveElement) {
|
|
580
|
-
if (checkActiveElement === void 0) {
|
|
581
|
-
checkActiveElement = true;
|
|
582
|
-
}
|
|
583
|
-
return environment && downshiftElements.some(function (contextNode) {
|
|
584
|
-
return contextNode && (isOrContainsNode(contextNode, target, environment) || checkActiveElement && isOrContainsNode(contextNode, environment.document.activeElement, environment));
|
|
585
|
-
});
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
// eslint-disable-next-line import/no-mutable-exports
|
|
589
|
-
var validateControlledUnchanged = noop;
|
|
590
|
-
/* istanbul ignore next */
|
|
591
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
592
|
-
validateControlledUnchanged = function validateControlledUnchanged(state, prevProps, nextProps) {
|
|
593
|
-
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";
|
|
594
|
-
Object.keys(state).forEach(function (propKey) {
|
|
595
|
-
if (prevProps[propKey] !== undefined && nextProps[propKey] === undefined) {
|
|
596
|
-
// eslint-disable-next-line no-console
|
|
597
|
-
console.error("downshift: A component has changed the controlled prop \"" + propKey + "\" to be uncontrolled. " + warningDescription);
|
|
598
|
-
} else if (prevProps[propKey] === undefined && nextProps[propKey] !== undefined) {
|
|
599
|
-
// eslint-disable-next-line no-console
|
|
600
|
-
console.error("downshift: A component has changed the uncontrolled prop \"" + propKey + "\" to be controlled. " + warningDescription);
|
|
601
|
-
}
|
|
602
|
-
});
|
|
603
|
-
};
|
|
604
|
-
}
|
|
605
|
-
|
|
606
530
|
var _excluded$4 = ["refKey", "ref"],
|
|
607
531
|
_excluded2$4 = ["onClick", "onPress", "onKeyDown", "onKeyUp", "onBlur"],
|
|
608
532
|
_excluded3$3 = ["onKeyDown", "onBlur", "onChange", "onInput", "onChangeText"],
|
|
@@ -753,7 +677,7 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
753
677
|
}
|
|
754
678
|
newStateToSet[key];
|
|
755
679
|
// if it's coming from props, then we don't care to set it internally
|
|
756
|
-
if (
|
|
680
|
+
if (_this.props[key] === undefined) {
|
|
757
681
|
nextState[key] = newStateToSet[key];
|
|
758
682
|
}
|
|
759
683
|
});
|
|
@@ -1487,7 +1411,7 @@ var Downshift = /*#__PURE__*/function () {
|
|
|
1487
1411
|
validateGetMenuPropsCalledCorrectly(this._menuNode, this.getMenuProps);
|
|
1488
1412
|
}
|
|
1489
1413
|
}
|
|
1490
|
-
if (
|
|
1414
|
+
if (this.props.selectedItem !== undefined && this.props.selectedItemChanged(prevProps.selectedItem, this.props.selectedItem)) {
|
|
1491
1415
|
this.internalSetState({
|
|
1492
1416
|
type: controlledPropUpdatedSelectedItem,
|
|
1493
1417
|
inputValue: this.props.itemToString(this.props.selectedItem)
|
|
@@ -1627,7 +1551,7 @@ function callOnChangeProps(action, props, state, newState) {
|
|
|
1627
1551
|
changes[key] = newState[key];
|
|
1628
1552
|
}
|
|
1629
1553
|
}
|
|
1630
|
-
if (props.onStateChange && Object.keys(changes).length) {
|
|
1554
|
+
if (typeof props.onStateChange === 'function' && Object.keys(changes).length) {
|
|
1631
1555
|
props.onStateChange(_extends({
|
|
1632
1556
|
type: type
|
|
1633
1557
|
}, changes));
|
|
@@ -1653,50 +1577,53 @@ function invokeOnChangeHandler(key, action, props, state, newState) {
|
|
|
1653
1577
|
* two reducers, one from downshift and an optional one from the user.
|
|
1654
1578
|
* Also calls the onChange handlers for state values that have changed.
|
|
1655
1579
|
*
|
|
1656
|
-
* @param
|
|
1657
|
-
* @param
|
|
1658
|
-
* @param
|
|
1659
|
-
* @param
|
|
1660
|
-
* @returns
|
|
1580
|
+
* @param reducer Reducer function from downshift.
|
|
1581
|
+
* @param props The hook props, also passed to createInitialState.
|
|
1582
|
+
* @param createInitialState Function that returns the initial state.
|
|
1583
|
+
* @param isStateEqual Function that checks if a previous state is equal to the next.
|
|
1584
|
+
* @returns An array with the state and an action dispatcher.
|
|
1661
1585
|
*/
|
|
1662
1586
|
function useEnhancedReducer(reducer, props, createInitialState, isStateEqual) {
|
|
1663
|
-
var prevStateRef = React__namespace.useRef(
|
|
1664
|
-
var actionRef = React__namespace.useRef(
|
|
1665
|
-
var propsRef = useLatestRef(props);
|
|
1587
|
+
var prevStateRef = React__namespace.useRef({});
|
|
1588
|
+
var actionRef = React__namespace.useRef();
|
|
1666
1589
|
var enhancedReducer = React__namespace.useCallback(function (state, action) {
|
|
1667
1590
|
actionRef.current = action;
|
|
1668
|
-
state = getState(state,
|
|
1669
|
-
var changes = reducer(state,
|
|
1670
|
-
var newState =
|
|
1591
|
+
state = getState(state, action.props);
|
|
1592
|
+
var changes = reducer(state, action);
|
|
1593
|
+
var newState = action.props.stateReducer(state, _extends({}, action, {
|
|
1671
1594
|
changes: changes
|
|
1672
1595
|
}));
|
|
1673
1596
|
return _extends({}, state, newState);
|
|
1674
|
-
}, [
|
|
1597
|
+
}, [reducer]);
|
|
1675
1598
|
var _React$useReducer = React__namespace.useReducer(enhancedReducer, props, createInitialState),
|
|
1676
1599
|
state = _React$useReducer[0],
|
|
1677
1600
|
dispatch = _React$useReducer[1];
|
|
1601
|
+
var propsRef = useLatestRef(props);
|
|
1602
|
+
var dispatchWithProps = React__namespace.useCallback(function (action) {
|
|
1603
|
+
return dispatch(_extends({}, action, {
|
|
1604
|
+
props: propsRef.current
|
|
1605
|
+
}));
|
|
1606
|
+
}, [propsRef]);
|
|
1678
1607
|
var action = actionRef.current;
|
|
1679
1608
|
React__namespace.useEffect(function () {
|
|
1680
|
-
var
|
|
1681
|
-
var
|
|
1682
|
-
var shouldCallOnChangeProps = action && prevStateRef.current && !isStateEqual(prevState, state);
|
|
1609
|
+
var prevState = getState(prevStateRef.current, action == null ? void 0 : action.props);
|
|
1610
|
+
var shouldCallOnChangeProps = action && !isStateEqual(prevState, state);
|
|
1683
1611
|
if (shouldCallOnChangeProps) {
|
|
1684
|
-
callOnChangeProps(action,
|
|
1612
|
+
callOnChangeProps(action, action.props, prevState, state);
|
|
1685
1613
|
}
|
|
1686
1614
|
prevStateRef.current = state;
|
|
1687
|
-
}, [state, action, isStateEqual
|
|
1688
|
-
return [state,
|
|
1615
|
+
}, [state, action, isStateEqual]);
|
|
1616
|
+
return [state, dispatchWithProps];
|
|
1689
1617
|
}
|
|
1690
1618
|
|
|
1691
1619
|
/**
|
|
1692
1620
|
* Wraps the useEnhancedReducer and applies the controlled prop values before
|
|
1693
1621
|
* returning the new state.
|
|
1694
|
-
*
|
|
1695
|
-
* @param
|
|
1696
|
-
* @param
|
|
1697
|
-
* @param
|
|
1698
|
-
* @
|
|
1699
|
-
* @returns {Array} An array with the state and an action dispatcher.
|
|
1622
|
+
* @param reducer Reducer function from downshift.
|
|
1623
|
+
* @param props The hook props, also passed to createInitialState.
|
|
1624
|
+
* @param createInitialState Function that returns the initial state.
|
|
1625
|
+
* @param isStateEqual Function that checks if a previous state is equal to the next.
|
|
1626
|
+
* @returns An array with the state and an action dispatcher.
|
|
1700
1627
|
*/
|
|
1701
1628
|
function useControlledReducer$1(reducer, props, createInitialState, isStateEqual) {
|
|
1702
1629
|
var _useEnhancedReducer = useEnhancedReducer(reducer, props, createInitialState, isStateEqual),
|
|
@@ -1749,16 +1676,8 @@ function useIsInitialMount() {
|
|
|
1749
1676
|
return isInitialMountRef.current;
|
|
1750
1677
|
}
|
|
1751
1678
|
|
|
1752
|
-
/**
|
|
1753
|
-
* Default state reducer that returns the changes.
|
|
1754
|
-
*
|
|
1755
|
-
*/
|
|
1756
|
-
function stateReducer(_s, a) {
|
|
1757
|
-
return a.changes;
|
|
1758
|
-
}
|
|
1759
|
-
|
|
1760
1679
|
// Shared between all exports.
|
|
1761
|
-
var propTypes$
|
|
1680
|
+
var propTypes$4 = {
|
|
1762
1681
|
environment: PropTypes.shape({
|
|
1763
1682
|
addEventListener: PropTypes.func.isRequired,
|
|
1764
1683
|
removeEventListener: PropTypes.func.isRequired,
|
|
@@ -1774,30 +1693,47 @@ var propTypes$5 = {
|
|
|
1774
1693
|
stateReducer: PropTypes.func
|
|
1775
1694
|
};
|
|
1776
1695
|
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1696
|
+
/**
|
|
1697
|
+
* Returns the default value based on the defaultProp and defaultStateValue.
|
|
1698
|
+
*
|
|
1699
|
+
* @param defaultProp The default prop value.
|
|
1700
|
+
* @param defaultStateValue The default state value.
|
|
1701
|
+
* @returns The resolved default value.
|
|
1702
|
+
*/
|
|
1703
|
+
function getDefaultValue(defaultProp, defaultStateValue) {
|
|
1704
|
+
return defaultProp === undefined ? defaultStateValue : defaultProp;
|
|
1783
1705
|
}
|
|
1784
1706
|
|
|
1785
|
-
|
|
1786
|
-
|
|
1707
|
+
/**
|
|
1708
|
+
* Returns the initial value for a state variable, based on the following precedence:
|
|
1709
|
+
* 1. The controlled value (if it's not undefined)
|
|
1710
|
+
* 2. The initialValue (if it's not undefined)
|
|
1711
|
+
* 3. The defaultValue (if it's not undefined)
|
|
1712
|
+
* 4. The defaultStateValue
|
|
1713
|
+
*
|
|
1714
|
+
* @param value The controlled value of the state variable.
|
|
1715
|
+
* @param initialValue The initial value of the state variable.
|
|
1716
|
+
* @param defaultValue The default value of the state variable.
|
|
1717
|
+
* @param defaultStateValue The default state value to use if all other values are undefined.
|
|
1718
|
+
* @returns The initial value for the state variable.
|
|
1719
|
+
*/
|
|
1720
|
+
function getInitialValue(value, initialValue, defaultValue, defaultStateValue) {
|
|
1787
1721
|
if (value !== undefined) {
|
|
1788
1722
|
return value;
|
|
1789
1723
|
}
|
|
1790
|
-
var initialValue = props["initial" + capitalizeString(propKey)];
|
|
1791
1724
|
if (initialValue !== undefined) {
|
|
1792
1725
|
return initialValue;
|
|
1793
1726
|
}
|
|
1794
|
-
|
|
1727
|
+
if (defaultValue !== undefined) {
|
|
1728
|
+
return defaultValue;
|
|
1729
|
+
}
|
|
1730
|
+
return defaultStateValue;
|
|
1795
1731
|
}
|
|
1796
1732
|
|
|
1797
1733
|
/**
|
|
1798
1734
|
* Debounced call for updating the a11y message.
|
|
1799
1735
|
*/
|
|
1800
|
-
var updateA11yStatus = debounce
|
|
1736
|
+
var updateA11yStatus = debounce(function (status, document) {
|
|
1801
1737
|
setStatus(status, document);
|
|
1802
1738
|
}, 200);
|
|
1803
1739
|
|
|
@@ -1833,7 +1769,7 @@ function useA11yMessageStatus(getA11yStatusMessage, options, dependencyArray, en
|
|
|
1833
1769
|
}
|
|
1834
1770
|
|
|
1835
1771
|
// Shared between useSelect, useCombobox, Downshift.
|
|
1836
|
-
var
|
|
1772
|
+
var dropdownPropTypes = _extends({}, propTypes$4, {
|
|
1837
1773
|
getA11yStatusMessage: PropTypes.func,
|
|
1838
1774
|
highlightedIndex: PropTypes.number,
|
|
1839
1775
|
defaultHighlightedIndex: PropTypes.number,
|
|
@@ -1856,62 +1792,145 @@ var propTypes$4 = _extends({}, propTypes$5, {
|
|
|
1856
1792
|
scrollIntoView: PropTypes.func
|
|
1857
1793
|
});
|
|
1858
1794
|
|
|
1859
|
-
var
|
|
1795
|
+
var dropdownDefaultProps = {
|
|
1860
1796
|
itemToString: function itemToString(item) {
|
|
1861
1797
|
return item ? String(item) : '';
|
|
1862
1798
|
},
|
|
1863
1799
|
itemToKey: function itemToKey(item) {
|
|
1864
1800
|
return item;
|
|
1865
1801
|
},
|
|
1866
|
-
|
|
1802
|
+
isItemDisabled: function isItemDisabled(_item) {
|
|
1803
|
+
return false;
|
|
1804
|
+
},
|
|
1805
|
+
stateReducer: function stateReducer(_state, actionAndChanges) {
|
|
1806
|
+
return actionAndChanges.changes;
|
|
1807
|
+
},
|
|
1867
1808
|
scrollIntoView: scrollIntoView,
|
|
1868
1809
|
environment: /* istanbul ignore next (ssr) */
|
|
1869
1810
|
typeof window === 'undefined' || false ? undefined : window
|
|
1870
1811
|
};
|
|
1871
1812
|
|
|
1872
|
-
var
|
|
1813
|
+
var dropdownDefaultStateValues = {
|
|
1873
1814
|
highlightedIndex: -1,
|
|
1874
1815
|
isOpen: false,
|
|
1875
1816
|
selectedItem: null,
|
|
1876
1817
|
inputValue: ''
|
|
1877
1818
|
};
|
|
1878
1819
|
|
|
1879
|
-
//
|
|
1880
|
-
var
|
|
1881
|
-
|
|
1882
|
-
|
|
1820
|
+
// https://github.com/downshift-js/downshift/issues/1674#issuecomment-3924320872
|
|
1821
|
+
var SafeReact$1 = _extends({}, React__namespace);
|
|
1822
|
+
var reactUseId$1 = SafeReact$1.useId;
|
|
1823
|
+
var useElementIds$1 = typeof reactUseId$1 === 'function' ? useElementIdsR18$1 : useElementIdsLegacy$1;
|
|
1824
|
+
function useElementIdsR18$1(_ref) {
|
|
1825
|
+
var id = _ref.id,
|
|
1826
|
+
labelId = _ref.labelId,
|
|
1827
|
+
menuId = _ref.menuId,
|
|
1828
|
+
getItemId = _ref.getItemId,
|
|
1829
|
+
toggleButtonId = _ref.toggleButtonId,
|
|
1830
|
+
inputId = _ref.inputId;
|
|
1831
|
+
var reactId = "downshift-" + reactUseId$1();
|
|
1832
|
+
if (!id) {
|
|
1833
|
+
id = reactId;
|
|
1834
|
+
}
|
|
1835
|
+
var elementIds = React__namespace.useMemo(function () {
|
|
1836
|
+
return {
|
|
1837
|
+
labelId: labelId != null ? labelId : id + "-label",
|
|
1838
|
+
menuId: menuId != null ? menuId : id + "-menu",
|
|
1839
|
+
getItemId: getItemId != null ? getItemId : function (index) {
|
|
1840
|
+
return id + "-item-" + index;
|
|
1841
|
+
},
|
|
1842
|
+
toggleButtonId: toggleButtonId != null ? toggleButtonId : id + "-toggle-button",
|
|
1843
|
+
inputId: inputId != null ? inputId : id + "-input"
|
|
1844
|
+
};
|
|
1845
|
+
}, [getItemId, id, inputId, labelId, menuId, toggleButtonId]);
|
|
1846
|
+
return elementIds;
|
|
1883
1847
|
}
|
|
1884
|
-
function
|
|
1885
|
-
var
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1848
|
+
function useElementIdsLegacy$1(_ref2) {
|
|
1849
|
+
var id = _ref2.id,
|
|
1850
|
+
labelId = _ref2.labelId,
|
|
1851
|
+
menuId = _ref2.menuId,
|
|
1852
|
+
getItemId = _ref2.getItemId,
|
|
1853
|
+
toggleButtonId = _ref2.toggleButtonId,
|
|
1854
|
+
inputId = _ref2.inputId;
|
|
1855
|
+
var baseIdRef = React__namespace.useRef(id != null ? id : "downshift-" + generateId());
|
|
1856
|
+
var baseId = baseIdRef.current;
|
|
1857
|
+
var elementIds = React__namespace.useMemo(function () {
|
|
1858
|
+
return {
|
|
1859
|
+
labelId: labelId != null ? labelId : baseId + "-label",
|
|
1860
|
+
menuId: menuId != null ? menuId : baseId + "-menu",
|
|
1861
|
+
getItemId: getItemId != null ? getItemId : function (index) {
|
|
1862
|
+
return baseId + "-item-" + index;
|
|
1863
|
+
},
|
|
1864
|
+
toggleButtonId: toggleButtonId != null ? toggleButtonId : baseId + "-toggle-button",
|
|
1865
|
+
inputId: inputId != null ? inputId : baseId + "-input"
|
|
1866
|
+
};
|
|
1867
|
+
}, [getItemId, inputId, labelId, menuId, toggleButtonId, baseId]);
|
|
1868
|
+
return elementIds;
|
|
1897
1869
|
}
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1870
|
+
|
|
1871
|
+
/**
|
|
1872
|
+
* Returns the new highlightedIndex based on the defaultHighlightedIndex prop, if it's not disabled.
|
|
1873
|
+
*
|
|
1874
|
+
* @param props Props passed to the hook.
|
|
1875
|
+
* @returns The highlighted index.
|
|
1876
|
+
*/
|
|
1877
|
+
function getDefaultHighlightedIndex(items, isItemDisabled, defaultHighlightedIndex) {
|
|
1878
|
+
var highlightedIndex = getDefaultValue(defaultHighlightedIndex, dropdownDefaultStateValues.highlightedIndex);
|
|
1879
|
+
if (items[highlightedIndex] && isItemDisabled(items[highlightedIndex], highlightedIndex)) {
|
|
1880
|
+
return -1;
|
|
1881
|
+
}
|
|
1882
|
+
return highlightedIndex;
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
/**
|
|
1886
|
+
* Handles selection on Enter / Alt + ArrowUp. Closes the menu and resets the highlighted index, unless there is a highlighted.
|
|
1887
|
+
* In that case, selects the item and resets to defaults for open state and highlighted idex.
|
|
1888
|
+
*
|
|
1889
|
+
* @param props The Dropdown props.
|
|
1890
|
+
* @param highlightedIndex The index from the state.
|
|
1891
|
+
* @param inputValue Also return the input value for state.
|
|
1892
|
+
* @returns The changes for the state.
|
|
1893
|
+
*/
|
|
1894
|
+
function getChangesOnSelection(items, itemToString, defaultIsOpen, defaultHighlightedIndex, highlightedIndex, inputValue) {
|
|
1895
|
+
var shouldSelect = items.length && highlightedIndex >= 0;
|
|
1896
|
+
return _extends({
|
|
1897
|
+
isOpen: false,
|
|
1898
|
+
highlightedIndex: -1
|
|
1899
|
+
}, shouldSelect && _extends({
|
|
1900
|
+
selectedItem: items[highlightedIndex],
|
|
1901
|
+
isOpen: getDefaultValue(defaultIsOpen, dropdownDefaultStateValues.isOpen),
|
|
1902
|
+
highlightedIndex: getDefaultValue(defaultHighlightedIndex, dropdownDefaultStateValues.highlightedIndex)
|
|
1903
|
+
}, inputValue && {
|
|
1904
|
+
inputValue: itemToString(items[highlightedIndex])
|
|
1905
|
+
}));
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
/* eslint-disable max-params */
|
|
1909
|
+
/**
|
|
1910
|
+
* Returns the highlighted index when the menu is opened.
|
|
1911
|
+
* Takes into account the initialHighlightedIndex, defaultHighlightedIndex,
|
|
1912
|
+
* selectedItem, and the open direction offset.
|
|
1913
|
+
*
|
|
1914
|
+
* @param items The list of items.
|
|
1915
|
+
* @param initialHighlightedIndex The initial highlighted index prop.
|
|
1916
|
+
* @param defaultHighlightedIndex The default highlighted index prop.
|
|
1917
|
+
* @param isItemDisabled Callback to determine if an item is disabled.
|
|
1918
|
+
* @param itemToKey Callback to get a unique key from an item.
|
|
1919
|
+
* @param selectedItem The currently selected item.
|
|
1920
|
+
* @param highlightedIndex The current highlighted index from state.
|
|
1921
|
+
* @param offset The direction of navigation: positive opens downward, negative opens upward.
|
|
1922
|
+
* @returns The new highlighted index.
|
|
1923
|
+
*/
|
|
1924
|
+
function getHighlightedIndexOnOpen(items, initialHighlightedIndex, defaultHighlightedIndex, isItemDisabled, itemToKey, selectedItem, highlightedIndex, offset) {
|
|
1906
1925
|
if (items.length === 0) {
|
|
1907
1926
|
return -1;
|
|
1908
1927
|
}
|
|
1909
1928
|
|
|
1910
1929
|
// initialHighlightedIndex will give value to highlightedIndex on initial state only.
|
|
1911
|
-
if (initialHighlightedIndex !== undefined && highlightedIndex === initialHighlightedIndex && !isItemDisabled(items[initialHighlightedIndex], initialHighlightedIndex)) {
|
|
1930
|
+
if (initialHighlightedIndex !== undefined && highlightedIndex === initialHighlightedIndex && items[initialHighlightedIndex] && !isItemDisabled(items[initialHighlightedIndex], initialHighlightedIndex)) {
|
|
1912
1931
|
return initialHighlightedIndex;
|
|
1913
1932
|
}
|
|
1914
|
-
if (defaultHighlightedIndex !== undefined && !isItemDisabled(items[defaultHighlightedIndex], defaultHighlightedIndex)) {
|
|
1933
|
+
if (defaultHighlightedIndex !== undefined && items[defaultHighlightedIndex] && !isItemDisabled(items[defaultHighlightedIndex], defaultHighlightedIndex)) {
|
|
1915
1934
|
return defaultHighlightedIndex;
|
|
1916
1935
|
}
|
|
1917
1936
|
if (selectedItem) {
|
|
@@ -1919,21 +1938,71 @@ function getHighlightedIndexOnOpen(props, state, offset) {
|
|
|
1919
1938
|
return itemToKey(selectedItem) === itemToKey(item);
|
|
1920
1939
|
});
|
|
1921
1940
|
}
|
|
1922
|
-
if (offset < 0 && !isItemDisabled(items[items.length - 1], items.length - 1)) {
|
|
1941
|
+
if (offset < 0 && items[items.length - 1] && !isItemDisabled(items[items.length - 1], items.length - 1)) {
|
|
1923
1942
|
return items.length - 1;
|
|
1924
1943
|
}
|
|
1925
|
-
if (offset > 0 && !isItemDisabled(items[0], 0)) {
|
|
1944
|
+
if (offset > 0 && items[0] && !isItemDisabled(items[0], 0)) {
|
|
1926
1945
|
return 0;
|
|
1927
1946
|
}
|
|
1928
1947
|
return -1;
|
|
1929
1948
|
}
|
|
1949
|
+
|
|
1950
|
+
function getInitialState$3(props) {
|
|
1951
|
+
var selectedItem = getInitialValue(props.selectedItem, props.initialSelectedItem, props.defaultSelectedItem, dropdownDefaultStateValues.selectedItem);
|
|
1952
|
+
var isOpen = getInitialValue(props.isOpen, props.initialIsOpen, props.defaultIsOpen, dropdownDefaultStateValues.isOpen);
|
|
1953
|
+
var highlightedIndex = getInitialHighlightedIndex(props);
|
|
1954
|
+
var inputValue = getInitialValue(props.inputValue, props.initialInputValue, props.defaultInputValue, dropdownDefaultStateValues.inputValue);
|
|
1955
|
+
return {
|
|
1956
|
+
highlightedIndex: highlightedIndex < 0 && selectedItem && isOpen ? props.items.findIndex(function (item) {
|
|
1957
|
+
return props.itemToKey(item) === props.itemToKey(selectedItem);
|
|
1958
|
+
}) : highlightedIndex,
|
|
1959
|
+
isOpen: isOpen,
|
|
1960
|
+
selectedItem: selectedItem,
|
|
1961
|
+
inputValue: inputValue
|
|
1962
|
+
};
|
|
1963
|
+
}
|
|
1964
|
+
|
|
1965
|
+
/**
|
|
1966
|
+
* Returns the new highlightedIndex based on the initialHighlightedIndex prop, if not disabled.
|
|
1967
|
+
* @param props Props from useCombobox or useSelect.
|
|
1968
|
+
* @returns The highlighted index.
|
|
1969
|
+
*/
|
|
1970
|
+
function getInitialHighlightedIndex(props) {
|
|
1971
|
+
var highlightedIndex = getInitialValue(props.highlightedIndex, props.initialHighlightedIndex, props.defaultHighlightedIndex, dropdownDefaultStateValues.highlightedIndex);
|
|
1972
|
+
if (highlightedIndex > -1 && props.items[highlightedIndex] && props.isItemDisabled(props.items[highlightedIndex], highlightedIndex)) {
|
|
1973
|
+
return -1;
|
|
1974
|
+
}
|
|
1975
|
+
return highlightedIndex;
|
|
1976
|
+
}
|
|
1977
|
+
|
|
1978
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1979
|
+
// eslint-disable-next-line import/no-mutable-exports
|
|
1980
|
+
var useControlPropsValidator = noop;
|
|
1981
|
+
/* istanbul ignore next */
|
|
1982
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
1983
|
+
useControlPropsValidator = function useControlPropsValidator(_ref) {
|
|
1984
|
+
var props = _ref.props,
|
|
1985
|
+
state = _ref.state;
|
|
1986
|
+
// used for checking when props are moving from controlled to uncontrolled.
|
|
1987
|
+
var prevPropsRef = React__namespace.useRef(props);
|
|
1988
|
+
var isInitialMount = useIsInitialMount();
|
|
1989
|
+
React__namespace.useEffect(function () {
|
|
1990
|
+
if (isInitialMount) {
|
|
1991
|
+
return;
|
|
1992
|
+
}
|
|
1993
|
+
validateControlledUnchanged(state, prevPropsRef.current, props);
|
|
1994
|
+
prevPropsRef.current = props;
|
|
1995
|
+
}, [state, props, isInitialMount]);
|
|
1996
|
+
};
|
|
1997
|
+
}
|
|
1998
|
+
|
|
1930
1999
|
/**
|
|
1931
2000
|
* Tracks mouse and touch events, such as mouseDown, touchMove and touchEnd.
|
|
1932
2001
|
*
|
|
1933
|
-
* @param
|
|
1934
|
-
* @param
|
|
1935
|
-
* @param
|
|
1936
|
-
* @returns
|
|
2002
|
+
* @param environment The environment to add the event listeners to, for instance window.
|
|
2003
|
+
* @param handleBlur The function that is called if mouseDown or touchEnd occured outside the downshiftElements.
|
|
2004
|
+
* @param downshiftRefs The refs for the elements that should not trigger a blur action from mouseDown or touchEnd.
|
|
2005
|
+
* @returns The mouse and touch events information.
|
|
1937
2006
|
*/
|
|
1938
2007
|
function useMouseAndTouchTracker(environment, handleBlur, downshiftRefs) {
|
|
1939
2008
|
var mouseAndTouchTrackersRef = React__namespace.useRef({
|
|
@@ -1997,8 +2066,8 @@ var useGetterPropsCalledChecker = function useGetterPropsCalledChecker() {
|
|
|
1997
2066
|
/**
|
|
1998
2067
|
* Custom hook that checks if getter props are called correctly.
|
|
1999
2068
|
*
|
|
2000
|
-
* @param
|
|
2001
|
-
* @returns
|
|
2069
|
+
* @param propKeys Getter prop names to be handled.
|
|
2070
|
+
* @returns Setter function called inside getter props to set call information.
|
|
2002
2071
|
*/
|
|
2003
2072
|
/* istanbul ignore next */
|
|
2004
2073
|
if (process.env.NODE_ENV !== 'production') {
|
|
@@ -2037,117 +2106,55 @@ if (process.env.NODE_ENV !== 'production') {
|
|
|
2037
2106
|
elementRef: elementRef
|
|
2038
2107
|
};
|
|
2039
2108
|
}, []);
|
|
2040
|
-
return setGetterPropCallInfo;
|
|
2041
|
-
};
|
|
2042
|
-
}
|
|
2043
|
-
function useScrollIntoView(_ref) {
|
|
2044
|
-
var highlightedIndex = _ref.highlightedIndex,
|
|
2045
|
-
isOpen = _ref.isOpen,
|
|
2046
|
-
itemRefs = _ref.itemRefs,
|
|
2047
|
-
getItemNodeFromIndex = _ref.getItemNodeFromIndex,
|
|
2048
|
-
menuElement = _ref.menuElement,
|
|
2049
|
-
scrollIntoViewProp = _ref.scrollIntoView;
|
|
2050
|
-
// used not to scroll on highlight by mouse.
|
|
2051
|
-
var shouldScrollRef = React__namespace.useRef(true);
|
|
2052
|
-
// Scroll on highlighted item if change comes from keyboard.
|
|
2053
|
-
useIsomorphicLayoutEffect(function () {
|
|
2054
|
-
if (highlightedIndex < 0 || !isOpen || !Object.keys(itemRefs.current).length) {
|
|
2055
|
-
return;
|
|
2056
|
-
}
|
|
2057
|
-
if (shouldScrollRef.current === false) {
|
|
2058
|
-
shouldScrollRef.current = true;
|
|
2059
|
-
} else {
|
|
2060
|
-
scrollIntoViewProp(getItemNodeFromIndex(highlightedIndex), menuElement);
|
|
2061
|
-
}
|
|
2062
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2063
|
-
}, [highlightedIndex]);
|
|
2064
|
-
return shouldScrollRef;
|
|
2065
|
-
}
|
|
2066
|
-
|
|
2067
|
-
// eslint-disable-next-line import/no-mutable-exports
|
|
2068
|
-
var useControlPropsValidator = noop;
|
|
2069
|
-
/* istanbul ignore next */
|
|
2070
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
2071
|
-
useControlPropsValidator = function useControlPropsValidator(_ref2) {
|
|
2072
|
-
var props = _ref2.props,
|
|
2073
|
-
state = _ref2.state;
|
|
2074
|
-
// used for checking when props are moving from controlled to uncontrolled.
|
|
2075
|
-
var prevPropsRef = React__namespace.useRef(props);
|
|
2076
|
-
var isInitialMount = useIsInitialMount();
|
|
2077
|
-
React__namespace.useEffect(function () {
|
|
2078
|
-
if (isInitialMount) {
|
|
2079
|
-
return;
|
|
2080
|
-
}
|
|
2081
|
-
validateControlledUnchanged(state, prevPropsRef.current, props);
|
|
2082
|
-
prevPropsRef.current = props;
|
|
2083
|
-
}, [state, props, isInitialMount]);
|
|
2084
|
-
};
|
|
2085
|
-
}
|
|
2086
|
-
|
|
2087
|
-
/**
|
|
2088
|
-
* Handles selection on Enter / Alt + ArrowUp. Closes the menu and resets the highlighted index, unless there is a highlighted.
|
|
2089
|
-
* In that case, selects the item and resets to defaults for open state and highlighted idex.
|
|
2090
|
-
* @param {Object} props The useCombobox props.
|
|
2091
|
-
* @param {number} highlightedIndex The index from the state.
|
|
2092
|
-
* @param {boolean} inputValue Also return the input value for state.
|
|
2093
|
-
* @returns The changes for the state.
|
|
2094
|
-
*/
|
|
2095
|
-
function getChangesOnSelection(props, highlightedIndex, inputValue) {
|
|
2096
|
-
var _props$items;
|
|
2097
|
-
if (inputValue === void 0) {
|
|
2098
|
-
inputValue = true;
|
|
2099
|
-
}
|
|
2100
|
-
var shouldSelect = ((_props$items = props.items) == null ? void 0 : _props$items.length) && highlightedIndex >= 0;
|
|
2101
|
-
return _extends({
|
|
2102
|
-
isOpen: false,
|
|
2103
|
-
highlightedIndex: -1
|
|
2104
|
-
}, shouldSelect && _extends({
|
|
2105
|
-
selectedItem: props.items[highlightedIndex],
|
|
2106
|
-
isOpen: getDefaultValue$1(props, 'isOpen', defaultStateValues$1),
|
|
2107
|
-
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex', defaultStateValues$1)
|
|
2108
|
-
}, inputValue && {
|
|
2109
|
-
inputValue: props.itemToString(props.items[highlightedIndex])
|
|
2110
|
-
}));
|
|
2111
|
-
}
|
|
2112
|
-
|
|
2113
|
-
/**
|
|
2114
|
-
* Check if a state is equal for dropdowns, by comparing isOpen, inputValue, highlightedIndex and selected item.
|
|
2115
|
-
* Used by useSelect and useCombobox.
|
|
2116
|
-
*
|
|
2117
|
-
* @param {Object} prevState
|
|
2118
|
-
* @param {Object} newState
|
|
2119
|
-
* @returns {boolean} Wheather the states are deeply equal.
|
|
2120
|
-
*/
|
|
2121
|
-
function isDropdownsStateEqual(prevState, newState) {
|
|
2122
|
-
return prevState.isOpen === newState.isOpen && prevState.inputValue === newState.inputValue && prevState.highlightedIndex === newState.highlightedIndex && prevState.selectedItem === newState.selectedItem;
|
|
2109
|
+
return setGetterPropCallInfo;
|
|
2110
|
+
};
|
|
2123
2111
|
}
|
|
2124
2112
|
|
|
2113
|
+
// istanbul ignore next
|
|
2114
|
+
var useIsomorphicLayoutEffect = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
|
|
2115
|
+
|
|
2125
2116
|
/**
|
|
2126
|
-
*
|
|
2127
|
-
*
|
|
2128
|
-
* @param
|
|
2129
|
-
* @
|
|
2117
|
+
* Utility hook that scrolls an item from a menu into view.
|
|
2118
|
+
* @param scrollIntoView The function that does the scroll.
|
|
2119
|
+
* @param highlightedIndex The index of the item that should be scrolled.
|
|
2120
|
+
* @param isOpen If the menu is open or not.
|
|
2121
|
+
* @param menuElement The menu element.
|
|
2122
|
+
* @param itemElements The object containing item elements.
|
|
2123
|
+
* @param getItemId The function to get the item id from index.
|
|
2124
|
+
* @returns Function that when called prevents the scroll.
|
|
2130
2125
|
*/
|
|
2131
|
-
function
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2126
|
+
function useScrollIntoView(scrollIntoView, highlightedIndex, isOpen, menuElement, itemElements, getItemId) {
|
|
2127
|
+
// used not to scroll on highlight by mouse.
|
|
2128
|
+
var shouldScrollRef = React__namespace.useRef(true);
|
|
2129
|
+
// Scroll on highlighted item if change comes from keyboard.
|
|
2130
|
+
useIsomorphicLayoutEffect(function () {
|
|
2131
|
+
if (highlightedIndex < 0 || !isOpen || !Object.keys(itemElements).length) {
|
|
2132
|
+
return;
|
|
2133
|
+
}
|
|
2134
|
+
if (shouldScrollRef.current) {
|
|
2135
|
+
var itemElement = itemElements[getItemId(highlightedIndex)];
|
|
2136
|
+
if (itemElement && menuElement) {
|
|
2137
|
+
scrollIntoView(itemElement, menuElement);
|
|
2138
|
+
}
|
|
2139
|
+
} else {
|
|
2140
|
+
shouldScrollRef.current = true;
|
|
2141
|
+
}
|
|
2142
|
+
}, [highlightedIndex]);
|
|
2143
|
+
return React__namespace.useCallback(function preventScroll() {
|
|
2144
|
+
shouldScrollRef.current = false;
|
|
2145
|
+
}, []);
|
|
2137
2146
|
}
|
|
2138
2147
|
|
|
2139
2148
|
/**
|
|
2140
|
-
*
|
|
2149
|
+
* Check if a state is equal for dropdowns, by comparing isOpen, inputValue, highlightedIndex and selected item.
|
|
2150
|
+
* Used by useSelect and useCombobox.
|
|
2141
2151
|
*
|
|
2142
|
-
* @param
|
|
2143
|
-
* @
|
|
2152
|
+
* @param prevState The previous dropdown state.
|
|
2153
|
+
* @param newState The new dropdown state.
|
|
2154
|
+
* @returns Wheather the states are deeply equal.
|
|
2144
2155
|
*/
|
|
2145
|
-
function
|
|
2146
|
-
|
|
2147
|
-
if (highlightedIndex > -1 && props.isItemDisabled(props.items[highlightedIndex], highlightedIndex)) {
|
|
2148
|
-
return -1;
|
|
2149
|
-
}
|
|
2150
|
-
return highlightedIndex;
|
|
2156
|
+
function isDropdownStateEqual(prevState, newState) {
|
|
2157
|
+
return prevState.isOpen === newState.isOpen && prevState.inputValue === newState.inputValue && prevState.highlightedIndex === newState.highlightedIndex && prevState.selectedItem === newState.selectedItem;
|
|
2151
2158
|
}
|
|
2152
2159
|
|
|
2153
2160
|
function downshiftCommonReducer(state, props, action, stateChangeTypes) {
|
|
@@ -2168,13 +2175,13 @@ function downshiftCommonReducer(state, props, action, stateChangeTypes) {
|
|
|
2168
2175
|
case stateChangeTypes.FunctionToggleMenu:
|
|
2169
2176
|
changes = {
|
|
2170
2177
|
isOpen: !state.isOpen,
|
|
2171
|
-
highlightedIndex: state.isOpen ? -1 : getHighlightedIndexOnOpen(props, state, 0)
|
|
2178
|
+
highlightedIndex: state.isOpen ? -1 : getHighlightedIndexOnOpen(props.items, props.initialHighlightedIndex, props.defaultHighlightedIndex, props.isItemDisabled, props.itemToKey, state.selectedItem, state.highlightedIndex, 0)
|
|
2172
2179
|
};
|
|
2173
2180
|
break;
|
|
2174
2181
|
case stateChangeTypes.FunctionOpenMenu:
|
|
2175
2182
|
changes = {
|
|
2176
2183
|
isOpen: true,
|
|
2177
|
-
highlightedIndex: getHighlightedIndexOnOpen(props, state, 0)
|
|
2184
|
+
highlightedIndex: getHighlightedIndexOnOpen(props.items, props.initialHighlightedIndex, props.defaultHighlightedIndex, props.isItemDisabled, props.itemToKey, state.selectedItem, state.highlightedIndex, 0)
|
|
2178
2185
|
};
|
|
2179
2186
|
break;
|
|
2180
2187
|
case stateChangeTypes.FunctionCloseMenu:
|
|
@@ -2183,9 +2190,12 @@ function downshiftCommonReducer(state, props, action, stateChangeTypes) {
|
|
|
2183
2190
|
};
|
|
2184
2191
|
break;
|
|
2185
2192
|
case stateChangeTypes.FunctionSetHighlightedIndex:
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2193
|
+
{
|
|
2194
|
+
var highlightedIndex = action.highlightedIndex;
|
|
2195
|
+
changes = {
|
|
2196
|
+
highlightedIndex: props.isItemDisabled(props.items[highlightedIndex], highlightedIndex) ? -1 : highlightedIndex
|
|
2197
|
+
};
|
|
2198
|
+
}
|
|
2189
2199
|
break;
|
|
2190
2200
|
case stateChangeTypes.FunctionSetInputValue:
|
|
2191
2201
|
changes = {
|
|
@@ -2194,10 +2204,10 @@ function downshiftCommonReducer(state, props, action, stateChangeTypes) {
|
|
|
2194
2204
|
break;
|
|
2195
2205
|
case stateChangeTypes.FunctionReset:
|
|
2196
2206
|
changes = {
|
|
2197
|
-
highlightedIndex: getDefaultHighlightedIndex(props),
|
|
2198
|
-
isOpen: getDefaultValue
|
|
2199
|
-
selectedItem: getDefaultValue
|
|
2200
|
-
inputValue: getDefaultValue
|
|
2207
|
+
highlightedIndex: getDefaultHighlightedIndex(props.items, props.isItemDisabled, props.defaultHighlightedIndex),
|
|
2208
|
+
isOpen: getDefaultValue(props.defaultIsOpen, dropdownDefaultStateValues.isOpen),
|
|
2209
|
+
selectedItem: getDefaultValue(props.defaultSelectedItem, dropdownDefaultStateValues.selectedItem),
|
|
2210
|
+
inputValue: getDefaultValue(props.defaultInputValue, dropdownDefaultStateValues.inputValue)
|
|
2201
2211
|
};
|
|
2202
2212
|
break;
|
|
2203
2213
|
default:
|
|
@@ -2207,17 +2217,11 @@ function downshiftCommonReducer(state, props, action, stateChangeTypes) {
|
|
|
2207
2217
|
}
|
|
2208
2218
|
/* eslint-enable complexity */
|
|
2209
2219
|
|
|
2210
|
-
var propTypes$3 = _extends({},
|
|
2220
|
+
var propTypes$3 = _extends({}, dropdownPropTypes, {
|
|
2211
2221
|
items: PropTypes.array.isRequired,
|
|
2212
2222
|
isItemDisabled: PropTypes.func
|
|
2213
2223
|
});
|
|
2214
2224
|
|
|
2215
|
-
var defaultProps$2 = _extends({}, defaultProps$3, {
|
|
2216
|
-
isItemDisabled: function isItemDisabled() {
|
|
2217
|
-
return false;
|
|
2218
|
-
}
|
|
2219
|
-
});
|
|
2220
|
-
|
|
2221
2225
|
function getItemIndexByCharacterKey(_ref) {
|
|
2222
2226
|
var keysSoFar = _ref.keysSoFar,
|
|
2223
2227
|
highlightedIndex = _ref.highlightedIndex,
|
|
@@ -2286,16 +2290,15 @@ var stateChangeTypes$3 = /*#__PURE__*/Object.freeze({
|
|
|
2286
2290
|
});
|
|
2287
2291
|
|
|
2288
2292
|
/* eslint-disable complexity */
|
|
2289
|
-
function downshiftSelectReducer(state,
|
|
2290
|
-
var _props$items;
|
|
2293
|
+
function downshiftSelectReducer(state, action) {
|
|
2291
2294
|
var type = action.type,
|
|
2292
|
-
|
|
2295
|
+
props = action.props;
|
|
2293
2296
|
var changes;
|
|
2294
2297
|
switch (type) {
|
|
2295
2298
|
case ItemClick$1:
|
|
2296
2299
|
changes = {
|
|
2297
|
-
isOpen: getDefaultValue
|
|
2298
|
-
highlightedIndex: getDefaultHighlightedIndex(props),
|
|
2300
|
+
isOpen: getDefaultValue(props.defaultIsOpen, dropdownDefaultStateValues.isOpen),
|
|
2301
|
+
highlightedIndex: getDefaultHighlightedIndex(props.items, props.isItemDisabled, props.defaultHighlightedIndex),
|
|
2299
2302
|
selectedItem: props.items[action.index]
|
|
2300
2303
|
};
|
|
2301
2304
|
break;
|
|
@@ -2322,7 +2325,8 @@ function downshiftSelectReducer(state, props, action) {
|
|
|
2322
2325
|
break;
|
|
2323
2326
|
case ToggleButtonKeyDownArrowDown:
|
|
2324
2327
|
{
|
|
2325
|
-
var
|
|
2328
|
+
var altKey = action.altKey;
|
|
2329
|
+
var _highlightedIndex = state.isOpen ? getHighlightedIndex(state.highlightedIndex, 1, props.items, props.isItemDisabled) : altKey && state.selectedItem == null ? -1 : getHighlightedIndexOnOpen(props.items, props.initialHighlightedIndex, props.defaultHighlightedIndex, props.isItemDisabled, props.itemToKey, state.selectedItem, state.highlightedIndex, 1);
|
|
2326
2330
|
changes = {
|
|
2327
2331
|
highlightedIndex: _highlightedIndex,
|
|
2328
2332
|
isOpen: true
|
|
@@ -2330,20 +2334,23 @@ function downshiftSelectReducer(state, props, action) {
|
|
|
2330
2334
|
}
|
|
2331
2335
|
break;
|
|
2332
2336
|
case ToggleButtonKeyDownArrowUp:
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
highlightedIndex:
|
|
2339
|
-
|
|
2340
|
-
|
|
2337
|
+
{
|
|
2338
|
+
var _altKey = action.altKey;
|
|
2339
|
+
if (state.isOpen && _altKey) {
|
|
2340
|
+
changes = getChangesOnSelection(props.items, props.itemToString, props.defaultIsOpen, props.defaultHighlightedIndex, state.highlightedIndex, false);
|
|
2341
|
+
} else {
|
|
2342
|
+
var _highlightedIndex2 = state.isOpen ? getHighlightedIndex(state.highlightedIndex, -1, props.items, props.isItemDisabled) : getHighlightedIndexOnOpen(props.items, props.initialHighlightedIndex, props.defaultHighlightedIndex, props.isItemDisabled, props.itemToKey, state.selectedItem, state.highlightedIndex, -1);
|
|
2343
|
+
changes = {
|
|
2344
|
+
highlightedIndex: _highlightedIndex2,
|
|
2345
|
+
isOpen: true
|
|
2346
|
+
};
|
|
2347
|
+
}
|
|
2341
2348
|
}
|
|
2342
2349
|
break;
|
|
2343
2350
|
// only triggered when menu is open.
|
|
2344
2351
|
case ToggleButtonKeyDownEnter:
|
|
2345
2352
|
case ToggleButtonKeyDownSpaceButton:
|
|
2346
|
-
changes = getChangesOnSelection(props, state.highlightedIndex, false);
|
|
2353
|
+
changes = getChangesOnSelection(props.items, props.itemToString, props.defaultIsOpen, props.defaultHighlightedIndex, state.highlightedIndex, false);
|
|
2347
2354
|
break;
|
|
2348
2355
|
case ToggleButtonKeyDownHome:
|
|
2349
2356
|
changes = {
|
|
@@ -2377,7 +2384,7 @@ function downshiftSelectReducer(state, props, action) {
|
|
|
2377
2384
|
changes = _extends({
|
|
2378
2385
|
isOpen: false,
|
|
2379
2386
|
highlightedIndex: -1
|
|
2380
|
-
}, state.highlightedIndex >= 0 &&
|
|
2387
|
+
}, state.highlightedIndex >= 0 && props.items.length && {
|
|
2381
2388
|
selectedItem: props.items[state.highlightedIndex]
|
|
2382
2389
|
});
|
|
2383
2390
|
break;
|
|
@@ -2393,60 +2400,9 @@ function downshiftSelectReducer(state, props, action) {
|
|
|
2393
2400
|
}
|
|
2394
2401
|
/* eslint-enable complexity */
|
|
2395
2402
|
|
|
2396
|
-
// https://github.com/downshift-js/downshift/issues/1674#issuecomment-3924320872
|
|
2397
|
-
var SafeReact$1 = _extends({}, React__namespace);
|
|
2398
|
-
var reactUseId$1 = SafeReact$1.useId;
|
|
2399
|
-
var useElementIds$1 = typeof reactUseId$1 === 'function' ? useElementIdsR18$1 : useElementIdsLegacy$1;
|
|
2400
|
-
function useElementIdsR18$1(_ref) {
|
|
2401
|
-
var id = _ref.id,
|
|
2402
|
-
labelId = _ref.labelId,
|
|
2403
|
-
menuId = _ref.menuId,
|
|
2404
|
-
getItemId = _ref.getItemId,
|
|
2405
|
-
toggleButtonId = _ref.toggleButtonId,
|
|
2406
|
-
inputId = _ref.inputId;
|
|
2407
|
-
var reactId = "downshift-" + reactUseId$1();
|
|
2408
|
-
if (!id) {
|
|
2409
|
-
id = reactId;
|
|
2410
|
-
}
|
|
2411
|
-
var elementIds = React__namespace.useMemo(function () {
|
|
2412
|
-
return {
|
|
2413
|
-
labelId: labelId != null ? labelId : id + "-label",
|
|
2414
|
-
menuId: menuId != null ? menuId : id + "-menu",
|
|
2415
|
-
getItemId: getItemId != null ? getItemId : function (index) {
|
|
2416
|
-
return id + "-item-" + index;
|
|
2417
|
-
},
|
|
2418
|
-
toggleButtonId: toggleButtonId != null ? toggleButtonId : id + "-toggle-button",
|
|
2419
|
-
inputId: inputId != null ? inputId : id + "-input"
|
|
2420
|
-
};
|
|
2421
|
-
}, [getItemId, id, inputId, labelId, menuId, toggleButtonId]);
|
|
2422
|
-
return elementIds;
|
|
2423
|
-
}
|
|
2424
|
-
function useElementIdsLegacy$1(_ref2) {
|
|
2425
|
-
var id = _ref2.id,
|
|
2426
|
-
labelId = _ref2.labelId,
|
|
2427
|
-
menuId = _ref2.menuId,
|
|
2428
|
-
getItemId = _ref2.getItemId,
|
|
2429
|
-
toggleButtonId = _ref2.toggleButtonId,
|
|
2430
|
-
inputId = _ref2.inputId;
|
|
2431
|
-
var baseIdRef = React__namespace.useRef(id != null ? id : "downshift-" + generateId());
|
|
2432
|
-
var baseId = baseIdRef.current;
|
|
2433
|
-
var elementIds = React__namespace.useMemo(function () {
|
|
2434
|
-
return {
|
|
2435
|
-
labelId: labelId != null ? labelId : baseId + "-label",
|
|
2436
|
-
menuId: menuId != null ? menuId : baseId + "-menu",
|
|
2437
|
-
getItemId: getItemId != null ? getItemId : function (index) {
|
|
2438
|
-
return baseId + "-item-" + index;
|
|
2439
|
-
},
|
|
2440
|
-
toggleButtonId: toggleButtonId != null ? toggleButtonId : baseId + "-toggle-button",
|
|
2441
|
-
inputId: inputId != null ? inputId : baseId + "-input"
|
|
2442
|
-
};
|
|
2443
|
-
}, [getItemId, inputId, labelId, menuId, toggleButtonId, baseId]);
|
|
2444
|
-
return elementIds;
|
|
2445
|
-
}
|
|
2446
|
-
|
|
2447
2403
|
var _excluded$3 = ["onClick"],
|
|
2448
|
-
_excluded2$3 = ["onMouseLeave", "refKey", "ref"],
|
|
2449
|
-
_excluded3$2 = ["onBlur", "onClick", "onPress", "onKeyDown", "refKey", "ref"],
|
|
2404
|
+
_excluded2$3 = ["onMouseLeave", "refKey", "ref", "aria-label"],
|
|
2405
|
+
_excluded3$2 = ["onBlur", "onClick", "onPress", "onKeyDown", "refKey", "ref", "disabled"],
|
|
2450
2406
|
_excluded4$1 = ["item", "index", "onMouseMove", "onClick", "onMouseDown", "onPress", "refKey", "disabled", "ref"];
|
|
2451
2407
|
useSelect.stateChangeTypes = stateChangeTypes$3;
|
|
2452
2408
|
function useSelect(userProps) {
|
|
@@ -2455,22 +2411,23 @@ function useSelect(userProps) {
|
|
|
2455
2411
|
}
|
|
2456
2412
|
validatePropTypes$1(userProps, useSelect, propTypes$3);
|
|
2457
2413
|
// Props defaults and destructuring.
|
|
2458
|
-
var props = _extends({},
|
|
2414
|
+
var props = _extends({}, dropdownDefaultProps, userProps);
|
|
2459
2415
|
var scrollIntoView = props.scrollIntoView,
|
|
2460
2416
|
environment = props.environment,
|
|
2461
2417
|
getA11yStatusMessage = props.getA11yStatusMessage;
|
|
2462
2418
|
// Initial state depending on controlled props.
|
|
2463
|
-
var _useControlledReducer = useControlledReducer$1(downshiftSelectReducer, props, getInitialState$3,
|
|
2419
|
+
var _useControlledReducer = useControlledReducer$1(downshiftSelectReducer, props, getInitialState$3, isDropdownStateEqual),
|
|
2464
2420
|
state = _useControlledReducer[0],
|
|
2465
2421
|
dispatch = _useControlledReducer[1];
|
|
2466
2422
|
var isOpen = state.isOpen,
|
|
2467
2423
|
highlightedIndex = state.highlightedIndex,
|
|
2468
2424
|
selectedItem = state.selectedItem,
|
|
2469
2425
|
inputValue = state.inputValue;
|
|
2470
|
-
|
|
2426
|
+
|
|
2427
|
+
// Element refs.
|
|
2471
2428
|
var toggleButtonRef = React.useRef(null);
|
|
2472
2429
|
var menuRef = React.useRef(null);
|
|
2473
|
-
var
|
|
2430
|
+
var itemsRef = React.useRef({});
|
|
2474
2431
|
|
|
2475
2432
|
// used to keep the inputValue clearTimeout object between renders.
|
|
2476
2433
|
var clearTimeoutRef = React.useRef(null);
|
|
@@ -2482,24 +2439,12 @@ function useSelect(userProps) {
|
|
|
2482
2439
|
props: props
|
|
2483
2440
|
});
|
|
2484
2441
|
|
|
2485
|
-
// Some utils.
|
|
2486
|
-
var getItemNodeFromIndex = React.useCallback(function (index) {
|
|
2487
|
-
return itemRefs.current[elementIds.getItemId(index)];
|
|
2488
|
-
}, [elementIds]);
|
|
2489
|
-
|
|
2490
2442
|
// Effects.
|
|
2491
2443
|
// Adds an a11y aria live status message if getA11yStatusMessage is passed.
|
|
2492
2444
|
useA11yMessageStatus(getA11yStatusMessage, state, [isOpen, highlightedIndex, selectedItem, inputValue], environment);
|
|
2493
2445
|
// Scroll on highlighted item if change comes from keyboard.
|
|
2494
|
-
var
|
|
2495
|
-
|
|
2496
|
-
highlightedIndex: highlightedIndex,
|
|
2497
|
-
isOpen: isOpen,
|
|
2498
|
-
itemRefs: itemRefs,
|
|
2499
|
-
scrollIntoView: scrollIntoView,
|
|
2500
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
2501
|
-
});
|
|
2502
|
-
// Sets cleanup for the keysSoFar callback, debounded after 500ms.
|
|
2446
|
+
var preventScroll = useScrollIntoView(scrollIntoView, highlightedIndex, isOpen, menuRef.current, itemsRef.current, elementIds.getItemId);
|
|
2447
|
+
// Sets cleanup for the keysSoFar callback, debounced after 500ms.
|
|
2503
2448
|
React.useEffect(function () {
|
|
2504
2449
|
// init the clean function here as we need access to dispatch.
|
|
2505
2450
|
clearTimeoutRef.current = debounce(function (outerDispatch) {
|
|
@@ -2511,7 +2456,8 @@ function useSelect(userProps) {
|
|
|
2511
2456
|
|
|
2512
2457
|
// Cancel any pending debounced calls on mount
|
|
2513
2458
|
return function () {
|
|
2514
|
-
|
|
2459
|
+
var _clearTimeoutRef$curr;
|
|
2460
|
+
(_clearTimeoutRef$curr = clearTimeoutRef.current) == null || _clearTimeoutRef$curr.cancel();
|
|
2515
2461
|
};
|
|
2516
2462
|
}, []);
|
|
2517
2463
|
// Invokes the keysSoFar callback set up above.
|
|
@@ -2519,7 +2465,7 @@ function useSelect(userProps) {
|
|
|
2519
2465
|
if (!inputValue) {
|
|
2520
2466
|
return;
|
|
2521
2467
|
}
|
|
2522
|
-
clearTimeoutRef.current(dispatch);
|
|
2468
|
+
clearTimeoutRef.current == null || clearTimeoutRef.current(dispatch);
|
|
2523
2469
|
}, [dispatch, inputValue]);
|
|
2524
2470
|
useControlPropsValidator({
|
|
2525
2471
|
props: props,
|
|
@@ -2527,7 +2473,7 @@ function useSelect(userProps) {
|
|
|
2527
2473
|
});
|
|
2528
2474
|
// Focus the toggle button on first render if required.
|
|
2529
2475
|
React.useEffect(function () {
|
|
2530
|
-
var focusOnOpen = getInitialValue
|
|
2476
|
+
var focusOnOpen = getInitialValue(props.isOpen, props.initialIsOpen, props.defaultIsOpen, dropdownDefaultStateValues.isOpen);
|
|
2531
2477
|
if (focusOnOpen && toggleButtonRef.current) {
|
|
2532
2478
|
toggleButtonRef.current.focus();
|
|
2533
2479
|
}
|
|
@@ -2548,7 +2494,7 @@ function useSelect(userProps) {
|
|
|
2548
2494
|
// Reset itemRefs on close.
|
|
2549
2495
|
React.useEffect(function () {
|
|
2550
2496
|
if (!isOpen) {
|
|
2551
|
-
|
|
2497
|
+
itemsRef.current = {};
|
|
2552
2498
|
}
|
|
2553
2499
|
}, [isOpen]);
|
|
2554
2500
|
|
|
@@ -2633,50 +2579,11 @@ function useSelect(userProps) {
|
|
|
2633
2579
|
};
|
|
2634
2580
|
}, [dispatch, latest]);
|
|
2635
2581
|
|
|
2636
|
-
// Action functions.
|
|
2637
|
-
var toggleMenu = React.useCallback(function () {
|
|
2638
|
-
dispatch({
|
|
2639
|
-
type: FunctionToggleMenu$1
|
|
2640
|
-
});
|
|
2641
|
-
}, [dispatch]);
|
|
2642
|
-
var closeMenu = React.useCallback(function () {
|
|
2643
|
-
dispatch({
|
|
2644
|
-
type: FunctionCloseMenu$1
|
|
2645
|
-
});
|
|
2646
|
-
}, [dispatch]);
|
|
2647
|
-
var openMenu = React.useCallback(function () {
|
|
2648
|
-
dispatch({
|
|
2649
|
-
type: FunctionOpenMenu$1
|
|
2650
|
-
});
|
|
2651
|
-
}, [dispatch]);
|
|
2652
|
-
var setHighlightedIndex = React.useCallback(function (newHighlightedIndex) {
|
|
2653
|
-
dispatch({
|
|
2654
|
-
type: FunctionSetHighlightedIndex$1,
|
|
2655
|
-
highlightedIndex: newHighlightedIndex
|
|
2656
|
-
});
|
|
2657
|
-
}, [dispatch]);
|
|
2658
|
-
var selectItem = React.useCallback(function (newSelectedItem) {
|
|
2659
|
-
dispatch({
|
|
2660
|
-
type: FunctionSelectItem$1,
|
|
2661
|
-
selectedItem: newSelectedItem
|
|
2662
|
-
});
|
|
2663
|
-
}, [dispatch]);
|
|
2664
|
-
var reset = React.useCallback(function () {
|
|
2665
|
-
dispatch({
|
|
2666
|
-
type: FunctionReset$2
|
|
2667
|
-
});
|
|
2668
|
-
}, [dispatch]);
|
|
2669
|
-
var setInputValue = React.useCallback(function (newInputValue) {
|
|
2670
|
-
dispatch({
|
|
2671
|
-
type: FunctionSetInputValue$1,
|
|
2672
|
-
inputValue: newInputValue
|
|
2673
|
-
});
|
|
2674
|
-
}, [dispatch]);
|
|
2675
2582
|
// Getter functions.
|
|
2676
|
-
var getLabelProps = React.useCallback(function (
|
|
2677
|
-
var _ref =
|
|
2583
|
+
var getLabelProps = React.useCallback(function (labelProps) {
|
|
2584
|
+
var _ref = labelProps != null ? labelProps : {},
|
|
2678
2585
|
onClick = _ref.onClick,
|
|
2679
|
-
|
|
2586
|
+
rest = _objectWithoutPropertiesLoose(_ref, _excluded$3);
|
|
2680
2587
|
var labelHandleClick = function labelHandleClick() {
|
|
2681
2588
|
var _toggleButtonRef$curr;
|
|
2682
2589
|
(_toggleButtonRef$curr = toggleButtonRef.current) == null || _toggleButtonRef$curr.focus();
|
|
@@ -2685,17 +2592,18 @@ function useSelect(userProps) {
|
|
|
2685
2592
|
id: elementIds.labelId,
|
|
2686
2593
|
htmlFor: elementIds.toggleButtonId,
|
|
2687
2594
|
onClick: callAllEventHandlers(onClick, labelHandleClick)
|
|
2688
|
-
},
|
|
2595
|
+
}, rest);
|
|
2689
2596
|
}, [elementIds]);
|
|
2690
|
-
var getMenuProps = React.useCallback(function (
|
|
2597
|
+
var getMenuProps = React.useCallback(function (menuProps, otherProps) {
|
|
2691
2598
|
var _extends2;
|
|
2692
|
-
var _ref2 =
|
|
2599
|
+
var _ref2 = menuProps != null ? menuProps : {},
|
|
2693
2600
|
onMouseLeave = _ref2.onMouseLeave,
|
|
2694
2601
|
_ref2$refKey = _ref2.refKey,
|
|
2695
2602
|
refKey = _ref2$refKey === void 0 ? 'ref' : _ref2$refKey,
|
|
2696
2603
|
ref = _ref2.ref,
|
|
2604
|
+
ariaLabel = _ref2['aria-label'],
|
|
2697
2605
|
rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$3);
|
|
2698
|
-
var _ref3 =
|
|
2606
|
+
var _ref3 = otherProps != null ? otherProps : {},
|
|
2699
2607
|
_ref3$suppressRefErro = _ref3.suppressRefError,
|
|
2700
2608
|
suppressRefError = _ref3$suppressRefErro === void 0 ? false : _ref3$suppressRefErro;
|
|
2701
2609
|
var menuHandleMouseLeave = function menuHandleMouseLeave() {
|
|
@@ -2706,11 +2614,11 @@ function useSelect(userProps) {
|
|
|
2706
2614
|
setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
|
|
2707
2615
|
return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
|
|
2708
2616
|
menuRef.current = menuNode;
|
|
2709
|
-
}), _extends2.id = elementIds.menuId, _extends2.role = 'listbox', _extends2['aria-
|
|
2617
|
+
}), _extends2.id = elementIds.menuId, _extends2.role = 'listbox', _extends2['aria-label'] = ariaLabel, _extends2['aria-labelledby'] = ariaLabel ? undefined : "" + elementIds.labelId, _extends2.onMouseLeave = callAllEventHandlers(onMouseLeave, menuHandleMouseLeave), _extends2), rest);
|
|
2710
2618
|
}, [dispatch, setGetterPropCallInfo, elementIds]);
|
|
2711
|
-
var getToggleButtonProps = React.useCallback(function (
|
|
2619
|
+
var getToggleButtonProps = React.useCallback(function (toggleButtonProps, otherProps) {
|
|
2712
2620
|
var _extends3;
|
|
2713
|
-
var _ref4 =
|
|
2621
|
+
var _ref4 = toggleButtonProps != null ? toggleButtonProps : {},
|
|
2714
2622
|
onBlur = _ref4.onBlur,
|
|
2715
2623
|
onClick = _ref4.onClick;
|
|
2716
2624
|
_ref4.onPress;
|
|
@@ -2718,8 +2626,9 @@ function useSelect(userProps) {
|
|
|
2718
2626
|
_ref4$refKey = _ref4.refKey,
|
|
2719
2627
|
refKey = _ref4$refKey === void 0 ? 'ref' : _ref4$refKey,
|
|
2720
2628
|
ref = _ref4.ref,
|
|
2629
|
+
disabled = _ref4.disabled,
|
|
2721
2630
|
rest = _objectWithoutPropertiesLoose(_ref4, _excluded3$2);
|
|
2722
|
-
var _ref5 =
|
|
2631
|
+
var _ref5 = otherProps != null ? otherProps : {},
|
|
2723
2632
|
_ref5$suppressRefErro = _ref5.suppressRefError,
|
|
2724
2633
|
suppressRefError = _ref5$suppressRefErro === void 0 ? false : _ref5$suppressRefErro;
|
|
2725
2634
|
var latestState = latest.current.state;
|
|
@@ -2737,9 +2646,9 @@ function useSelect(userProps) {
|
|
|
2737
2646
|
};
|
|
2738
2647
|
var toggleButtonHandleKeyDown = function toggleButtonHandleKeyDown(event) {
|
|
2739
2648
|
var key = normalizeArrowKey(event);
|
|
2740
|
-
if (key && toggleButtonKeyDownHandlers
|
|
2649
|
+
if (key && key in toggleButtonKeyDownHandlers) {
|
|
2741
2650
|
toggleButtonKeyDownHandlers[key](event);
|
|
2742
|
-
} else if (
|
|
2651
|
+
} else if (/^\S{1}$/.test(key)) {
|
|
2743
2652
|
dispatch({
|
|
2744
2653
|
type: ToggleButtonKeyDownCharacter,
|
|
2745
2654
|
key: key
|
|
@@ -2748,20 +2657,22 @@ function useSelect(userProps) {
|
|
|
2748
2657
|
};
|
|
2749
2658
|
var toggleProps = _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (toggleButtonNode) {
|
|
2750
2659
|
toggleButtonRef.current = toggleButtonNode;
|
|
2751
|
-
}), _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
|
|
2752
|
-
if (!
|
|
2660
|
+
}), _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['aria-label'] ? undefined : "" + elementIds.labelId, _extends3.id = elementIds.toggleButtonId, _extends3.role = 'combobox', _extends3.tabIndex = 0, _extends3.onBlur = callAllEventHandlers(onBlur, toggleButtonHandleBlur), _extends3.disabled = disabled, _extends3), rest);
|
|
2661
|
+
if (!disabled) {
|
|
2753
2662
|
/* istanbul ignore if (react-native) */
|
|
2754
2663
|
{
|
|
2755
|
-
|
|
2756
|
-
|
|
2664
|
+
Object.assign(toggleProps, {
|
|
2665
|
+
onClick: callAllEventHandlers(onClick, toggleButtonHandleClick),
|
|
2666
|
+
onKeyDown: callAllEventHandlers(onKeyDown, toggleButtonHandleKeyDown)
|
|
2667
|
+
});
|
|
2757
2668
|
}
|
|
2758
2669
|
}
|
|
2759
2670
|
setGetterPropCallInfo('getToggleButtonProps', suppressRefError, refKey, toggleButtonRef);
|
|
2760
2671
|
return toggleProps;
|
|
2761
2672
|
}, [dispatch, elementIds, latest, mouseAndTouchTrackers, setGetterPropCallInfo, toggleButtonKeyDownHandlers]);
|
|
2762
|
-
var getItemProps = React.useCallback(function (
|
|
2673
|
+
var getItemProps = React.useCallback(function (itemProps) {
|
|
2763
2674
|
var _extends4;
|
|
2764
|
-
var _ref6 =
|
|
2675
|
+
var _ref6 = itemProps != null ? itemProps : {},
|
|
2765
2676
|
itemProp = _ref6.item,
|
|
2766
2677
|
indexProp = _ref6.index,
|
|
2767
2678
|
onMouseMove = _ref6.onMouseMove,
|
|
@@ -2787,7 +2698,7 @@ function useSelect(userProps) {
|
|
|
2787
2698
|
if (mouseAndTouchTrackers.isTouchEnd || index === latestState.highlightedIndex) {
|
|
2788
2699
|
return;
|
|
2789
2700
|
}
|
|
2790
|
-
|
|
2701
|
+
preventScroll();
|
|
2791
2702
|
dispatch({
|
|
2792
2703
|
type: ItemMouseMove$1,
|
|
2793
2704
|
index: index,
|
|
@@ -2804,21 +2715,61 @@ function useSelect(userProps) {
|
|
|
2804
2715
|
return e.preventDefault();
|
|
2805
2716
|
}; // keep focus on the toggle after item click select.
|
|
2806
2717
|
|
|
2807
|
-
var
|
|
2718
|
+
var resultItemProps = _extends((_extends4 = {}, _extends4[refKey] = handleRefs(ref, function (itemNode) {
|
|
2808
2719
|
if (itemNode) {
|
|
2809
|
-
|
|
2720
|
+
itemsRef.current[elementIds.getItemId(index)] = itemNode;
|
|
2810
2721
|
}
|
|
2811
|
-
}), _extends4['aria-disabled'] = disabled, _extends4['aria-selected'] = item === latestState.selectedItem, _extends4.id = elementIds.getItemId(index), _extends4.role = 'option', _extends4), rest);
|
|
2722
|
+
}), _extends4['aria-disabled'] = disabled, _extends4['aria-selected'] = item === latestState.selectedItem, _extends4.id = elementIds.getItemId(index), _extends4.role = 'option', _extends4.onMouseMove = callAllEventHandlers(onMouseMove, itemHandleMouseMove), _extends4.onMouseDown = callAllEventHandlers(onMouseDown, itemHandleMouseDown), _extends4), rest);
|
|
2812
2723
|
if (!disabled) {
|
|
2813
2724
|
/* istanbul ignore next (react-native) */
|
|
2814
2725
|
{
|
|
2815
|
-
|
|
2726
|
+
Object.assign(resultItemProps, {
|
|
2727
|
+
onClick: callAllEventHandlers(onClick, itemHandleClick)
|
|
2728
|
+
});
|
|
2816
2729
|
}
|
|
2817
2730
|
}
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2731
|
+
return resultItemProps;
|
|
2732
|
+
}, [latest, elementIds, mouseAndTouchTrackers, preventScroll, dispatch]);
|
|
2733
|
+
|
|
2734
|
+
// Action functions.
|
|
2735
|
+
var toggleMenu = React.useCallback(function () {
|
|
2736
|
+
dispatch({
|
|
2737
|
+
type: FunctionToggleMenu$1
|
|
2738
|
+
});
|
|
2739
|
+
}, [dispatch]);
|
|
2740
|
+
var closeMenu = React.useCallback(function () {
|
|
2741
|
+
dispatch({
|
|
2742
|
+
type: FunctionCloseMenu$1
|
|
2743
|
+
});
|
|
2744
|
+
}, [dispatch]);
|
|
2745
|
+
var openMenu = React.useCallback(function () {
|
|
2746
|
+
dispatch({
|
|
2747
|
+
type: FunctionOpenMenu$1
|
|
2748
|
+
});
|
|
2749
|
+
}, [dispatch]);
|
|
2750
|
+
var setHighlightedIndex = React.useCallback(function (newHighlightedIndex) {
|
|
2751
|
+
dispatch({
|
|
2752
|
+
type: FunctionSetHighlightedIndex$1,
|
|
2753
|
+
highlightedIndex: newHighlightedIndex
|
|
2754
|
+
});
|
|
2755
|
+
}, [dispatch]);
|
|
2756
|
+
var selectItem = React.useCallback(function (newSelectedItem) {
|
|
2757
|
+
dispatch({
|
|
2758
|
+
type: FunctionSelectItem$1,
|
|
2759
|
+
selectedItem: newSelectedItem
|
|
2760
|
+
});
|
|
2761
|
+
}, [dispatch]);
|
|
2762
|
+
var reset = React.useCallback(function () {
|
|
2763
|
+
dispatch({
|
|
2764
|
+
type: FunctionReset$2
|
|
2765
|
+
});
|
|
2766
|
+
}, [dispatch]);
|
|
2767
|
+
var setInputValue = React.useCallback(function (newInputValue) {
|
|
2768
|
+
dispatch({
|
|
2769
|
+
type: FunctionSetInputValue$1,
|
|
2770
|
+
inputValue: newInputValue
|
|
2771
|
+
});
|
|
2772
|
+
}, [dispatch]);
|
|
2822
2773
|
return {
|
|
2823
2774
|
// prop getters.
|
|
2824
2775
|
getToggleButtonProps: getToggleButtonProps,
|
|
@@ -2841,6 +2792,18 @@ function useSelect(userProps) {
|
|
|
2841
2792
|
};
|
|
2842
2793
|
}
|
|
2843
2794
|
|
|
2795
|
+
function getInitialState$2(props) {
|
|
2796
|
+
var initialState = getInitialState$3(props);
|
|
2797
|
+
var selectedItem = initialState.selectedItem;
|
|
2798
|
+
var inputValue = initialState.inputValue;
|
|
2799
|
+
if (inputValue === '' && selectedItem && props.defaultInputValue === undefined && props.initialInputValue === undefined && props.inputValue === undefined) {
|
|
2800
|
+
inputValue = props.itemToString(selectedItem);
|
|
2801
|
+
}
|
|
2802
|
+
return _extends({}, initialState, {
|
|
2803
|
+
inputValue: inputValue
|
|
2804
|
+
});
|
|
2805
|
+
}
|
|
2806
|
+
|
|
2844
2807
|
var InputKeyDownArrowDown = process.env.NODE_ENV !== "production" ? '__input_keydown_arrow_down__' : 0;
|
|
2845
2808
|
var InputKeyDownArrowUp = process.env.NODE_ENV !== "production" ? '__input_keydown_arrow_up__' : 1;
|
|
2846
2809
|
var InputKeyDownEscape = process.env.NODE_ENV !== "production" ? '__input_keydown_escape__' : 2;
|
|
@@ -2892,27 +2855,6 @@ var stateChangeTypes$2 = /*#__PURE__*/Object.freeze({
|
|
|
2892
2855
|
ToggleButtonClick: ToggleButtonClick
|
|
2893
2856
|
});
|
|
2894
2857
|
|
|
2895
|
-
function getInitialState$2(props) {
|
|
2896
|
-
var initialState = getInitialState$3(props);
|
|
2897
|
-
var selectedItem = initialState.selectedItem;
|
|
2898
|
-
var inputValue = initialState.inputValue;
|
|
2899
|
-
if (inputValue === '' && selectedItem && props.defaultInputValue === undefined && props.initialInputValue === undefined && props.inputValue === undefined) {
|
|
2900
|
-
inputValue = props.itemToString(selectedItem);
|
|
2901
|
-
}
|
|
2902
|
-
return _extends({}, initialState, {
|
|
2903
|
-
inputValue: inputValue
|
|
2904
|
-
});
|
|
2905
|
-
}
|
|
2906
|
-
var propTypes$2 = _extends({}, propTypes$4, {
|
|
2907
|
-
items: PropTypes.array.isRequired,
|
|
2908
|
-
isItemDisabled: PropTypes.func,
|
|
2909
|
-
inputValue: PropTypes.string,
|
|
2910
|
-
defaultInputValue: PropTypes.string,
|
|
2911
|
-
initialInputValue: PropTypes.string,
|
|
2912
|
-
inputId: PropTypes.string,
|
|
2913
|
-
onInputValueChange: PropTypes.func
|
|
2914
|
-
});
|
|
2915
|
-
|
|
2916
2858
|
/**
|
|
2917
2859
|
* The useCombobox version of useControlledReducer, which also
|
|
2918
2860
|
* checks if the controlled prop selectedItem changed between
|
|
@@ -2920,20 +2862,20 @@ var propTypes$2 = _extends({}, propTypes$4, {
|
|
|
2920
2862
|
* string equivalent. It uses the common useEnhancedReducer to
|
|
2921
2863
|
* compute the rest of the state.
|
|
2922
2864
|
*
|
|
2923
|
-
* @param
|
|
2924
|
-
* @param
|
|
2925
|
-
* @param
|
|
2926
|
-
* @param
|
|
2927
|
-
* @returns
|
|
2865
|
+
* @param reducer Reducer function from downshift.
|
|
2866
|
+
* @param props The hook props, also passed to createInitialState.
|
|
2867
|
+
* @param createInitialState Function that returns the initial state.
|
|
2868
|
+
* @param isStateEqual Function that checks if a previous state is equal to the next.
|
|
2869
|
+
* @returns An array with the state and an action dispatcher.
|
|
2928
2870
|
*/
|
|
2929
2871
|
function useControlledReducer(reducer, props, createInitialState, isStateEqual) {
|
|
2930
|
-
var previousSelectedItemRef = React.useRef();
|
|
2872
|
+
var previousSelectedItemRef = React.useRef(null);
|
|
2931
2873
|
var _useEnhancedReducer = useEnhancedReducer(reducer, props, createInitialState, isStateEqual),
|
|
2932
2874
|
state = _useEnhancedReducer[0],
|
|
2933
2875
|
dispatch = _useEnhancedReducer[1];
|
|
2934
2876
|
var isInitialMount = useIsInitialMount();
|
|
2935
2877
|
React.useEffect(function () {
|
|
2936
|
-
if (
|
|
2878
|
+
if (props.selectedItem === undefined) {
|
|
2937
2879
|
return;
|
|
2938
2880
|
}
|
|
2939
2881
|
if (!isInitialMount // on first mount we already have the proper inputValue for a initial selected item.
|
|
@@ -2951,23 +2893,27 @@ function useControlledReducer(reducer, props, createInitialState, isStateEqual)
|
|
|
2951
2893
|
}, [state.selectedItem, props.selectedItem]);
|
|
2952
2894
|
return [getState(state, props), dispatch];
|
|
2953
2895
|
}
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2896
|
+
|
|
2897
|
+
var propTypes$2 = _extends({}, dropdownPropTypes, {
|
|
2898
|
+
items: PropTypes.array.isRequired,
|
|
2899
|
+
isItemDisabled: PropTypes.func,
|
|
2900
|
+
inputValue: PropTypes.string,
|
|
2901
|
+
defaultInputValue: PropTypes.string,
|
|
2902
|
+
initialInputValue: PropTypes.string,
|
|
2903
|
+
inputId: PropTypes.string,
|
|
2904
|
+
onInputValueChange: PropTypes.func
|
|
2958
2905
|
});
|
|
2959
2906
|
|
|
2960
2907
|
/* eslint-disable complexity */
|
|
2961
|
-
function downshiftUseComboboxReducer(state,
|
|
2962
|
-
var _props$items;
|
|
2908
|
+
function downshiftUseComboboxReducer(state, action) {
|
|
2963
2909
|
var type = action.type,
|
|
2964
|
-
|
|
2910
|
+
props = action.props;
|
|
2965
2911
|
var changes;
|
|
2966
2912
|
switch (type) {
|
|
2967
2913
|
case ItemClick:
|
|
2968
2914
|
changes = {
|
|
2969
|
-
isOpen: getDefaultValue
|
|
2970
|
-
highlightedIndex: getDefaultHighlightedIndex(props),
|
|
2915
|
+
isOpen: getDefaultValue(props.defaultIsOpen, dropdownDefaultStateValues.isOpen),
|
|
2916
|
+
highlightedIndex: getDefaultHighlightedIndex(props.items, props.isItemDisabled, props.defaultHighlightedIndex),
|
|
2971
2917
|
selectedItem: props.items[action.index],
|
|
2972
2918
|
inputValue: props.itemToString(props.items[action.index])
|
|
2973
2919
|
};
|
|
@@ -2979,15 +2925,15 @@ function downshiftUseComboboxReducer(state, props, action) {
|
|
|
2979
2925
|
};
|
|
2980
2926
|
} else {
|
|
2981
2927
|
changes = {
|
|
2982
|
-
highlightedIndex: altKey && state.selectedItem == null ? -1 : getHighlightedIndexOnOpen(props, state, 1),
|
|
2928
|
+
highlightedIndex: action.altKey && state.selectedItem == null ? -1 : getHighlightedIndexOnOpen(props.items, props.initialHighlightedIndex, props.defaultHighlightedIndex, props.isItemDisabled, props.itemToKey, state.selectedItem, state.highlightedIndex, 1),
|
|
2983
2929
|
isOpen: props.items.length >= 0
|
|
2984
2930
|
};
|
|
2985
2931
|
}
|
|
2986
2932
|
break;
|
|
2987
2933
|
case InputKeyDownArrowUp:
|
|
2988
2934
|
if (state.isOpen) {
|
|
2989
|
-
if (altKey) {
|
|
2990
|
-
changes = getChangesOnSelection(props, state.highlightedIndex);
|
|
2935
|
+
if (action.altKey) {
|
|
2936
|
+
changes = getChangesOnSelection(props.items, props.itemToString, props.defaultIsOpen, props.defaultHighlightedIndex, state.highlightedIndex, true);
|
|
2991
2937
|
} else {
|
|
2992
2938
|
changes = {
|
|
2993
2939
|
highlightedIndex: getHighlightedIndex(state.highlightedIndex, -1, props.items, props.isItemDisabled, true)
|
|
@@ -2995,13 +2941,13 @@ function downshiftUseComboboxReducer(state, props, action) {
|
|
|
2995
2941
|
}
|
|
2996
2942
|
} else {
|
|
2997
2943
|
changes = {
|
|
2998
|
-
highlightedIndex: getHighlightedIndexOnOpen(props, state, -1),
|
|
2944
|
+
highlightedIndex: getHighlightedIndexOnOpen(props.items, props.initialHighlightedIndex, props.defaultHighlightedIndex, props.isItemDisabled, props.itemToKey, state.selectedItem, state.highlightedIndex, -1),
|
|
2999
2945
|
isOpen: props.items.length >= 0
|
|
3000
2946
|
};
|
|
3001
2947
|
}
|
|
3002
2948
|
break;
|
|
3003
2949
|
case InputKeyDownEnter:
|
|
3004
|
-
changes = getChangesOnSelection(props, state.highlightedIndex);
|
|
2950
|
+
changes = getChangesOnSelection(props.items, props.itemToString, props.defaultIsOpen, props.defaultHighlightedIndex, state.highlightedIndex, true);
|
|
3005
2951
|
break;
|
|
3006
2952
|
case InputKeyDownEscape:
|
|
3007
2953
|
changes = _extends({
|
|
@@ -3036,7 +2982,7 @@ function downshiftUseComboboxReducer(state, props, action) {
|
|
|
3036
2982
|
changes = _extends({
|
|
3037
2983
|
isOpen: false,
|
|
3038
2984
|
highlightedIndex: -1
|
|
3039
|
-
}, state.highlightedIndex >= 0 &&
|
|
2985
|
+
}, state.highlightedIndex >= 0 && props.items.length && action.selectItem && {
|
|
3040
2986
|
selectedItem: props.items[state.highlightedIndex],
|
|
3041
2987
|
inputValue: props.itemToString(props.items[state.highlightedIndex])
|
|
3042
2988
|
});
|
|
@@ -3044,14 +2990,14 @@ function downshiftUseComboboxReducer(state, props, action) {
|
|
|
3044
2990
|
case InputChange:
|
|
3045
2991
|
changes = {
|
|
3046
2992
|
isOpen: true,
|
|
3047
|
-
highlightedIndex: getDefaultHighlightedIndex(props),
|
|
2993
|
+
highlightedIndex: getDefaultHighlightedIndex(props.items, props.isItemDisabled, props.defaultHighlightedIndex),
|
|
3048
2994
|
inputValue: action.inputValue
|
|
3049
2995
|
};
|
|
3050
2996
|
break;
|
|
3051
2997
|
case InputClick:
|
|
3052
2998
|
changes = {
|
|
3053
2999
|
isOpen: !state.isOpen,
|
|
3054
|
-
highlightedIndex: state.isOpen ? -1 : getHighlightedIndexOnOpen(props, state, 0)
|
|
3000
|
+
highlightedIndex: state.isOpen ? -1 : getHighlightedIndexOnOpen(props.items, props.initialHighlightedIndex, props.defaultHighlightedIndex, props.isItemDisabled, props.itemToKey, state.selectedItem, state.highlightedIndex, 0)
|
|
3055
3001
|
};
|
|
3056
3002
|
break;
|
|
3057
3003
|
case FunctionSelectItem:
|
|
@@ -3072,10 +3018,10 @@ function downshiftUseComboboxReducer(state, props, action) {
|
|
|
3072
3018
|
}
|
|
3073
3019
|
/* eslint-enable complexity */
|
|
3074
3020
|
|
|
3075
|
-
var _excluded$2 = ["onMouseLeave", "refKey", "ref"],
|
|
3021
|
+
var _excluded$2 = ["onMouseLeave", "refKey", "ref", "aria-label"],
|
|
3076
3022
|
_excluded2$2 = ["item", "index", "refKey", "ref", "onMouseMove", "onMouseDown", "onClick", "onPress", "disabled"],
|
|
3077
|
-
_excluded3$1 = ["onClick", "onPress", "refKey", "ref"],
|
|
3078
|
-
_excluded4 = ["onKeyDown", "onChange", "onInput", "onBlur", "onChangeText", "onClick", "refKey", "ref"];
|
|
3023
|
+
_excluded3$1 = ["onClick", "onPress", "refKey", "ref", "disabled"],
|
|
3024
|
+
_excluded4 = ["aria-label", "disabled", "onKeyDown", "onChange", "onInput", "onBlur", "onChangeText", "onClick", "refKey", "ref"];
|
|
3079
3025
|
useCombobox.stateChangeTypes = stateChangeTypes$2;
|
|
3080
3026
|
function useCombobox(userProps) {
|
|
3081
3027
|
if (userProps === void 0) {
|
|
@@ -3083,13 +3029,13 @@ function useCombobox(userProps) {
|
|
|
3083
3029
|
}
|
|
3084
3030
|
validatePropTypes$1(userProps, useCombobox, propTypes$2);
|
|
3085
3031
|
// Props defaults and destructuring.
|
|
3086
|
-
var props = _extends({},
|
|
3032
|
+
var props = _extends({}, dropdownDefaultProps, userProps);
|
|
3087
3033
|
var items = props.items,
|
|
3088
3034
|
scrollIntoView = props.scrollIntoView,
|
|
3089
3035
|
environment = props.environment,
|
|
3090
3036
|
getA11yStatusMessage = props.getA11yStatusMessage;
|
|
3091
3037
|
// Initial state depending on controlled props.
|
|
3092
|
-
var _useControlledReducer = useControlledReducer(downshiftUseComboboxReducer, props, getInitialState$2,
|
|
3038
|
+
var _useControlledReducer = useControlledReducer(downshiftUseComboboxReducer, props, getInitialState$2, isDropdownStateEqual),
|
|
3093
3039
|
state = _useControlledReducer[0],
|
|
3094
3040
|
dispatch = _useControlledReducer[1];
|
|
3095
3041
|
var isOpen = state.isOpen,
|
|
@@ -3099,7 +3045,7 @@ function useCombobox(userProps) {
|
|
|
3099
3045
|
|
|
3100
3046
|
// Element refs.
|
|
3101
3047
|
var menuRef = React.useRef(null);
|
|
3102
|
-
var
|
|
3048
|
+
var itemsRef = React.useRef({});
|
|
3103
3049
|
var inputRef = React.useRef(null);
|
|
3104
3050
|
var toggleButtonRef = React.useRef(null);
|
|
3105
3051
|
var isInitialMount = useIsInitialMount();
|
|
@@ -3113,29 +3059,19 @@ function useCombobox(userProps) {
|
|
|
3113
3059
|
state: state,
|
|
3114
3060
|
props: props
|
|
3115
3061
|
});
|
|
3116
|
-
var getItemNodeFromIndex = React.useCallback(function (index) {
|
|
3117
|
-
return itemRefs.current[elementIds.getItemId(index)];
|
|
3118
|
-
}, [elementIds]);
|
|
3119
3062
|
|
|
3120
3063
|
// Effects.
|
|
3121
3064
|
// Adds an a11y aria live status message if getA11yStatusMessage is passed.
|
|
3122
3065
|
useA11yMessageStatus(getA11yStatusMessage, state, [isOpen, highlightedIndex, selectedItem, inputValue], environment);
|
|
3123
3066
|
// Scroll on highlighted item if change comes from keyboard.
|
|
3124
|
-
var
|
|
3125
|
-
menuElement: menuRef.current,
|
|
3126
|
-
highlightedIndex: highlightedIndex,
|
|
3127
|
-
isOpen: isOpen,
|
|
3128
|
-
itemRefs: itemRefs,
|
|
3129
|
-
scrollIntoView: scrollIntoView,
|
|
3130
|
-
getItemNodeFromIndex: getItemNodeFromIndex
|
|
3131
|
-
});
|
|
3067
|
+
var preventScroll = useScrollIntoView(scrollIntoView, highlightedIndex, isOpen, menuRef.current, itemsRef.current, elementIds.getItemId);
|
|
3132
3068
|
useControlPropsValidator({
|
|
3133
|
-
|
|
3134
|
-
|
|
3069
|
+
state: state,
|
|
3070
|
+
props: props
|
|
3135
3071
|
});
|
|
3136
3072
|
// Focus the input on first render if required.
|
|
3137
3073
|
React.useEffect(function () {
|
|
3138
|
-
var focusOnOpen = getInitialValue
|
|
3074
|
+
var focusOnOpen = getInitialValue(props.isOpen, props.initialIsOpen, props.defaultIsOpen, dropdownDefaultStateValues.isOpen);
|
|
3139
3075
|
if (focusOnOpen && inputRef.current) {
|
|
3140
3076
|
inputRef.current.focus();
|
|
3141
3077
|
}
|
|
@@ -3161,13 +3097,13 @@ function useCombobox(userProps) {
|
|
|
3161
3097
|
// Reset itemRefs on close.
|
|
3162
3098
|
React.useEffect(function () {
|
|
3163
3099
|
if (!isOpen) {
|
|
3164
|
-
|
|
3100
|
+
itemsRef.current = {};
|
|
3165
3101
|
}
|
|
3166
3102
|
}, [isOpen]);
|
|
3167
3103
|
// Reset itemRefs on close.
|
|
3168
3104
|
React.useEffect(function () {
|
|
3169
3105
|
var _inputRef$current;
|
|
3170
|
-
if (!isOpen || !(environment != null && environment.document) || !(
|
|
3106
|
+
if (!isOpen || !(environment != null && environment.document) || !((_inputRef$current = inputRef.current) != null && _inputRef$current.focus)) {
|
|
3171
3107
|
return;
|
|
3172
3108
|
}
|
|
3173
3109
|
if (environment.document.activeElement !== inputRef.current) {
|
|
@@ -3257,29 +3193,30 @@ function useCombobox(userProps) {
|
|
|
3257
3193
|
htmlFor: elementIds.inputId
|
|
3258
3194
|
}, labelProps);
|
|
3259
3195
|
}, [elementIds]);
|
|
3260
|
-
var getMenuProps = React.useCallback(function (
|
|
3196
|
+
var getMenuProps = React.useCallback(function (menuProps, otherProps) {
|
|
3261
3197
|
var _extends2;
|
|
3262
|
-
var _ref =
|
|
3198
|
+
var _ref = menuProps != null ? menuProps : {},
|
|
3263
3199
|
onMouseLeave = _ref.onMouseLeave,
|
|
3264
3200
|
_ref$refKey = _ref.refKey,
|
|
3265
3201
|
refKey = _ref$refKey === void 0 ? 'ref' : _ref$refKey,
|
|
3266
3202
|
ref = _ref.ref,
|
|
3203
|
+
ariaLabel = _ref['aria-label'],
|
|
3267
3204
|
rest = _objectWithoutPropertiesLoose(_ref, _excluded$2);
|
|
3268
|
-
var _ref2 =
|
|
3205
|
+
var _ref2 = otherProps != null ? otherProps : {},
|
|
3269
3206
|
_ref2$suppressRefErro = _ref2.suppressRefError,
|
|
3270
3207
|
suppressRefError = _ref2$suppressRefErro === void 0 ? false : _ref2$suppressRefErro;
|
|
3271
3208
|
setGetterPropCallInfo('getMenuProps', suppressRefError, refKey, menuRef);
|
|
3272
3209
|
return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
|
|
3273
3210
|
menuRef.current = menuNode;
|
|
3274
|
-
}), _extends2.id = elementIds.menuId, _extends2.role = 'listbox', _extends2['aria-
|
|
3211
|
+
}), _extends2.id = elementIds.menuId, _extends2.role = 'listbox', _extends2['aria-label'] = ariaLabel, _extends2['aria-labelledby'] = ariaLabel ? undefined : "" + elementIds.labelId, _extends2.onMouseLeave = callAllEventHandlers(onMouseLeave, function () {
|
|
3275
3212
|
dispatch({
|
|
3276
3213
|
type: MenuMouseLeave
|
|
3277
3214
|
});
|
|
3278
3215
|
}), _extends2), rest);
|
|
3279
3216
|
}, [dispatch, setGetterPropCallInfo, elementIds]);
|
|
3280
|
-
var getItemProps = React.useCallback(function (
|
|
3217
|
+
var getItemProps = React.useCallback(function (itemProps) {
|
|
3281
3218
|
var _extends3, _ref4;
|
|
3282
|
-
var _ref3 =
|
|
3219
|
+
var _ref3 = itemProps != null ? itemProps : {},
|
|
3283
3220
|
itemProp = _ref3.item,
|
|
3284
3221
|
indexProp = _ref3.index,
|
|
3285
3222
|
_ref3$refKey = _ref3.refKey,
|
|
@@ -3307,7 +3244,7 @@ function useCombobox(userProps) {
|
|
|
3307
3244
|
if (mouseAndTouchTrackers.isTouchEnd || index === latestState.highlightedIndex) {
|
|
3308
3245
|
return;
|
|
3309
3246
|
}
|
|
3310
|
-
|
|
3247
|
+
preventScroll();
|
|
3311
3248
|
dispatch({
|
|
3312
3249
|
type: ItemMouseMove,
|
|
3313
3250
|
index: index,
|
|
@@ -3326,21 +3263,22 @@ function useCombobox(userProps) {
|
|
|
3326
3263
|
|
|
3327
3264
|
return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (itemNode) {
|
|
3328
3265
|
if (itemNode) {
|
|
3329
|
-
|
|
3266
|
+
itemsRef.current[elementIds.getItemId(index)] = itemNode;
|
|
3330
3267
|
}
|
|
3331
3268
|
}), _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), {
|
|
3332
3269
|
onMouseMove: callAllEventHandlers(onMouseMove, itemHandleMouseMove),
|
|
3333
3270
|
onMouseDown: callAllEventHandlers(onMouseDown, itemHandleMouseDown)
|
|
3334
3271
|
}, rest);
|
|
3335
|
-
}, [dispatch, elementIds, latest, mouseAndTouchTrackers,
|
|
3336
|
-
var getToggleButtonProps = React.useCallback(function (
|
|
3272
|
+
}, [dispatch, elementIds, latest, mouseAndTouchTrackers, preventScroll]);
|
|
3273
|
+
var getToggleButtonProps = React.useCallback(function (toggleButtonProps) {
|
|
3337
3274
|
var _extends4;
|
|
3338
|
-
var _ref5 =
|
|
3275
|
+
var _ref5 = toggleButtonProps != null ? toggleButtonProps : {},
|
|
3339
3276
|
onClick = _ref5.onClick;
|
|
3340
3277
|
_ref5.onPress;
|
|
3341
3278
|
var _ref5$refKey = _ref5.refKey,
|
|
3342
3279
|
refKey = _ref5$refKey === void 0 ? 'ref' : _ref5$refKey,
|
|
3343
3280
|
ref = _ref5.ref,
|
|
3281
|
+
disabled = _ref5.disabled,
|
|
3344
3282
|
rest = _objectWithoutPropertiesLoose(_ref5, _excluded3$1);
|
|
3345
3283
|
var latestState = latest.current.state;
|
|
3346
3284
|
var toggleButtonHandleClick = function toggleButtonHandleClick() {
|
|
@@ -3350,13 +3288,17 @@ function useCombobox(userProps) {
|
|
|
3350
3288
|
};
|
|
3351
3289
|
return _extends((_extends4 = {}, _extends4[refKey] = handleRefs(ref, function (toggleButtonNode) {
|
|
3352
3290
|
toggleButtonRef.current = toggleButtonNode;
|
|
3353
|
-
}), _extends4['aria-controls'] = elementIds.menuId, _extends4['aria-expanded'] = latestState.isOpen, _extends4.id = elementIds.toggleButtonId, _extends4.tabIndex = -1, _extends4), !
|
|
3291
|
+
}), _extends4['aria-controls'] = elementIds.menuId, _extends4['aria-expanded'] = latestState.isOpen, _extends4.id = elementIds.toggleButtonId, _extends4.tabIndex = -1, _extends4), !disabled && _extends({}, {
|
|
3354
3292
|
onClick: callAllEventHandlers(onClick, toggleButtonHandleClick)
|
|
3355
|
-
}),
|
|
3293
|
+
}), {
|
|
3294
|
+
disabled: disabled
|
|
3295
|
+
}, rest);
|
|
3356
3296
|
}, [dispatch, latest, elementIds]);
|
|
3357
|
-
var getInputProps = React.useCallback(function (
|
|
3297
|
+
var getInputProps = React.useCallback(function (inputProps, otherProps) {
|
|
3358
3298
|
var _extends5;
|
|
3359
|
-
var _ref6 =
|
|
3299
|
+
var _ref6 = inputProps != null ? inputProps : {},
|
|
3300
|
+
ariaLabel = _ref6['aria-label'],
|
|
3301
|
+
disabled = _ref6.disabled,
|
|
3360
3302
|
onKeyDown = _ref6.onKeyDown,
|
|
3361
3303
|
onChange = _ref6.onChange,
|
|
3362
3304
|
onInput = _ref6.onInput,
|
|
@@ -3367,15 +3309,16 @@ function useCombobox(userProps) {
|
|
|
3367
3309
|
refKey = _ref6$refKey === void 0 ? 'ref' : _ref6$refKey,
|
|
3368
3310
|
ref = _ref6.ref,
|
|
3369
3311
|
rest = _objectWithoutPropertiesLoose(_ref6, _excluded4);
|
|
3370
|
-
var _ref7 =
|
|
3312
|
+
var _ref7 = otherProps != null ? otherProps : {},
|
|
3371
3313
|
_ref7$suppressRefErro = _ref7.suppressRefError,
|
|
3372
3314
|
suppressRefError = _ref7$suppressRefErro === void 0 ? false : _ref7$suppressRefErro;
|
|
3373
3315
|
setGetterPropCallInfo('getInputProps', suppressRefError, refKey, inputRef);
|
|
3374
3316
|
var latestState = latest.current.state;
|
|
3375
3317
|
var inputHandleKeyDown = function inputHandleKeyDown(event) {
|
|
3376
3318
|
var key = normalizeArrowKey(event);
|
|
3377
|
-
if (key && inputKeyDownHandlers
|
|
3378
|
-
|
|
3319
|
+
if (key && key in inputKeyDownHandlers) {
|
|
3320
|
+
var validKey = key;
|
|
3321
|
+
inputKeyDownHandlers[validKey](event);
|
|
3379
3322
|
}
|
|
3380
3323
|
};
|
|
3381
3324
|
var inputHandleChange = function inputHandleChange(event) {
|
|
@@ -3403,13 +3346,13 @@ function useCombobox(userProps) {
|
|
|
3403
3346
|
/* istanbul ignore next (preact) */
|
|
3404
3347
|
var onChangeKey = 'onInput' ;
|
|
3405
3348
|
var eventHandlers = {};
|
|
3406
|
-
if (!
|
|
3349
|
+
if (!disabled) {
|
|
3407
3350
|
var _eventHandlers;
|
|
3408
3351
|
eventHandlers = (_eventHandlers = {}, _eventHandlers[onChangeKey] = callAllEventHandlers(onChange, onInput, inputHandleChange), _eventHandlers.onKeyDown = callAllEventHandlers(onKeyDown, inputHandleKeyDown), _eventHandlers.onBlur = callAllEventHandlers(onBlur, inputHandleBlur), _eventHandlers.onClick = callAllEventHandlers(onClick, inputHandleClick), _eventHandlers);
|
|
3409
3352
|
}
|
|
3410
3353
|
return _extends((_extends5 = {}, _extends5[refKey] = handleRefs(ref, function (inputNode) {
|
|
3411
3354
|
inputRef.current = inputNode;
|
|
3412
|
-
}), _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'] =
|
|
3355
|
+
}), _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'] = ariaLabel ? undefined : elementIds.labelId, _extends5['aria-label'] = ariaLabel, _extends5.autoComplete = 'off', _extends5.disabled = disabled, _extends5.id = elementIds.inputId, _extends5.role = 'combobox', _extends5.value = latestState.inputValue, _extends5), eventHandlers, rest);
|
|
3413
3356
|
}, [dispatch, elementIds, environment, inputKeyDownHandlers, latest, mouseAndTouchTrackers, setGetterPropCallInfo]);
|
|
3414
3357
|
|
|
3415
3358
|
// returns
|
|
@@ -3479,31 +3422,6 @@ var defaultStateValues = {
|
|
|
3479
3422
|
selectedItems: []
|
|
3480
3423
|
};
|
|
3481
3424
|
|
|
3482
|
-
/**
|
|
3483
|
-
* Returns the initial value for a state key in the following order:
|
|
3484
|
-
* 1. controlled prop, 2. initial prop, 3. default prop, 4. default
|
|
3485
|
-
* value from Downshift.
|
|
3486
|
-
*
|
|
3487
|
-
* @param {Object} props Props passed to the hook.
|
|
3488
|
-
* @param {string} propKey Props key to generate the value for.
|
|
3489
|
-
* @returns {any} The initial value for that prop.
|
|
3490
|
-
*/
|
|
3491
|
-
function getInitialValue(props, propKey) {
|
|
3492
|
-
return getInitialValue$1(props, propKey, defaultStateValues);
|
|
3493
|
-
}
|
|
3494
|
-
|
|
3495
|
-
/**
|
|
3496
|
-
* Returns the default value for a state key in the following order:
|
|
3497
|
-
* 1. controlled prop, 2. default prop, 3. default value from Downshift.
|
|
3498
|
-
*
|
|
3499
|
-
* @param {Object} props Props passed to the hook.
|
|
3500
|
-
* @param {string} propKey Props key to generate the value for.
|
|
3501
|
-
* @returns {any} The initial value for that prop.
|
|
3502
|
-
*/
|
|
3503
|
-
function getDefaultValue(props, propKey) {
|
|
3504
|
-
return getDefaultValue$1(props, propKey, defaultStateValues);
|
|
3505
|
-
}
|
|
3506
|
-
|
|
3507
3425
|
/**
|
|
3508
3426
|
* Gets the initial state based on the provided props. It uses initial, default
|
|
3509
3427
|
* and controlled props related to state in order to compute the initial value.
|
|
@@ -3512,8 +3430,8 @@ function getDefaultValue(props, propKey) {
|
|
|
3512
3430
|
* @returns {Object} The initial state.
|
|
3513
3431
|
*/
|
|
3514
3432
|
function getInitialState$1(props) {
|
|
3515
|
-
var activeIndex = getInitialValue(props,
|
|
3516
|
-
var selectedItems = getInitialValue(props,
|
|
3433
|
+
var activeIndex = getInitialValue(props.activeIndex, props.initialActiveIndex, props.defaultActiveIndex, defaultStateValues.activeIndex);
|
|
3434
|
+
var selectedItems = getInitialValue(props.selectedItems, props.initialSelectedItems, props.defaultSelectedItems, defaultStateValues.selectedItems);
|
|
3517
3435
|
return {
|
|
3518
3436
|
activeIndex: activeIndex,
|
|
3519
3437
|
selectedItems: selectedItems
|
|
@@ -3557,9 +3475,9 @@ function isStateEqual$1(prevState, newState) {
|
|
|
3557
3475
|
return prevState.selectedItems === newState.selectedItems && prevState.activeIndex === newState.activeIndex;
|
|
3558
3476
|
}
|
|
3559
3477
|
var propTypes$1 = {
|
|
3560
|
-
stateReducer:
|
|
3561
|
-
itemToKey:
|
|
3562
|
-
environment:
|
|
3478
|
+
stateReducer: dropdownPropTypes.stateReducer,
|
|
3479
|
+
itemToKey: dropdownPropTypes.itemToKey,
|
|
3480
|
+
environment: dropdownPropTypes.environment,
|
|
3563
3481
|
selectedItems: PropTypes.array,
|
|
3564
3482
|
initialSelectedItems: PropTypes.array,
|
|
3565
3483
|
defaultSelectedItems: PropTypes.array,
|
|
@@ -3573,9 +3491,9 @@ var propTypes$1 = {
|
|
|
3573
3491
|
keyNavigationPrevious: PropTypes.string
|
|
3574
3492
|
};
|
|
3575
3493
|
var defaultProps = {
|
|
3576
|
-
itemToKey:
|
|
3577
|
-
stateReducer:
|
|
3578
|
-
environment:
|
|
3494
|
+
itemToKey: dropdownDefaultProps.itemToKey,
|
|
3495
|
+
stateReducer: dropdownDefaultProps.stateReducer,
|
|
3496
|
+
environment: dropdownDefaultProps.environment,
|
|
3579
3497
|
keyNavigationNext: 'ArrowRight',
|
|
3580
3498
|
keyNavigationPrevious: 'ArrowLeft'
|
|
3581
3499
|
};
|
|
@@ -3621,10 +3539,11 @@ var stateChangeTypes$1 = /*#__PURE__*/Object.freeze({
|
|
|
3621
3539
|
});
|
|
3622
3540
|
|
|
3623
3541
|
/* eslint-disable complexity */
|
|
3624
|
-
function downshiftMultipleSelectionReducer(state,
|
|
3542
|
+
function downshiftMultipleSelectionReducer(state, action) {
|
|
3625
3543
|
var type = action.type,
|
|
3626
3544
|
index = action.index,
|
|
3627
|
-
selectedItem = action.selectedItem
|
|
3545
|
+
selectedItem = action.selectedItem,
|
|
3546
|
+
props = action.props;
|
|
3628
3547
|
var activeIndex = state.activeIndex,
|
|
3629
3548
|
selectedItems = state.selectedItems;
|
|
3630
3549
|
var changes;
|
|
@@ -3721,8 +3640,8 @@ function downshiftMultipleSelectionReducer(state, props, action) {
|
|
|
3721
3640
|
}
|
|
3722
3641
|
case FunctionReset:
|
|
3723
3642
|
changes = {
|
|
3724
|
-
activeIndex: getDefaultValue(props,
|
|
3725
|
-
selectedItems: getDefaultValue(props,
|
|
3643
|
+
activeIndex: getDefaultValue(props.defaultActiveIndex, defaultStateValues.activeIndex),
|
|
3644
|
+
selectedItems: getDefaultValue(props.defaultSelectedItems, defaultStateValues.selectedItems)
|
|
3726
3645
|
};
|
|
3727
3646
|
break;
|
|
3728
3647
|
default:
|
|
@@ -3954,7 +3873,7 @@ var stateChangeTypes = /*#__PURE__*/Object.freeze({
|
|
|
3954
3873
|
TagRemoveClick: TagRemoveClick
|
|
3955
3874
|
});
|
|
3956
3875
|
|
|
3957
|
-
function useTagGroupReducer(state,
|
|
3876
|
+
function useTagGroupReducer(state, action) {
|
|
3958
3877
|
var type = action.type;
|
|
3959
3878
|
var changes;
|
|
3960
3879
|
switch (type) {
|
|
@@ -4192,7 +4111,7 @@ var _useTagGroup = function useTagGroup(userProps) {
|
|
|
4192
4111
|
'aria-atomic': 'false',
|
|
4193
4112
|
'aria-relevant': 'additions',
|
|
4194
4113
|
role: 'listbox',
|
|
4195
|
-
onKeyDown: callAllEventHandlers
|
|
4114
|
+
onKeyDown: callAllEventHandlers(onKeyDown, handleKeyDown)
|
|
4196
4115
|
}, rest);
|
|
4197
4116
|
return tagGroupProps;
|
|
4198
4117
|
}, [dispatch, elementIds.tagGroupId]);
|
|
@@ -4217,11 +4136,11 @@ var _useTagGroup = function useTagGroup(userProps) {
|
|
|
4217
4136
|
var tagId = elementIds.getTagId(index);
|
|
4218
4137
|
return _extends((_extends2 = {
|
|
4219
4138
|
'aria-describedby': A11Y_DESCRIPTION_ELEMENT_ID
|
|
4220
|
-
}, _extends2[refKey] = handleRefs
|
|
4139
|
+
}, _extends2[refKey] = handleRefs(ref, function (itemNode) {
|
|
4221
4140
|
if (itemNode) {
|
|
4222
4141
|
itemRefs.current[tagId] = itemNode;
|
|
4223
4142
|
}
|
|
4224
|
-
}), _extends2['aria-labelledby'] = tagId, _extends2.role = 'option', _extends2.id = tagId, _extends2.onClick = callAllEventHandlers
|
|
4143
|
+
}), _extends2['aria-labelledby'] = tagId, _extends2.role = 'option', _extends2.id = tagId, _extends2.onClick = callAllEventHandlers(onClick, handleClick), _extends2.tabIndex = latestState.activeIndex === index ? 0 : -1, _extends2), rest);
|
|
4225
4144
|
}, [dispatch, elementIds, latest, itemRefs]);
|
|
4226
4145
|
var getTagRemoveProps = React.useCallback(function (options) {
|
|
4227
4146
|
var index = options.index,
|
|
@@ -4243,7 +4162,7 @@ var _useTagGroup = function useTagGroup(userProps) {
|
|
|
4243
4162
|
id: tagRemoveId,
|
|
4244
4163
|
tabIndex: -1,
|
|
4245
4164
|
'aria-labelledby': tagRemoveId + " " + tagId,
|
|
4246
|
-
onClick: callAllEventHandlers
|
|
4165
|
+
onClick: callAllEventHandlers(onClick, handleClick)
|
|
4247
4166
|
}, rest);
|
|
4248
4167
|
}, [elementIds, dispatch]);
|
|
4249
4168
|
|