-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcopyField.js
35 lines (30 loc) · 995 Bytes
/
copyField.js
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
// @flow
import type { InternalFieldState } from 'final-form/dist/types'
function copyField(
oldFields: { [string]: InternalFieldState },
oldKey: string,
newFields: { [string]: InternalFieldState },
newKey: string
) {
newFields[newKey] = {
...oldFields[oldKey],
name: newKey,
// prevent functions from being overwritten
// if the newFields[newKey] does not exist, it will be created
// when that field gets registered, with its own change/blur/focus callbacks
change: oldFields[newKey] && oldFields[newKey].change,
blur: oldFields[newKey] && oldFields[newKey].blur,
focus: oldFields[newKey] && oldFields[newKey].focus,
lastFieldState: undefined // clearing lastFieldState forces renotification
}
if (!newFields[newKey].change) {
delete newFields[newKey].change
}
if (!newFields[newKey].blur) {
delete newFields[newKey].blur
}
if (!newFields[newKey].focus) {
delete newFields[newKey].focus
}
}
export default copyField