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

type Props = {
  element: RatingType;
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export const RatingForm = ({ element, handleQuestion }: Props) => {
  const [choices, setChoices] = useState([...element.content.choices]);

  function handleContent(event: React.ChangeEvent<HTMLInputElement>) {
    const { id, value, name } = event.target;
    if (name === "text") {
      choices[+id].text = value;
      element.content.choices = choices;
    } else if (name === "minRateDescription") {
      element.content = { ...element.content, minRateDescription: value };
    } else if (name === "maxRateDescription") {
      element.content = { ...element.content, maxRateDescription: value };
    }
    handleQuestion(element._id);
    console.log(choices);
  }

  function deleteValue() {
    //제일 마지막 index 제거
    choices.splice(-1, 1);
    element.content.choices = choices;
    handleQuestion(element._id);
  }
  function addValue() {
    choices.push({ text: "0", value: 0 });
    element.content.choices = choices;
    handleQuestion(element._id);
  }
37
38

  return (
Jiwon Yoon's avatar
Jiwon Yoon committed
39
    <>
40
41
42
43
44
45
      <div className="flex place-content-between items-center p-5">
        <input
          name="minRateDescription"
          className="border-b-2 text-center"
          size={10}
          placeholder={element.content.minRateDescription}
Jiwon Yoon's avatar
Jiwon Yoon committed
46
          onChange={handleContent}
47
        ></input>
Jiwon Yoon's avatar
Jiwon Yoon committed
48
        {choices.map((e: any, index: number) => (
49
50
          <input
            name="text"
Jiwon Yoon's avatar
Jiwon Yoon committed
51
            id={`${index}`}
52
53
54
55
            type="text"
            className="border border-black rounded-full py-1 m-2 text-center"
            size={1}
            placeholder={e.text}
Jiwon Yoon's avatar
Jiwon Yoon committed
56
            onChange={handleContent}
57
58
59
60
61
62
63
          ></input>
        ))}
        <input
          name="maxRateDescription"
          className="border-b-2 text-center"
          size={10}
          placeholder={element.content.maxRateDescription}
Jiwon Yoon's avatar
Jiwon Yoon committed
64
          onChange={handleContent}
65
66
67
68
        ></input>
      </div>
      <div>
        <button
Jiwon Yoon's avatar
Jiwon Yoon committed
69
          type="button"
70
71
          name="rateValues"
          className="border border-red-500 rounded mx-2 px-2"
Jiwon Yoon's avatar
Jiwon Yoon committed
72
          onClick={deleteValue}
73
74
75
        >
          삭제
        </button>
Jiwon Yoon's avatar
Jiwon Yoon committed
76
77
78
79
80
81
        <button
          type="button"
          name="rateValues"
          className="border border-blue-500 rounded mx-2 px-2"
          onClick={addValue}
        >
82
83
84
          추가
        </button>
      </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
85
    </>
86
87
  );
};