From 6481aa4bb91a501988530e361c14829b09bc8083 Mon Sep 17 00:00:00 2001 From: Nikhil Agarwal Date: Tue, 5 Oct 2021 21:13:10 +0530 Subject: [PATCH] Bug fixes for final values --- src/FormProvider.tsx | 20 ++++++++++---------- src/atoms.ts | 10 +++------- src/types.ts | 1 + src/utils.ts | 2 +- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/FormProvider.tsx b/src/FormProvider.tsx index 592d591..9c43a83 100644 --- a/src/FormProvider.tsx +++ b/src/FormProvider.tsx @@ -153,7 +153,7 @@ export function useField(props: IFieldProps) { ); } }, - [skipUnregister, name, initialValues] + [skipUnregister, name, initialValues, ancestors] ); useEffect(() => { @@ -553,8 +553,8 @@ const getFormValues = (get: (val: RecoilValue) => any) => { if (f.param.name === ancestors[i].name) { for (let j = 0; j < i; j++) { if ( - fieldArray?.param.ancestors[j].name !== ancestors[j].name || - fieldArray?.param.ancestors[j].rowId !== ancestors[j].rowId + f.param.ancestors[j].name !== ancestors[j].name || + f.param.ancestors[j].rowId !== ancestors[j].rowId ) { return false; } @@ -571,9 +571,9 @@ const getFormValues = (get: (val: RecoilValue) => any) => { ), }); } else { - // throw new Error( - // `Field array '${ancestors[i].name}' in the ancestors of field ${fieldAtomValue.param.name} was not found` - // ); + throw new Error( + `Field array '${ancestors[i].name}' in the ancestors of field ${fieldAtomValue.param.name} was not found` + ); } } } @@ -641,16 +641,16 @@ export function useForm(props: IFormProps) { }, [handleReset]); const updateInitialValues = useRecoilTransaction_UNSTABLE( - ({ set, reset, get }) => + ({ set }) => ( values?: any, skipUnregister?: boolean, extraInfos?: any, formId?: string ) => { - if (!skipUnregister) { - resetDataAtoms(reset, get); - } + // if (!skipUnregister) { + // resetDataAtoms(reset, get); + // } initValuesVer.current = initValuesVer.current + 1; set(formInitialValuesAtom, (existingVal) => Object.assign({}, existingVal, { diff --git a/src/atoms.ts b/src/atoms.ts index aa3fc44..3aa68c6 100644 --- a/src/atoms.ts +++ b/src/atoms.ts @@ -61,19 +61,15 @@ export const fieldAtomFamily = atomFamily< default: (param) => { if (param.type === 'field') { return { - type: 'field', initVer: 0, - name: param.name, - ancestors: param.ancestors, + type: 'field', } as IFieldAtomValue; } return { - type: 'field-array', fieldNames: [], - ancestors: param.ancestors, initVer: 0, - name: param.name, rowIds: [], + type: 'field-array', } as IFieldArrayAtomValue; }, effects_UNSTABLE: (param) => [ @@ -359,7 +355,7 @@ export function setFieldArrayDataAndExtraInfo( ); } const oldRowIds = fieldArrayAtomValue.rowIds; - let dataRowsLength = dataArr.length; + let dataRowsLength = dataArr?.length ?? 0; let rowIdsToRemove: number[] = []; let rowIds: number[] = [...oldRowIds]; let startIndex = 0; diff --git a/src/types.ts b/src/types.ts index c263688..2aa86f5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,6 +3,7 @@ export interface IAtomValueBase { touched?: boolean; validate?: (data: any, otherParams?: any) => string | undefined | null; error?: string | null; + type: IFieldType; } export interface IFieldAtomValue extends IAtomValueBase { diff --git a/src/utils.ts b/src/utils.ts index b187bc4..a690b70 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -24,7 +24,7 @@ export function setPathInObj( if (ancestors?.length) { let prefix = ''; for (const ancestor of ancestors) { - prefix = prefix + `${ancestor.name}[${ancestor.index}]`; + prefix = prefix + `${ancestor.name}[${ancestor.index}].`; } path = prefix + path; }