Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Refactor(form-state): reset isSubmitted state when the user attempt…
Browse files Browse the repository at this point in the history
…s to submit the form
  • Loading branch information
wellyshen committed Apr 18, 2021
1 parent 6ff7531 commit 2401051
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-mice-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

Refactor(form-state): reset `isSubmitted` state when the user attempts to submnit the form
6 changes: 3 additions & 3 deletions docs/getting-started/bundle-size-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ React Cool Form is a tiny size library ([~ 7KB](https://bundlephobia.com/result?
| Name | Size |
| --------------------------------------------------- | ------- |
| [useForm](../api-reference/use-form) | ~ 5.6kB |
| [useFormMethods](../api-reference/use-form-methods) | ~ 254B |
| [useFormState](../api-reference/use-form-state) | ~ 301B |
| [useFormMethods](../api-reference/use-form-methods) | ~ 255B |
| [useFormState](../api-reference/use-form-state) | ~ 302B |
| [useControlled](../api-reference/use-controlled) | ~ 783B |
| [useFieldArray](../api-reference/use-field-array) | ~ 869B |
| [useFieldArray](../api-reference/use-field-array) | ~ 870B |
| [get](../api-reference/utility-functions#get) | ~ 5B |
| [set](../api-reference/utility-functions#set) | ~ 5B |
| [unset](../api-reference/utility-functions#unset) | ~ 6B |
24 changes: 12 additions & 12 deletions docs/getting-started/form-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ Here we will explore the form state and some [best practices for using it](#best

Form state is an `object` containing the following properties:

| Name | Type | Description |
| ------------ | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| values | `object` | The current values of the form. |
| errors | `object` | The current validation errors. [The shape will (should) match the shape of the form's values](./validation-guide#how-to-run). |
| touched | `object` | An object containing all the fields the user has touched/visited. |
| isDirty | `boolean` | Returns `true` if the user modifies any of the fields. `false` otherwise. |
| dirty | `object` | An object containing all the fields the user has modified. |
| isValidating | `boolean` | Returns `true` if the form is currently being validated. `false` otherwise. |
| isValid | `boolean` | Returns `true` if the form doesn't have any errors (i.e. the `errors` object is empty). `false` otherwise. |
| isSubmitting | `boolean` | Returns `true` if the form is currently being submitted. `false` if otherwise. |
| isSubmitted | `boolean` | Returns `true` if the form has been submitted successfully. `false` if otherwise. The value will remain until the [form is reset](./reset-form). |
| submitCount | `number` | Number of times the user tried to submit the form. The value will remain until the [form is reset](./reset-form). |
| Name | Type | Description |
| ------------ | --------- | ----------------------------------------------------------------------------------------------------------------------------- |
| values | `object` | The current values of the form. |
| errors | `object` | The current validation errors. [The shape will (should) match the shape of the form's values](./validation-guide#how-to-run). |
| touched | `object` | An object containing all the fields the user has touched/visited. |
| isDirty | `boolean` | Returns `true` if the user modifies any of the fields. `false` otherwise. |
| dirty | `object` | An object containing all the fields the user has modified. |
| isValidating | `boolean` | Returns `true` if the form is currently being validated. `false` otherwise. |
| isValid | `boolean` | Returns `true` if the form doesn't have any errors (i.e. the `errors` object is empty). `false` otherwise. |
| isSubmitting | `boolean` | Returns `true` if the form is currently being submitted. `false` if otherwise. |
| isSubmitted | `boolean` | Returns `true` if the form has been submitted successfully. `false` if otherwise. |
| submitCount | `number` | Number of times the user tried to submit the form. The value will remain until the [form is reset](./reset-form). |

## Using the Form State

Expand Down
6 changes: 6 additions & 0 deletions src/useForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ describe("useForm", () => {
</>
),
});

const value = "🍎";
fireEvent.input(getByTestId("foo"), { target: { value } });
fireEvent.submit(getByTestId("form"));
Expand Down Expand Up @@ -275,6 +276,11 @@ describe("useForm", () => {
isSubmitting: false,
isSubmitted: true,
});

fireEvent.submit(getByTestId("form"));
expect(getState("isSubmitted")).toBeFalsy();
await waitFor(() => expect(onSubmit).toHaveBeenCalled());
expect(getState("isSubmitted")).toBeTruthy();
});

it('should call "onError" event correctly', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ export default <V extends FormValues = FormValues>({
}, stateRef.current.touched);

setStateRef("touched", nextTouched);
setStateRef("isSubmitted", false);
setStateRef("isSubmitting", true);

try {
Expand Down

0 comments on commit 2401051

Please sign in to comment.