import React from 'react';
import PropTypes from 'prop-types'
import { Input } from 'antd';
import "./style/inputNumber.css"

export default class InputNumber extends React.Component {
    constructor() {
        super();
        this.state = {
            setValue: null
        }
    }
    // 正整数(＋1，＋2，＋3)： positiveInt
    // 整数(正整数、0、负整数)： int  
    // 浮点数：float
    onKeyUpHandle(e) {
        let {type} = this.props;
        if (type === 'positiveInt') {
            this.setState({setValue: e.target.value.replace(/^((-\d+)|(0+)|(\.\d+))$/g,'')})
            // 将值传给父组件
            this.props.getData(e.target.value);
        } else if (type === 'int') {
            this.setState({setValue: e.target.value.replace(/\./g,'')})
            // 将值传给父组件
            this.props.getData(e.target.value);
        } else if (type === 'float') {
            this.setState({setValue: e.target.value.replace(/\/[^(-?\\d+)(\\.\\d+)?$]/g,'')})
            // 将值传给父组件
            this.props.getData(e.target.value);
        } else {
            this.setState({setValue: e.target.value.replace(/\D/g,'')})
            // 将值传给父组件
            this.props.getData(e.target.value);
        }
        
    }
    render() {
        const placeholder = this.props.placeholder ? this.props.placeholder : "";
        return(
            <Input 
                placeholder={placeholder}
                value={this.state.setValue}
                type="number"
                className="input-number"
                onChange={this.onKeyUpHandle.bind(this)}
                style={this.props.style}
            />
        )
    }
}

InputNumber.propTypes = {
    // 验证属性
    placeholder: PropTypes.string,
    type: PropTypes.string
  }