Skip to content

Commit 491ee81

Browse files
Ashoatsatya164
authored andcommitted
fix: fix tabBarOnPress with MaterialTopTabs and fix isFocused (facebook#21)
* Hook up tabBarOnPress * Move onTabPress logic to createTabNavigator * Use old logic for determining focus state * Use navigation.isFocused() * Reorder jumpTo/onTabPress * [email protected]
1 parent 42e35f5 commit 491ee81

File tree

3 files changed

+8
-24
lines changed

3 files changed

+8
-24
lines changed

packages/bottom-tabs/src/navigators/createMaterialTopTabNavigator.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ class TabView extends React.PureComponent<Props> {
4545
return route.routeName;
4646
};
4747

48-
_getOnPress = (previousScene, { route }) => {
49-
const { descriptors } = this.props;
50-
const descriptor = descriptors[route.key];
51-
const options = descriptor.options;
52-
53-
return options.tabBarOnPress;
54-
};
55-
5648
_getTestIDProps = ({ route, focused }) => {
5749
const { descriptors } = this.props;
5850
const descriptor = descriptors[route.key];
@@ -107,6 +99,7 @@ class TabView extends React.PureComponent<Props> {
10799
getLabelText={this.props.getLabelText}
108100
getTestIDProps={this._getTestIDProps}
109101
renderIcon={this._renderIcon}
102+
onTabPress={this.props.onTabPress}
110103
/>
111104
);
112105
};

packages/bottom-tabs/src/utils/createTabNavigator.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,14 @@ export default function createTabNavigator(TabView: React.ComponentType<*>) {
7474
const { descriptors } = this.props;
7575
const descriptor = descriptors[route.key];
7676
const { navigation, options } = descriptor;
77+
const focused = navigation.isFocused();
7778

7879
if (options.tabBarOnPress) {
79-
options.tabBarOnPress({
80-
navigation,
81-
});
82-
} else {
83-
const isFocused =
84-
this.props.navigation.state.index ===
85-
this.props.navigation.state.routes.indexOf(route);
86-
87-
if (isFocused) {
88-
if (route.hasOwnProperty('index') && route.index > 0) {
89-
navigation.dispatch(StackActions.popToTop({ key: route.key }));
90-
} else {
91-
// TODO: do something to scroll to top
92-
}
93-
}
80+
options.tabBarOnPress({ navigation });
81+
} else if (focused && route.hasOwnProperty('index') && route.index > 0) {
82+
navigation.dispatch(StackActions.popToTop({ key: route.key }));
83+
} else if (focused) {
84+
// TODO: do something to scroll to top
9485
}
9586
};
9687

packages/bottom-tabs/src/views/BottomTabBar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ class TabBarBottom extends React.Component<Props> {
201201
<TouchableWithoutFeedback
202202
key={route.key}
203203
onPress={() => {
204-
jumpTo(route.key);
205204
onTabPress({ route });
205+
jumpTo(route.key);
206206
}}
207207
>
208208
<View

0 commit comments

Comments
 (0)