import { useState } from 'react' import AudioPlayer from '@/components/AudioPlayer' interface SentenceFillTestProps { word: string definition: string example: string exampleTranslation: string pronunciation?: string difficultyLevel: string exampleImage?: string onAnswer: (answer: string) => void onReportError: () => void onImageClick?: (image: string) => void disabled?: boolean } export const SentenceFillTest: React.FC = ({ word, definition, example, exampleTranslation, pronunciation, difficultyLevel, exampleImage, onAnswer, onReportError, onImageClick, disabled = false }) => { const [fillAnswer, setFillAnswer] = useState('') const [showResult, setShowResult] = useState(false) const [showHint, setShowHint] = useState(false) const handleSubmit = () => { if (disabled || showResult || !fillAnswer.trim()) return setShowResult(true) onAnswer(fillAnswer) } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !showResult && fillAnswer.trim()) { handleSubmit() } } const isCorrect = fillAnswer.toLowerCase().trim() === word.toLowerCase() const targetWordLength = word.length const inputWidth = Math.max(100, Math.max(targetWordLength * 12, fillAnswer.length * 12 + 20)) // 將例句中的目標詞替換為輸入框 const renderSentenceWithInput = () => { const parts = example.split(new RegExp(`\\b${word}\\b`, 'gi')) const matches = example.match(new RegExp(`\\b${word}\\b`, 'gi')) || [] return (
{parts.map((part, index) => ( {part} {index < matches.length && ( setFillAnswer(e.target.value)} onKeyDown={handleKeyDown} placeholder="" disabled={disabled || showResult} className={`inline-block px-2 py-1 text-center bg-transparent focus:outline-none disabled:bg-gray-100 ${ fillAnswer ? 'border-b-2 border-blue-500' : 'border-b-2 border-dashed border-gray-400 focus:border-blue-400 focus:border-solid' }`} style={{ width: `${inputWidth}px` }} /> {!fillAnswer && ( ____ )} )} ))}
) } return (
{/* 錯誤回報按鈕 */}
{/* 標題區 */}

例句填空

{difficultyLevel}
{/* 圖片區(如果有) */} {exampleImage && (

圖片提示

Example illustration onImageClick?.(exampleImage)} />
)} {/* 指示文字 */}

請點擊例句中的空白處輸入正確的單字:

{/* 填空句子區域 */}
{renderSentenceWithInput()}
{/* 操作按鈕 */}
{!showResult && fillAnswer.trim() && ( )}
{/* 提示區域 */} {showHint && (

詞彙定義:

{definition}

)} {/* 結果反饋區 */} {showResult && (

{isCorrect ? '正確!' : '錯誤!'}

{!isCorrect && (

正確答案是:{word}

)}

發音: {pronunciation && {pronunciation}}

完整例句:"{example}"

翻譯:"{exampleTranslation}"

)}
) }