Skip to content

Commit c4d0105

Browse files
committed
fix(carousel): add missing wrap prop and handling
1 parent f3d996c commit c4d0105

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

projects/coreui-angular/src/lib/carousel/carousel-state.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ export class CarouselState {
2626
if (prevState.activeItemIndex !== nextState.activeItemIndex) {
2727
const activeItemIndex = this.state.activeItemIndex || 0;
2828
const itemInterval = this.state.items && this.state.items[activeItemIndex]?.interval || -1;
29-
this.carouselService.setIndex({ active: nextState.activeItemIndex, interval: itemInterval });
29+
this.carouselService.setIndex({
30+
active: nextState.activeItemIndex,
31+
interval: itemInterval,
32+
lastItemIndex: (nextState.items?.length ?? 0) - 1
33+
});
3034
}
3135
}
3236

projects/coreui-angular/src/lib/carousel/carousel.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BehaviorSubject } from 'rxjs';
44
export interface ICarouselIndex {
55
active?: number;
66
interval?: number;
7+
lastItemIndex?: number;
78
}
89

910
@Injectable()

projects/coreui-angular/src/lib/carousel/carousel/carousel.component.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
6464
* @default 'slide'
6565
*/
6666
@Input() transition: 'slide' | 'crossfade' = 'slide';
67+
/**
68+
* Set whether the carousel should cycle continuously or have hard stops.
69+
* @type boolean
70+
* @default true
71+
*/
72+
@Input() wrap = true;
6773
/**
6874
* Event emitted on carousel item change. [docs]
6975
* @type number
@@ -156,12 +162,13 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
156162

157163
private carouselStateSubscribe(subscribe: boolean = true): void {
158164
if (subscribe) {
159-
this.carouselIndexSubscription = this.carouselService.carouselIndex$.subscribe((nextIndex) => {
160-
if ('active' in nextIndex) {
161-
this.itemChange.emit(nextIndex.active);
165+
this.carouselIndexSubscription = this.carouselService.carouselIndex$.subscribe((nextItem) => {
166+
if ('active' in nextItem) {
167+
this.itemChange.emit(nextItem.active);
162168
}
163-
this.activeItemInterval = typeof nextIndex.interval === 'number' && nextIndex.interval > -1 ? nextIndex.interval : this.interval;
164-
this.setTimer();
169+
this.activeItemInterval = typeof nextItem.interval === 'number' && nextItem.interval > -1 ? nextItem.interval : this.interval;
170+
const isLastItem = ((nextItem.active === nextItem.lastItemIndex) && this.direction === 'next') || ((nextItem.active === 0) && this.direction === 'prev');
171+
!this.wrap && isLastItem ? this.resetTimer() : this.setTimer();
165172
});
166173
} else {
167174
this.carouselIndexSubscription?.unsubscribe();

0 commit comments

Comments
 (0)