react-state-monad 1.0.17 → 1.0.19
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/package.json +1 -1
- package/src/hooks/useFieldState.ts +38 -1
package/package.json
CHANGED
@@ -18,4 +18,41 @@ export function useFieldState<TOriginal, TField>(
|
|
18
18
|
(original) => original[field] as TField, // Extracts the field value.
|
19
19
|
(newField, original) => ({...original, [field]: newField} as TOriginal) // Updates the field with the new value.
|
20
20
|
);
|
21
|
-
}
|
21
|
+
}
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Hook that remaps the keys of an object within a StateObject to a Map of StateObjects,
|
25
|
+
* allowing for independent updates of each key while keeping the overall object state synchronized.
|
26
|
+
*
|
27
|
+
* @template TOriginal - The type of the original state object.
|
28
|
+
* @param state - The StateObject containing the original object.
|
29
|
+
* @returns A Map where each key is mapped to a new StateObject representing the value of that key,
|
30
|
+
* allowing individual updates while keeping the object state synchronized.
|
31
|
+
*/
|
32
|
+
|
33
|
+
export function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Map<string, StateObject<TField>> {
|
34
|
+
// si state no tiene valor, retornar un invalid
|
35
|
+
|
36
|
+
if (!state.hasValue) {
|
37
|
+
return new Map<string, StateObject<TField>>();
|
38
|
+
}
|
39
|
+
|
40
|
+
if (Array.isArray(state.value)) {
|
41
|
+
console.warn('useRemapKeysState should be used with objects, use useRemapArray for arrays');
|
42
|
+
return new Map<string, StateObject<TField>>();
|
43
|
+
}
|
44
|
+
|
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
|
+
});
|
52
|
+
|
53
|
+
return map;
|
54
|
+
}
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|