Skip to content

Commit c7f70c9

Browse files
authored
Merge pull request #10127 from IgniteUI/sstoychev/for-of-margin
feat(for-of): allowing margins to properly size the internal v model (#10039)
2 parents 68695cc + 4559b52 commit c7f70c9

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ All notable changes for each version of this project will be documented in this
2222
### General
2323
- `igxGrid`, `igxHierarchicalGrid`, `igxTreeGrid`
2424
- 'oddRowCSS' and 'evenRowCSS' properties has been deprecated
25+
- `IgxForOf` - now takes margins into account when calculating the space that each element takes.
26+
_Note:_ If your virtualized items contain margins, please calculate them into the `itemSize` value for the best possible initial virtualized state.
27+
28+
## 12.1.6
2529

2630
## 12.1.6
2731

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.spec.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,33 @@ describe('IgxForOf directive -', () => {
324324
chunkSize = (virtualContainer as any)._calcMaxChunkSize();
325325
expect(chunkSize).toEqual(10);
326326
});
327+
328+
it('should take item margins into account when calculating the size cache', async () => {
329+
fix.componentInstance.height = '600px';
330+
fix.componentInstance.itemSize = '100px';
331+
const virtualContainer = fix.componentInstance.parentVirtDir;
332+
virtualContainer.igxForSizePropName = 'height';
333+
fix.componentInstance.data = [
334+
{ 1: '1', height: '100px', margin: '30px' },
335+
{ 1: '2', height: '100px', margin: '0px' },
336+
{ 1: '3', height: '100px', margin: '0px' },
337+
{ 1: '4', height: '100px', margin: '0px' },
338+
{ 1: '5', height: '100px', margin: '0px' },
339+
{ 1: '6', height: '100px', margin: '0px' },
340+
{ 1: '7', height: '100px', margin: '0px' },
341+
{ 1: '8', height: '100px', margin: '30px' },
342+
{ 1: '9', height: '100px', margin: '30px' },
343+
{ 1: '10', height: '100px', margin: '30px' }
344+
];
345+
fix.detectChanges();
346+
await wait(200);
347+
const cache = (fix.componentInstance.parentVirtDir as any).heightCache;
348+
expect(cache).toEqual([130, 100, 100, 100, 100, 100, 100, 100, 100, 100]);
349+
fix.componentInstance.scrollTop(400);
350+
fix.detectChanges();
351+
await wait(200);
352+
expect(cache).toEqual([130, 100, 100, 100, 100, 100, 100, 130, 130, 130]);
353+
});
327354
});
328355

329356
describe('vertical and horizontal virtual component', () => {
@@ -1185,7 +1212,7 @@ describe('IgxForOf directive -', () => {
11851212
const secondForOf = fix.componentInstance.secondForOfDir;
11861213
expect(secondForOf.state.startIndex).toBe(0);
11871214
});
1188-
});
1215+
});
11891216
});
11901217

11911218
class DataGenerator {
@@ -1401,7 +1428,9 @@ export class VirtualComponent {
14011428
[igxForScrollOrientation]="'vertical'"
14021429
[igxForContainerSize]='height'
14031430
[igxForItemSize]='itemSize'>
1404-
<div [style.display]="'flex'" [style.height]="rowData.height || itemSize || '50px'">
1431+
<div [style.display]="'flex'"
1432+
[style.height]="rowData.height || itemSize || '50px'"
1433+
[style.margin-top]="rowData.margin || '0px'">
14051434
<div [style.min-width]=cols[0].width>{{rowData['1']}}</div>
14061435
<div [style.min-width]=cols[1].width>{{rowData['2']}}</div>
14071436
<div [style.min-width]=cols[2].width>{{rowData['3']}}</div>

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
735735

736736
/**
737737
* @hidden
738-
* Function that recaculates and updates cache sizes.
738+
* Function that recalculates and updates cache sizes.
739739
*/
740740
public recalcUpdateSizes() {
741741
const dimension = this.igxForScrollOrientation === 'horizontal' ?
@@ -753,8 +753,9 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
753753
if (!this.isRemote && !this.igxForOf[index]) {
754754
continue;
755755
}
756+
const margin = this.getMargin(rNode, dimension);
756757
const oldVal = dimension === 'height' ? this.heightCache[index] : this.igxForOf[index][dimension];
757-
const newVal = dimension === 'height' ? h : rNode.clientWidth;
758+
const newVal = (dimension === 'height' ? h : rNode.clientWidth) + margin;
758759
if (dimension === 'height') {
759760
this.heightCache[index] = newVal;
760761
} else {
@@ -855,6 +856,8 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
855856

856857
this.dc.instance._viewContainer.element.nativeElement.style.top = -(scrollOffset) + 'px';
857858

859+
this._zone.onStable.pipe(first()).subscribe(this.recalcUpdateSizes.bind(this));
860+
858861
this.dc.changeDetectorRef.detectChanges();
859862
if (prevStartIndex !== this.state.startIndex) {
860863
this.chunkLoad.emit(this.state);
@@ -1404,6 +1407,16 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
14041407
this.scrollPosition = newSize === this.scrollPosition ? newSize + 1 : newSize;
14051408
}
14061409
}
1410+
1411+
private getMargin(node, dimension: string): number {
1412+
const styles = window.getComputedStyle(node);
1413+
if (dimension === 'height') {
1414+
return parseFloat(styles['marginTop']) +
1415+
parseFloat(styles['marginBottom']) || 0;
1416+
}
1417+
return parseFloat(styles['marginLeft']) +
1418+
parseFloat(styles['marginRight']) || 0;
1419+
}
14071420
}
14081421

14091422
export const getTypeNameForDebugging = (type: any): string => type.name || typeof type;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3879,6 +3879,7 @@ describe('IgxGrid - Filtering actions - Excel style filtering #grid', () => {
38793879
scrollbar.scrollTop = displayContainer.getBoundingClientRect().height / 2;
38803880
await wait(200);
38813881
fix.detectChanges();
3882+
await wait(100);
38823883

38833884
// Type string in search box
38843885
const inputNativeElement = GridFunctions.getExcelStyleSearchComponentInput(fix, searchComponent);

0 commit comments

Comments
 (0)