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

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

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/components/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Copy link
Member

@afc163 afc163 Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看上去直接 nextValue = Number(boundValue); 就行。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原本就是这样,存在一个问题,输入 '012' 的时候,nextValue = '012' ,Number(boundValue) = 12,最后展示的时候就是 12 了

}
if (nextValue !== value) {
setValue(nextValue)
Expand Down
18 changes: 18 additions & 0 deletions src/components/input/tests/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})