pacocoursey / cmdk

Fast, unstyled command menu React component.
https://cmdk.paco.me
MIT License
9.58k stars 275 forks source link

selection dialog is not being auto-closed after selecting an item. It requires user act to close it #301

Closed iranzithierry closed 1 week ago

iranzithierry commented 1 week ago

here's how am using it

<Command>
                    <CommandInput
                        placeholder={`Search ${title}...`}
                        className="h-9"
                    />
                    <CommandList>
                        <CommandEmpty>{`No ${title}s found.`}</CommandEmpty>
                        <CommandGroup>
                            {values.map((value) => (
                                <CommandItem
                                    value={value.label}
                                    key={value.value}
                                    onSelect={() => {
                                        setSelectedValue(value.value)
                                    }}
                                >
                                    {value.label}
                                    <CheckIcon
                                        className={cn(
                                            "ml-auto h-4 w-4",
                                            value.value === selectedValue
                                                ? "opacity-100"
                                                : "opacity-0"
                                        )}
                                    />
                                </CommandItem>
                            ))}
                        </CommandGroup>
                    </CommandList>
                </Command>
Lexachoc commented 1 week ago

looks like you're using shadcn. Calling setOpen(false) closes the dialog after selection.

https://github.com/pacocoursey/cmdk?tab=readme-ov-file#use


const [open, setOpen] = React.useState(false)

<Command>
    <CommandInput
        placeholder={`Search ${title}...`}
        className="h-9"
    />
    <CommandList>
        <CommandEmpty>{`No ${title}s found.`}</CommandEmpty>
        <CommandGroup>
            {values.map((value) => (
                <CommandItem
                    value={value.label}
                    key={value.value}
                    onSelect={() => {
                        setSelectedValue(value.value)
                        setOpen(false)
                    }}
                >
                    {value.label}
                    <CheckIcon
                        className={cn(
                            "ml-auto h-4 w-4",
                            value.value === selectedValue
                                ? "opacity-100"
                                : "opacity-0"
                        )}
                    />
                </CommandItem>
            ))}
        </CommandGroup>
    </CommandList>
</Command>

and if you're using Popover/Dialog:

<Popover open={open} onOpenChange={setOpen}>
<Dialog open={open} onOpenChange={setOpen}>