Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form's normalize does not work correctly with InputNumber #34162

Closed
1 task done
pobch opened this issue Feb 22, 2022 · 9 comments
Closed
1 task done

Form's normalize does not work correctly with InputNumber #34162

pobch opened this issue Feb 22, 2022 · 9 comments

Comments

@pobch
Copy link
Contributor

pobch commented Feb 22, 2022

  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

Edit on CodeSandbox

Steps to reproduce

  1. Go to https://codesandbox.io/s/ancient-dawn-fgwk0?file=/src/App.js
  2. Type "987" in the input

What is expected?

Without blurring the input, it should display "0"

What is actually happening?

The input display "087"

Environment Info
antd 4.18.8
React 17.0.2
System Mac OS 12.1
Browser Firefox 96
@pobch pobch changed the title Form's normalize does not work correctly with InputNumber Form's normalize does not work correctly with InputNumber Feb 22, 2022
@pobch
Copy link
Contributor Author

pobch commented Feb 22, 2022

Same bug in the case of using setFieldsValue inside onChange #34023

@afc163
Copy link
Member

afc163 commented Feb 23, 2022

It is not a bug, InputNumber will show 0 when you are typing 9 which is start of 987.

@pobch
Copy link
Contributor Author

pobch commented Feb 23, 2022

@afc163 ...If the same logic applied, it should behave like this:

  1. Start to type 9 -> normalize is triggered -> output 0
  2. Continue to type 8(now it's 08) -> normalize is triggered -> output 0

But, right now 1. and 2. are behave differently.

Moreover, if I switch from InputNumber to Input, this bug does not occur.

@afc163
Copy link
Member

afc163 commented Feb 28, 2022

Try <InputNumber stringMode /> in this case.

@afc163 afc163 closed this as completed Feb 28, 2022
@pobch
Copy link
Contributor Author

pobch commented Feb 28, 2022

Using stringMode does NOT solve the issue. @afc163

@afc163 afc163 reopened this Feb 28, 2022
@afc163
Copy link
Member

afc163 commented Feb 28, 2022

I suggest that creating a <InputNumberLessThen5 /> instead of using normalize: https://codesandbox.io/s/goofy-silence-1cpwkc?file=/src/App.js

import { useState } from "react";
import { Form, InputNumber } from "antd";
import "antd/dist/antd.css";

const InputNumberLessThen5 = (props) => {
  const [value, setValue] = useState(props.value);
  const onChange = (v) => {
    if (v >= 5) {
      setValue(5);
    } else {
      setValue(v);
    }
  };
  return <InputNumber {...props} value={value} onChange={onChange} />;
};

export default function App() {
  const [form] = Form.useForm();

  return (
    <Form form={form}>
      <Form.Item label="Must less than 5" name="lessThan5">
        <InputNumberLessThen5 />
      </Form.Item>
    </Form>
  );
}

@afc163 afc163 closed this as completed Feb 28, 2022
@pobch
Copy link
Contributor Author

pobch commented Feb 28, 2022

I suggest that creating a <InputNumberLessThen5 /> instead of using normalize: https://codesandbox.io/s/goofy-silence-1cpwkc?file=/src/App.js

Your approach in codesandbox still NOT resolve the issue. If I type 987(3 keystrokes), the logic inside onChange will NOT be applied on 2nd and 3rd keystroke. @afc163

@pobch
Copy link
Contributor Author

pobch commented Feb 28, 2022

I think this issue is clearly a bug. The behavior of the first keystroke should not differ from other keystrokes, no matter we're using normalize prop or not.

@rfgonzalezweb
Copy link

rfgonzalezweb commented Jan 5, 2024

it won't work well with <DatePicker/> either 😿
https://stackblitz.com/edit/react-ydr6fz?file=demo.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants