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

Commit 7d3dedb

Browse files
committed
Exporting internal utils, simplifying control component, making text fields controlled, giving all unit tests an initial state to avoid uncontrolled component warnings (will re-explore default values). Fixes #184, #169
1 parent 39f50ec commit 7d3dedb

File tree

9 files changed

+137
-77
lines changed

9 files changed

+137
-77
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"immutable": "^3.7.6",
5656
"jsdom": "^8.0.4",
5757
"mocha": "^2.4.5",
58-
"react": "^15.0.1",
58+
"react": "^15.0.2",
5959
"react-addons-test-utils": "^15.0.1",
6060
"react-dom": "^15.0.1",
6161
"react-redux": "^4.4.0",

src/components/control-component.js

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,26 @@ import { Component, createElement, PropTypes } from 'react';
22
import connect from 'react-redux/lib/components/connect';
33

44
import _get from 'lodash/get';
5-
import memoize from 'lodash/memoize';
65
import { sequenceEventActions } from '../utils/sequence';
76
import actions from '../actions';
87

9-
const createControlProps = memoize((props) => {
10-
const { model, modelValue, controlProps, mapProps } = props;
8+
function mapStateToProps(state, props) {
9+
const { model, controlProps, mapProps } = props;
10+
const modelString = typeof model === 'function'
11+
? model(state)
12+
: model;
1113

1214
if (!mapProps) {
13-
return false;
15+
return props;
1416
}
1517

1618
return mapProps({
1719
model,
18-
modelValue,
20+
modelValue: _get(state, modelString),
21+
...props,
1922
...controlProps,
2023
...sequenceEventActions(props),
2124
});
22-
});
23-
24-
function selector(state, props) {
25-
const controlProps = createControlProps(props);
26-
27-
const model = props.model;
28-
29-
const modelString = typeof model === 'function'
30-
? model(state)
31-
: model;
32-
33-
return {
34-
model: modelString,
35-
modelValue: _get(state, modelString),
36-
...controlProps,
37-
};
3825
}
3926

4027
class Control extends Component {
@@ -94,4 +81,4 @@ Control.defaultProps = {
9481
updateOn: 'change',
9582
};
9683

97-
export default connect(selector)(Control);
84+
export default connect(mapStateToProps)(Control);

src/components/field-component.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const {
1414
change,
1515
} = actions;
1616

17-
function selector(state, { model }) {
17+
function mapStateToProps(state, { model }) {
1818
const modelString = typeof model === 'function'
1919
? model(state)
2020
: model;
@@ -56,7 +56,9 @@ const controlPropsMap = {
5656
}),
5757
text: (props) => ({
5858
...props,
59-
defaultValue: props.modelValue,
59+
value: props.updateOn === 'change' && !props.defaultValue
60+
? props.modelValue
61+
: undefined,
6062
name: props.model,
6163
}),
6264
textarea: (props) => controlPropsMap.text(props),
@@ -217,7 +219,7 @@ function createFieldClass(customControlPropsMap = {}, defaultProps = {}) {
217219
...defaultProps,
218220
};
219221

220-
return connect(selector)(Field);
222+
return connect(mapStateToProps)(Field);
221223
}
222224

223225
export {

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
track,
2323
} from './utils/track';
2424

25+
import * as utils from './utils';
26+
2527
export {
2628
actions,
2729
actionTypes,
@@ -41,4 +43,5 @@ export {
4143
initialFieldState,
4244
modeled,
4345
track,
46+
utils,
4447
};

test/errors-component-spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ describe('<Errors />', () => {
1515

1616
describe('displaying errors from messages', () => {
1717
const store = applyMiddleware(thunk)(createStore)(combineReducers({
18-
testForm: formReducer('test', {}),
19-
test: modelReducer('test'),
18+
testForm: formReducer('test', { foo: '' }),
19+
test: modelReducer('test', { foo: '' }),
2020
}));
2121

2222
const form = TestUtils.renderIntoDocument(
@@ -115,8 +115,8 @@ describe('<Errors />', () => {
115115

116116
describe('displaying errors from form .errors', () => {
117117
const store = applyMiddleware(thunk)(createStore)(combineReducers({
118-
testForm: formReducer('test', {}),
119-
test: modelReducer('test'),
118+
testForm: formReducer('test', { foo: '' }),
119+
test: modelReducer('test', { foo: '' }),
120120
}));
121121

122122
let formValid = false;
@@ -161,8 +161,8 @@ describe('<Errors />', () => {
161161

162162
describe('displaying custom messages', () => {
163163
const store = applyMiddleware(thunk)(createStore)(combineReducers({
164-
testForm: formReducer('test', {}),
165-
test: modelReducer('test'),
164+
testForm: formReducer('test', { foo: '' }),
165+
test: modelReducer('test', { foo: '' }),
166166
}));
167167

168168
const form = TestUtils.renderIntoDocument(

test/field-component-spec.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,10 @@ describe('<Field /> component', () => {
516516
const reducer = formReducer('test');
517517
const store = applyMiddleware(thunk)(createStore)(combineReducers({
518518
testForm: reducer,
519-
test: modelReducer('test', {}),
519+
test: modelReducer('test', {
520+
foo: '',
521+
blur: '',
522+
}),
520523
}));
521524

522525
it('should set the proper field state for validation', () => {
@@ -1150,7 +1153,7 @@ describe('<Field /> component', () => {
11501153
});
11511154

11521155
describe('syncing control defaultValue on load', () => {
1153-
const reducer = modelReducer('test');
1156+
const reducer = modelReducer('test', { foo: '' });
11541157
const store = applyMiddleware(thunk)(createStore)(combineReducers({
11551158
test: reducer,
11561159
}));
@@ -1207,7 +1210,7 @@ describe('<Field /> component', () => {
12071210
});
12081211

12091212
describe('changeAction prop', () => {
1210-
const reducer = modelReducer('test');
1213+
const reducer = modelReducer('test', { foo: '' });
12111214
const store = applyMiddleware(thunk)(createStore)(combineReducers({
12121215
test: reducer,
12131216
}));
@@ -1244,7 +1247,7 @@ describe('<Field /> component', () => {
12441247
});
12451248

12461249
describe('event handlers on control', () => {
1247-
const reducer = modelReducer('test');
1250+
const reducer = modelReducer('test', { foo: '' });
12481251
const store = applyMiddleware(thunk)(createStore)(combineReducers({
12491252
test: reducer,
12501253
}));

test/field-parser-spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { modelReducer, formReducer, Field } from '../src';
88

99
describe('<Field parser={...} />', () => {
1010
const store = createStore(combineReducers({
11-
test: modelReducer('test'),
12-
testForm: formReducer('test'),
11+
test: modelReducer('test', { foo: '' }),
12+
testForm: formReducer('test', { foo: '' }),
1313
}));
1414

1515
const parseValue = val => ({

0 commit comments

Comments
 (0)