先上效果圖:
QQ20170629-111706.png
這是之前項目中封裝的首頁圖標組件多律,現(xiàn)在項目改版,所以這個組件便被舍棄了搂蜓,在此記錄下狼荞,以免忘記。
一帮碰、封裝組件
HomeCell.js
/**
* Created by sybil052 on 2017/3/28.
* 首頁grid網(wǎng)格布局
*/
import React, {Component, PropTypes} from 'react';
import {
View,
StyleSheet,
Text,
TouchableOpacity,
} from 'react-native';
import {
WHITE_COLOR,
RED_TEXT_COLOR,
} from '../../constants/staticColor';
const styles = StyleSheet.create({
badgeIcon: {
backgroundColor: RED_TEXT_COLOR,
width: 18,
height: 18,
alignSelf: 'center',
borderRadius: 9,
alignItems: 'center',
zIndex: 3,
justifyContent: 'center',
position: 'relative',
top: 6,
right: -12,
},
badgeNull: {
width: 18,
height: 18,
alignSelf: 'center',
borderRadius: 9,
alignItems: 'center',
zIndex: 3,
justifyContent: 'center',
position: 'relative',
top: 6,
right: -12,
},
container: {
width: 80,
height: 80,
borderRadius: 40,
},
});
export default class HomeCell extends Component {
// 定義相關屬性類型
static propTypes = {
badgeStyle: View.propTypes.style,
backgroundColor: View.propTypes.style,
title: PropTypes.string.isRequired,
padding: PropTypes.number,
renderImage: PropTypes.func,
clickAction: PropTypes.func,
badgeText: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};
// render外部傳遞的組件
renderImage(props) {
if (this.props.renderImage) {
// 這里將引用外部renderImage方法
return React.cloneElement(this.props.renderImage(), props);
}
return null;
}
render() {
const {title, renderImage, padding, badgeText, clickAction} = this.props;
return (
<TouchableOpacity
style={[{
paddingTop: 40,
paddingBottom: 40,
paddingLeft: 54,
paddingRight: 54,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: WHITE_COLOR,
}, this.props.badgeStyle]}
onPress={() => {
clickAction();
}} activeOpacity={0.6}
>
<View style={[styles.container, this.props.backgroundColor]}>
<View style={{alignSelf: 'center'}}>
{
badgeText ?
<View style={styles.badgeIcon}>
<Text
style={{
color: WHITE_COLOR,
fontSize: 11,
backgroundColor: 'transparent',
}}
>{badgeText}</Text></View>
: <View style={styles.badgeNull} />
}
{this.renderImage(this.props)}
</View>
<Text
style={{
marginTop: padding,
fontSize: 15,
color: WHITE_COLOR,
backgroundColor: 'transparent',
alignSelf: 'center',
}}
>{title}</Text>
</View>
</TouchableOpacity>
);
}
}
二相味、調用
home.js
render() {
const {homePageState} = this.props;
return (
<View style={{flexDirection: 'row', marginTop: 10}}>
<HomeCell
title="接單"http:// 文字
padding={3}// 文字與圖片間距
badgeStyle={{flex: 1}}
backgroundColor={{backgroundColor: BLUE_CIRCLE_COLOR}}// 大圓背景色
badgeText={homePageState == null ? 0 : homePageState.pendingCount}// 消息提示
renderImage={() => <Text style={styles.icon}></Text>}// 圖標
clickAction={() => { // 點擊事件}}
/>
<View style={styles.line}/>
<HomeCell
title="發(fā)運"
padding={2}
badgeStyle={{flex: 1}}
backgroundColor={{backgroundColor: ORANGE_CIRCLE_COLOR}}
badgeText={homePageState == null ? 0 : homePageState.notYetShipmentCount}
renderImage={() => <Text style={styles.icon}></Text>}
clickAction={() => {}}
/>
</View>
);
}
...
const styles = StyleSheet.create({
line: {
backgroundColor: DEVIDE_LINE_COLOR,
width: 0.5,
},
icon: {
fontFamily: 'iconfont',
fontSize: 23,
color: WHITE_COLOR,
},
});