react-hook-form: Ability to scroll to the input with error
It would be nice to have some scrollToError method which would scroll to the highest input found which name matches one of the errors keys.
Currently, there is a similar behavior that happens after the form validation but there are cases when errors are set manually for some fields using setError API, for example, after server’s error response.
I believe I could implement such functionality outside RHF but I’d have to store input refs and names separately for this and it just feels like it would be much better if it was implemented in RHF.
Or perhaps, alternatively, if I could access all input refs mapped to fields names from RHF it would make it simpler to implement such scrolling functionality manually.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 48 (23 by maintainers)
Here’s one that scrolls to the top-most input:
One thing I have noticed is
errorsare not in order they are registered but alphabetical. So the first ref that you scroll to may not be the top-most. I’m not sure how one would go about doing that.Thanks @vensauro
setFocusis coming, maybe it will be helpful?@bluebill1049 awesome, much appreciated ❤️
Is anyone already working on this? I would love to work on this if it’s available.
I actually used to expose
reffor error inputs. Let me think about this, see if it’s a good idea to embed in RHF or some alternative solution.Thanks for sharing. I don’t know why, but for me,
a.scrollHeight - b.scrollHeightis the correct thing (not b-a). Maybe because my layout is RTL.Yes, this is for v7, but can be done for v6,
and i used a enhanced version of this in v7:
on v6, if using controller, you can use the focus like this:
or you can customize the input, making a shared ref and put an onFocus function on the controller, that make the scroll for the ref, you can pick the ref from the render method, or from the useController return, if you use this ref, this ref will be the html element
no one is working on it just yet, i think the idea is to expose errors object with ref. so you can do a
scrollIntoViewand thenfocus().I think we can expose the ref in the errors object, and let user to do the scroll into the view or any other other stuff.