From cc498e0b65807ef95e37324c85fe7432040611f6 Mon Sep 17 00:00:00 2001 From: Chintan Kavathia Date: Thu, 7 Nov 2024 18:29:50 +0530 Subject: [PATCH] refactor: remove duplicate template code --- .../lib/components/body/body-row.directive.ts | 18 +++ .../src/lib/components/body/body.component.ts | 139 ++++++------------ .../src/lib/ngx-datatable.module.ts | 7 +- projects/ngx-datatable/src/public-api.ts | 1 + 4 files changed, 73 insertions(+), 92 deletions(-) create mode 100644 projects/ngx-datatable/src/lib/components/body/body-row.directive.ts diff --git a/projects/ngx-datatable/src/lib/components/body/body-row.directive.ts b/projects/ngx-datatable/src/lib/components/body/body-row.directive.ts new file mode 100644 index 000000000..a2150d011 --- /dev/null +++ b/projects/ngx-datatable/src/lib/components/body/body-row.directive.ts @@ -0,0 +1,18 @@ +import { Directive } from '@angular/core'; +import { Group, TreeStatus } from '../../types/public.types'; + +@Directive({ + selector: '[ngx-datatable-body-row]' +}) +export class DatatableBodyRowDirective { + static ngTemplateContextGuard( + directive: DatatableBodyRowDirective, + context: unknown + ): context is { + row: TRow; + groupValue: Group[]; + index: number; + } { + return true; + } +} diff --git a/projects/ngx-datatable/src/lib/components/body/body.component.ts b/projects/ngx-datatable/src/lib/components/body/body.component.ts index 80d0de676..bb1eb1d1c 100644 --- a/projects/ngx-datatable/src/lib/components/body/body.component.ts +++ b/projects/ngx-datatable/src/lib/components/body/body.component.ts @@ -105,6 +105,46 @@ import { [selected]="selected" (rowContextmenu)="rowContextmenu.emit($event)" > + + @let rowItem = groupRow ?? group; + + + + @if (rowDefTemplate) { - + } } - - @if (isRow(group)) { - - - } - - @if (isGroup(group)) { @for (row of group.value; track rowTrackingFn(i, row); let i = $index) { - - + } } diff --git a/projects/ngx-datatable/src/lib/ngx-datatable.module.ts b/projects/ngx-datatable/src/lib/ngx-datatable.module.ts index fc48e7476..1f4686262 100644 --- a/projects/ngx-datatable/src/lib/ngx-datatable.module.ts +++ b/projects/ngx-datatable/src/lib/ngx-datatable.module.ts @@ -39,6 +39,7 @@ import { DatatableRowDefDirective, DatatableRowDefInternalDirective } from './components/body/body-row-def.component'; +import { DatatableBodyRowDirective } from './components/body/body-row.directive'; @NgModule({ imports: [CommonModule], @@ -77,7 +78,8 @@ import { DisableRowDirective, DatatableRowDefComponent, DatatableRowDefDirective, - DatatableRowDefInternalDirective + DatatableRowDefInternalDirective, + DatatableBodyRowDirective ], exports: [ DatatableComponent, @@ -95,7 +97,8 @@ import { DatatableGroupHeaderTemplateDirective, DisableRowDirective, DatatableRowDefComponent, - DatatableRowDefDirective + DatatableRowDefDirective, + DatatableBodyRowDirective ] }) export class NgxDatatableModule { diff --git a/projects/ngx-datatable/src/public-api.ts b/projects/ngx-datatable/src/public-api.ts index d46426615..d204eb007 100644 --- a/projects/ngx-datatable/src/public-api.ts +++ b/projects/ngx-datatable/src/public-api.ts @@ -29,6 +29,7 @@ export * from './lib/components/columns/tree.directive'; export * from './lib/components/row-detail/row-detail.directive'; export * from './lib/components/row-detail/row-detail-template.directive'; export * from './lib/components/body/body-row-def.component'; +export * from './lib/components/body/body-row.directive'; // directives export * from './lib/directives/draggable.directive';