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

Commit bea932e

Browse files
committed
Make wrapper component for <Field> optional. Fixes #482
1 parent 12342d7 commit bea932e

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/components/field-component.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import omit from 'lodash/omit';
66
import isPlainObject from 'lodash/isPlainObject';
77
import pick from 'lodash/pick';
88
import { connect } from 'react-redux';
9+
import invariant from 'invariant';
910

1011
import actions from '../actions';
1112
import Control from './control-component';
@@ -177,25 +178,31 @@ function createFieldClass(customControlPropsMap = {}, defaultProps = {}) {
177178
}
178179

179180
render() {
180-
const { props } = this;
181181
const {
182182
component,
183183
children, // eslint-disable-line react/prop-types
184184
fieldValue,
185-
} = props;
185+
} = this.props;
186186

187187

188-
const allowedProps = omit(props, Object.keys(fieldPropTypes));
188+
const allowedProps = omit(this.props, Object.keys(fieldPropTypes));
189189
const renderableChildren = typeof children === 'function'
190190
? children(fieldValue)
191191
: children;
192192

193+
if (!component) {
194+
invariant(React.Children.count(renderableChildren) === 1,
195+
'Empty wrapper components for <Field> are only possible'
196+
+ 'when there is a single child. Please check the children'
197+
+ `passed into <Field model="${this.props.model}">.`);
198+
199+
return this.createControlComponent(renderableChildren);
200+
}
201+
193202
return React.createElement(
194203
component,
195204
allowedProps,
196-
React.Children.map(
197-
renderableChildren,
198-
(child) => this.createControlComponent(child)));
205+
this.mapChildrenToControl(renderableChildren));
199206
}
200207
}
201208

test/field-component-spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ Object.keys(testContexts).forEach((testKey) => {
117117
assert.ok(TestUtils.findRenderedDOMComponentWithTag(field, 'input'));
118118
});
119119

120+
it('should not wrap child components if only one child and null component', () => {
121+
const store = applyMiddleware(thunk)(createStore)(combineReducers({
122+
test: modelReducer('test', { foo: 'bar' }),
123+
testForm: formReducer('test'),
124+
}));
125+
const field = TestUtils.renderIntoDocument(
126+
<Provider store={store}>
127+
<Field model="test.foo" component={null}>
128+
<input />
129+
</Field>
130+
</Provider>
131+
);
132+
133+
assert.throws(() => TestUtils.findRenderedDOMComponentWithTag(field, 'div'));
134+
135+
assert.ok(TestUtils.findRenderedDOMComponentWithTag(field, 'input'));
136+
});
137+
120138
it('should recursively handle nested control components', () => {
121139
const store = applyMiddleware(thunk)(createStore)(combineReducers({
122140
test: modelReducer('test', { foo: 'bar' }),

0 commit comments

Comments
 (0)