Skip to content

Commit fc28760

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

6 files changed

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

src/hooks/useCounter/useCounter.md

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

0 commit comments

Comments
 (0)