Skip to content

Commit fd1e110

Browse files
rafaelss95emoralesb05
authored and
emoralesb05
committed
fix(overlay): dimension not updated after init
1 parent c3e267f commit fd1e110

File tree

5 files changed

+106
-8
lines changed

5 files changed

+106
-8
lines changed

src/cdk/overlay/overlay-directives.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ describe('Overlay directives', () => {
141141

142142
const pane = overlayContainerElement.children[0] as HTMLElement;
143143
expect(pane.style.width).toEqual('250px');
144+
145+
fixture.componentInstance.isOpen = false;
146+
fixture.detectChanges();
147+
148+
fixture.componentInstance.width = 500;
149+
fixture.componentInstance.isOpen = true;
150+
fixture.detectChanges();
151+
152+
expect(pane.style.width).toEqual('500px');
144153
});
145154

146155
it('should set the height', () => {
@@ -150,6 +159,15 @@ describe('Overlay directives', () => {
150159

151160
const pane = overlayContainerElement.children[0] as HTMLElement;
152161
expect(pane.style.height).toEqual('100vh');
162+
163+
fixture.componentInstance.isOpen = false;
164+
fixture.detectChanges();
165+
166+
fixture.componentInstance.height = '50vh';
167+
fixture.componentInstance.isOpen = true;
168+
fixture.detectChanges();
169+
170+
expect(pane.style.height).toEqual('50vh');
153171
});
154172

155173
it('should set the min width', () => {
@@ -159,6 +177,15 @@ describe('Overlay directives', () => {
159177

160178
const pane = overlayContainerElement.children[0] as HTMLElement;
161179
expect(pane.style.minWidth).toEqual('250px');
180+
181+
fixture.componentInstance.isOpen = false;
182+
fixture.detectChanges();
183+
184+
fixture.componentInstance.minWidth = 500;
185+
fixture.componentInstance.isOpen = true;
186+
fixture.detectChanges();
187+
188+
expect(pane.style.minWidth).toEqual('500px');
162189
});
163190

164191
it('should set the min height', () => {
@@ -168,6 +195,15 @@ describe('Overlay directives', () => {
168195

169196
const pane = overlayContainerElement.children[0] as HTMLElement;
170197
expect(pane.style.minHeight).toEqual('500px');
198+
199+
fixture.componentInstance.isOpen = false;
200+
fixture.detectChanges();
201+
202+
fixture.componentInstance.minHeight = 500;
203+
fixture.componentInstance.isOpen = true;
204+
fixture.detectChanges();
205+
206+
expect(pane.style.minHeight).toEqual('500px');
171207
});
172208

173209
it('should create the backdrop if designated', () => {

src/cdk/overlay/overlay-directives.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
336336
private _attachOverlay() {
337337
if (!this._overlayRef) {
338338
this._createOverlay();
339+
} else {
340+
// Update the overlay size, in case the directive's inputs have changed
341+
this._overlayRef.updateSize({
342+
width: this.width,
343+
minWidth: this.minWidth,
344+
height: this.height,
345+
minHeight: this.minHeight,
346+
});
339347
}
340348

341349
this._position.withDirection(this.dir);

src/lib/paginator/paginator.spec.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('MatPaginator', () => {
2323
MatPaginatorWithoutPageSizeApp,
2424
MatPaginatorWithoutOptionsApp,
2525
MatPaginatorWithoutInputsApp,
26+
MatPaginatorWithStringValues
2627
],
2728
providers: [MatPaginatorIntl]
2829
}).compileComponents();
@@ -251,6 +252,18 @@ describe('MatPaginator', () => {
251252
fixture.detectChanges();
252253
expect(fixture.nativeElement.querySelector('.mat-select')).toBeNull();
253254
});
255+
256+
it('should handle the number inputs being passed in as strings', () => {
257+
const withStringFixture = TestBed.createComponent(MatPaginatorWithStringValues);
258+
const withStringPaginator = withStringFixture.componentInstance.paginator;
259+
260+
withStringFixture.detectChanges();
261+
262+
expect(withStringPaginator.pageIndex).toEqual(0);
263+
expect(withStringPaginator.length).toEqual(100);
264+
expect(withStringPaginator.pageSize).toEqual(10);
265+
expect(withStringPaginator.pageSizeOptions).toEqual([5, 10, 25, 100]);
266+
});
254267
});
255268

256269
function getPreviousButton(fixture: ComponentFixture<any>) {
@@ -264,10 +277,10 @@ function getNextButton(fixture: ComponentFixture<any>) {
264277
@Component({
265278
template: `
266279
<mat-paginator [pageIndex]="pageIndex"
267-
[pageSize]="pageSize"
268-
[pageSizeOptions]="pageSizeOptions"
269-
[length]="length"
270-
(page)="latestPageEvent = $event">
280+
[pageSize]="pageSize"
281+
[pageSizeOptions]="pageSizeOptions"
282+
[length]="length"
283+
(page)="latestPageEvent = $event">
271284
</mat-paginator>
272285
`,
273286
})
@@ -312,3 +325,16 @@ class MatPaginatorWithoutPageSizeApp {
312325
class MatPaginatorWithoutOptionsApp {
313326
@ViewChild(MatPaginator) paginator: MatPaginator;
314327
}
328+
329+
@Component({
330+
template: `
331+
<mat-paginator pageIndex="0"
332+
pageSize="10"
333+
[pageSizeOptions]="['5', '10', '25', '100']"
334+
length="100">
335+
</mat-paginator>
336+
`
337+
})
338+
class MatPaginatorWithStringValues {
339+
@ViewChild(MatPaginator) paginator: MatPaginator;
340+
}

src/lib/paginator/paginator.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {coerceNumberProperty} from '@angular/cdk/coercion';
910
import {
1011
ChangeDetectionStrategy,
1112
ChangeDetectorRef,
@@ -64,7 +65,7 @@ export class MatPaginator implements OnInit, OnDestroy {
6465
@Input()
6566
get pageIndex(): number { return this._pageIndex; }
6667
set pageIndex(pageIndex: number) {
67-
this._pageIndex = pageIndex;
68+
this._pageIndex = coerceNumberProperty(pageIndex);
6869
this._changeDetectorRef.markForCheck();
6970
}
7071
_pageIndex: number = 0;
@@ -73,7 +74,7 @@ export class MatPaginator implements OnInit, OnDestroy {
7374
@Input()
7475
get length(): number { return this._length; }
7576
set length(length: number) {
76-
this._length = length;
77+
this._length = coerceNumberProperty(length);
7778
this._changeDetectorRef.markForCheck();
7879
}
7980
_length: number = 0;
@@ -82,7 +83,7 @@ export class MatPaginator implements OnInit, OnDestroy {
8283
@Input()
8384
get pageSize(): number { return this._pageSize; }
8485
set pageSize(pageSize: number) {
85-
this._pageSize = pageSize;
86+
this._pageSize = coerceNumberProperty(pageSize);
8687
this._updateDisplayedPageSizeOptions();
8788
}
8889
private _pageSize: number;
@@ -91,7 +92,7 @@ export class MatPaginator implements OnInit, OnDestroy {
9192
@Input()
9293
get pageSizeOptions(): number[] { return this._pageSizeOptions; }
9394
set pageSizeOptions(pageSizeOptions: number[]) {
94-
this._pageSizeOptions = pageSizeOptions;
95+
this._pageSizeOptions = (pageSizeOptions || []).map(p => coerceNumberProperty(p));
9596
this._updateDisplayedPageSizeOptions();
9697
}
9798
private _pageSizeOptions: number[] = [];

src/lib/select/select.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,33 @@ describe('MatSelect', () => {
645645
expect(() => fixture.componentInstance.select.open()).not.toThrow();
646646
});
647647

648+
it('should set the width of the overlay based on the trigger and resize', fakeAsync(() => {
649+
trigger.style.width = '200px';
650+
fixture.detectChanges();
651+
flush();
652+
653+
trigger.click();
654+
fixture.detectChanges();
655+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
656+
expect(pane.style.minWidth).toBe('200px',
657+
'Expected pane minWidth to be 200px initially');
658+
659+
const backdrop =
660+
overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
661+
backdrop.click();
662+
fixture.detectChanges();
663+
flush();
664+
665+
trigger.style.width = '400px';
666+
fixture.detectChanges();
667+
flush();
668+
669+
trigger.click();
670+
fixture.detectChanges();
671+
expect(pane.style.minWidth).toBe('400px',
672+
'Expected pane minWidth to be 400px after resize');
673+
}));
674+
648675
it('should open the panel when trigger is clicked', fakeAsync(() => {
649676
trigger.click();
650677
fixture.detectChanges();

0 commit comments

Comments
 (0)