-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Unable to set focus to React ComboBox component #5299
Comments
I did find a way around this. You can add the autoFocus property to the ComboBox. I had to tweak the code a bit. Something like the following:
Feels like a hack though, and triggers a11y warnings in eslint (though I'm not sure setting focus programmatically is any better in that regard). Thoughts? |
I believe now that we have removed the focus trap dependency we should see the expected behavior (ref #5260) |
I don't believe this has anything to do with focus trap. I want to be able to get a ref to the underlying input element so I can set focus. Am I missing something? |
to my understanding it has always been possible to use refs to access the input (e.g. https://ibm.biz/BdqrgR) the part about the focus trap external dependency was related to focusing on the input, since that library appeared to be stealing focus |
I can't open that link, as I don't have permission or any way to create an account (so far as I can see). However, if you look at the source you will see that the ref to that input is a private class member and not something that I can pass in. What should be happening is that the component should be wrapped in a call to If you utilize the Let me know if that doesn't make sense. Thanks! |
@supermonkeybrainz Are you an IBMer? If so, feel free to write something to our Slack channel and we'll give you the link. |
@supermonkeybrainz converted to code sandbox https://codesandbox.io/s/sweet-bush-kkb65 |
@asudoh I am not an IBM-er, just an interested user of carbon. :) @emyarod Thanks for the codesandbox, that clears up what you are trying to say. A creative solution, to be sure. :D I think the |
Oh wait, that still doesn't fix my issue. I need to set focus to the input programmatically, not handle events from the input. Since I only can access the input through the EDIT: See my original post for the example situation I am dealing with. |
@supermonkeybrainz You can set a |
I will go that route, thanks! |
@supermonkeybrainz the sandbox was meant to show ref usage, here it is modified for setting focus https://codesandbox.io/s/agitated-tree-tnioe as a workaround |
Got it, thanks for the example! |
Closing this since it seems the issue has been addressed. Feel free to let me know if that's not the case! |
What package(s) are you using?
carbon-components
carbon-components-react
Summary
Generally with React we would set focus on a component by getting a
ref
to the component and calling thefocus
function on the current ref. Unfortunately, there is noforwardRef
and we have no way to get aref
to theinput
within theComboBox
. I can't seem to figure out a way to inject anything in via props. Is there some way to make this happen?Relevant information
An example of what I am trying to do:
Basically, when the user clicks a Button the ComboBox will show with the dropdown portion visible. I would also like the text input to have focus so the user can start typing right away. You can see I'm using a
setTimeout
to manually wait for the box to appear so I can set focus to it. Would love some other more reliable method. Suggestions?The text was updated successfully, but these errors were encountered: