import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { useEffect, useState } from 'react'; interface Props { question: ExamQuestion; answer: any; onAnswerChange: (answer: any) => void; } const FillBlankQuestion = ({ question, answer, onAnswerChange }: Props) => { const blanks = question.options?.blanks; const totalInputs = blanks?.length ?? 1; const buildInitialValues = () => { if (Array.isArray(answer?.answers)) { const existing = answer?.answers as string[]; return Array.from({ length: totalInputs }, (_, index) => existing[index] || ''); } if (answer?.answers && typeof answer.answers === 'object') { const record = answer.answers as Record; return Array.from({ length: totalInputs }, (_, index) => record[index] || ''); } return Array.from({ length: totalInputs }, () => ''); }; const [values, setValues] = useState(buildInitialValues); useEffect(() => { setValues(buildInitialValues()); // eslint-disable-next-line react-hooks/exhaustive-deps }, [question.id]); const emitChange = (newValues: string[]) => { setValues(newValues); onAnswerChange({ answers: newValues, }); }; const handleChange = (index: number, value: string) => { const newValues = [...values]; newValues[index] = value; emitChange(newValues); }; // Parse description placeholders when blanks metadata provided const questionText = question.description || ''; const parts = blanks ? questionText.split(/\[BLANK_(\d+)\]/g) : [questionText]; return (

Fill in the blanks with appropriate answers:

{blanks ? parts.map((part, index) => { if (index % 2 === 0) { return ; } const placeholderIndex = parseInt(part, 10); return ( handleChange(placeholderIndex, e.target.value)} placeholder={`Blank ${placeholderIndex + 1}`} className="mx-2 inline-block w-48" autoComplete="off" /> ); }) : ( )}
{Array.from({ length: totalInputs }).map((_, index) => (
Blank {index + 1}: handleChange(index, e.target.value)} placeholder="Your answer" className="flex-1" autoComplete="off" />
))}
{question.options?.case_sensitive && (

Note: Answers are case-sensitive.

)}
); }; export default FillBlankQuestion;