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

fix(grid): set min width to header groups programmatically #3404

Merged
merged 2 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions projects/igniteui-angular/src/lib/grids/grid-base.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,22 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
}
}

/**
* Returns the `IgxGridHeaderGroupComponent`'s minimum allowed width.
* Used internally for restricting header group component width.
* The values below depend on the header cell default right/left padding values.
* @memberof IgxGridBaseComponent
*/
get defaultHeaderGroupMinWidth(): number {
if (this.isCosy()) {
return 32;
} else if (this.isCompact()) {
return 24;
} else {
return 48;
}
}

/**
* Returns the maximum width of the container for the pinned `IgxColumnComponent`s.
* ```typescript
Expand Down Expand Up @@ -2476,6 +2492,22 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
return this._unpinnedColumns.filter((col) => !col.hidden); // .sort((col1, col2) => col1.index - col2.index);
}

/**
* Returns the `width` to be set on `IgxGridHeaderGroupComponent`.
* @memberof IgxGridBaseComponent
*/
public getHeaderGroupWidth(column: IgxColumnComponent): string {
const colWidth = column.width;
const minWidth = this.defaultHeaderGroupMinWidth;
const isPercentageWidth = colWidth && typeof colWidth === 'string' && colWidth.indexOf('%') !== -1;

if (!isPercentageWidth && parseInt(column.width, 10) < minWidth) {
return minWidth.toString();
}

return column.width;
}

