Skip to content

Commit d596c13

Browse files
committed
fix issue with intialPage in Carousel not working
1 parent 95b2de3 commit d596c13

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

demo/src/screens/componentScreens/CarouselScreen.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {StyleSheet} from 'react-native';
33
import {Constants, View, Text, Carousel, Image, Card} from 'react-native-ui-lib'; // eslint-disable-line
44
import _ from 'lodash';
55

6-
const INITIAL_PAGE = 0;
6+
const INITIAL_PAGE = 2;
77
const WIDTH = Constants.screenWidth - 120;
88

99
const IMAGES = [
@@ -39,7 +39,7 @@ class CarouselScreen extends Component {
3939
onChangePage={index => this.onChangePage(index)}
4040
pageWidth={WIDTH}
4141
// itemSpacings={20}
42-
// initialPage={INITIAL_PAGE}
42+
initialPage={INITIAL_PAGE}
4343
containerStyle={{height: 160/* , flex: 1 */}}
4444
pageControlPosition={'under'}
4545
pageControlProps={{onPagePress: this.onPagePress}}
@@ -73,7 +73,7 @@ class CarouselScreen extends Component {
7373
</View>
7474

7575
<View padding-20>
76-
<Carousel test migrate containerStyle={{height: 160}}>
76+
<Carousel migrate containerStyle={{height: 160}} initialPage={INITIAL_PAGE} loop>
7777
{_.map(IMAGES, (image, index) => {
7878
return (
7979
<View key={index} flex padding-10 bottom>

src/components/carousel/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,28 @@ export default class Carousel extends BaseComponent {
156156
const snapToOffsets = _.times(presenter.getChildrenLength(this.props), index => initialBreak + index * pageWidth);
157157
return snapToOffsets;
158158
}
159-
}
159+
};
160160

161161
shouldUsePageWidth() {
162162
const {loop, pageWidth} = this.props;
163163
return !loop && pageWidth;
164164
}
165165

166-
onContainerLayout = ({nativeEvent: {layout: {width: containerWidth}}}) => {
166+
onContainerLayout = ({
167+
nativeEvent: {
168+
layout: {width: containerWidth}
169+
}
170+
}) => {
171+
const {initialPage, pageWidth} = this.props;
167172
const update = {containerWidth};
168-
if (!this.props.pageWidth) {
173+
if (!pageWidth) {
169174
update.pageWidth = containerWidth;
175+
update.initialOffset = {
176+
x: presenter.calcOffset(this.props, {currentPage: initialPage, pageWidth: containerWidth})
177+
};
170178
}
171179
this.setState(update);
172-
}
180+
};
173181

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

0 commit comments

Comments
 (0)