From 9a81f711f6ff129b646d8bdf40763673a9f52efb Mon Sep 17 00:00:00 2001 From: Nikhil Agarwal Date: Wed, 6 Oct 2021 12:23:28 +0530 Subject: [PATCH] Allow ancestors for watching fields + fix field array watch --- example/forms/SimpleFieldArray.tsx | 2 +- src/FormProvider.tsx | 4 +--- src/atoms.ts | 3 ++- src/types.ts | 6 +++++- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/example/forms/SimpleFieldArray.tsx b/example/forms/SimpleFieldArray.tsx index 498dedd..6d2cf8a 100644 --- a/example/forms/SimpleFieldArray.tsx +++ b/example/forms/SimpleFieldArray.tsx @@ -38,7 +38,7 @@ function SimpleFieldArray(props) { fieldArrayName='items' colNames={['amount']} calculateFunc={values => - values.items?.reduce((acc, val) => acc + (val?.amount ?? 0), 0) + values.reduce((acc, val) => acc + (val?.amount ?? 0), 0) } />
diff --git a/src/FormProvider.tsx b/src/FormProvider.tsx index 1e810ff..3af5daf 100644 --- a/src/FormProvider.tsx +++ b/src/FormProvider.tsx @@ -194,9 +194,7 @@ export function useField(props: IFieldProps) { data, extraInfo, error: validate ? validate(data, otherParams) : undefined, - ancestors, - name, - }) + } as Partial) ); }, [otherParams, validate, ancestors, name, setAtomValue] diff --git a/src/atoms.ts b/src/atoms.ts index 3aa68c6..41816a6 100644 --- a/src/atoms.ts +++ b/src/atoms.ts @@ -198,7 +198,8 @@ export function getFieldArrayDataAndExtraInfo( const extraInfo: any = []; const errors: IFieldError[] = []; const fieldArrayAtom = fieldAtomFamily({ - ...params, + ancestors: params.ancestors, + name: params.name, type: 'field-array', formId, }); diff --git a/src/types.ts b/src/types.ts index 2aa86f5..b8d130e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -31,10 +31,14 @@ export interface FinalValues { } export interface IFieldWatchParams { - fieldNames: string[]; + fieldNames: ( + | string + | { ancestors?: { name: string; rowId: number }[]; name: string } + )[]; } export interface IFieldArrayColWatchParams { + ancestors?: { name: string; rowId: number }[]; fieldArrayName: string; fieldNames?: string[]; }