-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Form lib] Add "updateFieldValues" to the form hook API #130544
Conversation
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]); |
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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(() => { |
There was a problem hiding this comment.
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
}
});
@@ -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 () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
test('should update field values and discard unknwon fields provided', async () => { | |
test('should update field values and discard unknown fields provided', async () => { |
There was a problem hiding this 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 💯
Thanks for the review @yuliacech ! |
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.