Skip to content
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

Used resetFieldState to fix insert and unshift bugs #34

Merged
merged 1 commit into from
Jul 15, 2019
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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.13.0",
"final-form": "^4.17.0",
"final-form": "^4.18.0",
"flow-bin": "^0.102.0",
"glow": "^1.2.2",
"husky": "^3.0.0",
Expand All @@ -73,7 +73,7 @@
"typescript": "^3.5.3"
},
"peerDependencies": {
"final-form": "^4.17.0"
"final-form": "^4.18.0"
},
"lint-staged": {
"*.{js*,ts*,json,md,css}": [
Expand Down
8 changes: 4 additions & 4 deletions src/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { MutableState, Mutator, Tools } from 'final-form'
const insert: Mutator<any> = (
[name, index, value]: any[],
state: MutableState<any>,
{ changeValue }: Tools<any>
{ changeValue, resetFieldState }: Tools<any>
) => {
changeValue(state, name, (array: ?(any[])): any[] => {
const copy = [...(array || [])]
Expand All @@ -22,12 +22,12 @@ const insert: Mutator<any> = (
if (fieldIndex >= index) {
// inc index one higher
const incrementedKey = `${name}[${fieldIndex + 1}]${tokens[2]}`
changes[incrementedKey] = state.fields[key]
changes[incrementedKey] = { ...state.fields[key] } // make copy of field state
changes[incrementedKey].name = incrementedKey
changes[incrementedKey].forceUpdate = true
changes[incrementedKey].lastFieldState = undefined
}
if (fieldIndex === index) {
delete state.fields[key]
resetFieldState(key)
}
}
})
Expand Down
33 changes: 25 additions & 8 deletions src/insert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getIn, setIn } from 'final-form'
describe('insert', () => {
const getOp = (index, value) => {
const changeValue = jest.fn()
const resetFieldState = jest.fn()
const state = {
formState: {
values: {
Expand All @@ -23,12 +24,13 @@ describe('insert', () => {
}
}
}
insert(['foo', index, value], state, { changeValue })
insert(['foo', index, value], state, { changeValue, resetFieldState })
return changeValue.mock.calls[0][2]
}

it('should call changeValue once', () => {
const changeValue = jest.fn()
const resetFieldState = jest.fn()
const state = {
formState: {
values: {
Expand All @@ -52,7 +54,10 @@ describe('insert', () => {
}
}
}
const result = insert(['foo', 0, 'bar'], state, { changeValue })
const result = insert(['foo', 0, 'bar'], state, {
changeValue,
resetFieldState
})
expect(result).toBeUndefined()
expect(changeValue).toHaveBeenCalled()
expect(changeValue).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -86,6 +91,9 @@ describe('insert', () => {
const after = mutate(before)
state.formState.values = setIn(state.formState.values, name, after) || {}
}
const resetFieldState = name => {
state.fields[name].touched = false
}
const state = {
formState: {
values: {
Expand All @@ -100,7 +108,7 @@ describe('insert', () => {
},
'foo[1]': {
name: 'foo[1]',
touched: false,
touched: true,
error: 'B Error'
},
'foo[2]': {
Expand All @@ -115,7 +123,10 @@ describe('insert', () => {
}
}
}
const returnValue = insert(['foo', 1, 'NEWVALUE'], state, { changeValue })
const returnValue = insert(['foo', 1, 'NEWVALUE'], state, {
changeValue,
resetFieldState
})
expect(returnValue).toBeUndefined()
expect(state.formState.values.foo).not.toBe(array) // copied
expect(state).toEqual({
Expand All @@ -130,23 +141,29 @@ describe('insert', () => {
touched: true,
error: 'A Error'
},
'foo[1]': {
name: 'foo[1]',
touched: false,
error: 'B Error',
lastFieldState: undefined
},
'foo[2]': {
name: 'foo[2]',
touched: false,
touched: true,
error: 'B Error',
forceUpdate: true
lastFieldState: undefined
},
'foo[3]': {
name: 'foo[3]',
touched: true,
error: 'C Error',
forceUpdate: true
lastFieldState: undefined
},
'foo[4]': {
name: 'foo[4]',
touched: false,
error: 'D Error',
forceUpdate: true
lastFieldState: undefined
}
}
})
Expand Down
1 change: 0 additions & 1 deletion src/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ const move: Mutator<any> = (
change: state.fields[destKey] && state.fields[destKey].change,
blur: state.fields[destKey] && state.fields[destKey].blur,
focus: state.fields[destKey] && state.fields[destKey].focus,
forceUpdate: true,
lastFieldState: undefined // clearing lastFieldState forces renotification
}
}
Expand Down
38 changes: 16 additions & 22 deletions src/move.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,19 @@ describe('move', () => {
name: 'foo[0]',
touched: true,
error: 'Error B',
lastFieldState: undefined,
forceUpdate: true
lastFieldState: undefined
},
'foo[1]': {
name: 'foo[1]',
touched: false,
error: 'Error C',
lastFieldState: undefined,
forceUpdate: true
lastFieldState: undefined
},
'foo[2]': {
name: 'foo[2]',
touched: true,
error: 'Error A',
lastFieldState: undefined,
forceUpdate: true
lastFieldState: undefined
},
'foo[3]': {
name: 'foo[3]',
Expand Down Expand Up @@ -176,22 +173,19 @@ describe('move', () => {
name: 'foo[0]',
touched: false,
error: 'Error C',
lastFieldState: undefined,
forceUpdate: true
lastFieldState: undefined
},
'foo[1]': {
name: 'foo[1]',
touched: true,
error: 'Error A',
lastFieldState: undefined,
forceUpdate: true
lastFieldState: undefined
},
'foo[2]': {
name: 'foo[2]',
touched: true,
error: 'Error B',
lastFieldState: undefined,
forceUpdate: true
lastFieldState: undefined
},
'foo[3]': {
name: 'foo[3]',
Expand Down Expand Up @@ -281,7 +275,7 @@ describe('move', () => {
name: 'foo[0].dog',
touched: true,
error: 'Error B Dog',
forceUpdate: true
lastFieldState: undefined
},
'foo[0].cat': {
name: 'foo[0].cat',
Expand All @@ -302,13 +296,13 @@ describe('move', () => {
name: 'foo[2].dog',
touched: true,
error: 'Error A Dog',
forceUpdate: true
lastFieldState: undefined
},
'foo[2].cat': {
name: 'foo[2].cat',
touched: false,
error: 'Error A Cat',
forceUpdate: true
lastFieldState: undefined
},
'foo[3].dog': {
name: 'foo[3].dog',
Expand Down Expand Up @@ -402,13 +396,13 @@ describe('move', () => {
name: 'foo[0].dog',
touched: true,
error: 'Error C Dog',
forceUpdate: true
lastFieldState: undefined
},
'foo[0].cat': {
name: 'foo[0].cat',
touched: false,
error: 'Error C Cat',
forceUpdate: true
lastFieldState: undefined
},
'foo[1].dog': {
name: 'foo[1].dog',
Expand All @@ -424,13 +418,13 @@ describe('move', () => {
name: 'foo[2].dog',
touched: true,
error: 'Error B Dog',
forceUpdate: true
lastFieldState: undefined
},
'foo[2].cat': {
name: 'foo[2].cat',
touched: true,
error: 'Error B Cat',
forceUpdate: true
lastFieldState: undefined
},
'foo[3].dog': {
name: 'foo[3].dog',
Expand Down Expand Up @@ -489,19 +483,19 @@ describe('move', () => {
name: 'foo[0].dog',
touched: true,
error: 'Error B Dog',
forceUpdate: true
lastFieldState: undefined
},
'foo[1].dog': {
name: 'foo[1].dog',
touched: true,
error: 'Error A Dog',
forceUpdate: true
lastFieldState: undefined
},
'foo[1].cat': {
name: 'foo[1].cat',
touched: false,
error: 'Error A Cat',
forceUpdate: true
lastFieldState: undefined
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const remove: Mutator<any> = (
const decrementedKey = `${name}[${fieldIndex - 1}]${tokens[2]}`
state.fields[decrementedKey] = backup[key]
state.fields[decrementedKey].name = decrementedKey
state.fields[decrementedKey].forceUpdate = true
state.fields[decrementedKey].lastFieldState = undefined
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/remove.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ describe('remove', () => {
name: 'foo[1]',
touched: true,
error: 'C Error',
forceUpdate: true
lastFieldState: undefined
},
'foo[2]': {
name: 'foo[2]',
touched: false,
error: 'D Error',
forceUpdate: true
lastFieldState: undefined
},
anotherField: {
name: 'anotherField',
Expand Down
2 changes: 1 addition & 1 deletion src/removeBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const removeBatch: Mutator<any> = (
countBelow(sortedIndexes, fieldIndex)}]${tokens[2]}`
newFields[decrementedKey] = state.fields[key]
newFields[decrementedKey].name = decrementedKey
newFields[decrementedKey].forceUpdate = true
newFields[decrementedKey].lastFieldState = undefined
}
} else {
newFields[key] = state.fields[key]
Expand Down
10 changes: 5 additions & 5 deletions src/removeBatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('removeBatch', () => {
name: 'foo[0]',
touched: true,
error: 'First Error',
forceUpdate: true
lastFieldState: undefined
}
}
})
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('removeBatch', () => {
name: 'foo[0]',
touched: false,
error: 'Second Error',
forceUpdate: true
lastFieldState: undefined
}
}
})
Expand Down Expand Up @@ -228,19 +228,19 @@ describe('removeBatch', () => {
name: 'foo[0]',
touched: true,
error: 'A Error',
forceUpdate: true
lastFieldState: undefined
},
'foo[1]': {
name: 'foo[1]',
touched: false,
error: 'D Error',
forceUpdate: true
lastFieldState: undefined
},
'foo[2]': {
name: 'foo[2]',
touched: true,
error: 'E Error',
forceUpdate: true
lastFieldState: undefined
},
anotherField: {
name: 'anotherField',
Expand Down
Loading