Skip to content

Commit

Permalink
Bug fixes for final values
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilag committed Oct 5, 2021
1 parent e476f19 commit 6481aa4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/FormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export function useField<D = any, E = any>(props: IFieldProps<D>) {
);
}
},
[skipUnregister, name, initialValues]
[skipUnregister, name, initialValues, ancestors]
);

useEffect(() => {
Expand Down Expand Up @@ -553,8 +553,8 @@ const getFormValues = (get: (val: RecoilValue<any>) => 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;
}
Expand All @@ -571,9 +571,9 @@ const getFormValues = (get: (val: RecoilValue<any>) => 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`
);
}
}
}
Expand Down Expand Up @@ -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, {
Expand Down
10 changes: 3 additions & 7 deletions src/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => [
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<D = any, E = any> extends IAtomValueBase {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 6481aa4

Please sign in to comment.