From 2e128eec912c0d37c775e393f85f4d2c62dc4057 Mon Sep 17 00:00:00 2001 From: Alexandre Garbe Date: Tue, 12 Nov 2024 16:23:06 +0100 Subject: [PATCH] feat(admin): added first version of CompetenceBlocForm for certification v2 --- .../[certificationCompetenceBlocId]/page.tsx | 7 ++ .../_components/CompetenceBlocForm.tsx | 85 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 packages/reva-admin-react/src/app/(admin)/certifications-v2/[certificationId]/bloc-competence/_components/CompetenceBlocForm.tsx diff --git a/packages/reva-admin-react/src/app/(admin)/certifications-v2/[certificationId]/bloc-competence/[certificationCompetenceBlocId]/page.tsx b/packages/reva-admin-react/src/app/(admin)/certifications-v2/[certificationId]/bloc-competence/[certificationCompetenceBlocId]/page.tsx index 78f5873d2..cb5ff4c3e 100644 --- a/packages/reva-admin-react/src/app/(admin)/certifications-v2/[certificationId]/bloc-competence/[certificationCompetenceBlocId]/page.tsx +++ b/packages/reva-admin-react/src/app/(admin)/certifications-v2/[certificationId]/bloc-competence/[certificationCompetenceBlocId]/page.tsx @@ -4,6 +4,7 @@ import { useUpdateCompetenceBlocPage } from "./updateCompetenceBloc.hook"; import { FormOptionalFieldsDisclaimer } from "@/components/form-optional-fields-disclaimer/FormOptionalFieldsDisclaimer"; import { GrayCard } from "@/components/card/gray-card/GrayCard"; import { Breadcrumb } from "@codegouvfr/react-dsfr/Breadcrumb"; +import { CompetenceBlocForm } from "../_components/CompetenceBlocForm"; type CertificationCompetenceBlocForPage = Exclude< ReturnType["competenceBloc"], @@ -68,5 +69,11 @@ const PageContent = ({
{competenceBloc.FCCompetences}
+ console.log({ f })} + /> ); diff --git a/packages/reva-admin-react/src/app/(admin)/certifications-v2/[certificationId]/bloc-competence/_components/CompetenceBlocForm.tsx b/packages/reva-admin-react/src/app/(admin)/certifications-v2/[certificationId]/bloc-competence/_components/CompetenceBlocForm.tsx new file mode 100644 index 000000000..c537b289d --- /dev/null +++ b/packages/reva-admin-react/src/app/(admin)/certifications-v2/[certificationId]/bloc-competence/_components/CompetenceBlocForm.tsx @@ -0,0 +1,85 @@ +import { FormButtons } from "@/components/form/form-footer/FormButtons"; +import { Input } from "@codegouvfr/react-dsfr/Input"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useFieldArray, useForm } from "react-hook-form"; +import { z } from "zod"; + +export const competenceBlocFormSchema = z.object({ + label: z + .string() + .min(2, "Ce champ doit contenir au moins 2 caractères") + .default(""), + competences: z + .object({ + id: z.string().optional(), + label: z + .string() + .min(2, "Ce champ doit contenir au moins 2 caractères") + .default(""), + }) + .array(), +}); + +export type CompetenceBlocFormData = z.infer; + +export const CompetenceBlocForm = ({ + onSubmit, + defaultValues, + backUrl, + className = "", +}: { + onSubmit(data: CompetenceBlocFormData): Promise; + defaultValues: Partial; + backUrl: string; + className?: string; +}) => { + const methods = useForm({ + resolver: zodResolver(competenceBlocFormSchema), + defaultValues, + }); + + const { + register, + control, + handleSubmit, + formState: { isDirty, isSubmitting, errors }, + } = methods; + + const { fields: competencesFields } = useFieldArray({ + control, // control props comes from useForm (optional: if you are using FormProvider) + name: "competences", // unique name for your Field Array + }); + + const handleFormSubmit = handleSubmit(onSubmit, (e) => console.log(e)); + return ( +
+ +
+ {competencesFields.map((c, cIndex) => ( + + ))} +
+ + + ); +};