Skip to content

Commit 7430e61

Browse files
authored
Allowed field pattern to be a string[] (#11)
1 parent 9cdb1c7 commit 7430e61

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ A calculation to perform, with an optional `isEqual` predicate to determine if a
115115

116116
### `FieldName: string`
117117

118-
### `FieldPattern: FieldName | RegExp`
118+
### `FieldPattern: FieldName | RegExp | FieldName[]`
119119

120120
A pattern to match a field with.
121121

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"name": "final-form-calculate",
33
"version": "1.0.2",
4-
"description":
5-
"Decorator for calculating field values based on other field values in 🏁 Final Form",
4+
"description": "Decorator for calculating field values based on other field values in 🏁 Final Form",
65
"main": "dist/final-form-calculate.cjs.js",
76
"jsnext:main": "dist/final-form-calculate.es.js",
87
"module": "dist/final-form-calculate.es.js",
9-
"files": ["dist"],
8+
"files": [
9+
"dist"
10+
],
1011
"scripts": {
1112
"start": "nps",
1213
"test": "nps test",
1314
"precommit": "lint-staged && npm start validate"
1415
},
15-
"author":
16-
"Erik Rasmussen <[email protected]> (http://github.com/erikras)",
16+
"author": "Erik Rasmussen <[email protected]> (http://github.com/erikras)",
1717
"license": "MIT",
1818
"repository": {
1919
"type": "git",
@@ -62,7 +62,10 @@
6262
"final-form": ">=1.3.0"
6363
},
6464
"lint-staged": {
65-
"*.{js,json,md,css}": ["prettier --write", "git add"]
65+
"*.{js,json,md,css}": [
66+
"prettier --write",
67+
"git add"
68+
]
6669
},
6770
"bundlesize": [
6871
{

src/decorator.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ const createDecorator = (...calculations: Calculation[]): Decorator => (
3737
if (typeof field === 'string') {
3838
runUpdates(field, isEqual || tripleEquals, updates)
3939
} else {
40-
// field is a regex
41-
const regex = (field: RegExp)
40+
// field is a either array or regex
41+
const matches = Array.isArray(field)
42+
? name => ~field.indexOf(name)
43+
: name => (field: RegExp).test(name)
4244
fields.forEach(fieldName => {
43-
if (regex.test(fieldName)) {
45+
if (matches(fieldName)) {
4446
runUpdates(fieldName, isEqual || tripleEquals, updates)
4547
}
4648
})

src/decorator.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,49 @@ describe('decorator', () => {
8989
expect(bar.mock.calls[1][0].value).toBe('bazbar')
9090
})
9191

92+
it('should update one field when another changes, using array of field names', () => {
93+
const form = createForm({ onSubmit: onSubmitMock })
94+
const spy = jest.fn()
95+
const foo = jest.fn()
96+
const bar = jest.fn()
97+
form.subscribe(spy, { values: true })
98+
form.registerField('foo', foo, { value: true })
99+
form.registerField('bar', bar, { value: true })
100+
const decorator = createDecorator({
101+
field: ['cat', 'dog', 'rat', 'foo', 'hog'],
102+
updates: {
103+
bar: fooValue => `${fooValue}bar`
104+
}
105+
})
106+
const unsubscribe = decorator(form)
107+
expect(typeof unsubscribe).toBe('function')
108+
109+
expect(spy).toHaveBeenCalled()
110+
expect(spy).toHaveBeenCalledTimes(1)
111+
expect(spy.mock.calls[0][0].values).toEqual({})
112+
113+
expect(foo).toHaveBeenCalled()
114+
expect(foo).toHaveBeenCalledTimes(1)
115+
expect(foo.mock.calls[0][0].value).toBeUndefined()
116+
117+
expect(bar).toHaveBeenCalled()
118+
expect(bar).toHaveBeenCalledTimes(1)
119+
expect(bar.mock.calls[0][0].value).toBeUndefined()
120+
121+
// change foo (should trigger calculation on bar)
122+
form.change('foo', 'baz')
123+
124+
expect(spy).toHaveBeenCalledTimes(3)
125+
expect(spy.mock.calls[1][0].values).toEqual({ foo: 'baz' })
126+
expect(spy.mock.calls[2][0].values).toEqual({ foo: 'baz', bar: 'bazbar' })
127+
128+
expect(foo).toHaveBeenCalledTimes(2)
129+
expect(foo.mock.calls[1][0].value).toBe('baz')
130+
131+
expect(bar).toHaveBeenCalledTimes(2)
132+
expect(bar.mock.calls[1][0].value).toBe('bazbar')
133+
})
134+
92135
it('should cease when unsubscribed', () => {
93136
const form = createForm({ onSubmit: onSubmitMock })
94137
const spy = jest.fn()

src/types.js.flow

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22
type FieldName = string
3-
export type FieldPattern = FieldName | RegExp
3+
export type FieldPattern = FieldName | RegExp | FieldName[]
44
export type UpdatesByName = {
55
[FieldName]: (value: any, allValues: ?Object) => any
66
}

0 commit comments

Comments
 (0)