"use client"; import z from "zod"; import { Input } from "./ui/input"; import { useTranslations } from "use-intl"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "./ui/form"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select"; import { LABEL_COLORS } from "./labels-selector"; const labelFormSchema = z.object({ name: z.string().nonempty(), color: z .string() .regex(/^#?([0-9a-f]{6}|[0-9a-f]{3})$/i) .nonempty() }); export type LabelFormData = z.infer; export type OrgLabelFormProps = { onSubmit: (data: LabelFormData) => void; defaultValue?: LabelFormData; }; export function OrgLabelForm({ onSubmit, defaultValue }: OrgLabelFormProps) { const t = useTranslations(); const colorValues = Object.values(LABEL_COLORS); const randomColor = colorValues[Math.floor(Math.random() * colorValues.length)]; const form = useForm({ resolver: zodResolver(labelFormSchema), defaultValues: { name: defaultValue?.name ?? "", color: defaultValue?.color ?? randomColor } }); return (
{ if (await form.trigger()) { onSubmit(form.getValues()); } }} > ( {t("labelNameField")} )} /> ( {t("labelColorField")} )} /> ); }