@@ -5,10 +5,18 @@ import ImageSliderNavigation, { ImageSliderNavDirection, ImageSliderNavStyle } f
5
5
import ImageSliderBullets from './ImageSliderBullets' ;
6
6
import useSlideIndex from './hooks/useSlideIndex' ;
7
7
8
+ type URLObject = {
9
+ url : string ;
10
+ } ;
11
+
12
+ const getURLValueFromImages = ( images : URLObject [ ] | string [ ] , index : number ) => {
13
+ return ( images as URLObject [ ] ) [ index ] ?. url || ( images as string [ ] ) [ index ] ;
14
+ } ;
15
+
8
16
export type SimpleImageSliderProps = {
9
17
width : number | string ;
10
18
height : number | string ;
11
- images : Array < { url : string } > ;
19
+ images : URLObject [ ] | string [ ] ;
12
20
style ?: React . CSSProperties ;
13
21
showNavs : boolean ;
14
22
showBullets : boolean ;
@@ -59,8 +67,8 @@ const SimpleImageSlider: React.FC<SimpleImageSliderProps> = ({
59
67
autoPlay,
60
68
autoPlayDelay : autoPlayDelay + slideDuration
61
69
} ) ;
62
- const [ currentSliderStyle , setCurrentSlideStyle ] = useState ( styles . getImageSlide ( images [ 0 ] . url , slideDuration , 0 , useGPURender ) ) ;
63
- const [ nextSliderStyle , setNextSliderStyle ] = useState ( styles . getImageSlide ( images [ 1 ] ?. url , slideDuration , 1 , useGPURender ) ) ;
70
+ const [ currentSliderStyle , setCurrentSlideStyle ] = useState ( styles . getImageSlide ( getURLValueFromImages ( images , 0 ) , slideDuration , 0 , useGPURender ) ) ;
71
+ const [ nextSliderStyle , setNextSliderStyle ] = useState ( styles . getImageSlide ( getURLValueFromImages ( images , 1 ) , slideDuration , 1 , useGPURender ) ) ;
64
72
const isSlidingRef = useRef ( false ) ;
65
73
66
74
const handleClick = useCallback (
@@ -100,8 +108,8 @@ const SimpleImageSlider: React.FC<SimpleImageSliderProps> = ({
100
108
return ;
101
109
}
102
110
103
- const currentUrl : string = images [ getNextLoopingIdx ( isRightDirection ? slideIdx - 1 : slideIdx + 1 ) ] . url ;
104
- const nextUrl : string = images [ slideIdx ] . url ;
111
+ const currentUrl : string = getURLValueFromImages ( images , getNextLoopingIdx ( isRightDirection ? slideIdx - 1 : slideIdx + 1 ) ) ;
112
+ const nextUrl : string = getURLValueFromImages ( images , slideIdx ) ;
105
113
const currentOffsetX : 1 | - 1 = isRightDirection ? - 1 : 1 ;
106
114
const nextReadyOffsetX : 1 | - 1 = isRightDirection ? 1 : - 1 ;
107
115
@@ -116,8 +124,8 @@ const SimpleImageSlider: React.FC<SimpleImageSliderProps> = ({
116
124
117
125
const handleSlideEnd = useCallback ( ( ) => {
118
126
isSlidingRef . current = false ;
119
- ImagePreLoader . load ( images [ slideIdx + 2 ] ?. url ) ;
120
- setCurrentSlideStyle ( styles . getImageSlide ( images [ slideIdx ] . url , 0 , 0 , useGPURender ) ) ;
127
+ ImagePreLoader . load ( getURLValueFromImages ( images , slideIdx + 2 ) ) ;
128
+ setCurrentSlideStyle ( styles . getImageSlide ( getURLValueFromImages ( images , slideIdx ) , 0 , 0 , useGPURender ) ) ;
121
129
onCompleteSlide ?.( slideIdx + 1 , images . length ) ;
122
130
} , [ onCompleteSlide , slideIdx ] ) ;
123
131
0 commit comments