Skip to content

Commit e02f751

Browse files
committedMar 7, 2022
fix(carousel-item): add missing item interval prop
1 parent d63a8d2 commit e02f751

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed
 

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
88
@Component({
99
selector: 'c-carousel-item',
1010
templateUrl: './carousel-item.component.html',
11-
styleUrls: ['./carousel-item.component.scss'],
11+
styleUrls: ['./carousel-item.component.scss']
1212
})
1313
export class CarouselItemComponent implements OnDestroy, AfterViewInit {
1414

1515
static ngAcceptInputType_active: BooleanInput;
16-
private _active = false;
1716

1817
index?: number;
1918
private carouselIndexSubscription?: Subscription;
@@ -29,12 +28,20 @@ export class CarouselItemComponent implements OnDestroy, AfterViewInit {
2928
get active(): boolean {
3029
return this._active;
3130
}
31+
private _active = false;
32+
33+
/**
34+
* Time delay before cycling to next item. If -1, uses carousel interval value.
35+
* @type number
36+
* @default -1
37+
*/
38+
@Input() interval: number = -1;
3239

3340
@HostBinding('class')
3441
get hostClasses(): any {
3542
return {
3643
'carousel-item': true,
37-
active: this.active,
44+
active: this.active
3845
};
3946
}
4047

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export class CarouselState {
2424
const nextState = { ...this._state, ...state };
2525
this._state = nextState;
2626
if (prevState.activeItemIndex !== nextState.activeItemIndex) {
27-
this.carouselService.setIndex({active: nextState.activeItemIndex});
27+
const activeItemIndex = this.state.activeItemIndex || 0;
28+
const itemInterval = this.state.items && this.state.items[activeItemIndex]?.interval || -1;
29+
this.carouselService.setIndex({ active: nextState.activeItemIndex, interval: itemInterval });
2830
}
2931
}
3032

@@ -35,7 +37,7 @@ export class CarouselState {
3537
item.index = i;
3638
});
3739
this.state = {
38-
items: itemsArray,
40+
items: itemsArray
3941
};
4042
} else {
4143
this.reset();
@@ -46,7 +48,7 @@ export class CarouselState {
4648
this.carouselService.setIndex(nextIndex);
4749
}
4850

49-
direction(direction: 'next' | 'prev' = 'next' ): number {
51+
direction(direction: 'next' | 'prev' = 'next'): number {
5052
this.state = { direction };
5153
const { activeItemIndex = -1, items } = this.state;
5254
const itemsCount = items?.length ?? 0;

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { BehaviorSubject } from 'rxjs';
33

44
export interface ICarouselIndex {
55
active?: number;
6+
interval?: number;
67
}
78

89
@Injectable()

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

+15-4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
8484
public onMouseleave($event: MouseEvent): void {
8585
this.setTimer();
8686
}
87+
private activeItemInterval = 0;
8788

8889
constructor(
8990
@Inject(CarouselConfig) private config: CarouselConfig,
@@ -103,16 +104,24 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
103104

104105
ngAfterContentInit(): void {
105106
this.carouselState.state = { activeItemIndex: this.activeIndex, animate: this.animate };
106-
this.setTimer();
107107
}
108108

109+
set visible(value) {
110+
this._visible = value;
111+
}
112+
get visible() {
113+
return this._visible;
114+
}
115+
private _visible: boolean = true;
116+
109117
setTimer(): void {
118+
const interval = this.activeItemInterval || 0;
110119
this.resetTimer();
111-
if (this.interval > 0) {
120+
if (interval > 0) {
112121
this.timerId = setTimeout(() => {
113122
const nextIndex = this.carouselState.direction(this.direction);
114123
this.carouselState.state = { activeItemIndex: nextIndex };
115-
}, this.interval);
124+
}, interval);
116125
}
117126
}
118127

@@ -124,8 +133,10 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
124133
if (subscribe) {
125134
this.carouselIndexSubscription = this.carouselService.carouselIndex$.subscribe((nextIndex) => {
126135
if ('active' in nextIndex) {
127-
this.setTimer();
136+
this.itemChange.emit(nextIndex.active);
128137
}
138+
this.activeItemInterval = typeof nextIndex.interval === 'number' && nextIndex.interval > -1 ? nextIndex.interval : this.interval;
139+
this.setTimer();
129140
});
130141
} else {
131142
this.carouselIndexSubscription?.unsubscribe();

0 commit comments

Comments
 (0)