ARadioForm.tsx 1.15 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import React, { useState } from "react";
2
import { RadioType, AnswersType, AnswerProps } from "../types";
Lee SeoYeon's avatar
Lee SeoYeon committed
3

4
interface Props extends AnswerProps {
5
  element: RadioType;
Jiwon Yoon's avatar
Jiwon Yoon committed
6
  answerQuestion: any | undefined;
7
}
Lee SeoYeon's avatar
Lee SeoYeon committed
8

Jiwon Yoon's avatar
Jiwon Yoon committed
9
export const ARadioForm = ({ element, answerQuestion }: Props) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
10
11
12
13
  const [answer, setAnswer] = useState("");

  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { value } = event.currentTarget;
Jiwon Yoon's avatar
Jiwon Yoon committed
14
    answerQuestion.answer = value;
Jiwon Yoon's avatar
Jiwon Yoon committed
15
    setAnswer(value);
Jiwon Yoon's avatar
Jiwon Yoon committed
16
17
18
19
20
21
    if (answerQuestion.answer) {
      answerQuestion.requiredCheck = true;
    } else {
      answerQuestion.requiredCheck = false;
    }
    console.log(answerQuestion);
Jiwon Yoon's avatar
Jiwon Yoon committed
22
  };
Lee SeoYeon's avatar
Lee SeoYeon committed
23
  return (
Lee SeoYeon's avatar
Lee SeoYeon committed
24
    <div className="flex w-full gap-2 justify-around my-3">
25
      {element.content.choices.map((choice) => (
26
        <div key={choice.text}>
Jiwon Yoon's avatar
Jiwon Yoon committed
27
28
29
30
31
32
33
34
35
36
37
          <input
            className="mr-2"
            type="radio"
            id={choice.text}
            name={element._id}
            onChange={handleChange}
            value={choice.text}
          ></input>
          <label className="text-lg" id={choice.text}>
            {choice.text}
          </label>
Lee SeoYeon's avatar
Lee SeoYeon committed
38
        </div>
39
      ))}
Lee SeoYeon's avatar
Lee SeoYeon committed
40
41
42
    </div>
  );
};