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

Commit 9cf7f9f

Browse files
committed
Adding intent for setting model value to newest load value after reset. Fixes #533
1 parent cd8498b commit 9cf7f9f

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/components/control-component.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ function createControlClass(customControlPropsMap = {}, s = defaultStrategy) {
346346
handleIntents() {
347347
const {
348348
model,
349+
modelValue,
349350
fieldValue,
350351
fieldValue: { intents },
351352
controlProps,
@@ -383,6 +384,14 @@ function createControlClass(customControlPropsMap = {}, s = defaultStrategy) {
383384
this.validate();
384385
}
385386
return;
387+
388+
case 'load':
389+
if (!shallowEqual(modelValue, intent.value)) {
390+
dispatch(actions.clearIntents(model, intent));
391+
dispatch(actions.load(model, intent.value));
392+
}
393+
return;
394+
386395
default:
387396
return;
388397
}

src/reducers/form-actions-reducer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ const resetFieldState = (field, key) => {
2323
return i.assign(initialFieldState, {
2424
value: field.initialValue,
2525
model: field.model,
26-
intents: [{ type: 'validate' }],
26+
intents: [
27+
{ type: 'validate' },
28+
{ type: 'load', value: field.initialValue },
29+
],
2730
});
2831
}
2932

@@ -32,7 +35,10 @@ const resetFieldState = (field, key) => {
3235
return i.assign(initialFieldState, {
3336
value: field.initialValue,
3437
model: field.model,
35-
intents: [{ type: 'validate' }],
38+
intents: [
39+
{ type: 'validate' },
40+
{ type: 'load', value: field.initialValue },
41+
],
3642
});
3743
};
3844

test/control-component-spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,30 @@ Object.keys(testContexts).forEach((testKey) => {
915915
});
916916
});
917917

918+
describe('initial value after reset', () => {
919+
const initialState = getInitialState({ foo: '' });
920+
const reducer = formReducer('test');
921+
const store = testCreateStore({
922+
testForm: reducer,
923+
test: modelReducer('test', initialState),
924+
});
925+
926+
TestUtils.renderIntoDocument(
927+
<Provider store={store}>
928+
<Control.text
929+
model="test.foo"
930+
/>
931+
</Provider>
932+
);
933+
934+
it('should reset the control to the last loaded value', () => {
935+
store.dispatch(actions.load('test.foo', 'new foo'));
936+
store.dispatch(actions.reset('test.foo'));
937+
938+
assert.equal(get(store.getState().test, 'foo'), 'new foo');
939+
});
940+
});
941+
918942
describe('errors property', () => {
919943
const reducer = formReducer('test');
920944

0 commit comments

Comments
 (0)