Skip to content

Commit 2e4b00a

Browse files
committed
add paging for TabController.TabBar in centerSelected mode
1 parent 82efda5 commit 2e4b00a

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/components/tabController/TabBar.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class TabBar extends PureComponent {
103103
*/
104104
centerSelected: PropTypes.bool,
105105
/**
106-
* (Experimental) Pass to optimize loading time by measuring tab bar items text
106+
* (Experimental) Pass to optimize loading time by measuring tab bar items text
107107
* instead of waiting for onLayout
108108
*/
109109
optimize: PropTypes.bool
@@ -171,13 +171,27 @@ class TabBar extends PureComponent {
171171
return centerSelected ? this.containerWidth / 2 - guesstimateCenterValue : 0;
172172
}
173173

174+
getSnapBreakpoints() {
175+
const {centerSelected} = this.props;
176+
const {itemsWidths, itemsOffsets} = this.state;
177+
178+
if (itemsWidths && centerSelected) {
179+
return _.times(itemsWidths.length, (index) => {
180+
const screenCenter = this.containerWidth / 2;
181+
const itemOffset = itemsOffsets[index];
182+
const itemWidth = itemsWidths[index];
183+
return itemOffset - screenCenter + itemWidth / 2;
184+
});
185+
}
186+
}
187+
174188
measureItems = async () => {
175189
const {labelStyle} = this.props;
176190
const measuring = _.map(this.props.items, (item) => {
177191
return Typography.measureTextSize(item.label, labelStyle);
178192
});
179193
const results = await Promise.all(measuring);
180-
const widths = _.map(results, item => item.width + Spacings.s4 * 2);
194+
const widths = _.map(results, (item) => item.width + Spacings.s4 * 2);
181195
const offsets = [];
182196
_.forEach(widths, (width, index) => {
183197
if (index === 0) {
@@ -190,7 +204,7 @@ class TabBar extends PureComponent {
190204
this._itemsOffsets = offsets;
191205
// TODO: consider saving this setState and ride registerTabItems setState
192206
this.setItemsLayouts();
193-
}
207+
};
194208

195209
registerTabItems() {
196210
const {registerTabItems} = this.context;
@@ -248,12 +262,12 @@ class TabBar extends PureComponent {
248262

249263
setItemsLayouts = () => {
250264
const {selectedIndex} = this.context;
251-
const itemsOffsets = _.map(this._itemsOffsets, offset => offset + INDICATOR_INSET);
265+
const itemsOffsets = _.map(this._itemsOffsets, (offset) => offset + INDICATOR_INSET);
252266
const itemsWidths = _.map(this._itemsWidths, (width) => width - INDICATOR_INSET * 2);
253267

254268
this.setState({itemsWidths, itemsOffsets});
255269
this.focusSelected([selectedIndex], false);
256-
}
270+
};
257271

258272
onScroll = ({nativeEvent: {contentOffset}}) => {
259273
this.tabBarScrollOffset = contentOffset.x;
@@ -375,8 +389,10 @@ class TabBar extends PureComponent {
375389
scrollEnabled={scrollEnabled}
376390
onContentSizeChange={this.onContentSizeChange}
377391
onScroll={this.onScroll}
378-
scrollEventThrottle={100}
392+
scrollEventThrottle={16}
379393
testID={testID}
394+
snapToOffsets={this.getSnapBreakpoints()}
395+
decelerationRate={'fast'}
380396
>
381397
<View style={[styles.tabBar, height && {height}, {paddingHorizontal: this.centerOffset}]}>
382398
{this.renderTabBarItems()}

0 commit comments

Comments
 (0)