-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle keyboard input for improved accessibility
- Loading branch information
1 parent
5afe6e5
commit 060c7cc
Showing
9 changed files
with
175 additions
and
31 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useState, useRef, useEffect } from "react"; | ||
import useDeleteCategoryWithTasks from "./useDeleteCategoryWithTasks"; | ||
|
||
const useCategoryListItem = (categoryId: string, isFocused: boolean) => { | ||
const [isEditMode, setEditMode] = useState(false); | ||
const deleteCategoryWithTasks = useDeleteCategoryWithTasks(); | ||
const categoryRef = useRef<HTMLLIElement>(null); | ||
|
||
useEffect(() => { | ||
if (isFocused && categoryRef.current) { | ||
categoryRef.current.focus(); | ||
} | ||
}, [isFocused]); | ||
|
||
const handleKeyDown = (event: React.KeyboardEvent<HTMLLIElement>) => { | ||
if (!isFocused || isEditMode) return; | ||
|
||
if (event.ctrlKey && event.key === "e") { | ||
event.preventDefault(); | ||
setEditMode(true); | ||
} else if (event.key === "Delete") { | ||
deleteCategoryWithTasks(categoryId); | ||
} | ||
}; | ||
|
||
return { isEditMode, setEditMode, handleKeyDown, categoryRef }; | ||
}; | ||
|
||
export default useCategoryListItem; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useState } from "react"; | ||
import Task from "../types/Task"; | ||
import Category from "../types/Category"; | ||
|
||
const useKeyboardNavigation = (items: Task[] | Category[]) => { | ||
const [focusedIndex, setFocusedIndex] = useState(0); | ||
|
||
const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => { | ||
if (event.key === "ArrowDown") { | ||
event.preventDefault(); | ||
setFocusedIndex((prevIndex) => | ||
prevIndex === items.length - 1 ? 0 : prevIndex + 1, | ||
); | ||
} else if (event.key === "ArrowUp") { | ||
event.preventDefault(); | ||
setFocusedIndex((prevIndex) => | ||
prevIndex === 0 ? items.length - 1 : prevIndex - 1, | ||
); | ||
} | ||
}; | ||
|
||
return { focusedIndex, handleKeyDown }; | ||
}; | ||
|
||
export default useKeyboardNavigation; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useRef, useEffect, useState } from "react"; | ||
import useTaskStore from "../store/useTaskStore"; | ||
|
||
const useTaskListItem = (taskId: string, isFocused: boolean) => { | ||
const [isEditMode, setEditMode] = useState(false); | ||
const { deleteTask, toggleCompletion } = useTaskStore(); | ||
const taskRef = useRef<HTMLLIElement>(null); | ||
|
||
useEffect(() => { | ||
if (isFocused && taskRef.current) { | ||
taskRef.current.focus(); | ||
} | ||
}, [isFocused]); | ||
|
||
const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => { | ||
if (!isFocused || isEditMode) return; | ||
|
||
if (event.ctrlKey && event.key === "e") { | ||
event.preventDefault(); | ||
setEditMode(true); | ||
} else if (event.key === "Enter") { | ||
toggleCompletion(taskId); | ||
} else if (event.key === "Delete") { | ||
deleteTask(taskId); | ||
} | ||
}; | ||
|
||
return { isEditMode, setEditMode, handleKeyDown, taskRef }; | ||
}; | ||
|
||
export default useTaskListItem; |