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

Adds Option To Ignore Updates On InitialValues #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 30 additions & 18 deletions src/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,37 @@ const createDecorator = <FormValues: FormValuesShape>(
}
}
const fields = form.getRegisteredFields()
calculations.forEach(({ field, isEqual, updates }) => {
if (typeof field === 'string') {
runUpdates(field, isEqual || tripleEquals, updates)
} else {
// field is a either array or regex
const matches = Array.isArray(field)
? name =>
~field.indexOf(name) ||
field.findIndex(
f => f instanceof RegExp && (f: RegExp).test(name)
) !== -1
: name => (field: RegExp).test(name)
fields.forEach(fieldName => {
if (matches(fieldName)) {
runUpdates(fieldName, isEqual || tripleEquals, updates)
}
})
calculations.forEach(
({ field, isEqual, updates, updateOnPristine = true }) => {
if (
(updateOnPristine && typeof field === 'string') ||
(!updateOnPristine &&
typeof field === 'string' &&
!form.getFieldState(field).pristine)
) {
runUpdates(field, isEqual || tripleEquals, updates)
} else if (typeof field !== 'string') {
// field is a either array or regex
const matches = Array.isArray(field)
? name =>
~field.indexOf(name) ||
field.findIndex(
f => f instanceof RegExp && (f: RegExp).test(name)
) !== -1
: name => (field: RegExp).test(name)
fields.forEach(fieldName => {
if (
(updateOnPristine && matches(fieldName)) ||
(!updateOnPristine &&
matches(fieldName) &&
!form.getFieldState(fieldName).pristine)
) {
runUpdates(fieldName, isEqual || tripleEquals, updates)
}
})
}
}
})
)
previousValues = values
})
},
Expand Down
94 changes: 94 additions & 0 deletions src/decorator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,100 @@ import createDecorator from './decorator'

const onSubmitMock = () => {}
describe('decorator', () => {
it('should not trigger update on initialValues if updateOnPristine is false', () => {
const form = createForm({
initialValues: { foo: 'test', bar: 'test' },
onSubmit: onSubmitMock
})
const spy = jest.fn()
const foo = jest.fn()
const bar = jest.fn()
form.subscribe(spy, { values: true })
form.registerField('foo', foo, { value: true })
form.registerField('bar', bar, { value: true })
const decorator = createDecorator({
field: 'foo',
updateOnPristine: false,
updates: {
bar: (fooValue) => `${fooValue}bar`
}
})
const unsubscribe = decorator(form)
expect(typeof unsubscribe).toBe('function')

expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][0].values).toEqual({ foo: 'test', bar: 'test' })

expect(foo).toHaveBeenCalled()
expect(foo).toHaveBeenCalledTimes(1)
expect(foo.mock.calls[0][0].value).toBe('test')

expect(bar).toHaveBeenCalled()
expect(bar).toHaveBeenCalledTimes(1)
expect(bar.mock.calls[0][0].value).toBe('test')

// change foo (should trigger calculation on bar)
form.change('foo', 'baz')

expect(spy).toHaveBeenCalledTimes(3)
expect(spy.mock.calls[1][0].values).toEqual({ foo: 'baz', bar: 'test' })
expect(spy.mock.calls[2][0].values).toEqual({ foo: 'baz', bar: 'bazbar' })

expect(foo).toHaveBeenCalledTimes(2)
expect(foo.mock.calls[1][0].value).toBe('baz')

expect(bar).toHaveBeenCalledTimes(2)
expect(bar.mock.calls[1][0].value).toBe('bazbar')
})

it('should not trigger update on initialValues if updateOnPristine is false, using array of field names', () => {
const form = createForm({
initialValues: { foo: 'test', bar: 'test' },
onSubmit: onSubmitMock
})
const spy = jest.fn()
const foo = jest.fn()
const bar = jest.fn()
form.subscribe(spy, { values: true })
form.registerField('foo', foo, { value: true })
form.registerField('bar', bar, { value: true })
const decorator = createDecorator({
field: ['cat', 'dog', 'rat', 'foo', 'hog'],
updateOnPristine: false,
updates: {
bar: (fooValue) => `${fooValue}bar`
}
})
const unsubscribe = decorator(form)
expect(typeof unsubscribe).toBe('function')

expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][0].values).toEqual({ foo: 'test', bar: 'test' })

expect(foo).toHaveBeenCalled()
expect(foo).toHaveBeenCalledTimes(1)
expect(foo.mock.calls[0][0].value).toBe('test')

expect(bar).toHaveBeenCalled()
expect(bar).toHaveBeenCalledTimes(1)
expect(bar.mock.calls[0][0].value).toBe('test')

// change foo (should trigger calculation on bar)
form.change('foo', 'baz')

expect(spy).toHaveBeenCalledTimes(3)
expect(spy.mock.calls[1][0].values).toEqual({ foo: 'baz', bar: 'test' })
expect(spy.mock.calls[2][0].values).toEqual({ foo: 'baz', bar: 'bazbar' })

expect(foo).toHaveBeenCalledTimes(2)
expect(foo.mock.calls[1][0].value).toBe('baz')

expect(bar).toHaveBeenCalledTimes(2)
expect(bar.mock.calls[1][0].value).toBe('bazbar')
})

it('should update one field when another changes', () => {
const form = createForm({ onSubmit: onSubmitMock })
const spy = jest.fn()
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type Updates = UpdatesByName | UpdatesForAll
export type Calculation = {
field: FieldPattern,
updates: Updates,
updateOnPristine?: boolean,
isEqual?: (a: any, b: any) => boolean,
}

Expand Down
1 change: 1 addition & 0 deletions src/types.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type Updates = UpdatesByName | UpdatesForAll

export type Calculation = {
field: FieldPattern,
updateOnPristine?: boolean,
isEqual?: (any, any) => boolean,
updates: Updates
}