|
1 | 1 | // @flow
|
2 | 2 | import type { MutableState, Mutator, Tools } from 'final-form'
|
| 3 | +import moveFieldState from './moveFieldState' |
3 | 4 |
|
4 | 5 | const move: Mutator<any> = (
|
5 | 6 | [name, from, to]: any[],
|
@@ -27,43 +28,28 @@ const move: Mutator<any> = (
|
27 | 28 | // decrement all indices between from and to
|
28 | 29 | for (let i = from; i < to; i++) {
|
29 | 30 | const destKey = `${name}[${i}]${suffix}`
|
30 |
| - moveFieldState({ |
31 |
| - destKey, |
32 |
| - source: state.fields[`${name}[${i + 1}]${suffix}`] |
33 |
| - }) |
| 31 | + moveFieldState( |
| 32 | + state, |
| 33 | + state.fields[`${name}[${i + 1}]${suffix}`], |
| 34 | + destKey |
| 35 | + ) |
34 | 36 | }
|
35 | 37 | } else {
|
36 | 38 | // moving to a lower index
|
37 | 39 | // increment all indices between to and from
|
38 | 40 | for (let i = from; i > to; i--) {
|
39 | 41 | const destKey = `${name}[${i}]${suffix}`
|
40 |
| - moveFieldState({ |
41 |
| - destKey, |
42 |
| - source: state.fields[`${name}[${i - 1}]${suffix}`] |
43 |
| - }) |
| 42 | + moveFieldState( |
| 43 | + state, |
| 44 | + state.fields[`${name}[${i - 1}]${suffix}`], |
| 45 | + destKey |
| 46 | + ) |
44 | 47 | }
|
45 | 48 | }
|
46 | 49 | const toKey = `${name}[${to}]${suffix}`
|
47 |
| - moveFieldState({ |
48 |
| - destKey: toKey, |
49 |
| - source: backup |
50 |
| - }) |
| 50 | + moveFieldState(state, backup, toKey) |
51 | 51 | }
|
52 | 52 | })
|
53 |
| - |
54 |
| - function moveFieldState({ destKey, source }) { |
55 |
| - state.fields[destKey] = { |
56 |
| - ...source, |
57 |
| - name: destKey, |
58 |
| - // prevent functions from being overwritten |
59 |
| - // if the state.fields[destKey] does not exist, it will be created |
60 |
| - // when that field gets registered, with its own change/blur/focus callbacks |
61 |
| - change: state.fields[destKey] && state.fields[destKey].change, |
62 |
| - blur: state.fields[destKey] && state.fields[destKey].blur, |
63 |
| - focus: state.fields[destKey] && state.fields[destKey].focus, |
64 |
| - lastFieldState: undefined // clearing lastFieldState forces renotification |
65 |
| - } |
66 |
| - } |
67 | 53 | }
|
68 | 54 |
|
69 | 55 | export default move
|
0 commit comments