Skip to content

Open source personal finance application

feat(filter): add checkbox selection with parent/child indeterminate state

Ilya Komichevbb90ead6cbe3
diff --git a/app/app/components/categories/SelectorTree.vue b/app/app/components/categories/SelectorTree.vue
index c5e8b45a..ff71c196 100644
--- a/app/app/components/categories/SelectorTree.vue
+++ b/app/app/components/categories/SelectorTree.vue
@@ -11,6 +11,7 @@ const props = defineProps<{
}>()
const emit = defineEmits<{
+ removeCategories: [ids: CategoryId[]]
selected: [id: CategoryId]
setCategories: [ids: CategoryId[]]
}>()
@@ -76,19 +77,28 @@ const hasNoMatches = computed(() =>
!!searchQuery.value && filteredRootIds.value.length === 0,
)
-function isAllChildrenSelected(rootId: CategoryId) {
+function rootCheckboxValue(rootId: CategoryId): boolean | 'indeterminate' {
+ if (!categoriesStore.hasChildren(rootId))
+ return props.selectedIds?.includes(rootId) ?? false
const children = categoriesStore.getChildrenIds(rootId)
- if (!children.length)
+ const count = children.filter(id => props.selectedIds?.includes(id)).length
+ if (count === 0)
return false
- return children.every(id => props.selectedIds?.includes(id))
+ if (count === children.length)
+ return true
+ return 'indeterminate'
}
-function rootActiveId(rootId: CategoryId): CategoryId | null {
- if (props.selectedIds?.includes(rootId))
- return rootId
- if (isAllChildrenSelected(rootId))
- return rootId
- return null
+function onRootCheckboxChange(rootId: CategoryId, value: boolean | 'indeterminate') {
+ if (!categoriesStore.hasChildren(rootId)) {
+ emit('selected', rootId)
+ return
+ }
+ const children = categoriesStore.getChildrenIds(rootId)
+ if (value === true)
+ emit('setCategories', children)
+ else
+ emit('removeCategories', children)
}
function onRootClick(rootId: CategoryId) {
@@ -99,10 +109,6 @@ function onRootClick(rootId: CategoryId) {
emit('selected', rootId)
}
-function onRootFilter(rootId: CategoryId) {
- emit('setCategories', categoriesStore.getChildrenIds(rootId))
-}
-
onMounted(async () => {
await nextTick()
searchInput.value?.focus()
@@ -152,20 +158,34 @@ onMounted(async () => {
v-for="rootId in filteredRootIds"
:key="rootId"
>
- <CategoriesItem
- :activeItemId="rootActiveId(rootId)"
- :category="categoriesStore.items[rootId]!"
- :categoryId="rootId"
- :isExpanded="isRootExpanded(rootId)"
- :isShowChevron="categoriesStore.hasChildren(rootId)"
- :leftMenuButton="true"
- :hideLeftMenuButton="true"
- :lineWidth="isRootExpanded(rootId) && categoriesStore.hasChildren(rootId) ? 0 : 1"
- class="group"
- @click="onRootClick(rootId)"
- @toggle="toggle(rootId)"
- @filter="onRootFilter(rootId)"
- />
+ <div class="group hover:bg-elevated/50 flex items-center rounded-sm select-none [&_.uiElement:hover]:bg-transparent">
+ <div
+ class="flex-center relative w-8 shrink-0 self-stretch pl-2"
+ @click.stop
+ >
+ <div
+ class="absolute inset-0 z-10"
+ @click.stop="onRootCheckboxChange(rootId, rootCheckboxValue(rootId) === true ? false : true)"
+ />
+ <UCheckbox
+ :modelValue="rootCheckboxValue(rootId)"
+ class="pointer-events-none"
+ />
+ </div>
+ <CategoriesItem
+ :activeItemId="null"
+ :category="categoriesStore.items[rootId]!"
+ :categoryId="rootId"
+ :isExpanded="isRootExpanded(rootId)"
+ :isShowChevron="categoriesStore.hasChildren(rootId)"
+ :leftMenuButton="true"
+ :hideLeftMenuButton="true"
+ :lineWidth="isRootExpanded(rootId) && categoriesStore.hasChildren(rootId) ? 0 : 1"
+ class="min-w-0 flex-1"
+ @click="onRootClick(rootId)"
+ @toggle="toggle(rootId)"
+ />
+ </div>
<div
v-if="isRootExpanded(rootId) && categoriesStore.hasChildren(rootId)"
@@ -174,18 +194,35 @@ onMounted(async () => {
: '-mt-px ml-5 pb-1 pl-3'"
>
<template v-if="view === 'list'">
- <CategoriesItem
+ <div
v-for="childId in visibleChildrenIds(rootId)"
:key="childId"
- :activeItemId="props.selectedIds?.includes(childId) ? childId : null"
- :category="categoriesStore.items[childId]!"
- :categoryId="childId"
- :leftMenuButton="true"
- :hideLeftMenuButton="true"
- :lineWidth="1"
- class="group"
+ class="group hover:bg-elevated/50 flex items-center rounded-sm select-none [&_.uiElement:hover]:bg-transparent"
@click="emit('selected', childId)"
- />
+ >
+ <div
+ class="flex-center relative w-8 shrink-0 self-stretch pl-2"
+ @click.stop
+ >
+ <div
+ class="absolute inset-0 z-10"
+ @click.stop="emit('selected', childId)"
+ />
+ <UCheckbox
+ :modelValue="props.selectedIds?.includes(childId) ?? false"
+ class="pointer-events-none"
+ />
+ </div>
+ <CategoriesItem
+ :activeItemId="null"
+ :category="categoriesStore.items[childId]!"
+ :categoryId="childId"
+ :leftMenuButton="true"
+ :hideLeftMenuButton="true"
+ :lineWidth="1"
+ class="min-w-0 flex-1"
+ />
+ </div>
</template>
<div v-else class="flex flex-wrap gap-1">
diff --git a/app/app/components/stat/date/Navigation.vue b/app/app/components/stat/date/Navigation.vue
index 2ddb9653..c8a5c818 100644
--- a/app/app/components/stat/date/Navigation.vue
+++ b/app/app/components/stat/date/Navigation.vue
@@ -77,7 +77,7 @@ function changeDate(way: 'next' | 'prev' | 'today') {
<template #content="{ close }">
<StatDateSelector
- class="min-w-[362px] pb-2 md:px-3"
+ class="min-w-[362px] pb-2"
@close="close"
/>
</template>
diff --git a/app/app/components/stat/filter/Selected.vue b/app/app/components/stat/filter/Selected.vue
index bc2fd48c..83569551 100644
--- a/app/app/components/stat/filter/Selected.vue
+++ b/app/app/components/stat/filter/Selected.vue
@@ -1,4 +1,6 @@
<script setup lang="ts">
+import type { CategoryId } from '~/components/categories/types'
+
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
import { filterKey } from '~/components/stat/injectionKeys'
import { useWalletsStore } from '~/components/wallets/useWalletsStore'
@@ -12,6 +14,43 @@ const filter = inject(filterKey)!
const walletsStore = useWalletsStore()
const categoriesStore = useCategoriesStore()
const itemClasses = 'bg-elevated/30 rounded-sm shrink-0'
+
+const displayCategoryIds = computed<CategoryId[]>(() => {
+ const selected = new Set(filter?.categoriesIds?.value ?? [])
+ if (!selected.size)
+ return []
+
+ const consumed = new Set<CategoryId>()
+ const result: CategoryId[] = []
+
+ for (const rootId of categoriesStore.categoriesRootIds) {
+ const children = categoriesStore.getChildrenIds(rootId)
+ if (!children.length)
+ continue
+
+ if (children.every(id => selected.has(id))) {
+ result.push(rootId)
+ children.forEach(id => consumed.add(id))
+ }
+ }
+
+ for (const id of (filter?.categoriesIds?.value ?? [])) {
+ if (!consumed.has(id))
+ result.push(id)
+ }
+
+ return result
+})
+
+function onCategoryClick(categoryId: CategoryId) {
+ const children = categoriesStore.getChildrenIds(categoryId)
+ const selected = filter?.categoriesIds?.value ?? []
+
+ if (children.length && children.every(id => selected.includes(id)))
+ filter.removeCategories(children)
+ else
+ filter.removeCategoryId(categoryId)
+}
</script>
<template>
@@ -25,6 +64,7 @@ const itemClasses = 'bg-elevated/30 rounded-sm shrink-0'
:wallet="walletsStore.itemsComputed[walletId]!"
insideClasses="!min-h-[38px]"
compact
+ isShowIcon
isShowCreditLimit
@click="filter.removeWalletId(walletId)"
/>
@@ -32,14 +72,14 @@ const itemClasses = 'bg-elevated/30 rounded-sm shrink-0'
<template v-if="props.isShowCategories">
<CategoriesItem
- v-for="categoryId in filter?.categoriesIds?.value"
+ v-for="categoryId in displayCategoryIds"
:key="categoryId"
:category="categoriesStore.items[categoryId]!"
:categoryId="categoryId"
:class="itemClasses"
stacked
insideClasses="!min-h-[38px]"
- @click="filter.removeCategoryId(categoryId)"
+ @click="onCategoryClick(categoryId)"
/>
</template>
</div>
diff --git a/app/app/components/stat/filter/Selector.vue b/app/app/components/stat/filter/Selector.vue
index 3ecd6264..7a1eeb89 100644
--- a/app/app/components/stat/filter/Selector.vue
+++ b/app/app/components/stat/filter/Selector.vue
@@ -44,6 +44,7 @@ const isLaptop = computed(() => width.value >= 766 && pointerType.value === 'mou
class="min-w-80"
@selected="filter.toggleCategoryId"
@setCategories="filter.setCategories"
+ @removeCategories="filter.removeCategories"
/>
</StatFilterSelectorItem>
</div>
diff --git a/app/app/components/stat/filter/useFilter.ts b/app/app/components/stat/filter/useFilter.ts
index 94b749ae..0bd7ba5b 100644
--- a/app/app/components/stat/filter/useFilter.ts
+++ b/app/app/components/stat/filter/useFilter.ts
@@ -38,6 +38,15 @@ function createQueryFilter<T extends string>(
})
}
+ function removeMultiple(idsToRemove: T[]) {
+ router.push({
+ query: {
+ ...route.query,
+ [queryKey]: ids.value.filter(id => !idsToRemove.includes(id)),
+ },
+ })
+ }
+
function removeId(id: T) {
router.push({
query: {
@@ -56,7 +65,7 @@ function createQueryFilter<T extends string>(
setId(id)
}
- return { ids, removeId, setId, setMultiple, toggleId }
+ return { ids, removeId, removeMultiple, setId, setMultiple, toggleId }
}
export function useFilter() {
@@ -78,8 +87,10 @@ export function useFilter() {
categoriesIds: categories.ids,
clearFilter,
isShow,
+ removeCategories: categories.removeMultiple,
removeCategoryId: categories.removeId,
removeWalletId: wallets.removeId,
+ removeWallets: wallets.removeMultiple,
setCategories: categories.setMultiple,
setCategoryId: categories.setId,
setWallets: wallets.setMultiple,
diff --git a/app/app/components/wallets/Selector.vue b/app/app/components/wallets/Selector.vue
index 46aa7a7d..45471916 100644
--- a/app/app/components/wallets/Selector.vue
+++ b/app/app/components/wallets/Selector.vue
@@ -139,19 +139,53 @@ onMounted(async () => {
{{ t('search.noResults') }}
</div>
- <WalletsItem
+ <template
v-for="walletId in selectedWalletsIdsWithCurrency"
:key="walletId"
- :activeItemId="props.activeItemId || (props.selectedIds?.includes(walletId) ? walletId : null)"
- :contextMenuItems="getWalletContextMenuItems(walletId)"
- :walletId
- :wallet="walletsStore.itemsComputed[walletId]!"
- :lineWidth="4"
- class="group/item"
- isShowIcon
- isShowCreditLimit
- @click="onClickWallet(walletId)"
- />
+ >
+ <div
+ v-if="props.selectedIds !== undefined"
+ class="hover:bg-elevated/50 flex items-center rounded-sm select-none [&_.uiElement:hover]:bg-transparent"
+ @click="onClickWallet(walletId)"
+ >
+ <div
+ class="flex-center relative w-10 shrink-0 self-stretch pl-2"
+ @click.stop
+ >
+ <div
+ class="absolute inset-0 z-10"
+ @click.stop="onClickWallet(walletId)"
+ />
+ <UCheckbox
+ :modelValue="props.selectedIds.includes(walletId)"
+ class="pointer-events-none"
+ />
+ </div>
+ <WalletsItem
+ :activeItemId="props.activeItemId ?? null"
+ :contextMenuItems="getWalletContextMenuItems(walletId)"
+ :walletId
+ :wallet="walletsStore.itemsComputed[walletId]!"
+ :lineWidth="4"
+ class="group/item min-w-0 flex-1"
+ isShowIcon
+ isShowCreditLimit
+ />
+ </div>
+
+ <WalletsItem
+ v-else
+ :activeItemId="props.activeItemId ?? null"
+ :contextMenuItems="getWalletContextMenuItems(walletId)"
+ :walletId
+ :wallet="walletsStore.itemsComputed[walletId]!"
+ :lineWidth="4"
+ class="group/item"
+ isShowIcon
+ isShowCreditLimit
+ @click="onClickWallet(walletId)"
+ />
+ </template>
</div>
<template v-if="walletsStore.currenciesUsed.length > 1">