index.jsx 696B

1234567891011121314151617181920212223242526272829303132
  1. import { InputNumber } from "antd"
  2. import { useEffect, useState } from "react"
  3. const epsilonN = N => num => Math.round( num * N + Number.EPSILON ) / N;
  4. const epsilon2 = epsilonN(1e2);
  5. export default (props) => {
  6. const { value, onChange, ...leftProps } = props
  7. const [percent, setPercent] = useState(0)
  8. useEffect(() => {
  9. setPercent(epsilon2(value * 100))
  10. }, [value])
  11. const handleChange = (val) => {
  12. onChange(epsilon2(val / 100))
  13. }
  14. return (
  15. <InputNumber
  16. min='0'
  17. {...leftProps}
  18. value={percent}
  19. onChange={handleChange}
  20. precision={2}
  21. formatter={value => `${value} %`}
  22. parser={value => value.replace(/\s?%/g, '')}
  23. />
  24. )
  25. }