|
| 1 | +<template> |
| 2 | + <div class="flex gap-4 grow"> |
| 3 | + <div class="filter-container grow"> |
| 4 | + <span class="filter-title">1차 카테고리</span> |
| 5 | + <div |
| 6 | + class="filter-dropdown" |
| 7 | + @click="toggleDropdown('main')"> |
| 8 | + <span class="grow text-center">선택</span> |
| 9 | + <CommonIcons |
| 10 | + :name="dropdownIcon" |
| 11 | + :style="{ fill: '#18181B' }" /> |
| 12 | + <ul |
| 13 | + @click.stop |
| 14 | + v-if="isMainOpened" |
| 15 | + class="filter-dropdown-option-list"> |
| 16 | + <li |
| 17 | + class="filter-dropdown-option" |
| 18 | + v-for="category in categoryList" |
| 19 | + :key="category.id" |
| 20 | + :class=" |
| 21 | + (main as number[]).includes(category.id) |
| 22 | + ? 'bg-primary1 text-white font-bold' |
| 23 | + : 'hover:bg-background-2 text-black' |
| 24 | + " |
| 25 | + @click="() => onMainClick(category)"> |
| 26 | + {{ category.content }} |
| 27 | + </li> |
| 28 | + </ul> |
| 29 | + </div> |
| 30 | + </div> |
| 31 | + <div class="filter-container grow"> |
| 32 | + <span class="filter-title">2차 카테고리</span> |
| 33 | + <div |
| 34 | + class="filter-dropdown" |
| 35 | + :class="isDisabled ? 'bg-background-2 text-disabled' : 'text-black'" |
| 36 | + @click="!isDisabled && toggleDropdown('sub')"> |
| 37 | + <span class="grow text-center">선택</span> |
| 38 | + <CommonIcons |
| 39 | + :name="dropdownIcon" |
| 40 | + :style="{ fill: isDisabled ? '#A1A1AA' : '#18181B' }" /> |
| 41 | + <ul |
| 42 | + @click.stop |
| 43 | + v-if="isSubOpened" |
| 44 | + class="filter-dropdown-option-list"> |
| 45 | + <ul |
| 46 | + class="flex flex-col gap-2" |
| 47 | + v-for="category in selectedCategoryList" |
| 48 | + :key="category.content"> |
| 49 | + <div class="w-full flex items-center gap-2"> |
| 50 | + <div class="h-[1px] grow bg-border-2" /> |
| 51 | + <span class="text-[10px] font-bold text-disabled"> |
| 52 | + {{ category.content }} |
| 53 | + </span> |
| 54 | + <div class="h-[1px] grow bg-border-2" /> |
| 55 | + </div> |
| 56 | + <li |
| 57 | + class="filter-dropdown-option" |
| 58 | + v-for="subCategory in category.subCategoryList" |
| 59 | + :key="subCategory.id" |
| 60 | + :class=" |
| 61 | + (sub as number[]).includes(subCategory.id) |
| 62 | + ? 'bg-primary1 text-white font-bold' |
| 63 | + : 'hover:bg-background-2 text-black' |
| 64 | + " |
| 65 | + @click="() => onSubClick(subCategory.id)"> |
| 66 | + {{ subCategory.content }} |
| 67 | + </li> |
| 68 | + </ul> |
| 69 | + </ul> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | +</template> |
| 74 | + |
| 75 | +<script setup lang="ts"> |
| 76 | +import { dropdownIcon } from '@/constants/iconPath' |
| 77 | +import type { Category, FilterCategory } from '@/types/common' |
| 78 | +import { computed, ref, watchEffect } from 'vue' |
| 79 | +import CommonIcons from '../common/CommonIcons.vue' |
| 80 | +
|
| 81 | +const { categoryList, main, sub } = defineProps<FilterCategory>() |
| 82 | +const emit = defineEmits(['update:main', 'update:sub']) |
| 83 | +
|
| 84 | +const isMainOpened = ref(false) |
| 85 | +const isSubOpened = ref(false) |
| 86 | +const toggleDropdown = (type: 'main' | 'sub') => |
| 87 | + type === 'main' |
| 88 | + ? (isMainOpened.value = !isMainOpened.value) |
| 89 | + : (isSubOpened.value = !isSubOpened.value) |
| 90 | +
|
| 91 | +const selectedCategoryList = ref<{ content: string; subCategoryList: Category[] }[]>([]) |
| 92 | +const isDisabled = computed(() => { |
| 93 | + return selectedCategoryList.value.length === 0 |
| 94 | +}) |
| 95 | +watchEffect(() => { |
| 96 | + if (isDisabled.value) isSubOpened.value = false |
| 97 | +}) |
| 98 | +
|
| 99 | +const onMainClick = (category: Category) => { |
| 100 | + if (selectedCategoryList.value.map(el => el.content).includes(category.content)) { |
| 101 | + selectedCategoryList.value = [...selectedCategoryList.value].filter( |
| 102 | + el => el.content !== category.content |
| 103 | + ) |
| 104 | + if (category.subCategoryList) { |
| 105 | + category.subCategoryList.forEach(el => { |
| 106 | + if ((sub as number[]).includes(el.id)) { |
| 107 | + emit('update:sub', el.id) |
| 108 | + } |
| 109 | + }) |
| 110 | + } |
| 111 | + } else { |
| 112 | + if (category.subCategoryList) { |
| 113 | + selectedCategoryList.value.push({ |
| 114 | + content: category.content, |
| 115 | + subCategoryList: category.subCategoryList |
| 116 | + }) |
| 117 | + } |
| 118 | + } |
| 119 | + emit('update:main', category.id) |
| 120 | +} |
| 121 | +const onSubClick = (value: number) => { |
| 122 | + emit('update:sub', value) |
| 123 | +} |
| 124 | +</script> |
| 125 | + |
| 126 | +<style scoped></style> |
0 commit comments