Line data Source code
1 : <script setup lang="ts">
2 : import { mapValues } from 'es-toolkit'
3 : import { computed } from 'vue'
4 : import { useI18n } from 'vue-i18n'
5 :
6 : import Button from '@comfyorg/design-system/button/Button'
7 : import Menu from '@comfyorg/design-system/menu/Menu'
8 : import type { MenuItem } from '@comfyorg/design-system/lib/menu/types'
9 :
10 : const { filterLabels } = defineProps<{
11 : filterLabels?: Record<string, string>
12 : }>()
13 :
14 2355 : const { t } = useI18n()
15 25 : const filters = defineModel<Record<string, boolean>>({ required: true })
16 25 : const allSelected = computed(() =>
17 : Object.values(filters.value).every((enabled) => enabled)
18 : )
19 25 : const items = computed<MenuItem[]>(() => [
20 : {
21 : label: t('g.all'),
22 : checked: allSelected.value,
23 : command: () => {
24 0 : filters.value = mapValues(filters.value, () => true)
25 : }
26 : },
27 : ...Object.entries(filters.value).map(([filter, enabled]) => ({
28 : label: filterLabels?.[filter] ? t(filterLabels[filter]) : filter,
29 : checked: enabled && !allSelected.value,
30 : command: () => toggleCategory(filter)
31 : }))
32 : ])
33 :
34 0 : function toggleCategory(category: string) {
35 0 : if (allSelected.value) {
36 0 : for (const k in filters.value) filters.value[k] = false
37 : }
38 0 : filters.value[category] = !filters.value[category]
39 0 : if (Object.values(filters.value).every((enabled) => !enabled)) {
40 0 : for (const k in filters.value) filters.value[k] = true
41 : }
42 : }
43 25 : </script>
44 : <template>
45 25 : <Menu :items>
46 : <template #trigger>
47 : <Button
48 : size="icon"
49 : :aria-label="$t('g.filter')"
50 : icon="icon-[lucide--list-filter]"
51 : />
52 : </template>
53 : </Menu>
54 : </template>
|