Skip to content

Commit

Permalink
馃崒 useFieldArray reduce function (#5471)
Browse files Browse the repository at this point in the history
* reduce function

* rename function
  • Loading branch information
bluebill1049 committed Jun 3, 2021
1 parent 9655571 commit a05b436
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions src/useFieldArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const useFieldArray = <
);
};

const getFocusDetail = (
const getFocusFieldName = (
index: number,
options?: FieldArrayMethodProps,
): string =>
Expand All @@ -131,24 +131,6 @@ export const useFieldArray = <
const cleanup = <T>(ref: T) =>
!compact(get(ref, name, [])).length && unset(ref, name);

const updateDirtyFieldsWithDefaultValues = <
T extends Partial<
FieldArrayWithId<TFieldValues, TFieldArrayName, TKeyName>
>[],
>(
updatedFieldArrayValues?: T,
) =>
updatedFieldArrayValues &&
set(
formStateRef.current.dirtyFields,
name,
setFieldArrayDirtyFields(
omitKey(updatedFieldArrayValues),
get(defaultValuesRef.current, name, []),
get(formStateRef.current.dirtyFields, name, []),
),
);

const batchStateUpdate = <T extends Function>(
method: T,
args: {
Expand Down Expand Up @@ -202,7 +184,16 @@ export const useFieldArray = <
get(formStateRef.current.dirtyFields, name, []),
),
);
updateDirtyFieldsWithDefaultValues(updatedFieldArrayValues);
updatedFieldArrayValues &&
set(
formStateRef.current.dirtyFields,
name,
setFieldArrayDirtyFields(
omitKey(updatedFieldArrayValues),
get(defaultValuesRef.current, name, []),
get(formStateRef.current.dirtyFields, name, []),
),
);
cleanup(formStateRef.current.dirtyFields);
}

Expand Down Expand Up @@ -269,7 +260,7 @@ export const useFieldArray = <
);
registerFieldArray(appendValue, currentIndex);

focusNameRef.current = getFocusDetail(currentIndex, options);
focusNameRef.current = getFocusFieldName(currentIndex, options);
};

const prepend = (
Expand Down Expand Up @@ -299,7 +290,7 @@ export const useFieldArray = <
);
registerFieldArray(prependValue);

focusNameRef.current = getFocusDetail(0, options);
focusNameRef.current = getFocusFieldName(0, options);
};

const remove = (index?: number | number[]) => {
Expand Down Expand Up @@ -347,7 +338,7 @@ export const useFieldArray = <
);
registerFieldArray(insertValue, index);

focusNameRef.current = getFocusDetail(index, options);
focusNameRef.current = getFocusFieldName(index, options);
};

const swap = (indexA: number, indexB: number) => {
Expand Down

0 comments on commit a05b436

Please sign in to comment.