@@ -60,6 +60,7 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
60
60
} = props ;
61
61
62
62
let [ showAllItems , setShowAllItems ] = useState ( false ) ;
63
+ let [ showNoItems , setShowNoItems ] = useState ( false ) ;
63
64
let [ isFocused , setFocusedState ] = useState ( false ) ;
64
65
let [ inputValue , setInputValue ] = useControlledState (
65
66
props . inputValue ,
@@ -72,6 +73,15 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
72
73
props . onSelectionChange ( key ) ;
73
74
}
74
75
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
+
75
85
// If key is the same, reset the inputValue and close the menu
76
86
// (scenario: user clicks on already selected option)
77
87
if ( key === selectedKey ) {
@@ -92,8 +102,8 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
92
102
// No default filter if items are controlled.
93
103
props . items != null || ! defaultFilter
94
104
? 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 ] ) ;
97
107
let [ lastCollection , setLastCollection ] = useState ( filteredCollection ) ;
98
108
99
109
// Track what action is attempting to open the menu
@@ -253,6 +263,13 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
253
263
lastSelectedKeyText . current = selectedItemText ;
254
264
} ) ;
255
265
266
+ useEffect ( ( ) => {
267
+ if ( showNoItems ) {
268
+ setShowNoItems ( false ) ;
269
+ }
270
+ // eslint-disable-next-line react-hooks/exhaustive-deps
271
+ } , [ inputValue ] ) ;
272
+
256
273
// Revert input value and close menu
257
274
let revert = ( ) => {
258
275
if ( allowsCustomValue && selectedKey == null ) {
0 commit comments