skip to Main Content

i would like keep the value written in the input, but antd always modify the number

for example,
case 1: if write 3.5 should be 3.5
case 2: if write 3,5 should be 3,5

this is the code:


import React from 'react';
import { InputNumber } from 'antd';
const onChange = (value) => {
  console.log('changed', value);
};
const App = () => <InputNumber min={1} max={10} defaultValue={3} onChange={onChange} />;
export default App;

when i written 3.5 is keeping 3.5 its ok
but when i typed 3,5 the value is changing to 10

2

Answers


  1. I haven’t worked with antd but the issue you’re seeing could be due to the parsing of the number, where when you use a , the number is interpreted is such a way that the max is reached.

    You could try adding a parser function that interprets the result:

    const [value, setValue] = useState(3);
    
      const onChange = (newValue) => {
        const formattedValue = String(newValue).replace(',', '.');
        setValue(formattedValue);
      };
    
    

    This is a pretty loose example, there’s probably a better way to do this that better fits your use case.

    Login or Signup to reply.
  2. you can custom Input using onchange

    const App: React.FC = () => {
    const [value, setValue] = useState('');
    
    const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const inputValue = e.target.value;
    
    const cleanedValue = inputValue.replace(/[^0-9.,]/g, '');
    
    setValue(cleanedValue);
    };
    return <Input value={value} onChange={handleInputChange} />;
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search