Skip to content

Commit 5535325

Browse files
crisbetojelbourn
authored andcommitted
fix(grid-list): tile being pulled outside the grid if no gap can be found (#9128)
Fixes and issue in the grid list where a tile, for which we couldn't find a gap in which to put it, is being pulled outside the grid due to the index defaulting to -1. Fixes #4515.
1 parent 4bd96ce commit 5535325

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/lib/grid-list/grid-list.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('MatGridList', () => {
2828
GridListWithComplexLayout,
2929
GridListWithFootersWithoutLines,
3030
GridListWithFooterContainingTwoLines,
31+
GridListWithoutMatchingGap,
3132
],
3233
});
3334

@@ -287,6 +288,15 @@ describe('MatGridList', () => {
287288
expect(getStyle(tile, 'height')).toBe('400px');
288289
});
289290

291+
it('should ensure that all tiles are inside the grid when there are no matching gaps', () => {
292+
const fixture = TestBed.createComponent(GridListWithoutMatchingGap);
293+
const tiles = fixture.debugElement.queryAll(By.css('mat-grid-tile'));
294+
295+
fixture.detectChanges();
296+
expect(tiles.every(tile => getComputedLeft(tile) >= 0))
297+
.toBe(true, 'Expected none of the tiles to have a negative `left`');
298+
});
299+
290300
});
291301

292302

@@ -298,7 +308,7 @@ function getStyle(el: DebugElement, prop: string): string {
298308
function getComputedLeft(element: DebugElement): number {
299309
// While the other properties in this test use `getComputedStyle`, we use `getBoundingClientRect`
300310
// for left because iOS Safari doesn't support using `getComputedStyle` to get the calculated
301-
// `left` balue when using CSS `calc`. We subtract the `left` of the document body because
311+
// `left` value when using CSS `calc`. We subtract the `left` of the document body because
302312
// browsers, by default, add a margin to the body (typically 8px).
303313
let elementRect = element.nativeElement.getBoundingClientRect();
304314
let bodyRect = document.body.getBoundingClientRect();
@@ -458,3 +468,13 @@ class GridListWithFootersWithoutLines { }
458468
</mat-grid-tile>
459469
</mat-grid-list>`})
460470
class GridListWithFooterContainingTwoLines { }
471+
472+
@Component({template: `
473+
<mat-grid-list cols="5">
474+
<mat-grid-tile [rowspan]="1" [colspan]="3">1</mat-grid-tile>
475+
<mat-grid-tile [rowspan]="2" [colspan]="2">2</mat-grid-tile>
476+
<mat-grid-tile [rowspan]="1" [colspan]="2">3</mat-grid-tile>
477+
<mat-grid-tile [rowspan]="2" [colspan]="2">4</mat-grid-tile>
478+
</mat-grid-list>
479+
`})
480+
class GridListWithoutMatchingGap { }

src/lib/grid-list/tile-coordinator.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ export class TileCoordinator {
108108

109109
// Continue iterating until we find a gap wide enough for this tile.
110110
} while (gapEndIndex - gapStartIndex < tileCols);
111-
return gapStartIndex;
111+
112+
// If we still didn't manage to find a gap, ensure that the index is
113+
// at least zero so the tile doesn't get pulled out of the grid.
114+
return Math.max(gapStartIndex, 0);
112115
}
113116

114117
/** Move "down" to the next row. */

0 commit comments

Comments
 (0)