SurveyTitleAndComment.tsx 2.2 KB
Newer Older
1
2
3
4
5
6
7
8
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import React, {
  ChangeEvent,
  ChangeEventHandler,
  MouseEventHandler,
  useState,
} from "react";

type Props = {
  // isEditing: boolean;
  title: string;
  comment: string;
  handleTitleComment: Function;
};

export const SurveyTitleAndComment = ({
  comment,
  title,
  handleTitleComment,
}: Props) => {
  const [state, setState] = useState({ title: title, comment: comment });
  const [disabled, setDisabled] = useState(true);

  console.log("title:", title, "comment:", comment, "state:", state);

  const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
    const { name, value } = e.target;
    setState({ ...state, [name]: value });
  };

  const onEdit = () => {
    setDisabled(false);
  };

  const onCancel = () => {
    setDisabled(true);
    setState({ title, comment });
  };

  const handleConfirm = () => {
    setDisabled(true);
    handleTitleComment(state);
  };

  return (
    <div
      className={`flex flex-col container w-4/5 h-auto border-2 items-center m-3 py-2 rounded-lg ${
        disabled ? "border-themeColor" : "border-red-500"
      }`}
    >
      <input
        type="text"
        name="title"
        className="font-bold text-4xl text-center m-2 border-b-2"
        placeholder="설문지 제목"
        autoComplete="on"
        value={state.title}
        disabled={disabled}
        onChange={handleChange}
      />
      <input
        type="text"
        name="comment"
        className="font-bold text-1xl text-center m-2 border-b-2 resize-none"
        placeholder="설문조사에 대한 설명을 입력해주세요"
        autoComplete="on"
        size={50}
        value={state.comment}
        disabled={disabled}
        onChange={handleChange}
      />

      <div className="flex w-11/12 justify-end">
        {disabled ? (
          <>
            <button type="button" className="px-1" onClick={onEdit}>
              수정
            </button>
          </>
        ) : (
          <>
            <button type="button" className="px-1" onClick={onCancel}>
              취소
            </button>
            <button type="button" className="px-1" onClick={handleConfirm}>
              확인
            </button>
          </>
        )}
      </div>
    </div>
  );
};