-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin): added first version of CompetenceBlocForm for certificat…
…ion v2
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...n)/certifications-v2/[certificationId]/bloc-competence/_components/CompetenceBlocForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<typeof competenceBlocFormSchema>; | ||
|
||
export const CompetenceBlocForm = ({ | ||
onSubmit, | ||
defaultValues, | ||
backUrl, | ||
className = "", | ||
}: { | ||
onSubmit(data: CompetenceBlocFormData): Promise<void>; | ||
defaultValues: Partial<CompetenceBlocFormData>; | ||
backUrl: string; | ||
className?: string; | ||
}) => { | ||
const methods = useForm<CompetenceBlocFormData>({ | ||
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 ( | ||
<form onSubmit={handleFormSubmit} className={`${className}`}> | ||
<Input | ||
label="Intitulé du bloc de compétences" | ||
state={errors.label ? "error" : "default"} | ||
stateRelatedMessage={errors.label?.message?.toString()} | ||
nativeInputProps={{ | ||
...register("label"), | ||
}} | ||
/> | ||
<div className="flex flex-col gap-2 mt-6 pl-4"> | ||
{competencesFields.map((c, cIndex) => ( | ||
<Input | ||
className="mb-0" | ||
label="" | ||
state={errors.label ? "error" : "default"} | ||
stateRelatedMessage={errors.label?.message?.toString()} | ||
key={c.id} | ||
nativeInputProps={{ ...register(`competences.${cIndex}.label`) }} | ||
/> | ||
))} | ||
</div> | ||
<FormButtons | ||
backUrl={backUrl} | ||
formState={{ | ||
isDirty, | ||
isSubmitting, | ||
}} | ||
/> | ||
</form> | ||
); | ||
}; |