Skip to content

Commit 9b99250

Browse files
authored
Merge pull request #9221 from IgniteUI/skrastev/fix-9158
Unify autosize logic between API and user interaction. Remove handling for circular dependency sizing.
2 parents e2eafda + f44e44e commit 9b99250

File tree

5 files changed

+60
-30
lines changed

5 files changed

+60
-30
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ All notable changes for each version of this project will be documented in this
3737
[summaryFormatter]="dateSummaryFormat">
3838
</igx-column>
3939
```
40+
- **Behavioral Change** - `Column Autosize` feature now does not handle templated headers where the first level children are sized based on parent like default `div` and etc. Autosizing for such headers will not result in change.
41+
- **Behavioral Change** - Calling `autosize` through the `IgxColumnComponent` API now takes into consideration the `minWidth` and `maxWidth` of the column.
42+
- A new `IgxColumnComponent` input property is exposed called `autosizeHeader`, which if false, allows the autosizing to ignore the header cell and autosize only based on content cells.
4043
### Themes:
4144
- Breaking Changes:
4245
- `IgxButton` theme has been simplified. The number of theme params (`igx-button-theme`) has been reduced significantly and no longer includes prefixed parameters (`flat-*`, `raised-*`, etc.). See the migration guide to update existing button themes. Updates performed with `ng update` will migrate existing button themes but some additional tweaking may be required to account for the abscense of prefixed params.

projects/igniteui-angular/src/lib/grids/columns/column.component.ts

+38-14
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
246246
@WatchColumnChanges()
247247
@Input()
248248
public resizable = false;
249+
250+
/**
251+
* Sets/gets whether the column header is included in autosize logic.
252+
* Useful when template for a column header is sized based on parent, for example a default `div`.
253+
* Default value is `false`.
254+
* ```typescript
255+
* let isResizable = this.column.resizable;
256+
* ```
257+
* ```html
258+
* <igx-column [resizable] = "true"></igx-column>
259+
* ```
260+
*
261+
* @memberof IgxColumnComponent
262+
*/
263+
@WatchColumnChanges()
264+
@Input()
265+
public autosizeHeader = true;
266+
249267
/**
250268
* Gets a value indicating whether the summary for the column is enabled.
251269
* ```typescript
@@ -2088,12 +2106,11 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
20882106
* ```
20892107
*
20902108
* @memberof IgxColumnComponent
2091-
* @param byHeader Set if column should be autized based only on the header content
2109+
* @param byHeaderOnly Set if column should be autosized based only on the header content.
20922110
*/
2093-
public autosize(byHeader = false) {
2111+
public autosize(byHeaderOnly = false) {
20942112
if (!this.columnGroup) {
2095-
const size = this.getAutoSize(byHeader);
2096-
this.width = size;
2113+
this.width = this.getAutoSize(byHeaderOnly);
20972114
this.grid.reflow();
20982115
}
20992116
}
@@ -2104,15 +2121,25 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
21042121
public getAutoSize(byHeader = false) {
21052122
const size = !byHeader ? this.getLargestCellWidth() :
21062123
(Object.values(this.getHeaderCellWidths()).reduce((a, b) => a + b) + 'px');
2107-
const gridAvailableSize = this.grid.calcWidth;
2108-
let newWidth;
21092124
const isPercentageWidth = this.width && typeof this.width === 'string' && this.width.indexOf('%') !== -1;
2125+
2126+
let newWidth;
21102127
if (isPercentageWidth) {
2128+
const gridAvailableSize = this.grid.calcWidth;
21112129
const percentageSize = parseFloat(size) / gridAvailableSize * 100;
21122130
newWidth = percentageSize + '%';
21132131
} else {
21142132
newWidth = size;
21152133
}
2134+
2135+
const maxWidth = isPercentageWidth ? this.maxWidthPercent : this.maxWidthPx;
2136+
const minWidth = isPercentageWidth ? this.minWidthPercent : this.minWidthPx;
2137+
if (this.maxWidth && (parseFloat(newWidth) > maxWidth)) {
2138+
newWidth = isPercentageWidth ? maxWidth + '%' : maxWidth + 'px';
2139+
} else if (parseFloat(newWidth) < minWidth) {
2140+
newWidth = isPercentageWidth ? minWidth + '%' : minWidth + 'px';
2141+
}
2142+
21162143
return newWidth;
21172144
}
21182145

@@ -2134,13 +2161,10 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
21342161
*/
21352162
public getHeaderCellWidths() {
21362163
const range = this.grid.document.createRange();
2137-
let headerWidth;
2138-
if (this.headerTemplate && this.headerCell.elementRef.nativeElement.children[0].children.length > 0) {
2139-
headerWidth = Math.max(...Array.from(this.headerCell.elementRef.nativeElement.children[0].children)
2140-
.map((child) => getNodeSizeViaRange(range, child)));
2141-
} else {
2142-
headerWidth = getNodeSizeViaRange(range, this.headerCell.elementRef.nativeElement.children[0]);
2143-
}
2164+
2165+
// We do not cover cases where there are children with width 100% and etc,
2166+
// because then we try to get new column size, based on header content, which is sized based on column size...
2167+
let headerWidth = getNodeSizeViaRange(range, this.headerCell.elementRef.nativeElement.children[0]);
21442168

21452169
if (this.sortable || this.filterable) {
21462170
headerWidth += this.headerCell.elementRef.nativeElement.children[1].getBoundingClientRect().width;
@@ -2188,7 +2212,7 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
21882212
largest.set(Math.max(...cellsContentWidths), cellPadding);
21892213
}
21902214

2191-
if (this.headerCell) {
2215+
if (this.headerCell && this.autosizeHeader) {
21922216
const headerCellWidths = this.getHeaderCellWidths();
21932217
largest.set(headerCellWidths.width, headerCellWidths.padding);
21942218
}

projects/igniteui-angular/src/lib/grids/grid/column-resizing.spec.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ describe('IgxGrid - Deferred Column Resizing #grid', () => {
375375

376376
it('should autosize column programmatically.', fakeAsync(/** height/width setter rAF */() => {
377377
const column = grid.getColumnByName('ID');
378+
column.minWidth = '30px';
378379
expect(column.width).toEqual('100px');
379380

380381
column.autosize();
@@ -417,11 +418,12 @@ describe('IgxGrid - Deferred Column Resizing #grid', () => {
417418
const column = grid.getColumnByName('ID');
418419
column.minWidth = '70px';
419420
expect(column.minWidth).toEqual('70px');
421+
expect(column.width).toEqual('100px');
420422

421423
column.autosize();
422424
fixture.detectChanges();
423425

424-
expect(column.width).toEqual('63px');
426+
expect(column.width).toEqual('70px');
425427
}));
426428
});
427429

@@ -678,6 +680,20 @@ describe('IgxGrid - Deferred Column Resizing #grid', () => {
678680

679681
expect(column.width).toEqual('89px');
680682
});
683+
684+
it('should ignore header template during autosize if autosizeHeader is false.', () => {
685+
const column = grid.getColumnByName('ID');
686+
column.minWidth = '10px';
687+
column.autosizeHeader = false;
688+
fixture.detectChanges();
689+
690+
expect(column.width).toEqual('150px');
691+
692+
column.autosize();
693+
fixture.detectChanges();
694+
695+
expect(column.width).toEqual('55px');
696+
});
681697
});
682698

683699
describe('Multi Column Headers tests: ', () => {

projects/igniteui-angular/src/lib/grids/resizing/resizing.service.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,7 @@ export class IgxColumnResizingService {
8080
*/
8181
public autosizeColumnOnDblClick() {
8282
const currentColWidth = this.column.headerCell.elementRef.nativeElement.getBoundingClientRect().width;
83-
const isPercentageWidth = this.column.width && typeof this.column.width === 'string' && this.column.width.indexOf('%') !== -1;
84-
let size = this.column.getAutoSize();
85-
const maxWidth = isPercentageWidth ? this.column.maxWidthPercent : this.column.maxWidthPx;
86-
const minWidth = isPercentageWidth ? this.column.minWidthPercent : this.column.minWidthPx;
87-
if (this.column.maxWidth && (parseFloat(size) > maxWidth)) {
88-
size = isPercentageWidth ? maxWidth + '%' : maxWidth + 'px';
89-
} else if (parseFloat(size) < minWidth) {
90-
size = isPercentageWidth ? minWidth + '%' : minWidth + 'px';
91-
}
92-
this.column.width = size;
83+
this.column.width = this.column.getAutoSize();
9384

9485
this.zone.run(() => {});
9586

projects/igniteui-angular/src/lib/test-utils/template-strings.spec.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,7 @@ export class ColumnDefinitions {
163163
<igx-column [field]="'ReleaseDate'" [resizable]="true" dataType="date"></igx-column>
164164
<igx-column [field]="'Category'" [width]="'150px'" [resizable]="true" dataType="string">
165165
<ng-template igxCell igxHeader>
166-
<div>
167-
<div style="width: 40px; min-width: 40px; min-height: 40px; background-color: gray;">
168-
JS
169-
</div>
170-
</div>
166+
<igx-avatar initials="JS"></igx-avatar>
171167
</ng-template>
172168
</igx-column>
173169
<igx-column [field]="'Items'" [width]="'60px'" [hasSummary]="true" [resizable]="true" dataType="string"></igx-column>

0 commit comments

Comments
 (0)