Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(igxGrid): Apply min/max width constraints on user-set and auto-w… #15449

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,14 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
*/
@WatchColumnChanges()
@Input()
public maxWidth: string;
public set maxWidth(value: string) {
this._maxWidth = value;

this.grid.notifyChanges(true);
}
public get maxWidth(): string {
return this._maxWidth;
}
/**
* Sets/gets the class selector of the column header.
* ```typescript
Expand Down Expand Up @@ -937,6 +943,15 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
return isPercentageWidth ? parseFloat(this.minWidth) / 100 * gridAvailableSize : parseFloat(this.minWidth);
}

/**
* @hidden
*/
public get userSetMinWidthPx() {
const gridAvailableSize = this.grid.calcWidth;
const isPercentageWidth = this._defaultMinWidth && typeof this._defaultMinWidth === 'string' && this._defaultMinWidth.indexOf('%') !== -1;
return isPercentageWidth ? parseFloat(this._defaultMinWidth) / 100 * gridAvailableSize : parseFloat(this._defaultMinWidth);
}

/**
* @hidden
*/
Expand Down Expand Up @@ -968,7 +983,7 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
return;
}
this._defaultMinWidth = value;

this.grid.notifyChanges(true);
}
public get minWidth(): string {
return !this._defaultMinWidth ? this.defaultMinWidth : this._defaultMinWidth;
Expand Down Expand Up @@ -1732,6 +1747,11 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
*/
public destroy$ = new Subject<any>();

/**
* @hidden
*/
public widthConstrained = false;

/**
* @hidden
*/
Expand Down Expand Up @@ -1806,6 +1826,10 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
* @hidden
*/
protected _defaultMinWidth = '';
/**
* @hidden
*/
protected _maxWidth;
/**
* @hidden
*/
Expand Down Expand Up @@ -2091,7 +2115,8 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
if (size && !!size.width) {
result.push(size.width + 'px');
} else {
result.push(parseFloat(this.grid.getPossibleColumnWidth()) + 'px');
const currentWidth = parseFloat(this.grid.getPossibleColumnWidth());
result.push((this.getConstrainedSizePx(currentWidth)) + 'px');
}
}
return result;
Expand Down Expand Up @@ -2553,6 +2578,23 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
return res.join(' ');
}

/**
* @hidden
* @internal
*/
public getConstrainedSizePx(newSize){
if (this.maxWidth && newSize > this.maxWidthPx) {
this.widthConstrained = true;
return this.maxWidthPx;
} else if (this.minWidth && newSize < this.userSetMinWidthPx) {
this.widthConstrained = true;
return this.userSetMinWidthPx;
} else {
this.widthConstrained = false;
return newSize;
}
}

/**
* @hidden
* @internal
Expand All @@ -2562,14 +2604,17 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
const isPercentageWidth = colWidth && typeof colWidth === 'string' && colWidth.indexOf('%') !== -1;
const isAutoWidth = colWidth && typeof colWidth === 'string' && colWidth === 'fit-content';
if (isPercentageWidth && this.grid.isColumnWidthSum) {
this._calcWidth = this.grid.minColumnWidth;
this._calcWidth = this.userSetMinWidthPx ? this.userSetMinWidthPx : this.grid.minColumnWidth;
} else if (isPercentageWidth) {
this._calcWidth = parseFloat(colWidth) / 100 * this.grid.calcWidth;
const currentCalcWidth = parseFloat(colWidth) / 100 * this.grid.calcWidth;
this._calcWidth = this.grid.calcWidth ? this.getConstrainedSizePx(currentCalcWidth) : 0;
} else if (!colWidth || isAutoWidth && !this.autoSize) {
// no width
this._calcWidth = this.defaultWidth || this.grid.getPossibleColumnWidth();
const currentCalcWidth = this.defaultWidth || this.grid.getPossibleColumnWidth();
this._calcWidth = this.getConstrainedSizePx(currentCalcWidth);
} else {
this._calcWidth = this.width;
const currentCalcWidth = parseFloat(this.width);
this._calcWidth =this.getConstrainedSizePx(currentCalcWidth);
}
this.calcPixelWidth = parseFloat(this._calcWidth);
}
Expand Down
15 changes: 9 additions & 6 deletions projects/igniteui-angular/src/lib/grids/grid-base.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5420,22 +5420,25 @@ export abstract class IgxGridBaseDirective implements GridType,

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

const columnsToSize = this.hasColumnLayouts ?
combinedBlocksSize - columnsWithSetWidths.length :
visibleChildColumns.length - columnsWithSetWidths.length;
const sumExistingWidths = columnsWithSetWidths
.reduce((prev, curr) => {
const colWidth = curr.width;
const colInstance = this.hasColumnLayouts ? curr.ref : curr;
const colWidth = !colInstance.widthConstrained ? curr.width : colInstance.calcPixelWidth;
let widthValue = parseFloat(colWidth);
if (isNaN(widthValue)) {
widthValue = MINIMUM_COLUMN_WIDTH;
}
const currWidth = colWidth && typeof colWidth === 'string' && colWidth.indexOf('%') !== -1 ?
widthValue / 100 * computedWidth :
widthValue;
return prev + currWidth;
// apply constraints, since constraint may change width
const constrainedWidth = this.hasColumnLayouts ? currWidth : colInstance.getConstrainedSizePx(currWidth);
return prev + constrainedWidth;
}, 0);

// When all columns are hidden, return 0px width
Expand Down Expand Up @@ -6551,7 +6554,7 @@ export abstract class IgxGridBaseDirective implements GridType,
if (width && typeof width !== 'string') {
width = String(width);
}
const minWidth = width.indexOf('%') === -1 ? column.minWidthPx : column.minWidthPercent;
const minWidth = width.indexOf('%') === -1 ? column.userSetMinWidthPx : column.minWidthPercent;
const maxWidth = width.indexOf('%') === -1 ? column.maxWidthPx : column.maxWidthPercent;
if (column.hidden) {
return width;
Expand Down Expand Up @@ -7333,8 +7336,8 @@ export abstract class IgxGridBaseDirective implements GridType,
let maxSize = Math.ceil(Math.max(...cellsContentWidths)) + 1;
if (col.maxWidth && maxSize > col.maxWidthPx) {
maxSize = col.maxWidthPx;
} else if (maxSize < col.minWidthPx) {
maxSize = col.minWidthPx;
} else if (maxSize < col.userSetMinWidthPx) {
maxSize = col.userSetMinWidthPx;
}
col.autoSize = maxSize;
col.resetCaches();
Expand Down
3 changes: 2 additions & 1 deletion projects/igniteui-angular/src/lib/grids/grid/column.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,8 @@ describe('IgxGrid - Column properties #grid', () => {
tick();

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