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 lib] Reset the form whenever its "defaultValue" changes #74950

Closed
sebelga opened this issue Aug 13, 2020 · 2 comments · Fixed by #75302
Closed

[Form lib] Reset the form whenever its "defaultValue" changes #74950

sebelga opened this issue Aug 13, 2020 · 2 comments · Fixed by #75302
Labels
enhancement New value added to drive a business result Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more

Comments

@sebelga
Copy link
Contributor

sebelga commented Aug 13, 2020

We have seen in several places the benefit of being able to change the defaultValue passed to the useForm and automatically trigger a form.reset() for the consumer.

This would allow the following data flow

const [formDefaultValue, setFormDefaultValue] = useState(someInitialDefaultValue);
const { form } = useForm({ defaultValue: formDefaultValue });

const onTypeChange = (type: string) => {
  // Whenever the processor type changes, update the default value of the form
  // that will reset the form
  setFormDefaultValue(() => {
	if (type === someInitialDefaultValue?.type) {
      return someInitialDefaultValue;
    }
    // Only return the "type", the rest of the field will read the defautValue from their config.
    return { type };
  });
};

// JSX
<UseField path="type" onChange={onTypeChange} /> 
@sebelga sebelga added enhancement New value added to drive a business result Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more labels Aug 13, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/es-ui (Team:Elasticsearch UI)

@sebelga
Copy link
Contributor Author

sebelga commented Aug 18, 2020

After discussing it with @jloleysens, we decided to make it more explicit for the consumer when he wants to reset the form, and give him the possibility to provide a new defaultValue object if required.

I was worried that, with the above solution, we would be in a middle ground between a "controlled" and and "uncontrolled" component. If we automatically reset the form and update the fields value each time the defaultValue changes, then it would be a controlled component. In that case it should not be defaultValue but value. But this is not what we want as the form will never be controlled in a practical sense.

So instead of relying on the above solution, I will add a property to the form.reset() handler to pas a new defaultValue object.

const onSomeCallback = () => {
  form.reset({ defaultValue: { another: 'object' } })
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New value added to drive a business result Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants