Skip to content

Fix move/swap breaking input fields #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,40 @@ const move: Mutator = (
// decrement all indices between from and to
for (let i = from; i < to; i++) {
const destKey = `${name}[${i}]${suffix}`
state.fields[destKey] = {
...state.fields[`${name}[${i + 1}]${suffix}`],
name: destKey,
lastFieldState: undefined // clearing lastFieldState forces renotification
}
moveFieldState({
destKey,
source: state.fields[`${name}[${i + 1}]${suffix}`]
})
}
} else {
// moving to a lower index
// increment all indices between to and from
for (let i = from; i > to; i--) {
const destKey = `${name}[${i}]${suffix}`
state.fields[destKey] = {
...state.fields[`${name}[${i - 1}]${suffix}`],
name: destKey,
lastFieldState: undefined // clearing lastFieldState forces renotification
}
moveFieldState({
destKey,
source: state.fields[`${name}[${i - 1}]${suffix}`]
})
}
}
const toKey = `${name}[${to}]${suffix}`
state.fields[toKey] = {
...backup,
name: toKey,
lastFieldState: undefined // clearing lastFieldState forces renotification
}
moveFieldState({
destKey: toKey,
source: backup
})
}
})

function moveFieldState({ destKey, source }) {
state.fields[destKey] = {
...source,
name: destKey,
change: state.fields[destKey].change, // prevent functions from being overwritten
blur: state.fields[destKey].blur,
focus: state.fields[destKey].focus,
lastFieldState: undefined // clearing lastFieldState forces renotification
}
}
}

export default move
51 changes: 51 additions & 0 deletions src/move.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,55 @@ describe('move', () => {
}
})
})

it('should preserve functions in field state', () => {
// implementation of changeValue taken directly from Final Form
const changeValue = (state, name, mutate) => {
const before = getIn(state.formState.values, name)
const after = mutate(before)
state.formState.values = setIn(state.formState.values, name, after) || {}
}
const state = {
formState: {
values: {
foo: ['apple', 'banana', 'carrot', 'date']
}
},
fields: {
'foo[0]': {
name: 'foo[0]',
touched: true,
error: 'Error A',
lastFieldState: 'anything',
change: () => 'foo[0]'
},
'foo[1]': {
name: 'foo[1]',
touched: true,
error: 'Error B',
lastFieldState: 'anything',
change: () => 'foo[1]'
},
'foo[2]': {
name: 'foo[2]',
touched: false,
error: 'Error C',
lastFieldState: 'anything',
change: () => 'foo[2]'
},
'foo[3]': {
name: 'foo[3]',
touched: false,
error: 'Error D',
lastFieldState: 'anything',
change: () => 'foo[3]'
}
}
}
move(['foo', 0, 2], state, { changeValue })
expect(state.fields['foo[0]'].change()).toBe('foo[0]')
expect(state.fields['foo[1]'].change()).toBe('foo[1]')
expect(state.fields['foo[2]'].change()).toBe('foo[2]')
expect(state.fields['foo[3]'].change()).toBe('foo[3]')
})
})
30 changes: 20 additions & 10 deletions src/swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,28 @@ const swap: Mutator = (
const aKey = aPrefix + suffix
const bKey = bPrefix + suffix
const fieldA = state.fields[aKey]
state.fields[aKey] = {
...state.fields[bKey],
name: aKey,
lastFieldState: undefined // clearing lastFieldState forces renotification
}
state.fields[bKey] = {
...fieldA,
name: bKey,
lastFieldState: undefined // clearing lastFieldState forces renotification
}

moveFieldState({
destKey: aKey,
source: state.fields[bKey]
})
moveFieldState({
destKey: bKey,
source: fieldA
})
}
})

function moveFieldState({ destKey, source }) {
state.fields[destKey] = {
...source,
name: destKey,
change: state.fields[destKey].change, // prevent functions from being overwritten
blur: state.fields[destKey].blur,
focus: state.fields[destKey].focus,
lastFieldState: undefined // clearing lastFieldState forces renotification
}
}
}

export default swap
51 changes: 51 additions & 0 deletions src/swap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,55 @@ describe('swap', () => {
}
})
})

it('should preserve functions in field state', () => {
// implementation of changeValue taken directly from Final Form
const changeValue = (state, name, mutate) => {
const before = getIn(state.formState.values, name)
const after = mutate(before)
state.formState.values = setIn(state.formState.values, name, after) || {}
}
const state = {
formState: {
values: {
foo: ['apple', 'banana', 'carrot', 'date']
}
},
fields: {
'foo[0]': {
name: 'foo[0]',
touched: true,
error: 'Error A',
lastFieldState: 'anything',
change: () => 'foo[0]'
},
'foo[1]': {
name: 'foo[1]',
touched: true,
error: 'Error B',
lastFieldState: 'anything',
change: () => 'foo[1]'
},
'foo[2]': {
name: 'foo[2]',
touched: false,
error: 'Error C',
lastFieldState: 'anything',
change: () => 'foo[2]'
},
'foo[3]': {
name: 'foo[3]',
touched: false,
error: 'Error D',
lastFieldState: 'anything',
change: () => 'foo[3]'
}
}
}
swap(['foo', 0, 2], state, { changeValue })
expect(state.fields['foo[0]'].change()).toBe('foo[0]')
expect(state.fields['foo[1]'].change()).toBe('foo[1]')
expect(state.fields['foo[2]'].change()).toBe('foo[2]')
expect(state.fields['foo[3]'].change()).toBe('foo[3]')
})
})