Skip to content

Commit

Permalink
fix: click on radio button must trigger validation
Browse files Browse the repository at this point in the history
  • Loading branch information
pom421 committed Nov 21, 2023
1 parent 858eda4 commit ae7c7a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 13 additions & 2 deletions packages/app/src/components/RHF/PopulationFavorable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ export const PopulationFavorable = ({ legend, disabled }: Props) => {
const {
register,
formState: { errors },
trigger,
} = useFormContext();

const fieldMethods = register("populationFavorable");

return (
<RadioButtons
legend={legend || "Population envers laquelle l'écart est favorable"}
Expand All @@ -23,14 +26,22 @@ export const PopulationFavorable = ({ legend, disabled }: Props) => {
label: FavorablePopulation.Label[FavorablePopulation.Enum.WOMEN],
nativeInputProps: {
value: FavorablePopulation.Enum.WOMEN,
...register("populationFavorable"),
...fieldMethods,
onChange: e => {
trigger(); // Rerun validation to set isValid.
fieldMethods.onChange(e); // Inform RHF to update its state.
},
},
},
{
label: FavorablePopulation.Label[FavorablePopulation.Enum.MEN],
nativeInputProps: {
value: FavorablePopulation.Enum.MEN,
...register("populationFavorable"),
...fieldMethods,
onChange: e => {
trigger(); // Rerun validation to set isValid.
fieldMethods.onChange(e); // Inform RHF to update its state.
},
},
},
]}
Expand Down
14 changes: 12 additions & 2 deletions packages/app/src/components/RHF/RadioOuiNon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ type Props = {
};

export const RadioOuiNon = ({ legend, name, disabled }: PropsWithChildren<Props>) => {
const { register } = useFormContext();
const { register, trigger } = useFormContext();

disabled = disabled || false;

const fieldMethods = register(name);

return (
<RadioButtons
legend={legend}
Expand All @@ -22,14 +24,22 @@ export const RadioOuiNon = ({ legend, name, disabled }: PropsWithChildren<Props>
label: "Oui",
nativeInputProps: {
value: "oui",
...register(name),
...fieldMethods,
onChange: e => {
trigger(); // Rerun validation to set isValid.
fieldMethods.onChange(e); // Inform RHF to update its state.
},
},
},
{
label: "Non",
nativeInputProps: {
value: "non",
...register(name),
onChange: e => {
trigger(); // Rerun validation to set isValid.
fieldMethods.onChange(e); // Inform RHF to update its state.
},
},
},
]}
Expand Down

0 comments on commit ae7c7a6

Please sign in to comment.