How to set focus on the last field added to a fieldArray #1686
-
In useFieldArray example, it would help the UX to set keyboard focus to the newly added element. How can that be done? That would be easy if I've created a simple useFieldArray example with a TODO where I think the setFocus should go. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 10 replies
-
That's actually a really good suggestion. Make sense for us to automatically focus on the appended item tho.
|
Beta Was this translation helpful? Give feedback.
-
Take a look at this CSB: https://codesandbox.io/s/react-hook-form-usefieldarray-focus-last-added-2px1i?file=/src/src/useFieldArray.ts I think we will ship this with the release of V6. |
Beta Was this translation helpful? Give feedback.
-
it does not work on 7.30.0 when using append({}, false) |
Beta Was this translation helpful? Give feedback.
-
Is there a chance that 'append' could get a callback argument in a future version? The goal would be the ability to execute code after the append takes place, and the callback would get the ref of the newly minted component passed into it so that you could focus the new item yourself. The reason I ask is that I'm building a React Native app, so the built-in focus argument on append doesn't actually focus to open the on-screen keyboard in my app. I have to run .current.focus() to focus an input in RN Or maybe an onAppend listener function that you could get from useFieldArray along with everything else. Thoughts? |
Beta Was this translation helpful? Give feedback.
-
doesn't work. I am trying to focus MUI TextField component - it is wrapped in Controller. Is there any workaround? "react-hook-form": "^7.36.1", |
Beta Was this translation helpful? Give feedback.
-
For those wondering how to disable autofocus on new fields just write append({...}, { shouldFocus: false })
|
Beta Was this translation helpful? Give feedback.
Take a look at this CSB: https://codesandbox.io/s/react-hook-form-usefieldarray-focus-last-added-2px1i?file=/src/src/useFieldArray.ts
I think we will ship this with the release of V6.