-
-
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
ff4cfeb
commit abe60ff
Showing
5 changed files
with
320 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,37 @@ | ||
import type { VariantProps } from "class-variance-authority"; | ||
import * as React from "react"; | ||
import { cva } from "class-variance-authority"; | ||
|
||
import { cn } from "."; | ||
|
||
const badgeVariants = cva( | ||
"inline-flex items-center rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2", | ||
{ | ||
variants: { | ||
variant: { | ||
default: | ||
"border-transparent bg-primary text-primary-foreground shadow hover:bg-primary/80", | ||
secondary: | ||
"border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80", | ||
destructive: | ||
"border-transparent bg-destructive text-destructive-foreground shadow hover:bg-destructive/80", | ||
outline: "text-foreground", | ||
}, | ||
}, | ||
defaultVariants: { | ||
variant: "default", | ||
}, | ||
}, | ||
); | ||
|
||
export interface BadgeProps | ||
extends React.HTMLAttributes<HTMLDivElement>, | ||
VariantProps<typeof badgeVariants> {} | ||
|
||
function Badge({ className, variant, ...props }: BadgeProps) { | ||
return ( | ||
<div className={cn(badgeVariants({ variant }), className)} {...props} /> | ||
); | ||
} | ||
|
||
export { Badge, badgeVariants }; |
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