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

Commit f3c4a50

Browse files
committed
Moving control prop mapping to the internal Control state instead of mapStateToProps.
1 parent 1813bad commit f3c4a50

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/components/control-component.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,61 @@ import { sequenceEventActions } from '../utils/sequence';
88
import actions from '../actions';
99

1010
function mapStateToProps(state, props) {
11-
const { model, controlProps, mapProps } = props;
11+
const { model, mapProps } = props;
12+
13+
if (!mapProps) return props;
14+
1215
const modelString = typeof model === 'function'
1316
? model(state)
1417
: model;
1518
const fieldValue = getFieldFromState(state, modelString);
1619

17-
if (!mapProps) {
18-
return {
19-
...props,
20-
fieldValue,
21-
};
22-
}
23-
24-
return mapProps({
20+
return {
2521
model,
2622
modelValue: _get(state, modelString),
2723
fieldValue,
28-
...props,
29-
...controlProps,
30-
...sequenceEventActions(props),
31-
});
24+
};
3225
}
3326

3427
class Control extends Component {
3528
constructor(props) {
3629
super(props);
3730

31+
const { controlProps, mapProps } = props;
32+
3833
this.handleKeyPress = this.handleKeyPress.bind(this);
3934

4035
this.state = {
4136
value: props.modelValue,
37+
mappedProps: mapProps({
38+
...props,
39+
...controlProps,
40+
...sequenceEventActions(props),
41+
}),
4242
};
4343
}
4444

4545
componentWillMount() {
46-
const { onLoad, modelValue } = this.props;
46+
const { modelValue } = this.props;
47+
const { onLoad } = this.state.mappedProps;
4748

4849
if (onLoad) {
4950
onLoad(modelValue);
5051
}
5152
}
5253

54+
componentWillReceiveProps(nextProps) {
55+
const { mapProps } = nextProps;
56+
57+
this.setState({
58+
mappedProps: mapProps({
59+
...nextProps,
60+
...nextProps.controlProps,
61+
...sequenceEventActions(nextProps),
62+
}),
63+
});
64+
}
65+
5366
componentDidUpdate(prevProps) {
5467
const {
5568
modelValue,
@@ -67,7 +80,7 @@ class Control extends Component {
6780
}
6881

6982
handleKeyPress(event) {
70-
const { onSubmit } = this.props;
83+
const { onSubmit } = this.state.mappedProps;
7184

7285
if (onSubmit && event.key === 'Enter') {
7386
onSubmit(event);
@@ -107,7 +120,7 @@ class Control extends Component {
107120
return cloneElement(
108121
control,
109122
{
110-
...this.props,
123+
...this.state.mappedProps,
111124
onKeyPress: this.handleKeyPress,
112125
});
113126
}
@@ -116,7 +129,7 @@ class Control extends Component {
116129
component,
117130
{
118131
...controlProps,
119-
...this.props,
132+
...this.state.mappedProps,
120133
onKeyPress: this.handleKeyPress,
121134
},
122135
controlProps.children);

test/field-component-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ describe('<Field /> component', () => {
15371537
</Field>
15381538
</Provider>
15391539
);
1540-
const filter = ({constructor}) =>
1540+
const filter = ({ constructor }) =>
15411541
constructor.displayName === 'Connect(Control)';
15421542
const components = TestUtils.findAllInRenderedTree(field, filter);
15431543
assert.lengthOf(components, 1, 'exactly one connected Control was rendered');

0 commit comments

Comments
 (0)