RatingForm.tsx 2.69 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;
Jiwon Yoon's avatar
Jiwon Yoon committed
7
  save: boolean;
8
9
};

Jiwon Yoon's avatar
Jiwon Yoon committed
10
export const RatingForm = ({ element, handleQuestion, save }: Props) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  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() {
34
    choices.push({ text: "", value: choices.length });
Jiwon Yoon's avatar
Jiwon Yoon committed
35
36
37
    element.content.choices = choices;
    handleQuestion(element._id);
  }
38
39

  return (
Jiwon Yoon's avatar
Jiwon Yoon committed
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}
Jiwon Yoon's avatar
Jiwon Yoon committed
46
47
          placeholder="비동의"
          value={element.content.minRateDescription}
Jiwon Yoon's avatar
Jiwon Yoon committed
48
          onChange={handleContent}
Jiwon Yoon's avatar
Jiwon Yoon committed
49
          disabled={save}
50
        ></input>
Jiwon Yoon's avatar
Jiwon Yoon committed
51
        {choices.map((choice: any, index: number) => (
52
53
          <input
            name="text"
Jiwon Yoon's avatar
Jiwon Yoon committed
54
            id={`${index}`}
55
56
57
            type="text"
            className="border border-black rounded-full py-1 m-2 text-center"
            size={1}
Jiwon Yoon's avatar
Jiwon Yoon committed
58
59
            placeholder="0"
            value={choice.text}
Jiwon Yoon's avatar
Jiwon Yoon committed
60
            onChange={handleContent}
Jiwon Yoon's avatar
Jiwon Yoon committed
61
            disabled={save}
62
63
64
65
66
67
          ></input>
        ))}
        <input
          name="maxRateDescription"
          className="border-b-2 text-center"
          size={10}
Jiwon Yoon's avatar
Jiwon Yoon committed
68
69
          placeholder="동의"
          value={element.content.maxRateDescription}
Jiwon Yoon's avatar
Jiwon Yoon committed
70
          onChange={handleContent}
Jiwon Yoon's avatar
Jiwon Yoon committed
71
          disabled={save}
72
73
74
75
        ></input>
      </div>
      <div>
        <button
Jiwon Yoon's avatar
Jiwon Yoon committed
76
          type="button"
77
78
          name="rateValues"
          className="border border-red-500 rounded mx-2 px-2"
Jiwon Yoon's avatar
Jiwon Yoon committed
79
          onClick={deleteValue}
Jiwon Yoon's avatar
Jiwon Yoon committed
80
          disabled={save}
81
82
83
        >
          삭제
        </button>
Jiwon Yoon's avatar
Jiwon Yoon committed
84
85
86
87
88
        <button
          type="button"
          name="rateValues"
          className="border border-blue-500 rounded mx-2 px-2"
          onClick={addValue}
Jiwon Yoon's avatar
Jiwon Yoon committed
89
          disabled={save}
Jiwon Yoon's avatar
Jiwon Yoon committed
90
        >
91
92
93
          추가
        </button>
      </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
94
    </>
95
96
  );
};