Skip to content

Commit

Permalink
Suuper strange edge case. Chrome on Android doesn't allow this standa…
Browse files Browse the repository at this point in the history
…rd keypress so need a hack around it.
  • Loading branch information
ryqndev committed May 5, 2024
1 parent dd41d75 commit 31d2eeb
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/app/pages/ColorGame/components/HexColorInput/HexColorInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,26 @@ export const HexColorInput = memo(function HexColorInput({
focusEndOfInput(input[0].current);
};

const handleChange = (index) => (event) => {
const userKeyInput = event.key.toUpperCase();
const handleInput = (index) => (event) => {
const userKeyInput = event.nativeEvent.data.toUpperCase();

if (!DIGITS.includes(userKeyInput)) return;

setGuess((prev) => {
const next = [...prev];
next[index] = userKeyInput;
return next;
});

if (index > 4) return;
focusEndOfInput(input[index + 1].current);
};

const handleChange = (index) => (event) => {
if (event.keyCode === 229) {
// really strange use case where chrome on android doesn't allow this method of input
return;
}
switch (event.key) {
case "Backspace":
if (index > 0 && !guess[index].length) {
Expand All @@ -84,18 +101,11 @@ export const HexColorInput = memo(function HexColorInput({
return;

default:
if (!DIGITS.includes(userKeyInput)) return;

setGuess((prev) => {
const next = [...prev];
next[index] = userKeyInput;
return next;
});

if (index > 4) return;
focusEndOfInput(input[index + 1].current);
// handle actual text input with the oninput handler
return;
}
};

return (
<form className={cn.container} onSubmit={handleSubmit}>
<div className={cn.input}>
Expand All @@ -108,6 +118,7 @@ export const HexColorInput = memo(function HexColorInput({
/>
{guess.map((digit, index) => (
<input
onInput={handleInput(index)}
key={index}
ref={input[index]}
type="text"
Expand Down

0 comments on commit 31d2eeb

Please sign in to comment.