Skip to content

Commit 6e1decb

Browse files
fix(module:carousel): not adapting to new size when resizing (#8374)
* fix(module:carousel): not adapting to new size when resizing * fix(module:carousel): not adapting to new size when resizing * fix(module:carousel): not adapting to new size when resizing
1 parent ce33294 commit 6e1decb

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

components/carousel/carousel.component.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ import {
3232
ViewEncapsulation
3333
} from '@angular/core';
3434
import { fromEvent, Subject } from 'rxjs';
35-
import { takeUntil } from 'rxjs/operators';
35+
import { debounceTime, distinctUntilChanged, takeUntil } from 'rxjs/operators';
3636

37+
import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer';
3738
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
3839
import { NzDragService, NzResizeService } from 'ng-zorro-antd/core/services';
3940
import { BooleanInput, NumberInput, NzSafeAny } from 'ng-zorro-antd/core/types';
@@ -182,6 +183,7 @@ export class NzCarouselComponent implements AfterContentInit, AfterViewInit, OnD
182183
private readonly platform: Platform,
183184
private readonly resizeService: NzResizeService,
184185
private readonly nzDragService: NzDragService,
186+
private nzResizeObserver: NzResizeObserver,
185187
@Optional() private directionality: Directionality,
186188
@Optional() @Inject(NZ_CAROUSEL_CUSTOM_STRATEGIES) private customStrategies: NzCarouselStrategyRegistryItem[]
187189
) {
@@ -222,6 +224,13 @@ export class NzCarouselComponent implements AfterContentInit, AfterViewInit, OnD
222224
});
223225
});
224226
});
227+
228+
this.nzResizeObserver
229+
.observe(this.el)
230+
.pipe(debounceTime(100), distinctUntilChanged(), takeUntil(this.destroy$))
231+
.subscribe(() => {
232+
this.layout();
233+
});
225234
}
226235

227236
ngAfterContentInit(): void {

components/carousel/carousel.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { BidiModule, Dir } from '@angular/cdk/bidi';
22
import { LEFT_ARROW, RIGHT_ARROW } from '@angular/cdk/keycodes';
33
import { Component, DebugElement, ViewChild } from '@angular/core';
4-
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
4+
import { ComponentFixture, discardPeriodicTasks, fakeAsync, TestBed, tick } from '@angular/core/testing';
55
import { By } from '@angular/platform-browser';
66

7+
import { NzResizeObserver } from 'ng-zorro-antd/cdk/resize-observer';
78
import { dispatchKeyboardEvent, dispatchMouseEvent } from 'ng-zorro-antd/core/testing';
89

910
import { NzCarouselContentDirective } from './carousel-content.directive';
@@ -76,6 +77,28 @@ describe('carousel', () => {
7677
).toBe('A');
7778
});
7879

80+
it('should call layout on component resize', fakeAsync(() => {
81+
testComponent.nzCarouselComponent.ngOnInit();
82+
const spy = spyOn(testComponent.nzCarouselComponent, 'layout');
83+
window.dispatchEvent(new Event('resize'));
84+
tick(500);
85+
86+
(testComponent.nzCarouselComponent['nzResizeObserver'] as NzResizeObserver)
87+
.observe(testComponent.nzCarouselComponent.el)
88+
.subscribe(() => {
89+
expect(spy).toHaveBeenCalled();
90+
});
91+
}));
92+
93+
it('should call layout on component resize', fakeAsync(() => {
94+
const spyOnResize = spyOn(testComponent.nzCarouselComponent, 'layout');
95+
window.dispatchEvent(new Event('resize'));
96+
tick(500);
97+
98+
expect(spyOnResize).toHaveBeenCalled();
99+
discardPeriodicTasks();
100+
}));
101+
79102
it('should click content change', () => {
80103
expect(carouselContents[0].nativeElement.classList).toContain('slick-active');
81104
carouselWrapper.nativeElement.querySelector('.slick-dots').lastElementChild.click();

0 commit comments

Comments
 (0)