fix(tree): sort category nodes by name in flattenTree function

This commit is contained in:
Nils-Johan Gynther
2026-04-19 08:31:59 +02:00
parent a069d7d3d7
commit ee1f4c58a1
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));