当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 使用react实现手机号的数据同步显示功能的示例代码

使用react实现手机号的数据同步显示功能的示例代码

2018年04月10日  | 移动技术网IT编程  | 我要评论

本文介绍了使用react实现手机号的数据同步显示功能的示例代码,分享给大家,具体如下:

要求如下

  1. 输入框输入内容数据长度大于0,展示出预览信息
  2. 光标离开关闭预览信息
  3. 预览信息每隔4位插入一个特殊字符_,输入内容不变
  4. 限制长度为13位
  5. 只允许输入数字(0-9)
// zinput.js
import react, {
 component
} from 'react';
import './zinput.css'

// note: 获取焦点事件 原生onfocus 即可
// note: 离开焦点事件 原生onblur即可
// note: 输入框数据过滤 直接在change方法里进行过滤
// note: 条件处理 通过不同条件返回不同节点做条件处理
class zinput extends component {
 constructor(props) {
  super(props);
  this.state = {
   value: '',
   showbig: false,
  };
  this.handlechange = this.handlechange.bind(this);
  this.inputonfocus = this.inputonfocus.bind(this);
  this.inputonblur = this.inputonblur.bind(this);
 }
 inputonfocus() {
  if (this.state.value.length > 0) {
   this.setstate({
    showbig: true
   })
  }
 }
 inputonblur() {
  this.setstate({
   showbig: false
  })
  if(this.props.chanegnumber){
   this.props.chanegnumber(this.state.value)
  }
 }
 handlechange(event) {
  let val = event.target.value.substr(0, 13)
   .replace(/[^\d]/g, '')
  event.target.value = val
  this.setstate({
   value: val,
   showbig: true,
  });
 }
 /**
  * 根据字符串没隔len位插入一个下滑杠,返回处理后的字符串
  * @method  getstr
  * @author 朱阳星
  * @datetime 2018-04-02t09:57:58+080
  * @email  zhuyangxing@foxmail.com
  * @param  {string} str 待处理字符串
  * @param  {number} len 每隔位数插入下滑杠
  * @return  {string} 处理后的字符串
  */
 getstr(str, len) {
  let lenth = str.length
  let len1 = len - 1
  let newstr = ''
  for (var i = 0; i < lenth; i++) {
   if (i % len === len1 && i > 0) {
    newstr += str.charat(i) + '_'
   } else {
    newstr += str.charat(i)
   }
  }
  if (newstr.length % (len + 1) === 0) {
   // 解决最后一位为补充项问题
   newstr = newstr.substr(0, newstr.length - 1)
  }
  return newstr
 }
 render() {
  // note return 需要用圆括号包住并处理
  // note 条件语句里没有节点也要用空字符串进行处理 否则sonalint会报错,页面也会报错
  const showbig = this.state.showbig ? (
   <div classname="big-show">{ this.getstr(this.state.value,4) }</div>
  ) : ''
  return (
   <div classname="zinput">
    <input classname="input" 
        type = "text" 
        onfocus={ this.inputonfocus }
        onblur={ this.inputonblur }
        value={ this.state.value } 
        onchange={ this.handlechange }>
        </input>
    {showbig}
   </div>
  )
 }
}

export default zinput; // don't forget to use export default!

<!-- zinput.css -->
.zinput{
  position: absolute;
  top:80px;
  left:40px;
  
}
.input {
 position: absolute;
 top: 0;
 left: 0;
}
.big-show {
  position: relative;
  top: -40px;
  font-size: 36px;
  line-height: 40px;
  background-color: red;
}

功能虽然实现,但是肯定是作为某个节点的某个子组件使用的,父组件调用方法有两种

1.使用refs直接获取子组件state的值

constructor(props) {
 super(props);
 this.handerclick2 = this.handerclick2.bind(this);
}
handerclick2(){
 // note 父组件通过refs获取子组件的state 
 console.log("使用ref获取子组件的值",this.refs.zinput.state.value)
}
render() {
 return (
  <div classname="app">
   <zinput ref="zinput"></zinput>
   <input type="button" value="获取电话号码的值22" onclick={ this.handerclick2 }/>
  </div> 
 );
}

2.每次子组件焦点离开时调用父组件传过来的方法,修改父组件state值

constructor(props) {
 super(props);
 this.state = {
  phonenumber: '',
 };
 this.handerclick = this.handerclick.bind(this);
 this.changephonenumber = this.changephonenumber.bind(this);
}
changephonenumber(number){
 this.setstate({
   phonenumber: number,
 })
}
handerclick(){
 // note 根据react的思想是在子组件处理完某件事的时候调用父组件的方法修改父组件的state值
 console.log("使用state获取值",this.state.phonenumber)
}
render() {
 return (
  <div classname="app">
   <zinput ref="zinput" chanegnumber={this.changephonenumber}></zinput>
   <input type="button" value="获取电话号码的值" onclick={ this.handerclick }/>
  </div>
 );
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网