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

Fixed some bugs using final-form's new forceUpdate flag #32

Merged
merged 1 commit into from
Jul 13, 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
9,991 changes: 3,377 additions & 6,614 deletions package-lock.json

Large diffs are not rendered by default.

59 changes: 30 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,55 @@
},
"homepage": "https://github.com/final-form/final-form-arrays#readme",
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/core": "^7.5.4",
"@babel/plugin-external-helpers": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-decorators": "^7.0.0",
"@babel/plugin-proposal-export-namespace-from": "^7.0.0",
"@babel/plugin-proposal-function-sent": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.5.0",
"@babel/plugin-proposal-decorators": "^7.4.4",
"@babel/plugin-proposal-export-namespace-from": "^7.5.2",
"@babel/plugin-proposal-function-sent": "^7.5.0",
"@babel/plugin-proposal-json-strings": "^7.0.0",
"@babel/plugin-proposal-numeric-separator": "^7.0.0",
"@babel/plugin-proposal-throw-expressions": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-syntax-import-meta": "^7.0.0",
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.0.0",
"@babel/plugin-transform-flow-strip-types": "^7.4.4",
"@babel/plugin-transform-runtime": "^7.5.0",
"@babel/preset-env": "^7.5.4",
"@babel/preset-flow": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.4.2",
"bundlesize": "^0.17.1",
"babel-eslint": "^10.0.2",
"babel-jest": "^24.8.0",
"bundlesize": "^0.18.0",
"doctoc": "^1.3.0",
"eslint": "^5.13.0",
"eslint": "^6.0.1",
"eslint-config-react-app": "^3.0.6",
"eslint-plugin-babel": "^5.1.0",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-flowtype": "^3.2.1",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.12.4",
"final-form": "^4.11.1",
"flow-bin": "^0.92.1",
"eslint-plugin-react": "^7.13.0",
"final-form": "^4.17.0",
"flow-bin": "^0.102.0",
"glow": "^1.2.2",
"husky": "^1.3.1",
"jest": "^24.1.0",
"lint-staged": "^8.1.3",
"nps": "^5.7.1",
"husky": "^3.0.0",
"jest": "^24.8.0",
"lint-staged": "^9.2.0",
"nps": "^5.9.5",
"nps-utils": "^1.5.0",
"prettier": "^1.16.4",
"prettier-eslint-cli": "^4.7.0",
"rollup": "^1.1.2",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^9.1.5",
"prettier": "^1.18.2",
"prettier-eslint-cli": "^5.0.0",
"react": "^16.8.6",
"rollup": "^1.16.7",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-flow": "^1.1.1",
"rollup-plugin-node-resolve": "^4.0.0",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-uglify": "^6.0.2",
"typescript": "^3.3.1"
"typescript": "^3.5.3"
},
"peerDependencies": {
"final-form": ">=1.2.0"
"final-form": "^4.17.0"
},
"lint-staged": {
"*.{js*,ts*,json,md,css}": [
Expand Down
12 changes: 5 additions & 7 deletions src/concat.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// @flow
import type { MutableState, Mutator, Tools } from 'final-form'

const concat: Mutator = (
const concat: Mutator<any> = (
[name, value]: any[],
state: MutableState,
{ changeValue }: Tools
state: MutableState<any>,
{ changeValue }: Tools<any>
) => {
changeValue(
state,
name,
(array: ?(any[])): any[] => (array ? [...array, ...value] : value)
changeValue(state, name, (array: ?(any[])): any[] =>
array ? [...array, ...value] : value
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import swap from './swap'
import unshift from './unshift'
import update from './update'

const mutators: { [string]: Mutator } = {
const mutators: { [string]: Mutator<any> } = {
insert,
concat,
move,
Expand Down
2 changes: 1 addition & 1 deletion src/index.js.flow
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import type { Mutator } from 'final-form'

type DefaultType = { [string]: Mutator }
type DefaultType = { [string]: Mutator<any> }

declare export default DefaultType

Expand Down
21 changes: 9 additions & 12 deletions src/insert.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
// @flow
import type { MutableState, Mutator, Tools } from 'final-form'

const insert: Mutator = (
const insert: Mutator<any> = (
[name, index, value]: any[],
state: MutableState,
{ changeValue }: Tools
state: MutableState<any>,
{ changeValue }: Tools<any>
) => {
changeValue(
state,
name,
(array: ?(any[])): any[] => {
const copy = [...(array || [])]
copy.splice(index, 0, value)
return copy
}
)
changeValue(state, name, (array: ?(any[])): any[] => {
const copy = [...(array || [])]
copy.splice(index, 0, value)
return copy
})

// now we have increment any higher indexes
const pattern = new RegExp(`^${name}\\[(\\d+)\\](.*)`)
Expand All @@ -28,6 +24,7 @@ const insert: Mutator = (
const incrementedKey = `${name}[${fieldIndex + 1}]${tokens[2]}`
changes[incrementedKey] = state.fields[key]
changes[incrementedKey].name = incrementedKey
changes[incrementedKey].forceUpdate = true
}
if (fieldIndex === index) {
delete state.fields[key]
Expand Down
9 changes: 6 additions & 3 deletions src/insert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,20 @@ describe('insert', () => {
'foo[2]': {
name: 'foo[2]',
touched: false,
error: 'B Error'
error: 'B Error',
forceUpdate: true
},
'foo[3]': {
name: 'foo[3]',
touched: true,
error: 'C Error'
error: 'C Error',
forceUpdate: true
},
'foo[4]': {
name: 'foo[4]',
touched: false,
error: 'D Error'
error: 'D Error',
forceUpdate: true
}
}
})
Expand Down
25 changes: 11 additions & 14 deletions src/move.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
// @flow
import type { MutableState, Mutator, Tools } from 'final-form'

const move: Mutator = (
const move: Mutator<any> = (
[name, from, to]: any[],
state: MutableState,
{ changeValue }: Tools
state: MutableState<any>,
{ changeValue }: Tools<any>
) => {
if (from === to) {
return
}
changeValue(
state,
name,
(array: ?(any[])): any[] => {
const copy = [...(array || [])]
const value = copy[from]
copy.splice(from, 1)
copy.splice(to, 0, value)
return copy
}
)
changeValue(state, name, (array: ?(any[])): any[] => {
const copy = [...(array || [])]
const value = copy[from]
copy.splice(from, 1)
copy.splice(to, 0, value)
return copy
})
const fromPrefix = `${name}[${from}]`
Object.keys(state.fields).forEach(key => {
if (key.substring(0, fromPrefix.length) === fromPrefix) {
Expand Down Expand Up @@ -65,6 +61,7 @@ const move: Mutator = (
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
52 changes: 34 additions & 18 deletions src/move.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,22 @@ describe('move', () => {
name: 'foo[0]',
touched: true,
error: 'Error B',
lastFieldState: undefined
lastFieldState: undefined,
forceUpdate: true
},
'foo[1]': {
name: 'foo[1]',
touched: false,
error: 'Error C',
lastFieldState: undefined
lastFieldState: undefined,
forceUpdate: true
},
'foo[2]': {
name: 'foo[2]',
touched: true,
error: 'Error A',
lastFieldState: undefined
lastFieldState: undefined,
forceUpdate: true
},
'foo[3]': {
name: 'foo[3]',
Expand Down Expand Up @@ -173,19 +176,22 @@ describe('move', () => {
name: 'foo[0]',
touched: false,
error: 'Error C',
lastFieldState: undefined
lastFieldState: undefined,
forceUpdate: true
},
'foo[1]': {
name: 'foo[1]',
touched: true,
error: 'Error A',
lastFieldState: undefined
lastFieldState: undefined,
forceUpdate: true
},
'foo[2]': {
name: 'foo[2]',
touched: true,
error: 'Error B',
lastFieldState: undefined
lastFieldState: undefined,
forceUpdate: true
},
'foo[3]': {
name: 'foo[3]',
Expand Down Expand Up @@ -259,7 +265,7 @@ describe('move', () => {
}
}
move(['foo', 0, 2], state, { changeValue })
expect(state).toEqual({
expect(state).toMatchObject({
formState: {
values: {
foo: [
Expand All @@ -274,7 +280,8 @@ describe('move', () => {
'foo[0].dog': {
name: 'foo[0].dog',
touched: true,
error: 'Error B Dog'
error: 'Error B Dog',
forceUpdate: true
},
'foo[0].cat': {
name: 'foo[0].cat',
Expand All @@ -294,12 +301,14 @@ describe('move', () => {
'foo[2].dog': {
name: 'foo[2].dog',
touched: true,
error: 'Error A Dog'
error: 'Error A Dog',
forceUpdate: true
},
'foo[2].cat': {
name: 'foo[2].cat',
touched: false,
error: 'Error A Cat'
error: 'Error A Cat',
forceUpdate: true
},
'foo[3].dog': {
name: 'foo[3].dog',
Expand Down Expand Up @@ -377,7 +386,7 @@ describe('move', () => {
}
}
move(['foo', 2, 0], state, { changeValue })
expect(state).toEqual({
expect(state).toMatchObject({
formState: {
values: {
foo: [
Expand All @@ -392,12 +401,14 @@ describe('move', () => {
'foo[0].dog': {
name: 'foo[0].dog',
touched: true,
error: 'Error C Dog'
error: 'Error C Dog',
forceUpdate: true
},
'foo[0].cat': {
name: 'foo[0].cat',
touched: false,
error: 'Error C Cat'
error: 'Error C Cat',
forceUpdate: true
},
'foo[1].dog': {
name: 'foo[1].dog',
Expand All @@ -412,12 +423,14 @@ describe('move', () => {
'foo[2].dog': {
name: 'foo[2].dog',
touched: true,
error: 'Error B Dog'
error: 'Error B Dog',
forceUpdate: true
},
'foo[2].cat': {
name: 'foo[2].cat',
touched: true,
error: 'Error B Cat'
error: 'Error B Cat',
forceUpdate: true
},
'foo[3].dog': {
name: 'foo[3].dog',
Expand Down Expand Up @@ -475,17 +488,20 @@ describe('move', () => {
'foo[0].dog': {
name: 'foo[0].dog',
touched: true,
error: 'Error B Dog'
error: 'Error B Dog',
forceUpdate: true
},
'foo[1].dog': {
name: 'foo[1].dog',
touched: true,
error: 'Error A Dog'
error: 'Error A Dog',
forceUpdate: true
},
'foo[1].cat': {
name: 'foo[1].cat',
touched: false,
error: 'Error A Cat'
error: 'Error A Cat',
forceUpdate: true
}
}
})
Expand Down
Loading