Focus table when typing (fixes #45)

This commit is contained in:
Lynn
2022-01-19 16:50:58 +01:00
parent bbc1517a98
commit 978e9386e6

View File

@@ -1,4 +1,4 @@
import { useEffect, useState } from "react"; import { useEffect, useRef, useState } from "react";
import { Row, RowState } from "./Row"; import { Row, RowState } from "./Row";
import dictionary from "./dictionary.json"; import dictionary from "./dictionary.json";
import { Clue, clue, describeClue, violation } from "./clue"; import { Clue, clue, describeClue, violation } from "./clue";
@@ -70,6 +70,7 @@ function Game(props: GameProps) {
return challenge || randomTarget(wordLength); return challenge || randomTarget(wordLength);
}); });
const [gameNumber, setGameNumber] = useState(1); const [gameNumber, setGameNumber] = useState(1);
const tableRef = useRef<HTMLTableElement>(null);
const startNextGame = () => { const startNextGame = () => {
if (challenge) { if (challenge) {
// Clear the URL parameters: // Clear the URL parameters:
@@ -96,11 +97,7 @@ function Game(props: GameProps) {
setCurrentGuess((guess) => setCurrentGuess((guess) =>
(guess + key.toLowerCase()).slice(0, wordLength) (guess + key.toLowerCase()).slice(0, wordLength)
); );
// When typing a guess, make sure a later "Enter" press won't activate a link or button. tableRef.current?.focus();
const active = document.activeElement as HTMLElement;
if (active && ["A", "BUTTON"].includes(active.tagName)) {
active.blur();
}
setHint(""); setHint("");
} else if (key === "Backspace") { } else if (key === "Backspace") {
setCurrentGuess((guess) => guess.slice(0, -1)); setCurrentGuess((guess) => guess.slice(0, -1));
@@ -231,7 +228,12 @@ function Game(props: GameProps) {
Give up Give up
</button> </button>
</div> </div>
<table className="Game-rows" tabIndex={0} aria-label="Table of guesses"> <table
className="Game-rows"
tabIndex={0}
aria-label="Table of guesses"
ref={tableRef}
>
<tbody>{tableRows}</tbody> <tbody>{tableRows}</tbody>
</table> </table>
<p <p