Skip to content

Commit 331055c

Browse files
JonnyBurgersatya164
authored andcommitted
implement onTabLongPress event handler (facebook#66)
* implement onTabLongPress event handler * style fix * defaultHandler for long press
1 parent 8ecc08b commit 331055c

4 files changed

Lines changed: 44 additions & 13 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class TabNavigationView extends React.PureComponent<Props, State> {
5555
getTestID,
5656
renderIcon,
5757
onTabPress,
58+
onTabLongPress,
5859
} = this.props;
5960

6061
const { descriptors } = this.props;
@@ -74,6 +75,7 @@ class TabNavigationView extends React.PureComponent<Props, State> {
7475
navigation={navigation}
7576
screenProps={screenProps}
7677
onTabPress={onTabPress}
78+
onTabLongPress={onTabLongPress}
7779
getLabelText={getLabelText}
7880
getButtonComponent={getButtonComponent}
7981
getAccessibilityLabel={getAccessibilityLabel}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class MaterialTabView extends React.PureComponent<Props, State> {
109109
getTestID={this.props.getTestID}
110110
renderIcon={this._renderIcon}
111111
onTabPress={this.props.onTabPress}
112+
onTabLongPress={this.props.onTabLongPress}
112113
/>
113114
);
114115
};

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type InjectedProps = {
2323
renderScene: (props: { route: any }) => ?React.Node,
2424
onIndexChange: (index: number) => any,
2525
onTabPress: (props: { route: any }) => mixed,
26+
onTabLongPress: (props: { route: any }) => mixed,
2627
navigation: any,
2728
descriptors: any,
2829
screenProps?: any,
@@ -117,25 +118,27 @@ export default function createTabNavigator(TabView: React.ComponentType<*>) {
117118
return options.tabBarTestID;
118119
};
119120

121+
_makeDefaultHandler = ({ route, navigation }) => () => {
122+
if (navigation.isFocused()) {
123+
if (route.hasOwnProperty('index') && route.index > 0) {
124+
// If current tab has a nested navigator, pop to top
125+
navigation.dispatch(StackActions.popToTop({ key: route.key }));
126+
} else {
127+
navigation.emit('refocus');
128+
}
129+
} else {
130+
this._jumpTo(route.routeName);
131+
}
132+
};
133+
120134
_handleTabPress = ({ route }) => {
121135
this._isTabPress = true;
122136

123137
const { descriptors } = this.props;
124138
const descriptor = descriptors[route.key];
125139
const { navigation, options } = descriptor;
126140

127-
const defaultHandler = () => {
128-
if (navigation.isFocused()) {
129-
if (route.hasOwnProperty('index') && route.index > 0) {
130-
// If current tab has a nested navigator, pop to top
131-
navigation.dispatch(StackActions.popToTop({ key: route.key }));
132-
} else {
133-
navigation.emit('refocus');
134-
}
135-
} else {
136-
this._jumpTo(route.routeName);
137-
}
138-
};
141+
const defaultHandler = this._makeDefaultHandler({ route, navigation });
139142

140143
if (options.tabBarOnPress) {
141144
options.tabBarOnPress({ navigation, defaultHandler });
@@ -144,6 +147,20 @@ export default function createTabNavigator(TabView: React.ComponentType<*>) {
144147
}
145148
};
146149

150+
_handleTabLongPress = ({ route }) => {
151+
const { descriptors } = this.props;
152+
const descriptor = descriptors[route.key];
153+
const { navigation, options } = descriptor;
154+
155+
const defaultHandler = this._makeDefaultHandler({ route, navigation });
156+
157+
if (options.tabBarOnLongPress) {
158+
options.tabBarOnLongPress({ navigation, defaultHandler });
159+
} else {
160+
defaultHandler();
161+
}
162+
};
163+
147164
_handleIndexChange = index => {
148165
if (this._isTabPress) {
149166
this._isTabPress = false;
@@ -187,6 +204,7 @@ export default function createTabNavigator(TabView: React.ComponentType<*>) {
187204
renderScene={this._renderScene}
188205
onIndexChange={this._handleIndexChange}
189206
onTabPress={this._handleTabPress}
207+
onTabLongPress={this._handleTabLongPress}
190208
navigation={navigation}
191209
descriptors={descriptors}
192210
screenProps={screenProps}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Props = TabBarOptions & {
3232
descriptors: any,
3333
jumpTo: any,
3434
onTabPress: any,
35+
onTabLongPress: any,
3536
getAccessibilityLabel: (props: { route: any }) => string,
3637
getButtonComponent: ({ route: any }) => any,
3738
getLabelText: ({ route: any }) => any,
@@ -50,11 +51,18 @@ const DEFAULT_MAX_TAB_ITEM_WIDTH = 125;
5051

5152
class TouchableWithoutFeedbackWrapper extends React.Component<*> {
5253
render() {
53-
const { onPress, testID, accessibilityLabel, ...props } = this.props;
54+
const {
55+
onPress,
56+
onLongPress,
57+
testID,
58+
accessibilityLabel,
59+
...props
60+
} = this.props;
5461

5562
return (
5663
<TouchableWithoutFeedback
5764
onPress={onPress}
65+
onLongPress={onLongPress}
5866
testID={testID}
5967
hitSlop={{ left: 15, right: 15, top: 5, bottom: 5 }}
6068
accessibilityLabel={accessibilityLabel}
@@ -191,6 +199,7 @@ class TabBarBottom extends React.Component<Props> {
191199
activeBackgroundColor,
192200
inactiveBackgroundColor,
193201
onTabPress,
202+
onTabLongPress,
194203
safeAreaInset,
195204
style,
196205
tabStyle,
@@ -228,6 +237,7 @@ class TabBarBottom extends React.Component<Props> {
228237
<ButtonComponent
229238
key={route.key}
230239
onPress={() => onTabPress({ route })}
240+
onLongPress={() => onTabLongPress({ route })}
231241
testID={testID}
232242
accessibilityLabel={accessibilityLabel}
233243
style={[

0 commit comments

Comments
 (0)