react native 封装TextInput组件
上一篇 react-native文章提到了TextInput组件对安卓的适配问题,因此对该组件进行封装很有必要。
TextInput介绍
官网地址:
https://facebook.github.io/react-native/docs/textinput
附加中文网地址:https://reactnative.cn/docs/textinput/
TextInput是一个允许用户在应用中通过键盘输入文本的基本组件。本组件的属性提供了多种特性的配置,譬如自动完成、自动大小写、占位文字,以及多种不同的键盘类型(如纯数字键盘)等等。最简单的用法就是丢一个TextInput
到应用里,然后订阅它的onChangeText
事件来读取用户的输入。
'use strict';
import React, { Component } from 'react'
import PropTypes from 'prop-types'
//import rpx from '../util/unit' import {
TextInput,
StyleSheet,
Platform,
Dimensions
} from 'react-native' const deviceH = Dimensions.get('window').height
const deviceW = Dimensions.get('window').width const basePx = 750 function rpx(px) {
return px * deviceW / basePx
} export default class Input extends Component{
constructor(props) {
super(props)
}
static propTypes = {
value:PropTypes.string
}
shouldComponentUpdate(nextProps){
return Platform.OS !== 'ios' || (this.props.value === nextProps.value &&
(nextProps.defaultValue == undefined || nextProps.defaultValue == '' )) ||
(this.props.defaultValue === nextProps.defaultValue && (nextProps.value == undefined || nextProps.value == '' )); }
blur() {
this.refs.textInput.blur()
}
render() {
return (
<TextInput
ref="textInput"
placeholderTextColor='#ccc'
placeholder={'输入代码、名称或简拼'}
style={[styles.textInput,this.props.style]}
underlineColorAndroid="transparent"
{...this.props}
/>
)
}
}
const styles = StyleSheet.create({
textInput:{
height:rpx(60),
fontSize:rpx(30),
color:'#333',
backgroundColor:'#fff',
borderRadius:rpx(4),
paddingHorizontal:rpx(20),
paddingVertical: 0
}
})
https://blog.csdn.net/halo1416/article/details/82703503
备注:TextInput组件内容超出加省列号:ellipsizeMode = 'tail' numberOfLines = {1 }
注明:IOS下TextInput不能输入中文,需要加上
shouldComponentUpdate(nextProps){
return Platform.OS !== 'ios' || (this.props.value === nextProps.value &&
(nextProps.defaultValue == undefined || nextProps.defaultValue == '' )) ||
(this.props.defaultValue === nextProps.defaultValue && (nextProps.value == undefined || nextProps.value == '' )); }
关于shouldComponentUpdate 可参考文章:http://www.80iter.com/blog/1512370656110845
react native 封装TextInput组件的更多相关文章
- React Native之TextInput的介绍与使用(富文本封装与使用实例,常用输入框封装与使用实例)
React Native之TextInput的介绍与使用(富文本封装与使用实例,常用输入框封装与使用实例) TextInput组件介绍 TextInput是一个允许用户在应用中通过键盘输入文本的基本组 ...
- react native之组织组件
这些组件包括<TabView>,<NavigatorView>和<ListView>,他们实现了手机端最常用的交互和导航.你会发现这些组件在实际的项目中会非常有用. ...
- React Native之倒计时组件的实现(ios android)
React Native之倒计时组件的实现(ios android) 一,需求分析 1,app需实现类似于淘宝的活动倒计时,并在倒计时结束时,活动也结束. 2,实现订单倒计时,并在倒计时结束时,订单关 ...
- React Native封装Toast与加载Loading组件
React Native开发封装Toast与加载Loading组件 在App开发中,我们避免不了使用的两个组件,一个Toast,一个网络加载Loading,在RN开发中,也是一样,React Nati ...
- React Native常用第三方组件汇总--史上最全 之一
React Native 项目常用第三方组件汇总: react-native-animatable 动画 react-native-carousel 轮播 react-native-countdown ...
- React Native常用第三方组件汇总--史上最全[转]
本文出处: http://blog.csdn.net/chichengjunma/article/details/52920137 React Native 项目常用第三方组件汇总: react-na ...
- React Native 之 TextInput使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- React Native知识6-NavigatorIOS组件
NavigatorIOS包装了UIKit的导航功能,可以使用左划功能来返回到上一界面.本组件并非由Facebook官方开发组维护.这一组件的开发完全由社区主导.如果纯js的方案能够满足你的需求的话,那 ...
- react native 之子组件和父组件之间的通信
react native开发中,为了封装性经常需要自定义组件,这样就会出现父组件和子组件,那么怎么在父组件和子组件之间相互通信呢,也就是怎么在各自界面push和pop.传值. 父组件传递给子组件: 父 ...
随机推荐
- 用JS在html页面实现打印功能
首先在head里面加入下面一段js代码: <script language="javascript"> function preview(oper) { if (ope ...
- springboot源码分析-SpringApplication
SpringApplication SpringApplication类提供了一种方便的方法来引导从main()方法启动的Spring应用程序 SpringBoot 包扫描注解源码分析 @Spring ...
- centos安装jenkins
1.安装jdk yum install java java -version 2.安装jenkins 添加Jenkins库到yum库,Jenkins将从这里下载安装. wget -O /etc/yum ...
- 用mapreduce 处理气象数据集
用mapreduce 处理气象数据集 编写程序求每日最高最低气温,区间最高最低气温 气象数据集下载地址为:ftp://ftp.ncdc.noaa.gov/pub/data/noaa 按学号后三位下载不 ...
- 使用vue-cli快速搭建vue项目
直接上干货...... 步骤: 1.安装node.js:(下载地址:https://nodejs.org/en/download/)安装完成以后,在命令窗口输入node -v 查看node版本. ...
- java 线程方法 ---- sleep()
class MyThread implements Runnable{ @Override public void run() { for (int i = 0; i < 5; i++){ Sy ...
- C语言经典算法 - 多维矩阵转一维矩阵的代码
下边内容内容是关于C语言经典算法 - 多维矩阵转一维矩阵的内容,应该能对码农也有好处. #include <stdio.h>#include <stdlib.h>int mai ...
- Oracle函数——日期函数
Oracle中的时间类型只有date和TIMESTAMP,TIMESTAMP是比date更精确的类型.日期时间函数用于处理时间类型的数据,Oracle以7位数字格式来存放日期数据,包括世纪.年.月.日 ...
- maven中央仓库、远程仓库地址
1.http://repo1.maven.org/maven2 (官方,速度一般) 2.http://maven.aliyun.com/nexus/content/repositories/centr ...
- 使用cmd查看电脑连接过的wifi密码并将密码发送至指定邮箱(三)
之前,我写了使用cmd查看电脑连接过的wifi密码(二)和使用cmd查看电脑连接过的wifi密码(一)但其中的功能不完善,在本次的更新中新增了更多的功能,其实2018/10/24 就更新完成了,一直拖 ...