-
Notifications
You must be signed in to change notification settings - Fork 34
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
Fixes issue #3059 - scroll to first validation error on BaseForm #3060
Conversation
src/components/BaseForm.tsx
Outdated
<FormWithTheme | ||
{...restProps} | ||
focusOnFirstError={errorFocus} | ||
error |
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.
🤔 what's error
here?
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.
I have no idea why that was there, removed.
formRef.current | ||
.querySelector(`[for="root${err.property.replace(/\./g, "_")}"]`) |
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.
formRef.current | |
.querySelector(`[for="root${err.property.replace(/\./g, "_")}"]`) | |
const fieldName = err.property.replaceAll(".", "_"); | |
formRef.current | |
.querySelector(`[for="root${fieldName}"]`) |
This could be fragile since it seems to rely on an internal RSJF naming. Maybe we could add a link to it, I can't find it in upstream source https://github.com/rjsf-team/react-jsonschema-form/blob/main/packages/bootstrap-4/src/FieldErrorTemplate/FieldErrorTemplate.tsx
return errors; | ||
}} | ||
/> | ||
); |
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.
Shall we add a test here to make sure the query selector that we use in the code is found in the page? .querySelector(`[for="root${err.property.replace(/\./g, "_")}"]`)
This way if RJSF changes the way it names its fields, the test would break
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.
Good call, added
Fixes issue 3059. When a user receives a validation error for a specific property, it will now scroll and center the label for that property in their view.
If an error is thrown for the form rather than a specific property, it will scroll to the top of the form where the full errors list appears.
simplescreenrecorder-2023-11-15_13.41.51.mp4