Skip to content

Commit 4e8eca3

Browse files
committed
Fix orientation issues in TabController
1 parent 4389ee7 commit 4e8eca3

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/components/tabController/PageCarousel.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ class PageCarousel extends PureComponent {
4040
};
4141

4242
renderCodeBlock = () => {
43-
const {targetPage} = this.context;
44-
return block([Animated.onChange(targetPage, [call([targetPage], this.onTabChange)])]);
43+
const {targetPage, containerWidth} = this.context;
44+
return block([
45+
Animated.onChange(targetPage, [call([targetPage], this.onTabChange)]),
46+
Animated.onChange(containerWidth, [call([targetPage], this.onTabChange)])
47+
]);
4548
};
4649

4750
render() {

src/components/tabController/TabPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +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,
55+
this.context.asCarousel ? {width: this.context.containerWidth} : undefined,
5656
{zIndex: this._zIndex}
5757
];
5858

src/components/tabController/index.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ const {
3232
block,
3333
onChange,
3434
interpolate,
35-
round
35+
round,
36+
multiply
3637
} = Reanimated;
3738

3839
/**
@@ -82,12 +83,28 @@ class TabController extends Component {
8283
targetPage: new Value(this.props.selectedIndex),
8384
currentPage: new Value(this.props.selectedIndex),
8485
carouselOffset: new Value(this.props.selectedIndex * Math.round(this.pageWidth)),
86+
containerWidth: new Value(this.pageWidth),
8587
// // callbacks
8688
registerTabItems: this.registerTabItems,
8789
onChangeIndex: this.props.onChangeIndex
8890
};
8991
}
9092

93+
static getDerivedStateFromProps(nextProps, prevState) {
94+
if (!_.isUndefined(nextProps.carouselPageWidth) && nextProps.carouselPageWidth !== prevState.pageWidth) {
95+
return {
96+
pageWidth: nextProps.carouselPageWidth
97+
};
98+
}
99+
return null;
100+
}
101+
102+
componentDidUpdate(prevProps, prevState) {
103+
if (prevState.pageWidth !== this.state.pageWidth) {
104+
this.state.containerWidth.setValue(this.state.pageWidth);
105+
}
106+
}
107+
91108
get pageWidth() {
92109
return this.props.carouselPageWidth || Constants.screenWidth;
93110
}
@@ -102,7 +119,7 @@ class TabController extends Component {
102119
};
103120

104121
renderCodeBlock = () => {
105-
const {itemStates, ignoredItems, currentPage, targetPage, carouselOffset} = this.state;
122+
const {itemStates, ignoredItems, currentPage, targetPage, carouselOffset, containerWidth} = this.state;
106123
const {selectedIndex} = this.props;
107124
const clock = new Clock();
108125
const fromPage = new Value(selectedIndex);
@@ -135,11 +152,11 @@ class TabController extends Component {
135152

136153
/* Page change by Carousel scroll */
137154
onChange(carouselOffset, [
138-
set(isScrolling, lessThan(round(abs(diff(carouselOffset))), round(this.pageWidth))),
155+
set(isScrolling, lessThan(round(abs(diff(carouselOffset))), round(containerWidth))),
139156
cond(and(not(isAnimating)), [
140157
set(currentPage,
141158
interpolate(round(carouselOffset), {
142-
inputRange: itemStates.map((v, i) => Math.round(i * this.pageWidth)),
159+
inputRange: itemStates.map((v, i) => round(multiply(i, containerWidth))),
143160
outputRange: itemStates.map((v, i) => i)
144161
})),
145162
set(toPage, currentPage)

0 commit comments

Comments
 (0)