-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add recursive component to handle nested category tree
Signed-off-by: Robert Goniszewski <robertgoniszewski@outlook.com>
- Loading branch information
1 parent
a2d79f6
commit 85abb8c
Showing
4 changed files
with
48 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/lib/components/CategoryTreeItem/CategoryTreeItem.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<script lang="ts"> | ||
import type { Category } from '$lib/types/Category.type'; | ||
import Icon from '../Icon/Icon.svelte'; | ||
export let nestedTimes = 0; | ||
export let children = [] as Category[]; | ||
export let name = ''; | ||
export let icon = null as string | null; | ||
export let slug = ''; | ||
export let color = ''; | ||
</script> | ||
|
||
<div class="flex items-center" style={`margin-left: ${nestedTimes + 1}rem;`}> | ||
{#if !icon} | ||
<div class="my-auto h-4 w-4 rounded-full" style={`background-color: ${color || '#a0a0a0'};`} /> | ||
{:else} | ||
<Icon name={icon} size={16} {color} /> | ||
{/if} | ||
<a href={`/categories/${slug}`} class="link m-1 hover:text-primary">{name}</a> | ||
</div> | ||
|
||
{#each children as child} | ||
<svelte:self nestedTimes={nestedTimes + 1} {...child} /> | ||
{/each} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { Category } from '$lib/types/Category.type'; | ||
|
||
export function buildCategoryTree( | ||
categories: Category[], | ||
parent?: Category | ||
): (Category & { children?: Category[] })[] { | ||
return categories | ||
.filter((c) => c.parent?.id === parent?.id) | ||
.map((c) => ({ | ||
...c, | ||
children: buildCategoryTree(categories, c) | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters