ARadioForm.tsx 1.1 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
          <input
            className="mr-2"
            type="radio"
            id={choice.text}
            name={element._id}
            onChange={handleChange}
            value={choice.text}
Jiwon Yoon's avatar
Jiwon Yoon committed
34
            required={element.isRequired}
Jiwon Yoon's avatar
Jiwon Yoon committed
35
36
37
38
          ></input>
          <label className="text-lg" id={choice.text}>
            {choice.text}
          </label>
Lee SeoYeon's avatar
Lee SeoYeon committed
39
        </div>
40
      ))}
Lee SeoYeon's avatar
Lee SeoYeon committed
41
42
43
    </div>
  );
};