diff --git a/app/(game)/play/PlayClient.tsx b/app/(game)/play/PlayClient.tsx
index 7af82aa..75a41df 100644
--- a/app/(game)/play/PlayClient.tsx
+++ b/app/(game)/play/PlayClient.tsx
@@ -16,13 +16,14 @@ import KatexRenderer from "@/components/math/KatexRenderer";
import { useGameStore } from "@/store/gameStore";
import type { ProblemCount } from "@/store/gameStore";
-const MOBILE_BREAKPOINT_QUERY = "(min-width: 640px)";
+const DESKTOP_INPUT_DEVICE_QUERY = "(any-hover: hover) and (any-pointer: fine)";
const MOBILE_KEYBOARD_SPACER_OFFSET = 480;
+const REAL_NUMBER_PATTERN = /^-?\d+(?:\.\d+)?$/;
function parseAnswer(value: string) {
const trimmed = value.trim();
- if (!/^-?\d+$/.test(trimmed)) return null;
+ if (!REAL_NUMBER_PATTERN.test(trimmed)) return null;
return Number(trimmed);
}
@@ -108,7 +109,7 @@ export default function PlayClient({ problemCount }: PlayClientProps) {
const parsedB = parseAnswer(answerB);
if (parsedA === null || parsedB === null) {
- setInputError("두 칸 모두 정수로 입력하세요.");
+ setInputError("두 칸 모두 실수로 입력하세요.");
return;
}
@@ -128,7 +129,7 @@ export default function PlayClient({ problemCount }: PlayClientProps) {
}, []);
const scrollKatexToViewportTop = useCallback(() => {
- if (window.matchMedia(MOBILE_BREAKPOINT_QUERY).matches) return;
+ if (window.matchMedia(DESKTOP_INPUT_DEVICE_QUERY).matches) return;
function alignKatex() {
const katexElement =
@@ -301,7 +302,6 @@ export default function PlayClient({ problemCount }: PlayClientProps) {
{!submitted && mobileFocusSpacerHeight > 0 ? (
) : null}
diff --git a/components/game/AnswerForm.tsx b/components/game/AnswerForm.tsx
index 2700530..6323e73 100644
--- a/components/game/AnswerForm.tsx
+++ b/components/game/AnswerForm.tsx
@@ -3,14 +3,32 @@
import {
forwardRef,
useCallback,
+ useEffect,
useId,
useImperativeHandle,
useRef,
+ useState,
} from "react";
-import type { FormEvent } from "react";
+import type { FocusEvent, FormEvent } from "react";
export type ActiveAnswerInput = 0 | 1;
+const ANSWER_INPUT_PATTERN = /^-?\d*(?:\.\d*)?$/;
+const DESKTOP_INPUT_DEVICE_QUERY = "(any-hover: hover) and (any-pointer: fine)";
+const KEYPAD_KEYS = [
+ "1",
+ "2",
+ "3",
+ "4",
+ "5",
+ "6",
+ "7",
+ "8",
+ "9",
+ "+/-",
+ "0",
+];
+
export interface AnswerFormHandle {
focusInput: () => void;
}
@@ -46,6 +64,9 @@ const AnswerForm = forwardRef(function Answer
const inputId = useId();
const formRef = useRef(null);
const inputRef = useRef(null);
+ const blurTimeoutRef = useRef(null);
+ const [keypadOpen, setKeypadOpen] = useState(false);
+ const [desktopInputDevice, setDesktopInputDevice] = useState(false);
const activeAnswer = activeInput === 0 ? answerA : answerB;
const activeLabel = activeInput === 0 ? "첫째 항" : "둘째 항";
@@ -53,6 +74,45 @@ const AnswerForm = forwardRef(function Answer
const activeAnswerChange =
activeInput === 0 ? onAnswerAChange : onAnswerBChange;
+ function updateAnswer(value: string) {
+ if (!ANSWER_INPUT_PATTERN.test(value)) return;
+
+ activeAnswerChange(value);
+ }
+
+ function handleInputFocus() {
+ if (blurTimeoutRef.current !== null) {
+ window.clearTimeout(blurTimeoutRef.current);
+ blurTimeoutRef.current = null;
+ }
+
+ setKeypadOpen(true);
+ onInputFocus?.();
+ }
+
+ function handleInputBlur(event: FocusEvent) {
+ const nextFocusedElement = event.relatedTarget;
+
+ if (
+ nextFocusedElement instanceof Node &&
+ formRef.current?.contains(nextFocusedElement)
+ ) {
+ return;
+ }
+
+ blurTimeoutRef.current = window.setTimeout(() => {
+ blurTimeoutRef.current = null;
+
+ if (formRef.current?.contains(document.activeElement)) {
+ setKeypadOpen(true);
+ return;
+ }
+
+ setKeypadOpen(false);
+ onInputBlur?.();
+ }, 0);
+ }
+
const focusInput = useCallback(() => {
const input = inputRef.current;
@@ -69,6 +129,23 @@ const AnswerForm = forwardRef(function Answer
useImperativeHandle(ref, () => ({ focusInput }), [focusInput]);
+ useEffect(() => {
+ const mediaQuery = window.matchMedia(DESKTOP_INPUT_DEVICE_QUERY);
+ const updateInputDevice = () => {
+ setDesktopInputDevice(mediaQuery.matches);
+ };
+
+ updateInputDevice();
+ mediaQuery.addEventListener("change", updateInputDevice);
+
+ return () => {
+ if (blurTimeoutRef.current !== null) {
+ window.clearTimeout(blurTimeoutRef.current);
+ }
+ mediaQuery.removeEventListener("change", updateInputDevice);
+ };
+ }, []);
+
function setAnswer(input: ActiveAnswerInput, value: string) {
if (input === 0) {
onAnswerAChange(value);
@@ -84,6 +161,16 @@ const AnswerForm = forwardRef(function Answer
focusInput();
}
+ function appendKeypadValue(value: string) {
+ updateAnswer(`${activeAnswer}${value}`);
+ focusInput();
+ }
+
+ function deleteLastCharacter() {
+ updateAnswer(activeAnswer.slice(0, -1));
+ focusInput();
+ }
+
function moveToInput(input: ActiveAnswerInput, reset = false) {
onActiveInputChange(input);
if (reset) {
@@ -99,6 +186,7 @@ const AnswerForm = forwardRef(function Answer
}
formRef.current?.requestSubmit();
+ focusInput();
}
const answerButtons = [
@@ -128,50 +216,26 @@ const AnswerForm = forwardRef(function Answer
>
{activeLabel}
-
-
- activeAnswerChange(event.target.value)}
- onKeyDown={(event) => {
- if (event.key === "Enter") {
- event.preventDefault();
- handleEnter();
- }
- }}
- autoComplete="off"
- enterKeyHint={activeEnterKeyHint}
- inputMode="numeric"
- pattern="-?[0-9]*"
- className="h-full min-w-0 flex-1 px-3 text-lg font-semibold text-slate-950 outline-none sm:rounded-md sm:border sm:border-slate-300 sm:px-4 sm:transition sm:focus:border-emerald-600 sm:focus:ring-2 sm:focus:ring-emerald-200"
- />
-
-
+ }}
+ autoComplete="off"
+ enterKeyHint={activeEnterKeyHint}
+ inputMode="none"
+ pattern="-?[0-9]*(\.[0-9]*)?"
+ className="h-12 w-full min-w-0 rounded-md border border-slate-300 bg-white px-3 text-lg font-semibold text-slate-950 outline-none transition focus:border-emerald-600 focus:ring-2 focus:ring-emerald-200 sm:px-4"
+ />
(function Answer
);
})}
+
+ {keypadOpen && !desktopInputDevice ? (
+
+ {KEYPAD_KEYS.map((key) => (
+
+ ))}
+
+
+
+
+ ) : null}
{error ? (