props update trigger a reset only in relevant cases

This commit is contained in:
eric-burel 2018-11-26 14:59:57 +01:00
parent 463b954be2
commit 047181992e
2 changed files with 13 additions and 14 deletions

View file

@ -65,6 +65,13 @@ import {
import withCollectionProps from './withCollectionProps';
import { callbackProps } from './propTypes';
// props that should trigger a form reset
const RESET_PROPS = [
'collection', 'collectionName', 'typeName', 'document', 'schema', 'currentUser',
'fields', 'removeFields'
]
const compactParent = (object, path) => {
const parentPath = getParentPath(path);
@ -636,14 +643,15 @@ class SmartForm extends Component {
/*
When props change, reinitialize state
// TODO: only need to check nextProps.prefilledProps?
// TODO: see https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
When props change, reinitialize the form state
Triggered only for data related props (collection, document, currentUser etc.)
@see https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
*/
UNSAFE_componentWillReceiveProps(nextProps) {
if (!isEqual(this.props, nextProps)) {
const needReset = !!RESET_PROPS.find(prop => !isEqual(this.props[prop], nextProps[prop]))
if (needReset) {
this.setState(getInitialStateFromProps(nextProps));
}
}

View file

@ -289,11 +289,6 @@ describe('vulcan-forms/components', function() {
});
describe('Form state management', function() {
//const simulateKeyPresses = (wrapper, value) => {
// wrapper.find('input').first().simulate('change', {target:{value:'bar'}})
// return wrapper
//}
// TODO: the change callback is triggerd but `foo` becomes null instead of "bar
// so it's added to the deletedValues and not changedValues
it.skip('store typed value', function() {
@ -307,10 +302,6 @@ describe('vulcan-forms/components', function() {
console.log(wrapper.state());
expect(wrapper.state().currentValues).toEqual({foo:'bar'})
});
// TODO: store those props in a config object of the Form
const RESET_PROPS = [
'collection', 'collectionName', 'typeName', 'document', 'schema', 'currentUser'
]
it('reset state when relevant props change', function() {
const wrapper = shallowWithContext(<Form {...defaultProps} collectionName="Foos" collection={Foos} />);
wrapper.setState({ currentValues: { foo: 'bar' } })