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

Commit 985d11a

Browse files
committed
More thunk removal. #550
1 parent fae1351 commit 985d11a

9 files changed

+186
-120
lines changed

Diff for: docs/guides/faqs.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Frequently Asked Questions
2+
3+
This list will be updated frequently!
4+
5+
### How do you add conditional class names based on field state?
6+
7+
Use the `mapProps={{...}}` property on `<Control>` components to set any props on the control component based on field state, like this:
8+
9+
```jsx
10+
<Control.text
11+
model="user.firstName"
12+
mapProps={{
13+
className: ({fieldValue}) => fieldValue.focus
14+
? 'focused'
15+
: ''
16+
}}
17+
/>
18+
```
19+
20+
The props that are provided to each function value in your `mapProps` mapping are:
21+
- `modelValue`
22+
- `fieldValue`
23+
- `onFocus`
24+
- `onBlur`
25+
- `onChange`
26+
27+
as well as other additional props:
28+
- all props on the `<Control>`
29+
- all props on the `controlProps={{...}}` prop (if any)
30+
- `onKeyPress`
31+
- `viewValue`
32+
33+
### How do you validate across fields?
34+
35+
Validation across fields becomes a higher form-level concern, which follows Redux's general tree-structure data flow pattern. If you want a reusable group of fields that are validated, you can **nest forms** as long as you set the form's `component="..."` prop to something other than `"form"` (because you can't nest forms in HTML). [See this issue response for more information.](https://github.com/davidkpiano/react-redux-form/issues/545#issuecomment-261944846) Here's an example:
36+
37+
```jsx
38+
import { Form, Control } from 'react-redux-form';
39+
40+
const isOver18 = (day, month, year) => { ... };
41+
42+
const BirthDate = ({forModel}) => (
43+
<Form
44+
model={`${forModel}.birth`}
45+
component="div"
46+
validators={{
47+
'': ({day, month, year}) => isOver18(day, month, year),
48+
}}
49+
>
50+
<Control model=".day" placeholder="Day" />
51+
<Control model=".month" placeholder="Month" />
52+
<Control model=".year" placeholder="Year" />
53+
</Form>
54+
);
55+
56+
export default BirthDate;
57+
```
58+

Diff for: docs/recipes/redux-form.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Redux Form Examples
2+
3+
These examples are implementations of the examples from [Redux-Form](http://redux-form.com/6.2.1/examples), shown for comparison.
4+
5+
### Simple Form
6+
7+
<p data-height="300" data-theme-id="13607" data-slug-hash="e384d9c541679c8141979c590ac24d80" data-default-tab="js,result" data-user="davidkpiano" data-embed-version="2" data-pen-title="RRF - Simple Form" data-editable="true" class="codepen">See the Pen <a href="http://codepen.io/davidkpiano/pen/e384d9c541679c8141979c590ac24d80/">RRF - Simple Form</a> by David Khourshid (<a href="http://codepen.io/davidkpiano">@davidkpiano</a>) on <a href="http://codepen.io">CodePen</a>.</p>
8+
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
9+
10+
### Synchronous Validation
11+
12+
<p data-height="300" data-theme-id="13607" data-slug-hash="381bb491da76e74305f5d2851644fbbf" data-default-tab="js,result" data-user="davidkpiano" data-embed-version="2" data-pen-title="RRF - Synchronous Validation" data-editable="true" class="codepen">See the Pen <a href="http://codepen.io/davidkpiano/pen/381bb491da76e74305f5d2851644fbbf/">RRF - Synchronous Validation</a> by David Khourshid (<a href="http://codepen.io/davidkpiano">@davidkpiano</a>) on <a href="http://codepen.io">CodePen</a>.</p>
13+
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
14+
15+
### Submit Validation
16+
17+
<p data-height="300" data-theme-id="13607" data-slug-hash="c683e0cf7ee54736b49b2ce30aba956f" data-default-tab="js,result" data-user="davidkpiano" data-embed-version="2" data-pen-title="RRF - Submit Validation" data-editable="true" class="codepen">See the Pen <a href="http://codepen.io/davidkpiano/pen/c683e0cf7ee54736b49b2ce30aba956f/">RRF - Submit Validation</a> by David Khourshid (<a href="http://codepen.io/davidkpiano">@davidkpiano</a>) on <a href="http://codepen.io">CodePen</a>.</p>
18+
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>

Diff for: test/custom-control-component-spec.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
import { assert } from 'chai';
33
import React, { Component, PropTypes } from 'react';
44
import TestUtils from 'react-addons-test-utils';
5-
import { applyMiddleware, combineReducers, createStore } from 'redux';
65
import { Provider } from 'react-redux';
7-
import thunk from 'redux-thunk';
86

97
import { modelReducer, formReducer, Control } from '../src';
8+
import { testCreateStore } from './utils';
109

1110
describe('custom <Control /> components', () => {
1211
class CustomText extends Component {
@@ -72,10 +71,10 @@ describe('custom <Control /> components', () => {
7271
MinifiedText.propTypes = { onChange: PropTypes.function };
7372

7473
it('should handle custom prop mappings', () => {
75-
const store = applyMiddleware(thunk)(createStore)(combineReducers({
74+
const store = testCreateStore({
7675
testForm: formReducer('test'),
7776
test: modelReducer('test', { foo: 'bar' }),
78-
}));
77+
});
7978

8079
const field = TestUtils.renderIntoDocument(
8180
<Provider store={store}>
@@ -101,10 +100,10 @@ describe('custom <Control /> components', () => {
101100
});
102101

103102
it('should handle string prop mappings', () => {
104-
const store = applyMiddleware(thunk)(createStore)(combineReducers({
103+
const store = testCreateStore({
105104
testForm: formReducer('test'),
106105
test: modelReducer('test', { foo: 'bar' }),
107-
}));
106+
});
108107

109108
const field = TestUtils.renderIntoDocument(
110109
<Provider store={store}>
@@ -127,10 +126,10 @@ describe('custom <Control /> components', () => {
127126
});
128127

129128
it('should work with minified components (no displayName)', () => {
130-
const store = applyMiddleware(thunk)(createStore)(combineReducers({
129+
const store = testCreateStore({
131130
testForm: formReducer('test'),
132131
test: modelReducer('test', { foo: 'bar' }),
133-
}));
132+
});
134133

135134
const field = TestUtils.renderIntoDocument(
136135
<Provider store={store}>
@@ -153,10 +152,10 @@ describe('custom <Control /> components', () => {
153152
});
154153

155154
it('should work with custom checkboxes', () => {
156-
const store = applyMiddleware(thunk)(createStore)(combineReducers({
155+
const store = testCreateStore({
157156
testForm: formReducer('test'),
158157
test: modelReducer('test', { foo: true }),
159-
}));
158+
});
160159

161160
const field = TestUtils.renderIntoDocument(
162161
<Provider store={store}>
@@ -177,10 +176,10 @@ describe('custom <Control /> components', () => {
177176
});
178177

179178
it('should work with custom checkboxes (multi)', () => {
180-
const store = applyMiddleware(thunk)(createStore)(combineReducers({
179+
const store = testCreateStore({
181180
testForm: formReducer('test'),
182181
test: modelReducer('test', { items: [1, 2, 3] }),
183-
}));
182+
});
184183

185184
const field = TestUtils.renderIntoDocument(
186185
<Provider store={store}>
@@ -230,10 +229,10 @@ describe('custom <Control /> components', () => {
230229
});
231230

232231
it('should pass event to asyncValidator', (done) => {
233-
const store = applyMiddleware(thunk)(createStore)(combineReducers({
232+
const store = testCreateStore({
234233
testForm: formReducer('test'),
235234
test: modelReducer('test', { foo: '' }),
236-
}));
235+
});
237236

238237
class TextInput extends React.Component {
239238
render() {
@@ -289,10 +288,10 @@ describe('custom <Control /> components', () => {
289288
});
290289

291290
it('should pass fieldValue in mapProps', () => {
292-
const store = applyMiddleware(thunk)(createStore)(combineReducers({
291+
const store = testCreateStore({
293292
testForm: formReducer('test'),
294293
test: modelReducer('test', { foo: '' }),
295-
}));
294+
});
296295

297296
class TextInput extends React.Component {
298297
render() {

0 commit comments

Comments
 (0)