Skip to content

Commit f99e4f5

Browse files
authored
feat(igxGrid): Apply min/max width constraints on user-set and auto-w… (#15449)
1 parent 7110f09 commit f99e4f5

File tree

5 files changed

+328
-15
lines changed

5 files changed

+328
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
All notable changes for each version of this project will be documented in this file.
44

55
## 19.2.0
6+
67
### General
78
- `IgxCarousel`
89
- Removed deprecated property `keyboardSupport`.
910
- `IgxSlide`
1011
- **Deprecation** - `tabIndex` has been deprecated and will be removed in a future version.
12+
- `IgxGrid`, `IgxHierarchicalGrid`, `IgxTreeGrid`
13+
- A column's `minWidth` and `maxWidth` constrain the user-specified `width` so that it cannot go outside their bounds.
1114

1215
## 19.1.1
1316
### New Features

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

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,14 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
524524
*/
525525
@WatchColumnChanges()
526526
@Input()
527-
public maxWidth: string;
527+
public set maxWidth(value: string) {
528+
this._maxWidth = value;
528529

530+
this.grid.notifyChanges(true);
531+
}
532+
public get maxWidth(): string {
533+
return this._maxWidth;
534+
}
529535
/**
530536
* Sets/gets the class selector of the column header.
531537
* ```typescript
@@ -938,6 +944,15 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
938944
return isPercentageWidth ? parseFloat(this.minWidth) / 100 * gridAvailableSize : parseFloat(this.minWidth);
939945
}
940946

947+
/**
948+
* @hidden
949+
*/
950+
public get userSetMinWidthPx() {
951+
const gridAvailableSize = this.grid.calcWidth;
952+
const isPercentageWidth = this._defaultMinWidth && typeof this._defaultMinWidth === 'string' && this._defaultMinWidth.indexOf('%') !== -1;
953+
return isPercentageWidth ? parseFloat(this._defaultMinWidth) / 100 * gridAvailableSize : parseFloat(this._defaultMinWidth);
954+
}
955+
941956
/**
942957
* @hidden
943958
*/
@@ -969,7 +984,7 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
969984
return;
970985
}
971986
this._defaultMinWidth = value;
972-
987+
this.grid.notifyChanges(true);
973988
}
974989
public get minWidth(): string {
975990
return !this._defaultMinWidth ? this.defaultMinWidth : this._defaultMinWidth;
@@ -1737,6 +1752,11 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
17371752
*/
17381753
public destroy$ = new Subject<any>();
17391754

1755+
/**
1756+
* @hidden
1757+
*/
1758+
public widthConstrained = false;
1759+
17401760
/**
17411761
* @hidden
17421762
*/
@@ -1811,6 +1831,10 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
18111831
* @hidden
18121832
*/
18131833
protected _defaultMinWidth = '';
1834+
/**
1835+
* @hidden
1836+
*/
1837+
protected _maxWidth;
18141838
/**
18151839
* @hidden
18161840
*/
@@ -2096,7 +2120,8 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
20962120
if (size && !!size.width) {
20972121
result.push(size.width + 'px');
20982122
} else {
2099-
result.push(parseFloat(this.grid.getPossibleColumnWidth()) + 'px');
2123+
const currentWidth = parseFloat(this.grid.getPossibleColumnWidth());
2124+
result.push((this.getConstrainedSizePx(currentWidth)) + 'px');
21002125
}
21012126
}
21022127
return result;
@@ -2558,6 +2583,23 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
25582583
return res.join(' ');
25592584
}
25602585

