Question.tsx 2.91 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";
5
6
7
8
9
10

export interface BasicQuestionType {
  type: string;
  name: string;
  title: string;
  isRequired: boolean;
Jiwon Yoon's avatar
Jiwon Yoon committed
11
  comment: string;
12
  content: any;
Jiwon Yoon's avatar
Jiwon Yoon committed
13
  [key: string]: string | number | boolean | any;
14
15
16
17
18
}
export interface EssayType extends BasicQuestionType {}
export interface RadioType extends BasicQuestionType {
  content: {
    hasOther: boolean;
Jiwon Yoon's avatar
Jiwon Yoon committed
19
    choices: any[];
20
21
22
    otherText: string;
  };
}
Jiwon Yoon's avatar
Jiwon Yoon committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
export interface CheckboxType extends BasicQuestionType {
  content: {
    choices: any[];
    maxCount: number;
  };
}

const EssayQ: EssayType = {
  type: "assay",
  name: "Question1",
  title: "Question1",
  isRequired: false,
  comment: "질문에 대한 설명을 입력해주세요",
  content: null,
};
const RadioQ: RadioType = {
39
40
41
42
  type: "radio",
  name: "Question2",
  title: "Question2",
  isRequired: false,
Jiwon Yoon's avatar
Jiwon Yoon committed
43
  comment: "질문에 대한 설명을 입력해주세요",
44
45
46
47
48
49
  content: {
    hasOther: false,
    otherText: "",
    choices: ["1", "2", "3"],
  },
};
Jiwon Yoon's avatar
Jiwon Yoon committed
50
const CheckboxQ: CheckboxType = {
51
52
53
54
  type: "checkbox",
  name: "Question3",
  title: "Question3",
  isRequired: false,
Jiwon Yoon's avatar
Jiwon Yoon committed
55
  comment: "질문에 대한 설명을 입력해주세요",
56
57
58
59
60
61
  content: {
    choices: ["ch1", "ch2", "ch3"],
    maxCount: 2,
  },
};

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

Jiwon Yoon's avatar
Jiwon Yoon committed
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
export const Question = () => {
  const [questionList, setQuestionList] = useState<BasicQuestionType[]>([
    EssayQ,
    RadioQ,
  ]);
  // 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":
            return <QCheckbox element={element} />;
          default:
            break;
        }
        // if (element.type === "assay") {
        //   return (
        //     <QAssay element={element} QuestionListChange={QuestionListChange} />
        //   );
        // } else if (element.type === "radio") {
        //   return <QRadio element={element} />;
        // } else if (element.type === "checkbox") {
        //   return <QCheckbox element={element} />;
        // }
      })}
    </>
  );
};