'use client' import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import * as z from "zod" import { Button } from "@/registry/new-york/ui/button" import { Input } from "@/registry/new-york/ui/input" import { Switch } from "@/registry/new-york/ui/switch" import { Textarea } from "@/registry/new-york/ui/textarea" import { IconSelector } from './IconSelector' import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, FormDescription, } from "@/registry/new-york/ui/form" import { useToast } from "@/registry/new-york/hooks/use-toast" const formSchema = z.object({ title: z.string().min(2, { message: "标题至少需要2个字符" }), icon: z.string().min(1, { message: "请选择图标" }), description: z.string().optional(), enabled: z.boolean().default(true) }) interface AddNavigationFormProps { onSubmit: (values: { title: string; icon: string; description?: string; enabled: boolean; }) => void defaultValues?: { title: string icon: string description?: string enabled: boolean } onCancel?: () => void } export function AddNavigationForm({ onSubmit, defaultValues, onCancel }: AddNavigationFormProps) { const { toast } = useToast() const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: defaultValues || { title: "", icon: "FolderKanban", description: "", enabled: true } }) const { isSubmitting } = form.formState const onSubmitHandler = async (values: z.infer) => { try { await onSubmit({ title: values.title, icon: values.icon, description: values.description, enabled: values.enabled }) toast({ title: defaultValues ? "更新成功" : "添加成功", description: `导航项 "${values.title}" 已${defaultValues ? "更新" : "添加"}`, }) } catch (error) { toast({ title: "操作失败", description: "请稍后重试", variant: "destructive", }) } } return (
( 标题 )} /> ( 图标 从 Lucide 图标库中选择一个图标 )} /> ( 描述