Move hint buttons from a separate flex column into the table as the first <td> of each row, ensuring pixel-perfect alignment with grid rows. Use position:sticky with box-shadow to keep hints fixed on the left while scrolling horizontally. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
66 lines
2.1 KiB
JavaScript
66 lines
2.1 KiB
JavaScript
import React, { useRef, useCallback, useMemo } from 'react';
|
|
import LetterInput from './LetterInput';
|
|
import ActorPopover from './ActorPopover';
|
|
|
|
function isLetter(ch) {
|
|
return /[a-zA-Z]/.test(ch);
|
|
}
|
|
|
|
export default function GameRow({ actorName, pos, colStart, totalWidth, hintType, hintText }) {
|
|
const inputRefs = useRef([]);
|
|
const letters = actorName.split('');
|
|
|
|
const letterIndices = useMemo(
|
|
() => letters.reduce((acc, ch, i) => { if (isLetter(ch)) acc.push(i); return acc; }, []),
|
|
[actorName]
|
|
);
|
|
|
|
const setInputRef = useCallback((index) => (el) => {
|
|
inputRefs.current[index] = el;
|
|
}, []);
|
|
|
|
const focusNextInput = useCallback((charIndex, direction) => {
|
|
const currentPos = letterIndices.indexOf(charIndex);
|
|
const nextPos = currentPos + direction;
|
|
if (nextPos >= 0 && nextPos < letterIndices.length) {
|
|
inputRefs.current[letterIndices[nextPos]]?.focus();
|
|
}
|
|
}, [letterIndices]);
|
|
|
|
return (
|
|
<tr>
|
|
<td className="hint-cell">
|
|
<ActorPopover hintType={hintType} hintText={hintText} />
|
|
</td>
|
|
{Array.from({ length: totalWidth + 1 }, (_, colIndex) => {
|
|
const charIndex = colIndex - colStart;
|
|
const isInRange = charIndex >= 0 && charIndex < letters.length;
|
|
|
|
if (!isInRange) {
|
|
return <td key={colIndex} />;
|
|
}
|
|
|
|
const ch = letters[charIndex];
|
|
|
|
if (!isLetter(ch)) {
|
|
return (
|
|
<td key={colIndex} className="letter-static">
|
|
{ch}
|
|
</td>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<LetterInput
|
|
key={colIndex}
|
|
highlighted={charIndex === pos}
|
|
inputRef={setInputRef(charIndex)}
|
|
onNext={() => focusNextInput(charIndex, 1)}
|
|
onPrev={() => focusNextInput(charIndex, -1)}
|
|
/>
|
|
);
|
|
})}
|
|
</tr>
|
|
);
|
|
}
|