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

Commit 335f196

Browse files
committed
Strategy refactoring and deprecating createFieldClass
1 parent f18eb9a commit 335f196

File tree

4 files changed

+46
-417
lines changed

4 files changed

+46
-417
lines changed

src/components/control-component.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,20 @@ const propTypes = {
8383
component: PropTypes.any,
8484
dispatch: PropTypes.func,
8585
parser: PropTypes.func,
86-
getter: PropTypes.func,
8786
ignore: PropTypes.oneOfType([
8887
PropTypes.arrayOf(PropTypes.string),
8988
PropTypes.string,
9089
]),
9190
dynamic: PropTypes.bool,
9291
};
9392

94-
function createControlClass(customControlPropsMap = {}, defaultProps = {}) {
93+
const defaultStrategy = {
94+
get: _get,
95+
getFieldFromState,
96+
actions,
97+
};
98+
99+
function createControlClass(customControlPropsMap = {}, s = defaultStrategy) {
95100
const controlPropsMap = {
96101
...defaultControlPropsMap,
97102
...customControlPropsMap,
@@ -592,34 +597,30 @@ function createControlClass(customControlPropsMap = {}, defaultProps = {}) {
592597
Control.propTypes = propTypes;
593598

594599
Control.defaultProps = {
595-
changeAction: actions.change,
600+
changeAction: s.actions.change,
596601
updateOn: 'change',
597602
asyncValidateOn: 'blur',
598603
parser: identity,
599604
controlProps: emptyControlProps,
600-
getter: _get,
601-
getFieldFromState,
602605
ignore: [],
603606
dynamic: false,
604607
mapProps: controlPropsMap.default,
605608
component: 'input',
606-
...defaultProps,
607609
};
608610

609611
function mapStateToProps(state, props) {
610612
const {
611613
model,
612-
getter = Control.defaultProps.getter,
613614
controlProps = omit(props, Object.keys(propTypes)),
614615
} = props;
615616

616617
const modelString = getModel(model, state);
617-
const fieldValue = getFieldFromState(state, modelString)
618+
const fieldValue = s.getFieldFromState(state, modelString)
618619
|| initialFieldState;
619620

620621
return {
621622
model: modelString,
622-
modelValue: getter(state, modelString),
623+
modelValue: s.get(state, modelString),
623624
fieldValue,
624625
controlProps,
625626
};

src/components/field-component.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,11 @@ const fieldPropTypes = {
5757
componentMap: PropTypes.object,
5858
dynamic: PropTypes.bool,
5959
dispatch: PropTypes.func,
60-
getter: PropTypes.func,
6160

6261
// Calculated props
6362
fieldValue: PropTypes.object,
64-
65-
// TODO: refactor
66-
getFieldFromState: PropTypes.func,
6763
};
6864

69-
function mapStateToProps(state, props) {
70-
const {
71-
model,
72-
} = props;
73-
74-
const modelString = getModel(model, state);
75-
const fieldValue = getFieldFromState(state, modelString)
76-
|| initialFieldState;
77-
78-
return {
79-
model: modelString,
80-
fieldValue,
81-
};
82-
}
83-
8465
function getControlType(control, props, options) {
8566
const { controlPropsMap: _controlPropsMap } = options;
8667

@@ -113,7 +94,27 @@ function getControlType(control, props, options) {
11394
}
11495
}
11596

116-
function createFieldClass(customControlPropsMap = {}, defaultProps = {}) {
97+
const defaultStrategy = {
98+
Control,
99+
getFieldFromState,
100+
};
101+
102+
function createFieldClass(customControlPropsMap = {}, s = defaultStrategy) {
103+
function mapStateToProps(state, props) {
104+
const {
105+
model,
106+
} = props;
107+
108+
const modelString = getModel(model, state);
109+
const fieldValue = s.getFieldFromState(state, modelString)
110+
|| initialFieldState;
111+
112+
return {
113+
model: modelString,
114+
fieldValue,
115+
};
116+
}
117+
117118
const options = {
118119
controlPropsMap: {
119120
...controlPropsMap,
@@ -156,15 +157,15 @@ function createFieldClass(customControlPropsMap = {}, defaultProps = {}) {
156157
);
157158
}
158159

159-
return (
160-
<Control
161-
{...controlProps}
162-
control={control}
163-
controlProps={control.props}
164-
component={control.type}
165-
mapProps={mapProps}
166-
/>
167-
);
160+
return React.createElement(
161+
s.Control,
162+
{
163+
...controlProps,
164+
control,
165+
controlProps: control.props,
166+
component: control.type,
167+
mapProps,
168+
});
168169
}
169170

170171
mapChildrenToControl(children) {
@@ -217,9 +218,6 @@ function createFieldClass(customControlPropsMap = {}, defaultProps = {}) {
217218
changeAction: actions.change,
218219
dynamic: true,
219220
component: 'div',
220-
getter: _get,
221-
getFieldFromState,
222-
...defaultProps,
223221
};
224222

225223
return resolveModel(connect(mapStateToProps)(Field));

src/immutable.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,18 @@ const immutableActions = {
115115
const immutableModelReducer = createModeler(immutableStrategy);
116116
const immutableModelReducerEnhancer = createModelReducerEnhancer(immutableModelReducer);
117117
const immutableControlPropsMap = createControlPropsMap(immutableStrategy);
118-
const ImmutableField = createFieldClass(immutableControlPropsMap, {
119-
getter: immutableGetFromState,
118+
const ImmutableControl = createControlClass(immutableControlPropsMap, {
119+
get: immutableGetFromState,
120120
getFieldFromState: immutableGetFieldFromState,
121-
changeAction: immutableModelActions.change,
121+
actions: immutableModelActions,
122122
});
123-
const ImmutableErrors = createErrorsClass(immutableStrategy);
124-
const ImmutableControl = createControlClass(immutableControlPropsMap, {
123+
const ImmutableField = createFieldClass(immutableControlPropsMap, {
124+
Control: ImmutableControl,
125125
getter: immutableGetFromState,
126126
getFieldFromState: immutableGetFieldFromState,
127127
changeAction: immutableModelActions.change,
128128
});
129+
const ImmutableErrors = createErrorsClass(immutableStrategy);
129130
const ImmutableForm = createFormClass({
130131
...immutableStrategy,
131132
actions: immutableActions,

0 commit comments

Comments
 (0)