ACheckboxForm.tsx 936 Bytes
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import React, { useState } from "react";
2
import { CheckboxType, AnswersType, AnswerProps } from "../types";
Lee SeoYeon's avatar
Lee SeoYeon committed
3

4
interface Props extends AnswerProps {
5
  element: CheckboxType;
6
}
7

8
export const ACheckboxForm = ({ 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
20
      {element.content.choices.map((choice) => (
        <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
21
22
23
24
25
          <input
            className="mr-2"
            type="checkbox"
            value={choice.text}
            onChange={handleChange}
Jiwon Yoon's avatar
Jiwon Yoon committed
26
            required={element.isRequired}
Jiwon Yoon's avatar
Jiwon Yoon committed
27
          />
28
          <label className="text-lg">{choice.text}</label>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
29
        </div>
30
      ))}
Lee SeoYeon's avatar
Lee SeoYeon committed
31
32
33
    </div>
  );
};