import { h } from 'preact'; import { useEffect, useRef, useState } from 'preact/hooks'; import styles from '../NewGame.module.css'; import { ProgressIndicator } from '../ProgressIndicator'; interface PlayerStepProps { playerNameHistory: string[]; onNext: (name: string) => void; onCancel: () => void; initialValue?: string; } export const Player2Step = ({ playerNameHistory, onNext, onCancel, initialValue = '' }: PlayerStepProps) => { const [player2, setPlayer2] = useState(initialValue); const [error, setError] = useState(null); const [filteredNames, setFilteredNames] = useState(playerNameHistory); const inputRef = useRef(null); useEffect(() => { if (!player2) { setFilteredNames(playerNameHistory); } else { setFilteredNames( playerNameHistory.filter(name => name.toLowerCase().includes(player2.toLowerCase()) ) ); } }, [player2, playerNameHistory]); const handleSubmit = (e: Event) => { e.preventDefault(); if (!player2.trim()) { setError('Bitte Namen für Spieler 2 eingeben'); return; } setError(null); onNext(player2.trim()); }; const handleQuickPick = (name: string) => { setError(null); onNext(name); }; const handleClear = () => { setPlayer2(''); setError(null); if (inputRef.current) inputRef.current.focus(); }; return (
Name Spieler 2
{ const target = e.target as HTMLInputElement; setPlayer2(target.value); }} autoComplete="off" autoCapitalize="words" spellCheck={false} enterKeyHint="next" aria-label="Name Spieler 2" onFocus={(e) => { const target = e.currentTarget as HTMLInputElement; target.scrollIntoView({ block: 'center', behavior: 'smooth' }); }} style={{ fontSize: '1.2rem', minHeight: 48, marginTop: 12, marginBottom: 12, width: '100%', paddingRight: 44 }} ref={inputRef} /> {player2 && ( )}
{filteredNames.length > 0 && (
{filteredNames.slice(0, 10).map((name, idx) => ( ))}
)}
{error &&
{error}
}
); };