diff --git a/src/app/public/modules/grid/fixtures/grid-fixtures.module.ts b/src/app/public/modules/grid/fixtures/grid-fixtures.module.ts index 77fa10a..3c66368 100644 --- a/src/app/public/modules/grid/fixtures/grid-fixtures.module.ts +++ b/src/app/public/modules/grid/fixtures/grid-fixtures.module.ts @@ -1,6 +1,8 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; +import { SkyWindowRefService } from '@skyux/core'; + import { SkyGridModule } from '../'; import { GridTestComponent } from './grid.component.fixture'; @@ -19,6 +21,9 @@ import { GridAsyncTestComponent } from './grid-async.component.fixture'; CommonModule, SkyGridModule ], + providers: [ + SkyWindowRefService + ], exports: [ GridTestComponent, GridEmptyTestComponent, diff --git a/src/app/public/modules/grid/fixtures/grid.component.fixture.html b/src/app/public/modules/grid/fixtures/grid.component.fixture.html index 3e71868..d836e92 100644 --- a/src/app/public/modules/grid/fixtures/grid.component.fixture.html +++ b/src/app/public/modules/grid/fixtures/grid.component.fixture.html @@ -61,3 +61,18 @@ {{value}} + + + diff --git a/src/app/public/modules/grid/fixtures/grid.component.fixture.ts b/src/app/public/modules/grid/fixtures/grid.component.fixture.ts index 856f972..2981065 100644 --- a/src/app/public/modules/grid/fixtures/grid.component.fixture.ts +++ b/src/app/public/modules/grid/fixtures/grid.component.fixture.ts @@ -108,4 +108,12 @@ export class GridTestComponent { public onResize(columnWidths: Array) { this.columnWidthsChange = columnWidths; } + + public hideColumn() { + this.selectedColumnIds = ['column1', 'column3', 'column4', 'column5']; + } + + public showColumn() { + this.selectedColumnIds = ['column1', 'column2', 'column3', 'column4', 'column5']; + } } diff --git a/src/app/public/modules/grid/grid.component.spec.ts b/src/app/public/modules/grid/grid.component.spec.ts index 3dde08e..97a8ee4 100644 --- a/src/app/public/modules/grid/grid.component.spec.ts +++ b/src/app/public/modules/grid/grid.component.spec.ts @@ -141,7 +141,7 @@ function isWithin(actual: number, base: number, distance: number) { } function verifyWidthsMatch(actual: number, expected: number) { - expect(isWithin(actual, expected, 2)).toEqual(true); + expect(isWithin(actual, expected, 5)).toEqual(true); } function verifyAllWidthsMatch(actualWidths: number[], expectedWidths: number[]) { @@ -150,6 +150,16 @@ function verifyAllWidthsMatch(actualWidths: number[], expectedWidths: number[]) expect(isWithin(actualWidths[i], expectedWidths[i], 1)).toEqual(true); } } + +function showColumn2(fixture: ComponentFixture) { + let button = fixture.debugElement.query(By.css('#show-column-button')); + button.nativeElement.click(); +} + +function hideColumn2(fixture: ComponentFixture) { + let button = fixture.debugElement.query(By.css('#hide-column-button')); + button.nativeElement.click(); +} //#endregion describe('Grid Component', () => { @@ -568,7 +578,9 @@ describe('Grid Component', () => { verifyWidthsMatch(initialTableWidth + resizeXDistance, newTableWidth); verifyAllWidthsMatch(expectedColumnWidths, newColumnWidths); component.columnWidthsChange.forEach((cwc, index) => { - verifyWidthsMatch(cwc.width, expectedColumnWidths[index]); + if (cwc.id.indexOf('hidden') === -1) { + verifyWidthsMatch(cwc.width, expectedColumnWidths[index]); + } }); })); @@ -618,7 +630,9 @@ describe('Grid Component', () => { verifyAllWidthsMatch(getColumnWidths(fixture), expectedColumnWidths); verifyWidthsMatch(Number(inputRange.nativeElement.value), initialColumnWidths[columnIndex] + deltaX); component.columnWidthsChange.forEach((cwc, index) => { - verifyWidthsMatch(cwc.width, expectedColumnWidths[index]); + if (cwc.id.indexOf('hidden') === -1) { + verifyWidthsMatch(cwc.width, expectedColumnWidths[index]); + } }); // Decrease first column. @@ -634,7 +648,9 @@ describe('Grid Component', () => { verifyAllWidthsMatch(getColumnWidths(fixture), expectedColumnWidths); verifyWidthsMatch(Number(inputRange.nativeElement.value), initialColumnWidths[columnIndex] + deltaX); component.columnWidthsChange.forEach((cwc, index) => { - verifyWidthsMatch(cwc.width, expectedColumnWidths[index]); + if (cwc.id.indexOf('hidden') === -1) { + verifyWidthsMatch(cwc.width, expectedColumnWidths[index]); + } }); // Run accessibility test. @@ -655,6 +671,52 @@ describe('Grid Component', () => { let expectedColumnInputs = getColumnResizeInputMaxValues(fixture); expect(initialMaxValues).toEqual(expectedColumnInputs); })); + + it('should reset table width when columns are hidden/shown', fakeAsync(() => { + // Get initial baseline for comparison. + let initialTableWidth = getTableWidth(fixture); + + // Resize first column. + let resizeXDistance = 50; + resizeColumn(fixture, resizeXDistance, 0); + + // Hide column 2. + hideColumn2(fixture); + fixture.detectChanges(); + tick(); + + // Assert table width should be 50 additional pixels for resize of col2, + // and less 150 for the hidden column. + let newTableWidth = getTableWidth(fixture); + let expectedTableWidth = initialTableWidth + 50 - 150; + verifyWidthsMatch(expectedTableWidth, newTableWidth); + + // Show column 2. + showColumn2(fixture); + fixture.detectChanges(); + tick(); + + // Now that we've put col2 back, assert table width + // should be 50 additional pixels for resize of col2 + newTableWidth = getTableWidth(fixture); + expectedTableWidth = initialTableWidth + 50; + verifyWidthsMatch(expectedTableWidth, newTableWidth); + })); + + it('should properly emit column widths even when columns are hidden', fakeAsync(() => { + // Hide column 2. + hideColumn2(fixture); + fixture.detectChanges(); + tick(); + + // Resize first column. + let resizeXDistance = 50; + resizeColumn(fixture, resizeXDistance, 0); + + // Expect hidden column to be in emitted array. + let column2 = component.columnWidthsChange.find(cwc => cwc.id === 'column2'); + expect(column2).not.toBeNull(); + })); }); }); diff --git a/src/app/public/modules/grid/grid.component.ts b/src/app/public/modules/grid/grid.component.ts index ddc5d18..93b94d7 100644 --- a/src/app/public/modules/grid/grid.component.ts +++ b/src/app/public/modules/grid/grid.component.ts @@ -47,6 +47,7 @@ import { SkyGridColumnHeadingModelChange, SkyGridColumnWidthModelChange } from './types'; +import { SkyWindowRefService } from '@skyux/core'; let nextId = 0; @@ -132,7 +133,8 @@ export class SkyGridComponent implements AfterContentInit, OnChanges, OnDestroy constructor( private dragulaService: DragulaService, private ref: ChangeDetectorRef, - private gridAdapter: SkyGridAdapterService + private gridAdapter: SkyGridAdapterService, + private skyWindow: SkyWindowRefService ) { this.displayedColumns = new Array(); this.items = new Array(); @@ -185,6 +187,7 @@ export class SkyGridComponent implements AfterContentInit, OnChanges, OnDestroy this.setDisplayedColumns(); if (changes['selectedColumnIds'].previousValue !== changes['selectedColumnIds'].currentValue) { this.selectedColumnIdsChange.emit(this.selectedColumnIds); + this.resetTableWidth(); } } @@ -512,7 +515,7 @@ export class SkyGridComponent implements AfterContentInit, OnChanges, OnDestroy private getColumnWidthModelChange() { let columnWidthModelChange = new Array(); - this.displayedColumns.forEach(column => { + this.columns.forEach(column => { columnWidthModelChange.push({ id: column.id, field: column.field, @@ -539,6 +542,16 @@ export class SkyGridComponent implements AfterContentInit, OnChanges, OnDestroy this.startColumnWidth = column.width; } + private resetTableWidth() { + this.skyWindow.getWindow().setTimeout(() => { + this.gridAdapter.setStyle(this.tableElementRef, 'width', `auto`); + this.ref.detectChanges(); + this.tableWidth = this.tableElementRef.nativeElement.offsetWidth; + this.gridAdapter.setStyle(this.tableElementRef, 'width', `${this.tableWidth}px`); + this.ref.detectChanges(); + }); + } + private getRangeInputByIndex(index: string | number) { return this.columnRangeInputElementRefs.find(input => input.nativeElement.getAttribute('sky-cmp-index') === index.toString()