Skip to content

Commit

Permalink
fix issue with intialPage in Carousel not working
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanshar committed Nov 18, 2019
1 parent 95b2de3 commit d596c13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 3 additions & 3 deletions demo/src/screens/componentScreens/CarouselScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {StyleSheet} from 'react-native';
import {Constants, View, Text, Carousel, Image, Card} from 'react-native-ui-lib'; // eslint-disable-line
import _ from 'lodash';

const INITIAL_PAGE = 0;
const INITIAL_PAGE = 2;
const WIDTH = Constants.screenWidth - 120;

const IMAGES = [
Expand Down Expand Up @@ -39,7 +39,7 @@ class CarouselScreen extends Component {
onChangePage={index => this.onChangePage(index)}
pageWidth={WIDTH}
// itemSpacings={20}
// initialPage={INITIAL_PAGE}
initialPage={INITIAL_PAGE}
containerStyle={{height: 160/* , flex: 1 */}}
pageControlPosition={'under'}
pageControlProps={{onPagePress: this.onPagePress}}
Expand Down Expand Up @@ -73,7 +73,7 @@ class CarouselScreen extends Component {
</View>

<View padding-20>
<Carousel test migrate containerStyle={{height: 160}}>
<Carousel migrate containerStyle={{height: 160}} initialPage={INITIAL_PAGE} loop>
{_.map(IMAGES, (image, index) => {
return (
<View key={index} flex padding-10 bottom>
Expand Down
16 changes: 12 additions & 4 deletions src/components/carousel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,28 @@ export default class Carousel extends BaseComponent {
const snapToOffsets = _.times(presenter.getChildrenLength(this.props), index => initialBreak + index * pageWidth);
return snapToOffsets;
}
}
};

shouldUsePageWidth() {
const {loop, pageWidth} = this.props;
return !loop && pageWidth;
}

onContainerLayout = ({nativeEvent: {layout: {width: containerWidth}}}) => {
onContainerLayout = ({
nativeEvent: {
layout: {width: containerWidth}
}
}) => {
const {initialPage, pageWidth} = this.props;
const update = {containerWidth};
if (!this.props.pageWidth) {
if (!pageWidth) {
update.pageWidth = containerWidth;
update.initialOffset = {
x: presenter.calcOffset(this.props, {currentPage: initialPage, pageWidth: containerWidth})
};
}
this.setState(update);
}
};

onContentSizeChange = () => {
// this is to handle initial scroll position (content offset)
Expand Down

0 comments on commit d596c13

Please sign in to comment.