Skip to content

Commit

Permalink
Add cleanFields action
Browse files Browse the repository at this point in the history
clears fields values by a given array

Issue: redux-form#3055, Issue: redux-form#2224
  • Loading branch information
dvdmgl committed Oct 31, 2017
1 parent 6e262c2 commit 1fbf964
Show file tree
Hide file tree
Showing 13 changed files with 616 additions and 77 deletions.
16 changes: 12 additions & 4 deletions docs/api/ActionCreators.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ form, and, in the case of field-specific actions such as `CHANGE` or `BLUR`, the
### `arrayMove(form:String, field:String, from:Number, to:Number)`

> Moves an item from one index in the array to another. In effect, it performs a remove and an
> Moves an item from one index in the array to another. In effect, it performs a remove and an
insert, so the item already at the `to` position will be bumped to a higher index, not overwritten.

### `arrayPop(form:String, field:String)`
Expand Down Expand Up @@ -63,7 +63,15 @@ insert, so the item already at the `to` position will be bumped to a higher inde
### `clearSubmitErrors(form:String)`

> Removes `submitErrors` and `error`.
> Removes `submitErrors` and `error`.
### `clearFields(form:String, keepTouched: boolean, persistentSubmitErrors: boolean, ...fields:String)`

> Cleans fields values for all the fields passed in.
> If the `keepTouched` parameter is `true`, the values of currently touched fields will be retained
> If the `persistentSubmitErrors` parameter is `true`, the values of currently submit errors fields will be retained

### `destroy(...forms:String)`

Expand All @@ -83,7 +91,7 @@ your form fields.
to avoid overwriting user edits. (`keepDirty` can appear as either the third argument, or a property of `options` as
the 3rd or 4th argument, for the sake of backwards compatibility).

> If the `keepSubmitSucceeded` parameter is `true`,
> If the `keepSubmitSucceeded` parameter is `true`,
it will not clear the `submitSucceeded` flag if it is set.

### `registerField(form:String, name:String, type:String)`
Expand All @@ -96,7 +104,7 @@ it will not clear the `submitSucceeded` flag if it is set.
### `setSubmitFailed(form:String, ...fields:String)`

> Flips `submitFailed` flag `true`, removes `submitSucceeded` and `submitting`, marks all the fields passed in as `touched`, and if at least one field was passed flips `anyTouched` to `true`.
> Flips `submitFailed` flag `true`, removes `submitSucceeded` and `submitting`, marks all the fields passed in as `touched`, and if at least one field was passed flips `anyTouched` to `true`.
### `setSubmitSucceeded(form:String)`

Expand Down
4 changes: 1 addition & 3 deletions src/ConnectedFieldArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ const createConnectedFieldArray = (structure: Structure<*, *>) => {
const nextValue = nextProps.value

if (thisValue && nextValue) {
let nextValueItemsSame = nextValue.every(
val => ~thisValue.indexOf(val)
)
let nextValueItemsSame = nextValue.every(val => ~thisValue.indexOf(val))
let nextValueItemsOrderChanged = nextValue.some(
(val, index) => val !== thisValue[index]
)
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
CHANGE,
CLEAR_SUBMIT,
CLEAR_SUBMIT_ERRORS,
CLEAR_FIELDS,
DESTROY,
FOCUS,
INITIALIZE,
Expand Down Expand Up @@ -50,6 +51,7 @@ const {
change,
clearSubmit,
clearSubmitErrors,
clearFields,
destroy,
focus,
initialize,
Expand Down Expand Up @@ -386,6 +388,21 @@ describe('actions', () => {
expect(isFSA(clearSubmitErrors('myForm'))).toBe(true)
})

it('should create clear fields action', () => {
expect(clearFields('myForm', true, true, 'a', 'b')).toEqual({
type: CLEAR_FIELDS,

meta: {
form: 'myForm',
keepTouched: true,
persistentSubmitErrors: true,
fields: ['a', 'b']
}
})

expect(isFSA(clearSubmitErrors('myForm'))).toBe(true)
})

it('should create initialize action', () => {
const data = { a: 8, c: 9 }

Expand Down
204 changes: 135 additions & 69 deletions src/__tests__/createFieldProps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const describeCreateFieldProps = (name, structure, setup) => {

it('should pass value through', () => {
expect(
createFieldProps({ getIn, toJS, deepEqual }, 'foo', { value: 'hello' }).input.value
createFieldProps({ getIn, toJS, deepEqual }, 'foo', { value: 'hello' })
.input.value
).toBe('hello')
})

Expand Down Expand Up @@ -49,8 +50,9 @@ const describeCreateFieldProps = (name, structure, setup) => {

it('should pass initial value through', () => {
expect(
createFieldProps({ getIn, toJS, deepEqual }, 'foo', { initial: 'hello' }).meta
.initial
createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
initial: 'hello'
}).meta.initial
).toBe('hello')
})

Expand Down Expand Up @@ -112,10 +114,14 @@ const describeCreateFieldProps = (name, structure, setup) => {
})

it('should read active from state', () => {
const inactiveResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: empty
})
const inactiveResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: empty
}
)
expect(inactiveResult.meta.active).toBe(false)
const activeResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
Expand All @@ -127,21 +133,33 @@ const describeCreateFieldProps = (name, structure, setup) => {
})

it('should pass along submitting flag', () => {
const notSubmittingResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar'
})
const notSubmittingResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar'
}
)
expect(notSubmittingResult.meta.submitting).toBe(false)
const submittingResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
submitting: true
})
const submittingResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
submitting: true
}
)
expect(submittingResult.meta.submitting).toBe(true)
})

