上一篇 react-native文章提到了TextInput组件对安卓的适配问题,因此对该组件进行封装很有必要。

文章地址  react native定报预披项目知识点总结

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组件的更多相关文章

  1. React Native之TextInput的介绍与使用(富文本封装与使用实例,常用输入框封装与使用实例)

    React Native之TextInput的介绍与使用(富文本封装与使用实例,常用输入框封装与使用实例) TextInput组件介绍 TextInput是一个允许用户在应用中通过键盘输入文本的基本组 ...

  2. react native之组织组件

    这些组件包括<TabView>,<NavigatorView>和<ListView>,他们实现了手机端最常用的交互和导航.你会发现这些组件在实际的项目中会非常有用. ...

  3. React Native之倒计时组件的实现(ios android)

    React Native之倒计时组件的实现(ios android) 一,需求分析 1,app需实现类似于淘宝的活动倒计时,并在倒计时结束时,活动也结束. 2,实现订单倒计时,并在倒计时结束时,订单关 ...

  4. React Native封装Toast与加载Loading组件

    React Native开发封装Toast与加载Loading组件 在App开发中,我们避免不了使用的两个组件,一个Toast,一个网络加载Loading,在RN开发中,也是一样,React Nati ...

  5. React Native常用第三方组件汇总--史上最全 之一

    React Native 项目常用第三方组件汇总: react-native-animatable 动画 react-native-carousel 轮播 react-native-countdown ...

  6. React Native常用第三方组件汇总--史上最全[转]

    本文出处: http://blog.csdn.net/chichengjunma/article/details/52920137 React Native 项目常用第三方组件汇总: react-na ...

  7. React Native 之 TextInput使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  8. React Native知识6-NavigatorIOS组件

    NavigatorIOS包装了UIKit的导航功能,可以使用左划功能来返回到上一界面.本组件并非由Facebook官方开发组维护.这一组件的开发完全由社区主导.如果纯js的方案能够满足你的需求的话,那 ...

  9. react native 之子组件和父组件之间的通信

    react native开发中,为了封装性经常需要自定义组件,这样就会出现父组件和子组件,那么怎么在父组件和子组件之间相互通信呢,也就是怎么在各自界面push和pop.传值. 父组件传递给子组件: 父 ...

随机推荐

  1. MVC,EF 小小封装

    1.项目中经常要用到 EF,有时候大多数的增删改查都是重复性的东西,本次封装就是为了快速开发,期间没有考虑到架构上的各种思想,就感觉到欠缺点什么东西所以这次将这些拉出来,有存在问题的话还请各位多多指导 ...

  2. jQuery(三)、属性、CSS

    jQuery设置了很多为标签进行属性的操作,比如添加.删除. 一 .属性 1 attr(name | properties | [key, value | fn]) 设置或返回被选择的属性值. 参数: ...

  3. java_IO流

    IO流概述及分类 Reader  InputStream  OutputStream  Writer都是Object的直接子类 字节流: 字节输入流 InputStream(抽象类) |---File ...

  4. 对于python爬虫urllib库的一些理解(抽空更新)

    urllib库是Python中一个最基本的网络请求库.可以模拟浏览器的行为,向指定的服务器发送一个请求,并可以保存服务器返回的数据. urlopen函数: 在Python3的urllib库中,所有和网 ...

  5. python迭代器与生成器及yield

    一.迭代器(itertor) 1.可迭代: 在Python中如果一个对象有__iter__()方法或__getitem__()方法,则称这个对象是可迭代的(iterable). 其中__iter__( ...

  6. go语言模版编程

    传送门: 柏链项目学院 go语言与模版编程 什么是模版编程 模板是将一个事物的结构规律予以固定化.标准化的成果,它体现的是结构形式的标准化.对于我们程序员来说,更直白的理解是:对于要输出的内容,个人位 ...

  7. Python入门测试

    1.比如自然数10以下能被3或者5整除的有,3,5,6和9,那么这些数字的和为23. 求能被3或者5整除的1000以内数字的和 multiple_of_threes=[] for multiple_o ...

  8. 从输出日志中提取接口的入参和返回做为用例导入到excel中

    1  背景 接口用例已经在项目中的yml文件中编写,但是yml文件不能做为交付文档用,本文对工作中从接口输出日志中提取用例信息,并导入到excel文件中做了总些 2  工具 idea,notepad+ ...

  9. git@github.com: Permission denied (publickey).////remote: Permission to xxx/test.git denied to xxx.等权限问题

    Error msg git@github.com: Permission denied (publickey) 或者: remote: Permission to xxx/test.git denie ...

  10. 关于ES6的module的循环加载

    今天写js时,碰到了一个模块循环加载的错误,下面时例子: // testa.mjs import testb from './testb.mjs'; const {b} = testb; const ...