Skip to content

Commit

Permalink
Add a selector to get touched and visited props from state (#2859)
Browse files Browse the repository at this point in the history
* added a selector (getFormMeta) which retrieves the form.{$name}.fields slice from state

* added documentation for getFormMeta selector
  • Loading branch information
Sergey Bakin authored and erikras committed May 4, 2017
1 parent de13a80 commit fec790b
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/api/Selectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormMeta,
getFormAsyncErrors,
getFormSyncWarnings,
getFormSubmitErrors,
Expand All @@ -38,6 +39,7 @@ MyComponent = connect(
values: getFormValues('myForm')(state),
initialValues: getFormInitialValues('myForm')(state),
syncErrors: getFormSyncErrors('myForm')(state),
fields: getFormMeta('myForm')(state),
asyncErrors: getFormAsyncErrors('myForm')(state),
syncWarnings: getFormSyncWarnings('myForm')(state),
submitErrors: getFormSubmitErrors('myForm')(state),
Expand Down Expand Up @@ -67,6 +69,10 @@ MyComponent = connect(

> Returns the form synchronous validation errors.
### `getFormMeta(formName:String)` returns `(state) => formMeta:Object`

> Returns the form's fields meta data, namely `touched` and `visited`.
### `getFormAsyncErrors(formName:String)` returns `(state) => formAsyncErrors:Object`

> Returns the form asynchronous validation errors.
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/immutable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormMeta,
getFormAsyncErrors,
getFormSyncWarnings,
getFormSubmitErrors,
Expand Down Expand Up @@ -137,6 +138,9 @@ describe('immutable', () => {
it('should export getFormSyncErrors', () => {
expect(getFormSyncErrors).toExist().toBeA('function')
})
it('should export getFormMeta', () => {
expect(getFormMeta).toExist().toBeA('function')
})
it('should export getFormAsyncErrors', () => {
expect(getFormAsyncErrors).toExist().toBeA('function')
})
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormMeta,
getFormAsyncErrors,
getFormSyncWarnings,
getFormSubmitErrors,
Expand Down Expand Up @@ -141,6 +142,9 @@ describe('index', () => {
it('should export getFormSyncErrors', () => {
expect(getFormSyncErrors).toExist().toBeA('function')
})
it('should export getFormMeta', () => {
expect(getFormMeta).toExist().toBeA('function')
})
it('should export getFormAsyncErrors', () => {
expect(getFormAsyncErrors).toExist().toBeA('function')
})
Expand Down
2 changes: 2 additions & 0 deletions src/createAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import createGetFormNames from './selectors/getFormNames'
import createGetFormValues from './selectors/getFormValues'
import createGetFormInitialValues from './selectors/getFormInitialValues'
import createGetFormSyncErrors from './selectors/getFormSyncErrors'
import createGetFormMeta from './selectors/getFormMeta'
import createGetFormAsyncErrors from './selectors/getFormAsyncErrors'
import createGetFormSyncWarnings from './selectors/getFormSyncWarnings'
import createGetFormSubmitErrors from './selectors/getFormSubmitErrors'
Expand Down Expand Up @@ -40,6 +41,7 @@ const createAll = structure => ({
getFormValues: createGetFormValues(structure),
getFormInitialValues: createGetFormInitialValues(structure),
getFormSyncErrors: createGetFormSyncErrors(structure),
getFormMeta: createGetFormMeta(structure),
getFormAsyncErrors: createGetFormAsyncErrors(structure),
getFormSyncWarnings : createGetFormSyncWarnings(structure),
getFormSubmitErrors: createGetFormSubmitErrors(structure),
Expand Down
1 change: 1 addition & 0 deletions src/immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const {
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormMeta,
getFormAsyncErrors,
getFormSyncWarnings,
getFormSubmitErrors,
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const {
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormMeta,
getFormAsyncErrors,
getFormSyncWarnings,
getFormSubmitErrors,
Expand Down
85 changes: 85 additions & 0 deletions src/selectors/__tests__/getFormMeta.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import createGetFormMeta from '../getFormMeta'
import plain from '../../structure/plain'
import plainExpectations from '../../structure/plain/expectations'
import immutable from '../../structure/immutable'
import immutableExpectations from '../../structure/immutable/expectations'
import addExpectations from '../../__tests__/addExpectations'

const describeGetFormMeta = (name, structure, expect) => {
const getFormMeta = createGetFormMeta(structure)

const {fromJS, getIn} = structure

describe(name, () => {
it('should return a function', () => {
expect(createGetFormMeta('foo')).toBeA('function')
})

it('should get the form values from state', () => {
expect(getFormMeta('foo')(fromJS({
form: {
foo: {
fields: {
dog: {
visited: true,
touched: false
},
cat: {
visited: false,
touched: true
}
}
}
}
}))).toEqualMap({
dog: {
visited: true,
touched: false
},
cat: {
visited: false,
touched: true
}
})
})

it('should return undefined if there are no fields', () => {
expect(getFormMeta('foo')(fromJS({
form: {
foo: {}
}
}))).toEqual(undefined)
})

it('should use getFormState if provided', () => {
expect(getFormMeta('foo', state => getIn(state, 'someOtherSlice'))(fromJS({
someOtherSlice: {
foo: {
fields: {
dog: {
visited: true,
touched: false
},
cat: {
visited: false,
touched: true
}
}
}
}
}))).toEqualMap({
dog: {
visited: true,
touched: false
},
cat: {
visited: false,
touched: true
}
})
})
})
}

describeGetFormMeta('getFormMeta.plain', plain, addExpectations(plainExpectations))
describeGetFormMeta('getFormMeta.immutable', immutable, addExpectations(immutableExpectations))
5 changes: 5 additions & 0 deletions src/selectors/getFormMeta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const createGetFormMeta = ({ getIn }) =>
(form, getFormState = state => getIn(state, 'form')) =>
state => getIn(getFormState(state), `${form}.fields`)

export default createGetFormMeta

0 comments on commit fec790b

Please sign in to comment.