ComboBox state isOpen=false despite all conditions fulfilled
🐛 Bug Report
I have a custom ComboBox component in my component library built with react-aria and react-stately.
type ComboBoxProps = { shouldFilter?: boolean } & ComboBoxStateOptions<any>
const ComboBox = React.forwardRef<HTMLInputElement, ComboBoxProps>(
({ shouldFilter, ...props }, passedRef) => {
const { validationState } = props
// Setup filter function and state.
const { contains } = useFilter({ sensitivity: 'base' })
const state = useComboBoxState({
...props,
defaultFilter: shouldFilter ? contains : undefined,
})
const inputRef = usePassedRef(passedRef)
const buttonRef = React.useRef(null)
const listBoxRef = React.useRef(null)
const popoverRef = React.useRef(null)
const { isFocused, focusProps } = useFocusRing({ isTextInput: true })
const { buttonProps, inputProps, listBoxProps, ...wrapperProps } =
useComboBox(
{
...props,
inputRef,
buttonRef,
listBoxRef,
popoverRef,
},
state
)
const isValid = !validationState || validationState === 'valid'
console.log('state', state.collection.size, state.isOpen)
return (
<FieldWrapper {...mergeProps(wrapperProps, props)}>
<div className="relative inline-flex">
<input
ref={inputRef}
className={inputStyle(isFocused, isValid) + ' flex-grow'}
{...mergeProps(focusProps, inputProps)}
/>
<Button unstyled ref={buttonRef} {...buttonProps}>
<HeroIcon
aria-hidden="true"
icon="chevron-down"
className="absolute w-3 right-3 top-1/2 -translate-y-1/2"
/>
</Button>
</div>
<Appear show={state.isOpen}>
<Popover ref={popoverRef} isOpen={state.isOpen} onClose={state.close}>
<ListBox {...listBoxProps} ref={listBoxRef} state={state} />
</Popover>
</Appear>
</FieldWrapper>
)
}
)
And I'm using it in my consumer project with disabled filtering from useComboBoxState hook and manually updating the items to manipulate the content of the popover.
<ComboBox
ref={ref}
autoFocus
aria-label="address"
placeholder="Search address"
items={suggestions}
onSelectionChange={selectSugggestion}
onInputChange={updateSuggestions}
shouldCloseOnBlur={true}
shouldFilter={false}
onBlur={onBlur}
>
{(suggestion) => (
<ComboBox.Item>{suggestion.place_name}</ComboBox.Item>
)}
</ComboBox>
🤔 Expected Behavior
I'm expecting the popover to show (i.e the state.isOpen variable to change) as soon as the state.collection has any items.
😯 Current Behavior
As soon as I have anything in my defaultItems, the state.collection is updated with the items but the state.isOpen variable does not change until the next re-render.
As is probably apparent from the code, I'm looking up addresses with some API. The consequence is essentially that when I have a suggestion, I update the defaultItems, but the suggestions will not show until i insert another character into the input (e.g a space).
🌍 Your Environment
| Software | Version(s) |
|---|---|
| react-aria | 3.20.0 |
| react-stately | 3.18.0 |
| Browser | Chrome |
| Operating System | MacOs |
Solved it by enabling theallowsEmptyCollection option in useComboBoxState hook