-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0aebfca
commit 21735d3
Showing
4 changed files
with
283 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
"use client"; | ||
|
||
import * as React from "react"; | ||
|
||
import { cn } from "."; | ||
import { Badge } from "./badge"; | ||
import { Button } from "./button"; | ||
import { | ||
Command, | ||
CommandEmpty, | ||
CommandGroup, | ||
CommandInput, | ||
CommandItem, | ||
CommandSeparator, | ||
} from "./command"; | ||
import { Icons } from "./icons"; | ||
import { Input } from "./input"; | ||
import { Popover, PopoverContent, PopoverTrigger } from "./popover"; | ||
|
||
export type OptionType = { | ||
label: string; | ||
value: string; | ||
}; | ||
|
||
interface MultiSelectProps { | ||
options: OptionType[]; | ||
selected: string[]; | ||
onChange: React.Dispatch<React.SetStateAction<string[]>>; | ||
className?: string; | ||
} | ||
|
||
function MultiSelect({ | ||
options, | ||
selected, | ||
onChange, | ||
className, | ||
...props | ||
}: MultiSelectProps) { | ||
const [open, setOpen] = React.useState(false); | ||
|
||
const handleUnselect = (item: string) => { | ||
onChange(selected.filter((i) => i !== item)); | ||
}; | ||
|
||
const [newOption, setNewOption] = React.useState(""); | ||
|
||
const handleNewOptionEntry = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setNewOption(e.target.value); | ||
}; | ||
|
||
const handleNewOptionSubmit = () => { | ||
if (newOption) { | ||
options.push({ label: newOption, value: newOption }); | ||
onChange( | ||
selected.includes(newOption) | ||
? selected.filter((item) => item !== newOption) | ||
: [...selected, newOption], | ||
); | ||
setNewOption(""); | ||
setOpen(true); | ||
} | ||
}; | ||
|
||
return ( | ||
<Popover open={open} onOpenChange={setOpen} {...props}> | ||
<PopoverTrigger asChild> | ||
<Button | ||
variant="outline" | ||
role="combobox" | ||
aria-expanded={open} | ||
className={`w-full justify-between ${ | ||
selected.length > 1 ? "h-full" : "h-10" | ||
}`} | ||
onClick={() => setOpen(!open)} | ||
> | ||
<div className="flex flex-wrap gap-1"> | ||
{selected.map((item) => ( | ||
<Badge | ||
variant="secondary" | ||
key={item} | ||
className="mb-1 mr-1" | ||
onClick={() => handleUnselect(item)} | ||
> | ||
{item} | ||
<button | ||
className="ml-1 rounded-full outline-none ring-offset-background focus:ring-2 focus:ring-ring focus:ring-offset-2" | ||
onKeyDown={(e) => { | ||
if (e.key === "Enter") { | ||
handleUnselect(item); | ||
} | ||
}} | ||
onMouseDown={(e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
}} | ||
onClick={() => handleUnselect(item)} | ||
> | ||
<Icons.X className="h-3 w-3 text-muted-foreground hover:text-foreground" /> | ||
</button> | ||
</Badge> | ||
))} | ||
</div> | ||
<Icons.ChevronsUpDown className="h-4 w-4 shrink-0 opacity-50" /> | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent className="w-full p-0"> | ||
<Command className={className}> | ||
<CommandInput placeholder="Search ..." /> | ||
<CommandEmpty>No item found.</CommandEmpty> | ||
<CommandGroup className="max-h-64 overflow-auto"> | ||
{options.map((option) => ( | ||
<CommandItem | ||
key={option.value} | ||
onSelect={() => { | ||
onChange( | ||
selected.includes(option.value) | ||
? selected.filter((item) => item !== option.value) | ||
: [...selected, option.value], | ||
); | ||
setOpen(true); | ||
}} | ||
> | ||
<Icons.Check | ||
className={cn( | ||
"mr-2 h-4 w-4", | ||
selected.includes(option.value) | ||
? "opacity-100" | ||
: "opacity-0", | ||
)} | ||
/> | ||
{option.label} | ||
</CommandItem> | ||
))} | ||
</CommandGroup> | ||
<CommandSeparator /> | ||
<CommandGroup> | ||
<div className="flex gap-1"> | ||
<Input | ||
placeholder="other tags" | ||
value={newOption} | ||
onChange={handleNewOptionEntry} | ||
/> | ||
<Button variant="ghost" onClick={handleNewOptionSubmit}> | ||
<Icons.PlusCircleIcon /> | ||
</Button> | ||
</div> | ||
</CommandGroup> | ||
</Command> | ||
</PopoverContent> | ||
</Popover> | ||
); | ||
} | ||
|
||
export { MultiSelect }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters