Skip to content

Commit 7b98379

Browse files
committed
feat(core/hooks): add 'useHistory' hook
1 parent 2a901bb commit 7b98379

6 files changed

Lines changed: 652 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { useHistory } from './useHistory.ts';
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# useHistory
2+
3+
`useHistory`는 값의 변경 이력을 추적하고 undo/redo 기능을 제공하는 리액트 훅이에요. `setValue`를 호출할 때마다 값이 이력 스택에 기록되고, `undo``redo`를 사용해 이전 값을 탐색할 수 있어요.
4+
5+
## 인터페이스
6+
7+
```ts
8+
function useHistory<T>(
9+
initialValue: T,
10+
options?: { capacity?: number }
11+
): {
12+
value: T;
13+
setValue: (value: T) => void;
14+
history: readonly T[];
15+
pointer: number;
16+
undo: () => void;
17+
redo: () => void;
18+
canUndo: boolean;
19+
canRedo: boolean;
20+
clear: () => void;
21+
};
22+
```
23+
24+
### 파라미터
25+
26+
<Interface
27+
name="initialValue"
28+
type="T"
29+
description="추적할 초기 값이에요."
30+
/>
31+
32+
<Interface
33+
name="options"
34+
type="{ capacity?: number }"
35+
description="설정 옵션이에요."
36+
:nested="[
37+
{
38+
name: 'capacity',
39+
type: 'number',
40+
required: false,
41+
description: '유지할 이력의 최대 개수예요. 초과하면 가장 오래된 항목이 제거돼요. 기본값은 무제한이에요.',
42+
},
43+
]"
44+
/>
45+
46+
### 반환 값
47+
48+
<Interface
49+
name=""
50+
type="object"
51+
description="다음 값들을 포함하는 객체예요:"
52+
:nested="[
53+
{
54+
name: 'value',
55+
type: 'T',
56+
required: false,
57+
description: '현재 값이에요.',
58+
},
59+
{
60+
name: 'setValue',
61+
type: '(value: T) => void',
62+
required: false,
63+
description: '새로운 값을 설정하고 이력에 기록하는 함수예요.',
64+
},
65+
{
66+
name: 'history',
67+
type: 'readonly T[]',
68+
required: false,
69+
description: '기록된 모든 값의 읽기 전용 배열이에요.',
70+
},
71+
{
72+
name: 'pointer',
73+
type: 'number',
74+
required: false,
75+
description: '이력 스택에서 현재 위치예요.',
76+
},
77+
{
78+
name: 'undo',
79+
type: '() => void',
80+
required: false,
81+
description: '이전 값으로 되돌리는 함수예요.',
82+
},
83+
{
84+
name: 'redo',
85+
type: '() => void',
86+
required: false,
87+
description: '다음 값으로 이동하는 함수예요.',
88+
},
89+
{
90+
name: 'canUndo',
91+
type: 'boolean',
92+
required: false,
93+
description: '되돌릴 이전 값이 있는지 여부예요.',
94+
},
95+
{
96+
name: 'canRedo',
97+
type: 'boolean',
98+
required: false,
99+
description: '앞으로 이동할 다음 값이 있는지 여부예요.',
100+
},
101+
{
102+
name: 'clear',
103+
type: '() => void',
104+
required: false,
105+
description: '이력을 초기화하고 현재 값으로 리셋하는 함수예요.',
106+
},
107+
]"
108+
/>
109+
110+
## 예시
111+
112+
```tsx
113+
function TextEditor() {
114+
const { value, setValue, undo, redo, canUndo, canRedo } = useHistory('');
115+
116+
return (
117+
<div>
118+
<input value={value} onChange={e => setValue(e.target.value)} />
119+
<button onClick={undo} disabled={!canUndo}>
120+
Undo
121+
</button>
122+
<button onClick={redo} disabled={!canRedo}>
123+
Redo
124+
</button>
125+
</div>
126+
);
127+
}
128+
```
129+
130+
```tsx
131+
function Counter() {
132+
const { value, setValue, undo, canUndo, clear } = useHistory(0, {
133+
capacity: 10,
134+
});
135+
136+
return (
137+
<div>
138+
<p>Count: {value}</p>
139+
<button onClick={() => setValue(value + 1)}>Increment</button>
140+
<button onClick={undo} disabled={!canUndo}>
141+
Undo
142+
</button>
143+
<button onClick={clear}>Clear History</button>
144+
</div>
145+
);
146+
}
147+
```
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# useHistory
2+
3+
`useHistory` is a React hook that tracks the change history of a value and provides undo/redo functionality. Each time `setValue` is called, the value is recorded in the history stack, and you can navigate through past values using `undo` and `redo`.
4+
5+
## Interface
6+
7+
```ts
8+
function useHistory<T>(
9+
initialValue: T,
10+
options?: { capacity?: number }
11+
): {
12+
value: T;
13+
setValue: (value: T) => void;
14+
history: readonly T[];
15+
pointer: number;
16+
undo: () => void;
17+
redo: () => void;
18+
canUndo: boolean;
19+
canRedo: boolean;
20+
clear: () => void;
21+
};
22+
```
23+
24+
### Parameters
25+
26+
<Interface
27+
name="initialValue"
28+
type="T"
29+
description="The initial value to track."
30+
/>
31+
32+
<Interface
33+
name="options"
34+
type="{ capacity?: number }"
35+
description="Configuration options."
36+
:nested="[
37+
{
38+
name: 'capacity',
39+
type: 'number',
40+
required: false,
41+
description: 'The maximum number of history entries to keep. When exceeded, the oldest entry is removed. Defaults to unlimited.',
42+
},
43+
]"
44+
/>
45+
46+
### Return Value
47+
48+
<Interface
49+
name=""
50+
type="object"
51+
description="An object containing:"
52+
:nested="[
53+
{
54+
name: 'value',
55+
type: 'T',
56+
required: false,
57+
description: 'The current value.',
58+
},
59+
{
60+
name: 'setValue',
61+
type: '(value: T) => void',
62+
required: false,
63+
description: 'A function to set a new value and record it in the history.',
64+
},
65+
{
66+
name: 'history',
67+
type: 'readonly T[]',
68+
required: false,
69+
description: 'A readonly array of all recorded values.',
70+
},
71+
{
72+
name: 'pointer',
73+
type: 'number',
74+
required: false,
75+
description: 'The current position in the history stack.',
76+
},
77+
{
78+
name: 'undo',
79+
type: '() => void',
80+
required: false,
81+
description: 'A function to revert to the previous value.',
82+
},
83+
{
84+
name: 'redo',
85+
type: '() => void',
86+
required: false,
87+
description: 'A function to move forward to the next value.',
88+
},
89+
{
90+
name: 'canUndo',
91+
type: 'boolean',
92+
required: false,
93+
description: 'Whether there is a previous value to revert to.',
94+
},
95+
{
96+
name: 'canRedo',
97+
type: 'boolean',
98+
required: false,
99+
description: 'Whether there is a next value to move forward to.',
100+
},
101+
{
102+
name: 'clear',
103+
type: '() => void',
104+
required: false,
105+
description: 'A function to clear the history and reset to the current value.',
106+
},
107+
]"
108+
/>
109+
110+
## Example
111+
112+
```tsx
113+
function TextEditor() {
114+
const { value, setValue, undo, redo, canUndo, canRedo } = useHistory('');
115+
116+
return (
117+
<div>
118+
<input value={value} onChange={e => setValue(e.target.value)} />
119+
<button onClick={undo} disabled={!canUndo}>
120+
Undo
121+
</button>
122+
<button onClick={redo} disabled={!canRedo}>
123+
Redo
124+
</button>
125+
</div>
126+
);
127+
}
128+
```
129+
130+
```tsx
131+
function Counter() {
132+
const { value, setValue, undo, canUndo, clear } = useHistory(0, {
133+
capacity: 10,
134+
});
135+
136+
return (
137+
<div>
138+
<p>Count: {value}</p>
139+
<button onClick={() => setValue(value + 1)}>Increment</button>
140+
<button onClick={undo} disabled={!canUndo}>
141+
Undo
142+
</button>
143+
<button onClick={clear}>Clear History</button>
144+
</div>
145+
);
146+
}
147+
```

0 commit comments

Comments
 (0)