|
| 1 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 2 | +import { Meta } from '@storybook/react-vite'; |
| 3 | +import { CheckIcon } from 'lucide-react'; |
| 4 | +import { useForm } from 'react-hook-form'; |
| 5 | +import { z } from 'zod'; |
| 6 | + |
| 7 | +import { cn } from '@/lib/tailwind/utils'; |
| 8 | + |
| 9 | +import { |
| 10 | + Form, |
| 11 | + FormField, |
| 12 | + FormFieldController, |
| 13 | + FormFieldHelper, |
| 14 | +} from '@/components/form'; |
| 15 | +import { onSubmit } from '@/components/form/docs.utils'; |
| 16 | +import { FieldCheckbox } from '@/components/form/field-checkbox'; |
| 17 | +import { Button } from '@/components/ui/button'; |
| 18 | + |
| 19 | +export default { |
| 20 | + title: 'Form/FieldCheckboxGroup', |
| 21 | + component: FieldCheckbox, |
| 22 | +} satisfies Meta<typeof FieldCheckbox>; |
| 23 | + |
| 24 | +const zFormSchema = () => |
| 25 | + z.object({ |
| 26 | + lovesBears: z.boolean().refine((val) => val === true, { |
| 27 | + message: 'Please say you love bears.', |
| 28 | + }), |
| 29 | + }); |
| 30 | + |
| 31 | +const formOptions = { |
| 32 | + mode: 'onBlur', |
| 33 | + resolver: zodResolver(zFormSchema()), |
| 34 | + defaultValues: { |
| 35 | + lovesBears: false, |
| 36 | + }, |
| 37 | +} as const; |
| 38 | + |
| 39 | +export const Default = () => { |
| 40 | + const form = useForm(formOptions); |
| 41 | + |
| 42 | + return ( |
| 43 | + <Form {...form} onSubmit={onSubmit}> |
| 44 | + <div className="flex flex-col gap-4"> |
| 45 | + <FormField> |
| 46 | + <FormFieldController |
| 47 | + type="checkbox" |
| 48 | + control={form.control} |
| 49 | + name="lovesBears" |
| 50 | + > |
| 51 | + I love bears |
| 52 | + </FormFieldController> |
| 53 | + <FormFieldHelper>There is only one possible answer.</FormFieldHelper> |
| 54 | + </FormField> |
| 55 | + <div> |
| 56 | + <Button type="submit">Submit</Button> |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + </Form> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +export const DefaultValue = () => { |
| 64 | + const form = useForm({ |
| 65 | + ...formOptions, |
| 66 | + defaultValues: { |
| 67 | + lovesBears: true, |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + return ( |
| 72 | + <Form {...form} onSubmit={onSubmit}> |
| 73 | + <div className="flex flex-col gap-4"> |
| 74 | + <FormField> |
| 75 | + <FormFieldController |
| 76 | + type="checkbox" |
| 77 | + control={form.control} |
| 78 | + name="lovesBears" |
| 79 | + > |
| 80 | + I love bears |
| 81 | + </FormFieldController> |
| 82 | + <FormFieldHelper>There is only one possible answer.</FormFieldHelper> |
| 83 | + </FormField> |
| 84 | + <div> |
| 85 | + <Button type="submit">Submit</Button> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + </Form> |
| 89 | + ); |
| 90 | +}; |
| 91 | + |
| 92 | +export const Disabled = () => { |
| 93 | + const form = useForm({ |
| 94 | + ...formOptions, |
| 95 | + defaultValues: { |
| 96 | + lovesBears: true, |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + return ( |
| 101 | + <Form {...form} onSubmit={onSubmit}> |
| 102 | + <div className="flex flex-col gap-4"> |
| 103 | + <FormField> |
| 104 | + <FormFieldController |
| 105 | + type="checkbox" |
| 106 | + control={form.control} |
| 107 | + name="lovesBears" |
| 108 | + disabled |
| 109 | + > |
| 110 | + I love bears |
| 111 | + </FormFieldController> |
| 112 | + <FormFieldHelper>There is only one possible answer.</FormFieldHelper> |
| 113 | + </FormField> |
| 114 | + <div> |
| 115 | + <Button type="submit">Submit</Button> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + </Form> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +export const CustomCheckbox = () => { |
| 123 | + const form = useForm(formOptions); |
| 124 | + |
| 125 | + return ( |
| 126 | + <Form {...form} onSubmit={onSubmit}> |
| 127 | + <div className="flex flex-col gap-4"> |
| 128 | + <FormField> |
| 129 | + <FormFieldController |
| 130 | + type="checkbox" |
| 131 | + name="lovesBears" |
| 132 | + control={form.control} |
| 133 | + labelProps={{ |
| 134 | + className: |
| 135 | + 'relative flex cursor-pointer items-center justify-between gap-4 rounded-lg border border-border p-4 transition-colors outline-none focus-within:ring-[3px] focus-within:ring-ring/50 hover:bg-muted/50 has-[&[data-checked]]:bg-primary/5', |
| 136 | + }} |
| 137 | + render={(props, { checked }) => { |
| 138 | + return ( |
| 139 | + <div |
| 140 | + {...props} |
| 141 | + className="flex w-full items-center justify-between outline-none" |
| 142 | + > |
| 143 | + <div className="flex flex-col"> |
| 144 | + <span className="font-medium">I love bears !</span> |
| 145 | + <FormFieldHelper> |
| 146 | + There is only one possible answer. |
| 147 | + </FormFieldHelper> |
| 148 | + </div> |
| 149 | + <div |
| 150 | + className={cn( |
| 151 | + 'aspect-square rounded-full bg-primary p-1 opacity-0', |
| 152 | + { |
| 153 | + 'opacity-100': checked, |
| 154 | + } |
| 155 | + )} |
| 156 | + > |
| 157 | + <CheckIcon className="h-4 w-4 text-primary-foreground" /> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + ); |
| 161 | + }} |
| 162 | + /> |
| 163 | + </FormField> |
| 164 | + <div> |
| 165 | + <Button type="submit">Submit</Button> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + </Form> |
| 169 | + ); |
| 170 | +}; |
0 commit comments