mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-02-10 22:47:02 +08:00
Merge pull request #24 from react-navigation/@satya164/on-press
feat: add defaultHandler argument to tabBarOnPress. fixes #22
This commit is contained in:
@@ -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;
|
||||
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user