Repository: ant-design/ant-design-mobile
Pull request #6217 (2023-06-26 05:32:20)

fix(Input): fix the display issue of numbers starting with 0
#6175 

github-actions[bot]: PR preview has been successfully built and deployed to https://antd-mobile-preview-pr-6217.surge.sh
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/ant-design/ant-design-mobile/pull/6217?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ant-design) Report
Patch coverage: **`100.00`**% and no project coverage change.
> Comparison is base [(`234c083`)](https://app.codecov.io/gh/ant-design/ant-design-mobile/commit/234c083bb4dcea9be1f606af953a0250c75a3401?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ant-design) 90.75% compared to head [(`13a8201`)](https://app.codecov.io/gh/ant-design/ant-design-mobile/pull/6217?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ant-design) 90.75%.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master    #6217   +/-   ##
=======================================
  Coverage   90.75%   90.75%           
=======================================
  Files         304      304           
  Lines        6597     6598    +1     
  Branches     1644     1645    +1     
=======================================
+ Hits         5987     5988    +1     
  Misses        562      562           
  Partials       48       48           
```


| [Impacted Files](https://app.codecov.io/gh/ant-design/ant-design-mobile/pull/6217?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ant-design) | Coverage Δ | |
|---|---|---|
| [src/components/input/input.tsx](https://app.codecov.io/gh/ant-design/ant-design-mobile/pull/6217?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ant-design#diff-c3JjL2NvbXBvbmVudHMvaW5wdXQvaW5wdXQudHN4) | `97.46% <100.00%> (+0.03%)` | :arrow_up: |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/ant-design/ant-design-mobile/pull/6217?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ant-design).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ant-design).


Patch:
diff --git a/src/components/input/input.tsx b/src/components/input/input.tsx
index 9d2a9a315..0ab19f3d0 100644
--- a/src/components/input/input.tsx
+++ b/src/components/input/input.tsx
@@ -121,9 +121,13 @@ export const Input = forwardRef<InputRef, InputProps>((p, ref) => {
   function checkValue() {
     let nextValue = value
     if (props.type === 'number') {
-      nextValue =
+      const boundValue =
         nextValue &&
         bound(parseFloat(nextValue), props.min, props.max).toString()
+      // fix the display issue of numbers starting with 0
+      if (Number(nextValue) !== Number(boundValue)) {
+        nextValue = boundValue
+      }
     }
     if (nextValue !== value) {
       setValue(nextValue)


Test patch:
diff --git a/src/components/input/tests/input.test.tsx b/src/components/input/tests/input.test.tsx
index c6373bee4..46fee2229 100644
--- a/src/components/input/tests/input.test.tsx
+++ b/src/components/input/tests/input.test.tsx
@@ -190,4 +190,22 @@ describe('Input', () => {
     })
     expect(ref.current?.nativeElement?.value).toBe('')
   })
+
+  test('numbers that start with 0 should be work', () => {
+    const ref = createRef<InputRef>()
+    render(<Input type='number' ref={ref} />)
+    const input = document.querySelector('input')!
+    fireEvent.change(input, {
+      target: { value: '012' },
+    })
+    // input.blur()
+    act(() => {
+      input.focus()
+    })
+    act(() => {
+      input.blur()
+    })
+
+    expect(input.value).toBe('012')
+  })
 })