@react-stately/combobox 3.12.1 → 3.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/types.d.ts +33 -7
- package/dist/types.d.ts.map +1 -1
- package/dist/useComboBoxState.main.js +109 -37
- package/dist/useComboBoxState.main.js.map +1 -1
- package/dist/useComboBoxState.mjs +110 -38
- package/dist/useComboBoxState.module.js +110 -38
- package/dist/useComboBoxState.module.js.map +1 -1
- package/package.json +8 -8
- package/src/useComboBoxState.ts +138 -48
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {useFormValidationState as $49BJP$useFormValidationState} from "@react-stately/form";
|
|
2
2
|
import {getChildNodes as $49BJP$getChildNodes} from "@react-stately/collections";
|
|
3
|
-
import {
|
|
3
|
+
import {useListState as $49BJP$useListState, ListCollection as $49BJP$ListCollection} from "@react-stately/list";
|
|
4
4
|
import {useOverlayTriggerState as $49BJP$useOverlayTriggerState} from "@react-stately/overlays";
|
|
5
5
|
import {useState as $49BJP$useState, useMemo as $49BJP$useMemo, useRef as $49BJP$useRef, useCallback as $49BJP$useCallback, useEffect as $49BJP$useEffect} from "react";
|
|
6
6
|
import {useControlledState as $49BJP$useControlledState} from "@react-stately/utils";
|
|
@@ -21,30 +21,88 @@ import {useControlledState as $49BJP$useControlledState} from "@react-stately/ut
|
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
|
|
24
|
+
const $a9e7382a7d111cb5$var$EMPTY_VALUE = [];
|
|
24
25
|
function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
25
26
|
var _collection_getItem;
|
|
26
|
-
let { defaultFilter: defaultFilter, menuTrigger: menuTrigger = 'input', allowsEmptyCollection: allowsEmptyCollection = false, allowsCustomValue: allowsCustomValue, shouldCloseOnBlur: shouldCloseOnBlur = true } = props;
|
|
27
|
+
let { defaultFilter: defaultFilter, menuTrigger: menuTrigger = 'input', allowsEmptyCollection: allowsEmptyCollection = false, allowsCustomValue: allowsCustomValue, shouldCloseOnBlur: shouldCloseOnBlur = true, selectionMode: selectionMode = 'single' } = props;
|
|
27
28
|
let [showAllItems, setShowAllItems] = (0, $49BJP$useState)(false);
|
|
28
29
|
let [isFocused, setFocusedState] = (0, $49BJP$useState)(false);
|
|
29
30
|
let [focusStrategy, setFocusStrategy] = (0, $49BJP$useState)(null);
|
|
30
|
-
let
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
let defaultValue = (0, $49BJP$useMemo)(()=>{
|
|
32
|
+
var _props_defaultSelectedKey;
|
|
33
|
+
return props.defaultValue !== undefined ? props.defaultValue : selectionMode === 'single' ? (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : null : [];
|
|
34
|
+
}, [
|
|
35
|
+
props.defaultValue,
|
|
36
|
+
props.defaultSelectedKey,
|
|
37
|
+
selectionMode
|
|
38
|
+
]);
|
|
39
|
+
let value = (0, $49BJP$useMemo)(()=>{
|
|
40
|
+
return props.value !== undefined ? props.value : selectionMode === 'single' ? props.selectedKey : undefined;
|
|
41
|
+
}, [
|
|
42
|
+
props.value,
|
|
43
|
+
props.selectedKey,
|
|
44
|
+
selectionMode
|
|
45
|
+
]);
|
|
46
|
+
let [controlledValue, setControlledValue] = (0, $49BJP$useControlledState)(value, defaultValue, props.onChange);
|
|
47
|
+
// Only display the first selected item if in single selection mode but the value is an array.
|
|
48
|
+
let displayValue = selectionMode === 'single' && Array.isArray(controlledValue) ? controlledValue[0] : controlledValue;
|
|
49
|
+
let setValue = (value)=>{
|
|
50
|
+
if (selectionMode === 'single') {
|
|
51
|
+
var _props_onSelectionChange;
|
|
52
|
+
var _value_;
|
|
53
|
+
let key = Array.isArray(value) ? (_value_ = value[0]) !== null && _value_ !== void 0 ? _value_ : null : value;
|
|
54
|
+
setControlledValue(key);
|
|
55
|
+
if (key !== displayValue) (_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, key);
|
|
56
|
+
} else {
|
|
57
|
+
let keys = [];
|
|
58
|
+
if (Array.isArray(value)) keys = value;
|
|
59
|
+
else if (value != null) keys = [
|
|
60
|
+
value
|
|
61
|
+
];
|
|
62
|
+
setControlledValue(keys);
|
|
37
63
|
}
|
|
38
64
|
};
|
|
39
65
|
var _props_items;
|
|
40
|
-
let { collection: collection, selectionManager: selectionManager,
|
|
66
|
+
let { collection: collection, selectionManager: selectionManager, disabledKeys: disabledKeys } = (0, $49BJP$useListState)({
|
|
41
67
|
...props,
|
|
42
|
-
|
|
43
|
-
|
|
68
|
+
items: (_props_items = props.items) !== null && _props_items !== void 0 ? _props_items : props.defaultItems,
|
|
69
|
+
selectionMode: selectionMode,
|
|
70
|
+
disallowEmptySelection: selectionMode === 'single',
|
|
71
|
+
allowDuplicateSelectionEvents: true,
|
|
72
|
+
selectedKeys: (0, $49BJP$useMemo)(()=>$a9e7382a7d111cb5$var$convertValue(displayValue), [
|
|
73
|
+
displayValue
|
|
74
|
+
]),
|
|
75
|
+
onSelectionChange: (keys)=>{
|
|
76
|
+
// impossible, but TS doesn't know that
|
|
77
|
+
if (keys === 'all') return;
|
|
78
|
+
if (selectionMode === 'single') {
|
|
79
|
+
var _keys_values_next_value;
|
|
80
|
+
let key = (_keys_values_next_value = keys.values().next().value) !== null && _keys_values_next_value !== void 0 ? _keys_values_next_value : null;
|
|
81
|
+
if (key === displayValue) {
|
|
82
|
+
var _props_onSelectionChange;
|
|
83
|
+
(_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, key);
|
|
84
|
+
// If key is the same, reset the inputValue and close the menu
|
|
85
|
+
// (scenario: user clicks on already selected option)
|
|
86
|
+
resetInputValue();
|
|
87
|
+
closeMenu();
|
|
88
|
+
} else setValue(key);
|
|
89
|
+
} else setValue([
|
|
90
|
+
...keys
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
44
93
|
});
|
|
94
|
+
let selectedKey = selectionMode === 'single' ? selectionManager.firstSelectedKey : null;
|
|
95
|
+
let selectedItems = (0, $49BJP$useMemo)(()=>{
|
|
96
|
+
return [
|
|
97
|
+
...selectionManager.selectedKeys
|
|
98
|
+
].map((key)=>collection.getItem(key)).filter((item)=>item != null);
|
|
99
|
+
}, [
|
|
100
|
+
selectionManager.selectedKeys,
|
|
101
|
+
collection
|
|
102
|
+
]);
|
|
45
103
|
let [inputValue, setInputValue] = (0, $49BJP$useControlledState)(props.inputValue, $a9e7382a7d111cb5$var$getDefaultInputValue(props.defaultInputValue, selectedKey, collection) || '', props.onInputChange);
|
|
46
|
-
let [
|
|
47
|
-
let [
|
|
104
|
+
let [initialValue] = (0, $49BJP$useState)(displayValue);
|
|
105
|
+
let [initialInputValue] = (0, $49BJP$useState)(inputValue);
|
|
48
106
|
// Preserve original collection so we can show all items on demand
|
|
49
107
|
let originalCollection = collection;
|
|
50
108
|
let filteredCollection = (0, $49BJP$useMemo)(()=>// No default filter if items are controlled.
|
|
@@ -125,8 +183,7 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
125
183
|
setLastValue(itemText);
|
|
126
184
|
setInputValue(itemText);
|
|
127
185
|
};
|
|
128
|
-
|
|
129
|
-
let lastSelectedKey = (0, $49BJP$useRef)((_ref = (_props_selectedKey = props.selectedKey) !== null && _props_selectedKey !== void 0 ? _props_selectedKey : props.defaultSelectedKey) !== null && _ref !== void 0 ? _ref : null);
|
|
186
|
+
let lastValueRef = (0, $49BJP$useRef)(displayValue);
|
|
130
187
|
var _collection_getItem_textValue;
|
|
131
188
|
let lastSelectedKeyText = (0, $49BJP$useRef)(selectedKey != null ? (_collection_getItem_textValue = (_collection_getItem = collection.getItem(selectedKey)) === null || _collection_getItem === void 0 ? void 0 : _collection_getItem.textValue) !== null && _collection_getItem_textValue !== void 0 ? _collection_getItem_textValue : '' : '');
|
|
132
189
|
// intentional omit dependency array, want this to happen on every render
|
|
@@ -140,19 +197,19 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
140
197
|
// but we are currently showing all items via button press
|
|
141
198
|
if (!showAllItems && !allowsEmptyCollection && triggerState.isOpen && filteredCollection.size === 0) closeMenu();
|
|
142
199
|
// Close when an item is selected.
|
|
143
|
-
if (
|
|
200
|
+
if (displayValue != null && displayValue !== lastValueRef.current && selectionMode === 'single') closeMenu();
|
|
144
201
|
// Clear focused key when input value changes and display filtered collection again.
|
|
145
202
|
if (inputValue !== lastValue) {
|
|
146
203
|
selectionManager.setFocusedKey(null);
|
|
147
204
|
setShowAllItems(false);
|
|
148
|
-
// Set
|
|
205
|
+
// Set value to null when the user clears the input.
|
|
149
206
|
// If controlled, this is the application developer's responsibility.
|
|
150
|
-
if (inputValue === '' && (props.inputValue === undefined ||
|
|
207
|
+
if (selectionMode === 'single' && inputValue === '' && (props.inputValue === undefined || value === undefined)) setValue(null);
|
|
151
208
|
}
|
|
152
|
-
// If the
|
|
153
|
-
// Do nothing if both inputValue and
|
|
209
|
+
// If the value changed, update the input value.
|
|
210
|
+
// Do nothing if both inputValue and value are controlled.
|
|
154
211
|
// In this case, it's the user's responsibility to update inputValue in onSelectionChange.
|
|
155
|
-
if (
|
|
212
|
+
if (displayValue !== lastValueRef.current && (props.inputValue === undefined || value === undefined)) resetInputValue();
|
|
156
213
|
else if (lastValue !== inputValue) setLastValue(inputValue);
|
|
157
214
|
var _collection_getItem_textValue;
|
|
158
215
|
// Update the inputValue if the selected item's text changes from its last tracked value.
|
|
@@ -160,23 +217,25 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
160
217
|
// Only reset if the user isn't currently within the field so we don't erroneously modify user input.
|
|
161
218
|
// If inputValue is controlled, it is the user's responsibility to update the inputValue when items change.
|
|
162
219
|
let selectedItemText = selectedKey != null ? (_collection_getItem_textValue = (_collection_getItem = collection.getItem(selectedKey)) === null || _collection_getItem === void 0 ? void 0 : _collection_getItem.textValue) !== null && _collection_getItem_textValue !== void 0 ? _collection_getItem_textValue : '' : '';
|
|
163
|
-
if (!isFocused && selectedKey != null && props.inputValue === undefined && selectedKey ===
|
|
220
|
+
if (!isFocused && selectedKey != null && props.inputValue === undefined && selectedKey === lastValueRef.current) {
|
|
164
221
|
if (lastSelectedKeyText.current !== selectedItemText) {
|
|
165
222
|
setLastValue(selectedItemText);
|
|
166
223
|
setInputValue(selectedItemText);
|
|
167
224
|
}
|
|
168
225
|
}
|
|
169
|
-
|
|
226
|
+
lastValueRef.current = displayValue;
|
|
170
227
|
lastSelectedKeyText.current = selectedItemText;
|
|
171
228
|
});
|
|
172
229
|
let validation = (0, $49BJP$useFormValidationState)({
|
|
173
230
|
...props,
|
|
174
|
-
value: (0, $49BJP$useMemo)(()=>({
|
|
231
|
+
value: (0, $49BJP$useMemo)(()=>Array.isArray(displayValue) && displayValue.length === 0 ? null : {
|
|
175
232
|
inputValue: inputValue,
|
|
233
|
+
value: displayValue,
|
|
176
234
|
selectedKey: selectedKey
|
|
177
|
-
}
|
|
235
|
+
}, [
|
|
178
236
|
inputValue,
|
|
179
|
-
selectedKey
|
|
237
|
+
selectedKey,
|
|
238
|
+
displayValue
|
|
180
239
|
])
|
|
181
240
|
});
|
|
182
241
|
// Revert input value and close menu
|
|
@@ -185,15 +244,17 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
185
244
|
else commitSelection();
|
|
186
245
|
};
|
|
187
246
|
let commitCustomValue = ()=>{
|
|
188
|
-
|
|
189
|
-
|
|
247
|
+
let value = selectionMode === 'multiple' ? $a9e7382a7d111cb5$var$EMPTY_VALUE : null;
|
|
248
|
+
lastValueRef.current = value;
|
|
249
|
+
setValue(value);
|
|
190
250
|
closeMenu();
|
|
191
251
|
};
|
|
192
252
|
let commitSelection = ()=>{
|
|
193
253
|
// If multiple things are controlled, call onSelectionChange
|
|
194
|
-
if (
|
|
195
|
-
var _props_onSelectionChange, _collection_getItem;
|
|
254
|
+
if (value !== undefined && props.inputValue !== undefined) {
|
|
255
|
+
var _props_onSelectionChange, _props_onChange, _collection_getItem;
|
|
196
256
|
(_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, selectedKey);
|
|
257
|
+
(_props_onChange = props.onChange) === null || _props_onChange === void 0 ? void 0 : _props_onChange.call(props, displayValue);
|
|
197
258
|
var _collection_getItem_textValue;
|
|
198
259
|
// Stop menu from reopening from useEffect
|
|
199
260
|
let itemText = selectedKey != null ? (_collection_getItem_textValue = (_collection_getItem = collection.getItem(selectedKey)) === null || _collection_getItem === void 0 ? void 0 : _collection_getItem.textValue) !== null && _collection_getItem_textValue !== void 0 ? _collection_getItem_textValue : '' : '';
|
|
@@ -218,8 +279,8 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
218
279
|
if (triggerState.isOpen && selectionManager.focusedKey != null) {
|
|
219
280
|
// Reset inputValue and close menu here if the selected key is already the focused key. Otherwise
|
|
220
281
|
// fire onSelectionChange to allow the application to control the closing.
|
|
221
|
-
if (
|
|
222
|
-
else
|
|
282
|
+
if (selectionManager.isSelected(selectionManager.focusedKey) && selectionMode === 'single') commitSelection();
|
|
283
|
+
else selectionManager.select(selectionManager.focusedKey);
|
|
223
284
|
} else commitValue();
|
|
224
285
|
};
|
|
225
286
|
let valueOnFocus = (0, $49BJP$useRef)(inputValue);
|
|
@@ -246,8 +307,8 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
246
307
|
lastCollection
|
|
247
308
|
]);
|
|
248
309
|
var _props_defaultSelectedKey;
|
|
249
|
-
let defaultSelectedKey = (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey :
|
|
250
|
-
var _getDefaultInputValue;
|
|
310
|
+
let defaultSelectedKey = (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : selectionMode === 'single' ? initialValue : null;
|
|
311
|
+
var _selectedItems_, _getDefaultInputValue;
|
|
251
312
|
return {
|
|
252
313
|
...validation,
|
|
253
314
|
...triggerState,
|
|
@@ -256,16 +317,20 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
256
317
|
open: open,
|
|
257
318
|
close: commitValue,
|
|
258
319
|
selectionManager: selectionManager,
|
|
320
|
+
value: displayValue,
|
|
321
|
+
defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : initialValue,
|
|
322
|
+
setValue: setValue,
|
|
259
323
|
selectedKey: selectedKey,
|
|
324
|
+
selectedItems: selectedItems,
|
|
260
325
|
defaultSelectedKey: defaultSelectedKey,
|
|
261
|
-
setSelectedKey:
|
|
326
|
+
setSelectedKey: setValue,
|
|
262
327
|
disabledKeys: disabledKeys,
|
|
263
328
|
isFocused: isFocused,
|
|
264
329
|
setFocused: setFocused,
|
|
265
|
-
selectedItem:
|
|
330
|
+
selectedItem: (_selectedItems_ = selectedItems[0]) !== null && _selectedItems_ !== void 0 ? _selectedItems_ : null,
|
|
266
331
|
collection: displayedCollection,
|
|
267
332
|
inputValue: inputValue,
|
|
268
|
-
defaultInputValue: (_getDefaultInputValue = $a9e7382a7d111cb5$var$getDefaultInputValue(props.defaultInputValue, defaultSelectedKey, collection)) !== null && _getDefaultInputValue !== void 0 ? _getDefaultInputValue :
|
|
333
|
+
defaultInputValue: (_getDefaultInputValue = $a9e7382a7d111cb5$var$getDefaultInputValue(props.defaultInputValue, defaultSelectedKey, collection)) !== null && _getDefaultInputValue !== void 0 ? _getDefaultInputValue : initialInputValue,
|
|
269
334
|
setInputValue: setInputValue,
|
|
270
335
|
commit: commit,
|
|
271
336
|
revert: revert
|
|
@@ -302,6 +367,13 @@ function $a9e7382a7d111cb5$var$getDefaultInputValue(defaultInputValue, selectedK
|
|
|
302
367
|
}
|
|
303
368
|
return defaultInputValue;
|
|
304
369
|
}
|
|
370
|
+
function $a9e7382a7d111cb5$var$convertValue(value) {
|
|
371
|
+
if (value === undefined) return undefined;
|
|
372
|
+
if (value === null) return [];
|
|
373
|
+
return Array.isArray(value) ? value : [
|
|
374
|
+
value
|
|
375
|
+
];
|
|
376
|
+
}
|
|
305
377
|
|
|
306
378
|
|
|
307
379
|
export {$a9e7382a7d111cb5$export$b453a3bfd4a5fa9e as useComboBoxState};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {useFormValidationState as $49BJP$useFormValidationState} from "@react-stately/form";
|
|
2
2
|
import {getChildNodes as $49BJP$getChildNodes} from "@react-stately/collections";
|
|
3
|
-
import {
|
|
3
|
+
import {useListState as $49BJP$useListState, ListCollection as $49BJP$ListCollection} from "@react-stately/list";
|
|
4
4
|
import {useOverlayTriggerState as $49BJP$useOverlayTriggerState} from "@react-stately/overlays";
|
|
5
5
|
import {useState as $49BJP$useState, useMemo as $49BJP$useMemo, useRef as $49BJP$useRef, useCallback as $49BJP$useCallback, useEffect as $49BJP$useEffect} from "react";
|
|
6
6
|
import {useControlledState as $49BJP$useControlledState} from "@react-stately/utils";
|
|
@@ -21,30 +21,88 @@ import {useControlledState as $49BJP$useControlledState} from "@react-stately/ut
|
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
|
|
24
|
+
const $a9e7382a7d111cb5$var$EMPTY_VALUE = [];
|
|
24
25
|
function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
25
26
|
var _collection_getItem;
|
|
26
|
-
let { defaultFilter: defaultFilter, menuTrigger: menuTrigger = 'input', allowsEmptyCollection: allowsEmptyCollection = false, allowsCustomValue: allowsCustomValue, shouldCloseOnBlur: shouldCloseOnBlur = true } = props;
|
|
27
|
+
let { defaultFilter: defaultFilter, menuTrigger: menuTrigger = 'input', allowsEmptyCollection: allowsEmptyCollection = false, allowsCustomValue: allowsCustomValue, shouldCloseOnBlur: shouldCloseOnBlur = true, selectionMode: selectionMode = 'single' } = props;
|
|
27
28
|
let [showAllItems, setShowAllItems] = (0, $49BJP$useState)(false);
|
|
28
29
|
let [isFocused, setFocusedState] = (0, $49BJP$useState)(false);
|
|
29
30
|
let [focusStrategy, setFocusStrategy] = (0, $49BJP$useState)(null);
|
|
30
|
-
let
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
let defaultValue = (0, $49BJP$useMemo)(()=>{
|
|
32
|
+
var _props_defaultSelectedKey;
|
|
33
|
+
return props.defaultValue !== undefined ? props.defaultValue : selectionMode === 'single' ? (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : null : [];
|
|
34
|
+
}, [
|
|
35
|
+
props.defaultValue,
|
|
36
|
+
props.defaultSelectedKey,
|
|
37
|
+
selectionMode
|
|
38
|
+
]);
|
|
39
|
+
let value = (0, $49BJP$useMemo)(()=>{
|
|
40
|
+
return props.value !== undefined ? props.value : selectionMode === 'single' ? props.selectedKey : undefined;
|
|
41
|
+
}, [
|
|
42
|
+
props.value,
|
|
43
|
+
props.selectedKey,
|
|
44
|
+
selectionMode
|
|
45
|
+
]);
|
|
46
|
+
let [controlledValue, setControlledValue] = (0, $49BJP$useControlledState)(value, defaultValue, props.onChange);
|
|
47
|
+
// Only display the first selected item if in single selection mode but the value is an array.
|
|
48
|
+
let displayValue = selectionMode === 'single' && Array.isArray(controlledValue) ? controlledValue[0] : controlledValue;
|
|
49
|
+
let setValue = (value)=>{
|
|
50
|
+
if (selectionMode === 'single') {
|
|
51
|
+
var _props_onSelectionChange;
|
|
52
|
+
var _value_;
|
|
53
|
+
let key = Array.isArray(value) ? (_value_ = value[0]) !== null && _value_ !== void 0 ? _value_ : null : value;
|
|
54
|
+
setControlledValue(key);
|
|
55
|
+
if (key !== displayValue) (_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, key);
|
|
56
|
+
} else {
|
|
57
|
+
let keys = [];
|
|
58
|
+
if (Array.isArray(value)) keys = value;
|
|
59
|
+
else if (value != null) keys = [
|
|
60
|
+
value
|
|
61
|
+
];
|
|
62
|
+
setControlledValue(keys);
|
|
37
63
|
}
|
|
38
64
|
};
|
|
39
65
|
var _props_items;
|
|
40
|
-
let { collection: collection, selectionManager: selectionManager,
|
|
66
|
+
let { collection: collection, selectionManager: selectionManager, disabledKeys: disabledKeys } = (0, $49BJP$useListState)({
|
|
41
67
|
...props,
|
|
42
|
-
|
|
43
|
-
|
|
68
|
+
items: (_props_items = props.items) !== null && _props_items !== void 0 ? _props_items : props.defaultItems,
|
|
69
|
+
selectionMode: selectionMode,
|
|
70
|
+
disallowEmptySelection: selectionMode === 'single',
|
|
71
|
+
allowDuplicateSelectionEvents: true,
|
|
72
|
+
selectedKeys: (0, $49BJP$useMemo)(()=>$a9e7382a7d111cb5$var$convertValue(displayValue), [
|
|
73
|
+
displayValue
|
|
74
|
+
]),
|
|
75
|
+
onSelectionChange: (keys)=>{
|
|
76
|
+
// impossible, but TS doesn't know that
|
|
77
|
+
if (keys === 'all') return;
|
|
78
|
+
if (selectionMode === 'single') {
|
|
79
|
+
var _keys_values_next_value;
|
|
80
|
+
let key = (_keys_values_next_value = keys.values().next().value) !== null && _keys_values_next_value !== void 0 ? _keys_values_next_value : null;
|
|
81
|
+
if (key === displayValue) {
|
|
82
|
+
var _props_onSelectionChange;
|
|
83
|
+
(_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, key);
|
|
84
|
+
// If key is the same, reset the inputValue and close the menu
|
|
85
|
+
// (scenario: user clicks on already selected option)
|
|
86
|
+
resetInputValue();
|
|
87
|
+
closeMenu();
|
|
88
|
+
} else setValue(key);
|
|
89
|
+
} else setValue([
|
|
90
|
+
...keys
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
44
93
|
});
|
|
94
|
+
let selectedKey = selectionMode === 'single' ? selectionManager.firstSelectedKey : null;
|
|
95
|
+
let selectedItems = (0, $49BJP$useMemo)(()=>{
|
|
96
|
+
return [
|
|
97
|
+
...selectionManager.selectedKeys
|
|
98
|
+
].map((key)=>collection.getItem(key)).filter((item)=>item != null);
|
|
99
|
+
}, [
|
|
100
|
+
selectionManager.selectedKeys,
|
|
101
|
+
collection
|
|
102
|
+
]);
|
|
45
103
|
let [inputValue, setInputValue] = (0, $49BJP$useControlledState)(props.inputValue, $a9e7382a7d111cb5$var$getDefaultInputValue(props.defaultInputValue, selectedKey, collection) || '', props.onInputChange);
|
|
46
|
-
let [
|
|
47
|
-
let [
|
|
104
|
+
let [initialValue] = (0, $49BJP$useState)(displayValue);
|
|
105
|
+
let [initialInputValue] = (0, $49BJP$useState)(inputValue);
|
|
48
106
|
// Preserve original collection so we can show all items on demand
|
|
49
107
|
let originalCollection = collection;
|
|
50
108
|
let filteredCollection = (0, $49BJP$useMemo)(()=>// No default filter if items are controlled.
|
|
@@ -125,8 +183,7 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
125
183
|
setLastValue(itemText);
|
|
126
184
|
setInputValue(itemText);
|
|
127
185
|
};
|
|
128
|
-
|
|
129
|
-
let lastSelectedKey = (0, $49BJP$useRef)((_ref = (_props_selectedKey = props.selectedKey) !== null && _props_selectedKey !== void 0 ? _props_selectedKey : props.defaultSelectedKey) !== null && _ref !== void 0 ? _ref : null);
|
|
186
|
+
let lastValueRef = (0, $49BJP$useRef)(displayValue);
|
|
130
187
|
var _collection_getItem_textValue;
|
|
131
188
|
let lastSelectedKeyText = (0, $49BJP$useRef)(selectedKey != null ? (_collection_getItem_textValue = (_collection_getItem = collection.getItem(selectedKey)) === null || _collection_getItem === void 0 ? void 0 : _collection_getItem.textValue) !== null && _collection_getItem_textValue !== void 0 ? _collection_getItem_textValue : '' : '');
|
|
132
189
|
// intentional omit dependency array, want this to happen on every render
|
|
@@ -140,19 +197,19 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
140
197
|
// but we are currently showing all items via button press
|
|
141
198
|
if (!showAllItems && !allowsEmptyCollection && triggerState.isOpen && filteredCollection.size === 0) closeMenu();
|
|
142
199
|
// Close when an item is selected.
|
|
143
|
-
if (
|
|
200
|
+
if (displayValue != null && displayValue !== lastValueRef.current && selectionMode === 'single') closeMenu();
|
|
144
201
|
// Clear focused key when input value changes and display filtered collection again.
|
|
145
202
|
if (inputValue !== lastValue) {
|
|
146
203
|
selectionManager.setFocusedKey(null);
|
|
147
204
|
setShowAllItems(false);
|
|
148
|
-
// Set
|
|
205
|
+
// Set value to null when the user clears the input.
|
|
149
206
|
// If controlled, this is the application developer's responsibility.
|
|
150
|
-
if (inputValue === '' && (props.inputValue === undefined ||
|
|
207
|
+
if (selectionMode === 'single' && inputValue === '' && (props.inputValue === undefined || value === undefined)) setValue(null);
|
|
151
208
|
}
|
|
152
|
-
// If the
|
|
153
|
-
// Do nothing if both inputValue and
|
|
209
|
+
// If the value changed, update the input value.
|
|
210
|
+
// Do nothing if both inputValue and value are controlled.
|
|
154
211
|
// In this case, it's the user's responsibility to update inputValue in onSelectionChange.
|
|
155
|
-
if (
|
|
212
|
+
if (displayValue !== lastValueRef.current && (props.inputValue === undefined || value === undefined)) resetInputValue();
|
|
156
213
|
else if (lastValue !== inputValue) setLastValue(inputValue);
|
|
157
214
|
var _collection_getItem_textValue;
|
|
158
215
|
// Update the inputValue if the selected item's text changes from its last tracked value.
|
|
@@ -160,23 +217,25 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
160
217
|
// Only reset if the user isn't currently within the field so we don't erroneously modify user input.
|
|
161
218
|
// If inputValue is controlled, it is the user's responsibility to update the inputValue when items change.
|
|
162
219
|
let selectedItemText = selectedKey != null ? (_collection_getItem_textValue = (_collection_getItem = collection.getItem(selectedKey)) === null || _collection_getItem === void 0 ? void 0 : _collection_getItem.textValue) !== null && _collection_getItem_textValue !== void 0 ? _collection_getItem_textValue : '' : '';
|
|
163
|
-
if (!isFocused && selectedKey != null && props.inputValue === undefined && selectedKey ===
|
|
220
|
+
if (!isFocused && selectedKey != null && props.inputValue === undefined && selectedKey === lastValueRef.current) {
|
|
164
221
|
if (lastSelectedKeyText.current !== selectedItemText) {
|
|
165
222
|
setLastValue(selectedItemText);
|
|
166
223
|
setInputValue(selectedItemText);
|
|
167
224
|
}
|
|
168
225
|
}
|
|
169
|
-
|
|
226
|
+
lastValueRef.current = displayValue;
|
|
170
227
|
lastSelectedKeyText.current = selectedItemText;
|
|
171
228
|
});
|
|
172
229
|
let validation = (0, $49BJP$useFormValidationState)({
|
|
173
230
|
...props,
|
|
174
|
-
value: (0, $49BJP$useMemo)(()=>({
|
|
231
|
+
value: (0, $49BJP$useMemo)(()=>Array.isArray(displayValue) && displayValue.length === 0 ? null : {
|
|
175
232
|
inputValue: inputValue,
|
|
233
|
+
value: displayValue,
|
|
176
234
|
selectedKey: selectedKey
|
|
177
|
-
}
|
|
235
|
+
}, [
|
|
178
236
|
inputValue,
|
|
179
|
-
selectedKey
|
|
237
|
+
selectedKey,
|
|
238
|
+
displayValue
|
|
180
239
|
])
|
|
181
240
|
});
|
|
182
241
|
// Revert input value and close menu
|
|
@@ -185,15 +244,17 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
185
244
|
else commitSelection();
|
|
186
245
|
};
|
|
187
246
|
let commitCustomValue = ()=>{
|
|
188
|
-
|
|
189
|
-
|
|
247
|
+
let value = selectionMode === 'multiple' ? $a9e7382a7d111cb5$var$EMPTY_VALUE : null;
|
|
248
|
+
lastValueRef.current = value;
|
|
249
|
+
setValue(value);
|
|
190
250
|
closeMenu();
|
|
191
251
|
};
|
|
192
252
|
let commitSelection = ()=>{
|
|
193
253
|
// If multiple things are controlled, call onSelectionChange
|
|
194
|
-
if (
|
|
195
|
-
var _props_onSelectionChange, _collection_getItem;
|
|
254
|
+
if (value !== undefined && props.inputValue !== undefined) {
|
|
255
|
+
var _props_onSelectionChange, _props_onChange, _collection_getItem;
|
|
196
256
|
(_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, selectedKey);
|
|
257
|
+
(_props_onChange = props.onChange) === null || _props_onChange === void 0 ? void 0 : _props_onChange.call(props, displayValue);
|
|
197
258
|
var _collection_getItem_textValue;
|
|
198
259
|
// Stop menu from reopening from useEffect
|
|
199
260
|
let itemText = selectedKey != null ? (_collection_getItem_textValue = (_collection_getItem = collection.getItem(selectedKey)) === null || _collection_getItem === void 0 ? void 0 : _collection_getItem.textValue) !== null && _collection_getItem_textValue !== void 0 ? _collection_getItem_textValue : '' : '';
|
|
@@ -218,8 +279,8 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
218
279
|
if (triggerState.isOpen && selectionManager.focusedKey != null) {
|
|
219
280
|
// Reset inputValue and close menu here if the selected key is already the focused key. Otherwise
|
|
220
281
|
// fire onSelectionChange to allow the application to control the closing.
|
|
221
|
-
if (
|
|
222
|
-
else
|
|
282
|
+
if (selectionManager.isSelected(selectionManager.focusedKey) && selectionMode === 'single') commitSelection();
|
|
283
|
+
else selectionManager.select(selectionManager.focusedKey);
|
|
223
284
|
} else commitValue();
|
|
224
285
|
};
|
|
225
286
|
let valueOnFocus = (0, $49BJP$useRef)(inputValue);
|
|
@@ -246,8 +307,8 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
246
307
|
lastCollection
|
|
247
308
|
]);
|
|
248
309
|
var _props_defaultSelectedKey;
|
|
249
|
-
let defaultSelectedKey = (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey :
|
|
250
|
-
var _getDefaultInputValue;
|
|
310
|
+
let defaultSelectedKey = (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : selectionMode === 'single' ? initialValue : null;
|
|
311
|
+
var _selectedItems_, _getDefaultInputValue;
|
|
251
312
|
return {
|
|
252
313
|
...validation,
|
|
253
314
|
...triggerState,
|
|
@@ -256,16 +317,20 @@ function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
|
|
|
256
317
|
open: open,
|
|
257
318
|
close: commitValue,
|
|
258
319
|
selectionManager: selectionManager,
|
|
320
|
+
value: displayValue,
|
|
321
|
+
defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : initialValue,
|
|
322
|
+
setValue: setValue,
|
|
259
323
|
selectedKey: selectedKey,
|
|
324
|
+
selectedItems: selectedItems,
|
|
260
325
|
defaultSelectedKey: defaultSelectedKey,
|
|
261
|
-
setSelectedKey:
|
|
326
|
+
setSelectedKey: setValue,
|
|
262
327
|
disabledKeys: disabledKeys,
|
|
263
328
|
isFocused: isFocused,
|
|
264
329
|
setFocused: setFocused,
|
|
265
|
-
selectedItem:
|
|
330
|
+
selectedItem: (_selectedItems_ = selectedItems[0]) !== null && _selectedItems_ !== void 0 ? _selectedItems_ : null,
|
|
266
331
|
collection: displayedCollection,
|
|
267
332
|
inputValue: inputValue,
|
|
268
|
-
defaultInputValue: (_getDefaultInputValue = $a9e7382a7d111cb5$var$getDefaultInputValue(props.defaultInputValue, defaultSelectedKey, collection)) !== null && _getDefaultInputValue !== void 0 ? _getDefaultInputValue :
|
|
333
|
+
defaultInputValue: (_getDefaultInputValue = $a9e7382a7d111cb5$var$getDefaultInputValue(props.defaultInputValue, defaultSelectedKey, collection)) !== null && _getDefaultInputValue !== void 0 ? _getDefaultInputValue : initialInputValue,
|
|
269
334
|
setInputValue: setInputValue,
|
|
270
335
|
commit: commit,
|
|
271
336
|
revert: revert
|
|
@@ -302,6 +367,13 @@ function $a9e7382a7d111cb5$var$getDefaultInputValue(defaultInputValue, selectedK
|
|
|
302
367
|
}
|
|
303
368
|
return defaultInputValue;
|
|
304
369
|
}
|
|
370
|
+
function $a9e7382a7d111cb5$var$convertValue(value) {
|
|
371
|
+
if (value === undefined) return undefined;
|
|
372
|
+
if (value === null) return [];
|
|
373
|
+
return Array.isArray(value) ? value : [
|
|
374
|
+
value
|
|
375
|
+
];
|
|
376
|
+
}
|
|
305
377
|
|
|
306
378
|
|
|
307
379
|
export {$a9e7382a7d111cb5$export$b453a3bfd4a5fa9e as useComboBoxState};
|