UNPKG

3.43 kBTypeScriptView Raw
1/// <reference types="hoist-non-react-statics" />
2import * as React from 'react';
3import { FormikContextType, FormikProps, SharedRenderProps } from './types';
4export type FieldArrayRenderProps = ArrayHelpers & {
5 form: FormikProps<any>;
6 name: string;
7};
8export type FieldArrayConfig = {
9 /** Really the path to the array field to be updated */
10 name: string;
11 /** Should field array validate the form AFTER array updates/changes? */
12 validateOnChange?: boolean;
13} & SharedRenderProps<FieldArrayRenderProps>;
14export interface ArrayHelpers<T extends any[] = any[]> {
15 /** Imperatively add a value to the end of an array */
16 push<X = T[number]>(obj: X): void;
17 /** Curried fn to add a value to the end of an array */
18 handlePush<X = T[number]>(obj: X): () => void;
19 /** Imperatively swap two values in an array */
20 swap: (indexA: number, indexB: number) => void;
21 /** Curried fn to swap two values in an array */
22 handleSwap: (indexA: number, indexB: number) => () => void;
23 /** Imperatively move an element in an array to another index */
24 move: (from: number, to: number) => void;
25 /** Imperatively move an element in an array to another index */
26 handleMove: (from: number, to: number) => () => void;
27 /** Imperatively insert an element at a given index into the array */
28 insert<X = T[number]>(index: number, value: X): void;
29 /** Curried fn to insert an element at a given index into the array */
30 handleInsert<X = T[number]>(index: number, value: X): () => void;
31 /** Imperatively replace a value at an index of an array */
32 replace<X = T[number]>(index: number, value: X): void;
33 /** Curried fn to replace an element at a given index into the array */
34 handleReplace<X = T[number]>(index: number, value: X): () => void;
35 /** Imperatively add an element to the beginning of an array and return its length */
36 unshift<X = T[number]>(value: X): number;
37 /** Curried fn to add an element to the beginning of an array */
38 handleUnshift<X = T[number]>(value: X): () => void;
39 /** Curried fn to remove an element at an index of an array */
40 handleRemove: (index: number) => () => void;
41 /** Curried fn to remove a value from the end of the array */
42 handlePop: () => () => void;
43 /** Imperatively remove and element at an index of an array */
44 remove<X = T[number]>(index: number): X | undefined;
45 /** Imperatively remove and return value from the end of the array */
46 pop<X = T[number]>(): X | undefined;
47}
48/**
49 * Some array helpers!
50 */
51export declare const move: <T>(array: T[], from: number, to: number) => unknown[];
52export declare const swap: <T>(arrayLike: ArrayLike<T>, indexA: number, indexB: number) => unknown[];
53export declare const insert: <T>(arrayLike: ArrayLike<T>, index: number, value: T) => unknown[];
54export declare const replace: <T>(arrayLike: ArrayLike<T>, index: number, value: T) => unknown[];
55export declare const FieldArray: React.FC<FieldArrayConfig> & import("hoist-non-react-statics").NonReactStatics<React.ComponentClass<{
56 /** Really the path to the array field to be updated */
57 name: string;
58 /** Should field array validate the form AFTER array updates/changes? */
59 validateOnChange?: boolean | undefined;
60} & SharedRenderProps<FieldArrayRenderProps> & {
61 formik: FormikContextType<any>;
62}, any>, {}>;