'use client'; import { zodResolver } from '@hookform/resolvers/zod'; import type { {{pascalCase name}}Response } from '@seclusion/shared'; import { useEffect } from 'react'; import { useForm } from 'react-hook-form'; import { toast } from 'sonner'; import { z } from 'zod'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; {{#if hasTextarea}} import { Textarea } from '@/components/ui/textarea'; {{/if}} {{#if hasSelect}} import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; {{/if}} {{#if hasSwitch}} import { Switch } from '@/components/ui/switch'; {{/if}} import { useUpdate{{pascalCase name}} } from '@/hooks/use{{pascalCase pluralName}}'; const edit{{pascalCase name}}Schema = z.object({ {{#each updateFields}} {{name}}: {{{zodValidation this}}}.optional(), {{/each}} }); type Edit{{pascalCase name}}FormValues = z.infer; interface {{pascalCase name}}EditDialogProps { {{camelCase name}}: {{pascalCase name}}Response | null; open: boolean; onOpenChange: (open: boolean) => void; } export function {{pascalCase name}}EditDialog({ {{camelCase name}}, open, onOpenChange, }: {{pascalCase name}}EditDialogProps) { const update{{pascalCase name}} = useUpdate{{pascalCase name}}(); const form = useForm({ resolver: zodResolver(edit{{pascalCase name}}Schema), defaultValues: { {{#each updateFields}} {{name}}: {{{defaultValue this}}}, {{/each}} }, }); // 当数据变化时重置表单 useEffect(() => { if ({{camelCase name}}) { form.reset({ {{#each updateFields}} {{name}}: {{camelCase ../name}}.{{name}}{{#if nullable}} ?? {{{defaultValue this}}}{{/if}}, {{/each}} }); } }, [{{camelCase name}}, form]); const onSubmit = async (values: Edit{{pascalCase name}}FormValues) => { if (!{{camelCase name}}) return; try { await update{{pascalCase name}}.mutateAsync({ id: {{camelCase name}}.id, data: values, }); toast.success('{{chineseName}}已更新'); onOpenChange(false); } catch (error) { toast.error(error instanceof Error ? error.message : '更新失败'); } }; return ( 编辑{{chineseName}} 修改{{chineseName}}信息
{{#each updateFields}} ( {{label}} {{{formControl this}}} )} /> {{/each}}

ID:{' '} {{{camelCase name}}?.id}

); }