Refactor accessibility code

This commit is contained in:
Justin Edmund 2023-06-16 16:18:44 -07:00
parent 3693124043
commit 1d06303b93
2 changed files with 19 additions and 39 deletions

View file

@ -111,44 +111,28 @@ const RaidCombobox = (props: Props) => {
// Handle Arrow key press event by focusing the list item above or below the current one based on the direction
const handleArrowKeyPressed = useCallback(
(direction: 'Up' | 'Down') => {
if (!listRef.current) return
const current = listRef.current?.querySelector(
'.Raid:focus'
) as HTMLElement | null
// Get the currently focused item
const current = listRef.current.querySelector('.Raid:focus')
// Select the item above or below based on direction
if (current) {
// If there is no item below, select the next parent element and then select the first element in that group
let next: Element | null | undefined
if (direction === 'Down' && !current.nextElementSibling) {
const nextParent =
current.parentElement?.parentElement?.nextElementSibling
if (nextParent) {
const next = nextParent.querySelector('.Raid')
if (next) {
;(next as HTMLElement).focus()
}
}
}
// If there is no item above, select the previous parent element and then select the first element in that group
if (direction === 'Up' && !current.previousElementSibling) {
next = nextParent?.querySelector('.Raid')
} else if (direction === 'Up' && !current.previousElementSibling) {
const previousParent =
current.parentElement?.parentElement?.previousElementSibling
if (previousParent) {
const next = previousParent.querySelector('.Raid:last-child')
if (next) {
;(next as HTMLElement).focus()
}
}
next = previousParent?.querySelector('.Raid:last-child')
} else {
next =
direction === 'Up'
? current.previousElementSibling
: current.nextElementSibling
}
}
// Select the item above or below based on direction
if (current) {
const next =
direction === 'Up'
? current.previousElementSibling
: current.nextElementSibling
if (next) {
;(next as HTMLElement).focus()
}
@ -321,13 +305,6 @@ const RaidCombobox = (props: Props) => {
const isRef = isSelected ? scrollToItem : undefined
const imageUrl = `${process.env.NEXT_PUBLIC_SIERO_IMG_URL}/raids/${raid.slug}.png`
const sectionIndex = sections?.[currentSection - 1]?.findIndex((group) =>
group.raids.some((r) => r.id === raid.id)
)
const raidTabIndex =
sectionIndex !== undefined ? tabIndex + sectionIndex : -1
return (
<RaidItem
className={isSelected ? 'Selected' : ''}
@ -336,6 +313,7 @@ const RaidCombobox = (props: Props) => {
key={key}
selected={isSelected}
ref={isRef}
role="listitem"
tabIndex={0}
value={raid.slug}
onEscapeKeyPressed={handleEscapeKeyPressed}

View file

@ -1,10 +1,10 @@
import React, { ComponentProps, PropsWithChildren } from 'react'
import { useTranslation } from 'next-i18next'
import { CommandItem } from '~components/common/Command'
import './index.scss'
import classNames from 'classnames'
import './index.scss'
interface Props {
interface Props extends ComponentProps<'div'> {
className?: string
icon?: {
alt: string
@ -33,6 +33,8 @@ const RaidItem = React.forwardRef<HTMLDivElement, PropsWithChildren<Props>>(
}: PropsWithChildren<Props>,
forwardedRef
) {
const { t } = useTranslation('common')
const classes = classNames(
{ SelectItem: true, Raid: true },
props.className