2586+
/**
2587+
* @hidden
2588+
* @internal
2589+
*/
2590+
public getConstrainedSizePx(newSize){
2591+
if (this.maxWidth && newSize > this.maxWidthPx) {
2592+
this.widthConstrained = true;
2593+
return this.maxWidthPx;
2594+
} else if (this.minWidth && newSize < this.userSetMinWidthPx) {
2595+
this.widthConstrained = true;
2596+
return this.userSetMinWidthPx;
2597+
} else {
2598+
this.widthConstrained = false;
2599+
return newSize;
2600+
}
2601+
}
2602+
25612603
/**
25622604
* @hidden
25632605
* @internal
@@ -2567,14 +2609,17 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
25672609
const isPercentageWidth = colWidth && typeof colWidth === 'string' && colWidth.indexOf('%') !== -1;
25682610
const isAutoWidth = colWidth && typeof colWidth === 'string' && colWidth === 'fit-content';
25692611
if (isPercentageWidth && this.grid.isColumnWidthSum) {
2570-
this._calcWidth = this.grid.minColumnWidth;
2612+
this._calcWidth = this.userSetMinWidthPx ? this.userSetMinWidthPx : this.grid.minColumnWidth;
25712613
} else if (isPercentageWidth) {
2572-
this._calcWidth = parseFloat(colWidth) / 100 * this.grid.calcWidth;
2614+
const currentCalcWidth = parseFloat(colWidth) / 100 * this.grid.calcWidth;
2615+
this._calcWidth = this.grid.calcWidth ? this.getConstrainedSizePx(currentCalcWidth) : 0;
25732616
} else if (!colWidth || isAutoWidth && !this.autoSize) {
25742617
// no width
2575-
this._calcWidth = this.defaultWidth || this.grid.getPossibleColumnWidth();
2618+
const currentCalcWidth = this.defaultWidth || this.grid.getPossibleColumnWidth();
2619+
this._calcWidth = this.getConstrainedSizePx(currentCalcWidth);
25762620
} else {
2577-
this._calcWidth = this.width;
2621+
const currentCalcWidth = parseFloat(this.width);
2622+
this._calcWidth =this.getConstrainedSizePx(currentCalcWidth);
25782623
}
25792624
this.calcPixelWidth = parseFloat(this._calcWidth);
25802625
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5424,22 +5424,25 @@ export abstract class IgxGridBaseDirective implements GridType,
54245424

54255425
const columnsWithSetWidths = this.hasColumnLayouts ?
54265426
visibleCols.filter(c => c.widthSetByUser) :
5427-
visibleChildColumns.filter(c => c.widthSetByUser && c.width !== 'fit-content');
5427+
visibleChildColumns.filter(c => (c.widthSetByUser || c.widthConstrained) && c.width !== 'fit-content');
54285428

54295429
const columnsToSize = this.hasColumnLayouts ?
54305430
combinedBlocksSize - columnsWithSetWidths.length :
54315431
visibleChildColumns.length - columnsWithSetWidths.length;
54325432
const sumExistingWidths = columnsWithSetWidths
54335433
.reduce((prev, curr) => {
5434-
const colWidth = curr.width;
5434+
const colInstance = this.hasColumnLayouts ? curr.ref : curr;
5435+
const colWidth = !colInstance.widthConstrained ? curr.width : colInstance.calcPixelWidth;
54355436
let widthValue = parseFloat(colWidth);
54365437
if (isNaN(widthValue)) {
54375438
widthValue = MINIMUM_COLUMN_WIDTH;
54385439
}
54395440
const currWidth = colWidth && typeof colWidth === 'string' && colWidth.indexOf('%') !== -1 ?
54405441
widthValue / 100 * computedWidth :
54415442
widthValue;
5442-
return prev + currWidth;
5443+
// apply constraints, since constraint may change width
5444+
const constrainedWidth = this.hasColumnLayouts ? currWidth : colInstance.getConstrainedSizePx(currWidth);
5445+
return prev + constrainedWidth;
54435446
}, 0);
54445447

54455448
// When all columns are hidden, return 0px width
@@ -6555,7 +6558,7 @@ export abstract class IgxGridBaseDirective implements GridType,
65556558
if (width && typeof width !== 'string') {
65566559
width = String(width);
65576560
}
6558-
const minWidth = width.indexOf('%') === -1 ? column.minWidthPx : column.minWidthPercent;
6561+
const minWidth = width.indexOf('%') === -1 ? column.userSetMinWidthPx : column.minWidthPercent;
65596562
const maxWidth = width.indexOf('%') === -1 ? column.maxWidthPx : column.maxWidthPercent;
65606563
if (column.hidden) {
65616564
return width;
@@ -7343,8 +7346,8 @@ export abstract class IgxGridBaseDirective implements GridType,
73437346
let maxSize = Math.ceil(Math.max(...cellsContentWidths)) + 1;
73447347
if (col.maxWidth && maxSize > col.maxWidthPx) {
73457348
maxSize = col.maxWidthPx;
7346-
} else if (maxSize < col.minWidthPx) {
7347-
maxSize = col.minWidthPx;
7349+
} else if (maxSize < col.userSetMinWidthPx) {
7350+
maxSize = col.userSetMinWidthPx;
73487351
}
73497352
col.autoSize = maxSize;
73507353
col.resetCaches();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,8 @@ describe('IgxGrid - Column properties #grid', () => {
15641564
tick();
15651565

15661566
let widths = grid.columns.map(x => x.width);
1567-
expect(widths).toEqual(['80px', '130px', '121px', '114px', '92px', '80px', '86px', '108px', '82px', '80px']);
1567+
// default min of 80px is disregarded for user-set widths, including auto.
1568+
expect(widths).toEqual(['68px', '130px', '121px', '114px', '92px', '72px', '86px', '108px', '82px', '69px']);
15681569
fix.componentInstance.data = SampleTestData.contactInfoData();
15691570
fix.detectChanges();
15701571
tick();

0 commit comments

Comments
 (0)