DropdownForm.tsx 2.06 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;
Yoon, Daeki's avatar
Yoon, Daeki committed
7
  isEditing: boolean;
8
9
};

Yoon, Daeki's avatar
Yoon, Daeki committed
10
export const DropdownForm = ({ element, handleQuestion, isEditing }: Props) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
13
14
15
16
17
18
19
  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);
  }
Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
22
23
24
25
26
27
28
29
30
  function deleteValue() {
    //제일 마지막 index 제거
    choices.splice(-1, 1);
    element.content.choices = choices;
    handleQuestion(element._id);
  }
  function addValue() {
    choices.push({ text: "", value: choices.length });
    element.content.choices = choices;
    handleQuestion(element._id);
  }
31
  return (
Jiwon Yoon's avatar
Jiwon Yoon committed
32
    <>
jang dong hyeok's avatar
jang dong hyeok committed
33
      <div id="content" className="mt-4 p-5">
Jiwon Yoon's avatar
Jiwon Yoon committed
34
35
        <select className="mr-3">
          {choices.map((choice: any, index: number) => (
jang dong hyeok's avatar
jang dong hyeok committed
36
            <option key={choice.value}>{choice.text}</option>
Jiwon Yoon's avatar
Jiwon Yoon committed
37
38
          ))}
        </select>
Jiwon Yoon's avatar
Jiwon Yoon committed
39
        {choices.map((choice: any, index: number) => (
jang dong hyeok's avatar
jang dong hyeok committed
40
          <div key={choice.value} className="my-5">
Jiwon Yoon's avatar
Jiwon Yoon committed
41
42
43
44
45
46
47
            <input
              id={`${index}`}
              type="text"
              className="mx-2 border-b-2"
              placeholder="선택지"
              value={choice.text}
              onChange={handleContent}
Yoon, Daeki's avatar
Yoon, Daeki committed
48
              disabled={!isEditing}
Jiwon Yoon's avatar
Jiwon Yoon committed
49
50
            ></input>
          </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
51
        ))}
Jiwon Yoon's avatar
Jiwon Yoon committed
52
53
54
55
56
57
58
      </div>
      <div>
        <button
          type="button"
          name="rateValues"
          className="border border-red-500 rounded mx-2 px-2"
          onClick={deleteValue}
Yoon, Daeki's avatar
Yoon, Daeki committed
59
          disabled={!isEditing}
Jiwon Yoon's avatar
Jiwon Yoon committed
60
61
62
63
64
65
66
67
        >
          삭제
        </button>
        <button
          type="button"
          name="rateValues"
          className="border border-blue-500 rounded mx-2 px-2"
          onClick={addValue}
Yoon, Daeki's avatar
Yoon, Daeki committed
68
          disabled={!isEditing}
Jiwon Yoon's avatar
Jiwon Yoon committed
69
70
71
72
73
        >
          추가
        </button>
      </div>
    </>
74
75
  );
};