feat: enhance category filter functionality to include child categories in selection and deselection; update transaction page to expand selected categories based on parent-child relationships
This commit is contained in:
@@ -87,21 +87,58 @@ export function CategoryFilterCombobox({
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if this is a parent category
|
||||
const isParentCategory = parentCategories.some((p) => p.id === newValue);
|
||||
const childCategories = isParentCategory
|
||||
? childrenByParent[newValue] || []
|
||||
: [];
|
||||
|
||||
// Category selection - toggle
|
||||
let newSelection: string[];
|
||||
|
||||
if (isAll || isUncategorized) {
|
||||
// Start fresh with just this category
|
||||
newSelection = [newValue];
|
||||
// Start fresh with this category and its children (if parent)
|
||||
if (isParentCategory && childCategories.length > 0) {
|
||||
newSelection = [
|
||||
newValue,
|
||||
...childCategories.map((child) => child.id),
|
||||
];
|
||||
} else {
|
||||
newSelection = [newValue];
|
||||
}
|
||||
} else if (value.includes(newValue)) {
|
||||
// Remove category
|
||||
newSelection = value.filter((v) => v !== newValue);
|
||||
// Remove category and its children (if parent)
|
||||
if (isParentCategory && childCategories.length > 0) {
|
||||
const childIds = childCategories.map((child) => child.id);
|
||||
newSelection = value.filter(
|
||||
(v) => v !== newValue && !childIds.includes(v)
|
||||
);
|
||||
} else {
|
||||
newSelection = value.filter((v) => v !== newValue);
|
||||
}
|
||||
if (newSelection.length === 0) {
|
||||
newSelection = ["all"];
|
||||
}
|
||||
} else {
|
||||
// Add category
|
||||
newSelection = [...value, newValue];
|
||||
// Add category and its children (if parent)
|
||||
if (isParentCategory && childCategories.length > 0) {
|
||||
const childIds = childCategories.map((child) => child.id);
|
||||
newSelection = [
|
||||
...value.filter((v) => !childIds.includes(v)), // Remove any existing children
|
||||
newValue,
|
||||
...childIds,
|
||||
];
|
||||
} else {
|
||||
// Check if this child's parent is already selected
|
||||
const category = categories.find((c) => c.id === newValue);
|
||||
if (category?.parentId && value.includes(category.parentId)) {
|
||||
// Parent is selected, so we're adding a child - keep parent
|
||||
newSelection = [...value, newValue];
|
||||
} else {
|
||||
// Regular add
|
||||
newSelection = [...value, newValue];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onChange(newSelection);
|
||||
|
||||
Reference in New Issue
Block a user