react-state-monad 1.0.20 → 1.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/package.json +1 -1
- package/src/hooks/useFieldState.ts +12 -16
package/README.md
CHANGED
@@ -145,8 +145,15 @@ Parameters:
|
|
145
145
|
|
146
146
|
Returns a `StateObject` of type `TOrigin` representing the value if it is defined and non-null, otherwise an `EmptyState`.
|
147
147
|
|
148
|
+
### `useRemapKeysState<TOriginal, TField>`
|
148
149
|
|
150
|
+
This hook remaps the keys of a state object to a record of `StateObject`s, allowing for independent updates of each key while keeping the overall object state synchronized.
|
149
151
|
|
152
|
+
Parameters:
|
153
|
+
|
154
|
+
- `state`: The `StateObject` containing the original state.
|
155
|
+
|
156
|
+
Returns a record where each key is mapped to a new `StateObject` representing the value of that key, allowing individual updates while keeping the object state synchronized. If the state has no value or is an array, it returns an empty object.
|
150
157
|
|
151
158
|
|
152
159
|
|
package/package.json
CHANGED
@@ -21,36 +21,32 @@ export function useFieldState<TOriginal, TField>(
|
|
21
21
|
}
|
22
22
|
|
23
23
|
/**
|
24
|
-
* Hook that remaps the keys of
|
25
|
-
* allowing for independent updates of each key while keeping the overall object state synchronized.
|
24
|
+
* Hook that remaps the keys of a state object to a record of StateObjects.
|
26
25
|
*
|
27
26
|
* @template TOriginal - The type of the original state object.
|
28
|
-
* @
|
29
|
-
* @
|
30
|
-
*
|
27
|
+
* @template TField - The type of the field value to be derived.
|
28
|
+
* @param state - The StateObject containing the original state.
|
29
|
+
* @returns A record where each key is mapped to a new StateObject for the corresponding field.
|
31
30
|
*/
|
32
31
|
|
33
|
-
export function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>):
|
32
|
+
export function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Record<string, StateObject<TField>> {
|
34
33
|
// si state no tiene valor, retornar un invalid
|
35
34
|
|
36
35
|
if (!state.hasValue) {
|
37
|
-
return
|
36
|
+
return {} as Record<string, StateObject<TField>>;
|
38
37
|
}
|
39
38
|
|
40
39
|
if (Array.isArray(state.value)) {
|
41
40
|
console.warn('useRemapKeysState should be used with objects, use useRemapArray for arrays');
|
42
|
-
return
|
41
|
+
return {} as Record<string, StateObject<TField>>;
|
43
42
|
}
|
44
43
|
|
45
|
-
const keys = Object.keys(state.value);
|
46
|
-
|
47
|
-
const map = new Map<string, StateObject<TField>>();
|
48
|
-
|
49
|
-
keys.forEach((key) => {
|
50
|
-
map.set(key, useFieldState(state, key as ValidFieldFrom<TOriginal, TField>));
|
51
|
-
});
|
44
|
+
const keys = Object.keys(state.value);
|
52
45
|
|
53
|
-
return
|
46
|
+
return keys.reduce((acc, key) => {
|
47
|
+
acc[key] = useFieldState(state, key as ValidFieldFrom<TOriginal, TField>);
|
48
|
+
return acc;
|
49
|
+
}, {} as Record<string, StateObject<TField>>);
|
54
50
|
}
|
55
51
|
|
56
52
|
|