forked from jaredpalmer/formik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FieldArray.tsx
290 lines (259 loc) · 8.02 KB
/
FieldArray.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import * as React from 'react';
import cloneDeep from 'lodash.clonedeep';
import { connect } from './connect';
import {
FormikContext,
FormikState,
SharedRenderProps,
FormikProps,
} from './types';
import { getIn, isEmptyChildren, isFunction, setIn } from './utils';
export type FieldArrayRenderProps = ArrayHelpers & {
form: FormikProps<any>;
name: string;
};
export type FieldArrayConfig = {
/** Really the path to the array field to be updated */
name: string;
/** Should field array validate the form AFTER array updates/changes? */
validateOnChange?: boolean;
} & SharedRenderProps<FieldArrayRenderProps>;
export interface ArrayHelpers {
/** Imperatively add a value to the end of an array */
push: (obj: any) => void;
/** Curried fn to add a value to the end of an array */
handlePush: (obj: any) => () => void;
/** Imperatively swap two values in an array */
swap: (indexA: number, indexB: number) => void;
/** Curried fn to swap two values in an array */
handleSwap: (indexA: number, indexB: number) => () => void;
/** Imperatively move an element in an array to another index */
move: (from: number, to: number) => void;
/** Imperatively move an element in an array to another index */
handleMove: (from: number, to: number) => () => void;
/** Imperatively insert an element at a given index into the array */
insert: (index: number, value: any) => void;
/** Curried fn to insert an element at a given index into the array */
handleInsert: (index: number, value: any) => () => void;
/** Imperatively replace a value at an index of an array */
replace: (index: number, value: any) => void;
/** Curried fn to replace an element at a given index into the array */
handleReplace: (index: number, value: any) => () => void;
/** Imperatively add an element to the beginning of an array and return its length */
unshift: (value: any) => number;
/** Curried fn to add an element to the beginning of an array */
handleUnshift: (value: any) => () => void;
/** Curried fn to remove an element at an index of an array */
handleRemove: (index: number) => () => void;
/** Curried fn to remove a value from the end of the array */
handlePop: () => () => void;
/** Imperatively remove and element at an index of an array */
remove<T>(index: number): T | undefined;
/** Imperatively remove and return value from the end of the array */
pop<T>(): T | undefined;
}
/**
* Some array helpers!
*/
export const move = (array: any[], from: number, to: number) => {
const copy = [...(array || [])];
const value = copy[from];
copy.splice(from, 1);
copy.splice(to, 0, value);
return copy;
};
export const swap = (array: any[], indexA: number, indexB: number) => {
const copy = [...(array || [])];
const a = copy[indexA];
copy[indexA] = copy[indexB];
copy[indexB] = a;
return copy;
};
export const insert = (array: any[], index: number, value: any) => {
const copy = [...(array || [])];
copy.splice(index, 0, value);
return copy;
};
export const replace = (array: any[], index: number, value: any) => {
const copy = [...(array || [])];
copy[index] = value;
return copy;
};
class FieldArrayInner<Values = {}> extends React.Component<
FieldArrayConfig & { formik: FormikContext<Values> },
{}
> {
static defaultProps = {
validateOnChange: true,
};
constructor(props: FieldArrayConfig & { formik: FormikContext<Values> }) {
super(props);
// We need TypeScript generics on these, so we'll bind them in the constructor
this.remove = this.remove.bind(this);
this.pop = this.pop.bind(this);
}
updateArrayField = (
fn: Function,
alterTouched: boolean,
alterErrors: boolean
) => {
const {
name,
validateOnChange,
formik: { setFormikState, validateForm, values, touched, errors },
} = this.props;
setFormikState(
(prevState: FormikState<any>) => ({
...prevState,
values: setIn(prevState.values, name, fn(getIn(values, name))),
errors: alterErrors
? setIn(prevState.errors, name, fn(getIn(errors, name)))
: prevState.errors,
touched: alterTouched
? setIn(prevState.touched, name, fn(getIn(touched, name)))
: prevState.touched,
}),
() => {
if (validateOnChange) {
validateForm();
}
}
);
};
push = (value: any) =>
this.updateArrayField(
(array: any[]) => [...(array || []), cloneDeep(value)],
false,
false
);
handlePush = (value: any) => () => this.push(value);
swap = (indexA: number, indexB: number) =>
this.updateArrayField(
(array: any[]) => swap(array, indexA, indexB),
true,
true
);
handleSwap = (indexA: number, indexB: number) => () =>
this.swap(indexA, indexB);
move = (from: number, to: number) =>
this.updateArrayField(
(array: any[]) => move(array, from, to),
true,
true
);
handleMove = (from: number, to: number) => () => this.move(from, to);
insert = (index: number, value: any) =>
this.updateArrayField(
(array: any[]) => insert(array, index, value),
true,
true
);
handleInsert = (index: number, value: any) => () => this.insert(index, value);
replace = (index: number, value: any) =>
this.updateArrayField(
(array: any[]) => replace(array, index, value),
false,
false
);
handleReplace = (index: number, value: any) => () =>
this.replace(index, value);
unshift = (value: any) => {
let length = -1;
this.updateArrayField(
(array: any[]) => {
const arr = array ? [value, ...array] : [value];
if (length < 0) length = arr.length;
return arr;
},
true,
true
);
return length;
};
handleUnshift = (value: any) => () => this.unshift(value);
remove<T>(index: number): T {
// We need to make sure we also remove relevant pieces of `touched` and `errors`
let result: any;
this.updateArrayField(
// so this gets call 3 times
(array?: any[]) => {
const copy = array ? [...array] : [];
if (!result) {
result = copy[index];
}
if (isFunction(copy.splice)) {
copy.splice(index, 1);
}
return copy;
},
true,
true
);
return result;
}
handleRemove = (index: number) => () => this.remove<any>(index);
pop<T>(): T {
// Remove relevant pieces of `touched` and `errors` too!
let result: any;
this.updateArrayField(
// so this gets call 3 times
(array: any[]) => {
const tmp = array;
if (!result) {
result = tmp && tmp.pop && tmp.pop();
}
return tmp;
},
true,
true
);
return result;
}
handlePop = () => () => this.pop<any>();
render() {
const arrayHelpers: ArrayHelpers = {
push: this.push,
pop: this.pop,
swap: this.swap,
move: this.move,
insert: this.insert,
replace: this.replace,
unshift: this.unshift,
remove: this.remove,
handlePush: this.handlePush,
handlePop: this.handlePop,
handleSwap: this.handleSwap,
handleMove: this.handleMove,
handleInsert: this.handleInsert,
handleReplace: this.handleReplace,
handleUnshift: this.handleUnshift,
handleRemove: this.handleRemove,
};
const {
component,
render,
children,
name,
formik: {
validate: _validate,
validationSchema: _validationSchema,
...restOfFormik
},
} = this.props;
const props: FieldArrayRenderProps = {
...arrayHelpers,
form: restOfFormik,
name,
};
return component
? React.createElement(component as any, props)
: render
? (render as any)(props)
: children // children come last, always called
? typeof children === 'function'
? (children as any)(props)
: !isEmptyChildren(children) ? React.Children.only(children) : null
: null;
}
}
export const FieldArray = connect<FieldArrayConfig, any>(FieldArrayInner);