react+antd.3x implements ip input box

react+antd.3x implements ip input box

This article shares the specific code of react+antd.3x to implement the ip input box for your reference. The specific content is as follows

The manifestation is as follows:

js+html

/**
 * zks 2021 10 26
 * IP input box, used to create and modify virtual subnets * Usage: See antd-form custom form controls.
 */
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) => {
        // Ensure the minimum value is 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;
         //Left arrow jumps to the left, and does nothing if(e.keyCode === 37) {
          if(type === 0) {} else {
            self._refs[`refip_${type-1}`].current.focus();
          }
        }
        //The right arrow, enter key, space bar, and colon all jump to the right, and the right one does nothing 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;
  }
}

How to use

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • React example of how to get the value of the input box
  • Solve the problem of react-native soft keyboard popping up and blocking the input box
  • How to solve the problem of input box being blocked by mobile keyboard in react android
  • React-Native makes a text input box component implementation code

<<:  HTML web page creation tutorial Use iframe tags carefully

>>:  How much data can be stored in a MySQL table?

Recommend

How to install nginx on win10

Because the company asked me to build a WebServic...

Detailed explanation of four solutions to floating problems in CSS layout

1. Cause: The effect after the subbox is set to f...

The perfect solution to the Chinese garbled characters in mysql6.x under win7

1. Stop the MySQL service in the command line: ne...

The core process of nodejs processing tcp connection

A few days ago, I exchanged some knowledge about ...

How to view the IP address of the Docker container

I always thought that Docker had no IP address. I...

Detailed explanation of Vue3 sandbox mechanism

Table of contents Preface Browser compiled versio...

Detailed explanation of the practical use of HTML table layout

When is the table used? Nowadays, tables are gene...

Vue implements irregular screenshots

Table of contents Image capture through svg CSS p...

JS implements a simple counter

Use HTML CSS and JavaScript to implement a simple...

How to convert mysql bin-log log files to sql files

View mysqlbinlog version mysqlbinlog -V [--versio...

ul list tag design web page multi-column layout

I suddenly thought of this method when I was writi...

W3C Tutorial (16): Other W3C Activities

This section provides an overview of some other i...

Detailed explanation of MySQL date string timestamp conversion

The conversion between time, string and timestamp...

How does the composite index of MySQL take effect?

Table of contents background Understanding compos...

Analysis on the problem of data loss caused by forced refresh of vuex

vuex-persistedstate Core principle: store all vue...