Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions apps/v4/registry/new-york-v4/examples/combobox-dropdown-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as React from "react"
import { MoreHorizontal } from "lucide-react"


import { Button } from "@/registry/new-york-v4/ui/button"
import {
Command,
Expand Down Expand Up @@ -38,7 +39,17 @@ const labels = [

export default function ComboboxDropdownMenu() {
const [label, setLabel] = React.useState("feature")
const [open, setOpen] = React.useState(false)
const [open, setOpen] = React.useState(false)
// Autofocus fix for CommandInput inside DropdownMenu
const inputRef = React.useRef<HTMLInputElement>(null)

React.useEffect(() => {
if (open && inputRef.current) {
setTimeout(() => {
inputRef.current?.focus()
}, 10)
}
}, [open])

return (
<div className="flex w-full flex-col items-start justify-between rounded-md border px-4 py-3 sm:flex-row sm:items-center">
Expand All @@ -64,11 +75,11 @@ export default function ComboboxDropdownMenu() {
<DropdownMenuSubTrigger>Apply label</DropdownMenuSubTrigger>
<DropdownMenuSubContent className="p-0">
<Command>
<CommandInput
placeholder="Filter label..."
autoFocus={true}
className="h-9"
/>
<CommandInput
ref={inputRef}
placeholder="Filter label..."
className="h-9"
/>
<CommandList>
<CommandEmpty>No label found.</CommandEmpty>
<CommandGroup>
Expand Down
24 changes: 19 additions & 5 deletions apps/v4/registry/new-york-v4/examples/combobox-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ export default function ComboboxForm() {
),
})
}
const [open, setOpen] = React.useState(false)
const inputRef = React.useRef<HTMLInputElement>(null)

React.useEffect(() => {
if (open && inputRef.current) {
setTimeout(() => {
inputRef.current?.focus()
}, 10)
}
}, [open])



return (
<Form {...form}>
Expand All @@ -73,7 +85,7 @@ export default function ComboboxForm() {
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel>Language</FormLabel>
<Popover>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<FormControl>
<Button
Expand All @@ -95,10 +107,12 @@ export default function ComboboxForm() {
</PopoverTrigger>
<PopoverContent className="w-[200px] p-0">
<Command>
<CommandInput
placeholder="Search framework..."
className="h-9"
/>
<CommandInput
ref={inputRef}
placeholder="Search language..."
className="h-9"
/>

<CommandList>
<CommandEmpty>No framework found.</CommandEmpty>
<CommandGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ export default function ComboboxDropdownMenu() {
const [label, setLabel] = React.useState("feature")
const [open, setOpen] = React.useState(false)

// Autofocus fix (added)
const inputRef = React.useRef<HTMLInputElement>(null)

React.useEffect(() => {
if (open && inputRef.current) {
setTimeout(() => {
inputRef.current?.focus()
}, 10)
}
}, [open])

return (
<div className="flex w-full flex-col items-start justify-between rounded-md border px-4 py-3 sm:flex-row sm:items-center">
<p className="text-sm font-medium leading-none">
Expand Down Expand Up @@ -74,9 +85,9 @@ export default function ComboboxDropdownMenu() {
<DropdownMenuSubContent className="p-0">
<Command>
<CommandInput
placeholder="Filter label..."
autoFocus={true}
/>
ref={inputRef}
placeholder="Filter label..."
/>
<CommandList>
<CommandEmpty>No label found.</CommandEmpty>
<CommandGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ const labels = [
export default function ComboboxDropdownMenu() {
const [label, setLabel] = React.useState("feature")
const [open, setOpen] = React.useState(false)
const inputRef = React.useRef<HTMLInputElement>(null)

React.useEffect(() => {
if (open && inputRef.current) {
setTimeout(() => {
inputRef.current?.focus()
}, 10)
}
}, [open])

return (
<div className="flex w-full flex-col items-start justify-between rounded-md border px-4 py-3 sm:flex-row sm:items-center">
Expand All @@ -65,10 +74,10 @@ export default function ComboboxDropdownMenu() {
<DropdownMenuSubContent className="p-0">
<Command>
<CommandInput
placeholder="Filter label..."
autoFocus={true}
className="h-9"
/>
ref={inputRef}
placeholder="Filter label..."
className="h-9"
/>
<CommandList>
<CommandEmpty>No label found.</CommandEmpty>
<CommandGroup>
Expand Down
18 changes: 16 additions & 2 deletions deprecated/www/registry/new-york/examples/combobox-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ export default function ComboboxForm() {
),
})
}
// Autofocus fix
const [open, setOpen] = React.useState(false)
const inputRef = React.useRef<HTMLInputElement>(null)

React.useEffect(() => {
if (open && inputRef.current) {
setTimeout(() => {
inputRef.current?.focus()
}, 10)
}
}, [open])


return (
<Form {...form}>
Expand All @@ -74,7 +86,8 @@ export default function ComboboxForm() {
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel>Language</FormLabel>
<Popover>
<Popover open={open} onOpenChange={setOpen}>

<PopoverTrigger asChild>
<FormControl>
<Button
Expand All @@ -97,7 +110,8 @@ export default function ComboboxForm() {
<PopoverContent className="w-[200px] p-0">
<Command>
<CommandInput
placeholder="Search framework..."
ref={inputRef}
placeholder="Search language..."
className="h-9"
/>
<CommandList>
Expand Down