@@ -4,67 +4,37 @@ import Tabs from 'react-native-tabs';
44
55import Actions from './Actions' ;
66export default class TabBar extends React . Component {
7- constructor ( props ) {
8- super ( props ) ;
9- }
107 onSelect ( el ) {
118 if ( ! Actions [ el . props . name ] ) {
129 throw new Error ( "No action is defined for name=" + el . props . name + " actions:" + JSON . stringify ( Object . keys ( Actions ) ) ) ;
1310 }
14- if ( el . props . selected && Actions [ el . props . defaultRoute ] ) {
15- Actions [ el . props . defaultRoute ] ( { hideTabBar : el . props . hideTabBar } ) ;
16- } else {
17- Actions [ el . props . name ] ( { hideTabBar : el . props . hideTabBar } ) ;
18- }
19- InteractionManager . runAfterInteractions ( ( ) =>
20- this . setState ( { hideTabBar : el . props . hideTabBar } ) ) ;
21- return { selected : true } ;
22- }
23- getChildrenState ( selectedRoute ) {
24- var self = this ;
25- let selected = false ;
26- var children = [ ] ;
27- React . Children . forEach ( this . props . children , function ( el , index ) {
28- const schema = self . props . router && self . props . router . schemas [ el . props . schema ] ? self . props . router . schemas [ el . props . schema ] : { } ;
29- let props = { ...schema , ...el . props } ;
30- if ( ! el . props . name )
31- console . error ( "No name is defined for element" ) ;
32- if ( selectedRoute ) {
33- if ( selectedRoute == el . props . name ) {
34- props . selected = true ;
35- } else {
36- props . selected = false ;
37- }
38- }
39-
40- var Icon = props . icon || console . error ( "No icon class is defined for " + el . name ) ;
41- children . push ( < Icon key = { el . props . name } { ...props } /> ) ;
42- if ( props . selected || index === 0 ) {
43- selected = el ;
44- }
45- } ) ;
46- return { children, hideTabBar : selected . props . hideTabBar } ;
47- }
48- componentWillMount ( ) {
49- if ( ! this . props . children ) {
50- return ;
51- }
52- this . state = this . getChildrenState ( this . props . selected ) ;
53-
54- }
11+ Actions [ el . props . name ] ( { hideTabBar : el . props . hideTabBar } ) ;
5512
56- componentWillReceiveProps ( { selected} ) {
57- //console.log("TABBAR "+selected);
58- InteractionManager . runAfterInteractions ( ( ) =>
59- this . setState ( this . getChildrenState ( selected ) ) ) ;
6013 }
6114 render ( ) {
62- if ( this . state . hideTabBar ) {
15+ if ( this . props . hideTabBar ) {
6316 return < View />
6417 }
18+ // choose initial route
19+ let selected = this . props . selected
20+ if ( ! selected ) {
21+ React . Children . forEach ( this . props . children , el => {
22+ if ( ! selected || el . props . initial ) {
23+ selected = el . props . name ;
24+ }
25+ } ) ;
26+ }
6527 return (
66- < Tabs style = { [ { backgroundColor :'white' } , this . props . tabBarStyle ] } onSelect = { this . onSelect . bind ( this ) } { ...this . props } >
67- { this . state . children }
28+ < Tabs style = { [ { backgroundColor :'white' } , this . props . tabBarStyle ] } onSelect = { this . onSelect . bind ( this ) } { ...this . props } selected = { selected } >
29+ { React . Children . map ( this . props . children , el => {
30+ const schema = this . props . router && this . props . router . schemas [ el . props . schema ] ? this . props . router . schemas [ el . props . schema ] : { } ;
31+ let props = { ...schema , ...el . props } ;
32+ if ( ! el . props . name )
33+ console . error ( "No name is defined for element" ) ;
34+
35+ var Icon = props . icon || console . error ( "No icon class is defined for " + el . name ) ;
36+ return < Icon key = { el . props . name } { ...props } /> ;
37+ } ) }
6838 </ Tabs >
6939 ) ;
7040 }
0 commit comments