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

Allow setValues to extend rather than replace #887

Open
esbenp opened this issue Sep 9, 2018 · 7 comments
Open

Allow setValues to extend rather than replace #887

esbenp opened this issue Sep 9, 2018 · 7 comments

Comments

@esbenp
Copy link

esbenp commented Sep 9, 2018

Current Behavior

Currently setValues will replace all current form state

Desired Behavior

A flag to change that behaviour to simply extend current values would be nice

Suggested Solution

I propose setValues signature is changed to setValues: (fields: { [field: string]: any }, replace?: bool = true) => void. Passing false would extend current values rather than replace it (ala values ={...currentValues, ...newValues}

Who does this impact? Who is this for?

The scenario I have experienced is sometimes I want a subform to override the values of the parent form. Therefore, I pass down setValues to the child that might override the values. However, I do not wish for the child to be unnecessarily aware of all parent values, so passing down values to do something like setValues({ ...values, [key]: newValue }) seems a bit much. In that case I could do setValues({ [key]: newValue }, false) instead. That way I do not have to worry about passing down the entire form's values nor extending setValues behaviour.

Describe alternatives you've considered

Right now I am passing down setValues={(childValues) => setValues({ ...values, ...childValues }) to the child instead of just passing setValues. It works fine, but it gets a bit cumbersome because I have to extend setValues this way everywhere where I need this behaviour.

Additional context

Other issue: #356

@esbenp
Copy link
Author

esbenp commented Sep 9, 2018

Simple change suggestion in Formik.jsx:

setValues = (input: FormikValues, replace?: bool = true) => {
  const values = replace ? input : {...this.state.value, ...input}

  this.setState({ values }, () => {
    if (this.props.validateOnChange) {
    this.runValidations(values);
    }
  });
};

If the maintainers wish for this change to happen I can provide PR and test

@stale
Copy link

stale bot commented Nov 8, 2018

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

@stale stale bot added the stale label Nov 8, 2018
@stale
Copy link

stale bot commented Nov 15, 2018

ProBot automatically closed this due to inactivity. Holler if this is a mistake, and we'll re-open it.

@stale stale bot closed this as completed Nov 15, 2018
@BerekHalfhand
Copy link

Open up! Make it work like setState, please

@AdnanTheExcellent
Copy link

+1 for this feature.

@johnrom
Copy link
Collaborator

johnrom commented Jun 8, 2019

I'm reopening because there is actually movement here. See:

#1513
#1255

@johnrom johnrom reopened this Jun 8, 2019
@stale stale bot removed the stale label Jun 8, 2019
@johnrom johnrom added this to the v2.0 milestone Jun 8, 2019
@viniciusdacal
Copy link
Contributor

any updates on this? I could try to implement this, making it work like setState() as proposed on #1513

@stale stale bot added the stale label Sep 24, 2019
@jaredpalmer jaredpalmer removed this from the v2.0.1 milestone Sep 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants