Fix: Prevent Checkbox and Radio from unexpectedly calling onBlur when focus moves within the component#8108
Fix: Prevent Checkbox and Radio from unexpectedly calling onBlur when focus moves within the component#8108uniqueeest wants to merge 3 commits intoadobe:mainfrom
Conversation
|
@snowystinger How do I pass that test? A few tests fail because of the focus on the label. |
| validationBehavior | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| }, useToggleState(props), inputRef); | ||
| let {focusableProps} = useFocusable(props, ref); |
There was a problem hiding this comment.
This is returning a tabIndex, making it focusable, as the name suggests. Any reason you've chosen useFocusable over useFocusWithin? https://react-spectrum.adobe.com/react-aria/useFocusWithin.html
There was a problem hiding this comment.
@snowystinger 8274341 Test case fails even after changing to focusWithin, is changing tabIndex the wrong approach?
There was a problem hiding this comment.
well, it's best to start with the goal, if the goal isn't to make the label focusable, then useFocusable isn't the right approach because you'd need to override part of its functionality.
Now that you're using a more appropriate hook, you can see that it's failing because focus isn't making it to the input. Since you know you haven't changed the tabIndex anymore, you can look more at the new hook and what it returns vs what you're adding as props on the label.
your new onFocus/onBlur are going to be in conflict with the properties returned by useFocusWithin. Instead of overwriting, use the properties that are passed to the hook in order to handle focus/blur.
|
Had a thought, focus occurs on mousedown, so i can prevent default for that event on the label, and it should affect focus being directed to the input Thank you for the tests btw! |
Closes #8045
✅ Pull Request Checklist:
📝 Test Instructions:
CheckboxandRadionow correctly triggeronBlurandonFocusonly when focus moves in/out of the component, not when moving internally between label and input.Tab) and mouse (click) to testfocusandblurbehavior:onBlurshould not be called when clicking from<label>to internal<input>.onBlurshould be called when focus leaves the component.should support focus ringshould support focus eventsshould not call onBlur when focus stays within labelshould call onBlur when focus leaves the label🧢 Your Project: