@react-stately/combobox 3.12.2 → 3.14.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.
@@ -1,308 +0,0 @@
1
- import {useFormValidationState as $49BJP$useFormValidationState} from "@react-stately/form";
2
- import {getChildNodes as $49BJP$getChildNodes} from "@react-stately/collections";
3
- import {useSingleSelectListState as $49BJP$useSingleSelectListState, ListCollection as $49BJP$ListCollection} from "@react-stately/list";
4
- import {useOverlayTriggerState as $49BJP$useOverlayTriggerState} from "@react-stately/overlays";
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
- import {useControlledState as $49BJP$useControlledState} from "@react-stately/utils";
7
-
8
- /*
9
- * Copyright 2020 Adobe. All rights reserved.
10
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
11
- * you may not use this file except in compliance with the License. You may obtain a copy
12
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
13
- *
14
- * Unless required by applicable law or agreed to in writing, software distributed under
15
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
16
- * OF ANY KIND, either express or implied. See the License for the specific language
17
- * governing permissions and limitations under the License.
18
- */
19
-
20
-
21
-
22
-
23
-
24
- function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
25
- var _collection_getItem;
26
- let { defaultFilter: defaultFilter, menuTrigger: menuTrigger = 'input', allowsEmptyCollection: allowsEmptyCollection = false, allowsCustomValue: allowsCustomValue, shouldCloseOnBlur: shouldCloseOnBlur = true } = props;
27
- let [showAllItems, setShowAllItems] = (0, $49BJP$useState)(false);
28
- let [isFocused, setFocusedState] = (0, $49BJP$useState)(false);
29
- let [focusStrategy, setFocusStrategy] = (0, $49BJP$useState)(null);
30
- let onSelectionChange = (key)=>{
31
- if (props.onSelectionChange) props.onSelectionChange(key);
32
- // If key is the same, reset the inputValue and close the menu
33
- // (scenario: user clicks on already selected option)
34
- if (key === selectedKey) {
35
- resetInputValue();
36
- closeMenu();
37
- }
38
- };
39
- var _props_items;
40
- let { collection: collection, selectionManager: selectionManager, selectedKey: selectedKey, setSelectedKey: setSelectedKey, selectedItem: selectedItem, disabledKeys: disabledKeys } = (0, $49BJP$useSingleSelectListState)({
41
- ...props,
42
- onSelectionChange: onSelectionChange,
43
- items: (_props_items = props.items) !== null && _props_items !== void 0 ? _props_items : props.defaultItems
44
- });
45
- let [inputValue, setInputValue] = (0, $49BJP$useControlledState)(props.inputValue, $a9e7382a7d111cb5$var$getDefaultInputValue(props.defaultInputValue, selectedKey, collection) || '', props.onInputChange);
46
- let [initialSelectedKey] = (0, $49BJP$useState)(selectedKey);
47
- let [initialValue] = (0, $49BJP$useState)(inputValue);
48
- // Preserve original collection so we can show all items on demand
49
- let originalCollection = collection;
50
- let filteredCollection = (0, $49BJP$useMemo)(()=>// No default filter if items are controlled.
51
- props.items != null || !defaultFilter ? collection : $a9e7382a7d111cb5$var$filterCollection(collection, inputValue, defaultFilter), [
52
- collection,
53
- inputValue,
54
- defaultFilter,
55
- props.items
56
- ]);
57
- let [lastCollection, setLastCollection] = (0, $49BJP$useState)(filteredCollection);
58
- // Track what action is attempting to open the menu
59
- let menuOpenTrigger = (0, $49BJP$useRef)('focus');
60
- let onOpenChange = (open)=>{
61
- if (props.onOpenChange) props.onOpenChange(open, open ? menuOpenTrigger.current : undefined);
62
- selectionManager.setFocused(open);
63
- if (!open) selectionManager.setFocusedKey(null);
64
- };
65
- let triggerState = (0, $49BJP$useOverlayTriggerState)({
66
- ...props,
67
- onOpenChange: onOpenChange,
68
- isOpen: undefined,
69
- defaultOpen: undefined
70
- });
71
- let open = (focusStrategy = null, trigger)=>{
72
- let displayAllItems = trigger === 'manual' || trigger === 'focus' && menuTrigger === 'focus';
73
- // Prevent open operations from triggering if there is nothing to display
74
- // Also prevent open operations from triggering if items are uncontrolled but defaultItems is empty, even if displayAllItems is true.
75
- // This is to prevent comboboxes with empty defaultItems from opening but allow controlled items comboboxes to open even if the inital list is empty (assumption is user will provide swap the empty list with a base list via onOpenChange returning `menuTrigger` manual)
76
- if (allowsEmptyCollection || filteredCollection.size > 0 || displayAllItems && originalCollection.size > 0 || props.items) {
77
- if (displayAllItems && !triggerState.isOpen && props.items === undefined) // Show all items if menu is manually opened. Only care about this if items are undefined
78
- setShowAllItems(true);
79
- menuOpenTrigger.current = trigger;
80
- setFocusStrategy(focusStrategy);
81
- triggerState.open();
82
- }
83
- };
84
- let toggle = (focusStrategy = null, trigger)=>{
85
- let displayAllItems = trigger === 'manual' || trigger === 'focus' && menuTrigger === 'focus';
86
- // If the menu is closed and there is nothing to display, early return so toggle isn't called to prevent extraneous onOpenChange
87
- if (!(allowsEmptyCollection || filteredCollection.size > 0 || displayAllItems && originalCollection.size > 0 || props.items) && !triggerState.isOpen) return;
88
- if (displayAllItems && !triggerState.isOpen && props.items === undefined) // Show all items if menu is toggled open. Only care about this if items are undefined
89
- setShowAllItems(true);
90
- // Only update the menuOpenTrigger if menu is currently closed
91
- if (!triggerState.isOpen) menuOpenTrigger.current = trigger;
92
- toggleMenu(focusStrategy);
93
- };
94
- let updateLastCollection = (0, $49BJP$useCallback)(()=>{
95
- setLastCollection(showAllItems ? originalCollection : filteredCollection);
96
- }, [
97
- showAllItems,
98
- originalCollection,
99
- filteredCollection
100
- ]);
101
- // If menu is going to close, save the current collection so we can freeze the displayed collection when the
102
- // user clicks outside the popover to close the menu. Prevents the menu contents from updating as the menu closes.
103
- let toggleMenu = (0, $49BJP$useCallback)((focusStrategy = null)=>{
104
- if (triggerState.isOpen) updateLastCollection();
105
- setFocusStrategy(focusStrategy);
106
- triggerState.toggle();
107
- }, [
108
- triggerState,
109
- updateLastCollection
110
- ]);
111
- let closeMenu = (0, $49BJP$useCallback)(()=>{
112
- if (triggerState.isOpen) {
113
- updateLastCollection();
114
- triggerState.close();
115
- }
116
- }, [
117
- triggerState,
118
- updateLastCollection
119
- ]);
120
- let [lastValue, setLastValue] = (0, $49BJP$useState)(inputValue);
121
- let resetInputValue = ()=>{
122
- var _collection_getItem;
123
- var _collection_getItem_textValue;
124
- 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 : '' : '';
125
- setLastValue(itemText);
126
- setInputValue(itemText);
127
- };
128
- var _props_selectedKey, _ref;
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);
130
- var _collection_getItem_textValue;
131
- 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
- // intentional omit dependency array, want this to happen on every render
133
- // eslint-disable-next-line react-hooks/exhaustive-deps
134
- (0, $49BJP$useEffect)(()=>{
135
- var _collection_getItem;
136
- // Open and close menu automatically when the input value changes if the input is focused,
137
- // and there are items in the collection or allowEmptyCollection is true.
138
- if (isFocused && (filteredCollection.size > 0 || allowsEmptyCollection) && !triggerState.isOpen && inputValue !== lastValue && menuTrigger !== 'manual') open(null, 'input');
139
- // Close the menu if the collection is empty. Don't close menu if filtered collection size is 0
140
- // but we are currently showing all items via button press
141
- if (!showAllItems && !allowsEmptyCollection && triggerState.isOpen && filteredCollection.size === 0) closeMenu();
142
- // Close when an item is selected.
143
- if (selectedKey != null && selectedKey !== lastSelectedKey.current) closeMenu();
144
- // Clear focused key when input value changes and display filtered collection again.
145
- if (inputValue !== lastValue) {
146
- selectionManager.setFocusedKey(null);
147
- setShowAllItems(false);
148
- // Set selectedKey to null when the user clears the input.
149
- // If controlled, this is the application developer's responsibility.
150
- if (inputValue === '' && (props.inputValue === undefined || props.selectedKey === undefined)) setSelectedKey(null);
151
- }
152
- // If the selectedKey changed, update the input value.
153
- // Do nothing if both inputValue and selectedKey are controlled.
154
- // In this case, it's the user's responsibility to update inputValue in onSelectionChange.
155
- if (selectedKey !== lastSelectedKey.current && (props.inputValue === undefined || props.selectedKey === undefined)) resetInputValue();
156
- else if (lastValue !== inputValue) setLastValue(inputValue);
157
- var _collection_getItem_textValue;
158
- // Update the inputValue if the selected item's text changes from its last tracked value.
159
- // This is to handle cases where a selectedKey is specified but the items aren't available (async loading) or the selected item's text value updates.
160
- // Only reset if the user isn't currently within the field so we don't erroneously modify user input.
161
- // If inputValue is controlled, it is the user's responsibility to update the inputValue when items change.
162
- 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 === lastSelectedKey.current) {
164
- if (lastSelectedKeyText.current !== selectedItemText) {
165
- setLastValue(selectedItemText);
166
- setInputValue(selectedItemText);
167
- }
168
- }
169
- lastSelectedKey.current = selectedKey;
170
- lastSelectedKeyText.current = selectedItemText;
171
- });
172
- let validation = (0, $49BJP$useFormValidationState)({
173
- ...props,
174
- value: (0, $49BJP$useMemo)(()=>({
175
- inputValue: inputValue,
176
- selectedKey: selectedKey
177
- }), [
178
- inputValue,
179
- selectedKey
180
- ])
181
- });
182
- // Revert input value and close menu
183
- let revert = ()=>{
184
- if (allowsCustomValue && selectedKey == null) commitCustomValue();
185
- else commitSelection();
186
- };
187
- let commitCustomValue = ()=>{
188
- lastSelectedKey.current = null;
189
- setSelectedKey(null);
190
- closeMenu();
191
- };
192
- let commitSelection = ()=>{
193
- // If multiple things are controlled, call onSelectionChange
194
- if (props.selectedKey !== undefined && props.inputValue !== undefined) {
195
- var _props_onSelectionChange, _collection_getItem;
196
- (_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, selectedKey);
197
- var _collection_getItem_textValue;
198
- // Stop menu from reopening from useEffect
199
- 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 : '' : '';
200
- setLastValue(itemText);
201
- closeMenu();
202
- } else {
203
- // If only a single aspect of combobox is controlled, reset input value and close menu for the user
204
- resetInputValue();
205
- closeMenu();
206
- }
207
- };
208
- const commitValue = ()=>{
209
- if (allowsCustomValue) {
210
- var _collection_getItem;
211
- var _collection_getItem_textValue;
212
- const 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 : '' : '';
213
- inputValue === itemText ? commitSelection() : commitCustomValue();
214
- } else // Reset inputValue and close menu
215
- commitSelection();
216
- };
217
- let commit = ()=>{
218
- if (triggerState.isOpen && selectionManager.focusedKey != null) {
219
- // Reset inputValue and close menu here if the selected key is already the focused key. Otherwise
220
- // fire onSelectionChange to allow the application to control the closing.
221
- if (selectedKey === selectionManager.focusedKey) commitSelection();
222
- else setSelectedKey(selectionManager.focusedKey);
223
- } else commitValue();
224
- };
225
- let valueOnFocus = (0, $49BJP$useRef)(inputValue);
226
- let setFocused = (isFocused)=>{
227
- if (isFocused) {
228
- valueOnFocus.current = inputValue;
229
- if (menuTrigger === 'focus' && !props.isReadOnly) open(null, 'focus');
230
- } else {
231
- if (shouldCloseOnBlur) commitValue();
232
- if (inputValue !== valueOnFocus.current) validation.commitValidation();
233
- }
234
- setFocusedState(isFocused);
235
- };
236
- let displayedCollection = (0, $49BJP$useMemo)(()=>{
237
- if (triggerState.isOpen) {
238
- if (showAllItems) return originalCollection;
239
- else return filteredCollection;
240
- } else return lastCollection;
241
- }, [
242
- triggerState.isOpen,
243
- originalCollection,
244
- filteredCollection,
245
- showAllItems,
246
- lastCollection
247
- ]);
248
- var _props_defaultSelectedKey;
249
- let defaultSelectedKey = (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : initialSelectedKey;
250
- var _getDefaultInputValue;
251
- return {
252
- ...validation,
253
- ...triggerState,
254
- focusStrategy: focusStrategy,
255
- toggle: toggle,
256
- open: open,
257
- close: commitValue,
258
- selectionManager: selectionManager,
259
- selectedKey: selectedKey,
260
- defaultSelectedKey: defaultSelectedKey,
261
- setSelectedKey: setSelectedKey,
262
- disabledKeys: disabledKeys,
263
- isFocused: isFocused,
264
- setFocused: setFocused,
265
- selectedItem: selectedItem,
266
- collection: displayedCollection,
267
- inputValue: inputValue,
268
- defaultInputValue: (_getDefaultInputValue = $a9e7382a7d111cb5$var$getDefaultInputValue(props.defaultInputValue, defaultSelectedKey, collection)) !== null && _getDefaultInputValue !== void 0 ? _getDefaultInputValue : initialValue,
269
- setInputValue: setInputValue,
270
- commit: commit,
271
- revert: revert
272
- };
273
- }
274
- function $a9e7382a7d111cb5$var$filterCollection(collection, inputValue, filter) {
275
- return new (0, $49BJP$ListCollection)($a9e7382a7d111cb5$var$filterNodes(collection, collection, inputValue, filter));
276
- }
277
- function $a9e7382a7d111cb5$var$filterNodes(collection, nodes, inputValue, filter) {
278
- let filteredNode = [];
279
- for (let node of nodes){
280
- if (node.type === 'section' && node.hasChildNodes) {
281
- let filtered = $a9e7382a7d111cb5$var$filterNodes(collection, (0, $49BJP$getChildNodes)(node, collection), inputValue, filter);
282
- if ([
283
- ...filtered
284
- ].some((node)=>node.type === 'item')) filteredNode.push({
285
- ...node,
286
- childNodes: filtered
287
- });
288
- } else if (node.type === 'item' && filter(node.textValue, inputValue)) filteredNode.push({
289
- ...node
290
- });
291
- else if (node.type !== 'item') filteredNode.push({
292
- ...node
293
- });
294
- }
295
- return filteredNode;
296
- }
297
- function $a9e7382a7d111cb5$var$getDefaultInputValue(defaultInputValue, selectedKey, collection) {
298
- if (defaultInputValue == null) {
299
- var _collection_getItem;
300
- var _collection_getItem_textValue;
301
- if (selectedKey != null) return (_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 : '';
302
- }
303
- return defaultInputValue;
304
- }
305
-
306
-
307
- export {$a9e7382a7d111cb5$export$b453a3bfd4a5fa9e as useComboBoxState};
308
- //# sourceMappingURL=useComboBoxState.module.js.map
@@ -1,308 +0,0 @@
1
- import {useFormValidationState as $49BJP$useFormValidationState} from "@react-stately/form";
2
- import {getChildNodes as $49BJP$getChildNodes} from "@react-stately/collections";
3
- import {useSingleSelectListState as $49BJP$useSingleSelectListState, ListCollection as $49BJP$ListCollection} from "@react-stately/list";
4
- import {useOverlayTriggerState as $49BJP$useOverlayTriggerState} from "@react-stately/overlays";
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
- import {useControlledState as $49BJP$useControlledState} from "@react-stately/utils";
7
-
8
- /*
9
- * Copyright 2020 Adobe. All rights reserved.
10
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
11
- * you may not use this file except in compliance with the License. You may obtain a copy
12
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
13
- *
14
- * Unless required by applicable law or agreed to in writing, software distributed under
15
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
16
- * OF ANY KIND, either express or implied. See the License for the specific language
17
- * governing permissions and limitations under the License.
18
- */
19
-
20
-
21
-
22
-
23
-
24
- function $a9e7382a7d111cb5$export$b453a3bfd4a5fa9e(props) {
25
- var _collection_getItem;
26
- let { defaultFilter: defaultFilter, menuTrigger: menuTrigger = 'input', allowsEmptyCollection: allowsEmptyCollection = false, allowsCustomValue: allowsCustomValue, shouldCloseOnBlur: shouldCloseOnBlur = true } = props;
27
- let [showAllItems, setShowAllItems] = (0, $49BJP$useState)(false);
28
- let [isFocused, setFocusedState] = (0, $49BJP$useState)(false);
29
- let [focusStrategy, setFocusStrategy] = (0, $49BJP$useState)(null);
30
- let onSelectionChange = (key)=>{
31
- if (props.onSelectionChange) props.onSelectionChange(key);
32
- // If key is the same, reset the inputValue and close the menu
33
- // (scenario: user clicks on already selected option)
34
- if (key === selectedKey) {
35
- resetInputValue();
36
- closeMenu();
37
- }
38
- };
39
- var _props_items;
40
- let { collection: collection, selectionManager: selectionManager, selectedKey: selectedKey, setSelectedKey: setSelectedKey, selectedItem: selectedItem, disabledKeys: disabledKeys } = (0, $49BJP$useSingleSelectListState)({
41
- ...props,
42
- onSelectionChange: onSelectionChange,
43
- items: (_props_items = props.items) !== null && _props_items !== void 0 ? _props_items : props.defaultItems
44
- });
45
- let [inputValue, setInputValue] = (0, $49BJP$useControlledState)(props.inputValue, $a9e7382a7d111cb5$var$getDefaultInputValue(props.defaultInputValue, selectedKey, collection) || '', props.onInputChange);
46
- let [initialSelectedKey] = (0, $49BJP$useState)(selectedKey);
47
- let [initialValue] = (0, $49BJP$useState)(inputValue);
48
- // Preserve original collection so we can show all items on demand
49
- let originalCollection = collection;
50
- let filteredCollection = (0, $49BJP$useMemo)(()=>// No default filter if items are controlled.
51
- props.items != null || !defaultFilter ? collection : $a9e7382a7d111cb5$var$filterCollection(collection, inputValue, defaultFilter), [
52
- collection,
53
- inputValue,
54
- defaultFilter,
55
- props.items
56
- ]);
57
- let [lastCollection, setLastCollection] = (0, $49BJP$useState)(filteredCollection);
58
- // Track what action is attempting to open the menu
59
- let menuOpenTrigger = (0, $49BJP$useRef)('focus');
60
- let onOpenChange = (open)=>{
61
- if (props.onOpenChange) props.onOpenChange(open, open ? menuOpenTrigger.current : undefined);
62
- selectionManager.setFocused(open);
63
- if (!open) selectionManager.setFocusedKey(null);
64
- };
65
- let triggerState = (0, $49BJP$useOverlayTriggerState)({
66
- ...props,
67
- onOpenChange: onOpenChange,
68
- isOpen: undefined,
69
- defaultOpen: undefined
70
- });
71
- let open = (focusStrategy = null, trigger)=>{
72
- let displayAllItems = trigger === 'manual' || trigger === 'focus' && menuTrigger === 'focus';
73
- // Prevent open operations from triggering if there is nothing to display
74
- // Also prevent open operations from triggering if items are uncontrolled but defaultItems is empty, even if displayAllItems is true.
75
- // This is to prevent comboboxes with empty defaultItems from opening but allow controlled items comboboxes to open even if the inital list is empty (assumption is user will provide swap the empty list with a base list via onOpenChange returning `menuTrigger` manual)
76
- if (allowsEmptyCollection || filteredCollection.size > 0 || displayAllItems && originalCollection.size > 0 || props.items) {
77
- if (displayAllItems && !triggerState.isOpen && props.items === undefined) // Show all items if menu is manually opened. Only care about this if items are undefined
78
- setShowAllItems(true);
79
- menuOpenTrigger.current = trigger;
80
- setFocusStrategy(focusStrategy);
81
- triggerState.open();
82
- }
83
- };
84
- let toggle = (focusStrategy = null, trigger)=>{
85
- let displayAllItems = trigger === 'manual' || trigger === 'focus' && menuTrigger === 'focus';
86
- // If the menu is closed and there is nothing to display, early return so toggle isn't called to prevent extraneous onOpenChange
87
- if (!(allowsEmptyCollection || filteredCollection.size > 0 || displayAllItems && originalCollection.size > 0 || props.items) && !triggerState.isOpen) return;
88
- if (displayAllItems && !triggerState.isOpen && props.items === undefined) // Show all items if menu is toggled open. Only care about this if items are undefined
89
- setShowAllItems(true);
90
- // Only update the menuOpenTrigger if menu is currently closed
91
- if (!triggerState.isOpen) menuOpenTrigger.current = trigger;
92
- toggleMenu(focusStrategy);
93
- };
94
- let updateLastCollection = (0, $49BJP$useCallback)(()=>{
95
- setLastCollection(showAllItems ? originalCollection : filteredCollection);
96
- }, [
97
- showAllItems,
98
- originalCollection,
99
- filteredCollection
100
- ]);
101
- // If menu is going to close, save the current collection so we can freeze the displayed collection when the
102
- // user clicks outside the popover to close the menu. Prevents the menu contents from updating as the menu closes.
103
- let toggleMenu = (0, $49BJP$useCallback)((focusStrategy = null)=>{
104
- if (triggerState.isOpen) updateLastCollection();
105
- setFocusStrategy(focusStrategy);
106
- triggerState.toggle();
107
- }, [
108
- triggerState,
109
- updateLastCollection
110
- ]);
111
- let closeMenu = (0, $49BJP$useCallback)(()=>{
112
- if (triggerState.isOpen) {
113
- updateLastCollection();
114
- triggerState.close();
115
- }
116
- }, [
117
- triggerState,
118
- updateLastCollection
119
- ]);
120
- let [lastValue, setLastValue] = (0, $49BJP$useState)(inputValue);
121
- let resetInputValue = ()=>{
122
- var _collection_getItem;
123
- var _collection_getItem_textValue;
124
- 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 : '' : '';
125
- setLastValue(itemText);
126
- setInputValue(itemText);
127
- };
128
- var _props_selectedKey, _ref;
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);
130
- var _collection_getItem_textValue;
131
- 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
- // intentional omit dependency array, want this to happen on every render
133
- // eslint-disable-next-line react-hooks/exhaustive-deps
134
- (0, $49BJP$useEffect)(()=>{
135
- var _collection_getItem;
136
- // Open and close menu automatically when the input value changes if the input is focused,
137
- // and there are items in the collection or allowEmptyCollection is true.
138
- if (isFocused && (filteredCollection.size > 0 || allowsEmptyCollection) && !triggerState.isOpen && inputValue !== lastValue && menuTrigger !== 'manual') open(null, 'input');
139
- // Close the menu if the collection is empty. Don't close menu if filtered collection size is 0
140
- // but we are currently showing all items via button press
141
- if (!showAllItems && !allowsEmptyCollection && triggerState.isOpen && filteredCollection.size === 0) closeMenu();
142
- // Close when an item is selected.
143
- if (selectedKey != null && selectedKey !== lastSelectedKey.current) closeMenu();
144
- // Clear focused key when input value changes and display filtered collection again.
145
- if (inputValue !== lastValue) {
146
- selectionManager.setFocusedKey(null);
147
- setShowAllItems(false);
148
- // Set selectedKey to null when the user clears the input.
149
- // If controlled, this is the application developer's responsibility.
150
- if (inputValue === '' && (props.inputValue === undefined || props.selectedKey === undefined)) setSelectedKey(null);
151
- }
152
- // If the selectedKey changed, update the input value.
153
- // Do nothing if both inputValue and selectedKey are controlled.
154
- // In this case, it's the user's responsibility to update inputValue in onSelectionChange.
155
- if (selectedKey !== lastSelectedKey.current && (props.inputValue === undefined || props.selectedKey === undefined)) resetInputValue();
156
- else if (lastValue !== inputValue) setLastValue(inputValue);
157
- var _collection_getItem_textValue;
158
- // Update the inputValue if the selected item's text changes from its last tracked value.
159
- // This is to handle cases where a selectedKey is specified but the items aren't available (async loading) or the selected item's text value updates.
160
- // Only reset if the user isn't currently within the field so we don't erroneously modify user input.
161
- // If inputValue is controlled, it is the user's responsibility to update the inputValue when items change.
162
- 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 === lastSelectedKey.current) {
164
- if (lastSelectedKeyText.current !== selectedItemText) {
165
- setLastValue(selectedItemText);
166
- setInputValue(selectedItemText);
167
- }
168
- }
169
- lastSelectedKey.current = selectedKey;
170
- lastSelectedKeyText.current = selectedItemText;
171
- });
172
- let validation = (0, $49BJP$useFormValidationState)({
173
- ...props,
174
- value: (0, $49BJP$useMemo)(()=>({
175
- inputValue: inputValue,
176
- selectedKey: selectedKey
177
- }), [
178
- inputValue,
179
- selectedKey
180
- ])
181
- });
182
- // Revert input value and close menu
183
- let revert = ()=>{
184
- if (allowsCustomValue && selectedKey == null) commitCustomValue();
185
- else commitSelection();
186
- };
187
- let commitCustomValue = ()=>{
188
- lastSelectedKey.current = null;
189
- setSelectedKey(null);
190
- closeMenu();
191
- };
192
- let commitSelection = ()=>{
193
- // If multiple things are controlled, call onSelectionChange
194
- if (props.selectedKey !== undefined && props.inputValue !== undefined) {
195
- var _props_onSelectionChange, _collection_getItem;
196
- (_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, selectedKey);
197
- var _collection_getItem_textValue;
198
- // Stop menu from reopening from useEffect
199
- 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 : '' : '';
200
- setLastValue(itemText);
201
- closeMenu();
202
- } else {
203
- // If only a single aspect of combobox is controlled, reset input value and close menu for the user
204
- resetInputValue();
205
- closeMenu();
206
- }
207
- };
208
- const commitValue = ()=>{
209
- if (allowsCustomValue) {
210
- var _collection_getItem;
211
- var _collection_getItem_textValue;
212
- const 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 : '' : '';
213
- inputValue === itemText ? commitSelection() : commitCustomValue();
214
- } else // Reset inputValue and close menu
215
- commitSelection();
216
- };
217
- let commit = ()=>{
218
- if (triggerState.isOpen && selectionManager.focusedKey != null) {
219
- // Reset inputValue and close menu here if the selected key is already the focused key. Otherwise
220
- // fire onSelectionChange to allow the application to control the closing.
221
- if (selectedKey === selectionManager.focusedKey) commitSelection();
222
- else setSelectedKey(selectionManager.focusedKey);
223
- } else commitValue();
224
- };
225
- let valueOnFocus = (0, $49BJP$useRef)(inputValue);
226
- let setFocused = (isFocused)=>{
227
- if (isFocused) {
228
- valueOnFocus.current = inputValue;
229
- if (menuTrigger === 'focus' && !props.isReadOnly) open(null, 'focus');
230
- } else {
231
- if (shouldCloseOnBlur) commitValue();
232
- if (inputValue !== valueOnFocus.current) validation.commitValidation();
233
- }
234
- setFocusedState(isFocused);
235
- };
236
- let displayedCollection = (0, $49BJP$useMemo)(()=>{
237
- if (triggerState.isOpen) {
238
- if (showAllItems) return originalCollection;
239
- else return filteredCollection;
240
- } else return lastCollection;
241
- }, [
242
- triggerState.isOpen,
243
- originalCollection,
244
- filteredCollection,
245
- showAllItems,
246
- lastCollection
247
- ]);
248
- var _props_defaultSelectedKey;
249
- let defaultSelectedKey = (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : initialSelectedKey;
250
- var _getDefaultInputValue;
251
- return {
252
- ...validation,
253
- ...triggerState,
254
- focusStrategy: focusStrategy,
255
- toggle: toggle,
256
- open: open,
257
- close: commitValue,
258
- selectionManager: selectionManager,
259
- selectedKey: selectedKey,
260
- defaultSelectedKey: defaultSelectedKey,
261
- setSelectedKey: setSelectedKey,
262
- disabledKeys: disabledKeys,
263
- isFocused: isFocused,
264
- setFocused: setFocused,
265
- selectedItem: selectedItem,
266
- collection: displayedCollection,
267
- inputValue: inputValue,
268
- defaultInputValue: (_getDefaultInputValue = $a9e7382a7d111cb5$var$getDefaultInputValue(props.defaultInputValue, defaultSelectedKey, collection)) !== null && _getDefaultInputValue !== void 0 ? _getDefaultInputValue : initialValue,
269
- setInputValue: setInputValue,
270
- commit: commit,
271
- revert: revert
272
- };
273
- }
274
- function $a9e7382a7d111cb5$var$filterCollection(collection, inputValue, filter) {
275
- return new (0, $49BJP$ListCollection)($a9e7382a7d111cb5$var$filterNodes(collection, collection, inputValue, filter));
276
- }
277
- function $a9e7382a7d111cb5$var$filterNodes(collection, nodes, inputValue, filter) {
278
- let filteredNode = [];
279
- for (let node of nodes){
280
- if (node.type === 'section' && node.hasChildNodes) {
281
- let filtered = $a9e7382a7d111cb5$var$filterNodes(collection, (0, $49BJP$getChildNodes)(node, collection), inputValue, filter);
282
- if ([
283
- ...filtered
284
- ].some((node)=>node.type === 'item')) filteredNode.push({
285
- ...node,
286
- childNodes: filtered
287
- });
288
- } else if (node.type === 'item' && filter(node.textValue, inputValue)) filteredNode.push({
289
- ...node
290
- });
291
- else if (node.type !== 'item') filteredNode.push({
292
- ...node
293
- });
294
- }
295
- return filteredNode;
296
- }
297
- function $a9e7382a7d111cb5$var$getDefaultInputValue(defaultInputValue, selectedKey, collection) {
298
- if (defaultInputValue == null) {
299
- var _collection_getItem;
300
- var _collection_getItem_textValue;
301
- if (selectedKey != null) return (_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 : '';
302
- }
303
- return defaultInputValue;
304
- }
305
-
306
-
307
- export {$a9e7382a7d111cb5$export$b453a3bfd4a5fa9e as useComboBoxState};
308
- //# sourceMappingURL=useComboBoxState.module.js.map