it('should pass along submitFailed flag', () => {
const notFailedResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar'
})
const notFailedResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar'
}
)
expect(notFailedResult.meta.submitFailed).toBe(false)
const failedResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
Expand All @@ -151,9 +169,13 @@ const describeCreateFieldProps = (name, structure, setup) => {
})

it('should pass along all custom state props', () => {
const pristineResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar'
})
const pristineResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar'
}
)
expect(pristineResult.meta.customProp).toBe(undefined)
const customResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
Expand All @@ -165,9 +187,13 @@ const describeCreateFieldProps = (name, structure, setup) => {
})

it('should not override canonical props with custom props', () => {
const pristineResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar'
})
const pristineResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar'
}
)
expect(pristineResult.meta.customProp).toBe(undefined)
const customResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
Expand All @@ -180,40 +206,60 @@ const describeCreateFieldProps = (name, structure, setup) => {
})

it('should read touched from state', () => {
const untouchedResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: empty
})
const untouchedResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: empty
}
)
expect(untouchedResult.meta.touched).toBe(false)
const touchedResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: fromJS({
touched: true
})
})
const touchedResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: fromJS({
touched: true
})
}
)
expect(touchedResult.meta.touched).toBe(true)
})

it('should read visited from state', () => {
const notVisitedResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: empty
})
const notVisitedResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: empty
}
)
expect(notVisitedResult.meta.visited).toBe(false)
const visitedResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: fromJS({
visited: true
})
})
const visitedResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: fromJS({
visited: true
})
}
)
expect(visitedResult.meta.visited).toBe(true)
})

it('should read sync errors from prop', () => {
const noErrorResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: empty
})
const noErrorResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: empty
}
)
expect(noErrorResult.meta.error).toBeFalsy()
expect(noErrorResult.meta.valid).toBe(true)
expect(noErrorResult.meta.invalid).toBe(false)
Expand All @@ -228,24 +274,36 @@ const describeCreateFieldProps = (name, structure, setup) => {
})

it('should read sync warnings from prop', () => {
const noWarningResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: empty
})
const noWarningResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: empty
}
)
expect(noWarningResult.meta.warning).toBeFalsy()
const warningResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: empty,
syncWarning: 'This is an warning'
})
const warningResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: empty,
syncWarning: 'This is an warning'
}
)
expect(warningResult.meta.warning).toBe('This is an warning')
})

it('should read async errors from state', () => {
const noErrorResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: empty
})
const noErrorResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: empty
}
)
expect(noErrorResult.meta.error).toBeFalsy()
expect(noErrorResult.meta.valid).toBe(true)
expect(noErrorResult.meta.invalid).toBe(false)
Expand All @@ -260,10 +318,14 @@ const describeCreateFieldProps = (name, structure, setup) => {
})

it('should read submit errors from state', () => {
const noErrorResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: empty
})
const noErrorResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: empty
}
)
expect(noErrorResult.meta.error).toBeFalsy()
expect(noErrorResult.meta.valid).toBe(true)
expect(noErrorResult.meta.invalid).toBe(false)
Expand All @@ -278,10 +340,14 @@ const describeCreateFieldProps = (name, structure, setup) => {
})

it('should prioritize sync errors over async or submit errors', () => {
const noErrorResult = createFieldProps({ getIn, toJS, deepEqual }, 'foo', {
value: 'bar',
state: empty
})
const noErrorResult = createFieldProps(
{ getIn, toJS, deepEqual },
'foo',
{
value: 'bar',
state: empty
}
)
expect(noErrorResult.meta.error).toBeFalsy()
expect(noErrorResult.meta.valid).toBe(true)
expect(noErrorResult.meta.invalid).toBe(false)
Expand Down Expand Up @@ -448,4 +514,4 @@ describeCreateFieldProps('createFieldProps.plain', plain, () =>
)
describeCreateFieldProps('createFieldProps.immutable', immutable, () =>
expect.extend(immutableExpectations)
)
)
Loading

0 comments on commit 1fbf964

Please sign in to comment.