Skip to content

Commit 1686c2e

Browse files
committed
auto-cycle
1 parent ca2bd75 commit 1686c2e

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/controls/carousel/Carousel.tsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import * as telemetry from '../../common/telemetry';
1414
import CarouselImage from "./CarouselImage";
1515

1616
export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
17+
private _intervalId: number | undefined;
18+
1719
constructor(props: ICarouselProps) {
1820
super(props);
1921

@@ -42,9 +44,15 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
4244
this.setState({
4345
processingState: ProcessingState.idle
4446
});
47+
this.startCycle(); // restarting cycle when new slide is available
4548
}
4649
}
4750

51+
public componentDidMount() {
52+
// starting auto cycling
53+
this.startCycle();
54+
}
55+
4856

4957
public render(): React.ReactElement<ICarouselProps> {
5058
const { currentIndex, processingState } = this.state;
@@ -192,6 +200,9 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
192200
}
193201

194202
private onIndicatorClick = (e: React.MouseEvent<HTMLElement> | React.TouchEvent<HTMLElement>, index: number): void => {
203+
204+
this.startCycle();
205+
195206
if (this.props.onSelect) {
196207
this.props.onSelect(index);
197208
}
@@ -296,6 +307,9 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
296307
* Handles carousel button click.
297308
*/
298309
private onCarouselButtonClicked = (nextButtonClicked: boolean): void => {
310+
311+
this.startCycle();
312+
299313
const currentIndex = this.state.currentIndex;
300314

301315
let nextIndex = this.state.currentIndex;
@@ -406,4 +420,47 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
406420

407421
return result;
408422
}
423+
424+
private startCycle = (): void => {
425+
426+
const {
427+
interval,
428+
triggerPageEvent
429+
} = this.props;
430+
431+
if (this._intervalId) {
432+
if (triggerPageEvent) {
433+
clearTimeout(this._intervalId);
434+
}
435+
else {
436+
clearInterval(this._intervalId);
437+
}
438+
}
439+
440+
if (interval !== null) {
441+
const intervalValue = interval || 5000;
442+
if (!triggerPageEvent) {
443+
this._intervalId = setInterval(this.moveNext, intervalValue);
444+
}
445+
else {
446+
this._intervalId = setTimeout(this.moveNext, intervalValue);
447+
}
448+
}
449+
}
450+
451+
private moveNext = (): void => {
452+
if (!this.isCarouselButtonDisabled(true)) {
453+
this.onCarouselButtonClicked(true);
454+
}
455+
else {
456+
if (this._intervalId) {
457+
if (this.props.triggerPageEvent) {
458+
clearTimeout(this._intervalId);
459+
}
460+
else {
461+
clearInterval(this._intervalId);
462+
}
463+
}
464+
}
465+
}
409466
}

src/controls/carousel/ICarouselProps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ export interface ICarouselProps {
149149
slide?: boolean;
150150

151151
/**
152-
* The amount of time to delay between automatically cycling an item. If undefined, carousel will not automatically cycle.
152+
* The amount of time to delay between automatically cycling an item. If null, carousel will not automatically cycle.
153153
*/
154-
interval?: number;
154+
interval?: number | null;
155155

156156
/**
157157
* Specifies if set of slide position indicators is shown

0 commit comments

Comments
 (0)