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

fix: pressing arrow keys behavior on Radio buttons #1138

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tnyo43
Copy link

@tnyo43 tnyo43 commented May 25, 2023

What:

Fix a bug in the behavior of the keyboard operations on the radio buttons.

Why:

closes #1048

How:

  • updating test cases
    • increase amount of radio buttons in test situations to make the behavior easier to understand
    • fix the assertion of the test cases as it should be
  • fix the code to behave correctly

Checklist:

  • [N/A] Documentation
  • Tests
  • Ready to be merged

FYI

I know there is already another PR #1049 for this bug. However, it has past several months since it stop its activity.

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 5e9fd2b:

Sandbox Source
userEvent-dom Configuration
userEvent-react Configuration
silent-sun-kxlrgr Issue #1048
zen-feather-hx0h14 Issue #1048

Comment on lines +320 to +321
<input type="radio" name="group" value="e"/>
<input type="radio" name="group" value="f"/>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were only two available radio buttons actually (the values are "a" and "d").
I've added some other elements to make the behavior easier to understand.

@@ -28,7 +28,7 @@ const keydownBehavior: {
ArrowDown: (event, target, instance) => {
/* istanbul ignore else */
if (isElementType(target, 'input', {type: 'radio'} as const)) {
return () => walkRadio(instance, target, -1)
return () => walkRadio(instance, target, 1)
Copy link
Author

@tnyo43 tnyo43 May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the W3C specification about radio group describes, pressing the Down Arrow key behaves the same way as the Right Arrow key. "Moves focus to and checks the next radio button in the group" when the Down/Right arrow is pressed, so the direction of walkRadio should be 1.

The same is true for the Up and Left Arrow keys, so the direction should be -1.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the previous implementation is a bit too hard to understand. I have rewritten it to make it easier to understand what each process does.

  1. find the starting element
  2. get the next element according on the direction. If it's over the last / first element, go to the other end.
  3. if the element is...
    3.1. disabled, skip it and go to the next one.
    3.2. avaiable, and if it is equal to the starting element, do nothing. Otherwise, check it.

@tnyo43 tnyo43 marked this pull request as ready for review May 25, 2023 14:51
@tnyo43
Copy link
Author

tnyo43 commented Sep 2, 2023

@ph-fritsche @Gpx @kentcdodds can we merge this PR? If I can help something to merge, I will gladly cooperate with you.

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

Successfully merging this pull request may close these issues.

Radio inputs - using Arrow keys to navigate focuses incorrect inputs
1 participant