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

Commit 750cb5a

Browse files
authored
Merge pull request #584 from dave-clover/resetValidity-on-unmount
tweaks to resetValidity
2 parents 5ce4457 + 2529660 commit 750cb5a

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

src/components/control-component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function createControlClass(customControlPropsMap = {}, s = defaultStrategy) {
183183
const keys = Object.keys(validators)
184184
.concat(Object.keys(errors), this.willValidate ? validityKeys : []);
185185

186-
dispatch(actions.setValidity(model, omit(fieldValue.validity, keys)));
186+
dispatch(actions.resetValidity(model, keys));
187187
}
188188
}
189189

src/reducers/form-actions-reducer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,22 @@ export default function formActionsReducer(state, action, localPath) {
196196
case actionTypes.RESET_VALIDITY: {
197197
let validity = { ...fieldState.validity };
198198
let errors = { ...fieldState.errors };
199+
let valid;
199200

200201
if (action.omitKeys) {
201202
action.omitKeys.forEach((key) => {
202203
delete validity[key];
203204
delete errors[key];
204205
});
206+
valid = isValidityValid(validity);
205207
} else {
206208
validity = initialFieldState.validity;
207209
errors = initialFieldState.errors;
210+
valid = initialFieldState.valid;
208211
}
209212

210213
fieldUpdates = {
211-
valid: initialFieldState.valid,
214+
valid,
212215
validity,
213216
errors,
214217
};

test/control-component-spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,42 @@ Object.keys(testContexts).forEach((testKey) => {
16321632

16331633
assert.isTrue(store.getState().testForm.foo.valid);
16341634
});
1635+
1636+
it('should not clobber other non-field-specific validators', () => {
1637+
const initialState = getInitialState({});
1638+
const store = testCreateStore({
1639+
test: modelReducer('test', initialState),
1640+
testForm: formReducer('test', initialState),
1641+
});
1642+
1643+
store.dispatch(actions.setValidity('test.foo', {
1644+
validator: false,
1645+
}));
1646+
1647+
const container = document.createElement('div');
1648+
1649+
class WrappedControl extends React.Component {
1650+
componentWillUnmount() {
1651+
store.dispatch(actions.setErrors('test.foo', {}));
1652+
}
1653+
1654+
render() {
1655+
return <Control.input model="test.foo" />;
1656+
}
1657+
}
1658+
1659+
ReactDOM.render(
1660+
<Provider store={store}>
1661+
<WrappedControl />
1662+
</Provider>,
1663+
container);
1664+
1665+
assert.isFalse(store.getState().testForm.foo.validity.validator);
1666+
1667+
ReactDOM.unmountComponentAtNode(container);
1668+
1669+
assert.isUndefined(store.getState().testForm.foo.validity.validator);
1670+
});
16351671
});
16361672

16371673
describe('with <Control.reset>', () => {

test/field-component-spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,42 @@ Object.keys(testContexts).forEach((testKey) => {
19951995

19961996
assert.isTrue(store.getState().testForm.foo.valid);
19971997
});
1998+
1999+
it('should not clobber other non-field-specific validators', () => {
2000+
const initialState = {};
2001+
const store = testCreateStore({
2002+
test: modelReducer('test', initialState),
2003+
testForm: formReducer('test', initialState),
2004+
});
2005+
2006+
store.dispatch(actions.setValidity('test.foo', {
2007+
validator: false,
2008+
}));
2009+
2010+
const container = document.createElement('div');
2011+
2012+
class WrappedControl extends React.Component {
2013+
componentWillUnmount() {
2014+
store.dispatch(actions.setErrors('test.foo', {}));
2015+
}
2016+
2017+
render() {
2018+
return <Field model="test.foo" />;
2019+
}
2020+
}
2021+
2022+
ReactDOM.render(
2023+
<Provider store={store}>
2024+
<WrappedControl />
2025+
</Provider>,
2026+
container);
2027+
2028+
assert.isFalse(store.getState().testForm.foo.validity.validator);
2029+
2030+
ReactDOM.unmountComponentAtNode(container);
2031+
2032+
assert.isUndefined(store.getState().testForm.foo.validity.validator);
2033+
});
19982034
});
19992035

20002036
describe('with input type="reset"', () => {

0 commit comments

Comments
 (0)