Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 69a49a4

Browse files
committed
Merge remote-tracking branch 'davidkpiano/master'
2 parents 622ab41 + fb01932 commit 69a49a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2564
-1936
lines changed

.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"plugins": [
3-
"lodash"
3+
"lodash",
4+
["transform-react-remove-prop-types", {"mode": "wrap"}]
45
],
56
"presets": ["es2015", "react", "stage-2"]
67
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Join the chat at https://gitter.im/react-redux-form/Lobby](https://badges.gitter.im/react-redux-form/Lobby.svg)](https://gitter.im/react-redux-form/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
44
[![Build Status](https://travis-ci.org/davidkpiano/react-redux-form.svg?branch=master)](https://travis-ci.org/davidkpiano/react-redux-form) [![npm version](https://badge.fury.io/js/react-redux-form.svg)](https://badge.fury.io/js/react-redux-form)
55

6-
## [🆕 Read the Documentation](https://davidkpiano.github.io/react-redux-form)
6+
## [🆕 Read the Documentation](https://davidkpiano.github.io/react-redux-form/docs.html)
77

88
Or, if you're using an old version, [read the v0.14.x documentation](https://davidkpiano.gitbooks.io/react-redux-form/content/)
99

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [modelReducer](docs/api/modelReducer.md)
1919
* [formReducer](docs/api/formReducer.md)
2020
* [combineForms](docs/api/combineForms.md)
21+
* [createForms](docs/api/createForms.md)
2122
* [action creators](docs/api/actions.md)
2223
* [Recipes and Examples](docs/recipes/README.md)
2324
* [Quick Start](docs/recipes/quickstart.md)

docs/api/Form.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,6 @@ _(Any)_ The `component` that the `<Form>` should be rendered to (default: `"form
146146

147147
### Notes
148148
- For React Native, the `View` component is used to render the form, if you `import { Form } from 'react-redux-form/native'`.
149+
- In HTML, you are not allowed to nest forms. If you do want to nest forms, you will have to do one of the following:
150+
- If you want a nested form that doesn't submit, you can set the nested form's component to something other than `'form'`, like `<Form component="div" ...>`
151+
- If you _do_ want a "form" inside a form that does submit, you'll have to set the component and submit manually by dispatching `actions.validSubmit(model, promise)`.

docs/api/actions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ The "touched" state indicates that this model has been interacted with.
368368

369369
<h2 id="actions-setUntouched"></h2>
370370
## `actions.setUntouched(model)`
371-
Returns an action that, when handled by a [`formReducer`](TODO), changes the `.touched` state to `true`.
371+
Returns an action that, when handled by a [`formReducer`](TODO), changes the `.touched` state to `false`.
372372

373373
An "untouched" field indicates that this model has not been interacted with yet.
374374

docs/api/combineForms.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `combineForms(forms, [model], [options])`
22

3-
Similar to [`combineReducers()`](TODO) in `redux`, the `combineForms()` helper function takes a `forms` object where:
3+
Similar to [`combineReducers()`](http://redux.js.org/docs/api/combineReducers.html) in `redux`, the `combineForms()` helper function takes a `forms` object where:
44

55
- each key is a string model path
66
- each value is either:
@@ -10,7 +10,7 @@ Similar to [`combineReducers()`](TODO) in `redux`, the `combineForms()` helper f
1010
and turns it into a single reducing function (using `combineReducers()` internally) where essentially:
1111

1212
- each key/value pair is a `modelReducer()`
13-
- a `'forms'` key on the same object is a single `formReducer()` that handles all forms for all models.
13+
- a `'forms'` key on the same object is a single `formReducer()` that handles all forms for all models. (configurable in `options`)
1414

1515
### Arguments
1616

docs/api/createForms.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# `createForms(forms, [model], [options])`
2+
3+
```jsx
4+
import { combineReducers } from 'redux';
5+
import { createForms } from 'react-redux-form';
6+
7+
const initialUserState = {};
8+
const initialGoatState = {};
9+
10+
const reducer = combineReducers({
11+
foo: fooReducer,
12+
bar: barReducer,
13+
...createForms({
14+
user: initialUserState,
15+
goat: initialGoatState,
16+
}),
17+
});
18+
19+
// reducer state shape will be:
20+
// {
21+
// foo: fooReducer,
22+
// bar: barReducer,
23+
// user: modelReducer('user', initialUserState),
24+
// goat: modelReducer('goat', initialGoatState),
25+
// forms: formReducer(''),
26+
// }
27+
```
28+
29+
The `createForms()` helper function takes a `forms` object where:
30+
31+
- each key is a string model path
32+
- each value is either:
33+
- _(Any)_ an initial state, or
34+
- _(Function)_ an existing reducer
35+
36+
and turns it into an **object** where:
37+
38+
- each key/value pair is a `modelReducer()`
39+
- a `'forms'` key on the same object is a single `formReducer()` that handles all forms for all models. (configurable in `options`)
40+
41+
### Arguments
42+
43+
1. `forms` _(Object)_: An object whose keys correspond to relative string model paths and whose values correspond to:
44+
- _(Any)_ an initial state for the model, or
45+
- _(Function)_ an existing reducer for the model.
46+
2. `[model = '']` _(String)_: The string representation of the parent model containing the child models in the `forms` object. Defaults to an empty string.
47+
3. `[options]` _(Object)_: An options object to override the default options for `combineForms()`:
48+
49+
- `key` _(String)_: The single `formReducer` key. Defaults to `'forms'`.
50+
- `plugins` _(Array<Function>)_: An array of plugins to pass into the `formReducer()`. Defaults to no plugins.
51+
52+
### Returns
53+
54+
_(Object)_: An object that is the mapping of each reducer or initial state to a `modelReducer()` and a `.forms` prop that contains the `formReducer`.
55+
56+
### Notes
57+
58+
- The `createForms()` function is meant to be used with [`combineReducers()`](http://redux.js.org/docs/api/combineReducers.html) from `redux`. Use it when you want a flatter combined reducer structure.

examples/immutable/components/submit-button.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ SubmitButton.propTypes = {
1111
user: React.PropTypes.instanceOf(Immutable.Map).isRequired,
1212
};
1313

14-
const mapStateToProps = ({ user }) => ({ user });
14+
const mapStateToProps = (state) => {
15+
// Enable one of the two:
16+
return { user: state.user }; // Enable when using redux
17+
// return { user: state.get('user') }; // Enable when using redux-immutable
18+
};
1519

1620
export default connect(mapStateToProps)(SubmitButton);
1721

examples/immutable/components/user-form.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import React from 'react';
22
import { connect } from 'react-redux';
3-
import { Field, Form, actions } from 'react-redux-form/immutable';
3+
import Immutable from 'immutable';
4+
import { Field, Form, Errors, actions } from 'react-redux-form/immutable';
45

56
import SubmitButton from './submit-button';
67

78

9+
const isRequired = (val) => val && val.length > 0;
10+
const lessThan10 = (val) => {
11+
const lessThan = 10;
12+
if (!(val < 10)) {
13+
return { lessThan };
14+
}
15+
return false;
16+
};
17+
818
class UserForm extends React.Component {
919
constructor(props) {
1020
super(props);
@@ -20,7 +30,7 @@ class UserForm extends React.Component {
2030
// etc.
2131
const somePromise = new Promise((resolve) => {
2232
/* eslint-disable no-console */
23-
console.log(user);
33+
console.log(user.toJS());
2434
/* eslint-enable no-console */
2535
setTimeout(() => { resolve(true); }, 1000);
2636
});
@@ -33,19 +43,43 @@ class UserForm extends React.Component {
3343
model="user"
3444
onSubmit={this.handleSubmit}
3545
>
36-
<Field model="user.firstName">
46+
<Field model="user.firstName" validators={{ isRequired }}>
3747
<label>First name: </label>
3848
<input type="text" />
49+
<Errors
50+
wrapper="span"
51+
show={{ touched: true, focus: false }}
52+
model="user.firstName"
53+
messages={{
54+
isRequired: 'Please provide a first name.',
55+
}}
56+
/>
3957
</Field>
4058

41-
<Field model="user.lastName">
59+
<Field model="user.lastName" validators={{ isRequired }}>
4260
<label>Last name: </label>
4361
<input type="text" />
62+
<Errors
63+
wrapper="span"
64+
show={{ touched: true, focus: false }}
65+
model="user.lastName"
66+
messages={{
67+
isRequired: 'Please provide a last name.',
68+
}}
69+
/>
4470
</Field>
4571

46-
<Field model="user.dob">
47-
<label>A number: </label>
72+
<Field model="user.dob" errors={{ lessThan10 }} validateOn="change">
73+
<label>A number less than 10: </label>
4874
<input type="number" />
75+
<Errors
76+
wrapper="span"
77+
show={{ pristine: false }}
78+
model="user.dob"
79+
messages={{
80+
lessThan10: (value, { lessThan }) => `Error: ${value} is not less than ${lessThan}`,
81+
}}
82+
/>
4983
</Field>
5084

5185
<SubmitButton />

examples/immutable/store.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// ./store.js
22
import {
33
createStore,
4-
combineReducers,
54
applyMiddleware,
65
} from 'redux';
7-
import {
8-
modelReducer,
9-
formReducer,
10-
} from 'react-redux-form/immutable';
6+
7+
// Enable one of the two:
8+
import { combineReducers } from 'redux';
9+
//import { combineReducers } from 'redux-immutable';
10+
11+
import { createForms } from 'react-redux-form/immutable';
1112
import thunk from 'redux-thunk';
1213
import Immutable from 'immutable';
1314

@@ -18,8 +19,9 @@ const initialUserState = Immutable.fromJS({
1819
});
1920

2021
const store = applyMiddleware(thunk)(createStore)(combineReducers({
21-
user: modelReducer('user', initialUserState),
22-
userForm: formReducer('user', initialUserState),
22+
...createForms({
23+
user: initialUserState,
24+
})
2325
}));
2426

2527
export default store;

0 commit comments

Comments
 (0)