Skip to content

Commit

Permalink
Fix bugs for validateData and remove in field array
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilag committed Oct 5, 2021
1 parent 3347c2b commit 9be2649
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/FormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,11 @@ export function useFieldArray(props: IFieldArrayProps) {
}
);

const validateData = useRecoilTransaction_UNSTABLE(
({ get, set }) =>
const validateData = useRecoilCallback(
({ snapshot, set }) =>
() => {
const get = (atom: RecoilValue<any>) =>
snapshot.getLoadable(atom).contents;
const { errors } = getFieldArrayDataAndExtraInfo(
{ name, ancestors: ancestors ?? [] },
get,
Expand Down Expand Up @@ -307,12 +309,13 @@ export function useFieldArray(props: IFieldArrayProps) {
const remove = useRecoilTransaction_UNSTABLE(
({ set, get, reset }) =>
(index: number) => {
const formId = getFormId(get);
const fieldArrayAtomValue = get(
fieldAtomFamily({
ancestors: ancestors ?? [],
name,
type: 'field-array',
formId: initialValues.formId,
formId,
})
) as IFieldArrayAtomValue;
let rowIdToRemove = fieldArrayAtomValue.rowIds[index];
Expand All @@ -322,33 +325,35 @@ export function useFieldArray(props: IFieldArrayProps) {
get,
reset
);
const rowIds = [...fieldArrayAtomValue.rowIds];
rowIds.splice(index, 1);
const tempRowIds = [...fieldArrayAtomValue.rowIds];
tempRowIds.splice(index, 1);
set(
fieldAtomFamily({
ancestors: ancestors ?? [],
name,
type: 'field-array',
formId: initialValues.formId,
formId,
}),
Object.assign({}, fieldArrayAtomValue, {
rowIds,
})
(existingValue) =>
Object.assign({}, existingValue, {
rowIds: tempRowIds,
})
);
}
},
[]
[name, ancestors]
);

const removeAll = useRecoilTransaction_UNSTABLE(
({ get, set, reset }) =>
() => {
const formId = getFormId(get);
const fieldArrayAtomValue = get(
fieldAtomFamily({
name,
ancestors: ancestors ?? [],
type: 'field-array',
formId: initialValues.formId,
formId,
})
) as IFieldArrayAtomValue;
const rowIds = fieldArrayAtomValue.rowIds;
Expand All @@ -364,14 +369,14 @@ export function useFieldArray(props: IFieldArrayProps) {
name,
ancestors: ancestors ?? [],
type: 'field-array',
formId: initialValues.formId,
formId,
}),
Object.assign({}, fieldArrayAtomValue, {
rowIds: [],
})
);
},
[]
[name, ancestors]
);

const append = useRecoilTransaction_UNSTABLE(
Expand Down

0 comments on commit 9be2649

Please sign in to comment.