@@ -65,7 +65,7 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
65
65
const prevButtonDisabled = processing || this . isCarouselButtonDisabled ( false ) ;
66
66
const nextButtonDisabled = processing || this . isCarouselButtonDisabled ( true ) ;
67
67
68
- const element = this . getElementToDisplay ( ) ;
68
+ const element = this . getElementToDisplay ( currentIndex ) ;
69
69
70
70
return (
71
71
< div className = { this . getMergedStyles ( styles . container , containerStyles ) } >
@@ -87,7 +87,7 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
87
87
}
88
88
89
89
{
90
- ! processing && element
90
+ ! processing && this . renderSlide ( element )
91
91
}
92
92
{ this . getIndicatorsElement ( ) }
93
93
</ div >
@@ -104,12 +104,48 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
104
104
) ;
105
105
}
106
106
107
+ private renderSlide = ( element : JSX . Element ) : JSX . Element [ ] => {
108
+ const isAnimated = this . props . slide !== false && ! this . props . triggerPageEvent ;
109
+
110
+ const {
111
+ currentIndex,
112
+ previousIndex,
113
+ slideRight
114
+ } = this . state ;
115
+
116
+ if ( ! isAnimated || previousIndex === undefined ) {
117
+ return [ < div className = { styles . slideWrapper } >
118
+ { element }
119
+ </ div > ] ;
120
+ }
121
+
122
+ const previousElement = this . getElementToDisplay ( previousIndex ) ;
123
+
124
+ const result : JSX . Element [ ] = [ ] ;
125
+
126
+ result . push ( < div key = { currentIndex } className = { css ( styles . slideWrapper , {
127
+ [ styles . slideFromLeft ] : slideRight ,
128
+ [ styles . slideFromRight ] : ! slideRight
129
+ } ) } > { element } </ div > ) ;
130
+
131
+ if ( slideRight ) {
132
+ result . push ( < div key = { previousIndex } className = { css ( styles . slideWrapper , styles . slideRight , styles . right ) } > { previousElement } </ div > ) ;
133
+ }
134
+ else {
135
+ result . unshift ( < div key = { previousIndex } className = { css ( styles . slideWrapper , styles . slideLeft , styles . left ) } > { previousElement } </ div > ) ;
136
+ }
137
+
138
+ return result ;
139
+ }
140
+
107
141
private getIndicatorsElement = ( ) : JSX . Element | null => {
108
142
const {
109
143
indicators,
110
144
indicatorShape = CarouselIndicatorShape . rectangle ,
111
145
onRenderIndicator,
112
- triggerPageEvent
146
+ triggerPageEvent,
147
+ indicatorClassName,
148
+ indicatorStyle
113
149
} = this . props ;
114
150
115
151
const {
@@ -129,7 +165,10 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
129
165
}
130
166
else {
131
167
indicatorElements . push ( < li
132
- className = { i === currentIndex ? styles . active : undefined }
168
+ className = { css ( indicatorClassName , {
169
+ [ styles . active ] : i === currentIndex
170
+ } ) }
171
+ style = { indicatorStyle }
133
172
onClick = { e => this . onIndicatorClick ( e , i ) }
134
173
/> ) ;
135
174
}
@@ -157,8 +196,14 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
157
196
this . props . onSelect ( index ) ;
158
197
}
159
198
199
+ const {
200
+ currentIndex
201
+ } = this . state ;
202
+
160
203
this . setState ( {
161
- currentIndex : index
204
+ currentIndex : index ,
205
+ previousIndex : currentIndex ,
206
+ slideRight : index < currentIndex
162
207
} ) ;
163
208
}
164
209
@@ -258,36 +303,45 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
258
303
259
304
// Trigger parent control to update provided element
260
305
if ( this . props . triggerPageEvent ) {
261
- // Index validation needs to be done by the parent control specyfing canMove Next|Prev
262
- nextIndex = nextButtonClicked ? ( currentIndex + 1 ) : ( currentIndex - 1 ) ;
263
306
264
- // Trigger parent to provide new data
265
- this . props . triggerPageEvent ( nextIndex ) ;
266
- processingState = ProcessingState . processing ;
307
+ const canMove = nextButtonClicked ? this . props . canMoveNext !== false : this . props . canMovePrev !== false ;
267
308
268
- } else {
269
- nextIndex = this . getNextIndex ( nextButtonClicked ) ;
270
- const canMoveNext = this . props . canMoveNext != undefined ? this . props . canMoveNext : true ;
271
- const canMovePrev = this . props . canMovePrev != undefined ? this . props . canMovePrev : true ;
309
+ if ( canMove ) {
310
+ // Index validation needs to be done by the parent control specyfing canMove Next|Prev
311
+ nextIndex = nextButtonClicked ? ( currentIndex + 1 ) : ( currentIndex - 1 ) ;
272
312
273
- if ( canMoveNext && nextButtonClicked && this . props . onMoveNextClicked ) {
274
- this . props . onMoveNextClicked ( nextIndex ) ;
313
+ // Trigger parent to provide new data
314
+ this . props . triggerPageEvent ( nextIndex ) ;
315
+ processingState = ProcessingState . processing ;
275
316
}
276
- else if ( canMovePrev && ! nextButtonClicked && this . props . onMovePrevClicked ) {
277
- this . props . onMovePrevClicked ( nextIndex ) ;
317
+
318
+ } else {
319
+ nextIndex = this . getNextIndex ( nextButtonClicked ) ;
320
+ if ( nextIndex !== currentIndex ) {
321
+ if ( nextButtonClicked ) {
322
+ this . props . onMoveNextClicked ( nextIndex ) ;
323
+ }
324
+ else {
325
+ this . props . onMovePrevClicked ( nextIndex ) ;
326
+ }
278
327
}
279
328
280
329
processingState = ProcessingState . idle ;
281
330
}
282
331
283
- if ( this . props . onSelect ) {
284
- this . props . onSelect ( nextIndex ) ;
285
- }
286
332
287
- this . setState ( {
288
- currentIndex : nextIndex ,
289
- processingState
290
- } ) ;
333
+ if ( nextIndex !== currentIndex ) {
334
+ if ( this . props . onSelect ) {
335
+ this . props . onSelect ( nextIndex ) ;
336
+ }
337
+
338
+ this . setState ( {
339
+ currentIndex : nextIndex ,
340
+ previousIndex : currentIndex ,
341
+ slideRight : ! nextButtonClicked ,
342
+ processingState
343
+ } ) ;
344
+ }
291
345
}
292
346
293
347
/**
@@ -328,9 +382,8 @@ export class Carousel extends React.Component<ICarouselProps, ICarouselState> {
328
382
/**
329
383
* Returns current element to be displayed.
330
384
*/
331
- private getElementToDisplay = ( ) : JSX . Element => {
385
+ private getElementToDisplay = ( currentIndex : number ) : JSX . Element => {
332
386
const { element } = this . props ;
333
- const currentIndex = this . state . currentIndex ;
334
387
let result : JSX . Element = null ;
335
388
let arrayLen : number ;
336
389
0 commit comments