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

Commit 075f930

Browse files
committed
Added fieldValue to mapped props when withFieldValue provided to Control
1 parent 21652dd commit 075f930

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/components/control-component.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ function createControlClass(s = defaultStrategy) {
608608
component,
609609
control,
610610
getRef,
611+
fieldValue,
611612
} = this.props;
612613

613614
const mappedProps = omit(this.getMappedProps(), disallowedProps);
@@ -616,6 +617,10 @@ function createControlClass(s = defaultStrategy) {
616617
mappedProps.getRef = getRef;
617618
}
618619

620+
if (controlProps.withFieldValue) {
621+
mappedProps.fieldValue = fieldValue;
622+
}
623+
619624
// If there is an existing control, clone it
620625
if (control) {
621626
return cloneElement(

test/custom-control-component-spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,60 @@ describe('custom <Control /> components', () => {
355355
assert.equal(input.className.trim(), 'touched');
356356
});
357357

358+
it('should pass the fieldValue object when withFieldValue is true', () => {
359+
const store = testCreateStore({
360+
testForm: formReducer('test'),
361+
test: modelReducer('test', { foo: '' }),
362+
});
363+
364+
class TextInput extends React.Component {
365+
render() {
366+
const { fieldValue, ...otherProps } = this.props;
367+
const className = [
368+
fieldValue.focus ? 'focus' : '',
369+
fieldValue.touched ? 'touched' : '',
370+
].join(' ');
371+
372+
return (
373+
<div>
374+
<input
375+
className={className}
376+
{...otherProps}
377+
onChange={this.props.onChangeText}
378+
/>
379+
</div>
380+
);
381+
}
382+
}
383+
384+
TextInput.propTypes = {
385+
onChangeText: PropTypes.func,
386+
focus: PropTypes.bool,
387+
touched: PropTypes.bool,
388+
fieldValue: PropTypes.object,
389+
};
390+
391+
const field = TestUtils.renderIntoDocument(
392+
<Provider store={store}>
393+
<Control
394+
model="test.foo"
395+
component={TextInput}
396+
withFieldValue
397+
/>
398+
</Provider>
399+
);
400+
401+
const input = TestUtils.findRenderedDOMComponentWithTag(field, 'input');
402+
403+
TestUtils.Simulate.focus(input);
404+
405+
assert.equal(input.className.trim(), 'focus');
406+
407+
TestUtils.Simulate.blur(input);
408+
409+
assert.equal(input.className.trim(), 'touched');
410+
});
411+
358412
it('should not pass default mapped props to custom controls', (done) => {
359413
const store = testCreateStore({
360414
testForm: formReducer('test'),

0 commit comments

Comments
 (0)