Skip to content

Commit 482cc97

Browse files
felipepetucorafaellmarques
authored andcommitted
fix(table): ajusta redimensionamento do loading
1 parent 0f72ba9 commit 482cc97

File tree

5 files changed

+154
-13
lines changed

5 files changed

+154
-13
lines changed

projects/ui/src/lib/components/po-table/po-table-base.component.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { PoTableService } from './services/po-table.service';
2020
class PoTableComponent extends PoTableBaseComponent {
2121
checkInfiniteScroll() {}
2222
calculateHeightTableContainer(height) {}
23+
changeSizeLoading() {}
24+
changeHeaderWidth() {}
2325
}
2426

2527
describe('PoTableBaseComponent:', () => {
@@ -532,6 +534,39 @@ describe('PoTableBaseComponent:', () => {
532534
expect(component.calculateHeightTableContainer).not.toHaveBeenCalled();
533535
});
534536

537+
it('ngOnChanges: should call `changeSizeLoading` if height is changed', () => {
538+
spyOn(component, 'changeSizeLoading');
539+
const height = 400;
540+
const changes = <any>{ height };
541+
component.height = height;
542+
543+
component.ngOnChanges(changes);
544+
545+
expect(component['changeSizeLoading']).toHaveBeenCalled();
546+
});
547+
548+
it('ngOnChanges: should call `changeSizeLoading` if there is items', () => {
549+
spyOn(component, 'changeSizeLoading');
550+
items = component.items;
551+
const changes = <any>{ items };
552+
component.items = items;
553+
554+
component.ngOnChanges(changes);
555+
556+
expect(component['changeSizeLoading']).toHaveBeenCalled();
557+
});
558+
559+
it('ngOnChanges: should´n call `changeSizeLoading` if there is no items', () => {
560+
spyOn(component, 'changeSizeLoading');
561+
items = null;
562+
const changes = <any>{ items };
563+
component.items = null;
564+
565+
component.ngOnChanges(changes);
566+
567+
expect(component['changeSizeLoading']).toHaveBeenCalled();
568+
});
569+
535570
describe('isEverySelected:', () => {
536571
let rows;
537572

projects/ui/src/lib/components/po-table/po-table-base.component.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ export abstract class PoTableBaseComponent implements OnChanges, OnDestroy {
451451
itemsSelected: Array<any> = [];
452452
paramsFilter: {};
453453
filteredItems: Array<any> = [];
454+
initialized = false;
454455
private initialVisibleColumns: boolean = false;
455456
private _spacing: PoTableColumnSpacing = PoTableColumnSpacing.Medium;
456457
private _filteredColumns: Array<string>;
@@ -963,6 +964,11 @@ export abstract class PoTableBaseComponent implements OnChanges, OnDestroy {
963964
if (changes.height) {
964965
this.calculateHeightTableContainer(this.height);
965966
}
967+
968+
if ((changes.height || changes.items) && this.initialized) {
969+
this.changeHeaderWidth();
970+
}
971+
this.changeSizeLoading();
966972
}
967973

968974
selectAllRows() {
@@ -1277,4 +1283,8 @@ export abstract class PoTableBaseComponent implements OnChanges, OnDestroy {
12771283
protected abstract calculateHeightTableContainer(height);
12781284

12791285
protected abstract checkInfiniteScroll();
1286+
1287+
protected abstract changeSizeLoading();
1288+
1289+
protected abstract changeHeaderWidth();
12801290
}

projects/ui/src/lib/components/po-table/po-table.component.html

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@
5050
</po-container>
5151

5252
<ng-template #tableContainerTemplate>
53-
<div [class.po-table-container-relative]="loading">
54-
<div *ngIf="loading" class="po-table-overlay">
55-
<po-loading class="po-table-overlay-content" [p-text]="literals.loadingData"></po-loading>
56-
</div>
57-
53+
<div [class.po-table-container-sticky]="loading">
54+
<po-loading-overlay *ngIf="loading" [p-text]="literals.loadingData" [p-size]="sizeLoading"></po-loading-overlay>
5855
<div class="po-table-main-container">
5956
<div
6057
#tableWrapper
@@ -865,7 +862,7 @@
865862
</ng-template>
866863

867864
<ng-template #noColumnsWithHeight>
868-
<div class="po-table-header-fixed-inner" [style.width.px]="noColumnsHeader?.nativeElement.offsetWidth">
865+
<div class="po-table-header-fixed-inner" [style.width.px]="headerWidth">
869866
{{ hasValidColumns ? literals.noVisibleColumn : literals.noColumns }}
870867
</div>
871868
</ng-template>

projects/ui/src/lib/components/po-table/po-table.component.spec.ts

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,19 +2156,17 @@ describe('PoTableComponent:', () => {
21562156

21572157
fixture.detectChanges();
21582158

2159-
expect(nativeElement.querySelector('div.po-table-container-relative')).toBeTruthy();
2160-
expect(nativeElement.querySelector('div.po-table-overlay')).toBeTruthy();
2161-
expect(nativeElement.querySelector('po-loading.po-table-overlay-content')).toBeTruthy();
2159+
expect(nativeElement.querySelector('div.po-table-container-sticky')).toBeTruthy();
2160+
expect(nativeElement.querySelector('po-loading-overlay')).toBeTruthy();
21622161
});
21632162

21642163
it('shouldn`t show loading on table', () => {
21652164
component.loading = false;
21662165

21672166
fixture.detectChanges();
21682167

2169-
expect(nativeElement.querySelector('div.po-table-container-relative')).toBeFalsy();
2170-
expect(nativeElement.querySelector('div.po-table-overlay')).toBeFalsy();
2171-
expect(nativeElement.querySelector('po-loading.po-table-overlay-content')).toBeFalsy();
2168+
expect(nativeElement.querySelector('div.po-table-container-sticky')).toBeFalsy();
2169+
expect(nativeElement.querySelector('po-loading-overlay')).toBeFalsy();
21722170
});
21732171

21742172
it('should show td with `po-table-column-actions` class if has more than 1 action', () => {
@@ -3195,4 +3193,82 @@ describe('PoTableComponent:', () => {
31953193

31963194
expect(component['isDraggable']).toBeFalse();
31973195
});
3196+
3197+
it('changeSizeLoading: should set sm if height of parent element is smaller or equal 150px', () => {
3198+
const fakeTable = {
3199+
sizeLoading: 'sm',
3200+
tableWrapperElement: {
3201+
nativeElement: {
3202+
offsetHeight: 150
3203+
}
3204+
}
3205+
};
3206+
3207+
component['changeSizeLoading'].call(fakeTable);
3208+
3209+
expect(fakeTable.sizeLoading).toBe('sm');
3210+
});
3211+
3212+
it('changeSizeLoading: should set lg if height of parent element is higher or equal 260px', () => {
3213+
const fakeTable = {
3214+
sizeLoading: 'sm',
3215+
tableWrapperElement: {
3216+
nativeElement: {
3217+
offsetHeight: 260
3218+
}
3219+
}
3220+
};
3221+
3222+
component['changeSizeLoading'].call(fakeTable);
3223+
3224+
expect(fakeTable.sizeLoading).toBe('lg');
3225+
});
3226+
3227+
it('changeSizeLoading: should set md if height of parent element is between 150px and 260px', () => {
3228+
const fakeTable = {
3229+
sizeLoading: 'sm',
3230+
tableWrapperElement: {
3231+
nativeElement: {
3232+
offsetHeight: 200
3233+
}
3234+
}
3235+
};
3236+
3237+
component['changeSizeLoading'].call(fakeTable);
3238+
3239+
expect(fakeTable.sizeLoading).toBe('md');
3240+
});
3241+
3242+
it('changeSizeLoading: should set lg if there is no height in parentElement', () => {
3243+
const fakeTable = {
3244+
sizeLoading: 'sm',
3245+
tableTemplate: {
3246+
nativeElement: {
3247+
parentElement: {
3248+
anotherProperty: null
3249+
}
3250+
}
3251+
}
3252+
};
3253+
3254+
component['changeSizeLoading'].call(fakeTable);
3255+
3256+
expect(fakeTable.sizeLoading).toBe('lg');
3257+
});
3258+
3259+
it('changeHeaderWidth: should set width if noColumnsHeader ', () => {
3260+
const fakeTable = {
3261+
headerWidth: null,
3262+
noColumnsHeader: {
3263+
nativeElement: {
3264+
offsetWidth: 300
3265+
}
3266+
},
3267+
changeDetector: { detectChanges: () => {} }
3268+
};
3269+
3270+
component['changeHeaderWidth'].call(fakeTable);
3271+
3272+
expect(fakeTable.headerWidth).toBe(300);
3273+
});
31983274
});

projects/ui/src/lib/components/po-table/po-table.component.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ export class PoTableComponent extends PoTableBaseComponent implements AfterViewI
141141
inputFieldValue = '';
142142
JSON: JSON;
143143
newOrderColumns: Array<PoTableColumn>;
144+
sizeLoading: string = 'sm';
145+
headerWidth: number;
144146

145147
close: PoModalAction = {
146148
action: () => {
@@ -162,7 +164,6 @@ export class PoTableComponent extends PoTableBaseComponent implements AfterViewI
162164
private differ;
163165
private footerHeight;
164166
private headerHeight;
165-
private initialized = false;
166167
private timeoutResize;
167168
private visibleElement = false;
168169
private scrollEvent$: Observable<any>;
@@ -295,8 +296,17 @@ export class PoTableComponent extends PoTableBaseComponent implements AfterViewI
295296
this.idRadio = `po-radio-${uuid()}`;
296297
}
297298

299+
changeHeaderWidth() {
300+
if (this.noColumnsHeader) {
301+
this.headerWidth = this.noColumnsHeader?.nativeElement.offsetWidth;
302+
}
303+
this.changeDetector.detectChanges();
304+
}
305+
298306
ngAfterViewInit() {
299307
this.initialized = true;
308+
this.changeHeaderWidth();
309+
this.changeSizeLoading();
300310
}
301311

302312
showMoreInfiniteScroll({ target }): void {
@@ -309,6 +319,7 @@ export class PoTableComponent extends PoTableBaseComponent implements AfterViewI
309319
ngDoCheck() {
310320
this.checkChangesItems();
311321
this.verifyCalculateHeightTableContainer();
322+
312323
// Permite que os cabeçalhos sejam calculados na primeira vez que o componente torna-se visível,
313324
// evitando com isso, problemas com Tabs ou Divs que iniciem escondidas.
314325
if (this.tableWrapperElement?.nativeElement.offsetWidth && !this.visibleElement && this.initialized) {
@@ -725,6 +736,18 @@ export class PoTableComponent extends PoTableBaseComponent implements AfterViewI
725736
this.eventDelete.emit(newItemsFiltered);
726737
}
727738

739+
protected changeSizeLoading() {
740+
const tableHeight = this.tableWrapperElement?.nativeElement?.offsetHeight;
741+
742+
if (tableHeight <= 150) {
743+
this.sizeLoading = 'sm';
744+
} else if (tableHeight > 150 && tableHeight < 260) {
745+
this.sizeLoading = 'md';
746+
} else {
747+
this.sizeLoading = 'lg';
748+
}
749+
}
750+
728751
private checkChangesItems() {
729752
const changesItems = this.differ.diff(this.items);
730753

0 commit comments

Comments
 (0)