Skip to content

Commit

Permalink
Anagram helper improvements (#1831)
Browse files Browse the repository at this point in the history
Moving the focus for arrow keys is now outside of setState.
Remove unneeded spacing
  • Loading branch information
oliverabrahams authored Dec 3, 2024
1 parent a49f7b6 commit 240c044
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ export const AnagramHelper = ({ onClickClose }: AnagramHelperProps) => {
display: flex;
align-items: center;
flex-direction: column;
> * {
margin-bottom: ${space[4]}px;
}
`}
>
<div
Expand Down
38 changes: 22 additions & 16 deletions libs/@guardian/react-crossword/src/components/SolutionDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,30 @@ export const SolutionDisplay = ({
if (event.key.length === 1 && !keyDownRegex.test(event.key)) {
return;
}
setCandidateLetters((prevState) => {
const newCandidateLetters = [...prevState];
if (event.key.length === 1) {
if (event.key === 'ArrowLeft') {
inputRefs.current[index - 1]?.focus();
return;
}
if (event.key === 'ArrowRight') {
inputRefs.current[index + 1]?.focus();
return;
}
if (keyDownRegex.test(event.key)) {
setCandidateLetters((prevState) => {
const newCandidateLetters = [...prevState];
newCandidateLetters[index] = event.key.toUpperCase();
inputRefs.current[index + 1]?.focus();
}
if (event.key === 'Backspace') {
return newCandidateLetters;
});
inputRefs.current[index + 1]?.focus();
}
if (event.key === 'Backspace') {
setCandidateLetters((prevState) => {
const newCandidateLetters = [...prevState];
newCandidateLetters[index] = '';
inputRefs.current[index - 1]?.focus();
}
if (event.key === 'ArrowLeft') {
inputRefs.current[index - 1]?.focus();
}
if (event.key === 'ArrowRight') {
inputRefs.current[index + 1]?.focus();
}
return newCandidateLetters;
});
return newCandidateLetters;
});
inputRefs.current[index - 1]?.focus();
}
};

const onDragEnd = useCallback(() => {
Expand Down

0 comments on commit 240c044

Please sign in to comment.