React Native 封装自定义的Button

自定义Button

import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Dimensions,
    TouchableOpacity,
    TouchableHighlight
} from 'react-native';
import Text from '../text';
//模块声名并导出
export default class Button extends Component {
    //属性声名
    static propTypes = {
        txtStyle:React.PropTypes.any, // 文字颜色
        underlayColor:React.PropTypes.string, // 点击的时候按钮背景颜色
        underlayTxtColor:React.PropTypes.string, // 点击的时候文字颜色
        onPress: React.PropTypes.func, // 点击事件
        onPressOut: React.PropTypes.func, // 结束点击的时候触发事件
        onLongPress: React.PropTypes.func, // 长按事件
        imageView: React.PropTypes.element, // 图片

    };

    //默认属性
    static defaultProps = {
        underlayColor: '#00000000',
        underlayTxtColor: '#00000088',
        txtStyle: { color: '#000000' },
        imageView: null,
        disabled: false
    }
    //构造函数
    constructor(props) {
        super(props);
        // console.log(this.props);
        this.state = { //状态机变量声明
            showButtonTxt: true,
            onShowUnderlay: false
        }
    }

    onShowUnderlay = () => {
        // alert('_onShowUnderlay');
        this.setState({ onShowUnderlay: true });
    }

    onHideUnderlay = () => {
        // alert('_onHideUnderlay');
        this.setState({ onShowUnderlay: false });
    }

    //渲染
    render() {
        return (
            <TouchableHighlight
                onShowUnderlay={this.onShowUnderlay}
                onHideUnderlay={this.onHideUnderlay}
                underlayColor={this.props.underlayColor}
                onPress={this.props.onPress}
                onLongPress={this.props.onLongPress}
                onPressOut={this.props.onPressOut}
                style={[styles.button, this.props.style]}
                disabled={this.props.disabled}
            >
                {this.state.showButtonTxt
                    //样式继承,带条件样式
                    ? <View style={{ flexDirection: 'row',justifyContent:'center',alignItems:'center',paddingLeft:5,paddingRight:5 }}>
                        {this.props.imageView ? <View style={{ marginRight: 5 }}>{this.props.imageView}</View> : null}
                        <Text
                            style={[styles.txt, this.props.txtStyle, this.state.onShowUnderlay && {color: this.props.underlayTxtColor}]}>
                            {//显示成员变量
                                this.props.children}
                        </Text>
                    </View>
                    : null}

            </TouchableHighlight>
        );
    }
};

const styles = StyleSheet.create({
    button: {
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',

    },
    txt: {
        textAlign: 'center',
    }
})

调用组件

<Button
      underlayColor={Constant.colorTxtPrimary}
      underlayTxtColor='#FFFFFF'
      txtStyle={{color: '#FFFFFF', fontSize: 18, textAlign: 'center'}}
      style={{height: 40, width: width - 80, padding: 0, backgroundColor: 'pink',borderRadius: 4, marginTop: 25}}
      onPress={() => this.loginBtnClick()}>
        登录
</Button>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值