@proedis/react 1.2.3 → 1.3.0

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.
Files changed (48) hide show
  1. package/cjs/hooks/useAutoControlledState.js +69 -69
  2. package/cjs/hooks/useClickOutside.js +54 -54
  3. package/cjs/hooks/useDataManager.js +29 -29
  4. package/cjs/hooks/useDataSelector.js +76 -76
  5. package/cjs/hooks/useDebouncedCallback.js +34 -34
  6. package/cjs/hooks/useElementType.js +35 -35
  7. package/cjs/hooks/useEnhancedEffect.js +8 -8
  8. package/cjs/hooks/useEvent.js +37 -37
  9. package/cjs/hooks/useForkRef.js +16 -16
  10. package/cjs/hooks/useInputValue.js +15 -15
  11. package/cjs/hooks/useObjectState.js +37 -37
  12. package/cjs/hooks/useSafeState.js +27 -27
  13. package/cjs/hooks/useSyncedRef.js +16 -16
  14. package/cjs/hooks/useUnmountEffect.js +14 -14
  15. package/cjs/hooks/useWindowSize.js +54 -54
  16. package/cjs/index.js +0 -4
  17. package/cjs/utils/compose.js +40 -40
  18. package/cjs/utils/contextBuilder.js +36 -36
  19. package/cjs/utils/shorthands.js +148 -148
  20. package/esm/hooks/useAutoControlledState.js +69 -69
  21. package/esm/hooks/useClickOutside.js +54 -54
  22. package/esm/hooks/useDataManager.js +29 -29
  23. package/esm/hooks/useDataSelector.js +76 -76
  24. package/esm/hooks/useDebouncedCallback.js +34 -34
  25. package/esm/hooks/useElementType.js +35 -35
  26. package/esm/hooks/useEnhancedEffect.js +8 -8
  27. package/esm/hooks/useEvent.js +37 -37
  28. package/esm/hooks/useForkRef.js +16 -16
  29. package/esm/hooks/useInputValue.js +15 -15
  30. package/esm/hooks/useObjectState.js +37 -37
  31. package/esm/hooks/useSafeState.js +27 -27
  32. package/esm/hooks/useSyncedRef.js +16 -16
  33. package/esm/hooks/useUnmountEffect.js +14 -14
  34. package/esm/hooks/useWindowSize.js +54 -54
  35. package/esm/index.js +0 -2
  36. package/esm/utils/compose.js +40 -40
  37. package/esm/utils/contextBuilder.js +36 -36
  38. package/esm/utils/shorthands.js +148 -148
  39. package/package.json +19 -21
  40. package/tsconfig.declaration.tsbuildinfo +1 -1
  41. package/types/hooks/index.d.ts +0 -2
  42. package/types/utils/shorthands.d.ts +1 -1
  43. package/cjs/hooks/useQueryString.js +0 -92
  44. package/cjs/hooks/useQueryStringValue.js +0 -50
  45. package/esm/hooks/useQueryString.js +0 -70
  46. package/esm/hooks/useQueryStringValue.js +0 -29
  47. package/types/hooks/useQueryString.d.ts +0 -22
  48. package/types/hooks/useQueryStringValue.d.ts +0 -8
@@ -1,34 +1,34 @@
1
1
  import * as React from 'react';
2
2
 
