1
1
// @flow
2
2
import type { MutableState , Mutator , Tools } from 'final-form'
3
- import moveFields from './moveFields'
4
- import restoreFunctions from './restoreFunctions'
5
-
6
- const TMP : string = 'tmp'
3
+ import copyField from './copyField'
4
+ import { escapeRegexTokens } from './utils'
7
5
8
6
const move : Mutator < any > = (
9
7
[name, from, to]: any[],
@@ -21,34 +19,44 @@ const move: Mutator<any> = (
21
19
return copy
22
20
} )
23
21
24
- //make a copy of a state for further functions restore
25
- const backupState = { ...state , fields : { ...state . fields } }
26
-
27
- // move this row to tmp index
28
- const fromPrefix = `${ name } [${ from } ]`
29
- moveFields(name, fromPrefix, TMP, state)
30
-
31
- if (from < to ) {
32
- // moving to a higher index
33
- // decrement all indices between from and to
34
- for ( let i = from + 1 ; i <= to ; i ++ ) {
35
- const innerFromPrefix = `${ name } [${ i } ]`
36
- moveFields ( name , innerFromPrefix , `${ i - 1 } ` , state )
37
- }
22
+ const newFields = { }
23
+ const pattern = new RegExp(`^${ escapeRegexTokens ( name ) } \\[(\\d+)\\](.*)`)
24
+ let lowest
25
+ let highest
26
+ let increment
27
+ if (from > to ) {
28
+ lowest = to
29
+ highest = from
30
+ increment = 1
38
31
} else {
39
- // moving to a lower index
40
- // increment all indices between to and from
41
- for ( let i = from - 1 ; i >= to ; i -- ) {
42
- const innerFromPrefix = `${ name } [${ i } ]`
43
- moveFields ( name , innerFromPrefix , `${ i + 1 } ` , state )
44
- }
32
+ lowest = from
33
+ highest = to
34
+ increment = - 1
45
35
}
36
+ Object.keys(state.fields).forEach(key => {
37
+ const tokens = pattern . exec ( key )
38
+ if ( tokens ) {
39
+ const fieldIndex = Number ( tokens [ 1 ] )
40
+ if ( fieldIndex === from ) {
41
+ const newKey = `${ name } [${ to } ]${ tokens [ 2 ] } `
42
+ copyField ( state . fields , key , newFields , newKey )
43
+ return
44
+ }
45
+
46
+ if (lowest < = fieldIndex && fieldIndex <= highest ) {
47
+ // Shift all indices
48
+ const newKey = `${ name } [${ fieldIndex + increment } ]${ tokens [ 2 ] } `
49
+ copyField ( state . fields , key , newFields , newKey )
50
+ return
51
+ }
52
+ }
46
53
47
- // move from tmp index to destination
48
- const tmpPrefix = `${name } [ ${TMP } ] `
49
- moveFields ( name , tmpPrefix , to , state )
54
+ // Keep this field that does not match the name,
55
+ // or has index smaller or larger than affected range
56
+ newFields [ key ] = state . fields [ key ]
57
+ } )
50
58
51
- restoreFunctions ( state , backupState )
59
+ state . fields = newFields
52
60
}
53
61
54
62
export default move
0 commit comments