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

4
5
type Props = {
  element: RadioType;
Jiwon Yoon's avatar
Jiwon Yoon committed
6
7
  response: AnswerType;
  handleAnswer: () => void;
8
};
Lee SeoYeon's avatar
Lee SeoYeon committed
9

Jiwon Yoon's avatar
Jiwon Yoon committed
10
11
12
13
14
15
16
17
18
19
20
21
22
export const ARadioForm = ({ element, response, handleAnswer }: Props) => {
  const [answer, setAnswer] = useState("");

  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { value } = event.currentTarget;
    response.answers.map((a) => {
      if (a.questionId === element._id) {
        a.answer = value;
      }
    });
    setAnswer(value);
    handleAnswer();
  };
Lee SeoYeon's avatar
Lee SeoYeon committed
23
  return (
24
25
26
    <div className="flex w-full gap-4 justify-around my-3">
      {element.content.choices.map((choice) => (
        <div>
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>
  );
};