{
+ if (rootRef.current?.contains(event.relatedTarget as Node | null)) return
+ closeCombobox()
+ }}
+ data-testid="filter-field-combobox"
+ >
+
openCombobox()}
+ onChange={(event) => {
+ const nextQuery = event.target.value
+ const nextGroups = buildFieldGroups(fields, value, nextQuery)
+ const nextOptions = flattenGroups(nextGroups)
+ setOpen(true)
+ setQuery(nextQuery)
+ setHasTyped(true)
+ setHighlightedIndex(nextOptions.length > 0 ? 0 : -1)
+ }}
+ onKeyDown={(event) => {
+ if (event.key === 'ArrowDown') {
+ event.preventDefault()
+ if (!open) {
+ openCombobox()
+ return
+ }
+ if (options.length === 0) return
+ setHighlightedIndex((current) => {
+ if (current < 0) return 0
+ return (current + 1) % options.length
+ })
+ return
+ }
+
+ if (event.key === 'ArrowUp') {
+ event.preventDefault()
+ if (!open) {
+ openCombobox()
+ return
+ }
+ if (options.length === 0) return
+ setHighlightedIndex((current) => {
+ if (current < 0) return options.length - 1
+ return (current - 1 + options.length) % options.length
+ })
+ return
+ }
+
+ if (event.key === 'Enter') {
+ if (!open || highlightedIndex < 0 || options[highlightedIndex] === undefined) return
+ event.preventDefault()
+ selectOption(options[highlightedIndex])
+ return
+ }
+
+ if (event.key === 'Escape') {
+ if (!open) return
+ event.preventDefault()
+ closeCombobox()
+ }
+ }}
+ role="combobox"
+ aria-autocomplete="list"
+ aria-controls={listboxId}
+ aria-expanded={open}
+ aria-activedescendant={highlightedIndex >= 0 ? `${listboxId}-option-${highlightedIndex}` : undefined}
+ className="h-8 pr-7 text-sm"
+ data-testid="filter-field-combobox-input"
+ />
+
+ {open && (
+
+ {options.length === 0 ? (
+
+ No results
+
+ ) : (
+ fieldGroups.map((group, groupIndex) => (
+
+ {groupIndex > 0 &&
}
+ {group.options.map((field) => {
+ const optionIndex = options.indexOf(field)
+ return (
+
+ )
+ })}
+
+ ))
+ )}
+
+ )}
+
+ )
+}