Skip to content

Commit d9d4000

Browse files
Board hotkeys now work when board is not selected (#290)
1 parent 07458f2 commit d9d4000

File tree

1 file changed

+73
-51
lines changed

1 file changed

+73
-51
lines changed

app/Components/Sudoku Board/SudokuBoard.tsx

+73-51
Original file line numberDiff line numberDiff line change
@@ -433,65 +433,86 @@ const SudokuBoard = (props: SudokuBoardProps) => {
433433
];
434434
};
435435

436+
/**
437+
* When a user presses a key down, do the desired action
438+
* @param event onKeyDown event for react-native-web documented here: https://necolas.github.io/react-native-web/docs/interactions/#keyboardevent-props-api
439+
* @returns void
440+
*/
436441
const handleKeyDown = (event: any) => {
442+
const inputValue = event.nativeEvent.key;
443+
444+
switch (inputValue) {
445+
case "u":
446+
case "U":
447+
const isUndoButtonDisabled =
448+
sudokuBoard.actionHistory == null ||
449+
sudokuBoard.actionHistory.length == 0;
450+
if (!isUndoButtonDisabled) {
451+
undo();
452+
}
453+
return;
454+
case "p":
455+
case "P":
456+
handlePause(sudokuBoard, navigation);
457+
return;
458+
case "t":
459+
case "T":
460+
case "n":
461+
case "N":
462+
toggleNoteMode();
463+
return;
464+
default:
465+
break;
466+
}
467+
437468
if (sudokuBoard.selectedCell == null) {
438469
return;
439470
}
440471

441-
const inputValue = event.nativeEvent.key;
442472
if (/^[1-9]$/.test(inputValue)) {
443473
updateCellEntry(parseInt(inputValue, 10));
444-
} else if (
445-
inputValue == "Delete" ||
446-
inputValue == "Backspace" ||
447-
inputValue == "0" ||
448-
inputValue == "e" ||
449-
inputValue == "E" // e and E are for erase
450-
) {
451-
eraseSelected();
452-
} else if (inputValue == "u" || inputValue == "U") {
453-
undo();
454-
} else if (inputValue == "p" || inputValue == "P") {
455-
handlePause(sudokuBoard, navigation);
456-
} else if (
457-
inputValue == "t" ||
458-
inputValue == "T" ||
459-
inputValue == "n" ||
460-
inputValue == "N"
461-
) {
462-
toggleNoteMode();
463-
} else if (sudokuBoard.selectedCell) {
464-
let newCol = sudokuBoard.selectedCell.c;
465-
let newRow = sudokuBoard.selectedCell.r;
466-
switch (inputValue) {
467-
case "ArrowLeft":
468-
case "a":
469-
case "A":
470-
newCol = wrapDigit(newCol - 1);
471-
break;
472-
case "ArrowRight":
473-
case "d":
474-
case "D":
475-
newCol = wrapDigit(newCol + 1);
476-
break;
477-
case "ArrowUp":
478-
case "w":
479-
case "W":
480-
newRow = wrapDigit(newRow - 1);
481-
break;
482-
case "ArrowDown":
483-
case "s":
484-
case "S":
485-
newRow = wrapDigit(newRow + 1);
486-
break;
487-
default:
488-
return;
489-
}
490-
setSudokuBoard({
491-
...sudokuBoard,
492-
selectedCell: { r: newRow, c: newCol },
493-
});
474+
return;
475+
}
476+
477+
let newCol = sudokuBoard.selectedCell.c;
478+
let newRow = sudokuBoard.selectedCell.r;
479+
switch (inputValue) {
480+
case "Delete":
481+
case "Backspace":
482+
case "0":
483+
case "e":
484+
case "E":
485+
eraseSelected();
486+
return;
487+
// below cases do not return to allow for update of selected cell
488+
// todo create function for updating selectedCell for below cases to call
489+
case "ArrowLeft":
490+
case "a":
491+
case "A":
492+
newCol = wrapDigit(newCol - 1);
493+
break;
494+
case "ArrowRight":
495+
case "d":
496+
case "D":
497+
newCol = wrapDigit(newCol + 1);
498+
break;
499+
case "ArrowUp":
500+
case "w":
501+
case "W":
502+
newRow = wrapDigit(newRow - 1);
503+
break;
504+
case "ArrowDown":
505+
case "s":
506+
case "S":
507+
newRow = wrapDigit(newRow + 1);
508+
break;
509+
default:
510+
return;
494511
}
512+
setSudokuBoard({
513+
...sudokuBoard,
514+
selectedCell: { r: newRow, c: newCol },
515+
});
495516
};
496517

497518
const renderPuzzle = () => {
@@ -567,6 +588,7 @@ const SudokuBoard = (props: SudokuBoardProps) => {
567588
return (
568589
<View
569590
testID={"sudokuBoard"}
591+
//@ts-ignore react-native-web types not supported: https://github.com/necolas/react-native-web/issues/1684
570592
onKeyDown={handleKeyDown}
571593
style={{
572594
alignItems: "center",

0 commit comments

Comments
 (0)