FaqAccordion.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <template>
  2. <div class="faq--accordion--wrapper">
  3. <div class="faq--accordion--container">
  4. <div v-for="(item, index) in faqItems" :key="index" class="faq--accordion--item">
  5. <button
  6. class="faq--accordion--header"
  7. @click="toggleItem(index)"
  8. :class="{ active: activeIndex === index }"
  9. >
  10. <span class="faq--question">{{ item.question }}</span>
  11. <span class="faq--toggle--icon">{{ activeIndex === index ? "−" : "+" }}</span>
  12. </button>
  13. <div v-show="activeIndex === index" class="faq--accordion--content">
  14. <div
  15. class="faq--answer"
  16. v-html="item.answer"
  17. :style="{ 'white-space': item.preline == true ? 'pre-line' : 'normal' }"
  18. ></div>
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. </template>
  24. <script setup>
  25. // Props 정의
  26. const props = defineProps({
  27. faqItems: {
  28. type: Array,
  29. required: true,
  30. default: () => [],
  31. validator: (items) => {
  32. return items.every(
  33. (item) =>
  34. item.question &&
  35. item.answer &&
  36. typeof item.question === "string" &&
  37. typeof item.answer === "string"
  38. );
  39. },
  40. },
  41. });
  42. // 활성화된 아코디언 아이템 인덱스
  43. const activeIndex = ref(null);
  44. // 아코디언 토글 함수
  45. const toggleItem = (index) => {
  46. activeIndex.value = activeIndex.value === index ? null : index;
  47. };
  48. </script>