Skip to content

Commit 914c920

Browse files
committed
allow passing custom page width to TabController carousel
1 parent 6e051d2 commit 914c920

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/components/tabController/PageCarousel.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class PageCarousel extends PureComponent {
3232
};
3333

3434
scrollToPage = (pageIndex, animated) => {
35+
const {pageWidth} = this.context;
3536
const node = _.invoke(this.carousel, 'current.getNode');
3637
if (node) {
37-
node.scrollTo({x: pageIndex * Constants.screenWidth, animated: false});
38+
node.scrollTo({x: pageIndex * pageWidth, animated: false});
3839
}
3940
};
4041

@@ -44,7 +45,7 @@ class PageCarousel extends PureComponent {
4445
};
4546

4647
render() {
47-
const {selectedIndex} = this.context;
48+
const {selectedIndex, pageWidth} = this.context;
4849
return (
4950
<>
5051
<Animated.ScrollView
@@ -55,7 +56,7 @@ class PageCarousel extends PureComponent {
5556
showsHorizontalScrollIndicator={false}
5657
onScroll={this.onScroll}
5758
scrollEventThrottle={16}
58-
contentOffset={{x: selectedIndex * Constants.screenWidth}} // iOS only
59+
contentOffset={{x: selectedIndex * pageWidth}} // iOS only
5960
/>
6061

6162
<Code>{this.renderCodeBlock}</Code>

src/components/tabController/TabPage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default class TabPage extends PureComponent {
5252
_pageStyle = [
5353
{opacity: this._opacity},
5454
this.context.asCarousel ? styles.carouselPage : styles.page,
55+
this.context.asCarousel ? {width: this.context.pageWidth} : undefined,
5556
{zIndex: this._zIndex}
5657
];
5758

src/components/tabController/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ class TabController extends Component {
5959
/**
6060
* When using TabController.PageCarousel this should be turned on
6161
*/
62-
asCarousel: PropTypes.bool
62+
asCarousel: PropTypes.bool,
63+
/**
64+
* Pass for custom carousel page width
65+
*/
66+
carouselPageWidth: PropTypes.number
6367
};
6468

6569
static defaultProps = {
@@ -74,16 +78,21 @@ class TabController extends Component {
7478
selectedIndex: this.props.selectedIndex,
7579
asCarousel: this.props.asCarousel,
7680
itemStates: [],
81+
pageWidth: this.pageWidth,
7782
// animated values
7883
targetPage: new Value(this.props.selectedIndex),
7984
currentPage: new Value(this.props.selectedIndex),
80-
carouselOffset: new Value(this.props.selectedIndex * Math.round(Constants.screenWidth)),
85+
carouselOffset: new Value(this.props.selectedIndex * Math.round(this.pageWidth)),
8186
// // callbacks
8287
registerTabItems: this.registerTabItems,
8388
onChangeIndex: this.props.onChangeIndex
8489
};
8590
}
8691

92+
get pageWidth() {
93+
return this.props.carouselPageWidth || Constants.screenWidth;
94+
}
95+
8796
registerTabItems = (tabItemsCount, ignoredItems) => {
8897
const itemStates = _.times(tabItemsCount, () => new Value(State.UNDETERMINED));
8998
this.setState({itemStates, ignoredItems});
@@ -127,11 +136,11 @@ class TabController extends Component {
127136

128137
/* Page change by Carousel scroll */
129138
onChange(carouselOffset, [
130-
set(isScrolling, lessThan(round(abs(diff(carouselOffset))), round(Constants.screenWidth))),
139+
set(isScrolling, lessThan(round(abs(diff(carouselOffset))), round(this.pageWidth))),
131140
cond(and(not(isAnimating)), [
132141
set(currentPage,
133142
interpolate(round(carouselOffset), {
134-
inputRange: itemStates.map((v, i) => Math.round(i * Constants.screenWidth)),
143+
inputRange: itemStates.map((v, i) => Math.round(i * this.pageWidth)),
135144
outputRange: itemStates.map((v, i) => i)
136145
})),
137146
set(toPage, currentPage)

0 commit comments

Comments
 (0)