Skip to content

Commit

Permalink
feat: add defaultHandler argument to tabBarOnPress. fixes facebook#22
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Aug 18, 2019
1 parent 47b709a commit 267e9ec
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions packages/bottom-tabs/src/utils/createTabNavigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,47 @@ export default function createTabNavigator(TabView: React.ComponentType<*>) {
return route.routeName;
};

_handleOnTabPress = ({ route }) => {
_handleTabPress = ({ route }) => {
this._isTabPress = true;

const { descriptors } = this.props;
const descriptor = descriptors[route.key];
const { navigation, options } = descriptor;
const focused = navigation.isFocused();

const defaultHandler = () => {
if (navigation.isFocused()) {
if (route.hasOwnProperty('index') && route.index > 0) {
// If current tab has a nested navigator, pop to top
navigation.dispatch(StackActions.popToTop({ key: route.key }));
} else {
// TODO: do something to scroll to top
}
} else {
this._jumpTo(route.routeName);
}
};

if (options.tabBarOnPress) {
options.tabBarOnPress({ navigation });
} else if (focused && route.hasOwnProperty('index') && route.index > 0) {
navigation.dispatch(StackActions.popToTop({ key: route.key }));
} else if (focused) {
// TODO: do something to scroll to top
options.tabBarOnPress({ navigation, defaultHandler });
} else {
defaultHandler();
}
};

_handleIndexChange = index => {
const { navigation } = this.props;
navigation.dispatch(
NavigationActions.navigate({
routeName: navigation.state.routes[index].routeName,
})
);
if (this._isTabPress) {
this._isTabPress = false;
return;
}

this._jumpTo(this.props.navigation.state.routes[index].routeName);
};

_jumpTo = routeName =>
this.props.navigation.dispatch(NavigationActions.navigate({ routeName }));

_isTabPress: boolean = false;

render() {
const { descriptors, navigation, screenProps } = this.props;
const { state } = navigation;
Expand All @@ -111,7 +128,7 @@ export default function createTabNavigator(TabView: React.ComponentType<*>) {
renderIcon={this._renderIcon}
renderScene={this._renderScene}
onIndexChange={this._handleIndexChange}
onTabPress={this._handleOnTabPress}
onTabPress={this._handleTabPress}
navigation={navigation}
descriptors={descriptors}
screenProps={screenProps}
Expand Down

0 comments on commit 267e9ec

Please sign in to comment.