Skip to content

Commit 95265fa

Browse files
committed
fix(resizing): Unify autosize logic between API and user interaction. Remove handling for circular dependency sizing and introduce autosizeHeader. #9158
1 parent 7208467 commit 95265fa

File tree

5 files changed

+55
-29
lines changed

5 files changed

+55
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ 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+
- A new `IgxColumnComponent` input property is exposed called `autosizeHeader`, which if false, allows the autosizing to ignore the header template and autosize only based on content cells.
4042
### Themes:
4143
- Breaking Changes:
4244
- `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

Lines changed: 37 additions & 14 deletions
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,9 @@ 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, because then we try to get new column size, based on header content, which is sized based on column size...
2166+
let headerWidth = getNodeSizeViaRange(range, this.headerCell.elementRef.nativeElement.children[0]);
21442167

21452168
if (this.sortable || this.filterable) {
21462169
headerWidth += this.headerCell.elementRef.nativeElement.children[1].getBoundingClientRect().width;
@@ -2188,7 +2211,7 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy {
21882211
largest.set(Math.max(...cellsContentWidths), cellPadding);
21892212
}
21902213

2191-
if (this.headerCell) {
2214+
if (this.headerCell && this.autosizeHeader) {
21922215
const headerCellWidths = this.getHeaderCellWidths();
21932216
largest.set(headerCellWidths.width, headerCellWidths.padding);
21942217
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,20 @@ describe('IgxGrid - Deferred Column Resizing #grid', () => {
678678

679679
expect(column.width).toEqual('89px');
680680
});
681+
682+
it('should ignore header template during autosize if autosizeHeader is false.', () => {
683+
const column = grid.getColumnByName('ID');
684+
column.minWidth = "10px";
685+
column.autosizeHeader = false;
686+
fixture.detectChanges();
687+
688+
expect(column.width).toEqual('150px');
689+
690+
column.autosize();
691+
fixture.detectChanges();
692+
693+
expect(column.width).toEqual('55px');
694+
});
681695
});
682696

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

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

Lines changed: 1 addition & 10 deletions
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

Lines changed: 1 addition & 5 deletions
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)