Accordion.tsx 2.08 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
import React, { useState, useRef, useEffect } from "react";
import { BasicQuestionType } from "../types";
import { REssayForm } from "./REssayForm";
import { RCheckboxForm } from "./RCheckboxForm";
import { RRadioForm } from "./RRadioForm";
import { RDropdownForm } from "./RDropdownForm";
import { RFileForm } from "./RFileForm";
import { RRatingForm } from "./RRatingForm";
import { RDateForm } from "./RDateForm";
type AccordionProps = {
  question: BasicQuestionType;
};
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");
  };
  function getContent(question: BasicQuestionType) {
    switch (question.type) {
      case "essay":
        return <REssayForm question={question} />;
      case "radio":
        return <RRadioForm question={question} />;
      case "checkbox":
        return <RCheckboxForm question={question} />;
      case "dropdown":
        return <RDropdownForm question={question} />;
      case "file":
        return <RFileForm question={question} />;
      case "rating":
        return <RRatingForm question={question} />;
      case "date":
        return <RDateForm question={question} />;
      default:
        return <></>;
    }
  }
  console.log(question);
  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"
        >
          {question.answers && getContent(question)}
        </div>
      </div>
    </div>
  );
};

export default Accordion;