Skip to content

Instantly share code, notes, and snippets.

@joduplessis
Last active August 16, 2023 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joduplessis/0b6b10d67e69f54b12a2bef414d796f4 to your computer and use it in GitHub Desktop.
Save joduplessis/0b6b10d67e69f54b12a2bef414d796f4 to your computer and use it in GitHub Desktop.
Nice easy way of iterating over radiogroup
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