import React from 'react';
import { InputNumber } from 'antd';
import styles from './styles.less';
import { ApolloNumberProps } from '../editInterface';
import { antiAssign } from '../../../../utils/utils';
import { floatNumberReg, intNumberReg } from '../../../../utils/reg';

export const ApolloNumber = (props: ApolloNumberProps) => {
    const { onChange, columnConfig }: any = props;
    const { columnAttrObj = {} } = columnConfig;
    const { precision, dataType } = columnAttrObj;
    const selfProps = antiAssign(props, ['columnConfig', 'onChange']);
    const changeValue = (newValue: any) => {
        if (dataType === 'INT') {
            if (!intNumberReg.test(newValue)) {
                return false;
            }
        }
        if (dataType === 'DECIMAL') {
            if (!floatNumberReg.test(newValue)) {
                return false;
            }
        }
        if (typeof onChange === 'function') {
            onChange(newValue);
        }
    };
    const { onEmitChange, ...o } = selfProps;
    return (
        <InputNumber
            className={styles.number}
            {...o}
            precision={precision}
            onChange={changeValue}
        />
    );
};
