@@ -5,6 +5,20 @@ import React, { PureComponent } from 'react';
5
5
import PropTypes from 'prop-types' ;
6
6
import { ToolbarAndroid } from './react-native' ;
7
7
8
+ const ICON_PROP_NAMES = [ 'iconSize' , 'iconColor' , 'titleColor' ] ;
9
+ const LOGO_ICON_PROP_NAMES = [ ...ICON_PROP_NAMES , 'logoName' ] ;
10
+ const NAV_ICON_PROP_NAMES = [ ...ICON_PROP_NAMES , 'navIconName' ] ;
11
+ const OVERFLOW_ICON_PROP_NAMES = [ ...ICON_PROP_NAMES , 'overflowIconName' ] ;
12
+ const ACTIONS_PROP_NAMES = [ ...ICON_PROP_NAMES , 'actions' ] ;
13
+
14
+ const arePropsEqual = keys => ( prevProps , nextProps ) =>
15
+ isEqual ( pick ( prevProps , keys ) , pick ( nextProps , keys ) ) ;
16
+
17
+ const areLogoIconPropsEqual = arePropsEqual ( LOGO_ICON_PROP_NAMES ) ;
18
+ const areNavIconPropsEqual = arePropsEqual ( NAV_ICON_PROP_NAMES ) ;
19
+ const areOverflowIconPropsEqual = arePropsEqual ( OVERFLOW_ICON_PROP_NAMES ) ;
20
+ const areActionPropsEqual = arePropsEqual ( ACTIONS_PROP_NAMES ) ;
21
+
8
22
export default function createToolbarAndroidComponent (
9
23
IconNamePropType ,
10
24
getImageSource
@@ -26,68 +40,103 @@ export default function createToolbarAndroidComponent(
26
40
) ,
27
41
iconSize : PropTypes . number ,
28
42
iconColor : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
43
+ titleColor : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
29
44
} ;
30
45
31
46
static defaultProps = {
32
47
iconSize : 24 ,
33
48
} ;
34
49
35
- componentWillMount ( ) {
36
- this . updateIconSources ( this . props ) ;
50
+ state = {
51
+ logo : undefined ,
52
+ navIcon : undefined ,
53
+ overflowIcon : undefined ,
54
+ actions : undefined ,
55
+ } ;
56
+
57
+ componentDidMount ( ) {
58
+ this . updateLogoIconSource ( ) ;
59
+ this . updateNavIconSource ( ) ;
60
+ this . updateOverflowIconSource ( ) ;
61
+ this . updateActionIconSources ( ) ;
37
62
}
38
63
39
- componentWillReceiveProps ( nextProps ) {
40
- const keys = Object . keys ( IconToolbarAndroid . propTypes ) ;
41
- if ( ! isEqual ( pick ( nextProps , keys ) , pick ( this . props , keys ) ) ) {
42
- const stateToEvict = { } ;
43
- if ( ! nextProps . logoName ) {
44
- stateToEvict . logo = undefined ;
45
- }
46
- if ( ! nextProps . navIconName ) {
47
- stateToEvict . navIcon = undefined ;
48
- }
49
- if ( ! nextProps . overflowIconName ) {
50
- stateToEvict . overflowIcon = undefined ;
51
- }
52
- if ( this . state && Object . keys ( stateToEvict ) . length ) {
53
- this . setState ( stateToEvict , ( ) => this . updateIconSources ( nextProps ) ) ;
54
- } else {
55
- this . updateIconSources ( nextProps ) ;
56
- }
64
+ componentDidUpdate ( prevProps ) {
65
+ if ( ! areLogoIconPropsEqual ( prevProps , this . props ) ) {
66
+ this . updateLogoIconSource ( ) ;
67
+ }
68
+ if ( ! areNavIconPropsEqual ( prevProps , this . props ) ) {
69
+ this . updateNavIconSource ( ) ;
70
+ }
71
+ if ( ! areOverflowIconPropsEqual ( prevProps , this . props ) ) {
72
+ this . updateOverflowIconSource ( ) ;
73
+ }
74
+ if ( ! areActionPropsEqual ( prevProps , this . props ) ) {
75
+ this . updateActionIconSources ( ) ;
57
76
}
58
77
}
59
78
60
- updateIconSources ( props ) {
61
- const size = props . iconSize ;
62
- const color = props . iconColor || props . titleColor ;
63
- if ( props . logoName ) {
64
- getImageSource ( props . logoName , size , color ) . then ( logo =>
65
- this . setState ( { logo } )
79
+ async updateLogoIconSource ( ) {
80
+ const { logoName, iconSize, iconColor, titleColor } = this . props ;
81
+ if ( logoName ) {
82
+ const logo = await getImageSource (
83
+ logoName ,
84
+ iconSize ,
85
+ iconColor || titleColor
66
86
) ;
87
+ this . setState ( { logo } ) ;
88
+ // eslint-disable-next-line react/destructuring-assignment
89
+ } else if ( this . state . logo ) {
90
+ this . setState ( { logo : undefined } ) ;
67
91
}
68
- if ( props . navIconName ) {
69
- getImageSource ( props . navIconName , size , color ) . then ( navIcon =>
70
- this . setState ( { navIcon } )
92
+ }
93
+
94
+ async updateNavIconSource ( ) {
95
+ const { navIconName, iconSize, iconColor, titleColor } = this . props ;
96
+ if ( navIconName ) {
97
+ const navIcon = await getImageSource (
98
+ navIconName ,
99
+ iconSize ,
100
+ iconColor || titleColor
71
101
) ;
102
+ console . log ( { navIcon } ) ;
103
+ this . setState ( { navIcon } ) ;
104
+ // eslint-disable-next-line react/destructuring-assignment
105
+ } else if ( this . state . navIcon ) {
106
+ this . setState ( { navIcon : undefined } ) ;
72
107
}
73
- if ( props . overflowIconName ) {
74
- getImageSource ( props . overflowIconName , size , color ) . then ( overflowIcon =>
75
- this . setState ( { overflowIcon } )
108
+ }
109
+
110
+ async updateOverflowIconSource ( ) {
111
+ const { overflowIconName, iconSize, iconColor, titleColor } = this . props ;
112
+ if ( overflowIconName ) {
113
+ const overflowIcon = await getImageSource (
114
+ overflowIconName ,
115
+ iconSize ,
116
+ iconColor || titleColor
76
117
) ;
118
+ this . setState ( { overflowIcon } ) ;
119
+ // eslint-disable-next-line react/destructuring-assignment
120
+ } else if ( this . state . overflowIcon ) {
121
+ this . setState ( { overflowIcon : undefined } ) ;
77
122
}
123
+ }
78
124
79
- Promise . all (
80
- ( props . actions || [ ] ) . map ( action => {
125
+ async updateActionIconSources ( ) {
126
+ const { actions, iconSize, iconColor, titleColor } = this . props ;
127
+ const updatedActions = await Promise . all (
128
+ ( actions || [ ] ) . map ( action => {
81
129
if ( action . iconName ) {
82
130
return getImageSource (
83
131
action . iconName ,
84
- action . iconSize || size ,
85
- action . iconColor || color
132
+ action . iconSize || iconSize ,
133
+ action . iconColor || iconColor || titleColor
86
134
) . then ( icon => ( { ...action , icon } ) ) ;
87
135
}
88
136
return Promise . resolve ( action ) ;
89
137
} )
90
- ) . then ( actions => this . setState ( { actions } ) ) ;
138
+ ) ;
139
+ this . setState ( { actions : updatedActions } ) ;
91
140
}
92
141
93
142
render ( ) {
0 commit comments