ADropdownForm.tsx 1.05 KB
Newer Older
1
import React, { useState } from "react";
2
import { DropdownType, AnswersType } from "../types";
Lee SeoYeon's avatar
Lee SeoYeon committed
3

4
5
type Props = {
  element: DropdownType;
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 ADropdownForm = ({ element, handleAnswer, answers }: Props) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
13
14
  const [answer, setAnswer] = useState("");

  const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
    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 (
25
    <div className="flex flex-col container w-4/5 h-auto items-center m-3 py-3">
Jiwon Yoon's avatar
Jiwon Yoon committed
26
27
28
      <select
        className="py-2 hover:bg-themeColor bg-gray-200 rounded"
        onChange={handleChange}
Jiwon Yoon's avatar
Jiwon Yoon committed
29
        required={element.isRequired}
Jiwon Yoon's avatar
Jiwon Yoon committed
30
      >
31
        {element.content.choices.map((choice) => (
Jiwon Yoon's avatar
Jiwon Yoon committed
32
          <option value={choice.text}>{choice.text}</option>
33
        ))}
Lee SeoYeon's avatar
Lee SeoYeon committed
34
35
36
37
      </select>
    </div>
  );
};