Compare commits

...

2 Commits

Author SHA1 Message Date
Nils-Johan Gynther 0ae91917f0 Merge branch 'main' of ssh://gitea.gynther.se:2222/nilsjohan/recipe-app 2026-04-19 09:27:07 +02:00
Nils-Johan Gynther ee1f4c58a1 fix(tree): sort category nodes by name in flattenTree function 2026-04-19 08:31:59 +02:00
2 changed files with 4 additions and 2 deletions
@@ -18,7 +18,8 @@ const sortOptions = [
function flattenTree(nodes: CategoryNode[], depth = 0): { id: number; label: string }[] { function flattenTree(nodes: CategoryNode[], depth = 0): { id: number; label: string }[] {
const result: { id: number; label: string }[] = []; const result: { id: number; label: string }[] = [];
for (const node of nodes) { const sorted = [...nodes].sort((a, b) => a.name.localeCompare(b.name, 'sv'));
for (const node of sorted) {
result.push({ id: node.id, label: '\u00a0\u00a0'.repeat(depth) + (depth > 0 ? '↳ ' : '') + node.name }); result.push({ id: node.id, label: '\u00a0\u00a0'.repeat(depth) + (depth > 0 ? '↳ ' : '') + node.name });
if (node.children?.length) result.push(...flattenTree(node.children, depth + 1)); if (node.children?.length) result.push(...flattenTree(node.children, depth + 1));
} }
@@ -53,7 +53,8 @@ export default function EditProductForm({ product }: Props) {
// Bygg flat lista för select med indragna nivåer // Bygg flat lista för select med indragna nivåer
function flattenTree(nodes: CategoryNode[], depth = 0): { id: number; name: string; label: string }[] { function flattenTree(nodes: CategoryNode[], depth = 0): { id: number; name: string; label: string }[] {
const result: { id: number; name: string; label: string }[] = []; const result: { id: number; name: string; label: string }[] = [];
for (const node of nodes) { const sorted = [...nodes].sort((a, b) => a.name.localeCompare(b.name, 'sv'));
for (const node of sorted) {
const prefix = depth === 0 ? '' : depth === 1 ? '\u00a0\u00a0\u00a0↳ ' : '\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0↳ '; const prefix = depth === 0 ? '' : depth === 1 ? '\u00a0\u00a0\u00a0↳ ' : '\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0↳ ';
result.push({ id: node.id, name: node.name, label: prefix + node.name }); result.push({ id: node.id, name: node.name, label: prefix + node.name });
result.push(...flattenTree(node.children, depth + 1)); result.push(...flattenTree(node.children, depth + 1));