SurveyTitle.tsx 1.69 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
import React, {
  ChangeEvent,
  ChangeEventHandler,
  MouseEventHandler,
  useState,
} from "react";

type Props = {
  // isEditing: boolean;
  text: string;
  handleTitle: Function;
};

export const SurveyTitle = ({ text, handleTitle }: Props) => {
  const [title, setTitle] = useState(text);
  const [disabled, setDisabled] = useState(true);

  const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
    const { name, value } = e.target;
    setTitle(value);
  };

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

  const onCancel = () => {
    setDisabled(true);
    setTitle(text);
  };

  const handleClick = () => {
    console.log("title", title);
    setDisabled(true);
    handleTitle(title);
  };

  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"
47
        className="w-11/12 md:w-1/2 font-bold text-4xl text-center m-2 border-b-2"
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
        placeholder="설문지 제목"
        autoComplete="on"
        value={title}
        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={handleClick}>
              확인
            </button>
          </>
        )}
      </div>
    </div>
  );
};