Skip to content

Commit

Permalink
fix: use step increment precision when precisionProp is not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
markdowney committed Aug 15, 2023
1 parent f5b1a35 commit 2a7e4ed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/clever-tips-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@chakra-ui/number-input": patch
"@chakra-ui/counter": patch
---

Fixed increment/decrement of 0.1 \* step when precision is not specified.
17 changes: 11 additions & 6 deletions packages/components/counter/src/use-counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ export function useCounter(props: UseCounterProps = {}) {

// Function to clamp the value and round it to the precision
const clamp = useCallback(
(value: number) => {
(value: number, precisionToClamp = precision) => {
let nextValue = value

if (keepWithinRange) {
nextValue = clampValue(nextValue, min, max)
}

return toPrecision(nextValue, precision)
return toPrecision(nextValue, precisionToClamp)
},
[precision, keepWithinRange, max, min],
)
Expand All @@ -124,10 +124,14 @@ export function useCounter(props: UseCounterProps = {}) {
next = parse(value) + step
}

next = clamp(next as number)
/**
* Get the precision using step value passed to increment function
*/
const precision = precisionProp ?? getDecimalPlaces(parse(value), step)
next = clamp(next as number, precision)
update(next)
},
[clamp, stepProp, update, value],
[clamp, stepProp, update, value, precisionProp],
)

const decrement = useCallback(
Expand All @@ -141,10 +145,11 @@ export function useCounter(props: UseCounterProps = {}) {
next = parse(value) - step
}

next = clamp(next as number)
const precision = precisionProp ?? getDecimalPlaces(parse(value), step)
next = clamp(next as number, precision)
update(next)
},
[clamp, stepProp, update, value],
[clamp, stepProp, update, value, precisionProp],
)

const reset = useCallback(() => {
Expand Down
19 changes: 19 additions & 0 deletions packages/components/number-input/tests/number-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ test("should increase/decrease by 0.1*step on ctrl+Arrow", async () => {
expect(input).toHaveValue("0.00")
})

test("should increment/decrement properly without precision value", async () => {
const { getByTestId, user } = renderComponent({
defaultValue: 0,
step: 1,
})

const input = getByTestId("input")

await user.type(input, "[ArrowUp]")
expect(input).toHaveValue("1")
await user.keyboard("[ControlLeft>][ArrowUp][/ControlLeft]")
expect(input).toHaveValue("1.1")

await user.keyboard("[ControlLeft>][ArrowDown][/ControlLeft]")
expect(input).toHaveValue("1.0")
await user.keyboard("[ArrowDown]")
expect(input).toHaveValue("0")
})

test("should behave properly with precision value", async () => {
const { getByTestId, user } = renderComponent({
defaultValue: 0,
Expand Down

0 comments on commit 2a7e4ed

Please sign in to comment.