Skip to content

Commit 3adc8d1

Browse files
authored
fix(Grid): Add border to rendered height calc. (#17157)
1 parent 91ad647 commit 3adc8d1

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

projects/igniteui-angular/grids/grid/src/grid-base.directive.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7241,20 +7241,24 @@ export abstract class IgxGridBaseDirective implements GridType,
72417241
if (!this._height) {
72427242
return null;
72437243
}
7244+
const styles = this.document.defaultView.getComputedStyle(this.nativeElement);
72447245
const actualTheadRow = this.getTheadRowHeight();
72457246
const footerHeight = this.getFooterHeight();
72467247
const toolbarHeight = this.getToolbarHeight();
72477248
const pagingHeight = this.getPagingFooterHeight();
72487249
const groupAreaHeight = this.getGroupAreaHeight();
72497250
const scrHeight = this.getComputedHeight(this.scr.nativeElement);
7251+
const borderTop = parseFloat(styles.getPropertyValue('border-top-width')) || 0;
7252+
const borderBottom = parseFloat(styles.getPropertyValue('border-bottom-width')) || 0;
7253+
72507254
const renderedHeight = toolbarHeight + actualTheadRow +
72517255
footerHeight + pagingHeight + groupAreaHeight +
7252-
scrHeight;
7256+
scrHeight + borderTop + borderBottom;
72537257

72547258
let gridHeight = 0;
72557259

72567260
if (this.isPercentHeight) {
7257-
const computed = this.document.defaultView.getComputedStyle(this.nativeElement).getPropertyValue('height');
7261+
const computed = styles.getPropertyValue('height');
72587262
const autoSize = this._shouldAutoSize(renderedHeight);
72597263
if (autoSize || computed.indexOf('%') !== -1) {
72607264
const bodyHeight = this.getDataBasedBodyHeight();

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,30 @@ describe('IgxGrid Component Tests #grid', () => {
13071307
expect(parseInt(window.getComputedStyle(domGrid).height, 10)).toBe(300);
13081308
}));
13091309

1310+
it('should account for CSS border widths in body height calculation when height is percent #16640', fakeAsync(() => {
1311+
const fix = TestBed.createComponent(IgxGridWrappedInContComponent);
1312+
fix.componentInstance.outerHeight = 600;
1313+
fix.componentInstance.data = fix.componentInstance.fullData;
1314+
tick();
1315+
fix.detectChanges();
1316+
1317+
const grid = fix.componentInstance.grid;
1318+
const calcHeightNoBorder = grid.calcHeight;
1319+
expect(calcHeightNoBorder).not.toBeNull();
1320+
1321+
// Apply a 2px border (top and bottom) to the grid's native element
1322+
grid.nativeElement.style.borderTop = '2px solid black';
1323+
grid.nativeElement.style.borderBottom = '2px solid black';
1324+
1325+
// Trigger height recalculation
1326+
grid.reflow();
1327+
fix.detectChanges();
1328+
1329+
// The fix ensures border widths are included in the rendered height calculation,
1330+
// reducing the available body height accordingly and preventing continuous reflow growth
1331+
expect(grid.calcHeight).toBe(calcHeightNoBorder - 4);
1332+
}));
1333+
13101334
it('should keep auto-sizing if initial data is empty then set to a new array', fakeAsync(() => {
13111335
const fix = TestBed.createComponent(IgxGridWrappedInContComponent);
13121336
tick();

src/app/grid-auto-size/grid-auto-size.sample.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
margin-bottom: 16px;
1212
max-width: 900px;
1313
}
14+
15+
igx-grid {
16+
border: 1px solid lightgray;
17+
}

0 commit comments

Comments
 (0)