diff --git a/.changeset/great-experts-enjoy.md b/.changeset/great-experts-enjoy.md new file mode 100644 index 000000000..51dad09a9 --- /dev/null +++ b/.changeset/great-experts-enjoy.md @@ -0,0 +1,5 @@ +--- +'formik': patch +--- + +Fix return types and add docs to standalone `FieldArray` utility exports (`move`, `swap`, `insert`, `replace`). \ No newline at end of file diff --git a/packages/formik/src/FieldArray.tsx b/packages/formik/src/FieldArray.tsx index 1e0a4073e..732d263e5 100644 --- a/packages/formik/src/FieldArray.tsx +++ b/packages/formik/src/FieldArray.tsx @@ -63,10 +63,8 @@ export interface ArrayHelpers { pop(): X | undefined; } -/** - * Some array helpers! - */ -export const move = (array: T[], from: number, to: number) => { +/** Move an element in an array from one index to another. Returns a new array. */ +export const move = (array: T[], from: number, to: number): T[] => { const copy = copyArrayLike(array); const value = copy[from]; copy.splice(from, 1); @@ -74,11 +72,12 @@ export const move = (array: T[], from: number, to: number) => { return copy; }; +/** Swap two elements in an array by index. Returns a new array. */ export const swap = ( arrayLike: ArrayLike, indexA: number, indexB: number -) => { +): T[] => { const copy = copyArrayLike(arrayLike); const a = copy[indexA]; copy[indexA] = copy[indexB]; @@ -86,27 +85,29 @@ export const swap = ( return copy; }; +/** Insert an element at the given index. Returns a new array. */ export const insert = ( arrayLike: ArrayLike, index: number, value: T -) => { +): T[] => { const copy = copyArrayLike(arrayLike); copy.splice(index, 0, value); return copy; }; +/** Replace the element at the given index. Returns a new array. */ export const replace = ( arrayLike: ArrayLike, index: number, value: T -) => { +): T[] => { const copy = copyArrayLike(arrayLike); copy[index] = value; return copy; }; -const copyArrayLike = (arrayLike: ArrayLike) => { +const copyArrayLike = (arrayLike: ArrayLike): T[] => { if (!arrayLike) { return []; } else if (Array.isArray(arrayLike)) {