Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/hooks/useCounter/ko/useCounter.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@
## Interface

```ts
function useCounter(options: UseCounterOptions): UseCounterReturn;
function useCounter(
initialValue?: number,
options?: UseCounterOptions
): UseCounterReturn;
```

### 파라미터

<Interface
required
name="initialValue"
type="number"
required={false}
defaultValue="0"
description="카운터의 초기값이에요. 기본값은 0이에요."
/>

<Interface
name="options"
type="UseCounterOptions"
required={false}
description="카운터의 옵션이에요."
:nested="[
{
name: 'options.initialValue',
type: 'number',
required: false,
defaultValue: '0',
description: '카운터의 초기값이에요. 기본값은 0이에요.',
},
{
name: 'options.min',
type: 'number',
Expand Down Expand Up @@ -94,8 +98,7 @@ function useCounter(options: UseCounterOptions): UseCounterReturn;
import { useCounter } from 'react-simplikit';

function ShoppingCart() {
const { count, increment, decrement, reset } = useCounter({
initialValue: 1,
const { count, increment, decrement, reset } = useCounter(1, {
min: 1,
max: 10,
});
Expand Down
25 changes: 14 additions & 11 deletions src/hooks/useCounter/useCounter.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@
## Interface

```ts
function useCounter(options: UseCounterOptions): UseCounterReturn;
function useCounter(
initialValue?: number,
options?: UseCounterOptions
): UseCounterReturn;
```

### Parameters

<Interface
required
name="initialValue"
type="number"
required={false}
defaultValue="0"
description="Initial value for the counter. Defaults to 0."
/>

<Interface
name="options"
type="UseCounterOptions"
required={false}
description="The options for the counter."
:nested="[
{
name: 'options.initialValue',
type: 'number',
required: false,
defaultValue: '0',
description: 'Initial value for the counter. Defaults to 0.',
},
{
name: 'options.min',
type: 'number',
Expand Down Expand Up @@ -94,8 +98,7 @@ function useCounter(options: UseCounterOptions): UseCounterReturn;
import { useCounter } from 'react-simplikit';

function ShoppingCart() {
const { count, increment, decrement, reset } = useCounter({
initialValue: 1,
const { count, increment, decrement, reset } = useCounter(1, {
min: 1,
max: 10,
});
Expand Down
47 changes: 11 additions & 36 deletions src/hooks/useCounter/useCounter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,13 @@ describe('useCounter', () => {
});

it('should initialize with provided initial value', () => {
const { result } = renderHook(() =>
useCounter({
initialValue: 10,
})
);
const { result } = renderHook(() => useCounter(10));

expect(result.current.count).toBe(10);
});

it('should increment the counter', () => {
const { result } = renderHook(() =>
useCounter({
initialValue: 5,
})
);
const { result } = renderHook(() => useCounter(5));

act(() => {
result.current.increment();
Expand All @@ -35,11 +27,7 @@ describe('useCounter', () => {
});

it('should decrement the counter', () => {
const { result } = renderHook(() =>
useCounter({
initialValue: 5,
})
);
const { result } = renderHook(() => useCounter(5));

act(() => {
result.current.decrement();
Expand All @@ -49,11 +37,7 @@ describe('useCounter', () => {
});

it('should reset the counter to initial value', () => {
const { result } = renderHook(() =>
useCounter({
initialValue: 5,
})
);
const { result } = renderHook(() => useCounter(5));

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

it('should not go below minimum value', () => {
const { result } = renderHook(() =>
useCounter({
initialValue: 5,
useCounter(5, {
min: 3,
})
);
Expand All @@ -88,8 +71,7 @@ describe('useCounter', () => {

it('should not go above maximum value', () => {
const { result } = renderHook(() =>
useCounter({
initialValue: 5,
useCounter(5, {
max: 7,
})
);
Expand All @@ -105,8 +87,7 @@ describe('useCounter', () => {

it('should use the provided step value for increment and decrement', () => {
const { result } = renderHook(() =>
useCounter({
initialValue: 5,
useCounter(5, {
step: 2,
})
);
Expand All @@ -126,17 +107,15 @@ describe('useCounter', () => {

it('should adjust initial value to match constraints', () => {
const { result } = renderHook(() =>
useCounter({
initialValue: 1,
useCounter(1, {
min: 3,
})
);

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

const { result: result2 } = renderHook(() =>
useCounter({
initialValue: 10,
useCounter(10, {
max: 8,
})
);
Expand All @@ -146,7 +125,7 @@ describe('useCounter', () => {

it('should allow setting arbitrary value within constraints', () => {
const { result } = renderHook(() =>
useCounter({
useCounter(0, {
min: 3,
max: 8,
})
Expand All @@ -172,11 +151,7 @@ describe('useCounter', () => {
});

it('should work with updater function for setCount', () => {
const { result } = renderHook(() =>
useCounter({
initialValue: 5,
})
);
const { result } = renderHook(() => useCounter(5));

act(() => {
result.current.setCount(prev => prev + 3);
Expand Down
8 changes: 3 additions & 5 deletions src/hooks/useCounter/useCounter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useCallback, useState } from 'react';

type UseCounterOptions = {
initialValue?: number;
min?: number;
max?: number;
step?: number;
Expand All @@ -20,8 +19,8 @@ type UseCounterReturn = {
* `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.
*
* @param {number} [initialValue=0] - Initial value for the counter. Defaults to 0.
* @param {UseCounterOptions} options - The options for the counter.
* @param {number} [options.initialValue=0] - Initial value for the counter. Defaults to 0.
* @param {number} [options.min] - Minimum value the counter can reach. If not provided, there is no lower limit.
* @param {number} [options.max] - Maximum value the counter can reach. If not provided, there is no upper limit.
* @param {number} [options.step=1] - Value to increment or decrement by. Defaults to 1.
Expand All @@ -37,8 +36,7 @@ type UseCounterReturn = {
* import { useCounter } from 'react-simplikit';
*
* function ShoppingCart() {
* const { count, increment, decrement, reset } = useCounter({
* initialValue: 1,
* const { count, increment, decrement, reset } = useCounter(1, {
* min: 1,
* max: 10,
* });
Expand All @@ -53,7 +51,7 @@ type UseCounterReturn = {
* );
* }
*/
export function useCounter({ initialValue = 0, min, max, step = 1 }: UseCounterOptions = {}): UseCounterReturn {
export function useCounter(initialValue = 0, { min, max, step = 1 }: UseCounterOptions = {}): UseCounterReturn {
const validateValue = (value: number): number => {
let validatedValue = value;

Expand Down
Loading