import { InputNumber } from "antd"
import { useEffect, useState } from "react"
import epsilon2 from './float'

export default (props) => {

  const { value, onChange, ...leftProps } = props

  const [money, setMoney] = useState(0)

  useEffect(() => {
    setMoney(epsilon2(value / 100))
  }, [value])

  const handleChange = (val) => {
    onChange(epsilon2(val * 100))
  }

  return (
    <InputNumber
      min='0'
      {...leftProps}
      value={money}
      onChange={handleChange}
      precision={2}
      formatter={value => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
      parser={value => value.replace(/\￥\s?|(,*)/g, '')}
    />
  )
}
