Merge pull request #12877 from overleaf/td-history-file-tree-keyboard

History migration: Only activate document in file tree for space and enter keys

GitOrigin-RevId: 166a9ba36b1f19cd4e50d0e6f63ce04b164512cf
This commit is contained in:
Tim Down 2023-05-02 15:06:40 +01:00 committed by Copybot
parent 91db36bc01
commit a19dffeac2
2 changed files with 19 additions and 5 deletions

View file

@ -14,14 +14,15 @@ export default function HistoryFileTreeDoc({
file,
name,
}: HistoryFileTreeDocProps) {
const { isSelected, onClick } = useFileTreeItemSelection(file)
const { isSelected, handleClick, handleKeyDown } =
useFileTreeItemSelection(file)
return (
<li
role="treeitem"
className={classNames({ selected: isSelected })}
onClick={onClick}
onKeyDown={onClick}
onClick={handleClick}
onKeyDown={handleKeyDown}
aria-selected={isSelected}
aria-label={name}
tabIndex={0}

View file

@ -5,7 +5,7 @@ import type { FileDiff } from '../../services/types/file'
export function useFileTreeItemSelection(file: FileDiff) {
const { selection, setSelection } = useHistoryContext()
const handleClick = useCallback(() => {
const handleEvent = useCallback(() => {
if (file.pathname !== selection.selectedFile?.pathname) {
setSelection({
...selection,
@ -14,7 +14,20 @@ export function useFileTreeItemSelection(file: FileDiff) {
}
}, [file, selection, setSelection])
const handleClick = useCallback(() => {
handleEvent()
}, [handleEvent])
const handleKeyDown = useCallback(
event => {
if (event.key === 'Enter' || event.key === ' ') {
handleEvent()
}
},
[handleEvent]
)
const isSelected = selection.selectedFile?.pathname === file.pathname
return { isSelected, onClick: handleClick }
return { isSelected, handleClick, handleKeyDown }
}