Skip to content

Commit c76f1fe

Browse files
refactor(useCounter): modify useCounter props structure (#222)
Co-authored-by: seungrodotlee <seungrodotlee@gmail.com>
1 parent a8374e4 commit c76f1fe

4 files changed

Lines changed: 42 additions & 63 deletions

File tree

src/hooks/useCounter/ko/useCounter.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55
## Interface
66

77
```ts
8-
function useCounter(options: UseCounterOptions): UseCounterReturn;
8+
function useCounter(
9+
initialValue?: number,
10+
options?: UseCounterOptions
11+
): UseCounterReturn;
912
```
1013

1114
### 파라미터
1215

1316
<Interface
14-
required
17+
name="initialValue"
18+
type="number"
19+
required={false}
20+
defaultValue="0"
21+
description="카운터의 초기값이에요. 기본값은 0이에요."
22+
/>
23+
24+
<Interface
1525
name="options"
1626
type="UseCounterOptions"
27+
required={false}
1728
description="카운터의 옵션이에요."
1829
:nested="[
19-
{
20-
name: 'options.initialValue',
21-
type: 'number',
22-
required: false,
23-
defaultValue: '0',
24-
description: '카운터의 초기값이에요. 기본값은 0이에요.',
25-
},
2630
{
2731
name: 'options.min',
2832
type: 'number',
@@ -94,8 +98,7 @@ function useCounter(options: UseCounterOptions): UseCounterReturn;
9498
import { useCounter } from 'react-simplikit';
9599

96100
function ShoppingCart() {
97-
const { count, increment, decrement, reset } = useCounter({
98-
initialValue: 1,
101+
const { count, increment, decrement, reset } = useCounter(1, {
99102
min: 1,
100103
max: 10,
101104
});

src/hooks/useCounter/useCounter.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55
## Interface
66

77
```ts
8-
function useCounter(options: UseCounterOptions): UseCounterReturn;
8+
function useCounter(
9+
initialValue?: number,
10+
options?: UseCounterOptions
11+
): UseCounterReturn;
912
```
1013

1114
### Parameters
1215

1316
<Interface
14-
required
17+
name="initialValue"
18+
type="number"
19+
required={false}
20+
defaultValue="0"
21+
description="Initial value for the counter. Defaults to 0."
22+
/>
23+
24+
<Interface
1525
name="options"
1626
type="UseCounterOptions"
27+
required={false}
1728
description="The options for the counter."
1829
:nested="[
19-
{
20-
name: 'options.initialValue',
21-
type: 'number',
22-
required: false,
23-
defaultValue: '0',
24-
description: 'Initial value for the counter. Defaults to 0.',
25-
},
2630
{
2731
name: 'options.min',
2832
type: 'number',
@@ -94,8 +98,7 @@ function useCounter(options: UseCounterOptions): UseCounterReturn;
9498
import { useCounter } from 'react-simplikit';
9599

96100
function ShoppingCart() {
97-
const { count, increment, decrement, reset } = useCounter({
98-
initialValue: 1,
101+
const { count, increment, decrement, reset } = useCounter(1, {
99102
min: 1,
100103
max: 10,
101104
});

src/hooks/useCounter/useCounter.spec.ts

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,13 @@ describe('useCounter', () => {
1111
});
1212

1313
it('should initialize with provided initial value', () => {
14-
const { result } = renderHook(() =>
15-
useCounter({
16-
initialValue: 10,
17-
})
18-
);
14+
const { result } = renderHook(() => useCounter(10));
1915

2016
expect(result.current.count).toBe(10);
2117
});
2218

2319
it('should increment the counter', () => {
24-
const { result } = renderHook(() =>
25-
useCounter({
26-
initialValue: 5,
27-
})
28-
);
20+
const { result } = renderHook(() => useCounter(5));
2921

3022
act(() => {
3123
result.current.increment();
@@ -35,11 +27,7 @@ describe('useCounter', () => {
3527
});
3628

3729
it('should decrement the counter', () => {
38-
const { result } = renderHook(() =>
39-
useCounter({
40-
initialValue: 5,
41-
})
42-
);
30+
const { result } = renderHook(() => useCounter(5));
4331

4432
act(() => {
4533
result.current.decrement();
@@ -49,11 +37,7 @@ describe('useCounter', () => {
4937
});
5038

5139
it('should reset the counter to initial value', () => {
52-
const { result } = renderHook(() =>
53-
useCounter({
54-
initialValue: 5,
55-
})
56-
);
40+
const { result } = renderHook(() => useCounter(5));
5741

5842
act(() => {
5943
result.current.increment();
@@ -71,8 +55,7 @@ describe('useCounter', () => {
7155

7256
it('should not go below minimum value', () => {
7357
const { result } = renderHook(() =>
74-
useCounter({
75-
initialValue: 5,
58+
useCounter(5, {
7659
min: 3,
7760
})
7861
);
@@ -88,8 +71,7 @@ describe('useCounter', () => {
8871

8972
it('should not go above maximum value', () => {
9073
const { result } = renderHook(() =>
91-
useCounter({
92-
initialValue: 5,
74+
useCounter(5, {
9375
max: 7,
9476
})
9577
);
@@ -105,8 +87,7 @@ describe('useCounter', () => {
10587

10688
it('should use the provided step value for increment and decrement', () => {
10789
const { result } = renderHook(() =>
108-
useCounter({
109-
initialValue: 5,
90+
useCounter(5, {
11091
step: 2,
11192
})
11293
);
@@ -126,17 +107,15 @@ describe('useCounter', () => {
126107

127108
it('should adjust initial value to match constraints', () => {
128109
const { result } = renderHook(() =>
129-
useCounter({
130-
initialValue: 1,
110+
useCounter(1, {
131111
min: 3,
132112
})
133113
);
134114

135115
expect(result.current.count).toBe(3);
136116

137117
const { result: result2 } = renderHook(() =>
138-
useCounter({
139-
initialValue: 10,
118+
useCounter(10, {
140119
max: 8,
141120
})
142121
);
@@ -146,7 +125,7 @@ describe('useCounter', () => {
146125

147126
it('should allow setting arbitrary value within constraints', () => {
148127
const { result } = renderHook(() =>
149-
useCounter({
128+
useCounter(0, {
150129
min: 3,
151130
max: 8,
152131
})
@@ -172,11 +151,7 @@ describe('useCounter', () => {
172151
});
173152

174153
it('should work with updater function for setCount', () => {
175-
const { result } = renderHook(() =>
176-
useCounter({
177-
initialValue: 5,
178-
})
179-
);
154+
const { result } = renderHook(() => useCounter(5));
180155

181156
act(() => {
182157
result.current.setCount(prev => prev + 3);

src/hooks/useCounter/useCounter.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useCallback, useState } from 'react';
22

33
type UseCounterOptions = {
4-
initialValue?: number;
54
min?: number;
65
max?: number;
76
step?: number;
@@ -20,8 +19,8 @@ type UseCounterReturn = {
2019
* `useCounter` is a React hook that manages a numeric counter state with increment, decrement, and reset capabilities.
2120
* Optionally, you can provide minimum and maximum values to constrain the counter's range.
2221
*
22+
* @param {number} [initialValue=0] - Initial value for the counter. Defaults to 0.
2323
* @param {UseCounterOptions} options - The options for the counter.
24-
* @param {number} [options.initialValue=0] - Initial value for the counter. Defaults to 0.
2524
* @param {number} [options.min] - Minimum value the counter can reach. If not provided, there is no lower limit.
2625
* @param {number} [options.max] - Maximum value the counter can reach. If not provided, there is no upper limit.
2726
* @param {number} [options.step=1] - Value to increment or decrement by. Defaults to 1.
@@ -37,8 +36,7 @@ type UseCounterReturn = {
3736
* import { useCounter } from 'react-simplikit';
3837
*
3938
* function ShoppingCart() {
40-
* const { count, increment, decrement, reset } = useCounter({
41-
* initialValue: 1,
39+
* const { count, increment, decrement, reset } = useCounter(1, {
4240
* min: 1,
4341
* max: 10,
4442
* });
@@ -53,7 +51,7 @@ type UseCounterReturn = {
5351
* );
5452
* }
5553
*/
56-
export function useCounter({ initialValue = 0, min, max, step = 1 }: UseCounterOptions = {}): UseCounterReturn {
54+
export function useCounter(initialValue = 0, { min, max, step = 1 }: UseCounterOptions = {}): UseCounterReturn {
5755
const validateValue = (value: number): number => {
5856
let validatedValue = value;
5957

0 commit comments

Comments
 (0)