-
Notifications
You must be signed in to change notification settings - Fork 141
/
SMXTabBarIconItemIOS.ios.js
114 lines (94 loc) · 2.85 KB
/
SMXTabBarIconItemIOS.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
*
* @providesModule SMXTabBarItemIOS
* @flow
*/
'use strict';
var React = require('react-native');
var { Image, StyleSheet, View, requireNativeComponent, PropTypes, Dimensions} = React;
var onlyChild = React.Children.only;
// Copy this right in here from react-contrib
class StaticContainer extends React.Component {
shouldComponentUpdate(nextProps) {
return !!nextProps.shouldUpdate;
}
render() {
var child = this.props.children;
return (child === null || child === false) ? null : onlyChild(child);
}
}
var SmixxTabBarItemIOS = React.createClass({
propTypes: {
onPress: PropTypes.func.isRequired,
selected: PropTypes.bool.isRequired,
badgeValue: PropTypes.string,
title: PropTypes.string,
icon: PropTypes.object,
selectedIcon: PropTypes.object,
},
getInitialState: function() {
return {
hasBeenSelected: false,
};
},
componentWillMount: function() {
if (this.props.selected) {
this.setState({hasBeenSelected: true});
}
},
componentWillReceiveProps: function(nextProps: { selected: boolean }) {
if (this.state.hasBeenSelected || nextProps.selected) {
this.setState({hasBeenSelected: true});
}
},
render: function() {
var tabContents = null;
// if the tab has already been shown once, always continue to show it so we
// preserve state between tab transitions
if (this.state.hasBeenSelected) {
tabContents = (
<StaticContainer shouldUpdate={this.props.selected}>
{this.props.children}
</StaticContainer>
);
} else {
tabContents = <View />;
}
var iconName = this.props.iconName;
var iconSize = this.props.iconSize || 28;
// defaults selectedIconName to iconName, selectedIconSize to iconSize
var selectedIconName = this.props.selectedIconName || this.props.iconName;
var selectedIconSize = this.props.selectedIconSize || this.props.iconSize;
if(iconName.indexOf('|') == -1) {
throw Error('iconName "' + iconName + '" doesn\'t specify a font name prefix. ex. "ion|beer"');
}
if(selectedIconName.indexOf('|') == -1) {
throw Error('selectedIconName "' + selectedIconName + '" doesn\'t specify a font name prefix. ex. "ion|beer"');
}
var icon = {name : iconName, size: iconSize};
var selectedIcon = {name: selectedIconName, size: selectedIconSize};
return (
<SmixxTabBarItem
icon={icon}
selectedIcon={selectedIcon}
onPress={this.props.onPress}
selected={this.props.selected}
badgeValue={this.props.badgeValue}
title={this.props.title}
style={[styles.tab, this.props.style]}>
{tabContents}
</SmixxTabBarItem>
);
}
});
var styles = StyleSheet.create({
tab: {
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
}
});
var SmixxTabBarItem = requireNativeComponent('SMXTabBarItem', SmixxTabBarItemIOS);
module.exports = SmixxTabBarItemIOS;