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

 更新时间:2021年10月29日 14:34:52   作者:代码小戳  
这篇文章主要为大家详细介绍了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>

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

相关文章

  • React使用Echarts/Ant-design-charts的案例代码

    React使用Echarts/Ant-design-charts的案例代码

    这篇文章主要介绍了React使用Echarts/Ant-design-charts的实例代码,本文通过实例代码给大家讲解的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-11-11
  • 关于getDerivedStateFromProps填坑记录

    关于getDerivedStateFromProps填坑记录

    这篇文章主要介绍了关于getDerivedStateFromProps填坑记录,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • 基于React封装一个层次模糊效果的容器组件

    基于React封装一个层次模糊效果的容器组件

    这篇文章主要为大家详细介绍了如何基于React封装一个层次模糊效果的容器组件,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • react-native组件中NavigatorIOS和ListView结合使用的方法

    react-native组件中NavigatorIOS和ListView结合使用的方法

    这篇文章主要给大家介绍了关于react-native组件中NavigatorIOS和ListView结合使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-09-09
  • 详解Ant Design of React的安装和使用方法

    详解Ant Design of React的安装和使用方法

    这篇文章主要介绍了详解Ant Design of React的安装和使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • React 对接流式接口的具体使用

    React 对接流式接口的具体使用

    React应用中对接流式接口通常涉及到处理实时数据传输,本文就来介绍一下React 对接流式接口的具体使用,具有一定的参考价值,感兴趣的可以了解一下
    2025-04-04
  • 解决React报错Parameter 'props' implicitly has an 'any' type

    解决React报错Parameter 'props' implicitly&nb

    这篇文章主要为大家介绍了React报错Parameter 'props' implicitly has an 'any' type的解决处理方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • React hooks如何清除定时器并验证效果

    React hooks如何清除定时器并验证效果

    在React中,通过自定义Hook useTimeHook实现定时器的启动与清除,在App组件中使用Clock组件展示当前时间,利用useEffect钩子在组件挂载时启动定时器,同时确保组件卸载时清除定时器,避免内存泄露,这种方式简化了状态管理和副作用的处理
    2024-10-10
  • 在React中编写class样式的方法总结

    在React中编写class样式的方法总结

    在TypeScript (TSX) 中编写 CSS 样式类有几种方法,包括使用纯 CSS、CSS Modules、Styled Components 等,本文给大家介绍了几种常见方法的示例,通过代码示例讲解的非常详细,需要的朋友可以参考下
    2024-07-07
  • React Hooks之useRef获取元素示例详解

    React Hooks之useRef获取元素示例详解

    这篇文章主要介绍了React Hooks之useRef获取元素示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07

最新评论