@@ -95,51 +95,43 @@ class Appbar extends React.Component<Props> {
9595 : ! color ( backgroundColor ) . light ( ) ;
9696 }
9797
98- const childrenArray = React . Children . toArray ( children ) ;
99-
100- let isAppbarContentFound = false ;
101- let leftActions = 0 ;
102- let rightActions = 0 ;
98+ let shouldCenterContent = false ;
99+ let shouldAddLeftSpacing = false ;
100+ let shouldAddRightSpacing = false ;
103101
104102 if ( Platform . OS === 'ios' ) {
105- childrenArray . forEach ( child => {
106- if (
107- ! isAppbarContentFound &&
108- React . isValidElement ( child ) &&
109- child . type !== AppbarContent
110- ) {
111- leftActions ++ ;
112- } else if (
113- React . isValidElement ( child ) &&
114- child . type === AppbarContent
115- ) {
116- isAppbarContentFound = true ;
117- } else {
118- rightActions ++ ;
103+ let hasAppbarContent = false ;
104+ let leftItemsCount = 0 ;
105+ let rightItemsCount = 0 ;
106+
107+ React . Children . forEach ( children , child => {
108+ if ( React . isValidElement ( child ) ) {
109+ if ( child . type === AppbarContent ) {
110+ hasAppbarContent = true ;
111+ } else if ( hasAppbarContent ) {
112+ rightItemsCount ++ ;
113+ } else {
114+ leftItemsCount ++ ;
115+ }
119116 }
120117 } ) ;
121- }
122118
123- const centerIos =
124- Platform . OS === 'ios' && ( leftActions < 2 && rightActions < 2 ) ;
125-
126- if ( centerIos && leftActions === 0 ) {
127- childrenArray . unshift (
128- < View key = "left-empty-icon" style = { styles . emptyIcon } />
129- ) ;
130- }
131-
132- if ( centerIos && rightActions === 0 ) {
133- childrenArray . push (
134- < View key = "right-empty-icon" style = { styles . emptyIcon} / >
135- ) ;
119+ shouldCenterContent =
120+ hasAppbarContent && ( leftItemsCount < 2 && rightItemsCount < 2 ) ;
121+ shouldAddLeftSpacing = shouldCenterContent && leftItemsCount === 0 ;
122+ shouldAddRightSpacing = shouldCenterContent && rightItemsCount === 0 ;
136123 }
137124
138125 return (
139126 < View style = { [ { backgroundColor } , styles . appbar , restStyle ] } { ...rest } >
140- { childrenArray
141- . filter ( child => React . isValidElement ( child ) )
142- . map ( ( child : any , i ) => {
127+ { shouldAddLeftSpacing ? < View style = { styles . spacing } /> : null }
128+ { React . Children . toArray ( children )
129+ . filter ( Boolean )
130+ . map ( ( child , i ) => {
131+ if ( ! React . isValidElement ( child ) ) {
132+ return child ;
133+ }
134+
143135 const props : { color : ?string , style ?: any } = {
144136 color :
145137 typeof child . props . color !== 'undefined'
@@ -150,16 +142,17 @@ class Appbar extends React.Component<Props> {
150142 } ;
151143
152144 if ( child . type === AppbarContent ) {
153- // Extra margin between left icon and AppbarContent
154145 props . style = [
155- { marginHorizontal : i === 0 ? 0 : 8 } ,
156- centerIos && { alignItems : 'center' } ,
146+ // Since content is not first item, add extra left margin
147+ i !== 0 && { marginLeft : 8 } ,
148+ shouldCenterContent && { alignItems : 'center' } ,
157149 child . props . style ,
158150 ] ;
159151 }
160152
161153 return React . cloneElement ( child , props ) ;
162154 } ) }
155+ { shouldAddRightSpacing ? < View style = { styles . spacing} / > : null }
163156 </View >
164157 ) ;
165158 }
@@ -173,10 +166,8 @@ const styles = StyleSheet.create({
173166 paddingHorizontal : 4 ,
174167 elevation : 4 ,
175168 } ,
176- emptyIcon : {
177- height : 36 ,
178- width : 36 ,
179- marginHorizontal : 6 ,
169+ spacing : {
170+ width : 48 ,
180171 } ,
181172} ) ;
182173
0 commit comments