Skip to content

Commit

Permalink
Add initial value selector (#2460)
Browse files Browse the repository at this point in the history
* Add initial value selector

* Fixup
  • Loading branch information
griffinmichl authored and erikras committed Jan 20, 2017
1 parent d3f5146 commit fc363d7
Show file tree
Hide file tree
Showing 8 changed files with 76 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 @@ -18,6 +18,7 @@ defaults to `state => state.form`, assuming that you have mounted the `redux-for
```js
import {
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormAsyncErrors,
getFormSubmitErrors,
Expand All @@ -34,6 +35,7 @@ import {
MyComponent = connect(
state => ({
values: getFormValues('myForm')(state),
initialValues: getFormInitialValues('myForm')(state),
syncErrors: getFormSyncErrors('myForm')(state),
asyncErrors: getFormAsyncErrors('myForm')(state),
submitErrors: getFormSubmitErrors('myForm')(state),
Expand All @@ -55,6 +57,10 @@ MyComponent = connect(

> Gets the form values. Shocking, right?
### `getFormInitialValues(formName:String)` returns `(state) => formInitialValues:Object`

> Gets the form's initial values.
### `getFormSyncErrors(formName:String)` returns `(state) => formSyncErrors:Object`

> Returns the form synchronous 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 @@ -25,6 +25,7 @@ import {
focus,
formValueSelector,
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormAsyncErrors,
getFormSubmitErrors,
Expand Down Expand Up @@ -125,6 +126,9 @@ describe('immutable', () => {
it('should export getFormValues', () => {
expect(getFormValues).toExist().toBeA('function')
})
it('should export getFormInitialValues', () => {
expect(getFormInitialValues).toExist().toBeA('function')
})
it('should export getFormSyncErrors', () => {
expect(getFormSyncErrors).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 @@ -25,6 +25,7 @@ import {
focus,
formValueSelector,
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormAsyncErrors,
getFormSubmitErrors,
Expand Down Expand Up @@ -125,6 +126,9 @@ describe('index', () => {
it('should export getFormValues', () => {
expect(getFormValues).toExist().toBeA('function')
})
it('should export getFormInitialValues', () => {
expect(getFormInitialValues).toExist().toBeA('function')
})
it('should export getFormSyncErrors', () => {
expect(getFormSyncErrors).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 @@ -6,6 +6,7 @@ import createFieldArray from './FieldArray'
import createFormValueSelector from './formValueSelector'
import createValues from './values'
import createGetFormValues from './selectors/getFormValues'
import createGetFormInitialValues from './selectors/getFormInitialValues'
import createGetFormSyncErrors from './selectors/getFormSyncErrors'
import createGetFormAsyncErrors from './selectors/getFormAsyncErrors'
import createGetFormSubmitErrors from './selectors/getFormSubmitErrors'
Expand Down Expand Up @@ -34,6 +35,7 @@ const createAll = structure => ({
FormSection,
formValueSelector: createFormValueSelector(structure),
getFormValues: createGetFormValues(structure),
getFormInitialValues: createGetFormInitialValues(structure),
getFormSyncErrors: createGetFormSyncErrors(structure),
getFormAsyncErrors: createGetFormAsyncErrors(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 @@ -25,6 +25,7 @@ export const {
focus,
formValueSelector,
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormAsyncErrors,
getFormSubmitErrors,
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const {
focus,
formValueSelector,
getFormValues,
getFormInitialValues,
getFormSyncErrors,
getFormAsyncErrors,
getFormSubmitErrors,
Expand Down
53 changes: 53 additions & 0 deletions src/selectors/__tests__/getFormInitialValues.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import createGetFormInitialValues from '../getFormInitialValues'
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 describeGetFormInitialValues = (name, structure, expect) => {
const getFormInitialValues = createGetFormInitialValues(structure)

const { fromJS, getIn } = structure

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

it('should get the initial form values from state', () => {
expect(getFormInitialValues('foo')(fromJS({
form: {
foo: {
initial: {
dog: 'Snoopy',
cat: 'Garfield'
}
}
}
}))).toEqualMap({
dog: 'Snoopy',
cat: 'Garfield'
})
})

it('should use getFormState if provided', () => {
expect(getFormInitialValues('foo', state => getIn(state, 'someOtherSlice'))(fromJS({
someOtherSlice: {
foo: {
initial: {
dog: 'Snoopy',
cat: 'Garfield'
}
}
}
}))).toEqualMap({
dog: 'Snoopy',
cat: 'Garfield'
})
})
})
}

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

export default createGetFormInitialValues

0 comments on commit fc363d7

Please sign in to comment.