import type { InputNumberProps as AntInputNumberProps } from 'antd';
import { Form, InputNumber as AntInputNumber } from 'antd';
import type { FC } from 'react';
import type { BaseElementProps } from './types';

const { Item: FormItem } = Form;

type InputNumberProps = AntInputNumberProps<number>;

export default (function InputNumber({
  setValue,
  status,
  isValid,
  help,
  ...props
}: BaseElementProps<number>) {
  function handleChange(value: number | null) {
    if (value) {
      setValue(value);
    } else {
      setValue(0);
    }
  }

  return (
    <FormItem hasFeedback validateStatus={isValid === false ? 'error' : ''} help={help}>
      <AntInputNumber
        onChange={handleChange}
        disabled={status === 'Disabled'}
        style={{
          width: '100%',
        }}
        {...props}
      />
    </FormItem>
  );
} as FC<InputNumberProps>);
