41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
import React from "react";
|
|
import { CreateFormProps } from "@/validations/mutual/forms/type";
|
|
import { useForm } from "react-hook-form";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { Form } from "@/components/mutual/shadcnui/form";
|
|
import { renderInputsBySchema } from "@/lib/renderInputs";
|
|
import { Button } from "@/components/mutual/shadcnui/button";
|
|
|
|
const CreateForm: React.FC<CreateFormProps> = ({ schemas, labels, selectedRow }) => {
|
|
const createSchema = schemas.create
|
|
const findLabels = Object.entries(createSchema?.shape || {}).reduce((acc: any, [key, value]: any) => {
|
|
acc[key] = {
|
|
label: labels[key] || key,
|
|
description: value.description,
|
|
}
|
|
return acc
|
|
}, {})
|
|
const handleSubmit = (data: any) => {
|
|
console.log(data)
|
|
}
|
|
const form = useForm<FormData>({
|
|
resolver: zodResolver(createSchema),
|
|
defaultValues: {},
|
|
});
|
|
|
|
return (
|
|
<div>
|
|
<div>Create Form</div>
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-4">
|
|
{renderInputsBySchema(findLabels, form)}
|
|
<Button type="submit">Submit</Button>
|
|
</form>
|
|
</Form>
|
|
<div>selectedRow: {JSON.stringify(selectedRow)}</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default CreateForm
|