Skip to content

Commit

Permalink
fix(Input): fix the display issue of numbers starting with 0
Browse files Browse the repository at this point in the history
  • Loading branch information
miracles1919 committed Jun 26, 2023
1 parent 234c083 commit 13a8201
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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
}
}
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')
})
})

0 comments on commit 13a8201

Please sign in to comment.