Skip to content

Commit 8dc3260

Browse files
committed
[ADD] useMap and useState hooks
1 parent 153c1e4 commit 8dc3260

20 files changed

Lines changed: 242 additions & 86 deletions

apps/react-tools-demo/scripts/generateMarkdown.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "node:path";
22
import fs from 'node:fs/promises';
33
import {URL} from 'url';
4+
import process from "node:process";
45

56
const __dirname = new URL('.', import.meta.url).pathname;
67

@@ -40,7 +41,7 @@ function splitType(string){
4041
let newCode = [];
4142
let last = "";
4243
for (let part of string){
43-
if (last.split("(").length == last.split(")").length && last.split("[").length == last.split("]").length && last.split("{").length == last.split("}").length){
44+
if (last.split("<").length === last.split(">").length && last.split("(").length === last.split(")").length && last.split("[").length === last.split("]").length && last.split("{").length === last.split("}").length){
4445
last = part;
4546
newCode.push(part);
4647
}
@@ -374,6 +375,7 @@ async function generateMarkDown() {
374375
await deleteAllFiles(pathMarkdownDir);
375376
await generateUtilsMarkDown();
376377
await generateComponentsMarkDown();
378+
process.exit(0);
377379
} catch (error) {
378380
console.error(error);
379381
}

apps/react-tools-demo/src/components/hooks/useMap/UseMap.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
import { useCallback, useRef } from "react";
1+
import { useCallback, useMemo, useRef } from "react";
22
import { useMap } from "../../../../../../packages/react-tools/src";
33

4+
/**
5+
The component has:
6+
- A _useMap_ internal state.
7+
- An uncontrolled input with _inputRef_ ref used to execute buttons actions.
8+
- A button _Set_ that set the input value (format like __id__ _-_ __value__) into state by _set_ method of Map interface.
9+
- A button _Delete_ that remove item into state by _key_, written in input by _delete_ method of Map interface.
10+
- A button _Clear_ that clear state by _clear_ method of Map interface.
11+
- A button _IncrementAll_ that adds 1 to every value into map by _forEach_ method of Map interface.
12+
- A variable parsed create with useMemo that memoized a concatenated string of state key-value pairs, separated by comma.
13+
*/
414
const UseMap = () => {
515
const map = useMap<string, number>();
616

@@ -23,14 +33,21 @@ const UseMap = () => {
2333
map.forEach((value, key, map) => { map.set(key, value + 1) });
2434
}, [map]);
2535

36+
const parsed = useMemo(() => {
37+
const temp: string[] = [];
38+
temp.map
39+
map.forEach((value, key) => temp.push(key + " - " + value));
40+
return temp.join(", ");
41+
}, [map]);
42+
2643
return (<>
27-
{map.map((value, key) => key + " - " + value).join(", ")}
44+
{parsed}
2845
<br/>
2946
<input ref={inputRef} type="text" />
3047
<br />
3148
<div style={{ marginTop: 15, gridTemplateColumns: 'auto auto', justifyContent: 'center', display: 'grid', gap: '5px' }}>
32-
<button onClick={del}>Delete</button>
3349
<button onClick={set}>Set</button>
50+
<button onClick={del}>Delete</button>
3451
<button onClick={clear}>Clear</button>
3552
<button onClick={incrementAll}>Increment all</button>
3653
</div>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { useCallback, useMemo, useRef } from "react";
2+
import { useSet } from "../../../../../../packages/react-tools/src";
3+
4+
/**
5+
The component has:
6+
- A _useSet_ internal state.
7+
- An uncontrolled input with _inputRef_ ref used to execute buttons actions.
8+
- A button _Add_ that adds the input value into state by _add_ method of Set interface.
9+
- A button _Delete_ that remove item into state by _key_, written in input by _delete_ method of Set interface.
10+
- A button _Clear_ that clear state by _clear_ method of Set interface.
11+
- A variable _parsed_ create with __useMemo__ that memoized a concatenated string of state values, separated by comma.
12+
*/
13+
const UseSet = () => {
14+
const set = useSet<string | number>();
15+
16+
const inputRef = useRef<HTMLInputElement>(null);
17+
18+
const del = useCallback(() => {
19+
set.delete(inputRef.current!.value);
20+
}, [set]);
21+
22+
const add = useCallback(() => {
23+
const { value } = inputRef.current!;
24+
set.add(value);
25+
}, [set]);
26+
27+
const clear = useCallback(() => {
28+
set.clear();
29+
}, [set]);
30+
31+
const parsed = useMemo(() => {
32+
const temp: (string|number)[] = [];
33+
set.forEach((value) => temp.push(value));
34+
return temp.join(", ");
35+
}, [set]);
36+
37+
return (<>
38+
{parsed}
39+
<br/>
40+
<input ref={inputRef} type="text" />
41+
<br />
42+
<div style={{ marginTop: 15, gridTemplateColumns: 'auto auto', justifyContent: 'center', display: 'grid', gap: '5px' }}>
43+
<button onClick={add}>Add</button>
44+
<button onClick={del}>Delete</button>
45+
<button onClick={clear}>Clear</button>
46+
</div>
47+
</>);
48+
}
49+
50+
UseSet.displayName = "UseSet";
51+
52+
export {UseSet}

apps/react-tools-demo/src/constants/components.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const COMPONENTS = [
1010
"useLocalStorageState",
1111
"useSessionStorageState",
1212
"useMap",
13+
"useSet",
1314
"useMemoizedFunction",
1415
"useMemoCompare",
1516
"useMemoDeepCompare",

apps/react-tools-demo/src/markdown/useLocalStorageState.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,12 @@ object with serializer and deserializer function to handle values in localStorag
8181
> - __Union of__:
8282
> - __Array__:
8383
> - _T_
84-
> - _() => T_
85-
> - _() => void_
84+
> - _() => T, () => void_
8685
> - __Array__:
8786
> - _Dispatch<SetStateAction<T>>_
88-
> - _() => T_
89-
> - _() => void_
87+
> - _() => T, () => void_
9088
> - __Array__:
9189
> - _T_
9290
> - _Dispatch<SetStateAction<T>>_
93-
> - _() => T_
94-
> - _() => void_
91+
> - _() => T, () => void_
9592
>

apps/react-tools-demo/src/markdown/useMap.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# useMap
2-
Hooks to use _Map data structure_ to handle component state.
2+
Hooks to use _Map data structure_ to handle component state with all Map methods.
33

44
## Usage
55

66
```tsx
7-
87
const UseMap = () => {
98
const map = useMap<string, number>();
109

@@ -27,14 +26,21 @@ const UseMap = () => {
2726
map.forEach((value, key, map) => { map.set(key, value + 1) });
2827
}, [map]);
2928

29+
const parsed = useMemo(() => {
30+
const temp: string[] = [];
31+
temp.map
32+
map.forEach((value, key) => temp.push(key + " - " + value));
33+
return temp.join(", ");
34+
}, [map]);
35+
3036
return (<>
31-
{map.map((value, key) => key + " - " + value).join(", ")}
37+
{parsed}
3238
<br/>
3339
<input ref={inputRef} type="text" />
3440
<br />
3541
<div style={{ marginTop: 15, gridTemplateColumns: 'auto auto', justifyContent: 'center', display: 'grid', gap: '5px' }}>
36-
<button onClick={del}>Delete</button>
3742
<button onClick={set}>Set</button>
43+
<button onClick={del}>Delete</button>
3844
<button onClick={clear}>Clear</button>
3945
<button onClick={incrementAll}>Increment all</button>
4046
</div>
@@ -46,7 +52,13 @@ UseMap.displayName = "UseMap";
4652
export {UseMap}
4753
```
4854

49-
55+
> The component has:
56+
> - A _useMap_ internal state.
57+
> - An uncontrolled input with _inputRef_ ref used to execute buttons actions.
58+
> - A button _Set_ that set the input value (format like __id__ _-_ __value__) into state by _set_ method of Map interface.
59+
> - A button _Delete_ that remove item into state by _key_, written in input by _delete_ method of Map interface.
60+
> - A button _Clear_ that clear state by _clear_ method of Map interface.
61+
> - A button _IncrementAll_ that adds 1 to every value into map by _forEach_ method of Map interface.
5062
5163

5264
## API
@@ -64,6 +76,5 @@ An Array or other iterable object whose elements are key-value pairs, or functio
6476
> ### Returns
6577
>
6678
>
67-
> - _Map<K_
68-
> - _V>_
79+
> - _Map<K,V>_
6980
>

apps/react-tools-demo/src/markdown/usePubSubModel.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ usePubSubModel <T>(topic: string): { publish: (value?: T) => Promise<void>, subs
6060
> __result__: contains the _publish_ and _subscribe_ functions. The result of invoking the _subscribe_ function in turn returns a function that can be used to _unsubscribe_ the topic
6161
> - __Union of__:
6262
> - __Object__:
63-
> - __publish__ : _(value?: T) => Promise<void>_
64-
> - __subscribe__ : _(listener: (value?: T) => Promise<void_
63+
> - __publish__ : _(value?: T) => Promise<void>, subscribe: (listener: (value?: T) => Promise<void_
6564
> - _void) => () => void }_
6665
>

apps/react-tools-demo/src/markdown/useReducerGetReset.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ Function that should return the _initial state_. If it’s not specified, the in
2929
> - __Array__:
3030
> - _ReducerState<R>_
3131
> - _Dispatch<ReducerAction<R>>_
32-
> - _()=>ReducerState<R>_
33-
> - _()=>void_
32+
> - _()=>ReducerState<R>, ()=>void_
3433
>

apps/react-tools-demo/src/markdown/useReducerHistory.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,5 @@ history capacity (default 'no-limit').
3434
> - __Object__:
3535
> - __history__ : _readonly ReducerState<R>[]_
3636
> - __presentPointer__ : _number_
37-
> - __trackUpdate__ : _(enable:boolean) => void_
38-
> - __canUndo__ : _boolean_
39-
> - __canRedo__ : _boolean_
40-
> - __undo__ : _() => void_
41-
> - __redo__ : _() => void_
42-
> - __go__ : _(index: number) => void_
43-
> - __clear__ : _(value?: ReducerAction<R>) => void_
37+
> - __trackUpdate__ : _(enable:boolean) => void, canUndo: boolean, canRedo: boolean, undo: () => void, redo: () => void, go: (index: number) => void, clear: (value?: ReducerAction<R>) => void_
4438
>

apps/react-tools-demo/src/markdown/useReducerHistoryGetter.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,5 @@ history capacity (default 'no-limit').
3131
> - __Array__:
3232
> - _ReducerState<R>_
3333
> - _Dispatch<ReducerAction<R>>_
34-
> - _()=>ReducerState<R>_
35-
> - __Object__:
36-
> - __history__ : _readonly ReducerState<R>[]_
37-
> - __presentPointer__ : _number_
38-
> - __trackUpdate__ : _(enable:boolean) => void_
39-
> - __canUndo__ : _boolean_
40-
> - __canRedo__ : _boolean_
41-
> - __undo__ : _() => void_
42-
> - __redo__ : _() => void_
43-
> - __go__ : _(index: number) => void_
44-
> - __clear__ : _(value?: ReducerAction<R>) => void_
34+
> - _()=>ReducerState<R>, {history: readonly ReducerState<R>[], presentPointer: number, trackUpdate: (enable:boolean) => void, canUndo: boolean, canRedo: boolean, undo: () => void, redo: () => void, go: (index: number) => void, clear: (value?: ReducerAction<R>) => void}_
4535
>

0 commit comments

Comments
 (0)