Question.tsx 3.86 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
3
4
import React, { useState } from "react";
import { QAssay } from "./QAssay";
import { QCheckbox } from "./QCheckbox";
import { QRadio } from "./QRadio";
Jiwon Yoon's avatar
Jiwon Yoon committed
5
6
import { QDropdown } from "./QDropdown";
import { QFile } from "./QFile";
7
8
9
10
11
12

export interface BasicQuestionType {
  type: string;
  name: string;
  title: string;
  isRequired: boolean;
Jiwon Yoon's avatar
Jiwon Yoon committed
13
  comment: string;
14
  content: any;
Jiwon Yoon's avatar
Jiwon Yoon committed
15
  [key: string]: string | number | boolean | any;
16
17
18
19
20
}
export interface EssayType extends BasicQuestionType {}
export interface RadioType extends BasicQuestionType {
  content: {
    hasOther: boolean;
Jiwon Yoon's avatar
Jiwon Yoon committed
21
    choices: string[];
22
23
24
    otherText: string;
  };
}
Jiwon Yoon's avatar
Jiwon Yoon committed
25
26
export interface CheckboxType extends BasicQuestionType {
  content: {
Jiwon Yoon's avatar
Jiwon Yoon committed
27
    choices: string[];
Jiwon Yoon's avatar
Jiwon Yoon committed
28
29
30
    maxCount: number;
  };
}
Jiwon Yoon's avatar
Jiwon Yoon committed
31
32
33
34
35
36
37
38
39
40
41
42
export interface DropdownType extends BasicQuestionType {
  content: {
    choices: string[];
    hasNone: boolean;
  };
}
export interface FileType extends BasicQuestionType {
  content: {
    filename: string;
    value: string;
  };
}
Jiwon Yoon's avatar
Jiwon Yoon committed
43
44
45
46
47
48
49
50
51
const EssayQ: EssayType = {
  type: "assay",
  name: "Question1",
  title: "Question1",
  isRequired: false,
  comment: "질문에 대한 설명을 입력해주세요",
  content: null,
};
const RadioQ: RadioType = {
52
53
54
55
  type: "radio",
  name: "Question2",
  title: "Question2",
  isRequired: false,
Jiwon Yoon's avatar
Jiwon Yoon committed
56
  comment: "질문에 대한 설명을 입력해주세요",
57
58
59
  content: {
    hasOther: false,
    otherText: "",
Jiwon Yoon's avatar
Jiwon Yoon committed
60
    choices: ["radio1", "radio2", "radio3"],
61
62
  },
};
Jiwon Yoon's avatar
Jiwon Yoon committed
63
const CheckboxQ: CheckboxType = {
64
65
66
67
  type: "checkbox",
  name: "Question3",
  title: "Question3",
  isRequired: false,
Jiwon Yoon's avatar
Jiwon Yoon committed
68
  comment: "질문에 대한 설명을 입력해주세요",
69
  content: {
Jiwon Yoon's avatar
Jiwon Yoon committed
70
    choices: ["check1", "check2", "check3"],
71
72
73
    maxCount: 2,
  },
};
Jiwon Yoon's avatar
Jiwon Yoon committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const DropdownQ: DropdownType = {
  type: "dropdown",
  name: "Question4",
  title: "Question4",
  isRequired: false,
  comment: "질문에 대한 설명을 입력해주세요",
  content: {
    choices: ["drop1", "drop2", "drop3"],
    hasNone: false,
  },
};
const FileQ: FileType = {
  type: "file",
  name: "Question5",
  title: "Question5",
  isRequired: false,
  comment: "질문에 대한 설명을 입력해주세요",
  content: {
    filename: "",
    value: "",
  },
};
96

Jiwon Yoon's avatar
Jiwon Yoon committed
97
// const questionList: BasicQuestionType[] = [EssayQ, RadioQ, CheckboxQ];
98

Jiwon Yoon's avatar
Jiwon Yoon committed
99
100
101
102
export const Question = () => {
  const [questionList, setQuestionList] = useState<BasicQuestionType[]>([
    EssayQ,
    RadioQ,
Jiwon Yoon's avatar
Jiwon Yoon committed
103
104
105
    CheckboxQ,
    DropdownQ,
    FileQ,
Jiwon Yoon's avatar
Jiwon Yoon committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  ]);
  // const [survey, setSurvey] = useState();

  function QuestionListChange(e: React.ChangeEvent<HTMLInputElement>): void {
    const newList: BasicQuestionType[] = [...questionList];
    const targetId: any = e.target.id;
    const obj: any = newList.find((a) => a.name === e.target.name);
    obj[targetId] = e.target.value;
    setQuestionList(newList);
  }

  return (
    <>
      {console.log(questionList)}
      {questionList.map((element) => {
        switch (element.type) {
          case "assay":
            return (
              <QAssay
                element={element}
                QuestionListChange={QuestionListChange}
              />
            );
          case "radio":
            return (
              <QRadio
                element={element}
                QuestionListChange={QuestionListChange}
              />
            );
          case "checkbox":
Jiwon Yoon's avatar
Jiwon Yoon committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
            return (
              <QCheckbox
                element={element}
                QuestionListChange={QuestionListChange}
              />
            );
          case "dropdown":
            return (
              <QDropdown
                element={element}
                QuestionListChange={QuestionListChange}
              />
            );
          case "file":
            return (
              <QFile
                element={element}
                QuestionListChange={QuestionListChange}
              />
            );
Jiwon Yoon's avatar
Jiwon Yoon committed
157
158
159
160
161
162
163
          default:
            break;
        }
      })}
    </>
  );
};