react-native 封裝首頁圖標組件

先上效果圖:


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}>&#xe60d;</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}>&#xe611;</Text>}
                        clickAction={() => {}}
                    />
                </View>
            );
      }

 ...
const styles = StyleSheet.create({
    line: {
        backgroundColor: DEVIDE_LINE_COLOR,
        width: 0.5,
    },
    icon: {
        fontFamily: 'iconfont',
        fontSize: 23,
        color: WHITE_COLOR,
    },
});
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市收毫,隨后出現(xiàn)的幾起案子攻走,更是在濱河造成了極大的恐慌殷勘,老刑警劉巖此再,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異玲销,居然都是意外死亡输拇,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門贤斜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來策吠,“玉大人,你說我怎么就攤上這事瘩绒『锬ǎ” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵锁荔,是天一觀的道長蟀给。 經(jīng)常有香客問我,道長阳堕,這世上最難降的妖魔是什么跋理? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮恬总,結果婚禮上前普,老公的妹妹穿的比我還像新娘。我一直安慰自己壹堰,他們只是感情好拭卿,可當我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布骡湖。 她就那樣靜靜地躺著,像睡著了一般峻厚。 火紅的嫁衣襯著肌膚如雪勺鸦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天目木,我揣著相機與錄音换途,去河邊找鬼。 笑死刽射,一個胖子當著我的面吹牛军拟,可吹牛的內容都是我干的。 我是一名探鬼主播誓禁,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼懈息,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了摹恰?” 一聲冷哼從身側響起辫继,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎俗慈,沒想到半個月后姑宽,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡闺阱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年炮车,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片酣溃。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡瘦穆,死狀恐怖,靈堂內的尸體忽然破棺而出赊豌,到底是詐尸還是另有隱情扛或,我是刑警寧澤,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布碘饼,位于F島的核電站熙兔,受9級特大地震影響,放射性物質發(fā)生泄漏派昧。R本人自食惡果不足惜黔姜,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蒂萎。 院中可真熱鬧秆吵,春花似錦、人聲如沸五慈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至毙芜,卻和暖如春忽媒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背腋粥。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工晦雨, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人隘冲。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓闹瞧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親展辞。 傳聞我的和親對象是個殘疾皇子奥邮,可洞房花燭夜當晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,312評論 25 707
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件罗珍、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,123評論 4 61
  • 文/南風 我們公司樓下洽腺,有間門臉很小的飯館,我經(jīng)常去那里吃飯覆旱。 飯館是夫妻檔蘸朋,一間大堂只放得開六張桌子,另有幾間狹...
    小小南風閱讀 567評論 0 0
  • 紹興名人故居多通殃, 代代靈秀壯山河度液。 拜訪能否慰先賢厕宗? 烏蓬船里費琢磨画舌。 文修身,武報國已慢。 文武之道始于學曲聂。 讀書最...
    曹煥甫閱讀 376評論 0 2
  • 沒有人能救得了你,除非你自己佑惠。 今天立下目標直到學期末朋腋! 6:30-7:15網(wǎng)易視頻/刷訂閱號等 22:00-23...
    送你一首狂想曲閱讀 147評論 0 0