Skip to content

Commit

Permalink
fix(resizer): use default resize when resizeByContent has no data
Browse files Browse the repository at this point in the history
- if we load a grid that does not have data, we should instead call the autosizeColumns to avoid seeing the grid with very small width because it wasn't resize since it didn't find data content to begin with
  • Loading branch information
ghiscoding committed Feb 4, 2022
1 parent 186e80d commit 8499b61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/common/src/services/__tests__/resizer.service.spec.ts
Expand Up @@ -646,6 +646,25 @@ describe('Resizer Service', () => {
expect(reRenderColumnsSpy).toHaveBeenCalledWith(true);
});

it('should not return without resizing if "resizeByContentOnlyOnFirstLoad" is set to True and we already resized once', () => {
const setColumnsSpy = jest.spyOn(gridStub, 'setColumns');
const reRenderColumnsSpy = jest.spyOn(gridStub, 'reRenderColumns');
const pubSubSpy = jest.spyOn(eventPubSubService, 'publish');

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

expect(setColumnsSpy).toHaveBeenCalled();
expect(reRenderColumnsSpy).toHaveBeenCalledWith(true);
expect(pubSubSpy).toHaveBeenCalledWith('onBeforeResizeByContent', undefined, 0);

// calling a 2nd time should cancel any resize
// so we shouldn't expect the grid.setColumns to be called again
mockGridOptions.resizeByContentOnlyOnFirstLoad = true;
service.resizeColumnsByCellContent(false);
expect(setColumnsSpy).toHaveBeenCalledTimes(1);
});

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 and grid is editable', () => {
const setColumnsSpy = jest.spyOn(gridStub, 'setColumns');
const reRenderColumnsSpy = jest.spyOn(gridStub, 'reRenderColumns');
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/services/resizer.service.ts
Expand Up @@ -398,12 +398,12 @@ export class ResizerService {
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) {
if ((!Array.isArray(dataset) || dataset.length === 0) || (!recalculateColumnsTotalWidth && this._totalColumnsWidthByContent > 0 && this._totalColumnsWidthByContent < viewportWidth)) {
this._grid.autosizeColumns();
return;
}

if ((!Array.isArray(dataset) || dataset.length === 0) || (this._hasResizedByContentAtLeastOnce && this.gridOptions?.resizeByContentOnlyOnFirstLoad && !recalculateColumnsTotalWidth)) {
if ((this._hasResizedByContentAtLeastOnce && this.gridOptions?.resizeByContentOnlyOnFirstLoad && !recalculateColumnsTotalWidth)) {
return;
}

Expand Down
Binary file not shown.

0 comments on commit 8499b61

Please sign in to comment.