108 lines
3.3 KiB
JavaScript
108 lines
3.3 KiB
JavaScript
|
|
'use client';
|
||
|
|
import { useEffect, useState } from 'react';
|
||
|
|
|
||
|
|
const KEYPAD = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Digit entry with the single-player rule: every digit can be used only once.
|
||
|
|
* Supports clicks and the keyboard (0-9, Backspace, Enter).
|
||
|
|
*/
|
||
|
|
export default function NumberPad({ digitNumber, onSubmit, disabled = false, busy = false, submitLabel = 'Enter' }) {
|
||
|
|
const [digits, setDigits] = useState(() => Array(digitNumber).fill(''));
|
||
|
|
const used = new Set(digits.filter((d) => d !== ''));
|
||
|
|
const complete = digits.every((d) => d !== '');
|
||
|
|
const locked = disabled || busy;
|
||
|
|
|
||
|
|
const append = (val) => {
|
||
|
|
if (locked) return;
|
||
|
|
setDigits((cur) => {
|
||
|
|
const s = String(val);
|
||
|
|
const idx = cur.indexOf('');
|
||
|
|
if (idx === -1 || cur.includes(s)) return cur;
|
||
|
|
const next = [...cur];
|
||
|
|
next[idx] = s;
|
||
|
|
return next;
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
const remove = () => {
|
||
|
|
if (locked) return;
|
||
|
|
setDigits((cur) => {
|
||
|
|
const next = [...cur];
|
||
|
|
for (let i = next.length - 1; i >= 0; i--) {
|
||
|
|
if (next[i] !== '') {
|
||
|
|
next[i] = '';
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return next;
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
const submit = async () => {
|
||
|
|
if (locked || !complete) return;
|
||
|
|
const ok = await onSubmit(digits.join(''));
|
||
|
|
if (ok !== false) setDigits(Array(digitNumber).fill(''));
|
||
|
|
};
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
const onKey = (e) => {
|
||
|
|
if (e.target instanceof HTMLInputElement) return;
|
||
|
|
if (/^[0-9]$/.test(e.key)) append(e.key);
|
||
|
|
else if (e.key === 'Backspace') remove();
|
||
|
|
else if (e.key === 'Enter') submit();
|
||
|
|
};
|
||
|
|
window.addEventListener('keydown', onKey);
|
||
|
|
return () => window.removeEventListener('keydown', onKey);
|
||
|
|
});
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div className="flex flex-col items-center gap-6 w-full">
|
||
|
|
<div className="w-full overflow-x-auto">
|
||
|
|
<div className="flex gap-2 sm:gap-3 justify-center min-w-max px-1">
|
||
|
|
{digits.map((d, i) => (
|
||
|
|
<div
|
||
|
|
key={i}
|
||
|
|
className="w-9 h-12 sm:w-12 sm:h-16 flex items-center justify-center text-xl sm:text-3xl font-bold bg-neutral-800 rounded-lg border border-neutral-700"
|
||
|
|
>
|
||
|
|
{d}
|
||
|
|
</div>
|
||
|
|
))}
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="grid grid-cols-3 gap-3 w-56">
|
||
|
|
{KEYPAD.map((n) => {
|
||
|
|
const off = used.has(String(n)) || locked;
|
||
|
|
return (
|
||
|
|
<button
|
||
|
|
key={n}
|
||
|
|
onClick={() => append(n)}
|
||
|
|
disabled={off}
|
||
|
|
className={`py-3 rounded-lg text-xl font-semibold transition ${
|
||
|
|
off ? 'bg-neutral-700 opacity-40 cursor-not-allowed' : 'bg-neutral-600 hover:bg-neutral-500'
|
||
|
|
}`}
|
||
|
|
>
|
||
|
|
{n}
|
||
|
|
</button>
|
||
|
|
);
|
||
|
|
})}
|
||
|
|
<button
|
||
|
|
onClick={remove}
|
||
|
|
disabled={used.size === 0 || locked}
|
||
|
|
className="py-3 rounded-lg text-xl font-semibold bg-rose-600 hover:bg-rose-700 transition disabled:opacity-40"
|
||
|
|
>
|
||
|
|
⌫
|
||
|
|
</button>
|
||
|
|
<button
|
||
|
|
onClick={submit}
|
||
|
|
disabled={!complete || locked}
|
||
|
|
className="col-span-2 py-3 rounded-lg text-xl font-semibold bg-emerald-600 hover:bg-emerald-700 transition disabled:opacity-40"
|
||
|
|
>
|
||
|
|
{busy ? '...' : submitLabel}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|