Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/Field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
if (!isDirty.value && value !== initialValue) {
isDirty.value = true;
}
if (manualErrors.value.length > 0) {
manualErrors.value = [];
}
},
onBlur: () => {
if (!isTouched.value) {
Expand Down
77 changes: 77 additions & 0 deletions src/components/__tests__/Form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,83 @@ describe('Form', () => {
expect(await screen.findByText(apiErrors.name[0])).toBeVisible();
expect(await screen.findByText(apiErrors.email[0])).toBeVisible();
});

it('should clear field error when editing that field after setErrors', async () => {
const user = userEvent.setup();
const apiErrors = {
email: ['Email is already taken']
};

render(ExampleForm, {
global: config.global,
props: {
handleSubmit: (
_values: Record<string, unknown>,
{ setErrors }: { setErrors: (input: typeof apiErrors) => void }
) => {
setErrors(apiErrors);
},
initialValues: {
name: NAME_VALUE,
email: EMAIL_VALUE,
password: PASSWORD_VALUE,
confirmPassword: CONFIRM_PASSWORD_VALUE
}
}
});

const emailInput = screen.getByLabelText(EMAIL_LABEL);
const submitButton = screen.getByRole('button', { name: /submit/i });

await user.click(submitButton);
expect(await screen.findByText(apiErrors.email[0])).toBeVisible();

await user.type(emailInput, 'x');

await waitFor(() => {
expect(screen.queryByText(apiErrors.email[0])).not.toBeInTheDocument();
});
});

it('should only clear error of the edited field, not other fields', async () => {
const user = userEvent.setup();
const apiErrors = {
name: ['Name is invalid'],
email: ['Email is already taken']
};

render(ExampleForm, {
global: config.global,
props: {
handleSubmit: (
_values: Record<string, unknown>,
{ setErrors }: { setErrors: (input: typeof apiErrors) => void }
) => {
setErrors(apiErrors);
},
initialValues: {
name: NAME_VALUE,
email: EMAIL_VALUE,
password: PASSWORD_VALUE,
confirmPassword: CONFIRM_PASSWORD_VALUE
}
}
});

const emailInput = screen.getByLabelText(EMAIL_LABEL);
const submitButton = screen.getByRole('button', { name: /submit/i });

await user.click(submitButton);
expect(await screen.findByText(apiErrors.name[0])).toBeVisible();
expect(await screen.findByText(apiErrors.email[0])).toBeVisible();

await user.type(emailInput, 'x');

await waitFor(() => {
expect(screen.queryByText(apiErrors.email[0])).not.toBeInTheDocument();
});
expect(screen.getByText(apiErrors.name[0])).toBeVisible();
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export type FormSubmitHelpers = {
reset: () => void;
};

export type FormSubmitHandler<T extends Record<string, unknown> = Record<string, unknown>> = (
export type FormSubmitHandler<T extends object = Record<string, unknown>> = (
values: T,
helpers: FormSubmitHelpers
) => void | Promise<void>;
Expand Down
Loading