|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { zodResolver } from '@hookform/resolvers/zod' |
| 4 | +import { useForm } from 'react-hook-form' |
| 5 | +import { z } from 'zod' |
| 6 | +import { |
| 7 | + Form, |
| 8 | + // FormDescription, |
| 9 | + FormField, |
| 10 | + FormItem, |
| 11 | + FormLabel, |
| 12 | + FormMessage, |
| 13 | +} from '@/components/ui/form' |
| 14 | +import { Input } from '../ui/input' |
| 15 | +import { Textarea } from '../ui/textarea' |
| 16 | +import { Button } from '../ui/button' |
| 17 | +import { Library } from '@/lib/types/library' |
| 18 | + |
| 19 | +export type LibraryFormValues = Pick< |
| 20 | + Library, |
| 21 | + 'name' | 'logo' | 'address' | 'phone' | 'email' | 'description' |
| 22 | +> |
| 23 | + |
| 24 | +type LibraryFormProps = { |
| 25 | + initialData: LibraryFormValues |
| 26 | + onSubmit(data: LibraryFormValues): void |
| 27 | +} |
| 28 | + |
| 29 | +const FormSchema = z.object({ |
| 30 | + name: z.string().nonempty({ message: 'Name is required' }), |
| 31 | + logo: z.string().optional(), |
| 32 | + address: z.string().optional(), |
| 33 | + phone: z.string().optional(), |
| 34 | + email: z.string().optional(), |
| 35 | + description: z.string().optional(), |
| 36 | +}) |
| 37 | + |
| 38 | +export const LibraryForm: React.FC<LibraryFormProps> = ({ |
| 39 | + initialData, |
| 40 | + onSubmit, |
| 41 | +}) => { |
| 42 | + const form = useForm<z.infer<typeof FormSchema>>({ |
| 43 | + resolver: zodResolver(FormSchema), |
| 44 | + defaultValues: initialData, |
| 45 | + }) |
| 46 | + |
| 47 | + return ( |
| 48 | + <Form {...form}> |
| 49 | + <form onSubmit={form.handleSubmit(onSubmit)}> |
| 50 | + <div className="mx-auto max-w-sm space-y-6 md:space-y-0 md:grid md:grid-cols-2 md:gap-2"> |
| 51 | + <FormField |
| 52 | + control={form.control} |
| 53 | + name="name" |
| 54 | + render={({ field }) => ( |
| 55 | + <FormItem className="flex flex-col"> |
| 56 | + <FormLabel>Name</FormLabel> |
| 57 | + <Input placeholder="Name" {...field} /> |
| 58 | + <FormMessage /> |
| 59 | + </FormItem> |
| 60 | + )} |
| 61 | + /> |
| 62 | + |
| 63 | + <FormField |
| 64 | + control={form.control} |
| 65 | + name="logo" |
| 66 | + render={({ field }) => ( |
| 67 | + <FormItem className="flex flex-col"> |
| 68 | + <FormLabel>Logo</FormLabel> |
| 69 | + <Input placeholder="Logo URL" {...field} /> |
| 70 | + <FormMessage /> |
| 71 | + </FormItem> |
| 72 | + )} |
| 73 | + /> |
| 74 | + |
| 75 | + <FormField |
| 76 | + control={form.control} |
| 77 | + name="address" |
| 78 | + render={({ field }) => ( |
| 79 | + <FormItem className="flex flex-col col-span-2"> |
| 80 | + <FormLabel>Address</FormLabel> |
| 81 | + <Input placeholder="Address" {...field} /> |
| 82 | + <FormMessage /> |
| 83 | + </FormItem> |
| 84 | + )} |
| 85 | + /> |
| 86 | + |
| 87 | + <FormField |
| 88 | + control={form.control} |
| 89 | + name="email" |
| 90 | + render={({ field }) => ( |
| 91 | + <FormItem className="flex flex-col"> |
| 92 | + <FormLabel>Email</FormLabel> |
| 93 | + <Input placeholder="Email" type="email" {...field} /> |
| 94 | + <FormMessage /> |
| 95 | + </FormItem> |
| 96 | + )} |
| 97 | + /> |
| 98 | + |
| 99 | + <FormField |
| 100 | + control={form.control} |
| 101 | + name="phone" |
| 102 | + render={({ field }) => ( |
| 103 | + <FormItem className="flex flex-col"> |
| 104 | + <FormLabel>Phone</FormLabel> |
| 105 | + <Input placeholder="Phone" type="tel" {...field} /> |
| 106 | + <FormMessage /> |
| 107 | + </FormItem> |
| 108 | + )} |
| 109 | + /> |
| 110 | + |
| 111 | + <FormField |
| 112 | + control={form.control} |
| 113 | + name="description" |
| 114 | + render={({ field }) => ( |
| 115 | + <FormItem className="flex flex-col col-span-2"> |
| 116 | + <FormLabel>Description</FormLabel> |
| 117 | + <Textarea placeholder="About" {...field} rows={3} /> |
| 118 | + <FormMessage /> |
| 119 | + </FormItem> |
| 120 | + )} |
| 121 | + /> |
| 122 | + |
| 123 | + <Button |
| 124 | + type="submit" |
| 125 | + className="w-full md:w-auto col-span-2 place-self-end" |
| 126 | + disabled={form.formState.isSubmitting || !form.formState.isDirty} |
| 127 | + > |
| 128 | + Submit |
| 129 | + </Button> |
| 130 | + </div> |
| 131 | + </form> |
| 132 | + </Form> |
| 133 | + ) |
| 134 | +} |
0 commit comments