generated from hamadsuniverse/codecatalyst-sst-app
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTableCompletionQuestionComponent.tsx
64 lines (62 loc) · 1.74 KB
/
TableCompletionQuestionComponent.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react';
import { QuestionTableCompletion } from '../../utilities/readingUtilities';
import { Answer, SetAnswer } from './QuestionsComponent';
export const TableCompletionQuestionComponent = ({
question,
answer,
set,
}: {
question: QuestionTableCompletion;
answer: Answer;
set: SetAnswer;
}) => {
const handleInputChange = (
event: React.ChangeEvent<HTMLInputElement>,
rowIndex: number,
columnIndex: number,
) => {
const newInputValues = [...answer] as string[][];
newInputValues[rowIndex][columnIndex] = event.target.value;
set(newInputValues);
};
const renderQuestionTextWithInputs = (text: string, rowIndex: number) => {
const parts = text.split('-answer-');
return parts.map((part, index) => {
if (index === parts.length - 1) {
return <React.Fragment key={index}>{part}</React.Fragment>;
}
return (
<React.Fragment key={index}>
{part}
<input
type="text"
value={answer[rowIndex][index] || ''} // Bind input value to state
onChange={event => handleInputChange(event, rowIndex, index)} // Handle input change
className="lr-input"
/>
</React.Fragment>
);
});
};
console.log(answer);
return (
<div>
<p>{question.Question}</p>
<table>
<tbody>
{question.SubQuestions.map((subQuestion, rowIndex) => (
<tr key={rowIndex}>
<td>{subQuestion.RowTitle}</td>
<td>
{renderQuestionTextWithInputs(
subQuestion.QuestionText,
rowIndex,
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
);
};