diff --git a/src/demo-app/grid-list/grid-list-demo.html b/src/demo-app/grid-list/grid-list-demo.html index 49ab6f34851..10d526cf00c 100644 --- a/src/demo-app/grid-list/grid-list-demo.html +++ b/src/demo-app/grid-list/grid-list-demo.html @@ -11,6 +11,46 @@ + + Grid with 1 cell at the beginning of a new row + + + + + + + + + + + + + + Grid with col-span + + + + + + + + + + + + + + + + Fixed-height grid list diff --git a/src/lib/grid-list/grid-list.spec.ts b/src/lib/grid-list/grid-list.spec.ts index 05dbb582c63..88a0a80024c 100644 --- a/src/lib/grid-list/grid-list.spec.ts +++ b/src/lib/grid-list/grid-list.spec.ts @@ -246,6 +246,65 @@ describe('MatGridList', () => { expect(getStyle(tiles[3], 'top')).toBe('101px'); }); + it('should lay out tiles correctly', () => { + const fixture = createComponent(GridListWithLayout); + + fixture.detectChanges(); + const tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile')); + + expect(getStyle(tiles[0], 'width')).toBe('40px'); + expect(getStyle(tiles[0], 'height')).toBe('40px'); + expect(getComputedLeft(tiles[0])).toBe(0); + expect(getStyle(tiles[0], 'top')).toBe('0px'); + + expect(getStyle(tiles[1], 'width')).toBe('20px'); + expect(getStyle(tiles[1], 'height')).toBe('20px'); + expect(getComputedLeft(tiles[1])).toBe(40); + expect(getStyle(tiles[1], 'top')).toBe('0px'); + + expect(getStyle(tiles[2], 'width')).toBe('40px'); + expect(getStyle(tiles[2], 'height')).toBe('40px'); + expect(getComputedLeft(tiles[2])).toBe(60); + expect(getStyle(tiles[2], 'top')).toBe('0px'); + + expect(getStyle(tiles[3], 'width')).toBe('40px'); + expect(getStyle(tiles[3], 'height')).toBe('40px'); + expect(getComputedLeft(tiles[3])).toBe(0); + expect(getStyle(tiles[3], 'top')).toBe('40px'); + + expect(getStyle(tiles[4], 'width')).toBe('40px'); + expect(getStyle(tiles[4], 'height')).toBe('40px'); + expect(getComputedLeft(tiles[4])).toBe(40); + expect(getStyle(tiles[4], 'top')).toBe('40px'); + }); + + it('should lay out tiles correctly when single cell to be placed at the beginning', + () => { + const fixture = createComponent(GridListWithSingleCellAtBeginning); + + fixture.detectChanges(); + const tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile')); + + expect(getStyle(tiles[0], 'width')).toBe('40px'); + expect(getStyle(tiles[0], 'height')).toBe('40px'); + expect(getComputedLeft(tiles[0])).toBe(0); + expect(getStyle(tiles[0], 'top')).toBe('0px'); + + expect(getStyle(tiles[1], 'width')).toBe('20px'); + expect(getStyle(tiles[1], 'height')).toBe('40px'); + expect(getComputedLeft(tiles[1])).toBe(40); + expect(getStyle(tiles[1], 'top')).toBe('0px'); + + expect(getStyle(tiles[2], 'width')).toBe('40px'); + expect(getStyle(tiles[2], 'height')).toBe('40px'); + expect(getComputedLeft(tiles[2])).toBe(60); + expect(getStyle(tiles[2], 'top')).toBe('0px'); + + expect(getStyle(tiles[3], 'height')).toBe('20px'); + expect(getComputedLeft(tiles[3])).toBe(0); + expect(getStyle(tiles[3], 'top')).toBe('40px'); + }); + it('should add not add any classes to footers without lines', () => { const fixture = createComponent(GridListWithFootersWithoutLines); fixture.detectChanges(); @@ -486,6 +545,29 @@ class GridListWithComplexLayout { tiles: any[]; } +@Component({template: ` +
+ + + + + + + +
`}) +class GridListWithLayout {} + +@Component({template: ` +
+ + + + + + +
`}) +class GridListWithSingleCellAtBeginning {} + @Component({template: ` diff --git a/src/lib/grid-list/tile-coordinator.ts b/src/lib/grid-list/tile-coordinator.ts index 7ea74cd4cd8..c95bdca4cfe 100644 --- a/src/lib/grid-list/tile-coordinator.ts +++ b/src/lib/grid-list/tile-coordinator.ts @@ -91,6 +91,8 @@ export class TileCoordinator { // If we've reached the end of the row, go to the next row. if (this.columnIndex + tileCols > this.tracker.length) { this._nextRow(); + gapStartIndex = this.tracker.indexOf(0, this.columnIndex); + gapEndIndex = this._findGapEndIndex(gapStartIndex); continue; } @@ -99,6 +101,8 @@ export class TileCoordinator { // If there are no more empty spaces in this row at all, move on to the next row. if (gapStartIndex == -1) { this._nextRow(); + gapStartIndex = this.tracker.indexOf(0, this.columnIndex); + gapEndIndex = this._findGapEndIndex(gapStartIndex); continue; } @@ -108,8 +112,9 @@ export class TileCoordinator { // gap on the next iteration. this.columnIndex = gapStartIndex + 1; - // Continue iterating until we find a gap wide enough for this tile. - } while (gapEndIndex - gapStartIndex < tileCols); + // Continue iterating until we find a gap wide enough for this tile. Since gapEndIndex is + // exclusive, gapEndIndex is 0 means we didn't find a gap and should continue. + } while ((gapEndIndex - gapStartIndex < tileCols) || (gapEndIndex == 0)); // If we still didn't manage to find a gap, ensure that the index is // at least zero so the tile doesn't get pulled out of the grid.