3
- /* --------
4
- * Hook Definition
5
- * -------- */
6
- function useDataManager() {
7
- // ----
8
- // Internal State
9
- // ----
10
- const [deleting, setDeleting] = React.useState(undefined);
11
- const [updating, setUpdating] = React.useState(undefined);
12
- // ----
13
- // Handlers
14
- // ----
15
- const clearDeleting = React.useCallback(() => setDeleting(undefined), []);
16
- const clearUpdating = React.useCallback(() => setUpdating(undefined), []);
17
- const createDeletingHandler = React.useCallback((element) => () => setDeleting(element), []);
18
- const createUpdatingHandler = React.useCallback((element) => () => setUpdating(element), []);
19
- // ----
20
- // Hook Return
21
- // ----
22
- return {
23
- deleting,
24
- clearDeleting,
25
- clearUpdating,
26
- createDeletingHandler,
27
- createUpdatingHandler,
28
- setDeleting,
29
- setUpdating,
30
- updating
31
- };
3
+ /* --------
4
+ * Hook Definition
5
+ * -------- */
6
+ function useDataManager() {
7
+ // ----
8
+ // Internal State
9
+ // ----
10
+ const [deleting, setDeleting] = React.useState(undefined);
11
+ const [updating, setUpdating] = React.useState(undefined);
12
+ // ----
13
+ // Handlers
14
+ // ----
15
+ const clearDeleting = React.useCallback(() => setDeleting(undefined), []);
16
+ const clearUpdating = React.useCallback(() => setUpdating(undefined), []);
17
+ const createDeletingHandler = React.useCallback((element) => () => setDeleting(element), []);
18
+ const createUpdatingHandler = React.useCallback((element) => () => setUpdating(element), []);
19
+ // ----
20
+ // Hook Return
21
+ // ----
22
+ return {
23
+ deleting,
24
+ clearDeleting,
25
+ clearUpdating,
26
+ createDeletingHandler,
27
+ createUpdatingHandler,
28
+ setDeleting,
29
+ setUpdating,
30
+ updating
31
+ };
32
32
  }
33
33
 
34
34
  export { useDataManager };
@@ -2,82 +2,82 @@ import * as React from 'react';
2
2
  import { useSyncedRef } from './useSyncedRef.js';
3
3
  import { useUnmountEffect } from './useUnmountEffect.js';
4
4
 
5
- /* --------
6
- * Hook Definition
7
- * -------- */
8
- /**
9
- * Create a set of utilities to select and item from an array source
10
- * and to automatically assert the selected item is always valid
11
- * @param data Array data source
12
- * @param options
13
- */
14
- function useDataSelector(data, options) {
15
- // ----
16
- // Internal State
17
- // ----
18
- const [selected, setSelectedBase] = React.useState(options?.defaultSelected);
19
- // ----
20
- // Internal Hooks
21
- // ----
22
- const userDefinedOnSelectChange = useSyncedRef(options?.onSelectedChange);
23
- const userDefinedComparer = useSyncedRef(options?.comparer);
24
- // ----
25
- // Handlers & Callbacks
26
- // ----
27
- const getSourceItem = React.useCallback((item) => {
28
- /** Undefined item could always be selected */
29
- if (item === undefined) {
30
- return undefined;
31
- }
32
- /** Assert selectable data is always an array */
33
- const selectableData = Array.isArray(data) ? data : [];
34
- /** If a user-defined function exists, use it to check if the item could be selected or not */
35
- const { current: currentComparer } = userDefinedComparer;
36
- if (typeof currentComparer === 'function') {
37
- return selectableData.find((value) => currentComparer(item, value));
38
- }
39
- /** Else return a simple object reference check */
40
- return selectableData.includes(item) ? item : undefined;
41
- }, [data, userDefinedComparer]);
42
- const setSelected = React.useCallback((item) => {
43
- const sourceItem = getSourceItem(item);
44
- setSelectedBase(sourceItem);
45
- const { current: currentUserDefinedOnSelectChange } = userDefinedOnSelectChange;
46
- if (typeof currentUserDefinedOnSelectChange === 'function') {
47
- currentUserDefinedOnSelectChange(sourceItem);
48
- }
49
- }, [getSourceItem, userDefinedOnSelectChange]);
50
- const clearSelected = React.useCallback(() => setSelected(undefined), [setSelected]);
51
- const createSelectorHandler = React.useCallback((item) => () => setSelected(item), [setSelected]);
52
- // ----
53
- // Selected Assertion
54
- // ----
55
- React.useEffect(() => {
56
- /** Get source item from data */
57
- const sourceItem = getSourceItem(selected);
58
- /** If selected exists, but source item not, remove selection */
59
- if (selected && !sourceItem) {
60
- setSelected(undefined);
61
- }
62
- /** If both items exist, but are different, replace selection */
63
- else if (!!selected && !!sourceItem && selected !== sourceItem) {
64
- setSelected(sourceItem);
65
- }
66
- }, [getSourceItem, setSelected, selected]);
67
- // ----
68
- // Clear Selected on Unmount
69
- // ----
70
- useUnmountEffect(clearSelected);
71
- // ----
72
- // Return utilities
73
- // ----
74
- return {
75
- clearSelected,
76
- createSelectorHandler,
77
- selected,
78
- setSelected
79
- };
80
- }
5
+ /* --------
6
+ * Hook Definition
7
+ * -------- */
8
+ /**
9
+ * Create a set of utilities to select and item from an array source
10
+ * and to automatically assert the selected item is always valid
11
+ * @param data Array data source
12
+ * @param options
13
+ */
14
+ function useDataSelector(data, options) {
15
+ // ----
16
+ // Internal State
17
+ // ----
18
+ const [selected, setSelectedBase] = React.useState(options?.defaultSelected);
19
+ // ----
20
+ // Internal Hooks
21
+ // ----
22
+ const userDefinedOnSelectChange = useSyncedRef(options?.onSelectedChange);
23
+ const userDefinedComparer = useSyncedRef(options?.comparer);
24
+ // ----
25
+ // Handlers & Callbacks
26
+ // ----
27
+ const getSourceItem = React.useCallback((item) => {
28
+ /** Undefined item could always be selected */
29
+ if (item === undefined) {
30
+ return undefined;
31
+ }
32
+ /** Assert selectable data is always an array */
33
+ const selectableData = Array.isArray(data) ? data : [];
34
+ /** If a user-defined function exists, use it to check if the item could be selected or not */
35
+ const { current: currentComparer } = userDefinedComparer;
36
+ if (typeof currentComparer === 'function') {
37
+ return selectableData.find((value) => currentComparer(item, value));
38
+ }
39
+ /** Else return a simple object reference check */
40
+ return selectableData.includes(item) ? item : undefined;
41
+ }, [data, userDefinedComparer]);
42
+ const setSelected = React.useCallback((item) => {
43
+ const sourceItem = getSourceItem(item);
44
+ setSelectedBase(sourceItem);
45
+ const { current: currentUserDefinedOnSelectChange } = userDefinedOnSelectChange;
46
+ if (typeof currentUserDefinedOnSelectChange === 'function') {
47
+ currentUserDefinedOnSelectChange(sourceItem);
48
+ }
49
+ }, [getSourceItem, userDefinedOnSelectChange]);
50
+ const clearSelected = React.useCallback(() => setSelected(undefined), [setSelected]);
51
+ const createSelectorHandler = React.useCallback((item) => () => setSelected(item), [setSelected]);
52
+ // ----
53
+ // Selected Assertion
54
+ // ----
55
+ React.useEffect(() => {
56
+ /** Get source item from data */
57
+ const sourceItem = getSourceItem(selected);
58
+ /** If selected exists, but source item not, remove selection */
59
+ if (selected && !sourceItem) {
60
+ setSelected(undefined);
61
+ }
62
+ /** If both items exist, but are different, replace selection */
63
+ else if (!!selected && !!sourceItem && selected !== sourceItem) {
64
+ setSelected(sourceItem);
65
+ }
66
+ }, [getSourceItem, setSelected, selected]);
67
+ // ----
68
+ // Clear Selected on Unmount
69
+ // ----
70
+ useUnmountEffect(clearSelected);
71
+ // ----
72
+ // Return utilities
73
+ // ----
74
+ return {
75
+ clearSelected,
76
+ createSelectorHandler,
77
+ selected,
78
+ setSelected
79
+ };
80
+ }
81
81
  useDataSelector.idComparer = (first, second) => (first.id === second.id);
82
82
 
83
83
  export { useDataSelector };
@@ -2,40 +2,40 @@ import * as React from 'react';
2
2
  import { useSyncedRef } from './useSyncedRef.js';
3
3
  import { useUnmountEffect } from './useUnmountEffect.js';
4
4
 
5
- /**
6
- * Return a wrapped Debounced variant function of the
7
- * provided function.
8
- * Interval param will delay the returned function call
9
- * if a next invoking will be requested
10
- *
11
- * @param callback The function to call
12
- * @param deps Array dependencies list
13
- * @param interval The interval time
14
- */
15
- function useDebouncedCallback(callback, deps, interval) {
16
- /** Prepare the timeout ref */
17
- const timeout = React.useRef();
18
- const debounceCallback = useSyncedRef(callback);
19
- /** Set up a function that could be used to clear timeout */
20
- const clearTimeoutRef = React.useCallback(() => {
21
- if (timeout.current) {
22
- clearTimeout(timeout.current);
23
- timeout.current = undefined;
24
- }
25
- }, []);
26
- /** Cleanup the timeout on unmount */
27
- useUnmountEffect(() => {
28
- clearTimeoutRef();
29
- });
30
- /** Return a new wrapped function that will set up the timeout on call */
31
- return React.useCallback((...args) => {
32
- /** If the timeout already exists, clear it */
33
- clearTimeoutRef();
34
- /** Set a new timeout to wait for function call */
35
- timeout.current = setTimeout(() => {
36
- debounceCallback.current(...args);
37
- }, interval);
38
- }, [clearTimeoutRef, interval, debounceCallback]);
5
+ /**
6
+ * Return a wrapped Debounced variant function of the
7
+ * provided function.
8
+ * Interval param will delay the returned function call
9
+ * if a next invoking will be requested
10
+ *
11
+ * @param callback The function to call
12
+ * @param deps Array dependencies list
13
+ * @param interval The interval time
14
+ */
15
+ function useDebouncedCallback(callback, deps, interval) {
16
+ /** Prepare the timeout ref */
17
+ const timeout = React.useRef();
18
+ const debounceCallback = useSyncedRef(callback);
19
+ /** Set up a function that could be used to clear timeout */
20
+ const clearTimeoutRef = React.useCallback(() => {
21
+ if (timeout.current) {
22
+ clearTimeout(timeout.current);
23
+ timeout.current = undefined;
24
+ }
25
+ }, []);
26
+ /** Cleanup the timeout on unmount */
27
+ useUnmountEffect(() => {
28
+ clearTimeoutRef();
29
+ });
30
+ /** Return a new wrapped function that will set up the timeout on call */
31
+ return React.useCallback((...args) => {
32
+ /** If the timeout already exists, clear it */
33
+ clearTimeoutRef();
34
+ /** Set a new timeout to wait for function call */
35
+ timeout.current = setTimeout(() => {
36
+ debounceCallback.current(...args);
37
+ }, interval);
38
+ }, [clearTimeoutRef, interval, debounceCallback]);
39
39
  }
40
40
 
41
41
  export { useDebouncedCallback };
@@ -1,38 +1,38 @@
1
- /* --------
2
- * Hook Definition
3
- * -------- */
4
- /**
5
- * Get the ElementType that must be used to render a Component
6
- *
7
- * @param Component The Component to render
8
- * @param props Props received by Component
9
- * @param defaultElementType A default element type to use
10
- * @param getDefault A defined function used to compute the ElementType by props
11
- */
12
- function useElementType(Component, props, defaultElementType, getDefault) {
13
- /** Get default props directly from Component */
14
- const { defaultProps: componentDefaultProps } = Component;
15
- /** If received props contain the element type, use it to render the component */
16
- if (props.as && props.as !== componentDefaultProps?.as) {
17
- return props.as;
18
- }
19
- /** If a function has been provided to compute the element type, call it and return the element if exists */
20
- if (typeof getDefault === 'function') {
21
- const computedElementType = getDefault(props);
22
- if (computedElementType) {
23
- return computedElementType;
24
- }
25
- }
26
- /** If getDefault is an object than is a ElementType mapper using props, compute it and return */
27
- if (typeof getDefault === 'object') {
28
- return getDefault.using[props[getDefault.map]] || getDefault.default;
29
- }
30
- /** If props include a href key, return anchor element as the element type */
31
- if (props.href) {
32
- return 'a';
33
- }
34
- /** Return element type, falling back to a 'div' element */
35
- return componentDefaultProps?.as || defaultElementType || 'div';
1
+ /* --------
2
+ * Hook Definition
3
+ * -------- */
4
+ /**
5
+ * Get the ElementType that must be used to render a Component
6
+ *
7
+ * @param Component The Component to render
8
+ * @param props Props received by Component
9
+ * @param defaultElementType A default element type to use
10
+ * @param getDefault A defined function used to compute the ElementType by props
11
+ */
12
+ function useElementType(Component, props, defaultElementType, getDefault) {
13
+ /** Get default props directly from Component */
14
+ const { defaultProps: componentDefaultProps } = Component;
15
+ /** If received props contain the element type, use it to render the component */
16
+ if (props.as && props.as !== componentDefaultProps?.as) {
17
+ return props.as;
18
+ }
19
+ /** If a function has been provided to compute the element type, call it and return the element if exists */
20
+ if (typeof getDefault === 'function') {
21
+ const computedElementType = getDefault(props);
22
+ if (computedElementType) {
23
+ return computedElementType;
24
+ }
25
+ }
26
+ /** If getDefault is an object than is a ElementType mapper using props, compute it and return */
27
+ if (typeof getDefault === 'object') {
28
+ return getDefault.using[props[getDefault.map]] || getDefault.default;
29
+ }
30
+ /** If props include a href key, return anchor element as the element type */
31
+ if (props.href) {
32
+ return 'a';
33
+ }
34
+ /** Return element type, falling back to a 'div' element */
35
+ return componentDefaultProps?.as || defaultElementType || 'div';
36
36
  }
37
37
 
38
38
  export { useElementType };
@@ -1,14 +1,14 @@
1
1
  import * as React from 'react';
2
2
  import { isBrowser } from '@proedis/utils';
3
3
 
4
- /**
5
- * Return the best way to use the native
6
- * useEffect React hook, getting the
7
- * useLayoutEffect if current code is executing
8
- * on a real browser client
9
- */
10
- const useEnhancedEffect = isBrowser
11
- ? React.useLayoutEffect
4
+ /**
5
+ * Return the best way to use the native
6
+ * useEffect React hook, getting the
7
+ * useLayoutEffect if current code is executing
8
+ * on a real browser client
9
+ */
10
+ const useEnhancedEffect = isBrowser
11
+ ? React.useLayoutEffect
12
12
  : React.useEffect;
13
13
 
14
14
  export { useEnhancedEffect };
@@ -1,43 +1,43 @@
1
1
  import { useEnhancedEffect } from './useEnhancedEffect.js';
2
2
  import { useSyncedRef } from './useSyncedRef.js';
3
3
 
4
- /**
5
- * Attach a HTMLElement lister to a target DOM Element.
6
- *
7
- * @param name The event to listen to
8
- * @param callback The callback function to call
9
- * @param options Some options to customize the hook
10
- */
11
- function useEvent(name, callback, options) {
12
- /** Get options and set defaults */
13
- const { disabled = false, target = document.documentElement } = options || {};
14
- /**
15
- * Every time the hook will be called update the handler
16
- * function in the ref object.
17
- * This logic will help the event listener to get the last
18
- * version update of the handler, without having the need to save it
19
- * into react use effect dependencies
20
- */
21
- const handler = useSyncedRef(callback);
22
- useEnhancedEffect(() => {
23
- /** If hook has been disabled, avoid the event listen */
24
- if (disabled) {
25
- return;
26
- }
27
- /**
28
- * Wrap the handler function into a local scoped function to be removed
29
- * once the effect will be cleared.
30
- * HEADS UP, using the useRef method to save the handler; the function
31
- * could not be the same reference as the component mount
32
- */
33
- const eventCallback = (event) => (handler.current.apply(target, [event]));
34
- /** Attach the event listener to the DOM target */
35
- target.addEventListener(name, eventCallback);
36
- /** On effect clear, remove the event attached to the target */
37
- return () => {
38
- target.removeEventListener(name, eventCallback);
39
- };
40
- }, [disabled, name, target, handler]);
4
+ /**
5
+ * Attach a HTMLElement lister to a target DOM Element.
6
+ *
7
+ * @param name The event to listen to
8
+ * @param callback The callback function to call
9
+ * @param options Some options to customize the hook
10
+ */
11
+ function useEvent(name, callback, options) {
12
+ /** Get options and set defaults */
13
+ const { disabled = false, target = document.documentElement } = options || {};
14
+ /**
15
+ * Every time the hook will be called update the handler
16
+ * function in the ref object.
17
+ * This logic will help the event listener to get the last
18
+ * version update of the handler, without having the need to save it
19
+ * into react use effect dependencies
20
+ */
21
+ const handler = useSyncedRef(callback);
22
+ useEnhancedEffect(() => {
23
+ /** If hook has been disabled, avoid the event listen */
24
+ if (disabled) {
25
+ return;
26
+ }
27
+ /**
28
+ * Wrap the handler function into a local scoped function to be removed
29
+ * once the effect will be cleared.
30
+ * HEADS UP, using the useRef method to save the handler; the function
31
+ * could not be the same reference as the component mount
32
+ */
33
+ const eventCallback = (event) => (handler.current.apply(target, [event]));
34
+ /** Attach the event listener to the DOM target */
35
+ target.addEventListener(name, eventCallback);
36
+ /** On effect clear, remove the event attached to the target */
37
+ return () => {
38
+ target.removeEventListener(name, eventCallback);
39
+ };
40
+ }, [disabled, name, target, handler]);
41
41
  }
42
42
 
43
43
  export { useEvent };
@@ -1,21 +1,21 @@
1
1
  import * as React from 'react';
2
2
 
3
- /**
4
- * Group multiple ref object into a single ref that will
5
- * assign the referenced element to each single ref.
6
- *
7
- * @param refs
8
- */
9
- function useForkRef(...refs) {
10
- return React.useCallback((refValue) => {
11
- refs.forEach((ref) => ref.current = refValue ?? undefined);
12
- },
13
- // react-hooks/exhaustive-deps will warn the next line because
14
- // the array will always change at each call of the hook.
15
- // In this case, could disable the warning because the ref hook used
16
- // as arguments won't change, because ref objects are immutable
17
- // eslint-disable-next-line react-hooks/exhaustive-deps
18
- [...refs]);
3
+ /**
4
+ * Group multiple ref object into a single ref that will
5
+ * assign the referenced element to each single ref.
6
+ *
7
+ * @param refs
8
+ */
9
+ function useForkRef(...refs) {
10
+ return React.useCallback((refValue) => {
11
+ refs.forEach((ref) => ref.current = refValue ?? undefined);
12
+ },
13
+ // react-hooks/exhaustive-deps will warn the next line because
14
+ // the array will always change at each call of the hook.
15
+ // In this case, could disable the warning because the ref hook used
16
+ // as arguments won't change, because ref objects are immutable
17
+ // eslint-disable-next-line react-hooks/exhaustive-deps
18
+ [...refs]);
19
19
  }
20
20
 
21
21
  export { useForkRef };
@@ -1,20 +1,20 @@
1
1
  import * as React from 'react';
2
2
 
3
- function useInputValue(initialValue) {
4
- // ----
5
- // Internal Hooks
6
- // ----
7
- const [value, setValue] = React.useState(initialValue ?? '');
8
- // ----
9
- // Handler
10
- // ----
11
- const handleInputValueChange = React.useCallback((event) => {
12
- setValue(event.currentTarget.value);
13
- }, []);
14
- // ----
15
- // Hook Returns
16
- // ----
17
- return [value, handleInputValueChange, setValue];
3
+ function useInputValue(initialValue) {
4
+ // ----
5
+ // Internal Hooks
6
+ // ----
7
+ const [value, setValue] = React.useState(initialValue ?? '');
8
+ // ----
9
+ // Handler
10
+ // ----
11
+ const handleInputValueChange = React.useCallback((event) => {
12
+ setValue(event.currentTarget.value);
13
+ }, []);
14
+ // ----
15
+ // Hook Returns
16
+ // ----
17
+ return [value, handleInputValueChange, setValue];
18
18
  }
19
19
 
20
20
  export { useInputValue };
@@ -3,43 +3,43 @@ import { getValueAt, setValueAt } from '@proedis/utils';
3
3
  import { useSafeState } from './useSafeState.js';
4
4
  import { useSyncedRef } from './useSyncedRef.js';
5
5
 
6
- /**
7
- * Hook that could be used to manage an immutable object, updating or returning value
8
- * using key dotted path.
9
- *
10
- * @param initialObject
11
- * @param options
12
- */
13
- function useObjectState(initialObject, options) {
14
- /** Create the initial state using provided object */
15
- const [state, setState] = useSafeState(initialObject);
16
- /** Call the onChange function every time state changes */
17
- const onStateChangeRef = useSyncedRef(options?.onChange);
18
- React.useEffect(() => {
19
- const { current: currentOnChange } = onStateChangeRef;
20
- if (typeof currentOnChange === 'function') {
21
- currentOnChange(state);
22
- }
23
- }, [onStateChangeRef, state]);
24
- /** Create the utilities to instantly transact the state */
25
- const transact = React.useCallback((nextState) => {
26
- if (typeof nextState === 'function') {
27
- setState((curr) => ({
28
- ...nextState(curr)
29
- }));
30
- }
31
- else {
32
- setState({ ...nextState });
33
- }
34
- }, [setState]);
35
- /** Create the utility to get the value at specific path */
36
- const getValue = React.useCallback((path) => (getValueAt(state, path)), [state]);
37
- /** Set value at specific path */
38
- const setValue = React.useCallback((path, value) => {
39
- setState((curr) => (setValueAt(curr, path, value, { immutable: true })));
40
- }, [setState]);
41
- /** Hook returns */
42
- return [state, { getValue, setValue, transact }];
6
+ /**
7
+ * Hook that could be used to manage an immutable object, updating or returning value
8
+ * using key dotted path.
9
+ *
10
+ * @param initialObject
11
+ * @param options
12
+ */
13
+ function useObjectState(initialObject, options) {
14
+ /** Create the initial state using provided object */
15
+ const [state, setState] = useSafeState(initialObject);
16
+ /** Call the onChange function every time state changes */
17
+ const onStateChangeRef = useSyncedRef(options?.onChange);
18
+ React.useEffect(() => {
19
+ const { current: currentOnChange } = onStateChangeRef;
20
+ if (typeof currentOnChange === 'function') {
21
+ currentOnChange(state);
22
+ }
23
+ }, [onStateChangeRef, state]);
24
+ /** Create the utilities to instantly transact the state */
25
+ const transact = React.useCallback((nextState) => {
26
+ if (typeof nextState === 'function') {
27
+ setState((curr) => ({
28
+ ...nextState(curr)
29
+ }));
30
+ }
31
+ else {
32
+ setState({ ...nextState });
33
+ }
34
+ }, [setState]);
35
+ /** Create the utility to get the value at specific path */
36
+ const getValue = React.useCallback((path) => (getValueAt(state, path)), [state]);
37
+ /** Set value at specific path */
38
+ const setValue = React.useCallback((path, value) => {
39
+ setState((curr) => (setValueAt(curr, path, value, { immutable: true })));
40
+ }, [setState]);
41
+ /** Hook returns */
42
+ return [state, { getValue, setValue, transact }];
43
43
  }
44
44
 
45
45
  export { useObjectState };