Skip to content

Commit a20cf80

Browse files
committed
refactor: use signal queries for internal properties
The breaking change note covers future changes to prevent duplicate announcement in the changelog. BREAKING CHANGE: Several fields and methods are removed from the public API. Those fields were never intended to be exposed.
1 parent bffcbec commit a20cf80

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

projects/ngx-datatable/src/lib/components/datatable.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
[disableRowCheck]="disableRowCheck()"
6969
[rowDraggable]="rowDraggable()"
7070
[rowDragEvents]="rowDragEvents"
71-
[rowDefTemplate]="rowDefTemplate"
71+
[rowDefTemplate]="_rowDefTemplate()"
7272
[cssClasses]="cssClasses()"
7373
[(selected)]="selected"
7474
(page)="onBodyPage($event)"
@@ -99,7 +99,7 @@
9999
[pageSize]="pageSize"
100100
[offset]="offset"
101101
[footerHeight]="footerHeight()"
102-
[footerTemplate]="footer"
102+
[footerTemplate]="_footer()"
103103
[totalMessage]="messages().totalMessage ?? 'total'"
104104
[pagerLeftArrowIcon]="cssClasses().pagerLeftArrow"
105105
[pagerRightArrowIcon]="cssClasses().pagerRightArrow"

projects/ngx-datatable/src/lib/components/datatable.component.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Component,
77
computed,
88
ContentChild,
9+
contentChild,
910
contentChildren,
1011
DoCheck,
1112
effect,
@@ -26,8 +27,7 @@ import {
2627
signal,
2728
TemplateRef,
2829
untracked,
29-
viewChild,
30-
ViewChild
30+
viewChild
3131
} from '@angular/core';
3232
import { Subscription } from 'rxjs';
3333

@@ -609,34 +609,27 @@ export class DatatableComponent<TRow extends Row = any>
609609

610610
/**
611611
* Footer template gathered from the ContentChild
612+
* @internal
612613
*/
613-
@ContentChild(DatatableFooterDirective)
614-
footer?: DatatableFooterDirective;
614+
readonly _footer = contentChild(DatatableFooterDirective);
615615

616-
/**
617-
* Reference to the body component for manually
618-
* invoking functions on the body.
619-
*/
620-
@ViewChild(DataTableBodyComponent)
621-
bodyComponent!: DataTableBodyComponent<TRow & { treeStatus?: TreeStatus }>;
622-
623-
/**
624-
* Reference to the header component for manually
625-
* invoking functions on the header.
626-
*/
627-
@ViewChild(DataTableHeaderComponent)
628-
headerComponent!: DataTableHeaderComponent;
616+
private readonly _bodyComponent =
617+
viewChild.required<DataTableBodyComponent<TRow & { treeStatus?: TreeStatus }>>(
618+
DataTableBodyComponent
619+
);
629620

630-
@ViewChild(DataTableHeaderComponent, { read: ElementRef })
631-
headerElement?: ElementRef<HTMLElement>;
621+
private readonly _headerElement = viewChild(DataTableHeaderComponent, {
622+
read: ElementRef<HTMLElement>
623+
});
632624

633-
@ViewChild(DataTableBodyComponent, { read: ElementRef })
634-
private bodyElement!: ElementRef<HTMLElement>;
625+
private readonly _bodyElement = viewChild.required(DataTableBodyComponent, {
626+
read: ElementRef<HTMLElement>
627+
});
635628

636-
@ContentChild(DatatableRowDefDirective, {
629+
/** @internal */
630+
readonly _rowDefTemplate = contentChild(DatatableRowDefDirective, {
637631
read: TemplateRef
638-
})
639-
rowDefTemplate?: TemplateRef<any>;
632+
});
640633

641634
/**
642635
* Returns if all rows are selected.
@@ -645,9 +638,9 @@ export class DatatableComponent<TRow extends Row = any>
645638
const selected = this.selected();
646639
let allRowsSelected = this.rows() && selected && selected.length === this.rows()!.length;
647640

648-
if (this.bodyComponent && this.selectAllRowsOnPage()) {
649-
const indexes = this.bodyComponent.indexes;
650-
const rowsOnPage = indexes().last - indexes().first;
641+
if (this.selectAllRowsOnPage()) {
642+
const { first, last } = this._bodyComponent().indexes();
643+
const rowsOnPage = last - first;
651644
allRowsSelected = selected.length === rowsOnPage;
652645
}
653646

@@ -660,8 +653,9 @@ export class DatatableComponent<TRow extends Row = any>
660653
readonly bodyHeight = computed(() => {
661654
if (this.scrollbarV()) {
662655
let height = this.dimensions().height;
663-
if (this.headerElement) {
664-
height = height - this.headerElement.nativeElement.getBoundingClientRect().height;
656+
const headerElement = this._headerElement();
657+
if (headerElement) {
658+
height = height - headerElement.nativeElement.getBoundingClientRect().height;
665659
}
666660
return height - this.footerHeight();
667661
}
@@ -920,8 +914,8 @@ export class DatatableComponent<TRow extends Row = any>
920914
if (!width) {
921915
return [];
922916
}
923-
const bodyElement = this.bodyElement?.nativeElement;
924-
this.verticalScrollVisible = bodyElement?.scrollHeight > bodyElement?.clientHeight;
917+
const { scrollHeight, clientHeight } = this._bodyElement().nativeElement;
918+
this.verticalScrollVisible = scrollHeight > clientHeight;
925919
if (this.scrollbarV() || this.scrollbarVDynamic()) {
926920
width =
927921
width -
@@ -1001,7 +995,7 @@ export class DatatableComponent<TRow extends Row = any>
1001995
*/
1002996
onFooterPage(event: PagerPageEvent) {
1003997
this.offset = event.page - 1;
1004-
this.bodyComponent.updateOffsetY(this.offset);
998+
this._bodyComponent().updateOffsetY(this.offset);
1005999

10061000
this.page.emit({
10071001
count: this.count(),
@@ -1156,7 +1150,7 @@ export class DatatableComponent<TRow extends Row = any>
11561150

11571151
// Always go to first page when sorting to see the newly sorted data
11581152
this.offset = 0;
1159-
this.bodyComponent.updateOffsetY(this.offset);
1153+
this._bodyComponent().updateOffsetY(this.offset);
11601154
// Emit the page object with updated offset value
11611155
this.page.emit({
11621156
count: this.count(),
@@ -1172,10 +1166,9 @@ export class DatatableComponent<TRow extends Row = any>
11721166
* Toggle all row selection
11731167
*/
11741168
onHeaderSelect(): void {
1175-
if (this.bodyComponent && this.selectAllRowsOnPage()) {
1169+
if (this.selectAllRowsOnPage()) {
11761170
// before we splice, chk if we currently have all selected
1177-
const first = this.bodyComponent.indexes().first;
1178-
const last = this.bodyComponent.indexes().last;
1171+
const { first, last } = this._bodyComponent().indexes();
11791172
const allSelected = this.selected().length === last - first;
11801173

11811174
// do the opposite here

0 commit comments

Comments
 (0)