react-native自定义按钮
2018-06-17 20:32
495 查看
版权声明:本文为博主原创文章,若需转载,请注明出处 https://blog.csdn.net/suwu150/article/details/80720746
The final rendering
Description:
A custom button component that should render nicely on any platform. Supports a minimal level of customization.
Installation:
npm install --save react-native-customer-button
Example usage:
1.basic
import Button from 'react-native-customer-button'; ... _onPress = () => { console.log('onPress'); } render() { return ( <View style={styles.container}> <Button onPress={this._onPress} /> </View> ); }
Props:
Props | Explain | type | require | default |
---|---|---|---|---|
onPress | handler to be called when the user taps the button | function | yes | no |
title | text to display inside the button | string | yes | ok |
disabled | If true, disable all interactions for this component. | bool | no | false |
style | button style,The style used to modify the button | object | no | { backgroundColor: ‘#43CCFF’,height: 30,width: 100,} |
textStyle | The style of the text in the button | object | no | {fontSize: 15, color: ‘#000000’} |
icon | button icon file resource, resource file introduced by require | Image resources | no | no |
iconStyle | Image’s style | object | no | {} |
Examples from props:
render() { return ( <View style={styles.container}> <Text style={{ marginBottom: 10 }}>{this.state.count}</Text> <Button style={{ marginBottom: 5 }} onPress={() => this.setState({ count: this.state.count + 1 })} /> <Button title={'cancel'} style={{ marginBottom: 5 }} onPress={() => this.setState({ count: 'cancel' })} /> <Button title={'submit'} style={{ marginBottom: 5, backgroundColor: 'gray' }} onPress={() => this.setState({ count: 'submit' })} /> <Button title={'white font'} style={{ marginBottom: 5, backgroundColor: 'gray' }} textStyle={{ color: '#fff', fontSize: 16 }} underlayColor={'#ff0'} onPress={() => this.setState({ count: 'submit' })} /> <Button title={'white font'} style={{ marginBottom: 5, backgroundColor: 'gray' }} textStyle={{ color: '#fff', fontSize: 16 }} icon={require('../constants/icon_user.png')} underlayColor={'#ff0'} onPress={() => this.setState({ count: 'submit' })} /> </View> ); }
Code
import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { ViewPropTypes, TouchableOpacity, TouchableHighlight, Text, View, StyleSheet, Image } from 'react-native'; const NoDoublePress = { lastPressTime: 1, onPress(callback) { const curTime = new Date().getTime(); if (curTime - this.lastPressTime > 1000) { this.lastPressTime = curTime; callback(); } }, }; const styles = StyleSheet.create({ defaultStyle: { backgroundColor: '#43CCFF', justifyContent: 'center', alignItems: 'center', height: 30, width: 100, }, defaultTouchStyle: { width: '100%', height: '100%', flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }, defaultTextStyle: { fontSize: 15, color: '#000000' }, defaultRowContainer: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center' } }); class RNButton extends Component { constructor(props) { super(props); this.state = {}; } static propTypes = { title: PropTypes.string, style: ViewPropTypes.style, underlayColor: PropTypes.string, }; static defaultProps = { title: 'ok', }; onPress = (e) => { NoDoublePress.onPress(() => { this.props.onPress && this.props.onPress(e); }); }; renderIcon = () => { const { icon, iconStyle = {} } = this.props; return ( <Image source={icon} style={iconStyle} /> ); }; render() { const { title } = this.props; if (this.props.underlayColor) { return ( <View style={[styles.defaultStyle, this.props.style]}> <TouchableHighlight style={[styles.defaultTouchStyle]} onPress={this.onPress} underlayColor={this.props.underlayColor} > <View style={styles.defaultRowContainer}> {this.renderIcon()} <Text style={[styles.defaultTextStyle, this.props.textStyle]} > {this.props.title} </Text> </View> </TouchableHighlight> </View> ); } return ( <View style={[styles.defaultStyle, this.props.style]}> <TouchableOpacity style={[styles.defaultTouchStyle]} onPress={this.onPress} > <View style={styles.defaultRowContainer}> {this.renderIcon()} <Text style={[styles.defaultTextStyle, this.props.textStyle]} > {this.props.title} </Text> </View> </TouchableOpacity> </View> ); } } export default RNButton;
相关文章推荐
- react -native 自定义按钮
- React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)
- ReactNative——TabNavigator的深入了解,打造一个自定义的Tab
- React-Native 自定义Button 获取远程数据
- react-native-vector-icons的简单使用,图片,按钮,标签视图,导航条
- 【REACT NATIVE 系列教程之二】创建自定义组件&&导入与使用示例
- React-Native引用自定义组件
- React-Native自定义view显示(js端)
- react native RadioButton(单选按钮)
- react_native 项目实战 (4) 自定义分类 使用 CheckBox 以及 数据存储asyncStorage
- React-Native 使用自定义IconFont
- [教程] React Native基础实战(1)—— 制作一个简单的按钮
- react native Back 按钮封装
- React-native自定义安卓组件
- React-Native引用自定义组件类
- React-Native 自定义TextInput 登录页面
- react native的单选按钮react-native-radio-master
- React Native按下按钮呈现的效果
- React Native 扫码组件react-native-camera与自定义UI界面动画套装
- ReactNative自定义Avatar&Badge组件