Skip to content

Commit db29d71

Browse files
authored
Merge pull request #9521 from IgniteUI/hanastasov/fix-rowtype-interface-strictmode-m
fix(grid): use consistent return types to pass checks in strict mode
2 parents 30d3269 + 2f6d862 commit db29d71

File tree

7 files changed

+218
-56
lines changed

7 files changed

+218
-56
lines changed

projects/igniteui-angular/src/lib/grids/common/row.interface.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ export interface RowType {
2222
expanded?: boolean;
2323
deleted?: boolean;
2424
inEditMode?: boolean;
25-
children?: ITreeGridRecord[];
26-
parent?: ITreeGridRecord;
25+
children?: RowType[];
26+
parent?: RowType;
2727
hasChildren?: boolean;
28+
treeRow? : ITreeGridRecord;
2829
grid: GridType;
2930
update?: (value: any) => void;
3031
delete?: () => any;

projects/igniteui-angular/src/lib/grids/grid-public-row.ts

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,6 @@ abstract class BaseRow implements RowType {
164164
return this.grid.gridAPI.row_deleted_transaction(this.key);
165165
}
166166

167-
/**
168-
* Returns the child rows. Always returns null for IgxGridRow.
169-
*/
170-
public get children(): null | ITreeGridRecord[] {
171-
return null;
172-
}
173-
174-
/**
175-
* Returns the parent row, if any. Always returns null for IgxGridRow.
176-
*/
177-
public get parent(): null | ITreeGridRecord {
178-
return null;
179-
}
180-
181167
/**
182168
* Returns if the row has child rows. Always return false for IgxGridRow.
183169
*/
@@ -269,6 +255,26 @@ export class IgxGridRow extends BaseRow implements RowType {
269255
}
270256
return this.index + this.grid.page * this.grid.perPage;
271257
}
258+
259+
/**
260+
* Returns the parent row, if grid is grouped.
261+
*/
262+
public get parent(): RowType {
263+
let parent: IgxGroupByRow;
264+
if (!this.grid.groupingExpressions.length) {
265+
return undefined;
266+
}
267+
268+
let i = this.index - 1;
269+
while (i >= 0 && !parent) {
270+
const rec = this.grid.dataView[i];
271+
if (this.grid.isGroupByRecord(rec)) {
272+
parent = new IgxGroupByRow(this.grid, i, rec);
273+
}
274+
i--;
275+
}
276+
return parent;
277+
}
272278
}
273279

