ACheckbox.tsx 988 Bytes
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 (
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
          <input
            className="mr-2"
            type="checkbox"
            value={choice.text}
            onChange={handleChange}
          />
33
          <label className="text-lg">{choice.text}</label>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
34
        </div>
35
      ))}
Lee SeoYeon's avatar
Lee SeoYeon committed
36
37
38
    </div>
  );
};