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] Add "updateFieldValues" to the form hook API #130544

Merged
merged 2 commits into from
Apr 20, 2022

Conversation

sebelga
Copy link
Contributor

@sebelga sebelga commented Apr 19, 2022

Fixes #128458

Summary

This PR adds a new form.updateFieldValues() handler to update multiple field values at once. See the linked issue for context.

How to test

The changes in this PR are included in the current storybook (draft) work (#127898). You can pull that branch locally and start storybook as indicated in the PR description.
In Storybook, navigate to the "UseArray > Dynamic data" story.

@sebelga sebelga requested a review from a team as a code owner April 19, 2022 10:54
@sebelga sebelga requested a review from yuliacech April 19, 2022 10:54
@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 release_note:skip Skip the PR/issue when compiling release notes Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) v8.3.0 labels Apr 19, 2022
@elasticmachine
Copy link
Contributor

Pinging @elastic/platform-deployment-management (Team:Deployment Management)


// Create an internal hook field which behaves like any other form field except that it is not
// outputed in the form data (when calling form.submit() or form.getFormData())
// This allow us to run custom validations (passed to the props) on the Array items

const internalFieldPath = useMemo(() => `${path}__${uuid.v4()}`, [path]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need a predictable field name to be able to update the field values in updateFieldValues()

});
});

test('should be updated with the UseField "defaultValue" prop', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test is not related to the new API change but I realised it was missing so I added it 😊

@@ -578,16 +659,6 @@ export function useForm<T extends FormData = FormData, I extends FormData = T>(
// ----------------------------------
// -- EFFECTS
// ----------------------------------

useEffect(() => {
Copy link
Contributor Author

@sebelga sebelga Apr 19, 2022

Choose a reason for hiding this comment

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

That was a bug. We don't want the defaultValue internal ref to be updated when the defaultValue option changes

const { form } = useForm({
  defaultValue: {
    ... // this object will always be a new reference when the component re-renders
  }
});

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
esUiShared 129.7KB 130.3KB +595.0B

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

@@ -653,4 +689,321 @@ describe('useForm() hook', () => {
expect(errors).toEqual(['Field1 can not be empty', 'Field2 is invalid']);
});
});

describe('form.updateFieldValues()', () => {
test('should update field values and discard unknwon fields provided', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

typo

Suggested change
test('should update field values and discard unknwon fields provided', async () => {
test('should update field values and discard unknown fields provided', async () => {

Copy link
Contributor

@yuliacech yuliacech left a comment

Choose a reason for hiding this comment

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

The new function looks great, @sebelga!
Tested in the storybook branch and all works as expected. A special shoutout to your impressive test coverage for the new functionality 💯

@sebelga
Copy link
Contributor Author

sebelga commented Apr 20, 2022

Thanks for the review @yuliacech !

@sebelga sebelga merged commit 36a4c99 into elastic:main Apr 20, 2022
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Apr 20, 2022
@sebelga sebelga deleted the form_lib/add-updateFieldValues-handler branch April 20, 2022 08:46
kertal pushed a commit to kertal/kibana that referenced this pull request May 24, 2022
@nkhristinin nkhristinin mentioned this pull request May 25, 2022
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting 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 Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) v8.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Form lib] Expose method to update multiple field values
5 participants