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

4
interface Props extends AnswerProps {
5
  element: DropdownType;
6
}
7

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

  const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
    const { value } = event.currentTarget;
Yoon, Daeki's avatar
Yoon, Daeki committed
13
    answers && (answers.answer = value);
Jiwon Yoon's avatar
Jiwon Yoon committed
14
15
16
    setAnswer(value);
    handleAnswer();
  };
Lee SeoYeon's avatar
Lee SeoYeon committed
17
  return (
18
    <div className="flex flex-col container w-4/5 h-auto items-center m-3 py-3">
Jiwon Yoon's avatar
Jiwon Yoon committed
19
20
21
      <select
        className="py-2 hover:bg-themeColor bg-gray-200 rounded"
        onChange={handleChange}
Jiwon Yoon's avatar
Jiwon Yoon committed
22
        required={element.isRequired}
Jiwon Yoon's avatar
Jiwon Yoon committed
23
      >
24
        {element.content.choices.map((choice) => (
Jiwon Yoon's avatar
Jiwon Yoon committed
25
          <option value={choice.text}>{choice.text}</option>
26
        ))}
Lee SeoYeon's avatar
Lee SeoYeon committed
27
28
29
30
      </select>
    </div>
  );
};