ADropdown.tsx 1005 Bytes
Newer Older
1
import React, { useState } from "react";
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { DropdownType, AnswerType } from "../types";
Lee SeoYeon's avatar
Lee SeoYeon committed
3

4
5
type Props = {
  element: DropdownType;
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 ADropdownForm = ({ element, handleAnswer, response }: Props) => {
  const [answer, setAnswer] = useState("");

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