Skip to content

Commit

Permalink
fix: not show error message when reset
Browse files Browse the repository at this point in the history
  • Loading branch information
João Pedro Magalhães committed Apr 6, 2023
1 parent 66350dc commit 561b53a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
38 changes: 38 additions & 0 deletions lib/form/form.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1664,3 1664,41 @@ test("Form `deleteField` should remove field", async () => {
rerender(<Comp />);
expect(queryByText("emailHere")).not.toBeInTheDocument();
});

test("Form should not trigger validation when reset", async () => {
const Comp = () => {
return (
<Form>
{({ reset }) => (
<div>
<button onClick={reset}>Reset</button>
<Field<string>
name="email"
onChangeValidate={z.string().min(1, "email error")}
>
{({ value, setValue, errors }) => (
<>
<input
placeholder="Email"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
{errors.map((error) => (
<p key={error}>{error}</p>
))}
</>
)}
</Field>
</div>
)}
</Form>
);
};

const { getByText, getByPlaceholderText, queryByText } = render(<Comp />);

await user.type(getByPlaceholderText("Email"), "emailHere");
await user.click(getByText("Reset"));

expect(queryByText("email error")).not.toBeInTheDocument();
});
3 changes: 2 additions & 1 deletion lib/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 253,8 @@ function FormComp<T extends Record<string, any> = Record<string, any>>(
return !(v as FieldArrayInstance).setValues;
};

field.setErrors([]);
// We delay reset errors to ensure don't show an error message
setTimeout(() => field.setErrors([]));
field.setIsTouched(false);
field.setIsDirty(false);
if (isFieldArray(field)) {
Expand Down

0 comments on commit 561b53a

Please sign in to comment.