274280
export class IgxTreeGridRow extends BaseRow implements RowType {
@@ -299,15 +305,23 @@ export class IgxTreeGridRow extends BaseRow implements RowType {
299305
/**
300306
* Returns the child rows.
301307
*/
302-
public get children(): ITreeGridRecord[] {
303-
return this.treeRow.children;
308+
public get children(): RowType[] {
309+
const children: IgxTreeGridRow[] = [];
310+
if (this.treeRow.expanded) {
311+
this.treeRow.children.forEach((rec, i) => {
312+
const row = new IgxTreeGridRow(this.grid, this.index + 1 + i, rec.data);
313+
children.push(row);
314+
});
315+
}
316+
return children;
304317
}
305318

306319
/**
307320
* Returns the parent row.
308321
*/
309-
public get parent(): ITreeGridRecord {
310-
return this.treeRow.parent;
322+
public get parent(): RowType {
323+
const row = this.grid.getRowByKey(this.treeRow.parent.rowID);
324+
return row;
311325
}
312326

313327
/**
@@ -428,6 +442,18 @@ export class IgxGroupByRow implements RowType {
428442
return this._groupRow ? this._groupRow : this.grid.dataView[this.index];
429443
}
430444

445+
/**
446+
* Returns the child rows.
447+
*/
448+
public get children(): RowType[] {
449+
const children: IgxGridRow[] = [];
450+
this.groupRow.records.forEach((rec, i) => {
451+
const row = new IgxGridRow(this.grid, this.index + 1 + i, rec);
452+
children.push(row);
453+
});
454+
return children;
455+
}
456+
431457
/**
432458
* @hidden
433459
*/
@@ -437,6 +463,37 @@ export class IgxGroupByRow implements RowType {
437463
this.isGroupByRow = true;
438464
}
439465

466+
/**
467+
* Gets whether the row is selected.
468+
* Default value is `false`.
469+
* ```typescript
470+
* row.selected = true;
471+
* ```
472+
*/
473+
public get selected(): boolean {
474+
return this.children.every(row => row.selected);
475+
}
476+
477+
/**
478+
* Sets whether the row is selected.
479+
* Default value is `false`.
480+
* ```typescript
481+
* row.selected = !row.selected;
482+
* ```
483+
*/
484+
public set selected(val: boolean) {
485+
if (val) {
486+
this.children.forEach(row => {
487+
this.grid.selectionService.selectRowsWithNoEvent([row.key]);
488+
});
489+
} else {
490+
this.children.forEach(row => {
491+
this.grid.selectionService.deselectRowsWithNoEvent([row.key]);
492+
});
493+
}
494+
this.grid.cdr.markForCheck();
495+
}
496+
440497
/**
441498
* Gets/sets whether the group row is expanded.
442499
* ```typescript

projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts

Lines changed: 85 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,10 @@ describe('IgxGrid Component Tests #grid', () => {
19691969
const virtRowsLength = grid.dataRowList.length;
19701970
const indexToCompare = 32;
19711971

1972+
let firstRow = grid.getRowByIndex(0);
1973+
let secondRow = grid.getRowByIndex(1);
1974+
let thirdRow = grid.getRowByIndex(2);
1975+
19721976
expect(indexToCompare > virtRowsLength).toBe(true);
19731977
// Check if the comparable row is within the virt container
19741978
expect(grid.gridAPI.get_row_by_index(virtRowsLength - 1) instanceof IgxGridRowComponent).toBe(true);
@@ -1980,53 +1984,104 @@ describe('IgxGrid Component Tests #grid', () => {
19801984
expect(grid.getRowByIndex(32) instanceof IgxGridRow).toBe(true);
19811985

19821986
// GroupBy column and get the collapsed grouped row
1983-
expect(grid.getRowByIndex(0) instanceof IgxGridRow).toBe(true);
1987+
expect(firstRow instanceof IgxGridRow).toBe(true);
1988+
1989+
// index
1990+
expect(firstRow.index).toBe(0);
1991+
expect(firstRow.viewIndex).toBe(0);
1992+
expect(firstRow.parent).toBeUndefined();
19841993

19851994
fix.detectChanges();
19861995
grid.groupBy({ fieldName: 'col1', dir: SortingDirection.Asc });
19871996
fix.detectChanges();
19881997

1998+
firstRow = grid.getRowByIndex(0);
1999+
secondRow = grid.getRowByIndex(1);
2000+
thirdRow = grid.getRowByIndex(2);
2001+
19892002
// First row is IgxGroupByRow second row is igxGridRow
1990-
expect(grid.getRowByIndex(0) instanceof IgxGroupByRow).toBe(true);
1991-
expect(grid.getRowByIndex(1) instanceof IgxGridRow).toBe(true);
2003+
expect(firstRow instanceof IgxGroupByRow).toBe(true);
2004+
expect(secondRow instanceof IgxGridRow).toBe(true);
19922005

19932006
// expand/collapse first group row
1994-
grid.getRowByIndex(0).expanded = true;
2007+
firstRow.expanded = true;
19952008
fix.detectChanges();
1996-
expect(grid.getRowByIndex(0).expanded).toBe(true);
19972009

1998-
grid.getRowByIndex(0).expanded = false;
2010+
firstRow = grid.getRowByIndex(0);
2011+
secondRow = grid.getRowByIndex(1);
2012+
thirdRow = grid.getRowByIndex(2);
2013+
2014+
expect(firstRow.expanded).toBe(true);
2015+
firstRow = grid.getRowByIndex(0);
2016+
secondRow = grid.getRowByIndex(1);
2017+
thirdRow = grid.getRowByIndex(2);
2018+
2019+
// index
2020+
expect(secondRow.index).toBe(1);
2021+
2022+
// select group row
2023+
expect(firstRow.selected).toBeFalse();
2024+
expect(secondRow.selected).toBeFalse();
2025+
firstRow.children.forEach(row => {
2026+
expect(row.selected).toBeFalse();
2027+
});
2028+
firstRow.selected = !firstRow.selected;
2029+
2030+
expect(firstRow.selected).toBeTrue();
2031+
expect(secondRow.selected).toBeTrue();
2032+
firstRow.children.forEach(row => {
2033+
expect(row.selected).toBeTrue();
2034+
});
2035+
2036+
firstRow.selected = !firstRow.selected;
2037+
2038+
expect(firstRow.selected).toBeFalse();
2039+
expect(secondRow.selected).toBeFalse();
2040+
firstRow.children.forEach(row => {
2041+
expect(row.selected).toBeFalse();
2042+
});
2043+
2044+
(firstRow as IgxGroupByRow).toggle();
19992045
fix.detectChanges();
2000-
expect(grid.getRowByIndex(0).expanded).toBe(false);
2046+
expect(firstRow.expanded).toBe(false);
2047+
2048+
firstRow = grid.getRowByIndex(0);
2049+
secondRow = grid.getRowByIndex(1);
2050+
thirdRow = grid.getRowByIndex(2);
20012051

20022052
// First row is still IgxGroupByRow and now the second row is as well IgxGroupByRow
2003-
expect(grid.getRowByIndex(0) instanceof IgxGroupByRow).toBe(true);
2004-
expect(grid.getRowByIndex(1) instanceof IgxGroupByRow).toBe(true);
2053+
expect(firstRow instanceof IgxGroupByRow).toBe(true);
2054+
expect(firstRow.key).toBeUndefined();
2055+
expect(secondRow instanceof IgxGroupByRow).toBe(true);
20052056

20062057
// Check hasChildren and other API members for igxGrid
2007-
expect(grid.getRowByIndex(2).hasChildren).toBe(false);
2008-
expect(grid.getRowByIndex(2).index).toEqual(2);
2009-
expect(grid.getRowByIndex(1).isSummaryRow).toBeUndefined();
2058+
expect(thirdRow.hasChildren).toBe(false);
2059+
expect(thirdRow.children).toBeUndefined();
2060+
expect(thirdRow.parent instanceof IgxGroupByRow).toBe(true);
2061+
expect(thirdRow.parent.parent).toBeUndefined();
2062+
expect(thirdRow.index).toEqual(2);
2063+
expect(secondRow.isSummaryRow).toBeUndefined();
20102064

20112065
// GroupByRow check
2012-
expect(grid.getRowByIndex(2).isGroupByRow).toBeUndefined();
2013-
expect(grid.getRowByIndex(1).isGroupByRow).toBe(true);
2014-
expect(grid.getRowByIndex(2).groupRow).toBeUndefined();
2015-
expect(grid.getRowByIndex(1).groupRow).toBeTruthy();
2066+
expect(thirdRow.isGroupByRow).toBeUndefined();
2067+
expect(secondRow.isGroupByRow).toBe(true);
2068+
expect(thirdRow.groupRow).toBeUndefined();
2069+
expect(secondRow.groupRow).toBeTruthy();
2070+
20162071

20172072
// key and rowData check - first with group row (index 1) and then with IgxGridRow (index 2)
2018-
expect(grid.getRowByIndex(1).key).toBeUndefined();
2019-
expect(grid.getRowByIndex(1).data).toBeUndefined();
2020-
expect(grid.getRowByIndex(1).pinned).toBeUndefined();
2021-
expect(grid.getRowByIndex(1).selected).toBeUndefined();
2022-
expect(grid.getRowByIndex(2).key).toBeTruthy();
2023-
expect(grid.getRowByIndex(2).data).toBeTruthy();
2024-
expect(grid.getRowByIndex(2).pinned).toBe(false);
2025-
expect(grid.getRowByIndex(2).selected).toBe(false);
2073+
expect(secondRow.key).toBeUndefined();
2074+
expect(secondRow.data).toBeUndefined();
2075+
expect(secondRow.pinned).toBeUndefined();
2076+
expect(secondRow.selected).toBeFalse();
2077+
expect(thirdRow.key).toBeTruthy();
2078+
expect(thirdRow.data).toBeTruthy();
2079+
expect(thirdRow.pinned).toBe(false);
2080+
expect(thirdRow.selected).toBe(false);
20262081

20272082
// Toggle selection
2028-
grid.getRowByIndex(2).selected = true;
2029-
expect(grid.getRowByIndex(2).selected).toBe(true);
2083+
thirdRow.selected = true;
2084+
expect(thirdRow.selected).toBe(true);
20302085
});
20312086

20322087
it('Verify that getRowByIndex returns correct data when paging is enabled', fakeAsync(() => {
@@ -2047,8 +2102,11 @@ describe('IgxGrid Component Tests #grid', () => {
20472102
fix.detectChanges();
20482103
tick();
20492104

2105+
const firstRow = grid.getRowByIndex(0);
20502106
// Return the first row after page change
2051-
expect(grid.getRowByIndex(0) instanceof IgxGridRow).toBe(true);
2107+
expect(firstRow instanceof IgxGridRow).toBe(true);
2108+
expect(firstRow.index).toBe(0);
2109+
expect(firstRow.viewIndex).toBe(5);
20522110
}));
20532111
});
20542112

projects/igniteui-angular/src/lib/grids/hierarchical-grid/hierarchical-grid.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ describe('IgxHierarchicalGrid Template Changing Scenarios #hGrid', () => {
11691169
const firstRow = child1Grid.componentInstance.getRowByIndex(0);
11701170
firstRow.expanded = true;
11711171
expect(firstRow.hasChildren).toBe(true);
1172-
expect(firstRow.children).toBeNull();
1172+
expect(firstRow.children).toBeUndefined();
11731173
expect(firstRow.viewIndex).toEqual(0);
11741174
expect(firstRow.key).toBeDefined();
11751175
expect(firstRow.data.ID).toEqual('00');

projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid-integration.spec.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { IgxToggleModule } from '../../directives/toggle/toggle.directive';
2020
import { IgxNumberFilteringOperand, IgxStringFilteringOperand } from '../../data-operations/filtering-condition';
2121
import { IgxHierarchicalTransactionService } from '../../services/transaction/igx-hierarchical-transaction';
2222
import { IgxGridTransaction } from '../grid-base.directive';
23-
import { IgxGridCellComponent } from '../grid/public_api';
23+
import { IgxGridCellComponent, IgxTreeGridRow } from '../grid/public_api';
2424
import { IgxPaginatorComponent } from '../../paginator/paginator.component';
2525
import { HierarchicalTransaction, TransactionType } from '../../services/public_api';
2626
import { DropPosition } from '../moving/moving.service';
@@ -1706,10 +1706,12 @@ describe('IgxTreeGrid - Integration #tGrid', () => {
17061706
treeGrid.filter('ID', 957, IgxStringFilteringOperand.instance().condition('contains'), false);
17071707
fix.detectChanges();
17081708

1709+
const firstRow = treeGrid.getRowByIndex(0);
1710+
17091711
// Check getRowByIndex expanded, children and parent members
1710-
expect(treeGrid.getRowByIndex(0).expanded).toBe(true);
1712+
expect(firstRow.expanded).toBe(true);
17111713
// children.length equals the filtered our chidlren!
1712-
expect(treeGrid.getRowByIndex(0).children.length).toEqual(1);
1714+
expect(firstRow.children.length).toEqual(1);
17131715
expect(treeGrid.getRowByIndex(1).parent.rowID).toEqual(147);
17141716

17151717
const firstColumnField = treeGrid.columns[0].field;
@@ -1723,15 +1725,25 @@ describe('IgxTreeGrid - Integration #tGrid', () => {
17231725
treeGrid.filter('ID', 957, IgxStringFilteringOperand.instance().condition('contains'), false);
17241726
fix.detectChanges();
17251727

1728+
const firstRow = treeGrid.getRowByIndex(0);
1729+
17261730
// Check getRowByIndex expanded, children and parent members
1727-
expect(treeGrid.getRowByIndex(0).expanded).toBe(true);
1728-
expect(treeGrid.getRowByIndex(0).hasChildren).toBe(true);
1731+
expect(firstRow.expanded).toBe(true);
1732+
expect(firstRow.hasChildren).toBe(true);
1733+
expect(firstRow.children[0].hasChildren).toBeFalse();
17291734
// children.length equals the filtered our chidlren!
1730-
expect(treeGrid.getRowByIndex(0).children.length).toEqual(1);
1735+
expect(firstRow.children.length).toEqual(1);
1736+
expect(firstRow.children[0] instanceof IgxTreeGridRow).toBeTrue();
1737+
expect(firstRow.children[0].parent instanceof IgxTreeGridRow).toBeTrue();
1738+
expect(firstRow.children[0].parent.key).toBe(firstRow.key);
17311739
expect(treeGrid.getRowByIndex(1).parent.rowID).toEqual(147);
17321740

1733-
treeGrid.getRowByIndex(0).expanded = false;
1734-
expect(treeGrid.getRowByIndex(0).expanded).toBe(false);
1741+
firstRow.expanded = false;
1742+
expect(firstRow.expanded).toBe(false);
1743+
1744+
expect(firstRow.pinned).toBeFalse();
1745+
firstRow.pinned = true;
1746+
expect(firstRow.pinned).toBeTrue();
17351747
});
17361748
});
17371749
});

0 commit comments

Comments
 (0)