QRating.tsx 2.7 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import React, { useState } from "react";
2
import { IQuestionFormProps } from "../types";
3

4
5
6
7
8
export const QRating = ({
  element,
  handleQuestion,
  isEditing,
}: IQuestionFormProps) => {
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
  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() {
32
    choices.push({ text: "", value: choices.length });
Jiwon Yoon's avatar
Jiwon Yoon committed
33
34
35
    element.content.choices = choices;
    handleQuestion(element._id);
  }
36
37

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