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

Commit

Permalink
feat(useForm): set all fields to touched when submitting
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Nov 22, 2020
1 parent 3b94b09 commit 957de35
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-candles-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

feat(useForm): set all fields to touched when submitting
9 changes: 9 additions & 0 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,15 @@ export default <V extends FormValues = FormValues>({
e?.preventDefault();
e?.stopPropagation();

const touched = Object.keys({
...fieldsRef.current,
...controlledFieldsRef.current,
}).reduce((acc: Map, key) => {
acc = set(acc, key, true);
return acc;
}, {});

setStateRef("touched", touched);
setStateRef("isSubmitting", true);

try {
Expand Down
2 changes: 1 addition & 1 deletion src/useState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useReducer, useRef, useCallback } from "react";
import isEqual from "fast-deep-equal";

import { Debug, FormState, FormStateReturn, SetStateRef, Map } from "./types";
import { Debug, FormState, FormStateReturn, Map, SetStateRef } from "./types";
import useLatest from "./useLatest";
import { get, isEmptyObject, set } from "./utils";

Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/ban-types */

import { FieldElement } from "./types";
import { FieldElement, Map } from "./types";

export const warn = (...args: any[]): void => {
if (__DEV__) console.warn(...args);
};

export const arrayToMap = (arr: any[]): Record<string, boolean> =>
export const arrayToMap = (arr: any[]): Map =>
arr.reduce((obj, key) => {
obj[key] = true;
return obj;
Expand Down

0 comments on commit 957de35

Please sign in to comment.