@proedis/react 1.2.4 → 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.
- package/cjs/hooks/useAutoControlledState.js +69 -69
- package/cjs/hooks/useClickOutside.js +54 -54
- package/cjs/hooks/useDataManager.js +29 -29
- package/cjs/hooks/useDataSelector.js +76 -76
- package/cjs/hooks/useDebouncedCallback.js +34 -34
- package/cjs/hooks/useElementType.js +35 -35
- package/cjs/hooks/useEnhancedEffect.js +8 -8
- package/cjs/hooks/useEvent.js +37 -37
- package/cjs/hooks/useForkRef.js +16 -16
- package/cjs/hooks/useInputValue.js +15 -15
- package/cjs/hooks/useObjectState.js +37 -37
- package/cjs/hooks/useSafeState.js +27 -27
- package/cjs/hooks/useSyncedRef.js +16 -16
- package/cjs/hooks/useUnmountEffect.js +14 -14
- package/cjs/hooks/useWindowSize.js +54 -54
- package/cjs/index.js +0 -4
- package/cjs/utils/compose.js +40 -40
- package/cjs/utils/contextBuilder.js +36 -36
- package/cjs/utils/shorthands.js +148 -148
- package/esm/hooks/useAutoControlledState.js +69 -69
- package/esm/hooks/useClickOutside.js +54 -54
- package/esm/hooks/useDataManager.js +29 -29
- package/esm/hooks/useDataSelector.js +76 -76
- package/esm/hooks/useDebouncedCallback.js +34 -34
- package/esm/hooks/useElementType.js +35 -35
- package/esm/hooks/useEnhancedEffect.js +8 -8
- package/esm/hooks/useEvent.js +37 -37
- package/esm/hooks/useForkRef.js +16 -16
- package/esm/hooks/useInputValue.js +15 -15
- package/esm/hooks/useObjectState.js +37 -37
- package/esm/hooks/useSafeState.js +27 -27
- package/esm/hooks/useSyncedRef.js +16 -16
- package/esm/hooks/useUnmountEffect.js +14 -14
- package/esm/hooks/useWindowSize.js +54 -54
- package/esm/index.js +0 -2
- package/esm/utils/compose.js +40 -40
- package/esm/utils/contextBuilder.js +36 -36
- package/esm/utils/shorthands.js +148 -148
- package/package.json +19 -21
- package/tsconfig.declaration.tsbuildinfo +1 -1
- package/types/hooks/index.d.ts +0 -2
- package/types/utils/shorthands.d.ts +1 -1
- package/cjs/hooks/useQueryString.js +0 -92
- package/cjs/hooks/useQueryStringValue.js +0 -50
- package/esm/hooks/useQueryString.js +0 -70
- package/esm/hooks/useQueryStringValue.js +0 -29
- package/types/hooks/useQueryString.d.ts +0 -22
- package/types/hooks/useQueryStringValue.d.ts +0 -8
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { useEnhancedEffect } from './useEnhancedEffect.js';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Hook that could be used to wrap the original
|
|
6
|
-
* setState hook function to be called and to change
|
|
7
|
-
* state only if the using component is mounted
|
|
8
|
-
*
|
|
9
|
-
* @param initialState Initial state to use
|
|
10
|
-
*/
|
|
11
|
-
function useSafeState(initialState) {
|
|
12
|
-
/** Init internal state using default React hook */
|
|
13
|
-
const [state, setState] = React.useState(initialState);
|
|
14
|
-
/** Use a ref hook to check component mount */
|
|
15
|
-
const isMounted = React.useRef(false);
|
|
16
|
-
/** Update the ref on component mount/unmount */
|
|
17
|
-
useEnhancedEffect(() => {
|
|
18
|
-
isMounted.current = true;
|
|
19
|
-
return () => {
|
|
20
|
-
isMounted.current = false;
|
|
21
|
-
};
|
|
22
|
-
}, []);
|
|
23
|
-
/** Wrap original setState action to consider mounted state */
|
|
24
|
-
const wrappedSeState = React.useCallback((newState) => {
|
|
25
|
-
if (isMounted.current) {
|
|
26
|
-
setState(newState);
|
|
27
|
-
}
|
|
28
|
-
}, []);
|
|
29
|
-
/** Return state and wrapped set state */
|
|
30
|
-
return [state, wrappedSeState];
|
|
4
|
+
/**
|
|
5
|
+
* Hook that could be used to wrap the original
|
|
6
|
+
* setState hook function to be called and to change
|
|
7
|
+
* state only if the using component is mounted
|
|
8
|
+
*
|
|
9
|
+
* @param initialState Initial state to use
|
|
10
|
+
*/
|
|
11
|
+
function useSafeState(initialState) {
|
|
12
|
+
/** Init internal state using default React hook */
|
|
13
|
+
const [state, setState] = React.useState(initialState);
|
|
14
|
+
/** Use a ref hook to check component mount */
|
|
15
|
+
const isMounted = React.useRef(false);
|
|
16
|
+
/** Update the ref on component mount/unmount */
|
|
17
|
+
useEnhancedEffect(() => {
|
|
18
|
+
isMounted.current = true;
|
|
19
|
+
return () => {
|
|
20
|
+
isMounted.current = false;
|
|
21
|
+
};
|
|
22
|
+
}, []);
|
|
23
|
+
/** Wrap original setState action to consider mounted state */
|
|
24
|
+
const wrappedSeState = React.useCallback((newState) => {
|
|
25
|
+
if (isMounted.current) {
|
|
26
|
+
setState(newState);
|
|
27
|
+
}
|
|
28
|
+
}, []);
|
|
29
|
+
/** Return state and wrapped set state */
|
|
30
|
+
return [state, wrappedSeState];
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
export { useSafeState };
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* An augmented version of the useRef function.
|
|
5
|
-
* It will return a 'ref' style object with the
|
|
6
|
-
* current key memoized, returning each time
|
|
7
|
-
* the most updated version of the value
|
|
8
|
-
*
|
|
9
|
-
* @param value The value to store into ref object
|
|
10
|
-
*/
|
|
11
|
-
function useSyncedRef(value) {
|
|
12
|
-
const ref = React.useRef(value);
|
|
13
|
-
ref.current = value;
|
|
14
|
-
return React.useMemo(() => Object.freeze({
|
|
15
|
-
get current() {
|
|
16
|
-
return ref.current;
|
|
17
|
-
}
|
|
18
|
-
}), []);
|
|
3
|
+
/**
|
|
4
|
+
* An augmented version of the useRef function.
|
|
5
|
+
* It will return a 'ref' style object with the
|
|
6
|
+
* current key memoized, returning each time
|
|
7
|
+
* the most updated version of the value
|
|
8
|
+
*
|
|
9
|
+
* @param value The value to store into ref object
|
|
10
|
+
*/
|
|
11
|
+
function useSyncedRef(value) {
|
|
12
|
+
const ref = React.useRef(value);
|
|
13
|
+
ref.current = value;
|
|
14
|
+
return React.useMemo(() => Object.freeze({
|
|
15
|
+
get current() {
|
|
16
|
+
return ref.current;
|
|
17
|
+
}
|
|
18
|
+
}), []);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export { useSyncedRef };
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { useEnhancedEffect } from './useEnhancedEffect.js';
|
|
2
2
|
import { useSyncedRef } from './useSyncedRef.js';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Use this hook to call the provided
|
|
6
|
-
* callable function to be called once the
|
|
7
|
-
* component will unmount
|
|
8
|
-
*
|
|
9
|
-
* @param callback Function to be called
|
|
10
|
-
*/
|
|
11
|
-
function useUnmountEffect(callback) {
|
|
12
|
-
/** Save the callback function into an immutable object */
|
|
13
|
-
const unmountCallback = useSyncedRef(callback);
|
|
14
|
-
/** Attach an event to call function on unmount */
|
|
15
|
-
useEnhancedEffect(() => () => {
|
|
16
|
-
unmountCallback.current();
|
|
17
|
-
}, [unmountCallback]);
|
|
4
|
+
/**
|
|
5
|
+
* Use this hook to call the provided
|
|
6
|
+
* callable function to be called once the
|
|
7
|
+
* component will unmount
|
|
8
|
+
*
|
|
9
|
+
* @param callback Function to be called
|
|
10
|
+
*/
|
|
11
|
+
function useUnmountEffect(callback) {
|
|
12
|
+
/** Save the callback function into an immutable object */
|
|
13
|
+
const unmountCallback = useSyncedRef(callback);
|
|
14
|
+
/** Attach an event to call function on unmount */
|
|
15
|
+
useEnhancedEffect(() => () => {
|
|
16
|
+
unmountCallback.current();
|
|
17
|
+
}, [unmountCallback]);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
export { useUnmountEffect };
|
|
@@ -1,60 +1,60 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { useEnhancedEffect } from './useEnhancedEffect.js';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Monitor the change of the Window Size.
|
|
6
|
-
* Take careful that the object returned by the hook will never change:
|
|
7
|
-
* to use value within hook dependencies must deconstruct internal properties
|
|
8
|
-
*
|
|
9
|
-
* @param options Set hook options
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* // Do this
|
|
13
|
-
* const { height, width } = useWindowSize();
|
|
14
|
-
*
|
|
15
|
-
* React.useEffect(
|
|
16
|
-
* () => {
|
|
17
|
-
* // Wonderful code...
|
|
18
|
-
* },
|
|
19
|
-
* [ height, width ]
|
|
20
|
-
* );
|
|
21
|
-
*/
|
|
22
|
-
function useWindowSize(options) {
|
|
23
|
-
const { disabled } = options || {};
|
|
24
|
-
// ----
|
|
25
|
-
// Internal State
|
|
26
|
-
// ----
|
|
27
|
-
const [currentSize, setCurrentSize] = React.useState(() => ({
|
|
28
|
-
height: typeof window !== 'undefined' ? window.innerHeight : 0,
|
|
29
|
-
width: typeof window !== 'undefined' ? window.innerWidth : 0
|
|
30
|
-
}));
|
|
31
|
-
// ----
|
|
32
|
-
// Handler
|
|
33
|
-
// ----
|
|
34
|
-
const handleWindowSizeChange = React.useCallback(() => setCurrentSize((curr) => {
|
|
35
|
-
curr.height = window.innerHeight;
|
|
36
|
-
curr.width = window.innerWidth;
|
|
37
|
-
return curr;
|
|
38
|
-
}), []);
|
|
39
|
-
// ----
|
|
40
|
-
// Internal Hooks
|
|
41
|
-
// ----
|
|
42
|
-
useEnhancedEffect(() => {
|
|
43
|
-
/** If hook has been disabled, avoid the event listen */
|
|
44
|
-
if (disabled) {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
/** Attach event to the window element to listen for resize change */
|
|
48
|
-
window.addEventListener('resize', handleWindowSizeChange);
|
|
49
|
-
/** On effect clear, remove the event attached to the window */
|
|
50
|
-
return () => {
|
|
51
|
-
window.removeEventListener('resize', handleWindowSizeChange);
|
|
52
|
-
};
|
|
53
|
-
}, [disabled, handleWindowSizeChange]);
|
|
54
|
-
// ----
|
|
55
|
-
// Hook returns
|
|
56
|
-
// ----
|
|
57
|
-
return currentSize;
|
|
4
|
+
/**
|
|
5
|
+
* Monitor the change of the Window Size.
|
|
6
|
+
* Take careful that the object returned by the hook will never change:
|
|
7
|
+
* to use value within hook dependencies must deconstruct internal properties
|
|
8
|
+
*
|
|
9
|
+
* @param options Set hook options
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Do this
|
|
13
|
+
* const { height, width } = useWindowSize();
|
|
14
|
+
*
|
|
15
|
+
* React.useEffect(
|
|
16
|
+
* () => {
|
|
17
|
+
* // Wonderful code...
|
|
18
|
+
* },
|
|
19
|
+
* [ height, width ]
|
|
20
|
+
* );
|
|
21
|
+
*/
|
|
22
|
+
function useWindowSize(options) {
|
|
23
|
+
const { disabled } = options || {};
|
|
24
|
+
// ----
|
|
25
|
+
// Internal State
|
|
26
|
+
// ----
|
|
27
|
+
const [currentSize, setCurrentSize] = React.useState(() => ({
|
|
28
|
+
height: typeof window !== 'undefined' ? window.innerHeight : 0,
|
|
29
|
+
width: typeof window !== 'undefined' ? window.innerWidth : 0
|
|
30
|
+
}));
|
|
31
|
+
// ----
|
|
32
|
+
// Handler
|
|
33
|
+
// ----
|
|
34
|
+
const handleWindowSizeChange = React.useCallback(() => setCurrentSize((curr) => {
|
|
35
|
+
curr.height = window.innerHeight;
|
|
36
|
+
curr.width = window.innerWidth;
|
|
37
|
+
return curr;
|
|
38
|
+
}), []);
|
|
39
|
+
// ----
|
|
40
|
+
// Internal Hooks
|
|
41
|
+
// ----
|
|
42
|
+
useEnhancedEffect(() => {
|
|
43
|
+
/** If hook has been disabled, avoid the event listen */
|
|
44
|
+
if (disabled) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
/** Attach event to the window element to listen for resize change */
|
|
48
|
+
window.addEventListener('resize', handleWindowSizeChange);
|
|
49
|
+
/** On effect clear, remove the event attached to the window */
|
|
50
|
+
return () => {
|
|
51
|
+
window.removeEventListener('resize', handleWindowSizeChange);
|
|
52
|
+
};
|
|
53
|
+
}, [disabled, handleWindowSizeChange]);
|
|
54
|
+
// ----
|
|
55
|
+
// Hook returns
|
|
56
|
+
// ----
|
|
57
|
+
return currentSize;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
export { useWindowSize };
|
package/esm/index.js
CHANGED
|
@@ -9,8 +9,6 @@ export { useEvent } from './hooks/useEvent.js';
|
|
|
9
9
|
export { useForkRef } from './hooks/useForkRef.js';
|
|
10
10
|
export { useInputValue } from './hooks/useInputValue.js';
|
|
11
11
|
export { useObjectState } from './hooks/useObjectState.js';
|
|
12
|
-
export { useQueryString } from './hooks/useQueryString.js';
|
|
13
|
-
export { useQueryStringValue } from './hooks/useQueryStringValue.js';
|
|
14
12
|
export { useSafeState } from './hooks/useSafeState.js';
|
|
15
13
|
export { useSyncedRef } from './hooks/useSyncedRef.js';
|
|
16
14
|
export { useUnmountEffect } from './hooks/useUnmountEffect.js';
|
package/esm/utils/compose.js
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
1
|
import { jsx } from 'react/jsx-runtime';
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Return a single FunctionComponent, composed by nesting all components provided as arguments
|
|
6
|
-
* from the first one (root component) to the last one (last inner component).
|
|
7
|
-
* It is useful to avoid hell-nesting components declaration.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const Providers = compose(FirstProvider, SecondProvider, 'div', LastProvider);
|
|
11
|
-
*
|
|
12
|
-
* // Later
|
|
13
|
-
* <Providers>
|
|
14
|
-
* <MyApp />
|
|
15
|
-
* </Providers>
|
|
16
|
-
*
|
|
17
|
-
* // Will Become
|
|
18
|
-
* <FirstProvider>
|
|
19
|
-
* <SecondProvider>
|
|
20
|
-
* <div>
|
|
21
|
-
* <LastProvider>
|
|
22
|
-
* <MyApp />
|
|
23
|
-
* </LastProvider>
|
|
24
|
-
* </div>
|
|
25
|
-
* </SecondProvider>
|
|
26
|
-
* </FirstProvider>
|
|
27
|
-
*
|
|
28
|
-
* @param components
|
|
29
|
-
*/
|
|
30
|
-
function compose(...components) {
|
|
31
|
-
const ComposedComponent = (props) => (jsx(React.Fragment, { children: components.reduceRight((children, Provider) => {
|
|
32
|
-
/** Array has style of [ Component, props ] */
|
|
33
|
-
if (Array.isArray(Provider)) {
|
|
34
|
-
/** Split component and Props */
|
|
35
|
-
const [Component, componentProps] = Provider;
|
|
36
|
-
/** Return Component and inner children */
|
|
37
|
-
return (jsx(Component, { ...componentProps, children: children }));
|
|
38
|
-
}
|
|
39
|
-
/** Render provider without props */
|
|
40
|
-
return (jsx(Provider, { children: children }));
|
|
41
|
-
}, props.children) }));
|
|
42
|
-
ComposedComponent.displayName = 'ComposedComponent';
|
|
43
|
-
return ComposedComponent;
|
|
4
|
+
/**
|
|
5
|
+
* Return a single FunctionComponent, composed by nesting all components provided as arguments
|
|
6
|
+
* from the first one (root component) to the last one (last inner component).
|
|
7
|
+
* It is useful to avoid hell-nesting components declaration.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const Providers = compose(FirstProvider, SecondProvider, 'div', LastProvider);
|
|
11
|
+
*
|
|
12
|
+
* // Later
|
|
13
|
+
* <Providers>
|
|
14
|
+
* <MyApp />
|
|
15
|
+
* </Providers>
|
|
16
|
+
*
|
|
17
|
+
* // Will Become
|
|
18
|
+
* <FirstProvider>
|
|
19
|
+
* <SecondProvider>
|
|
20
|
+
* <div>
|
|
21
|
+
* <LastProvider>
|
|
22
|
+
* <MyApp />
|
|
23
|
+
* </LastProvider>
|
|
24
|
+
* </div>
|
|
25
|
+
* </SecondProvider>
|
|
26
|
+
* </FirstProvider>
|
|
27
|
+
*
|
|
28
|
+
* @param components
|
|
29
|
+
*/
|
|
30
|
+
function compose(...components) {
|
|
31
|
+
const ComposedComponent = (props) => (jsx(React.Fragment, { children: components.reduceRight((children, Provider) => {
|
|
32
|
+
/** Array has style of [ Component, props ] */
|
|
33
|
+
if (Array.isArray(Provider)) {
|
|
34
|
+
/** Split component and Props */
|
|
35
|
+
const [Component, componentProps] = Provider;
|
|
36
|
+
/** Return Component and inner children */
|
|
37
|
+
return (jsx(Component, { ...componentProps, children: children }));
|
|
38
|
+
}
|
|
39
|
+
/** Render provider without props */
|
|
40
|
+
return (jsx(Provider, { children: children }));
|
|
41
|
+
}, props.children) }));
|
|
42
|
+
ComposedComponent.displayName = 'ComposedComponent';
|
|
43
|
+
return ComposedComponent;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
export { compose };
|
|
@@ -1,41 +1,41 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Easy creation of a React Context providing the name to use
|
|
5
|
-
* and optionally an initial context.
|
|
6
|
-
* Object returned will reformat string and return well-named function and components
|
|
7
|
-
* @param name
|
|
8
|
-
* @param initialContext
|
|
9
|
-
*/
|
|
10
|
-
function contextBuilder(name, initialContext) {
|
|
11
|
-
/** Create the base context using provided data */
|
|
12
|
-
const BaseContext = React.createContext(initialContext);
|
|
13
|
-
/** Create the capitalized name */
|
|
14
|
-
const capitalizedName = `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
15
|
-
/** Create the hook function to return */
|
|
16
|
-
const useContextHook = () => {
|
|
17
|
-
/** Get the context value using built in hook */
|
|
18
|
-
const contextValue = React.useContext(BaseContext);
|
|
19
|
-
/** Assert the context value exists */
|
|
20
|
-
if (!contextValue) {
|
|
21
|
-
throw new Error(`use${capitalizedName}() hook must be invoked inside its right Context`);
|
|
22
|
-
}
|
|
23
|
-
/** Return extracted value */
|
|
24
|
-
return contextValue;
|
|
25
|
-
};
|
|
26
|
-
/** Assign name to Context objects */
|
|
27
|
-
BaseContext.displayName = capitalizedName;
|
|
28
|
-
/** Return context tools */
|
|
29
|
-
const context = { [`${capitalizedName}Context`]: BaseContext };
|
|
30
|
-
const contextHook = { [`use${capitalizedName}`]: useContextHook };
|
|
31
|
-
const contextProvider = { [`${capitalizedName}Provider`]: BaseContext.Provider };
|
|
32
|
-
const contextConsumer = { [`${capitalizedName}Consumer`]: BaseContext.Consumer };
|
|
33
|
-
return {
|
|
34
|
-
...context,
|
|
35
|
-
...contextHook,
|
|
36
|
-
...contextProvider,
|
|
37
|
-
...contextConsumer
|
|
38
|
-
};
|
|
3
|
+
/**
|
|
4
|
+
* Easy creation of a React Context providing the name to use
|
|
5
|
+
* and optionally an initial context.
|
|
6
|
+
* Object returned will reformat string and return well-named function and components
|
|
7
|
+
* @param name
|
|
8
|
+
* @param initialContext
|
|
9
|
+
*/
|
|
10
|
+
function contextBuilder(name, initialContext) {
|
|
11
|
+
/** Create the base context using provided data */
|
|
12
|
+
const BaseContext = React.createContext(initialContext);
|
|
13
|
+
/** Create the capitalized name */
|
|
14
|
+
const capitalizedName = `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
15
|
+
/** Create the hook function to return */
|
|
16
|
+
const useContextHook = () => {
|
|
17
|
+
/** Get the context value using built in hook */
|
|
18
|
+
const contextValue = React.useContext(BaseContext);
|
|
19
|
+
/** Assert the context value exists */
|
|
20
|
+
if (!contextValue) {
|
|
21
|
+
throw new Error(`use${capitalizedName}() hook must be invoked inside its right Context`);
|
|
22
|
+
}
|
|
23
|
+
/** Return extracted value */
|
|
24
|
+
return contextValue;
|
|
25
|
+
};
|
|
26
|
+
/** Assign name to Context objects */
|
|
27
|
+
BaseContext.displayName = capitalizedName;
|
|
28
|
+
/** Return context tools */
|
|
29
|
+
const context = { [`${capitalizedName}Context`]: BaseContext };
|
|
30
|
+
const contextHook = { [`use${capitalizedName}`]: useContextHook };
|
|
31
|
+
const contextProvider = { [`${capitalizedName}Provider`]: BaseContext.Provider };
|
|
32
|
+
const contextConsumer = { [`${capitalizedName}Consumer`]: BaseContext.Consumer };
|
|
33
|
+
return {
|
|
34
|
+
...context,
|
|
35
|
+
...contextHook,
|
|
36
|
+
...contextProvider,
|
|
37
|
+
...contextConsumer
|
|
38
|
+
};
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
export { contextBuilder };
|