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

Commit 1e75090

Browse files
committed
Merge branch 'master' of github.com:davidkpiano/react-redux-form
2 parents d20b469 + e2281c4 commit 1e75090

File tree

2 files changed

+102
-19
lines changed

2 files changed

+102
-19
lines changed

src/components/control-component.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -470,31 +470,19 @@ function createControlClass(s = defaultStrategy) {
470470
}
471471

472472
createEventHandler(eventName) {
473-
const {
474-
dispatch,
475-
model,
476-
updateOn,
477-
validateOn = updateOn,
478-
asyncValidateOn,
479-
controlProps,
480-
parser,
481-
ignore,
482-
withField,
483-
fieldValue,
484-
} = this.props;
485-
486473
const eventAction = {
487474
focus: actions.silentFocus,
488475
blur: actions.blur,
489476
}[eventName];
490477

491-
const controlEventHandler = {
492-
focus: controlProps.onFocus,
493-
blur: controlProps.onBlur,
494-
change: controlProps.onChange,
495-
}[eventName];
496-
497478
const dispatchBatchActions = (persistedEvent, forceUpdate = false) => {
479+
const {
480+
dispatch,
481+
model,
482+
updateOn,
483+
validateOn = updateOn,
484+
} = this.props;
485+
498486
const eventActions = [
499487
eventAction && eventAction(model),
500488
(forceUpdate || containsEvent(validateOn, eventName))
@@ -509,6 +497,21 @@ function createControlClass(s = defaultStrategy) {
509497
};
510498

511499
return (event, forceUpdate = false) => {
500+
const {
501+
asyncValidateOn,
502+
controlProps,
503+
parser,
504+
ignore,
505+
withField,
506+
fieldValue,
507+
} = this.props;
508+
509+
const controlEventHandler = {
510+
focus: controlProps.onFocus,
511+
blur: controlProps.onBlur,
512+
change: controlProps.onChange,
513+
}[eventName];
514+
512515
if (containsEvent(ignore, eventName)) {
513516
return controlEventHandler
514517
? controlEventHandler(event)

test/control-component-spec.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,86 @@ Object.keys(testContexts).forEach((testKey) => {
11881188
});
11891189
});
11901190

1191+
describe('dynamic model', () => {
1192+
const initialState = getInitialState({
1193+
foo: 'initial',
1194+
bar: 'initial',
1195+
});
1196+
const store = testCreateStore({
1197+
test: modelReducer('test', initialState),
1198+
testForm: formReducer('test'),
1199+
});
1200+
1201+
class DynamicModelControl extends React.Component {
1202+
constructor() {
1203+
super();
1204+
1205+
this.state = { model: 'foo' };
1206+
}
1207+
render() {
1208+
const { model } = this.state;
1209+
return (
1210+
<div>
1211+
<button onClick={() => this.setState({ model: 'bar' })} />
1212+
<Control.text model={`test.${model}`} />
1213+
</div>
1214+
);
1215+
}
1216+
}
1217+
1218+
it('updates the proper field on change', () => {
1219+
const field = TestUtils.renderIntoDocument(
1220+
<Provider store={store}>
1221+
<DynamicModelControl />
1222+
</Provider>
1223+
);
1224+
1225+
const control = TestUtils.findRenderedDOMComponentWithTag(field, 'input');
1226+
const button = TestUtils.findRenderedDOMComponentWithTag(field, 'button');
1227+
1228+
control.value = 'value1';
1229+
TestUtils.Simulate.change(control);
1230+
1231+
TestUtils.Simulate.click(button);
1232+
1233+
control.value = 'value2';
1234+
TestUtils.Simulate.change(control);
1235+
1236+
assert.equal(get(store.getState().test, 'foo'), 'value1');
1237+
assert.equal(get(store.getState().test, 'bar'), 'value2');
1238+
});
1239+
1240+
it('updates the proper form state of field on focus/blur', () => {
1241+
const field = TestUtils.renderIntoDocument(
1242+
<Provider store={store}>
1243+
<DynamicModelControl />
1244+
</Provider>
1245+
);
1246+
1247+
const control = TestUtils.findRenderedDOMComponentWithTag(field, 'input');
1248+
const button = TestUtils.findRenderedDOMComponentWithTag(field, 'button');
1249+
1250+
TestUtils.Simulate.focus(control);
1251+
assert.equal(store.getState().testForm.foo.focus, true);
1252+
assert.equal(store.getState().testForm.bar.focus, false);
1253+
1254+
TestUtils.Simulate.blur(control);
1255+
assert.equal(store.getState().testForm.foo.focus, false);
1256+
assert.equal(store.getState().testForm.bar.focus, false);
1257+
1258+
TestUtils.Simulate.click(button);
1259+
1260+
TestUtils.Simulate.focus(control);
1261+
assert.equal(store.getState().testForm.foo.focus, false,
1262+
'Second field should be focused instead, we switched the model');
1263+
assert.equal(store.getState().testForm.bar.focus, true);
1264+
1265+
TestUtils.Simulate.blur(control);
1266+
assert.equal(store.getState().testForm.foo.focus, false);
1267+
assert.equal(store.getState().testForm.bar.focus, false);
1268+
});
1269+
});
1270+
11911271
describe('updateOn prop', () => {
11921272
const onEvents = [
11931273
'change',

0 commit comments

Comments
 (0)