DropdownForm.tsx 1.01 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import React, { useState } from "react";
2
3
4
5
import { DropdownType } from "../types";

type Props = {
  element: DropdownType;
Jiwon Yoon's avatar
Jiwon Yoon committed
6
  handleQuestion: (id: string) => void;
7
8
};

Jiwon Yoon's avatar
Jiwon Yoon committed
9
10
11
12
13
14
15
16
17
18
export const DropdownForm = ({ element, handleQuestion }: Props) => {
  const [choices, setChoices] = useState([...element.content.choices]);

  function handleContent(event: React.ChangeEvent<HTMLInputElement>) {
    const { id, value } = event.target;
    choices[+id].text = value;
    element.content.choices = choices;
    handleQuestion(element._id);
    console.log(choices);
  }
19
  return (
Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
22
23
24
25
26
    <div id="content" className="flex mt-4">
      <select className="mr-3">
        {choices.map((choice: any, index: number) => (
          <option>{choice.text}</option>
        ))}
      </select>
      {choices.map((choice: any, index: number) => (
27
        <input
Jiwon Yoon's avatar
Jiwon Yoon committed
28
          id={`${index}`}
29
          type="text"
Jiwon Yoon's avatar
Jiwon Yoon committed
30
31
32
          className="mx-2 border-b-2"
          placeholder={choice.text}
          onChange={handleContent}
33
        ></input>
Jiwon Yoon's avatar
Jiwon Yoon committed
34
      ))}
35
36
37
    </div>
  );
};