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

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

Jiwon Yoon's avatar
Jiwon Yoon committed
10
11
12
13
14
15
16
17
18
19
20
21
22
export const ACheckboxForm = ({ 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 (
Lee SeoYeon's avatar
Lee SeoYeon committed
24
    <div className="flex w-full gap-2 justify-around my-3">
25
26
      {element.content.choices.map((choice) => (
        <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
27
28
29
30
31
          <input
            className="mr-2"
            type="checkbox"
            value={choice.text}
            onChange={handleChange}
Jiwon Yoon's avatar
Jiwon Yoon committed
32
            required={element.isRequired}
Jiwon Yoon's avatar
Jiwon Yoon committed
33
          />
34
          <label className="text-lg">{choice.text}</label>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
35
        </div>
36
      ))}
Lee SeoYeon's avatar
Lee SeoYeon committed
37
38
39
    </div>
  );
};