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

4
interface Props extends AnswerProps {
5
  element: RadioType;
6
}
Lee SeoYeon's avatar
Lee SeoYeon committed
7

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

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