File tree Expand file tree Collapse file tree 6 files changed +55
-4
lines changed
Expand file tree Collapse file tree 6 files changed +55
-4
lines changed Original file line number Diff line number Diff line change @@ -106,4 +106,11 @@ describe('BottomTabs', () => {
106106 await elementById ( TestIDs . POP_BTN ) . tap ( ) ;
107107 await expect ( elementById ( TestIDs . BOTTOM_TABS ) ) . toBeVisible ( ) ;
108108 } ) ;
109+
110+ it ( 'invoke bottomTabPressed event' , async ( ) => {
111+ await elementById ( TestIDs . THIRD_TAB_BAR_BTN ) . tap ( ) ;
112+ await expect ( elementByLabel ( 'BottomTabPressed' ) ) . toBeVisible ( ) ;
113+ await elementByLabel ( 'OK' ) . tap ( ) ;
114+ await expect ( elementByLabel ( 'First Tab' ) ) . toBeVisible ( ) ;
115+ } ) ;
109116} ) ;
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ import { VISIBLE_SCREEN_TEST_ID } from '../constants';
66import { LayoutStore } from '../Stores/LayoutStore' ;
77import { connect } from '../connect' ;
88import { TopBar } from './TopBar' ;
9+ import { events } from '../Stores/EventsStore' ;
10+ import _ from 'lodash' ;
911
1012export const ComponentScreen = connect (
1113 class extends Component < ComponentProps > {
@@ -34,7 +36,13 @@ export const ComponentScreen = connect(
3436 < Button
3537 testID = { bottomTabOptions ?. testID }
3638 title = { bottomTabOptions ?. text || '' }
37- onPress = { ( ) => LayoutStore . selectTabIndex ( this . props . layoutNode . getBottomTabs ( ) , i ) }
39+ onPress = { ( ) => {
40+ events . invokeBottomTabPressed ( {
41+ tabIndex : i ,
42+ } ) ;
43+ if ( _ . defaultTo ( bottomTabOptions ?. selectTabOnPress , true ) )
44+ LayoutStore . selectTabIndex ( this . props . layoutNode . getBottomTabs ( ) , i ) ;
45+ } }
3846 />
3947 < Text > { bottomTabOptions ?. badge } </ Text >
4048 </ View >
@@ -46,6 +54,9 @@ export const ComponentScreen = connect(
4654
4755 render ( ) {
4856 const Component = Navigation . mock . store . getWrappedComponent ( this . props . layoutNode . data . name ) ;
57+ if ( ! Component )
58+ throw new Error ( `${ this . props . layoutNode . data . name } has not been registered.` ) ;
59+
4960 return (
5061 < View testID = { this . isVisible ( ) ? VISIBLE_SCREEN_TEST_ID : undefined } >
5162 { this . props . layoutNode . getStack ( ) && (
Original file line number Diff line number Diff line change @@ -4,13 +4,15 @@ import {
44 ModalDismissedEvent ,
55} from '../../interfaces/ComponentEvents' ;
66import { ComponentDidAppearEvent , NavigationButtonPressedEvent } from '../../index' ;
7+ import { BottomTabPressedEvent } from '../../interfaces/Events' ;
78
89export const events = {
910 navigationButtonPressed : [ ( _event : NavigationButtonPressedEvent ) => { } ] ,
1011 componentWillAppear : [ ( _event : ComponentWillAppearEvent ) => { } ] ,
1112 componentDidAppear : [ ( _event : ComponentDidAppearEvent ) => { } ] ,
1213 componentDidDisappear : [ ( _event : ComponentDidDisappearEvent ) => { } ] ,
1314 modalDismissed : [ ( _event : ModalDismissedEvent ) => { } ] ,
15+ bottomTabPressed : [ ( _event : BottomTabPressedEvent ) => { } ] ,
1416 invokeComponentWillAppear : ( event : ComponentWillAppearEvent ) => {
1517 events . componentWillAppear &&
1618 events . componentWillAppear . forEach ( ( listener ) => {
@@ -41,4 +43,10 @@ export const events = {
4143 listener ( event ) ;
4244 } ) ;
4345 } ,
46+ invokeBottomTabPressed : ( event : BottomTabPressedEvent ) => {
47+ events . bottomTabPressed &&
48+ events . bottomTabPressed ?. forEach ( ( listener ) => {
49+ listener ( event ) ;
50+ } ) ;
51+ } ,
4452} ;
Original file line number Diff line number Diff line change @@ -73,10 +73,13 @@ export class NativeEventsReceiver {
7373 }
7474
7575 public registerBottomTabPressedListener (
76- _callback : ( data : BottomTabPressedEvent ) => void
76+ callback : ( data : BottomTabPressedEvent ) => void
7777 ) : EmitterSubscription {
78+ events . bottomTabPressed . push ( callback ) ;
7879 return {
79- remove : ( ) => { } ,
80+ remove : ( ) => {
81+ _ . remove ( events . bottomTabPressed , ( value ) => value === callback ) ;
82+ } ,
8083 } as EmitterSubscription ;
8184 }
8285
Original file line number Diff line number Diff line change @@ -38,6 +38,11 @@ export default class FirstBottomTabScreen extends React.Component<NavigationComp
3838
3939 dotVisible = true ;
4040 badgeVisible = true ;
41+ bottomTabPressedListener = Navigation . events ( ) . registerBottomTabPressedListener ( ( event ) => {
42+ if ( event . tabIndex == 2 ) {
43+ alert ( 'BottomTabPressed' ) ;
44+ }
45+ } ) ;
4146
4247 render ( ) {
4348 return (
@@ -68,6 +73,10 @@ export default class FirstBottomTabScreen extends React.Component<NavigationComp
6873 ) ;
6974 }
7075
76+ componentWillUnmount ( ) {
77+ this . bottomTabPressedListener . remove ( ) ;
78+ }
79+
7180 modifyBottomTabs = ( ) => {
7281 Navigation . mergeOptions ( this . props . componentId , {
7382 bottomTabs : {
Original file line number Diff line number Diff line change @@ -75,7 +75,7 @@ export default class LayoutsScreen extends NavigationComponent<NavigationCompone
7575
7676 stack = ( ) => Navigation . showModal ( stack ( Screens . Stack , 'StackId' ) ) ;
7777
78- bottomTabs = ( ) =>
78+ bottomTabs = ( ) => {
7979 Navigation . showModal ( {
8080 bottomTabs : {
8181 children : [
@@ -88,6 +88,18 @@ export default class LayoutsScreen extends NavigationComponent<NavigationCompone
8888 } ,
8989 'SecondTab'
9090 ) ,
91+ {
92+ component : {
93+ name : Screens . Pushed ,
94+ options : {
95+ bottomTab : {
96+ selectTabOnPress : false ,
97+ text : 'Tab 3' ,
98+ testID : testIDs . THIRD_TAB_BAR_BTN ,
99+ } ,
100+ } ,
101+ } ,
102+ } ,
91103 ] ,
92104 options : {
93105 bottomTabs : {
@@ -96,6 +108,7 @@ export default class LayoutsScreen extends NavigationComponent<NavigationCompone
96108 } ,
97109 } ,
98110 } ) ;
111+ } ;
99112
100113 sideMenu = ( ) =>
101114 Navigation . showModal ( {
You can’t perform that action at this time.
0 commit comments