import { Controller, useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { Button } from '@egose/shadcn-theme/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@egose/shadcn-theme/components/ui/card'; import { Checkbox } from '@egose/shadcn-theme/components/ui/checkbox'; import { Input } from '@egose/shadcn-theme/components/ui/input'; import { Label } from '@egose/shadcn-theme/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@egose/shadcn-theme/components/ui/select'; import { todoFormSchema } from '../shared/entity-schemas'; import type { Category, TodoFormInput } from '../types'; export type TodoFormValues = TodoFormInput; interface TodoFormProps { categories: Category[]; disabled?: boolean; initialValues?: Partial & { _id?: string }; submitLabel: string; onSubmit: (values: TodoFormValues) => Promise; onCancel?: () => void; } const formFieldClass = 'grid gap-2'; const errorTextClass = 'text-sm text-red-500'; const descriptionTextClass = 'text-sm text-muted-foreground'; const NONE_CATEGORY = '__none__'; const titleDescriptionId = 'todo-title-description'; const titleErrorId = 'todo-title-error'; const categoryDescriptionId = 'todo-category-description'; export function TodoForm({ categories, disabled = false, initialValues, submitLabel, onSubmit, onCancel, }: TodoFormProps) { const { register, handleSubmit, control, reset, formState: { errors, isSubmitting }, } = useForm({ resolver: zodResolver(todoFormSchema), defaultValues: { title: '', categoryId: '', completed: false, ...initialValues }, }); const titleRegistration = register('title'); return ( {initialValues?._id ? 'Edit todo' : 'New todo'}
{ const completed = await onSubmit(values); if (completed && !initialValues?._id) reset(); })} >
Enter 1 to 200 characters. {errors.title && ( {errors.title.message} )}
( )} /> Optional category for grouping this todo.
( field.onChange(checked === true)} /> )} />
{onCancel && ( )}
); }