Skip to content

Commit

Permalink
fix(number-input): Fix e character logic. (#6678)
Browse files Browse the repository at this point in the history
  • Loading branch information
anubra266 committed Sep 20, 2022
1 parent 2296878 commit 82edc72
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-dolphins-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chakra-ui/number-input": patch
---

Fix number input e character
26 changes: 12 additions & 14 deletions packages/components/number-input/src/use-number-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,24 +380,22 @@ export function useNumberInput(props: UseNumberInputProps = {}) {
*/
const validateAndClamp = useCallback(() => {
let next = counter.value as string | number
if (counter.value === "") return

if (next === "") return
const valueStartsWithE = /^[eE]/.test(counter.value.toString())

if (counter.valueAsNumber < min) {
next = min
}
if (valueStartsWithE) {
counter.setValue("")
} else {
if (counter.valueAsNumber < min) {
next = min
}
if (counter.valueAsNumber > max) {
next = max
}

if (counter.valueAsNumber > max) {
next = max
counter.cast(next)
}

/**
* `counter.cast` does 2 things:
*
* - sanitize the value by using parseFloat and some Regex
* - used to round value to computed precision or decimal points
*/
counter.cast(next)
}, [counter, max, min])

const onInputBlur = useCallback(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/components/number-input/tests/number-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ test("should derive values from surrounding FormControl", () => {
expect(onBlur).toHaveBeenCalled()
})

test("should fallback to min if `e` is typed", async () => {
test("should reset value if `e` is typed", async () => {
const { getByTestId, user } = renderComponent({ max: 30, min: 1 })
const input = getByTestId("input")
await user.type(input, "e")
// value is beyond max, so it should reset to `max`
fireEvent.blur(input)
expect(input).toHaveValue("1")
expect(input).toHaveValue("")
})

1 comment on commit 82edc72

@vercel
Copy link

@vercel vercel bot commented on 82edc72 Sep 20, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.