Skip to content

Commit

Permalink
fix(useCounter): set and reset should also be limited (#2179)
Browse files Browse the repository at this point in the history
  • Loading branch information
iChengbo committed Oct 25, 2022
1 parent d4c3263 commit cb78f28
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion packages/shared/useCounter/index.test.ts
Expand Up @@ -37,7 +37,7 @@ describe('useCounter', () => {
})

it('should be update limited counter', () => {
const { count, inc, dec, get } = useCounter(1, { min: -2, max: 15 })
const { count, inc, dec, get, set, reset } = useCounter(1, { min: -2, max: 15 })

expect(count.value).toBe(1)
expect(get()).toBe(1)
Expand All @@ -53,5 +53,23 @@ describe('useCounter', () => {
dec(20)
expect(count.value).toBe(-2)
expect(get()).toBe(-2)
reset()
expect(count.value).toBe(1)
expect(get()).toBe(1)
set(20)
expect(count.value).toBe(15)
expect(get()).toBe(15)
set(-10)
expect(count.value).toBe(-2)
expect(get()).toBe(-2)
reset()
expect(count.value).toBe(1)
expect(get()).toBe(1)
reset(20)
expect(count.value).toBe(15)
expect(get()).toBe(15)
reset(-10)
expect(count.value).toBe(-2)
expect(get()).toBe(-2)
})
})
2 changes: 1 addition & 1 deletion packages/shared/useCounter/index.ts
Expand Up @@ -23,7 +23,7 @@ export function useCounter(initialValue = 0, options: UseCounterOptions = {}) {
const inc = (delta = 1) => count.value = Math.min(max, count.value + delta)
const dec = (delta = 1) => count.value = Math.max(min, count.value - delta)
const get = () => count.value
const set = (val: number) => (count.value = val)
const set = (val: number) => (count.value = Math.max(min, Math.min(max, val)))
const reset = (val = initialValue) => {
initialValue = val
return set(val)
Expand Down

0 comments on commit cb78f28

Please sign in to comment.