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

4
5
type Props = {
  element: CheckboxType;
Yoon, Daeki's avatar
Yoon, Daeki committed
6
  answers: AnswersType | undefined;
Jiwon Yoon's avatar
Jiwon Yoon committed
7
  handleAnswer: () => void;
8
9
};

10
export const ACheckboxForm = ({ element, answers, handleAnswer }: Props) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
13
14
  const [answer, setAnswer] = useState("");

  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { value } = event.currentTarget;
15
16
17
18
19
    // response.answers.map((a) => {
    //   if (a.questionId === element._id) {
    //     a.answer = value;
    //   }
    // });
Yoon, Daeki's avatar
Yoon, Daeki committed
20
    answers && (answers.answer = value);
Jiwon Yoon's avatar
Jiwon Yoon committed
21
22
23
    setAnswer(value);
    handleAnswer();
  };
Lee SeoYeon's avatar
Lee SeoYeon committed
24
  return (
Lee SeoYeon's avatar
Lee SeoYeon committed
25
    <div className="flex w-full gap-2 justify-around my-3">
26
27
      {element.content.choices.map((choice) => (
        <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
28
29
30
31
32
          <input
            className="mr-2"
            type="checkbox"
            value={choice.text}
            onChange={handleChange}
Jiwon Yoon's avatar
Jiwon Yoon committed
33
            required={element.isRequired}
Jiwon Yoon's avatar
Jiwon Yoon committed
34
          />
35
          <label className="text-lg">{choice.text}</label>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
36
        </div>
37
      ))}
Lee SeoYeon's avatar
Lee SeoYeon committed
38
39
40
    </div>
  );
};