Skip to content
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

Can anyone check why manual setFocus not working as expected #113

Closed
hhanifkhan opened this issue Jan 19, 2024 · 2 comments
Closed

Can anyone check why manual setFocus not working as expected #113

hhanifkhan opened this issue Jan 19, 2024 · 2 comments
Labels
question Further information is requested

Comments

@hhanifkhan
Copy link

hhanifkhan commented Jan 19, 2024

Describe the bug
I'm trying to focus on first button with value "btn 1" using setFocus but its not working as expected. but when i use focusSelf its working fine. Is there anything missing in this code.

To Reproduce
Steps to reproduce the behavior:

  1. Open this code link and check app2.js code(https://codesandbox.io/p/sandbox/upbeat-wiles-42j3nf)
@predikament
Copy link
Collaborator

@hhanifkhan: Hello!

In your examples you are passing a prop named focusKey to your focusable component, but you never pass this value to the useFocusable hook itself - Without this the hook cannot know what you want your focus key value to be, so it automatically generates one.

Basically you are missing this important part that I've commented next to in this example:

const CoolButton = ({focusKey}) => {
  const {focused, ref} = useFocusable({
    focusKey: 'MY_BUTTONS_FOCUS_KEY' // <-- Add this, hardcode a value to check, or use your focusKey prop!
  });

  return <button ref={ref} style={{background: focused ? '#f0f' : '#0f0'}}>I'm a button!</>;
};

Now that you've done this the hook will know of this focus key and doing this will work as you expect:

setFocus('MY_BUTTONS_FOCUS_KEY');

Best of luck to you!

Closing this as it's not an issue.

@predikament predikament added the question Further information is requested label Jan 23, 2024
@hhanifkhan
Copy link
Author

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants