abstract genre checkbox components

This commit is contained in:
andrzej 2024-06-16 23:08:17 +02:00
parent 7a7cd39f6e
commit a650bd7183
4 changed files with 81 additions and 40 deletions

View File

@ -819,6 +819,11 @@ body {
width: 100%; width: 100%;
} }
.w-fit {
width: -moz-fit-content;
width: fit-content;
}
.min-w-\[8rem\] { .min-w-\[8rem\] {
min-width: 8rem; min-width: 8rem;
} }
@ -827,10 +832,24 @@ body {
min-width: var(--radix-select-trigger-width); min-width: var(--radix-select-trigger-width);
} }
.min-w-fit {
min-width: -moz-fit-content;
min-width: fit-content;
}
.max-w-sm { .max-w-sm {
max-width: 24rem; max-width: 24rem;
} }
.max-w-fit {
max-width: -moz-fit-content;
max-width: fit-content;
}
.max-w-screen-sm {
max-width: 640px;
}
.shrink-0 { .shrink-0 {
flex-shrink: 0; flex-shrink: 0;
} }

View File

@ -0,0 +1,30 @@
import { FormItem, FormControl, FormLabel } from "@/components/ui/form"
import { Checkbox } from "@/components/ui/checkbox"
export default function GenreCheckbox({ field, item }) {
return (
<FormItem
key={item.id}
className="flex flex-row items-start space-x-3 space-y-0"
>
<FormControl>
<Checkbox
checked={field.value?.includes(item.id)}
onCheckedChange={(checked) => {
return checked
? field.onChange([...field.value, item.id])
: field.onChange(
field.value?.filter(
(value) => value !== item.id
)
)
}}
/>
</FormControl>
<FormLabel className="text-sm font-normal">
{item.name}
</FormLabel>
</FormItem>
)
}

View File

@ -0,0 +1,27 @@
import { FormField, FormItem, FormLabel, FormControl } from "@/components/ui/form"
import { Popover, PopoverContent, PopoverTrigger } from "@radix-ui/react-popover"
import { Checkbox } from "@radix-ui/react-checkbox"
import { Button } from "@/components/ui/button"
import { cn } from "@/lib/utils"
export default function GenresTrigger({ value, genres }) {
return (
<>
<PopoverTrigger asChild>
<Button
variant={"outline"}
className={cn(
"min-w-fit max-w-screen-sm w-fit pl-3 text-left font-normal",
!value && "text-muted-foreground"
)}
>
{value.length !== 0 ? (
value.map((e, i) => genres.find(f => e === f.id).name +
(i < value.length - 1 ? ', ' : ''))
) : (
<FormLabel>Genres</FormLabel>
)}
</Button>
</PopoverTrigger>
</>
)
}

View File

@ -18,7 +18,6 @@ import { Input } from "@/components/ui/input"
import { Checkbox } from "@/components/ui/checkbox" import { Checkbox } from "@/components/ui/checkbox"
import { toast } from "@/components/ui/use-toast" import { toast } from "@/components/ui/use-toast"
import { import {
Popover, Popover,
PopoverContent, PopoverContent,
@ -26,6 +25,9 @@ import {
} from "@/components/ui/popover" } from "@/components/ui/popover"
import { cn } from "@/lib/utils" import { cn } from "@/lib/utils"
import { Badge } from "lucide-react" import { Badge } from "lucide-react"
import GenresTrigger from "./genresTrigger"
import GenresPopoverContent from "./genresPopoverContent"
import GenreCheckbox from "./genreCheckbox"
const formSchema = z.object({ const formSchema = z.object({
title: z.string().min(2).max(50), title: z.string().min(2).max(50),
@ -110,22 +112,7 @@ export default function StoryForm({ genres }) {
render={({ field }) => ( render={({ field }) => (
<FormItem className="flex flex-col"> <FormItem className="flex flex-col">
<Popover> <Popover>
<PopoverTrigger asChild> <GenresTrigger value={field.value} genres={genres} />
<Button
variant={"outline"}
className={cn(
"w-[240px] pl-3 text-left font-normal",
!field.value && "text-muted-foreground"
)}
>
{field.value.length !== 0 ? (
field.value.map((e,i)=>genres.find(f=>e===f.id).name+
( i<field.value.length-1?', ':'' ))
) : (
<FormLabel>Genres</FormLabel>
)}
</Button>
</PopoverTrigger>
<PopoverContent> <PopoverContent>
{genres.map((item) => ( {genres.map((item) => (
<FormField <FormField
@ -134,35 +121,13 @@ export default function StoryForm({ genres }) {
name="genres" name="genres"
render={({ field }) => { render={({ field }) => {
return ( return (
<FormItem <GenreCheckbox field={field} item={item} />
key={item.id}
className="flex flex-row items-start space-x-3 space-y-0"
>
<FormControl>
<Checkbox
checked={field.value?.includes(item.id)}
onCheckedChange={(checked) => {
return checked
? field.onChange([...field.value, item.id])
: field.onChange(
field.value?.filter(
(value) => value !== item.id
)
)
}}
/>
</FormControl>
<FormLabel className="text-sm font-normal">
{item.name}
</FormLabel>
</FormItem>
) )
}} }}
/> />
))} ))}
</PopoverContent> </PopoverContent>
</Popover> </Popover>
<FormMessage /> <FormMessage />
</FormItem> </FormItem>
)} )}