Skip to content

Commit 1641b94

Browse files
authored
feat: proper types for rows (#46)
* feat: use proper type for row related properties * fix: detect update in rows automatically if row identity changed * perf: only detect changes in rows on real changes * docs: use row related types in examples
1 parent b164538 commit 1641b94

File tree

67 files changed

+417
-341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+417
-341
lines changed

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

+15-13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
import { TableColumn } from '../../types/table-column.type';
1919
import { SortDirection } from '../../types/sort-direction.type';
2020
import { Keys } from '../../utils/keys';
21+
import { RowOrGroup } from "../../types/group.type";
22+
import { BehaviorSubject } from "rxjs";
2123

2224
export type TreeStatus = 'collapsed' | 'expanded' | 'loading' | 'disabled';
2325

@@ -70,19 +72,19 @@ export type TreeStatus = 'collapsed' | 'expanded' | 'loading' | 'disabled';
7072
</ng-template>
7173
`
7274
})
73-
export class DataTableBodyCellComponent implements DoCheck, OnDestroy {
74-
@Input() displayCheck: (row: any, column?: TableColumn, value?: any) => boolean;
75+
export class DataTableBodyCellComponent<TRow extends {level?: number} = any> implements DoCheck, OnDestroy {
76+
@Input() displayCheck: (row: RowOrGroup<TRow>, column?: TableColumn, value?: any) => boolean;
7577

76-
_disable$;
77-
@Input() set disable$(val: any) {
78+
_disable$: BehaviorSubject<boolean>;
79+
@Input() set disable$(val: BehaviorSubject<boolean>) {
7880
this._disable$ = val;
7981
this.cellContext.disable$ = val;
8082
};
8183
get disable$() {
8284
return this._disable$;
8385
}
8486

85-
@Input() set group(group: any) {
87+
@Input() set group(group: TRow[]) {
8688
this._group = group;
8789
this.cellContext.group = group;
8890
this.checkValueUpdates();
@@ -146,14 +148,14 @@ export class DataTableBodyCellComponent implements DoCheck, OnDestroy {
146148
return this._column;
147149
}
148150

149-
@Input() set row(row: any) {
151+
@Input() set row(row: RowOrGroup<TRow>) {
150152
this._row = row;
151153
this.cellContext.row = row;
152154
this.checkValueUpdates();
153155
this.cd.markForCheck();
154156
}
155157

156-
get row(): any {
158+
get row(): RowOrGroup<TRow> {
157159
return this._row;
158160
}
159161

@@ -275,15 +277,15 @@ export class DataTableBodyCellComponent implements DoCheck, OnDestroy {
275277
private _isSelected: boolean;
276278
private _sorts: any[];
277279
private _column: TableColumn;
278-
private _row: any;
279-
private _group: any;
280+
private _row: RowOrGroup<TRow>;
281+
private _group: TRow[];
280282
private _rowHeight: number;
281283
private _rowIndex: number;
282284
private _expanded: boolean;
283-
private _element: any;
285+
private _element: HTMLElement;
284286
private _treeStatus: TreeStatus;
285287

286-
constructor(element: ElementRef, private cd: ChangeDetectorRef) {
288+
constructor(element: ElementRef<HTMLElement>, private cd: ChangeDetectorRef) {
287289
this.cellContext = {
288290
onCheckboxChangeFn: this.onCheckboxChangeFn,
289291
activateFn: this.activateFn,
@@ -444,8 +446,8 @@ export class DataTableBodyCellComponent implements DoCheck, OnDestroy {
444446
this.treeAction.emit(this.row);
445447
}
446448

447-
calcLeftMargin(column: any, row: any) {
449+
calcLeftMargin(column: any, row: RowOrGroup<TRow>) {
448450
const levelIndent = column.treeLevelIndent != null ? column.treeLevelIndent : 50;
449-
return column.isTreeColumn ? row.level * levelIndent : 0;
451+
return column.isTreeColumn ? (row as TRow).level * levelIndent : 0;
450452
}
451453
}

projects/ngx-datatable/src/lib/components/body/body-row-wrapper.component.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Output
1313
} from '@angular/core';
1414
import { BehaviorSubject } from 'rxjs';
15+
import { RowOrGroup } from "../../types/group.type";
1516

1617
@Component({
1718
selector: 'datatable-row-wrapper',
@@ -44,16 +45,16 @@ import { BehaviorSubject } from 'rxjs';
4445
class: 'datatable-row-wrapper'
4546
}
4647
})
47-
export class DataTableRowWrapperComponent implements DoCheck, OnInit {
48+
export class DataTableRowWrapperComponent<TRow = any> implements DoCheck, OnInit {
4849
@Input() innerWidth: number;
4950
@Input() rowDetail: any;
5051
@Input() groupHeader: any;
5152
@Input() offsetX: number;
5253
@Input() detailRowHeight: any;
53-
@Input() row: any;
54+
@Input() row: RowOrGroup<TRow>;
5455
@Input() groupedRows: any;
55-
@Input() disableCheck: (row: any) => boolean;
56-
@Output() rowContextmenu = new EventEmitter<{ event: MouseEvent; row: any }>(false);
56+
@Input() disableCheck: (row: RowOrGroup<TRow>) => boolean;
57+
@Output() rowContextmenu = new EventEmitter<{ event: MouseEvent; row: RowOrGroup<TRow> }>(false);
5758

5859
@Input() set rowIndex(val: number) {
5960
this._rowIndex = val;
@@ -81,11 +82,11 @@ export class DataTableRowWrapperComponent implements DoCheck, OnInit {
8182
rowContext: any;
8283
disable$: BehaviorSubject<boolean>;
8384

84-
private rowDiffer: KeyValueDiffer<unknown, unknown>;
85+
private rowDiffer: KeyValueDiffer<keyof RowOrGroup<TRow>, any>;
8586
private _expanded = false;
8687
private _rowIndex: number;
8788

88-
constructor(private cd: ChangeDetectorRef, private differs: KeyValueDiffers) {
89+
constructor(private cd: ChangeDetectorRef, differs: KeyValueDiffers) {
8990
this.groupContext = {
9091
group: this.row,
9192
expanded: this.expanded,
@@ -112,9 +113,12 @@ export class DataTableRowWrapperComponent implements DoCheck, OnInit {
112113
ngDoCheck(): void {
113114
if (this.disableCheck) {
114115
const isRowDisabled = this.disableCheck(this.row);
115-
this.disable$.next(isRowDisabled);
116-
this.cd.markForCheck();
116+
if (isRowDisabled !== this.disable$.value) {
117+
this.disable$.next(isRowDisabled);
118+
this.cd.markForCheck();
119+
}
117120
}
121+
118122
if (this.rowDiffer.diff(this.row)) {
119123
this.rowContext.row = this.row;
120124
this.groupContext.group = this.row;

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { ScrollbarHelper } from '../../services/scrollbar-helper.service';
2121
import { translateXY } from '../../utils/translate';
2222
import { BehaviorSubject } from 'rxjs';
2323
import { DataTableRowWrapperComponent } from './body-row-wrapper.component';
24+
import { RowOrGroup } from "../../types/group.type";
2425

2526
@Component({
2627
selector: 'datatable-body-row',
@@ -54,7 +55,7 @@ import { DataTableRowWrapperComponent } from './body-row-wrapper.component';
5455
</div>
5556
`
5657
})
57-
export class DataTableBodyRowComponent implements DoCheck {
58+
export class DataTableBodyRowComponent<TRow = any> implements DoCheck {
5859
@Input() set columns(val: any[]) {
5960
this._columns = val;
6061
this.recalculateColumns(val);
@@ -82,12 +83,12 @@ export class DataTableBodyRowComponent implements DoCheck {
8283

8384
@Input() expanded: boolean;
8485
@Input() rowClass: any;
85-
@Input() row: any;
86-
@Input() group: any;
86+
@Input() row: RowOrGroup<TRow>;
87+
@Input() group: TRow[];
8788
@Input() isSelected: boolean;
8889
@Input() rowIndex: number;
8990
@Input() displayCheck: any;
90-
@Input() treeStatus: TreeStatus = 'collapsed';
91+
@Input() treeStatus?: TreeStatus = 'collapsed';
9192
@Input() ghostLoadingIndicator = false;
9293

9394
@Input() disable$: BehaviorSubject<boolean>;
@@ -145,7 +146,7 @@ export class DataTableBodyRowComponent implements DoCheck {
145146
@Output() activate: EventEmitter<any> = new EventEmitter();
146147
@Output() treeAction: EventEmitter<any> = new EventEmitter();
147148

148-
_element: any;
149+
_element: HTMLElement;
149150
_columnGroupWidths: any;
150151
_columnsByPin: any;
151152
_offsetX: number;
@@ -157,13 +158,13 @@ export class DataTableBodyRowComponent implements DoCheck {
157158
right: {}
158159
};
159160

160-
private _rowDiffer: KeyValueDiffer<unknown, unknown>;
161+
private _rowDiffer: KeyValueDiffer<keyof RowOrGroup<TRow>, any>;
161162

162163
constructor(
163-
private differs: KeyValueDiffers,
164+
differs: KeyValueDiffers,
164165
@SkipSelf() private scrollbarHelper: ScrollbarHelper,
165166
private cd: ChangeDetectorRef,
166-
element: ElementRef
167+
element: ElementRef<HTMLElement>
167168
) {
168169
this._element = element.nativeElement;
169170
this._rowDiffer = differs.find({}).create();

0 commit comments

Comments
 (0)