From 6096758da54315905de51e0f0997fa6fcc13bd4f Mon Sep 17 00:00:00 2001 From: Peter Laske <37439758+laske185@users.noreply.github.com> Date: Thu, 16 Nov 2023 08:49:18 +0100 Subject: [PATCH] fix: initial table sort --- .../src/components/table/component.tsx | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/components/src/components/table/component.tsx b/packages/components/src/components/table/component.tsx index 216131ba57..ef586bdf1e 100644 --- a/packages/components/src/components/table/component.tsx +++ b/packages/components/src/components/table/component.tsx @@ -178,21 +178,20 @@ export class KolTable implements API { watchValidator(this, '_headers', (value): boolean => typeof value === 'object' && value !== null, new Set(['KoliBriTableHeaders']), value, { hooks: { beforePatch: (nextValue: unknown) => { - const headers: KoliBriTableHeaders = nextValue as KoliBriTableHeaders; - headers.horizontal?.forEach((header) => { - header.forEach((cell) => { - if (typeof cell.sort === 'function' && typeof cell.sortDirection === 'string') { - this.setSortDirection(cell.sort, cell.sortDirection); - } - }); - }); - headers.vertical?.forEach((header) => { - header.forEach((cell) => { - if (typeof cell.sort === 'function' && typeof cell.sortDirection === 'string') { - this.setSortDirection(cell.sort, cell.sortDirection); + const applySort = (headers: KoliBriTableHeaderCell[]) => { + headers.forEach((cell, i) => { + const sortDirection = cell.sortDirection; + if (typeof cell.sort === 'function' && (sortDirection === 'ASC' || sortDirection === 'DESC')) { + this.setSortDirection(cell.sort, sortDirection); + setTimeout(() => this.updateSortedData({ key: cell.key || `cell-${i}`, label: cell.label, sortDirection })); } }); - }); + }; + + const headers: KoliBriTableHeaders = nextValue as KoliBriTableHeaders; + headers.horizontal?.forEach(applySort); + headers.vertical?.forEach(applySort); + if (headers.horizontal && headers.vertical && headers.horizontal?.length > 0 && headers.vertical?.length > 0) { this.disableSort = true; devHint(