Skip to content

Commit 6bf344b

Browse files
committed
feat(useCounter): add useCounter hook
1 parent 22aaa86 commit 6bf344b

6 files changed

Lines changed: 529 additions & 0 deletions

File tree

src/hooks/useCounter/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { useCounter } from './useCounter.ts';
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# useCounter
2+
3+
`useCounter`는 증가, 감소, 초기화 기능을 갖춘 숫자형 카운터 상태를 관리하는 리액트 훅이에요. 선택적으로 최소값과 최대값을 제공하여 카운터의 범위를 제한할 수 있어요.
4+
5+
## 인터페이스
6+
7+
```ts
8+
function useCounter(options?: UseCounterOptions): UseCounterReturn;
9+
10+
type UseCounterOptions = {
11+
initialValue?: number;
12+
min?: number;
13+
max?: number;
14+
step?: number;
15+
};
16+
17+
type UseCounterReturn = {
18+
count: number;
19+
increment: () => void;
20+
decrement: () => void;
21+
reset: () => void;
22+
setCount: (value: number | ((prev: number) => number)) => void;
23+
};
24+
```
25+
26+
### 파라미터
27+
28+
<Interface
29+
name="options"
30+
type="UseCounterOptions"
31+
description="카운터의 선택적 설정이에요."
32+
:nested="[
33+
{
34+
name: 'initialValue',
35+
type: 'number',
36+
description: '카운터의 초기값. 기본값은 0이에요.',
37+
},
38+
{
39+
name: 'min',
40+
type: 'number',
41+
description: '카운터가 도달할 수 있는 최소값. 제공되지 않으면 하한이 없어요.',
42+
},
43+
{
44+
name: 'max',
45+
type: 'number',
46+
description: '카운터가 도달할 수 있는 최대값. 제공되지 않으면 상한이 없어요.',
47+
},
48+
{
49+
name: 'step',
50+
type: 'number',
51+
description: '증가 또는 감소할 값. 기본값은 1이에요.',
52+
}
53+
]"
54+
/>
55+
56+
### 반환 값
57+
58+
<Interface
59+
name=""
60+
type="UseCounterReturn"
61+
description="카운트 값과 제어 함수를 포함하는 객체에요."
62+
:nested="[
63+
{
64+
name: 'count',
65+
type: 'number',
66+
description: '현재 카운트 값이에요.',
67+
},
68+
{
69+
name: 'increment',
70+
type: '() => void',
71+
description: '단계 크기만큼 카운터 증가해요.',
72+
},
73+
{
74+
name: 'decrement',
75+
type: '() => void',
76+
description: '단계 크기만큼 카운터 감소해요.',
77+
},
78+
{
79+
name: 'reset',
80+
type: '() => void',
81+
description: '카운터를 초기값으로 재설정해요.',
82+
},
83+
{
84+
name: 'setCount',
85+
type: '(value: number | ((prev: number) => number)) => void',
86+
description: '카운터를 제약 조건 내에서 특정 값으로 설정해요.',
87+
}
88+
]"
89+
/>
90+
91+
## 예시
92+
93+
```tsx
94+
import { useCounter } from 'react-simplikit';
95+
96+
function ShoppingCart() {
97+
const { count, increment, decrement, reset } = useCounter({
98+
initialValue: 1,
99+
min: 1,
100+
max: 10,
101+
});
102+
103+
return (
104+
<div>
105+
<span>수량: {count}</span>
106+
<button type="button" onClick={decrement}>-</button>
107+
<button type="button" onClick={increment}>+</button>
108+
<button type="button" onClick={reset}>초기화</button>
109+
</div>
110+
);
111+
}
112+
```
113+
114+
## 제약 조건
115+
116+
이 훅은 카운터가 지정된 범위 내에서 유지되도록 자동으로 보장해요:
117+
118+
- 값이 `max`보다 커지면 자동으로 `max` 값으로 제한돼요.
119+
- 값이 `min`보다 작아지면 자동으로 `min` 값으로 제한돼요.
120+
- `setCount`를 사용할 때 범위를 벗어나는 값은 자동으로 가장 가까운 경계값으로 조정돼요.

src/hooks/useCounter/useCounter.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# useCounter
2+
3+
`useCounter` is a React hook that manages a numeric counter state with increment, decrement, and reset capabilities. Optionally, you can provide minimum and maximum values to constrain the counter's range.
4+
5+
## Interface
6+
7+
```ts
8+
function useCounter(options?: UseCounterOptions): UseCounterReturn;
9+
10+
type UseCounterOptions = {
11+
initialValue?: number;
12+
min?: number;
13+
max?: number;
14+
step?: number;
15+
};
16+
17+
type UseCounterReturn = {
18+
count: number;
19+
increment: () => void;
20+
decrement: () => void;
21+
reset: () => void;
22+
setCount: (value: number | ((prev: number) => number)) => void;
23+
};
24+
```
25+
26+
### Parameters
27+
28+
<Interface
29+
name="options"
30+
type="UseCounterOptions"
31+
description="Optional configuration for the counter."
32+
:nested="[
33+
{
34+
name: 'initialValue',
35+
type: 'number',
36+
description: 'Initial value for the counter. Defaults to 0.',
37+
},
38+
{
39+
name: 'min',
40+
type: 'number',
41+
description: 'Minimum value the counter can reach. If not provided, there is no lower limit.',
42+
},
43+
{
44+
name: 'max',
45+
type: 'number',
46+
description: 'Maximum value the counter can reach. If not provided, there is no upper limit.',
47+
},
48+
{
49+
name: 'step',
50+
type: 'number',
51+
description: 'Value to increment or decrement by. Defaults to 1.',
52+
}
53+
]"
54+
/>
55+
56+
### Return Value
57+
58+
<Interface
59+
name=""
60+
type="UseCounterReturn"
61+
description="An object with count value and control functions."
62+
:nested="[
63+
{
64+
name: 'count',
65+
type: 'number',
66+
description: 'Current count value.',
67+
},
68+
{
69+
name: 'increment',
70+
type: '() => void',
71+
description: 'Increment the counter by the step amount.',
72+
},
73+
{
74+
name: 'decrement',
75+
type: '() => void',
76+
description: 'Decrement the counter by the step amount.',
77+
},
78+
{
79+
name: 'reset',
80+
type: '() => void',
81+
description: 'Reset the counter to its initial value.',
82+
},
83+
{
84+
name: 'setCount',
85+
type: '(value: number | ((prev: number) => number)) => void',
86+
description: 'Set the counter to a specific value within constraints.',
87+
}
88+
]"
89+
/>
90+
91+
## Example
92+
93+
```tsx
94+
import { useCounter } from 'react-simplikit';
95+
96+
function ShoppingCart() {
97+
const { count, increment, decrement, reset } = useCounter({
98+
initialValue: 1,
99+
min: 1,
100+
max: 10,
101+
});
102+
103+
return (
104+
<div>
105+
<span>Quantity: {count}</span>
106+
<button type="button" onClick={decrement}>-</button>
107+
<button type="button" onClick={increment}>+</button>
108+
<button type="button" onClick={reset}>Reset</button>
109+
</div>
110+
);
111+
}
112+
```
113+
114+
## Constraints
115+
116+
The hook automatically ensures that the counter stays within the specified bounds:
117+
118+
- When incrementing beyond `max`, the value will stay at `max`
119+
- When decrementing below `min`, the value will stay at `min`
120+
- When using `setCount`, any value outside the bounds will be adjusted to the nearest boundary

0 commit comments

Comments
 (0)