@@ -5,7 +5,7 @@ initializeIcons();
55
66import * as React from "react" ;
77import styles from "./Carousel.module.scss" ;
8- import { ICarouselProps , ICarouselState , CarouselButtonsDisplay , CarouselButtonsLocation } from "." ;
8+ import { ICarouselProps , ICarouselState , CarouselButtonsDisplay , CarouselButtonsLocation , CarouselIndicatorShape } from "." ;
99import { css , ICssInput } from "@uifabric/utilities/lib" ;
1010import { ProcessingState } from "./ICarouselState" ;
1111import { Spinner } from "office-ui-fabric-react/lib/Spinner" ;
@@ -48,16 +48,23 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
4848
4949 public render ( ) : React . ReactElement < ICarouselProps > {
5050 const { currentIndex, processingState } = this . state ;
51- const { containerStyles, contentContainerStyles, containerButtonsStyles, prevButtonStyles, nextButtonStyles, loadingComponentContainerStyles } = this . props ;
51+ const {
52+ containerStyles,
53+ contentContainerStyles,
54+ containerButtonsStyles,
55+ prevButtonStyles,
56+ nextButtonStyles,
57+ loadingComponentContainerStyles,
58+ prevButtonIconName = 'ChevronLeft' ,
59+ nextButtonIconName = 'ChevronRight' ,
60+ loadingComponent = < Spinner />
61+ } = this . props ;
5262
53- const prevButtonIconName = this . props . prevButtonIconName ? this . props . prevButtonIconName : "ChevronLeft" ;
54- const nextButtonIconName = this . props . nextButtonIconName ? this . props . nextButtonIconName : "ChevronRight" ;
5563 const processing = processingState === ProcessingState . processing ;
5664
5765 const prevButtonDisabled = processing || this . isCarouselButtonDisabled ( false ) ;
5866 const nextButtonDisabled = processing || this . isCarouselButtonDisabled ( true ) ;
5967
60- const loadingComponent = this . props . loadingComponent ? this . props . loadingComponent : < Spinner /> ;
6168 const element = this . getElementToDisplay ( ) ;
6269
6370 return (
@@ -80,10 +87,9 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
8087 }
8188
8289 {
83- ! processing && element &&
84- element
90+ ! processing && element
8591 }
86-
92+ { this . getIndicatorsElement ( ) }
8793 </ div >
8894
8995 < div className = { this . getMergedStyles ( this . getButtonContainerStyles ( ) , containerButtonsStyles ) }
@@ -98,6 +104,64 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
98104 ) ;
99105 }
100106
107+ private getIndicatorsElement = ( ) : JSX . Element | null => {
108+ const {
109+ indicators,
110+ indicatorShape = CarouselIndicatorShape . rectangle ,
111+ onRenderIndicator,
112+ triggerPageEvent
113+ } = this . props ;
114+
115+ const {
116+ currentIndex = 0
117+ } = this . state ;
118+
119+ if ( indicators === false ) {
120+ return null ;
121+ }
122+
123+ const elementsCount = triggerPageEvent ? this . props . elementsCount : isArray ( this . props . element ) ? ( this . props . element as any [ ] ) . length : 1 ;
124+
125+ const indicatorElements : JSX . Element [ ] = [ ] ;
126+ for ( let i = 0 ; i < elementsCount ; i ++ ) {
127+ if ( onRenderIndicator ) {
128+ indicatorElements . push ( onRenderIndicator ( i , this . onIndicatorClick ) ) ;
129+ }
130+ else {
131+ indicatorElements . push ( < li
132+ className = { i === currentIndex ? styles . active : undefined }
133+ onClick = { e => this . onIndicatorClick ( e , i ) }
134+ /> ) ;
135+ }
136+ }
137+
138+ if ( onRenderIndicator ) {
139+ return < div className = { styles . indicators } >
140+ { indicatorElements }
141+ </ div > ;
142+ }
143+ else {
144+ return < ol className = { css ( {
145+ [ styles . indicators ] : true ,
146+ [ styles . circle ] : indicatorShape === CarouselIndicatorShape . circle ,
147+ [ styles . rectangle ] : indicatorShape === CarouselIndicatorShape . rectangle ,
148+ [ styles . square ] : indicatorShape === CarouselIndicatorShape . square
149+ } ) } >
150+ { indicatorElements }
151+ </ ol > ;
152+ }
153+ }
154+
155+ private onIndicatorClick = ( e : React . MouseEvent < HTMLElement > | React . TouchEvent < HTMLElement > , index : number ) : void => {
156+ if ( this . props . onSelect ) {
157+ this . props . onSelect ( index ) ;
158+ }
159+
160+ this . setState ( {
161+ currentIndex : index
162+ } ) ;
163+ }
164+
101165 /**
102166 * Return merged styles for Button containers.
103167 */
0 commit comments