react + antd +ts实现IP输入框

效果
在这里插入图片描述
使用方式
在这里插入图片描述

tsx

import { InputNumber, Input } from 'antd';
import React, { useState } from 'react';
import styles from './index.less';

interface PriceInputProps {
  value?: string;
  onChange?: (value: string) => void;
  disabled?: boolean;
}

const PriceInput: React.FC<PriceInputProps> = ({
  value = '0.0.0.0',
  onChange,
  disabled = false,
}) => {
  const [numberArr, setNumberArr] = useState<string[]>(value?.split('.') || []);

  const triggerChange = (changedValue: string) => {
    onChange?.(changedValue);
  };

  const onNumberChange = (evalue: string, type: number) => {
    const copy = [...numberArr];
    copy[type] = evalue;
    setNumberArr(copy);
    triggerChange(copy.join('.'));
  };
  return (
    <Input.Group compact className={!disabled ? styles.inputGroup : styles.inputGroup_disable}>
      <InputNumber
        style={{ width: '24%' }}
        disabled={disabled}
        controls={false}
        value={numberArr[0]}
        className={styles.self_input}
        onChange={(e) => onNumberChange(e, 0)}
        min={'0'}
        max={'255'}
      />
      <span className={styles.dot} />
      <InputNumber
        type={'number'}
        style={{ width: '24%' }}
        disabled={disabled}
        controls={false}
        value={numberArr[1]}
        className={styles.self_input}
        onChange={(e) => onNumberChange(e, 1)}
        min={'0'}
        max={'255'}
      />
      <span className={styles.dot} />
      <InputNumber
        type={'number'}
        style={{ width: '24%' }}
        disabled={disabled}
        controls={false}
        value={numberArr[2]}
        className={styles.self_input}
        onChange={(e) => onNumberChange(e, 2)}
        min={'0'}
        max={'255'}
      />
      <span className={styles.dot} />
      <InputNumber
        type={'number'}
        style={{ width: '24%' }}
        disabled={disabled}
        controls={false}
        value={numberArr[3]}
        className={styles.self_input}
        onChange={(e) => onNumberChange(e, 3)}
        min={'0'}
        max={'255'}
      />
    </Input.Group>
  );
};

export default PriceInput;

css

.inputGroup {
  text-align: center;
  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);
  }
  .dot {
    position: relative;
    top: 15px;
    z-index: 9;
    width: 3px;
    height: 3px;
    background-color: #000;
    border: 1px solid #000;
    border-radius: 3px;
    opacity: 0.5;
  }
}
.inputGroup_disable {
  text-align: center;
  background: #f5f5f5;
  border: 1px solid #d9d9d9;
  border-radius: 2px;
  transition: all 0.3s;
  .dot {
    position: relative;
    top: 15px;
    z-index: 9;
    width: 3px;
    height: 3px;
    background-color: #000;
    border: 1px solid #000;
    border-radius: 3px;
    opacity: 0.5;
  }
}
.self_input {
  border: none;
  outline: 0px;
  box-shadow: none;
  &:hover {
    box-shadow: none;
  }
  &::selection {
    box-shadow: none;
  }
  &:focus {
    box-shadow: none !important;
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值