ARadioForm.tsx 1.03 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;
6
}
Lee SeoYeon's avatar
Lee SeoYeon committed
7

8
export const ARadioForm = ({ element, answers, handleAnswer }: 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;
Yoon, Daeki's avatar
Yoon, Daeki committed
13
    answers && (answers.answer = value);
Jiwon Yoon's avatar
Jiwon Yoon committed
14
15
16
    setAnswer(value);
    handleAnswer();
  };
Lee SeoYeon's avatar
Lee SeoYeon committed
17
  return (
Lee SeoYeon's avatar
Lee SeoYeon committed
18
    <div className="flex w-full gap-2 justify-around my-3">
19
      {element.content.choices.map((choice) => (
20
        <div key={choice.text}>
Jiwon Yoon's avatar
Jiwon Yoon committed
21
22
23
24
25
26
27
          <input
            className="mr-2"
            type="radio"
            id={choice.text}
            name={element._id}
            onChange={handleChange}
            value={choice.text}
Jiwon Yoon's avatar
Jiwon Yoon committed
28
            required={element.isRequired}
Jiwon Yoon's avatar
Jiwon Yoon committed
29
30
31
32
          ></input>
          <label className="text-lg" id={choice.text}>
            {choice.text}
          </label>
Lee SeoYeon's avatar
Lee SeoYeon committed
33
        </div>
34
      ))}
Lee SeoYeon's avatar
Lee SeoYeon committed
35
36
37
    </div>
  );
};