fix: multioption combobox
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { Check } from "lucide-react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Command,
|
||||
CommandEmpty,
|
||||
CommandGroup,
|
||||
CommandInput,
|
||||
CommandItem,
|
||||
CommandList,
|
||||
} from "@/components/ui/command"
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from "@/components/ui/popover"
|
||||
import ArrowDownIcon from "../icons/ArrowDownIcon"
|
||||
|
||||
export interface DropdownOption {
|
||||
id: string;
|
||||
label?: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
type Props = {
|
||||
title: string;
|
||||
options: Array<DropdownOption>;
|
||||
value: string;
|
||||
setValue: React.Dispatch<React.SetStateAction<string>>
|
||||
} & React.HTMLAttributes<HTMLDivElement>
|
||||
|
||||
export function Combobox({ options, value, title, setValue, }: Props) {
|
||||
const [open, setOpen] = React.useState(false)
|
||||
|
||||
return (
|
||||
<Popover open={open} onOpenChange={setOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
role="combobox"
|
||||
aria-expanded={open}
|
||||
className="flex font-normal w-full h-11 items-center justify-between gap-2 px-3 py-4 relative bg-white/29 rounded-xl border border-solid border-neutral-200 cursor-pointer focus:outline-none focus:ring-2 focus:ring-black"
|
||||
>
|
||||
{value
|
||||
? options.find((option) => option.title === value)?.label
|
||||
: "انتخاب کنید"}
|
||||
<ArrowDownIcon />
|
||||
<label
|
||||
htmlFor='content-select'
|
||||
className='pointer-events-none inline-flex flex-col items-end justify-center px-1 py-0 absolute -top-6 right-0 z-[2]'>
|
||||
<span className='relative w-fit mt-[-1px] text-xs tracking-[0] leading-4 whitespace-nowrap'>
|
||||
{title}
|
||||
</span>
|
||||
</label>
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-full p-0">
|
||||
<Command>
|
||||
<CommandInput placeholder="جستجو" className="h-9" />
|
||||
<CommandList>
|
||||
<CommandEmpty>نتیجه ای یافت نشد</CommandEmpty>
|
||||
<CommandGroup>
|
||||
{options.map((option) => (
|
||||
<CommandItem
|
||||
key={option.id}
|
||||
value={option.title}
|
||||
onSelect={(currentValue) => {
|
||||
setValue(currentValue === value ? "" : currentValue)
|
||||
setOpen(false)
|
||||
}}
|
||||
>
|
||||
<Check
|
||||
className={cn(
|
||||
"",
|
||||
value === option.title ? "opacity-100" : "opacity-0"
|
||||
)}
|
||||
/>
|
||||
{option.label}
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
</CommandList>
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user