@@ -9,17 +9,14 @@ import {
99 useEffect ,
1010 useMemo ,
1111} from 'react' ;
12- import { AccessibilityActionEvent , FlatList , ViewStyle , ViewToken } from 'react-native' ;
12+ import { FlatList , ViewStyle , ViewToken } from 'react-native' ;
1313
14- import { Box } from '../../' ;
1514import CarouselContext from './Carousel.context' ;
1615import { CarouselItemProps , CarouselItemsProps , CarouselRef } from './Carousel.props' ;
1716
18- const clampToRange = ( number : number , min : number , max : number ) =>
19- Math . min ( Math . max ( number , min ) , max ) ;
20-
2117export const CarouselItems = forwardRef ( function CarouselItems (
2218 {
19+ centered = false ,
2320 children,
2421 enabled = true ,
2522 inactiveItemOpacity = 1 ,
@@ -32,20 +29,21 @@ export const CarouselItems = forwardRef(function CarouselItems(
3229 } : CarouselItemsProps ,
3330 ref ?: ForwardedRef < CarouselRef > | null
3431) {
35- const { activeIndex = 0 , setActiveIndex, setNumItems } = useContext ( CarouselContext ) ;
32+ const {
33+ activeIndex = 0 ,
34+ setActiveIndex,
35+ setNumItems,
36+ } = useContext ( CarouselContext ) ;
3637
3738 // Ensure children is always converted to an array
3839 const items : Array < ReactElement < CarouselItemProps > > = useMemo (
3940 ( ) => Children . map ( children , child => child ) || [ ] ,
4041 [ children ]
4142 ) ;
42-
43+
4344 const innerMargin : number = width - ( itemWidth || width ) ;
44- const containerStyles : ViewStyle = {
45- width,
46- } ;
47- const carouselStyles : ViewStyle = {
48- marginHorizontal : innerMargin / 2 ,
45+ const styles : ViewStyle = {
46+ marginHorizontal : centered ? innerMargin / 2 : 0 ,
4947 overflow : showOverflow ? 'visible' : 'hidden' ,
5048 } ;
5149
@@ -67,59 +65,39 @@ export const CarouselItems = forwardRef(function CarouselItems(
6765 [ onSnapToItem , setActiveIndex ]
6866 ) ;
6967
70- const handleAccessibilityAction = ( { nativeEvent } : AccessibilityActionEvent ) => {
71- const value = nativeEvent . actionName === 'increment' ? 1 : - 1 ;
72- const index = clampToRange (
73- activeIndex + value ,
74- 0 ,
75- items && items . length ? items . length - 1 : 0
76- ) ;
77-
78- if ( ref && typeof ref !== 'function' && ref ?. current ) {
79- ref . current . scrollToIndex ( { index } ) ;
80- }
81- } ;
82-
8368 return (
84- < Box style = { [ containerStyles , style ] } >
85- < FlatList < ReactElement < CarouselItemProps > >
86- accessibilityActions = { [ { name : 'increment' } , { name : 'decrement' } ] }
87- accessibilityLabel = "Carousel"
88- accessibilityRole = "adjustable"
89- accessible = { true }
90- bounces = { false } // Prevents bouncing at the start and end of carousel scrolling (iOS only)
91- data = { items }
92- decelerationRate = "fast"
93- getItemLayout = { ( _ , index ) => ( {
94- length : itemWidth || width ,
95- offset : ( itemWidth || width ) * index ,
96- index,
97- } ) }
98- horizontal
99- initialScrollIndex = { activeIndex }
100- pagingEnabled
101- onAccessibilityAction = { handleAccessibilityAction }
102- onViewableItemsChanged = { handleViewableItemsChanged }
103- overScrollMode = "never" // Prevents stretching of first and last items when reaching each end of the carousel (Android only)
104- ref = { ref }
105- removeClippedSubviews = { ! showOverflow }
106- renderItem = { ( { index, item } ) =>
107- cloneElement ( item , {
108- active : index === activeIndex ,
109- inactiveOpacity : inactiveItemOpacity ,
110- key : item ?. key || item . props ?. id || index ,
111- width : itemWidth || width ,
112- } )
113- }
114- scrollEnabled = { enabled }
115- showsHorizontalScrollIndicator = { false }
116- snapToInterval = { itemWidth || width }
117- snapToAlignment = "center"
118- style = { carouselStyles }
119- viewabilityConfig = { { itemVisiblePercentThreshold : 51 } }
120- { ...props }
121- />
122- </ Box >
69+ < FlatList < ReactElement < CarouselItemProps > >
70+ bounces = { false } // Prevents bouncing at the start and end of carousel scrolling (iOS only)
71+ data = { items }
72+ decelerationRate = "fast"
73+ getItemLayout = { ( _ , index ) => ( {
74+ length : itemWidth || width ,
75+ offset : ( itemWidth || width ) * index ,
76+ index,
77+ } ) }
78+ horizontal
79+ initialScrollIndex = { activeIndex }
80+ pagingEnabled
81+ onViewableItemsChanged = { handleViewableItemsChanged }
82+ overScrollMode = "never" // Prevents stretching of first and last items when reaching each end of the carousel (Android only)
83+ ref = { ref }
84+ removeClippedSubviews = { ! showOverflow }
85+ renderItem = { ( { index, item } ) =>
86+ cloneElement ( item , {
87+ active : index === activeIndex ,
88+ inactiveOpacity : inactiveItemOpacity ,
89+ key : item ?. key || item . props ?. id || index ,
90+ width : itemWidth || width ,
91+ } )
92+ }
93+ scrollEnabled = { enabled }
94+ showsHorizontalScrollIndicator = { false }
95+ snapToInterval = { itemWidth || width }
96+ snapToAlignment = "center"
97+ style = { [ styles , style ] }
98+ viewabilityConfig = { { itemVisiblePercentThreshold : 51 } }
99+ { ...props }
100+ />
123101 ) ;
124102} ) ;
125103
0 commit comments