Last active
August 16, 2023 06:41
-
-
Save joduplessis/0b6b10d67e69f54b12a2bef414d796f4 to your computer and use it in GitHub Desktop.
Nice easy way of iterating over radiogroup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const radioGroup = e.target.closest('*[role=radiogroup]') | |
const isDown = e.keyCode == 40 | |
const isLeft = e.keyCode == 37 | |
const isUp = e.keyCode == 38 | |
const isRight = e.keyCode == 39 | |
if (radioGroup) { | |
const children = radioGroup.querySelectorAll('[role=radio]') | |
const index = [...children].indexOf(e.target) | |
const nextIndex = index + 1 == children.length ? 0 : index + 1 | |
const previousIndex = index - 1 == -1 ? children.length - 1 : index - 1 | |
if (isRight || isDown) children[nextIndex].focus() | |
if (isLeft || isUp) children[previousIndex].focus() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment