Skip to content

Commit 7e6347e

Browse files
authored
Fix move swap (#14)
* Made move and swap also alter field state * Upgrade deps
1 parent c237695 commit 7e6347e

14 files changed

+6893
-4543
lines changed

package-lock.json

Lines changed: 6106 additions & 4458 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-scripts.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ const crossEnv = npsUtils.crossEnv
88
module.exports = {
99
scripts: {
1010
test: {
11-
default: crossEnv('NODE_ENV=test jest --coverage'),
12-
update: crossEnv('NODE_ENV=test jest --coverage --updateSnapshot'),
13-
watch: crossEnv('NODE_ENV=test jest --watch'),
11+
default: crossEnv('NODE_ENV=test jest --env=node --coverage'),
12+
update: crossEnv(
13+
'NODE_ENV=test jest --env=node --coverage --updateSnapshot'
14+
),
15+
watch: crossEnv('NODE_ENV=test jest --env=node --watch'),
1416
codeCov: crossEnv(
1517
'cat ./coverage/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js'
1618
),
@@ -71,7 +73,13 @@ module.exports = {
7173
validate: {
7274
description:
7375
'This runs several scripts to make sure things look good before committing or on clean install',
74-
default: concurrent.nps('lint', 'flow', 'typescript', 'build.andTest', 'test')
76+
default: concurrent.nps(
77+
'lint',
78+
'flow',
79+
'typescript',
80+
'build.andTest',
81+
'test'
82+
)
7583
}
7684
},
7785
options: {

package.json

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
"jsnext:main": "dist/final-form-arrays.es.js",
77
"module": "dist/final-form-arrays.es.js",
88
"typings": "dist/index.d.ts",
9-
"files": ["dist"],
9+
"files": [
10+
"dist"
11+
],
1012
"scripts": {
1113
"start": "nps",
1214
"test": "nps test",
1315
"precommit": "lint-staged && npm start validate"
1416
},
15-
"author":
16-
"Erik Rasmussen <[email protected]> (http://github.com/erikras)",
17+
"author": "Erik Rasmussen <[email protected]> (http://github.com/erikras)",
1718
"license": "MIT",
1819
"repository": {
1920
"type": "git",
@@ -25,43 +26,50 @@
2526
"homepage": "https://github.com/final-form/final-form-arrays#readme",
2627
"devDependencies": {
2728
"babel-eslint": "^8.2.1",
28-
"babel-jest": "^22.1.0",
29+
"babel-jest": "^23.4.2",
2930
"babel-plugin-external-helpers": "^6.22.0",
3031
"babel-plugin-transform-flow-strip-types": "^6.22.0",
3132
"babel-preset-env": "^1.6.1",
3233
"babel-preset-stage-2": "^6.24.1",
33-
"bundlesize": "^0.15.3",
34+
"bundlesize": "^0.17.0",
3435
"doctoc": "^1.3.0",
35-
"eslint": "^4.16.0",
36+
"eslint": "^5.4.0",
3637
"eslint-config-react-app": "^2.1.0",
37-
"eslint-plugin-babel": "^4.1.2",
38+
"eslint-plugin-babel": "^5.1.0",
3839
"eslint-plugin-flowtype": "^2.41.0",
3940
"eslint-plugin-import": "^2.8.0",
4041
"eslint-plugin-jsx-a11y": "^6.0.3",
4142
"eslint-plugin-react": "^7.5.1",
4243
"final-form": "^4.0.4",
43-
"flow-bin": "^0.63.1",
44+
"flow-bin": "^0.79.1",
45+
"glow": "^1.2.2",
4446
"husky": "^0.14.3",
45-
"jest": "^22.1.4",
46-
"lint-staged": "^6.0.1",
47+
"jest": "^23.5.0",
48+
"lint-staged": "^7.2.2",
4749
"nps": "^5.7.1",
4850
"nps-utils": "^1.5.0",
4951
"prettier": "^1.10.2",
5052
"prettier-eslint-cli": "^4.7.0",
51-
"rollup": "^0.55.0",
53+
"rollup": "^0.64.1",
5254
"rollup-plugin-babel": "^3.0.3",
53-
"rollup-plugin-commonjs": "^8.2.6",
55+
"rollup-plugin-commonjs": "^9.1.5",
5456
"rollup-plugin-flow": "^1.1.1",
5557
"rollup-plugin-node-resolve": "^3.0.2",
5658
"rollup-plugin-replace": "^2.0.0",
57-
"rollup-plugin-uglify": "^3.0.0",
58-
"typescript": "^2.6.2"
59+
"rollup-plugin-uglify": "^4.0.0",
60+
"typescript": "^3.0.1"
5961
},
6062
"peerDependencies": {
6163
"final-form": ">=1.2.0"
6264
},
6365
"lint-staged": {
64-
"*.{js*,ts*,json,md,css}": ["prettier --write", "git add"]
66+
"*.{js*,ts*,json,md,css}": [
67+
"prettier --write",
68+
"git add"
69+
]
70+
},
71+
"jest": {
72+
"testEnvironment": "node"
6573
},
6674
"bundlesize": [
6775
{

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import resolve from 'rollup-plugin-node-resolve'
22
import babel from 'rollup-plugin-babel'
33
import flow from 'rollup-plugin-flow'
44
import commonjs from 'rollup-plugin-commonjs'
5-
import uglify from 'rollup-plugin-uglify'
5+
import { uglify } from 'rollup-plugin-uglify'
66
import replace from 'rollup-plugin-replace'
77

88
const minify = process.env.MINIFY

src/insert.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
// @flow
22
import type { MutableState, Mutator, Tools } from 'final-form'
33

4-
type Args = [string, number, any]
5-
64
const insert: Mutator = (
7-
[name, index, value]: Args,
5+
[name, index, value]: any[],
86
state: MutableState,
97
{ changeValue }: Tools
108
) => {
11-
changeValue(state, name, (array: ?(any[])): any[] => {
12-
const copy = [...(array || [])]
13-
copy.splice(index, 0, value)
14-
return copy
15-
})
9+
changeValue(
10+
state,
11+
name,
12+
(array: ?(any[])): any[] => {
13+
const copy = [...(array || [])]
14+
copy.splice(index, 0, value)
15+
return copy
16+
}
17+
)
1618
}
1719

1820
export default insert

src/move.js

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,61 @@
11
// @flow
22
import type { MutableState, Mutator, Tools } from 'final-form'
33

4-
type Args = [string, number, number]
5-
64
const move: Mutator = (
7-
[name, from, to]: Args,
5+
[name, from, to]: any[],
86
state: MutableState,
97
{ changeValue }: Tools
108
) => {
11-
changeValue(state, name, (array: ?(any[])): any[] => {
12-
const copy = [...(array || [])]
13-
const value = copy[from]
14-
copy.splice(from, 1)
15-
copy.splice(to, 0, value)
16-
return copy
9+
if (from === to) {
10+
return
11+
}
12+
changeValue(
13+
state,
14+
name,
15+
(array: ?(any[])): any[] => {
16+
const copy = [...(array || [])]
17+
const value = copy[from]
18+
copy.splice(from, 1)
19+
copy.splice(to, 0, value)
20+
return copy
21+
}
22+
)
23+
const fromPrefix = `${name}[${from}]`
24+
Object.keys(state.fields).forEach(key => {
25+
if (key.substring(0, fromPrefix.length) === fromPrefix) {
26+
const suffix = key.substring(fromPrefix.length)
27+
const fromKey = fromPrefix + suffix
28+
const backup = state.fields[fromKey]
29+
if (from < to) {
30+
// moving to a higher index
31+
// decrement all indices between from and to
32+
for (let i = from; i < to; i++) {
33+
const destKey = `${name}[${i}]${suffix}`
34+
state.fields[destKey] = {
35+
...state.fields[`${name}[${i + 1}]${suffix}`],
36+
name: destKey,
37+
lastFieldState: undefined // clearing lastFieldState forces renotification
38+
}
39+
}
40+
} else {
41+
// moving to a lower index
42+
// increment all indices between to and from
43+
for (let i = from; i > to; i--) {
44+
const destKey = `${name}[${i}]${suffix}`
45+
state.fields[destKey] = {
46+
...state.fields[`${name}[${i - 1}]${suffix}`],
47+
name: destKey,
48+
lastFieldState: undefined // clearing lastFieldState forces renotification
49+
}
50+
}
51+
}
52+
const toKey = `${name}[${to}]${suffix}`
53+
state.fields[toKey] = {
54+
...backup,
55+
name: toKey,
56+
lastFieldState: undefined // clearing lastFieldState forces renotification
57+
}
58+
}
1759
})
1860
}
1961

0 commit comments

Comments
 (0)