import _ from 'lodash'; import React, {Component} from 'react'; import {StyleSheet, ScrollView} from 'react-native'; import {Constants, Spacings, View, Text, Carousel, Image, Colors} from 'react-native-ui-lib'; import {renderBooleanOption, renderSliderOption} from '../ExampleScreenPresenter'; const INITIAL_PAGE = 2; const IMAGES = [ 'https://images.pexels.com/photos/2529159/pexels-photo-2529159.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500', 'https://images.pexels.com/photos/2529146/pexels-photo-2529146.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500', 'https://images.pexels.com/photos/2529158/pexels-photo-2529158.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500' ]; const BACKGROUND_COLORS = [ Colors.red50, Colors.yellow20, Colors.purple50, Colors.green50, Colors.cyan50, Colors.purple20, Colors.blue60, Colors.red10, Colors.green20, Colors.purple60 ]; interface Props {} interface State { orientation: typeof Constants.orientation; width: number; limitShownPages: boolean; numberOfPagesShown: number; currentPage: number; autoplay: boolean; } class CarouselScreen extends Component { carousel = React.createRef(); private dimensionsChangeListener: any; constructor(props: Props) { super(props); this.state = { orientation: Constants.orientation, width: this.getWidth(), limitShownPages: false, numberOfPagesShown: 7, currentPage: INITIAL_PAGE, autoplay: true }; } componentDidMount() { this.dimensionsChangeListener = Constants.addDimensionsEventListener(this.onOrientationChange); } componentWillUnmount() { Constants.removeDimensionsEventListener(this.dimensionsChangeListener || this.onOrientationChange); } onOrientationChange = () => { if (this.state.orientation !== Constants.orientation) { this.setState({ orientation: Constants.orientation, width: this.getWidth() }); } }; getWidth = () => { return Constants.windowWidth - Spacings.s5 * 2; }; onChangePage = (currentPage: number, _: any) => { this.setState({currentPage}); }; onPagePress = (index: number) => { if (this.carousel && this.carousel.current) { this.carousel.current.goToPage(index, true); } }; render() { const {limitShownPages, numberOfPagesShown, autoplay, width} = this.state; return ( Carousel {renderBooleanOption.call(this, 'Limit number of pages shown in page control', 'limitShownPages')} {renderBooleanOption.call(this, 'autoplay', 'autoplay')} {renderSliderOption.call(this, 'Number of pages shown', 'numberOfPagesShown', { min: 5, max: 10, step: 1, initial: 7 })} {_.map([...Array(numberOfPagesShown)], (_item, index) => ( CARD {index} ))} {this.state.currentPage} Looping Carousel {IMAGES.map((image, i) => { return ( ); })} ); } } // @ts-ignore const Page = ({children, style, ...others}) => { return ( {children} ); }; const styles = StyleSheet.create({ picker: { marginHorizontal: 20 }, page: { flex: 1, borderWidth: 1, borderRadius: 8 }, loopCarousel: { position: 'absolute', bottom: 15, left: 10 } }); export default CarouselScreen;