-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Using a custom widget causes re-render and loses focus #4299
Comments
@macintushar Without looking at the code of your widget we can't help you. I'm guessing that having the field rerender itself is the cause of the issue. React is really picky about how rerenders work especially around focus/blur behaviors and how callbacks work. I would focus on that first |
Hey @heath-freenome, this is the code i'm using. It's from the docs.
Screen.Recording.2024-09-15.at.6.46.31.PM.mov |
Hey @heath-freenome, did you get some time to check out the issue? ^ |
Hey @heath-freenome, could you please check this out? ^ |
React can be picky about rendering when props change. The onChange is not a
|
Hey @heath-freenome, I tested out the code you gave me with some minor changes to make it work properly (adding deps to useCallback), it seems to be working fine only if I use autoFocus as a prop. Please check out the two videos below to see what I mean: Code v1:
Output for v1 Screen.Recording.2024-10-03.at.1.35.11.PM.movCode for v2
Output for v2 Screen.Recording.2024-10-03.at.1.35.54.PM.movThe diff between the two is only the autoFocus prop. It seems to loose focus anytime there is a change and the only reason it's working is because of the autoFocus. Is this expected behaviour? Is there a way to get this to work without using the autoFocus prop? |
@heath-freenome noticed another issue. If i use a Textarea instead of an input, it results in some pretty weird behavior. It sends my cursor to the first character position every time i type. Please check out the video below Screen.Recording.2024-10-03.at.4.18.56.PM.movCode:
|
@macintushar Everything you are describing lends me to believe that something in how you are wrapping the |
@heath-freenome this is for something on the Multiwoven platform. It is open source but it's difficult for me to run this on a codesanbox. I'm not sure how i'd be able to show you this occurring without me getting on a call or something with you. You can run our code locally to try and emulate my steps if required. |
I don't see any issues in the console w.r.t. React or me breaking the Rules of React with my code change. Please let me know how we can proceed further. |
@heath-freenome All I see is this in the console which seems related to RJSF. The ID error was due to something I was testing but the other two still remain. Screen.Recording.2024-10-03.at.11.01.04.PM.mov |
@macintushar without a reproducible test case I'm not sure how to help you. Why do you need the custom widget anyway? From your demo it doesn't like the code does anything special. If you are just needing to inject an onChange handler or tweak props, then rather than build a whole component, you can wrap one of the OOTB |
@macintushar another wild guess is that you might be creating your widget inside of a component, which is re-created on each render. But like @heath-freenome said, without something like a CodeSandbox, we can't do much |
Prerequisites
What theme are you using?
chakra-ui
Version
5.x
Current Behavior
I have added a custom widget. When I type in it in the form, it re-renders the field, which causes it to lose focus on the input field and moves the cursor's current position to the beginning of the input field. Note: If autofocus is not present in the input props, it completely loses focus and I can't type at all.
Screen.Recording.2024-09-12.at.12.03.24.PM.mov
Expected Behavior
Ideally, it should work the same way the default input function works. It shouldn't lose focus and shouldn't rerender.
Steps To Reproduce
Environment
Anything else?
I followed the code written in the docs here
https://rjsf-team.github.io/react-jsonschema-form/docs/advanced-customization/custom-widgets-fields
The text was updated successfully, but these errors were encountered: