Skip to content

Commit

Permalink
fix(resizer): use autosize width when total width smaller than viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Nov 16, 2021
1 parent 073f79e commit 555fb0c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/common/src/services/__tests__/resizer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,18 @@ describe('Resizer Service', () => {
expect(mockColDefs[7].width).toBeLessThan(30);
});

it('should call the resize and expect to call "autosizeColumns" when total column widths is smaller than the grid viewport', () => {
Object.defineProperty(divContainer, 'offsetWidth', { writable: true, configurable: true, value: 2500 });

service.init(gridStub, divContainer);
service.resizeColumnsByCellContent(true);

const autosizeSpy = jest.spyOn(gridStub, 'autosizeColumns');
service.resizeColumnsByCellContent(false);

expect(autosizeSpy).toHaveBeenCalled();
});

it('should call the resize and expect first column have a fixed width while other will have a calculated width when resizing by their content', () => {
const setColumnsSpy = jest.spyOn(gridStub, 'setColumns');
const reRenderColumnsSpy = jest.spyOn(gridStub, 'reRenderColumns');
Expand Down
8 changes: 7 additions & 1 deletion packages/common/src/services/resizer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ export class ResizerService {
const columnWidths: { [columnId in string | number]: number; } = {};
let reRender = false;
let readItemCount = 0;
const viewportWidth = this._gridParentContainerElm?.offsetWidth ?? 0;

// if our columns total width is smaller than the grid viewport, we can call the column autosize directly without the need to recalculate all column widths
if (!recalculateColumnsTotalWidth && this._totalColumnsWidthByContent > 0 && this._totalColumnsWidthByContent < viewportWidth) {
this._grid.autosizeColumns();
return;
}

if ((!Array.isArray(dataset) || dataset.length === 0) || (this._hasResizedByContentAtLeastOnce && this.gridOptions?.resizeByContentOnlyOnFirstLoad && !recalculateColumnsTotalWidth)) {
return;
Expand Down Expand Up @@ -449,7 +456,6 @@ export class ResizerService {

// get the grid container viewport width and if our viewport calculated total columns is greater than the viewport width
// then we'll call reRenderColumns() when getting wider than viewport or else the default autosizeColumns() when we know we have plenty of space to shrink the columns
const viewportWidth = this._gridParentContainerElm?.offsetWidth ?? 0;
this._totalColumnsWidthByContent > viewportWidth ? this._grid.reRenderColumns(reRender) : this._grid.autosizeColumns();
this.pubSubService.publish('onAfterResizeByContent', { readItemCount, calculateColumnWidths });
}
Expand Down
Binary file not shown.

0 comments on commit 555fb0c

Please sign in to comment.