react+antd.3x实现ip输入框

2022-04-15 0 521

本文实例为大家分享了react+antd.3x实现ip输入框的具体代码,供大家参考,具体内容如下

表现形式如下:

react+antd.3x实现ip输入框

js+html

/**
 * zks 2021 10 26
 * ip输入框,作用于新建与修改虚拟子网  
 * 使用方式:参看antd-form自定义表单控件。
 */
import React  from 'react';
import { Input} from 'antd';
import styles from './index.less'
class IpInput extends React.Component{
    constructor(){
        super();
        this._refs = {
          refip_0:React.createRef(),
          refip_1:React.createRef(),
          refip_2:React.createRef(),
          refip_3:React.createRef()
        };
    }
    handleNumberChange = (e,type) => {
        //确保最小值为0;
        const number = parseInt(e.target.value || 0, 10);
        if (isNaN(number)) {
          return;
        }
        let Obj = {}
        Obj[`${type}`] = number
        this.triggerChange(Obj);
    };
    triggerChange = changedValue => {
        const { onChange, value } = this.props;
        if (onChange) {
          onChange({
            ...value,
            ...changedValue,
          });
        }
      };
    turnIpPOS = (e,type)=>{
        let self = this;
         //左箭头向左跳转,左一不做任何措施
        if(e.keyCode === 37) {
          if(type === 0) {} else {
            self._refs[`refip_${type-1}`].current.focus();
          }
        }
        //右箭头、回车键、空格键、冒号均向右跳转,右一不做任何措施
        if(e.keyCode === 39 || e.keyCode === 13 || e.keyCode === 32 || e.keyCode === 190) {
           if(type === 3) {} else {
            self._refs[`refip_${type+1}`].current.focus();
           }
        }
    }
    render(){
        const { value } = this.props;
        return (
            <Input.Group compact className = {styles.inputGroup}>
              <Input style={{ width: '24%' }} className = {styles.self_input} ref = {this._refs.refip_0} value = {value.ip_0} maxLength ={3} onChange={(e)=>{this.handleNumberChange(e,'ip_0')}} onKeyUp ={(e)=>this.turnIpPOS(e,0)}/>
              <span className = {styles.dot} ></span>
              <Input style={{ width: '24%' }} className = {styles.self_input} ref = {this._refs.refip_1} value = {value.ip_1} maxLength ={3} onChange={(e)=>{this.handleNumberChange(e,'ip_1')}} onKeyUp ={(e)=>this.turnIpPOS(e,1)}/>
              <span className = {styles.dot}></span>
              <Input style={{ width: '24%' }} className = {styles.self_input} ref = {this._refs.refip_2} value = {value.ip_2} maxLength ={3} onChange={(e)=>{this.handleNumberChange(e,'ip_2')}} onKeyUp ={(e)=>this.turnIpPOS(e,2)}/>
              <span className = {styles.dot}></span>
              <Input style={{ width: '24%' }} className = {styles.self_input} ref = {this._refs.refip_3} value = {value.ip_3} maxLength ={3} onChange={(e)=>{this.handleNumberChange(e,'ip_3')}} onKeyUp ={(e)=>this.turnIpPOS(e,3)}/>
            </Input.Group>
        )
    }
   
}
export default IpInput;

css

.inputGroup {
  border: 1px solid #d9d9d9;
  border-radius: 2px;
  transition: all 0.3s;
  &:hover {
    border-color: #45bbff;
    border-right-width: 1px !important;
    outline: 0;
    box-shadow: 0 0 0 2px rgba(29, 165, 255, 0.2);
  }
  text-align: center;
  .dot {
    width: 3px;
    height: 3px;
    border: 1px solid #000;
    border-radius: 3px;
    background-color: #000;
    opacity: 0.5;
    z-index: 9;
    position: relative;
    top: 21px;
  }
}
.self_input {
  border: none;
  outline: 0px;
  &:hover {
    box-shadow: none;
  }
  &::selection {
    box-shadow: none;
  }
  &:focus {
    box-shadow: none;
  }
}

使用方式

import IPInput from '../../public/IpInput';
<FormItem label="子网网关" {...formItemLayout}>
                {getFieldDecorator('price', {
                  initialValue: { ip_0: 255, ip_1: 235, ip_2: 255, ip_3: 255 },
                  rules: [{ validator: this.checkIp }],
                })(<IPInput />)}
 </FormItem>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持NICE源码。

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript react+antd.3x实现ip输入框 https://www.niceym.com/18827.html