Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

move isSubmitting state update after valid form #8829

Merged
merged 2 commits into from
Aug 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/__tests__/useForm/formState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,38 @@ describe('formState', () => {
expect(await screen.findByText('valid')).toBeVisible();
});

it('should remind isSubmitting when form is invalid', async () => {
const submittingState: boolean[] = [];

function App() {
const {
register,
formState: { isSubmitting },
handleSubmit,
} = useForm();

submittingState.push(isSubmitting);

return (
<form onSubmit={handleSubmit(() => {})}>
<input
{...register('value', { required: true })}
defaultValue="Any default value!"
/>
<button>Submit</button>
</form>
);
}

render(<App />);

await actComponent(async () => {
fireEvent.click(screen.getByRole('button'));
});

expect(submittingState).toEqual([false, false]);
});

describe('when defaultValue supplied', () => {
it('should update isValid to true for validation with inline defaultValue', async () => {
function App() {
Expand Down
8 changes: 4 additions & 4 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1023,10 +1023,6 @@ export function createFormControl<
let hasNoPromiseError = true;
let fieldValues: any = cloneObject(_formValues);

_subjects.state.next({
isSubmitting: true,
});

try {
if (_options.resolver) {
const { errors, values } = await _executeSchema();
Expand Down Expand Up @@ -1054,6 +1050,10 @@ export function createFormControl<
_names.mount,
);
}

_subjects.state.next({
isSubmitting: true,
});
} catch (err) {
hasNoPromiseError = false;
throw err;
Expand Down