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

Commit b733b5f

Browse files
committed
Do not pass default mapProps into custom components + unit test
1 parent 03e5bfb commit b733b5f

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

src/components/control-component.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ const defaultStrategy = {
112112

113113
function createControlClass(s = defaultStrategy) {
114114
const emptyControlProps = {};
115+
const emptyMapProps = {};
115116

116117
class Control extends Component {
117118
constructor(props) {
@@ -204,9 +205,11 @@ function createControlClass(s = defaultStrategy) {
204205

205206
return value;
206207
});
208+
} else if (typeof mapProps === 'function') {
209+
return mapProps(originalProps);
207210
}
208211

209-
return mapProps(originalProps);
212+
return emptyMapProps;
210213
}
211214

212215
getChangeAction(event) {
@@ -310,7 +313,7 @@ function createControlClass(s = defaultStrategy) {
310313
props: { fieldValue },
311314
} = this;
312315

313-
if (!node || !node.willValidate) {
316+
if (!node || (node && !node.willValidate)) {
314317
this.willValidate = false;
315318
return null;
316319
}
@@ -540,9 +543,10 @@ function createControlClass(s = defaultStrategy) {
540543
attachNode() {
541544
const node = findDOMNode && findDOMNode(this);
542545

543-
if (node) this.node = node;
544-
545-
this.willValidate = node.willValidate;
546+
if (node) {
547+
this.node = node;
548+
this.willValidate = node.willValidate;
549+
}
546550
}
547551

548552
validate(options) {
@@ -617,7 +621,6 @@ function createControlClass(s = defaultStrategy) {
617621
controlProps: emptyControlProps,
618622
ignore: [],
619623
dynamic: false,
620-
mapProps: controlPropsMap.default,
621624
component: 'input',
622625
withField: true,
623626
persist: false,
@@ -653,10 +656,10 @@ function createControlClass(s = defaultStrategy) {
653656
/* eslint-disable react/prop-types */
654657
const DefaultConnectedControl = (props) => (
655658
<ConnectedControl
656-
mapProps={{
657-
...controlPropsMap.default,
658-
...props.mapProps,
659-
}}
659+
mapProps={props.component
660+
? props.mapProps
661+
: controlPropsMap.default
662+
}
660663
{...omit(props, 'mapProps')}
661664
/>
662665
);

test/control-component-spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,9 @@ Object.keys(testContexts).forEach((testKey) => {
20462046
<Control
20472047
model="user.extraData"
20482048
component={ExtraData}
2049+
mapProps={{
2050+
value: ({ modelValue }) => modelValue,
2051+
}}
20492052
/>, store);
20502053

20512054
const div = TestUtils.findRenderedDOMComponentWithTag(control, 'div');

test/custom-control-component-spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,27 @@ describe('custom <Control /> components', () => {
348348

349349
assert.equal(input.className.trim(), 'touched');
350350
});
351+
352+
it('should not pass default mapped props to custom controls', (done) => {
353+
const store = testCreateStore({
354+
testForm: formReducer('test'),
355+
test: modelReducer('test', { foo: 'bar' }),
356+
});
357+
358+
const CustomControl = (props) => {
359+
assert.notProperty(props, 'onChange');
360+
done();
361+
362+
return null;
363+
};
364+
365+
TestUtils.renderIntoDocument(
366+
<Provider store={store}>
367+
<Control
368+
model="test.foo"
369+
component={CustomControl}
370+
/>
371+
</Provider>
372+
);
373+
});
351374
});

0 commit comments

Comments
 (0)