Skip to content

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

@sebelga

Description

@sebelga

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} /> 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Team:Kibana ManagementDev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more t//enhancementNew value added to drive a business result

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions