Message suggestions stay open when an empty field is clicked: the field's own focus no longer counts as outside the list
This commit is contained in:
@@ -156,6 +156,7 @@ function SuggestInput({
|
|||||||
onChange: (next: string) => void
|
onChange: (next: string) => void
|
||||||
} & Omit<ComponentProps<typeof Input>, "value" | "onChange">) {
|
} & Omit<ComponentProps<typeof Input>, "value" | "onChange">) {
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
|
const input = useRef<HTMLInputElement>(null)
|
||||||
const matches = suggestions.filter(
|
const matches = suggestions.filter(
|
||||||
(item) =>
|
(item) =>
|
||||||
item !== value && item.toLowerCase().includes(value.toLowerCase()),
|
item !== value && item.toLowerCase().includes(value.toLowerCase()),
|
||||||
@@ -168,6 +169,7 @@ function SuggestInput({
|
|||||||
value={value}
|
value={value}
|
||||||
autoComplete="off"
|
autoComplete="off"
|
||||||
{...props}
|
{...props}
|
||||||
|
ref={input}
|
||||||
onFocus={(event) => {
|
onFocus={(event) => {
|
||||||
setOpen(true)
|
setOpen(true)
|
||||||
onFocus?.(event)
|
onFocus?.(event)
|
||||||
@@ -189,6 +191,12 @@ function SuggestInput({
|
|||||||
align="start"
|
align="start"
|
||||||
className="w-[var(--radix-popover-trigger-width)] p-0"
|
className="w-[var(--radix-popover-trigger-width)] p-0"
|
||||||
onOpenAutoFocus={(event) => event.preventDefault()}
|
onOpenAutoFocus={(event) => event.preventDefault()}
|
||||||
|
// The field is outside the list. Focusing it opens the list, and the
|
||||||
|
// same focus event then reaches the list's outside check, which would
|
||||||
|
// close it again at once. Its own blur closes it instead.
|
||||||
|
onInteractOutside={(event) => {
|
||||||
|
if (event.target === input.current) event.preventDefault()
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<Command shouldFilter={false}>
|
<Command shouldFilter={false}>
|
||||||
<CommandList>
|
<CommandList>
|
||||||
|
|||||||
Reference in New Issue
Block a user