Skip to content

Commit 757131f

Browse files
committed
remove selection from useSearchAutocomplete
1 parent 0a1d0cd commit 757131f

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

packages/@react-aria/autocomplete/src/useSearchAutocomplete.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ export function useSearchAutocomplete<T>(props: AriaSearchAutocompleteOptions<T>
9393
listBoxRef,
9494
inputRef,
9595
onFocus: undefined,
96-
onBlur: undefined
96+
onBlur: undefined,
97+
selectedKey: null
9798
},
9899
state
99100
);

packages/@react-spectrum/autocomplete/src/SearchAutocomplete.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ function _SearchAutocompleteBase<T extends object>(props: SpectrumSearchAutocomp
8989
defaultFilter: contains,
9090
allowsEmptyCollection: isAsync,
9191
allowsCustomValue: true,
92-
onSelectionChange: (key) => key !== null && onSubmit(null, key),
93-
selectedKey: undefined,
94-
defaultSelectedKey: undefined
92+
selectedKey: null,
93+
defaultSelectedKey: null
9594
}
9695
);
9796
let layout = useListBoxLayout(state);

packages/@react-stately/combobox/src/useComboBoxState.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
6060
} = props;
6161

6262
let [showAllItems, setShowAllItems] = useState(false);
63+
let [showNoItems, setShowNoItems] = useState(false);
6364
let [isFocused, setFocusedState] = useState(false);
6465
let [inputValue, setInputValue] = useControlledState(
6566
props.inputValue,
@@ -72,6 +73,15 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
7273
props.onSelectionChange(key);
7374
}
7475

76+
// If key is controlled to be null, unset the selectedKey, set the inputValue, and show no items
77+
if (props.selectedKey === null) {
78+
setSelectedKey(null);
79+
setShowNoItems(true);
80+
if (key) {
81+
setInputValue(collection.getItem(key)?.textValue ?? '');
82+
}
83+
}
84+
7585
// If key is the same, reset the inputValue and close the menu
7686
// (scenario: user clicks on already selected option)
7787
if (key === selectedKey) {
@@ -92,8 +102,8 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
92102
// No default filter if items are controlled.
93103
props.items != null || !defaultFilter
94104
? collection
95-
: filterCollection(collection, inputValue, defaultFilter)
96-
), [collection, inputValue, defaultFilter, props.items]);
105+
: filterCollection(collection, inputValue, showNoItems ? () => false : defaultFilter)
106+
), [props.items, defaultFilter, collection, inputValue, showNoItems]);
97107
let [lastCollection, setLastCollection] = useState(filteredCollection);
98108

99109
// Track what action is attempting to open the menu
@@ -253,6 +263,13 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
253263
lastSelectedKeyText.current = selectedItemText;
254264
});
255265

266+
useEffect(() => {
267+
if (showNoItems) {
268+
setShowNoItems(false);
269+
}
270+
// eslint-disable-next-line react-hooks/exhaustive-deps
271+
}, [inputValue]);
272+
256273
// Revert input value and close menu
257274
let revert = () => {
258275
if (allowsCustomValue && selectedKey == null) {

0 commit comments

Comments
 (0)