downshift 7.3.2 → 7.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/downshift.cjs.js +64 -59
- package/dist/downshift.esm.js +64 -59
- package/dist/downshift.native.cjs.js +64 -59
- package/dist/downshift.umd.js +64 -59
- 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/src/hooks/utils.d.ts +10 -1
- package/package.json +1 -1
- package/preact/dist/downshift.cjs.js +64 -59
- package/preact/dist/downshift.esm.js +64 -59
- package/preact/dist/downshift.umd.js +64 -59
- 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
|
@@ -1836,14 +1836,19 @@
|
|
|
1836
1836
|
});
|
|
1837
1837
|
return elementIdsRef.current;
|
|
1838
1838
|
}
|
|
1839
|
-
function
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1839
|
+
function getItemAndIndex(itemProp, indexProp, items, errorMessage) {
|
|
1840
|
+
var item, index;
|
|
1841
|
+
if (itemProp === undefined) {
|
|
1842
|
+
if (indexProp === undefined) {
|
|
1843
|
+
throw new Error(errorMessage);
|
|
1844
|
+
}
|
|
1845
|
+
item = items[indexProp];
|
|
1846
|
+
index = indexProp;
|
|
1847
|
+
} else {
|
|
1848
|
+
index = indexProp === undefined ? items.indexOf(itemProp) : indexProp;
|
|
1849
|
+
item = itemProp;
|
|
1845
1850
|
}
|
|
1846
|
-
return
|
|
1851
|
+
return [item, index];
|
|
1847
1852
|
}
|
|
1848
1853
|
function itemToString(item) {
|
|
1849
1854
|
return item ? String(item) : '';
|
|
@@ -2172,6 +2177,32 @@
|
|
|
2172
2177
|
};
|
|
2173
2178
|
}
|
|
2174
2179
|
|
|
2180
|
+
/**
|
|
2181
|
+
* Handles selection on Enter / Alt + ArrowUp. Closes the menu and resets the highlighted index, unless there is a highlighted.
|
|
2182
|
+
* In that case, selects the item and resets to defaults for open state and highlighted idex.
|
|
2183
|
+
* @param {Object} props The useCombobox props.
|
|
2184
|
+
* @param {number} highlightedIndex The index from the state.
|
|
2185
|
+
* @param {boolean} inputValue Also return the input value for state.
|
|
2186
|
+
* @returns The changes for the state.
|
|
2187
|
+
*/
|
|
2188
|
+
function getChangesOnSelection(props, highlightedIndex, inputValue) {
|
|
2189
|
+
var _props$items;
|
|
2190
|
+
if (inputValue === void 0) {
|
|
2191
|
+
inputValue = true;
|
|
2192
|
+
}
|
|
2193
|
+
var shouldSelect = ((_props$items = props.items) == null ? void 0 : _props$items.length) && highlightedIndex >= 0;
|
|
2194
|
+
return _extends({
|
|
2195
|
+
isOpen: false,
|
|
2196
|
+
highlightedIndex: -1
|
|
2197
|
+
}, shouldSelect && _extends({
|
|
2198
|
+
selectedItem: props.items[highlightedIndex],
|
|
2199
|
+
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
2200
|
+
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
|
2201
|
+
}, inputValue && {
|
|
2202
|
+
inputValue: props.itemToString(props.items[highlightedIndex])
|
|
2203
|
+
}));
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2175
2206
|
function downshiftCommonReducer(state, action, stateChangeTypes) {
|
|
2176
2207
|
var type = action.type,
|
|
2177
2208
|
props = action.props;
|
|
@@ -2389,6 +2420,7 @@
|
|
|
2389
2420
|
|
|
2390
2421
|
/* eslint-disable complexity */
|
|
2391
2422
|
function downshiftSelectReducer(state, action) {
|
|
2423
|
+
var _props$items;
|
|
2392
2424
|
var type = action.type,
|
|
2393
2425
|
props = action.props,
|
|
2394
2426
|
altKey = action.altKey;
|
|
@@ -2431,12 +2463,7 @@
|
|
|
2431
2463
|
break;
|
|
2432
2464
|
case ToggleButtonKeyDownArrowUp:
|
|
2433
2465
|
if (state.isOpen && altKey) {
|
|
2434
|
-
changes =
|
|
2435
|
-
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
2436
|
-
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
|
2437
|
-
}, state.highlightedIndex >= 0 && {
|
|
2438
|
-
selectedItem: props.items[state.highlightedIndex]
|
|
2439
|
-
});
|
|
2466
|
+
changes = getChangesOnSelection(props, state.highlightedIndex, false);
|
|
2440
2467
|
} else {
|
|
2441
2468
|
var _highlightedIndex2 = state.isOpen ? getNextWrappingIndex(-1, state.highlightedIndex, props.items.length, action.getItemNodeFromIndex, false) : getHighlightedIndexOnOpen(props, state, -1);
|
|
2442
2469
|
changes = {
|
|
@@ -2448,12 +2475,7 @@
|
|
|
2448
2475
|
// only triggered when menu is open.
|
|
2449
2476
|
case ToggleButtonKeyDownEnter:
|
|
2450
2477
|
case ToggleButtonKeyDownSpaceButton:
|
|
2451
|
-
changes =
|
|
2452
|
-
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
2453
|
-
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
|
2454
|
-
}, state.highlightedIndex >= 0 && {
|
|
2455
|
-
selectedItem: props.items[state.highlightedIndex]
|
|
2456
|
-
});
|
|
2478
|
+
changes = getChangesOnSelection(props, state.highlightedIndex, false);
|
|
2457
2479
|
break;
|
|
2458
2480
|
case ToggleButtonKeyDownHome:
|
|
2459
2481
|
changes = {
|
|
@@ -2487,7 +2509,7 @@
|
|
|
2487
2509
|
changes = _extends({
|
|
2488
2510
|
isOpen: false,
|
|
2489
2511
|
highlightedIndex: -1
|
|
2490
|
-
}, state.highlightedIndex >= 0 && {
|
|
2512
|
+
}, state.highlightedIndex >= 0 && ((_props$items = props.items) == null ? void 0 : _props$items.length) && {
|
|
2491
2513
|
selectedItem: props.items[state.highlightedIndex]
|
|
2492
2514
|
});
|
|
2493
2515
|
break;
|
|
@@ -2859,8 +2881,9 @@
|
|
|
2859
2881
|
var _latest$current = latest.current,
|
|
2860
2882
|
latestState = _latest$current.state,
|
|
2861
2883
|
latestProps = _latest$current.props;
|
|
2862
|
-
var
|
|
2863
|
-
|
|
2884
|
+
var _getItemAndIndex = getItemAndIndex(itemProp, indexProp, latestProps.items, 'Pass either item or index to getItemProps!'),
|
|
2885
|
+
item = _getItemAndIndex[0],
|
|
2886
|
+
index = _getItemAndIndex[1];
|
|
2864
2887
|
var itemHandleMouseMove = function itemHandleMouseMove() {
|
|
2865
2888
|
if (index === latestState.highlightedIndex) {
|
|
2866
2889
|
return;
|
|
@@ -2878,18 +2901,14 @@
|
|
|
2878
2901
|
index: index
|
|
2879
2902
|
});
|
|
2880
2903
|
};
|
|
2881
|
-
var itemIndex = getItemIndex(index, item, latestProps.items);
|
|
2882
|
-
if (itemIndex < 0) {
|
|
2883
|
-
throw new Error('Pass either item or item index in getItemProps!');
|
|
2884
|
-
}
|
|
2885
2904
|
var itemProps = _extends((_extends4 = {
|
|
2886
2905
|
disabled: disabled,
|
|
2887
2906
|
role: 'option',
|
|
2888
2907
|
'aria-selected': "" + (item === selectedItem),
|
|
2889
|
-
id: elementIds.getItemId(
|
|
2908
|
+
id: elementIds.getItemId(index)
|
|
2890
2909
|
}, _extends4[refKey] = handleRefs(ref, function (itemNode) {
|
|
2891
2910
|
if (itemNode) {
|
|
2892
|
-
itemRefs.current[elementIds.getItemId(
|
|
2911
|
+
itemRefs.current[elementIds.getItemId(index)] = itemNode;
|
|
2893
2912
|
}
|
|
2894
2913
|
}), _extends4), rest);
|
|
2895
2914
|
if (!disabled) {
|
|
@@ -2900,7 +2919,7 @@
|
|
|
2900
2919
|
}
|
|
2901
2920
|
itemProps.onMouseMove = callAllEventHandlers(onMouseMove, itemHandleMouseMove);
|
|
2902
2921
|
return itemProps;
|
|
2903
|
-
}, [latest,
|
|
2922
|
+
}, [latest, selectedItem, elementIds, shouldScrollRef, dispatch]);
|
|
2904
2923
|
return {
|
|
2905
2924
|
// prop getters.
|
|
2906
2925
|
getToggleButtonProps: getToggleButtonProps,
|
|
@@ -3072,6 +3091,7 @@
|
|
|
3072
3091
|
|
|
3073
3092
|
/* eslint-disable complexity */
|
|
3074
3093
|
function downshiftUseComboboxReducer(state, action) {
|
|
3094
|
+
var _props$items;
|
|
3075
3095
|
var type = action.type,
|
|
3076
3096
|
props = action.props,
|
|
3077
3097
|
altKey = action.altKey;
|
|
@@ -3100,13 +3120,7 @@
|
|
|
3100
3120
|
case InputKeyDownArrowUp:
|
|
3101
3121
|
if (state.isOpen) {
|
|
3102
3122
|
if (altKey) {
|
|
3103
|
-
changes =
|
|
3104
|
-
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
3105
|
-
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
|
3106
|
-
}, state.highlightedIndex >= 0 && {
|
|
3107
|
-
selectedItem: props.items[state.highlightedIndex],
|
|
3108
|
-
inputValue: props.itemToString(props.items[state.highlightedIndex])
|
|
3109
|
-
});
|
|
3123
|
+
changes = getChangesOnSelection(props, state.highlightedIndex);
|
|
3110
3124
|
} else {
|
|
3111
3125
|
changes = {
|
|
3112
3126
|
highlightedIndex: getNextWrappingIndex(-1, state.highlightedIndex, props.items.length, action.getItemNodeFromIndex, true)
|
|
@@ -3120,13 +3134,7 @@
|
|
|
3120
3134
|
}
|
|
3121
3135
|
break;
|
|
3122
3136
|
case InputKeyDownEnter:
|
|
3123
|
-
changes =
|
|
3124
|
-
isOpen: getDefaultValue$1(props, 'isOpen'),
|
|
3125
|
-
highlightedIndex: getDefaultValue$1(props, 'highlightedIndex')
|
|
3126
|
-
}, state.highlightedIndex >= 0 && {
|
|
3127
|
-
selectedItem: props.items[state.highlightedIndex],
|
|
3128
|
-
inputValue: props.itemToString(props.items[state.highlightedIndex])
|
|
3129
|
-
});
|
|
3137
|
+
changes = getChangesOnSelection(props, state.highlightedIndex);
|
|
3130
3138
|
break;
|
|
3131
3139
|
case InputKeyDownEscape:
|
|
3132
3140
|
changes = _extends({
|
|
@@ -3161,7 +3169,7 @@
|
|
|
3161
3169
|
changes = _extends({
|
|
3162
3170
|
isOpen: false,
|
|
3163
3171
|
highlightedIndex: -1
|
|
3164
|
-
}, state.highlightedIndex >= 0 && action.selectItem && {
|
|
3172
|
+
}, state.highlightedIndex >= 0 && ((_props$items = props.items) == null ? void 0 : _props$items.length) && action.selectItem && {
|
|
3165
3173
|
selectedItem: props.items[state.highlightedIndex],
|
|
3166
3174
|
inputValue: props.itemToString(props.items[state.highlightedIndex])
|
|
3167
3175
|
});
|
|
@@ -3429,8 +3437,8 @@
|
|
|
3429
3437
|
var getItemProps = preact.useCallback(function (_temp3) {
|
|
3430
3438
|
var _extends3, _ref4;
|
|
3431
3439
|
var _ref3 = _temp3 === void 0 ? {} : _temp3,
|
|
3432
|
-
|
|
3433
|
-
|
|
3440
|
+
itemProp = _ref3.item,
|
|
3441
|
+
indexProp = _ref3.index,
|
|
3434
3442
|
_ref3$refKey = _ref3.refKey,
|
|
3435
3443
|
refKey = _ref3$refKey === void 0 ? 'ref' : _ref3$refKey,
|
|
3436
3444
|
ref = _ref3.ref,
|
|
@@ -3443,10 +3451,8 @@
|
|
|
3443
3451
|
var _latest$current = latest.current,
|
|
3444
3452
|
latestProps = _latest$current.props,
|
|
3445
3453
|
latestState = _latest$current.state;
|
|
3446
|
-
var
|
|
3447
|
-
|
|
3448
|
-
throw new Error('Pass either item or item index in getItemProps!');
|
|
3449
|
-
}
|
|
3454
|
+
var _getItemAndIndex = getItemAndIndex(itemProp, indexProp, latestProps.items, 'Pass either item or index to getItemProps!'),
|
|
3455
|
+
index = _getItemAndIndex[1];
|
|
3450
3456
|
var onSelectKey = 'onClick';
|
|
3451
3457
|
var customClickHandler = onClick;
|
|
3452
3458
|
var itemHandleMouseMove = function itemHandleMouseMove() {
|
|
@@ -3471,9 +3477,9 @@
|
|
|
3471
3477
|
};
|
|
3472
3478
|
return _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (itemNode) {
|
|
3473
3479
|
if (itemNode) {
|
|
3474
|
-
itemRefs.current[elementIds.getItemId(
|
|
3480
|
+
itemRefs.current[elementIds.getItemId(index)] = itemNode;
|
|
3475
3481
|
}
|
|
3476
|
-
}), _extends3.disabled = disabled, _extends3.role = 'option', _extends3['aria-selected'] = "" + (
|
|
3482
|
+
}), _extends3.disabled = disabled, _extends3.role = 'option', _extends3['aria-selected'] = "" + (index === latestState.highlightedIndex), _extends3.id = elementIds.getItemId(index), _extends3), !disabled && (_ref4 = {}, _ref4[onSelectKey] = callAllEventHandlers(customClickHandler, itemHandleClick), _ref4), {
|
|
3477
3483
|
onMouseMove: callAllEventHandlers(onMouseMove, itemHandleMouseMove),
|
|
3478
3484
|
onMouseDown: callAllEventHandlers(onMouseDown, itemHandleMouseDown)
|
|
3479
3485
|
}, rest);
|
|
@@ -4013,14 +4019,13 @@
|
|
|
4013
4019
|
ref = _ref3.ref,
|
|
4014
4020
|
onClick = _ref3.onClick,
|
|
4015
4021
|
onKeyDown = _ref3.onKeyDown,
|
|
4016
|
-
|
|
4017
|
-
|
|
4022
|
+
selectedItemProp = _ref3.selectedItem,
|
|
4023
|
+
indexProp = _ref3.index,
|
|
4018
4024
|
rest = _objectWithoutPropertiesLoose(_ref3, _excluded);
|
|
4019
4025
|
var latestState = latest.current.state;
|
|
4020
|
-
var
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
}
|
|
4026
|
+
var _getItemAndIndex = getItemAndIndex(selectedItemProp, indexProp, latestState.selectedItems, 'Pass either item or index to getSelectedItemProps!'),
|
|
4027
|
+
index = _getItemAndIndex[1];
|
|
4028
|
+
var isFocusable = index > -1 && index === latestState.activeIndex;
|
|
4024
4029
|
var selectedItemHandleClick = function selectedItemHandleClick() {
|
|
4025
4030
|
dispatch({
|
|
4026
4031
|
type: SelectedItemClick,
|
|
@@ -4037,7 +4042,7 @@
|
|
|
4037
4042
|
if (selectedItemNode) {
|
|
4038
4043
|
selectedItemRefs.current.push(selectedItemNode);
|
|
4039
4044
|
}
|
|
4040
|
-
}), _extends2.tabIndex =
|
|
4045
|
+
}), _extends2.tabIndex = isFocusable ? 0 : -1, _extends2.onClick = callAllEventHandlers(onClick, selectedItemHandleClick), _extends2.onKeyDown = callAllEventHandlers(onKeyDown, selectedItemHandleKeyDown), _extends2), rest);
|
|
4041
4046
|
}, [dispatch, latest, selectedItemKeyDownHandlers]);
|
|
4042
4047
|
var getDropdownProps = preact.useCallback(function (_temp2, _temp3) {
|
|
4043
4048
|
var _extends3;
|