Accordion.tsx 2.01 KB
Newer Older
1
import React, { useState, useRef, useEffect } from "react";
Yoon, Daeki's avatar
Yoon, Daeki committed
2
import { IQuestionData } from "../types";
3
4
5
6
7
8
9
10
11
12
// import {
//   REssay,
//   RCheckbox,
//   RRadio,
//   RDropdown,
//   RFile,
//   RRating,
//   RDate,
// } from "../forms";
import { getResultElementByType } from "../helpers/question.helper";
Yoon, Daeki's avatar
Yoon, Daeki committed
13

14
type AccordionProps = {
Yoon, Daeki's avatar
Yoon, Daeki committed
15
  question: IQuestionData;
16
};
Yoon, Daeki's avatar
Yoon, Daeki committed
17

18
19
20
21
22
23
24
25
26
export const Accordion = ({ question }: AccordionProps) => {
  const [isOpened, setOpened] = useState<boolean>(false);
  const [height, setHeight] = useState<string>("0px");
  const contentElement = useRef<HTMLDivElement>(null);

  const HandleOpening = () => {
    setOpened(!isOpened);
    setHeight(!isOpened ? `${contentElement.current?.scrollHeight}px` : "0px");
  };
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  // function getContent(question: IQuestionData) {
  //   switch (question.type) {
  //     case "singletext":
  //       return <REssay question={question} />;
  //     case "radio":
  //       return <RRadio question={question} />;
  //     case "checkbox":
  //       return <RCheckbox question={question} />;
  //     case "dropdown":
  //       return <RDropdown question={question} />;
  //     case "file":
  //       return <RFile question={question} />;
  //     case "rating":
  //       return <RRating question={question} />;
  //     case "date":
  //       return <RDate question={question} />;
  //     default:
  //       return <></>;
  //   }
  // }
Yoon, Daeki's avatar
Yoon, Daeki committed
47
48
49

  // console.log(question);

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  return (
    <div className="p-1">
      <div onClick={HandleOpening}>
        <div
          className={
            "bg-themeColor rounded-r-lg p-4 flex justify-between text-white"
          }
        >
          <h4 className="font-semibold">{question.title}</h4>
          {isOpened ? "" : ""}
        </div>
        <div
          ref={contentElement}
          style={{ height: height }}
          className="bg-gray-100 overflow-hidden transition-all duration-300"
        >
66
          {question.answers && getResultElementByType(question)}
67
68
69
70
71
72
73
        </div>
      </div>
    </div>
  );
};

export default Accordion;