/**
* Returns the `IgxColumnComponent` by field name.
* ```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class IgxGridHeaderGroupComponent implements DoCheck {
@HostBinding('style.min-width')
@HostBinding('style.flex-basis')
get width() {
return this.column.width;
return this.grid.getHeaderGroupWidth(this.column);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('IgxGrid - Deferred Column Resizing', () => {
configureTestSuite();
const COLUMN_HEADER_CLASS = '.igx-grid__th';
const COLUMN_HEADER_GROUP_CLASS = '.igx-grid__thead-item';
const COLUMN_FILTER_CELL_SELECTOR = 'igx-grid-filtering-cell';

beforeEach(async(() => {
TestBed.configureTestingModule({
Expand All @@ -28,7 +29,9 @@ describe('IgxGrid - Deferred Column Resizing', () => {
GridFeaturesComponent,
LargePinnedColGridComponent,
NullColumnsComponent,
MultiColumnHeadersComponent
MultiColumnHeadersComponent,
ColGridComponent,
ColPercentageGridComponent
],
imports: [
FormsModule,
Expand Down Expand Up @@ -719,6 +722,51 @@ describe('IgxGrid - Deferred Column Resizing', () => {
fixture.detectChanges();
expect(column.width).toEqual('111px');
}));

it('should size headers correctly when column width is below the allowed minimum.', fakeAsync(() => {
const fixture = TestBed.createComponent(ColGridComponent);
fixture.detectChanges();

const headers = fixture.debugElement.queryAll(By.css(COLUMN_HEADER_CLASS));
const headerGroups = fixture.debugElement.queryAll(By.css(COLUMN_HEADER_GROUP_CLASS));
const filteringCells = fixture.debugElement.queryAll(By.css(COLUMN_FILTER_CELL_SELECTOR));

expect(headers[0].nativeElement.getBoundingClientRect().width).toBe(49);
expect(headers[1].nativeElement.getBoundingClientRect().width).toBe(50);
expect(headers[2].nativeElement.getBoundingClientRect().width).toBe(49);

expect(filteringCells[0].nativeElement.getBoundingClientRect().width).toBe(49);
expect(filteringCells[1].nativeElement.getBoundingClientRect().width).toBe(50);
expect(filteringCells[2].nativeElement.getBoundingClientRect().width).toBe(49);

expect(headerGroups[0].nativeElement.getBoundingClientRect().width).toBe(48);
expect(headerGroups[1].nativeElement.getBoundingClientRect().width).toBe(50);
expect(headerGroups[2].nativeElement.getBoundingClientRect().width).toBe(48);
}));

it('should size headers correctly when column width is in %.', fakeAsync(() => {
const fixture = TestBed.createComponent(ColPercentageGridComponent);
fixture.detectChanges();

const headers = fixture.debugElement.queryAll(By.css(COLUMN_HEADER_CLASS));
const headerGroups = fixture.debugElement.queryAll(By.css(COLUMN_HEADER_GROUP_CLASS));
const filteringCells = fixture.debugElement.queryAll(By.css(COLUMN_FILTER_CELL_SELECTOR));

expect(headers[0].nativeElement.getBoundingClientRect().width).toBe(100);
expect(headers[1].nativeElement.getBoundingClientRect().width).toBe(100);
expect(headers[2].nativeElement.getBoundingClientRect().width).toBe(100);
expect(headers[3].nativeElement.getBoundingClientRect().width).toBe(100);

expect(filteringCells[0].nativeElement.getBoundingClientRect().width).toBe(100);
expect(filteringCells[1].nativeElement.getBoundingClientRect().width).toBe(100);
expect(filteringCells[2].nativeElement.getBoundingClientRect().width).toBe(100);
expect(filteringCells[3].nativeElement.getBoundingClientRect().width).toBe(100);

expect(headerGroups[0].nativeElement.getBoundingClientRect().width).toBe(100);
expect(headerGroups[1].nativeElement.getBoundingClientRect().width).toBe(100);
expect(headerGroups[2].nativeElement.getBoundingClientRect().width).toBe(100);
expect(headerGroups[3].nativeElement.getBoundingClientRect().width).toBe(100);
}));
});

@Component({
Expand Down Expand Up @@ -827,3 +875,41 @@ export class NullColumnsComponent implements OnInit {
this.data = SampleTestData.contactInfoData();
}
}

@Component({
template: GridTemplateStrings.declareGrid(`width="400px" height="600px" [allowFiltering]="true"`, ``,
`<igx-column [field]="'Items'" [width]="'40px'" dataType="string" [filterable]="true"></igx-column>
<igx-column [field]="'ID'" [width]="'50px'" [header]="'ID'" [filterable]="true"></igx-column>
<igx-column [field]="'ProductName'" [width]="'30px'" dataType="string" [filterable]="true"></igx-column>
<igx-column [field]="'Test'" width="300px" dataType="string" [resizable]="true"></igx-column>
<igx-column [field]="'Downloads'" width="300px" dataType="number" [resizable]="true"></igx-column>
<igx-column [field]="'Category'" width="300px" dataType="string" [resizable]="true"></igx-column>`
)
})
export class ColGridComponent implements OnInit {
data = [];

@ViewChild(IgxGridComponent) public grid: IgxGridComponent;

ngOnInit() {
this.data = SampleTestData.generateProductData(10);
}
}

@Component({
template: GridTemplateStrings.declareGrid(`width="400px" height="600px" [allowFiltering]="true"`, ``,
`<igx-column [field]="'Items'" [width]="'25%'" dataType="string" [filterable]="true"></igx-column>
<igx-column [field]="'ID'" [width]="'25%'" [header]="'ID'" [filterable]="true"></igx-column>
<igx-column [field]="'ProductName'" [width]="'25%'" dataType="string" [filterable]="true"></igx-column>
<igx-column [field]="'Test'"[width]="'25%'" dataType="string" [resizable]="true"></igx-column>`
)
})
export class ColPercentageGridComponent implements OnInit {
data = [];

@ViewChild(IgxGridComponent) public grid: IgxGridComponent;

ngOnInit() {
this.data = SampleTestData.generateProductData(10);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@
</ng-container>
<ng-container *ngIf="pinnedColumns.length > 0">
<ng-template ngFor let-col [ngForOf]="onlyTopLevel(pinnedColumns)">
<igx-grid-header-group [igxColumnMovingDrag]="col" [attr.droppable]="true" [igxColumnMovingDrop]="col" [column]="col" [gridID]="id" [style.min-width.px]="col.width" [style.flex-basis.px]="col.width"></igx-grid-header-group>
<igx-grid-header-group [igxColumnMovingDrag]="col" [attr.droppable]="true" [igxColumnMovingDrop]="col" [column]="col" [gridID]="id" [style.min-width.px]="getHeaderGroupWidth(col)" [style.flex-basis.px]="getHeaderGroupWidth(col)"></igx-grid-header-group>
</ng-template>
</ng-container>
<ng-template igxGridFor let-col [igxGridForOf]="onlyTopLevel(unpinnedColumns)" [igxForScrollOrientation]="'horizontal'" [igxForScrollContainer]="parentVirtDir"
[igxForContainerSize]='unpinnedWidth' [igxForTrackBy]='trackColumnChanges' #headerContainer>
<igx-grid-header-group [igxColumnMovingDrag]="col" [attr.droppable]="true" [igxColumnMovingDrop]="col" [column]="col" [gridID]="id" [style.min-width.px]="col.width" [style.flex-basis.px]="col.width"></igx-grid-header-group>
<igx-grid-header-group [igxColumnMovingDrag]="col" [attr.droppable]="true" [igxColumnMovingDrop]="col" [column]="col" [gridID]="id" [style.min-width.px]="getHeaderGroupWidth(col)" [style.flex-basis.px]="getHeaderGroupWidth(col)"></igx-grid-header-group>
</ng-template>
<span *ngIf="hasMovableColumns && draggedColumn" [igxColumnMovingDrop]="parentVirtDir" [attr.droppable]="true" id="right" class="igx-grid__scroll-on-drag-right"></span>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
</ng-container>
<ng-container *ngIf="pinnedColumns.length > 0">
<ng-template ngFor let-col [ngForOf]="onlyTopLevel(pinnedColumns)">
<igx-grid-header-group [igxColumnMovingDrag]="col" [attr.droppable]="true" [igxColumnMovingDrop]="col" [column]="col" [gridID]="id" [style.min-width.px]="col.width" [style.flex-basis.px]="col.width"></igx-grid-header-group>
<igx-grid-header-group [igxColumnMovingDrag]="col" [attr.droppable]="true" [igxColumnMovingDrop]="col" [column]="col" [gridID]="id" [style.min-width.px]="getHeaderGroupWidth(col)" [style.flex-basis.px]="getHeaderGroupWidth(col)"></igx-grid-header-group>
</ng-template>
</ng-container>
<ng-template igxGridFor let-col [igxGridForOf]="onlyTopLevel(unpinnedColumns)" [igxForScrollOrientation]="'horizontal'" [igxForScrollContainer]="parentVirtDir"
[igxForContainerSize]='unpinnedWidth' [igxForTrackBy]='trackColumnChanges' #headerContainer>
<igx-grid-header-group [igxColumnMovingDrag]="col" [attr.droppable]="true" [igxColumnMovingDrop]="col" [column]="col" [gridID]="id" [style.min-width.px]="col.width" [style.flex-basis.px]="col.width"></igx-grid-header-group>
<igx-grid-header-group [igxColumnMovingDrag]="col" [attr.droppable]="true" [igxColumnMovingDrop]="col" [column]="col" [gridID]="id" [style.min-width.px]="getHeaderGroupWidth(col)" [style.flex-basis.px]="getHeaderGroupWidth(col)"></igx-grid-header-group>
</ng-template>
<span *ngIf="hasMovableColumns && draggedColumn" [igxColumnMovingDrop]="parentVirtDir" [attr.droppable]="true" id="right" class="igx-grid__scroll-on-drag-right"></span>
</div>
Expand Down