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

Commit 8e524cb

Browse files
committed
Resetting now resets to last actions.load. Fixes #426
1 parent 3e6dd50 commit 8e524cb

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

examples/quick-start/components/user-form.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import { connect } from 'react-redux';
44

55
const required = (val) => !!(val && val.length);
66

7+
function hasToBeTrue(value) {
8+
if (value === false || typeof value !== 'boolean') {
9+
return false;
10+
}
11+
return true;
12+
}
13+
14+
// control
15+
716
class UserForm extends React.Component {
817
constructor(props) {
918
super(props);
@@ -27,7 +36,7 @@ class UserForm extends React.Component {
2736
render() {
2837
const { forms: { user } } = this.props;
2938

30-
console.log(user.$form.valid);
39+
console.log('check me', user.checkMe && user.checkMe.valid);
3140

3241
return (
3342
<Form model="user" onSubmit={v => console.log(v)}>
@@ -54,6 +63,13 @@ class UserForm extends React.Component {
5463
<span>Plastic</span>
5564
</label>
5665
</Field>
66+
67+
<Control.checkbox
68+
model={'user.checkMe'}
69+
validators={{ hasToBeTrue }}
70+
71+
validateOn={'change'}
72+
/>
5773
<button type="submit" disabled={!user.$form.valid}>
5874
Finish registration!
5975
</button>

examples/quick-start/store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const initialUserState = {
1818
const store = createStore(combineForms({
1919
user: initialUserState,
2020
}), applyMiddleware(
21-
createLogger(),
21+
// createLogger(),
2222
thunk));
2323

2424
export default store;

src/reducers/form/change-action-reducer.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import compact from 'lodash/compact';
77
import mapValues from '../../utils/map-values';
88
import { createInitialState } from '../form-reducer';
99
import initialFieldState from '../../constants/initial-field-state';
10-
10+
import updateParentForms from '../../utils/update-parent-forms';
1111

1212
function updateFieldValue(field, action) {
1313
const { value, removeKeys, silent, model } = action;
@@ -24,7 +24,12 @@ function updateFieldValue(field, action) {
2424
return i.merge(field, changedFieldProps);
2525
}
2626

27-
if (silent) return i.set(field, 'value', value);
27+
if (silent) {
28+
return i.merge(field, {
29+
value,
30+
initialValue: value,
31+
});
32+
}
2833

2934
if (removeKeys) {
3035
const valueIsArray = Array.isArray(field.$form.value);
@@ -87,6 +92,20 @@ function updateFieldValue(field, action) {
8792
i.set(dirtyFormState, 'value', value));
8893
}
8994

95+
function getFormValue(form) {
96+
if (!form.$form) return form.initialValue;
97+
98+
const result = mapValues(form, (field, key) => {
99+
if (key === '$form') return undefined;
100+
101+
return getFormValue(field);
102+
});
103+
104+
delete result.$form;
105+
106+
return result;
107+
}
108+
90109
export default function changeActionReducer(state, action, localPath) {
91110
if (action.type !== actionTypes.CHANGE) return state;
92111

@@ -96,5 +115,18 @@ export default function changeActionReducer(state, action, localPath) {
96115

97116
if (!localPath.length) return updatedField;
98117

99-
return i.setIn(state, localPath, updatedField);
118+
const updatedState = i.setIn(state, localPath, updatedField);
119+
120+
if (action.silent) {
121+
return updateParentForms(updatedState, localPath, (form) => {
122+
const formValue = getFormValue(form);
123+
124+
return {
125+
value: formValue,
126+
initialValue: formValue,
127+
};
128+
});
129+
}
130+
131+
return updatedState;
100132
}

test/form-reducer-actions-spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe('formReducer() (V1)', () => {
5454
expectedField: {
5555
pristine: true,
5656
value: 'string',
57+
initialValue: 'string',
5758
},
5859
},
5960
{
@@ -62,6 +63,7 @@ describe('formReducer() (V1)', () => {
6263
expectedField: {
6364
pristine: true,
6465
value: 42,
66+
initialValue: 42,
6567
},
6668
},
6769
{
@@ -70,6 +72,7 @@ describe('formReducer() (V1)', () => {
7072
expectedField: {
7173
pristine: true,
7274
value: { foo: 'bar' },
75+
initialValue: { foo: 'bar' },
7376
},
7477
},
7578
],
@@ -591,4 +594,30 @@ describe('formReducer() (V1)', () => {
591594
assert.equal(resetState.meta.bar.value, '');
592595
});
593596
});
597+
598+
describe('resetting after load', () => {
599+
const reducer = formReducer('test', {
600+
foo: '',
601+
});
602+
603+
const loadedState = reducer(undefined,
604+
actions.load('test.foo', 'new initial'));
605+
606+
it('should change the initial value for the field', () => {
607+
assert.equal(loadedState.foo.initialValue, 'new initial');
608+
assert.equal(loadedState.foo.value, 'new initial');
609+
});
610+
611+
it('should change the initial value for the form', () => {
612+
assert.deepEqual(loadedState.$form.initialValue, { foo: 'new initial' });
613+
assert.deepEqual(loadedState.$form.value, { foo: 'new initial' });
614+
});
615+
616+
it('resetting a parent field should reset child fields in form', () => {
617+
const resetState = reducer(loadedState, actions.reset('test'));
618+
619+
assert.deepEqual(resetState.$form.value, { foo: 'new initial' });
620+
assert.equal(resetState.foo.value, 'new initial');
621+
});
622+
});
594623
});

0 commit comments

Comments
 (0)