mirror of
https://github.com/massbug/judge4c.git
synced 2025-05-18 23:56:33 +00:00
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
|
"use client";
|
||
|
|
||
|
import * as z from "zod";
|
||
|
import {
|
||
|
Form,
|
||
|
FormControl,
|
||
|
FormField,
|
||
|
FormItem,
|
||
|
FormLabel,
|
||
|
FormMessage,
|
||
|
} from "@/components/ui/form";
|
||
|
import { toast } from "sonner";
|
||
|
import { useForm } from "react-hook-form";
|
||
|
import { Input } from "@/components/ui/input";
|
||
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||
|
|
||
|
const formSchema = z.record(z.string().min(1));
|
||
|
|
||
|
interface TestcaseFormInterface {
|
||
|
[key: string]: string | undefined;
|
||
|
}
|
||
|
|
||
|
export default function TestcaseForm(props: TestcaseFormInterface) {
|
||
|
const form = useForm<z.infer<typeof formSchema>>({
|
||
|
resolver: zodResolver(formSchema),
|
||
|
defaultValues: props,
|
||
|
});
|
||
|
|
||
|
function onSubmit(values: z.infer<typeof formSchema>) {
|
||
|
try {
|
||
|
console.log(values);
|
||
|
toast.success(JSON.stringify(values, null, 2));
|
||
|
} catch (error) {
|
||
|
console.error("Form submission error", error);
|
||
|
toast.error("Failed to submit the form. Please try again.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return (
|
||
|
<Form {...form}>
|
||
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4" id="testcase-form">
|
||
|
{Object.keys(props).map((fieldName) => (
|
||
|
<FormField
|
||
|
key={fieldName}
|
||
|
control={form.control}
|
||
|
name={fieldName}
|
||
|
render={({ field }) => (
|
||
|
<FormItem>
|
||
|
<FormLabel>{`${fieldName} =`}</FormLabel>
|
||
|
<FormControl>
|
||
|
<Input
|
||
|
className="bg-muted border-transparent shadow-none rounded-lg h-10"
|
||
|
placeholder={`Enter ${fieldName}`}
|
||
|
type="text"
|
||
|
{...field}
|
||
|
/>
|
||
|
</FormControl>
|
||
|
<FormMessage />
|
||
|
</FormItem>
|
||
|
)}
|
||
|
/>
|
||
|
))}
|
||
|
</form>
|
||
|
</Form>
|
||
|
);
|
||
|
}
|