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

Commit

Permalink
refactor(useForm): adjust the watch arg of getState()
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Nov 25, 2020
1 parent ef681c9 commit 35f9872
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-poets-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

refactor(useForm): adjust the `watch` arg of `getState()`
2 changes: 1 addition & 1 deletion examples/src/BasicForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export default (): JSX.Element => {
// submitCount: "submitCount",
})
); */
const errors = getState("errors", { watch: false });
const errors = getState("errors");
console.log("LOG ===> ", errors);

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export interface ValidateRef<V> {
export interface GetState {
(
path: string | string[] | Record<string, string>,
options?: { watch?: boolean; filterUntouchedErrors?: boolean }
watch?: { filterUntouchedErrors: boolean } | false
): any;
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/react-cool-form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ declare module "react-cool-form" {
interface GetState {
(
path: string | string[] | Record<string, string>,
options?: { watch?: boolean; filterUntouchedErrors?: boolean }
watch?: { filterUntouchedErrors: boolean } | false
): any;
}

Expand Down
11 changes: 7 additions & 4 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,13 @@ export default <V extends FormValues = FormValues>({
);

const getState = useCallback<GetState>(
(path, { watch = true, filterUntouchedErrors = true } = {}) => {
(path, watch = { filterUntouchedErrors: true }) => {
const touchedErrorEnhancer = (path: string, state: any) => {
if (!filterUntouchedErrors || !watch || !path.startsWith("errors"))
if (
!watch ||
!watch.filterUntouchedErrors ||
!path.startsWith("errors")
)
return state;

path = path.replace("errors", "touched");
Expand Down Expand Up @@ -559,8 +563,7 @@ export default <V extends FormValues = FormValues>({

const getOptions = useCallback(
() => ({
getState: ((path, options = { watch: false }) =>
getState(path, options)) as GetState,
getState: ((path, watch = false) => getState(path, watch)) as GetState,
setErrors,
setFieldError,
setValues,
Expand Down

0 comments on commit 35f9872

Please sign in to comment.