Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form`s values does not update when change initialValues #37

Closed
npsouza opened this issue Feb 20, 2017 · 4 comments
Closed

Form`s values does not update when change initialValues #37

npsouza opened this issue Feb 20, 2017 · 4 comments

Comments

@npsouza
Copy link

npsouza commented Feb 20, 2017

Currently, on Form's componentWillReceiveProps, it does the following:

componentWillReceiveProps(nextProps) {
    if (!isEqual(this.props.initialValues, nextProps.initialValues)) {
        this.setState(prevState => ({
            values: { ...nextProps.initialValues, ...prevState.values }
        }))
    }
}

When I update initialValues prop, the values ​​are not updated.
My solution was to change the merge order of the props, like this:

componentWillReceiveProps(nextProps) {
    if (!isEqual(this.props.initialValues, nextProps.initialValues)) {
        this.setState(prevState => ({
            values: { ...prevState.values, ...nextProps.initialValues }
        }))
    }
}

What do you think?

(I'm sorry for the English, it's not my native language.)

@hnordt
Copy link
Owner

hnordt commented Feb 20, 2017

Hum, yeah, it's a bug, but that's not the correct fix.

Say for example the form mounted with the following initialValues:

{ name: 'Héliton', age: '26' }

Then the user changed age to 27:

{ name: 'Héliton', age: '27' }

For some reason, you change initialValues to:

{ name: 'Harry', age: '28' }

From my point of view, as name is not dirty, it should be changed to Harry, and age should remain 27, because it's dirty, the user changed it.

In a nutshell, when you update initialValues only pristine values should receive the update.

What do you think?

@npsouza
Copy link
Author

npsouza commented Feb 21, 2017

Yes, it makes sense. So what is the solution to this bug?

@hnordt
Copy link
Owner

hnordt commented Feb 21, 2017

Probably something like:

componentWillReceiveProps(nextProps) {
  if (isEqual(nextProps.initialValues, this.props.initialValues)) {
    return
  }
  this.setState(prevState => {
    const dirtyValues = (
      Object.keys(prevState.values)
        .filter((path) => this.isDirty(path))
        .reduce((result, path) => ({
          ...result,
          [path]: prevState.values[path],
        }), {})
    )
    return {
      values: {
        ...nextProps.initialValues, 
        ...dirtyValues,
      }
    }
  })
}

Can you test please?

@hnordt
Copy link
Owner

hnordt commented Feb 21, 2017

Please test with deep paths too, e.g. a.deep.path[0].foo.bar

@hnordt hnordt closed this as completed Jun 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants