Skip to content

Commit c7bf418

Browse files
committed
Optimize swap, removing the need to move field state to a temporary field.
1 parent b32b947 commit c7bf418

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

src/swap.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// @flow
22
import type { MutableState, Mutator, Tools } from 'final-form'
3-
import moveFieldState from './moveFieldState'
4-
import moveFields from './moveFields';
5-
import restoreFunctions from './restoreFunctions';
6-
7-
const TMP: string = 'tmp'
3+
import copyField from './copyField'
84

95
const swap: Mutator<any> = (
106
[name, indexA, indexB]: any[],
@@ -22,19 +18,26 @@ const swap: Mutator<any> = (
2218
return copy
2319
})
2420

25-
//make a copy of a state for further functions restore
26-
const backupState = { ...state, fields: { ...state.fields } }
27-
2821
// swap all field state that begin with "name[indexA]" with that under "name[indexB]"
2922
const aPrefix = `${name}[${indexA}]`
3023
const bPrefix = `${name}[${indexB}]`
31-
const tmpPrefix = `${name}[${TMP}]`
32-
33-
moveFields(name, aPrefix, TMP, state)
34-
moveFields(name, bPrefix, indexA, state)
35-
moveFields(name, tmpPrefix, indexB, state)
24+
const newFields = {}
25+
Object.keys(state.fields).forEach(key => {
26+
if (key.substring(0, aPrefix.length) === aPrefix) {
27+
const suffix = key.substring(aPrefix.length)
28+
const newKey = bPrefix + suffix
29+
copyField(state.fields, key, newFields, newKey)
30+
} else if (key.substring(0, bPrefix.length) === bPrefix) {
31+
const suffix = key.substring(bPrefix.length)
32+
const newKey = aPrefix + suffix
33+
copyField(state.fields, key, newFields, newKey)
34+
} else {
35+
// Keep this field that does not match the name
36+
newFields[key] = state.fields[key]
37+
}
38+
})
3639

37-
restoreFunctions(state, backupState)
40+
state.fields = newFields
3841
}
3942

4043
export default swap

src/swap.test.js

+26-26
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ describe('swap', () => {
9393
}
9494
},
9595
fields: {
96-
'foo[0]': {
97-
name: 'foo[0]',
98-
touched: false,
99-
error: 'Error C',
96+
'foo[2]': {
97+
name: 'foo[2]',
98+
touched: true,
99+
error: 'Error A',
100100
lastFieldState: undefined
101101
},
102102
'foo[1]': {
@@ -105,10 +105,10 @@ describe('swap', () => {
105105
error: 'Error B',
106106
lastFieldState: 'anything' // unchanged
107107
},
108-
'foo[2]': {
109-
name: 'foo[2]',
110-
touched: true,
111-
error: 'Error A',
108+
'foo[0]': {
109+
name: 'foo[0]',
110+
touched: false,
111+
error: 'Error C',
112112
lastFieldState: undefined
113113
},
114114
'foo[3]': {
@@ -215,22 +215,22 @@ describe('swap', () => {
215215
}
216216
},
217217
fields: {
218-
'foo[0].dog': {
219-
name: 'foo[0].dog',
218+
'foo[2].dog': {
219+
name: 'foo[2].dog',
220220
touched: true,
221-
error: 'Error C Dog',
221+
error: 'Error A Dog',
222222
lastFieldState: undefined
223223
},
224-
'foo[0].cat': {
225-
name: 'foo[0].cat',
224+
'foo[2].cat': {
225+
name: 'foo[2].cat',
226226
touched: false,
227-
error: 'Error C Cat',
227+
error: 'Error A Cat',
228228
lastFieldState: undefined
229229
},
230-
'foo[0].axe': {
231-
name: 'foo[0].axe',
230+
'foo[2].rock': {
231+
name: 'foo[2].rock',
232232
touched: false,
233-
error: 'Error C Axe',
233+
error: 'Error A Rock',
234234
lastFieldState: undefined
235235
},
236236
'foo[1].dog': {
@@ -245,22 +245,22 @@ describe('swap', () => {
245245
error: 'Error B Cat',
246246
lastFieldState: 'anything' // unchanged
247247
},
248-
'foo[2].dog': {
249-
name: 'foo[2].dog',
248+
'foo[0].dog': {
249+
name: 'foo[0].dog',
250250
touched: true,
251-
error: 'Error A Dog',
251+
error: 'Error C Dog',
252252
lastFieldState: undefined
253253
},
254-
'foo[2].cat': {
255-
name: 'foo[2].cat',
254+
'foo[0].cat': {
255+
name: 'foo[0].cat',
256256
touched: false,
257-
error: 'Error A Cat',
257+
error: 'Error C Cat',
258258
lastFieldState: undefined
259259
},
260-
'foo[2].rock': {
261-
name: 'foo[2].rock',
260+
'foo[0].axe': {
261+
name: 'foo[0].axe',
262262
touched: false,
263-
error: 'Error A Rock',
263+
error: 'Error C Axe',
264264
lastFieldState: undefined
265265
},
266266
'foo[3].dog': {

0 commit comments

Comments
 (0)