Skip to content

Commit

Permalink
fix: Enter key on city gridcell fires navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswilty committed Oct 2, 2024
1 parent 4d4d903 commit 3695cda
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
import { ICellRendererParams } from 'ag-grid-community'
import { Link } from 'react-router-dom'
import { useCallback, useEffect, useId } from 'react'
import { Link, useNavigate } from 'react-router-dom'

import classes from './LocationCellRenderer.module.css'
import { RouteConstants } from '../../../../routes'

export const LocationCellRenderer = (
props: Pick<ICellRendererParams, 'value'>,
props: Pick<ICellRendererParams, 'value' | 'eGridCell'>,
) => {
const { eGridCell, value } = props
const linkTo = `${RouteConstants.SINGLE_CITY}/${value}`
const linkId = useId()
const navigate = useNavigate()

const navListener = useCallback(
(event: KeyboardEvent) => {
if (event.code === 'Enter') navigate(linkTo)
},
[linkTo, navigate],
)

useEffect(() => {
eGridCell.setAttribute('aria-activedescendant', linkId)
eGridCell.addEventListener('keydown', navListener)

return () => {
eGridCell.removeEventListener('keydown', navListener)
}
}, [eGridCell, linkId, navListener])

return (
<Link
to={`${RouteConstants.SINGLE_CITY}/${props.value}`}
id={linkId}
to={linkTo}
role="link"
className={classes['location-link']}
>
{props.value}
{value}
</Link>
)
}

0 comments on commit 3695cda

Please sign in to comment.