import { zodResolver } from '@hookform/resolvers/zod'; import { Icon } from '@iconify/react'; import { Button } from '@workspace/ui/components/button'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@workspace/ui/components/form'; import { Input } from '@workspace/ui/components/input'; import { ScrollArea } from '@workspace/ui/components/scroll-area'; import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, } from '@workspace/ui/components/sheet'; import { MarkdownEditor } from '@workspace/ui/custom-components/editor'; import { useTranslations } from 'next-intl'; import { useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; const formSchema = z.object({ title: z.string(), content: z.string().optional(), }); interface AnnouncementFormProps { onSubmit: (data: T) => Promise | boolean; initialValues?: T; loading?: boolean; trigger: string; title: string; } export default function AnnouncementForm>({ onSubmit, initialValues, loading, trigger, title, }: AnnouncementFormProps) { const t = useTranslations('announcement'); const [open, setOpen] = useState(false); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { title: '', content: '', ...initialValues, }, }); useEffect(() => { form?.reset(initialValues); }, [form, initialValues]); async function handleSubmit(data: { [x: string]: any }) { const bool = await onSubmit(data as T); if (bool) setOpen(false); } return ( {title}
( {t('form.title')} )} /> ( {t('form.content')} { form.setValue(field.name, value || ''); }} /> )} />
); }