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] Allow new "defaultValue" to be provided when resetting the… #75302

Merged
merged 5 commits into from
Aug 19, 2020

Conversation

sebelga
Copy link
Contributor

@sebelga sebelga commented Aug 18, 2020

This PR adds a new defaultValue option that can be passed to the form.reset() handler.

// The form object can be accessed anywhere through context
const form = useFormContext();

const onSomeCallback = () => {
  // Any field different than "foo" will have its value reset to the
  // defaultValue defined on the FieldConfig object, and if none was provided to `""` (empty string)
  form.reset({ defaultValue: { foo: 'bar' } });
}

This should also help in fixing the first part of this comment #72849 (comment)

@alisonelizabeth @jloleysens

Fixes #74950

@sebelga sebelga added enhancement New value added to drive a business result release_note:skip Skip the PR/issue when compiling release notes Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more v7.10.0 v8.0.0 labels Aug 18, 2020
@sebelga sebelga marked this pull request as ready for review August 18, 2020 16:11
@sebelga sebelga requested a review from a team as a code owner August 18, 2020 16:11
@elasticmachine
Copy link
Contributor

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

Copy link
Contributor

@jloleysens jloleysens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really cool @sebelga . I think this gives consumers precise control of form values given any external or internal form change.

I tested with the following ingest node pipeline form changes:

patch
diff --git i/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_processors_editor/components/manage_processor_form/processors/common_fields/processor_type_field.tsx w/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_processors_editor/components/manage_processor_form/processors/common_fields/processor_type_field.tsx
index e4ad90f61af..133950896cc 100644
--- i/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_processors_editor/components/manage_processor_form/processors/common_fields/processor_type_field.tsx
+++ w/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_processors_editor/components/manage_processor_form/processors/common_fields/processor_type_field.tsx
@@ -14,6 +14,7 @@ import {
   FieldConfig,
   UseField,
   fieldValidators,
+  useFormContext,
 } from '../../../../../../../shared_imports';
 
 import { getProcessorDescriptor, mapProcessorTypeToDescriptor } from '../../../shared';
@@ -64,6 +65,7 @@ const typeConfig: FieldConfig<any, string> = {
 };
 
 export const ProcessorTypeField: FunctionComponent<Props> = ({ initialType }) => {
+  const form = useFormContext();
   return (
     <UseField<string> config={typeConfig} defaultValue={initialType} path="type">
       {(typeField) => {
@@ -123,7 +125,10 @@ export const ProcessorTypeField: FunctionComponent<Props> = ({ initialType }) =>
               onCreateOption={onCreateComboOption}
               onChange={(options: Array<EuiComboBoxOptionOption<string>>) => {
                 const [selection] = options;
-                typeField.setValue(selection?.value! ?? '');
+                const type = selection?.value! ?? '';
+                console.log('resetting form default value to', { type });
+                typeField.setValue(type);
+                form.reset({ defaultValue: { type } });
               }}
               noSuggestions={false}
               singleSelection={{

And was able to to get the desired behaviour of resetting the default values on type changes.

@sebelga
Copy link
Contributor Author

sebelga commented Aug 19, 2020

Thanks for the review @jloleysens ! I had to fix a few test (great that they caught the regression on the mappings editor! 😊

I haven't tested but I don't think you need to set the value if you reset right after, as resetting sets the value on the field.

typeField.setValue(type); // You might not need to do that
form.reset({ defaultValue: { type } });

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Build metrics

async chunks size

id value diff baseline
indexManagement 1.5MB +1.3KB 1.5MB

page load bundle size

id value diff baseline
esUiShared 988.1KB +940.0B 987.2KB

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@sebelga sebelga merged commit 0c6f682 into elastic:master Aug 19, 2020
@sebelga sebelga deleted the form-lib/add-defaultvalue-in-reset-2 branch August 19, 2020 16:08
gmmorris added a commit to gmmorris/kibana that referenced this pull request Aug 19, 2020
* master: (30 commits)
  [code coverage] always download node before team assignment (elastic#75424)
  [Form lib] Allow new "defaultValue" to be provided when resetting the… (elastic#75302)
  [Logs UI] Add "View in machine learning" links in the anomaly explorer (elastic#74555)
  skip flaky suite (elastic#75440)
  skip flaky suite (elastic#75386)
  [Saved objects] Add support for version on create & bulkCreate when overwriting a document (elastic#75172)
  [Functional]Table Vis increase sleep time in order filter to be applied (elastic#75138)
  MOAR RAM (elastic#75423)
  [Visualize] Horizontal Bar Percentiles Overlapping (elastic#75315)
  [ML] DF Analytics / Transforms: Fix job row actions menu invalid DOM nesting warning (elastic#74499)
  [ML] Inference models management (elastic#74978)
  [Monitoring] Migrate karma tests (elastic#75301)
  [Index template] Add filters to simulate preview (elastic#74497)
  Bump and consolidate dependencies (elastic#75360)
  [Ingest Manager] Fix agent config rollout rate limit to use constants (elastic#75364)
  Update Node.js to version 10.22.0 (elastic#75254)
  [ML] Anomaly Explorer / Single Metric Viewer: Fix error reporting for annotations. (elastic#74953)
  [Discover] Fix histogram cloud tests (elastic#75268)
  Uiactions to navigate to visualize or maps (elastic#74121)
  Use prefix search invis editor field/agg combo box (elastic#75290)
  ...
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 release_note:skip Skip the PR/issue when compiling release notes Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more v7.10.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Form lib] Reset the form whenever its "defaultValue" changes
4 participants