@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
|
@@ -23,75 +23,75 @@ function _interopNamespaceDefault(e) {
|
|
|
23
23
|
|
|
24
24
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
25
25
|
|
|
26
|
-
/* --------
|
|
27
|
-
* Hook Function
|
|
28
|
-
* -------- */
|
|
29
|
-
/**
|
|
30
|
-
* Augmented useState with AutoControlled option.
|
|
31
|
-
* Use this hook if the controlling of this state
|
|
32
|
-
* will be optionally demanded externally and
|
|
33
|
-
* potentially controlled by a prop or an arbitrary value
|
|
34
|
-
*
|
|
35
|
-
* @param initialState The initial state of the AutoControlled State
|
|
36
|
-
* @param options Options to pass controlled value and default value
|
|
37
|
-
*/
|
|
38
|
-
function useAutoControlledState(initialState, options) {
|
|
39
|
-
/**
|
|
40
|
-
* Get the user defined options to check if
|
|
41
|
-
* the state will be auto-controlled or
|
|
42
|
-
* controlled externally by user
|
|
43
|
-
*/
|
|
44
|
-
const { value, defaultValue } = options || {};
|
|
45
|
-
/**
|
|
46
|
-
* To avoid the useCallback change every time
|
|
47
|
-
* the value change, and let invalidate the
|
|
48
|
-
* other hooks, depending on it, save the
|
|
49
|
-
* AutoControlled state into an internal ref
|
|
50
|
-
*/
|
|
51
|
-
const isAutoControlled = React__namespace.useRef(value === undefined);
|
|
52
|
-
/**
|
|
53
|
-
* Update the isAutoControlled value any time the
|
|
54
|
-
* value will change
|
|
55
|
-
*/
|
|
56
|
-
useEnhancedEffect.useEnhancedEffect(() => {
|
|
57
|
-
isAutoControlled.current = value === undefined;
|
|
58
|
-
}, [value]);
|
|
59
|
-
/**
|
|
60
|
-
* As this hook will be principally used on a component
|
|
61
|
-
* with value (like input, checkbox) or in component
|
|
62
|
-
* with an external controllable state (like modal),
|
|
63
|
-
* use the SafeState hook to avoid state change when the
|
|
64
|
-
* component is unmounted
|
|
65
|
-
*/
|
|
66
|
-
const [state, setState] = useSafeState.useSafeState(() => {
|
|
67
|
-
/** Get initial value from user props */
|
|
68
|
-
const initialProp = value === undefined ? defaultValue : undefined;
|
|
69
|
-
/** If initialProps is undefined, could return it */
|
|
70
|
-
if (initialProp !== undefined) {
|
|
71
|
-
return initialProp;
|
|
72
|
-
}
|
|
73
|
-
/** Else, return the initialState provided within the function */
|
|
74
|
-
return typeof initialState === 'function'
|
|
75
|
-
? initialState()
|
|
76
|
-
: initialState;
|
|
77
|
-
});
|
|
78
|
-
/**
|
|
79
|
-
* Create a new function that could be used to attempt to modify
|
|
80
|
-
* the state object, only if the state is not being controlled
|
|
81
|
-
* by the external property
|
|
82
|
-
*/
|
|
83
|
-
const trySetState = React__namespace.useCallback((newState) => {
|
|
84
|
-
/** Update the state only if current prop is not */
|
|
85
|
-
if (isAutoControlled.current) {
|
|
86
|
-
setState(newState);
|
|
87
|
-
}
|
|
88
|
-
}, [setState]);
|
|
89
|
-
/** Return the Hook Result with state and utilities */
|
|
90
|
-
return [
|
|
91
|
-
value === undefined ? state : value,
|
|
92
|
-
trySetState,
|
|
93
|
-
setState
|
|
94
|
-
];
|
|
26
|
+
/* --------
|
|
27
|
+
* Hook Function
|
|
28
|
+
* -------- */
|
|
29
|
+
/**
|
|
30
|
+
* Augmented useState with AutoControlled option.
|
|
31
|
+
* Use this hook if the controlling of this state
|
|
32
|
+
* will be optionally demanded externally and
|
|
33
|
+
* potentially controlled by a prop or an arbitrary value
|
|
34
|
+
*
|
|
35
|
+
* @param initialState The initial state of the AutoControlled State
|
|
36
|
+
* @param options Options to pass controlled value and default value
|
|
37
|
+
*/
|
|
38
|
+
function useAutoControlledState(initialState, options) {
|
|
39
|
+
/**
|
|
40
|
+
* Get the user defined options to check if
|
|
41
|
+
* the state will be auto-controlled or
|
|
42
|
+
* controlled externally by user
|
|
43
|
+
*/
|
|
44
|
+
const { value, defaultValue } = options || {};
|
|
45
|
+
/**
|
|
46
|
+
* To avoid the useCallback change every time
|
|
47
|
+
* the value change, and let invalidate the
|
|
48
|
+
* other hooks, depending on it, save the
|
|
49
|
+
* AutoControlled state into an internal ref
|
|
50
|
+
*/
|
|
51
|
+
const isAutoControlled = React__namespace.useRef(value === undefined);
|
|
52
|
+
/**
|
|
53
|
+
* Update the isAutoControlled value any time the
|
|
54
|
+
* value will change
|
|
55
|
+
*/
|
|
56
|
+
useEnhancedEffect.useEnhancedEffect(() => {
|
|
57
|
+
isAutoControlled.current = value === undefined;
|
|
58
|
+
}, [value]);
|
|
59
|
+
/**
|
|
60
|
+
* As this hook will be principally used on a component
|
|
61
|
+
* with value (like input, checkbox) or in component
|
|
62
|
+
* with an external controllable state (like modal),
|
|
63
|
+
* use the SafeState hook to avoid state change when the
|
|
64
|
+
* component is unmounted
|
|
65
|
+
*/
|
|
66
|
+
const [state, setState] = useSafeState.useSafeState(() => {
|
|
67
|
+
/** Get initial value from user props */
|
|
68
|
+
const initialProp = value === undefined ? defaultValue : undefined;
|
|
69
|
+
/** If initialProps is undefined, could return it */
|
|
70
|
+
if (initialProp !== undefined) {
|
|
71
|
+
return initialProp;
|
|
72
|
+
}
|
|
73
|
+
/** Else, return the initialState provided within the function */
|
|
74
|
+
return typeof initialState === 'function'
|
|
75
|
+
? initialState()
|
|
76
|
+
: initialState;
|
|
77
|
+
});
|
|
78
|
+
/**
|
|
79
|
+
* Create a new function that could be used to attempt to modify
|
|
80
|
+
* the state object, only if the state is not being controlled
|
|
81
|
+
* by the external property
|
|
82
|
+
*/
|
|
83
|
+
const trySetState = React__namespace.useCallback((newState) => {
|
|
84
|
+
/** Update the state only if current prop is not */
|
|
85
|
+
if (isAutoControlled.current) {
|
|
86
|
+
setState(newState);
|
|
87
|
+
}
|
|
88
|
+
}, [setState]);
|
|
89
|
+
/** Return the Hook Result with state and utilities */
|
|
90
|
+
return [
|
|
91
|
+
value === undefined ? state : value,
|
|
92
|
+
trySetState,
|
|
93
|
+
setState
|
|
94
|
+
];
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
exports.useAutoControlledState = useAutoControlledState;
|
|
@@ -3,60 +3,60 @@
|
|
|
3
3
|
var useEnhancedEffect = require('./useEnhancedEffect.js');
|
|
4
4
|
var useSyncedRef = require('./useSyncedRef.js');
|
|
5
5
|
|
|
6
|
-
/** The default events name to listen to */
|
|
7
|
-
const DEFAULT_EVENTS = [
|
|
8
|
-
'mousedown',
|
|
9
|
-
'touchstart'
|
|
10
|
-
];
|
|
11
|
-
/**
|
|
12
|
-
* Use this hook to fire an event every time a click occurs outside
|
|
13
|
-
* the provided target element
|
|
14
|
-
* @param target
|
|
15
|
-
* @param callback
|
|
16
|
-
* @param events
|
|
17
|
-
*/
|
|
18
|
-
function useClickOutside(target, callback, events = DEFAULT_EVENTS) {
|
|
19
|
-
/** Wrap dependent data into synced ref to maintain immutable */
|
|
20
|
-
const targetElement = useSyncedRef.useSyncedRef(target);
|
|
21
|
-
const callbackFunction = useSyncedRef.useSyncedRef(callback);
|
|
22
|
-
/** Use the Enhanced Effect to attach event listeners */
|
|
23
|
-
useEnhancedEffect.useEnhancedEffect(() => {
|
|
24
|
-
/**
|
|
25
|
-
* Internal function used to check if the event
|
|
26
|
-
* target received from the listener it is contained within the
|
|
27
|
-
* requested target element provided within the hook
|
|
28
|
-
*
|
|
29
|
-
* @param event HTML Event received
|
|
30
|
-
*/
|
|
31
|
-
function handler(event) {
|
|
32
|
-
/** Assert the target element exists */
|
|
33
|
-
if (!targetElement.current.current) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
/** Extract the event target */
|
|
37
|
-
const { target: eventTarget } = event;
|
|
38
|
-
const cb = callbackFunction.current;
|
|
39
|
-
if (!eventTarget
|
|
40
|
-
|| (!!eventTarget && !targetElement.current.current.contains(eventTarget))) {
|
|
41
|
-
/** Call the callback */
|
|
42
|
-
cb.call(this, event);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/** Attach the events on all requested keys */
|
|
46
|
-
events.forEach((eventName) => {
|
|
47
|
-
/** List for event directly on document element */
|
|
48
|
-
document.addEventListener(eventName, handler, { passive: true });
|
|
49
|
-
});
|
|
50
|
-
/** On effect clear, remove all events */
|
|
51
|
-
return () => {
|
|
52
|
-
events.forEach((eventName) => {
|
|
53
|
-
/** List for event directly on document element */
|
|
54
|
-
document.removeEventListener(eventName, handler);
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
},
|
|
58
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
59
|
-
[callbackFunction, targetElement, ...events]);
|
|
6
|
+
/** The default events name to listen to */
|
|
7
|
+
const DEFAULT_EVENTS = [
|
|
8
|
+
'mousedown',
|
|
9
|
+
'touchstart'
|
|
10
|
+
];
|
|
11
|
+
/**
|
|
12
|
+
* Use this hook to fire an event every time a click occurs outside
|
|
13
|
+
* the provided target element
|
|
14
|
+
* @param target
|
|
15
|
+
* @param callback
|
|
16
|
+
* @param events
|
|
17
|
+
*/
|
|
18
|
+
function useClickOutside(target, callback, events = DEFAULT_EVENTS) {
|
|
19
|
+
/** Wrap dependent data into synced ref to maintain immutable */
|
|
20
|
+
const targetElement = useSyncedRef.useSyncedRef(target);
|
|
21
|
+
const callbackFunction = useSyncedRef.useSyncedRef(callback);
|
|
22
|
+
/** Use the Enhanced Effect to attach event listeners */
|
|
23
|
+
useEnhancedEffect.useEnhancedEffect(() => {
|
|
24
|
+
/**
|
|
25
|
+
* Internal function used to check if the event
|
|
26
|
+
* target received from the listener it is contained within the
|
|
27
|
+
* requested target element provided within the hook
|
|
28
|
+
*
|
|
29
|
+
* @param event HTML Event received
|
|
30
|
+
*/
|
|
31
|
+
function handler(event) {
|
|
32
|
+
/** Assert the target element exists */
|
|
33
|
+
if (!targetElement.current.current) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
/** Extract the event target */
|
|
37
|
+
const { target: eventTarget } = event;
|
|
38
|
+
const cb = callbackFunction.current;
|
|
39
|
+
if (!eventTarget
|
|
40
|
+
|| (!!eventTarget && !targetElement.current.current.contains(eventTarget))) {
|
|
41
|
+
/** Call the callback */
|
|
42
|
+
cb.call(this, event);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** Attach the events on all requested keys */
|
|
46
|
+
events.forEach((eventName) => {
|
|
47
|
+
/** List for event directly on document element */
|
|
48
|
+
document.addEventListener(eventName, handler, { passive: true });
|
|
49
|
+
});
|
|
50
|
+
/** On effect clear, remove all events */
|
|
51
|
+
return () => {
|
|
52
|
+
events.forEach((eventName) => {
|
|
53
|
+
/** List for event directly on document element */
|
|
54
|
+
document.removeEventListener(eventName, handler);
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
59
|
+
[callbackFunction, targetElement, ...events]);
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
exports.useClickOutside = useClickOutside;
|
|
@@ -21,35 +21,35 @@ function _interopNamespaceDefault(e) {
|
|
|
21
21
|
|
|
22
22
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
23
23
|
|
|
24
|
-
/* --------
|
|
25
|
-
* Hook Definition
|
|
26
|
-
* -------- */
|
|
27
|
-
function useDataManager() {
|
|
28
|
-
// ----
|
|
29
|
-
// Internal State
|
|
30
|
-
// ----
|
|
31
|
-
const [deleting, setDeleting] = React__namespace.useState(undefined);
|
|
32
|
-
const [updating, setUpdating] = React__namespace.useState(undefined);
|
|
33
|
-
// ----
|
|
34
|
-
// Handlers
|
|
35
|
-
// ----
|
|
36
|
-
const clearDeleting = React__namespace.useCallback(() => setDeleting(undefined), []);
|
|
37
|
-
const clearUpdating = React__namespace.useCallback(() => setUpdating(undefined), []);
|
|
38
|
-
const createDeletingHandler = React__namespace.useCallback((element) => () => setDeleting(element), []);
|
|
39
|
-
const createUpdatingHandler = React__namespace.useCallback((element) => () => setUpdating(element), []);
|
|
40
|
-
// ----
|
|
41
|
-
// Hook Return
|
|
42
|
-
// ----
|
|
43
|
-
return {
|
|
44
|
-
deleting,
|
|
45
|
-
clearDeleting,
|
|
46
|
-
clearUpdating,
|
|
47
|
-
createDeletingHandler,
|
|
48
|
-
createUpdatingHandler,
|
|
49
|
-
setDeleting,
|
|
50
|
-
setUpdating,
|
|
51
|
-
updating
|
|
52
|
-
};
|
|
24
|
+
/* --------
|
|
25
|
+
* Hook Definition
|
|
26
|
+
* -------- */
|
|
27
|
+
function useDataManager() {
|
|
28
|
+
// ----
|
|
29
|
+
// Internal State
|
|
30
|
+
// ----
|
|
31
|
+
const [deleting, setDeleting] = React__namespace.useState(undefined);
|
|
32
|
+
const [updating, setUpdating] = React__namespace.useState(undefined);
|
|
33
|
+
// ----
|
|
34
|
+
// Handlers
|
|
35
|
+
// ----
|
|
36
|
+
const clearDeleting = React__namespace.useCallback(() => setDeleting(undefined), []);
|
|
37
|
+
const clearUpdating = React__namespace.useCallback(() => setUpdating(undefined), []);
|
|
38
|
+
const createDeletingHandler = React__namespace.useCallback((element) => () => setDeleting(element), []);
|
|
39
|
+
const createUpdatingHandler = React__namespace.useCallback((element) => () => setUpdating(element), []);
|
|
40
|
+
// ----
|
|
41
|
+
// Hook Return
|
|
42
|
+
// ----
|
|
43
|
+
return {
|
|
44
|
+
deleting,
|
|
45
|
+
clearDeleting,
|
|
46
|
+
clearUpdating,
|
|
47
|
+
createDeletingHandler,
|
|
48
|
+
createUpdatingHandler,
|
|
49
|
+
setDeleting,
|
|
50
|
+
setUpdating,
|
|
51
|
+
updating
|
|
52
|
+
};
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
exports.useDataManager = useDataManager;
|
|
@@ -23,82 +23,82 @@ function _interopNamespaceDefault(e) {
|
|
|
23
23
|
|
|
24
24
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
25
25
|
|
|
26
|
-
/* --------
|
|
27
|
-
* Hook Definition
|
|
28
|
-
* -------- */
|
|
29
|
-
/**
|
|
30
|
-
* Create a set of utilities to select and item from an array source
|
|
31
|
-
* and to automatically assert the selected item is always valid
|
|
32
|
-
* @param data Array data source
|
|
33
|
-
* @param options
|
|
34
|
-
*/
|
|
35
|
-
function useDataSelector(data, options) {
|
|
36
|
-
// ----
|
|
37
|
-
// Internal State
|
|
38
|
-
// ----
|
|
39
|
-
const [selected, setSelectedBase] = React__namespace.useState(options?.defaultSelected);
|
|
40
|
-
// ----
|
|
41
|
-
// Internal Hooks
|
|
42
|
-
// ----
|
|
43
|
-
const userDefinedOnSelectChange = useSyncedRef.useSyncedRef(options?.onSelectedChange);
|
|
44
|
-
const userDefinedComparer = useSyncedRef.useSyncedRef(options?.comparer);
|
|
45
|
-
// ----
|
|
46
|
-
// Handlers & Callbacks
|
|
47
|
-
// ----
|
|
48
|
-
const getSourceItem = React__namespace.useCallback((item) => {
|
|
49
|
-
/** Undefined item could always be selected */
|
|
50
|
-
if (item === undefined) {
|
|
51
|
-
return undefined;
|
|
52
|
-
}
|
|
53
|
-
/** Assert selectable data is always an array */
|
|
54
|
-
const selectableData = Array.isArray(data) ? data : [];
|
|
55
|
-
/** If a user-defined function exists, use it to check if the item could be selected or not */
|
|
56
|
-
const { current: currentComparer } = userDefinedComparer;
|
|
57
|
-
if (typeof currentComparer === 'function') {
|
|
58
|
-
return selectableData.find((value) => currentComparer(item, value));
|
|
59
|
-
}
|
|
60
|
-
/** Else return a simple object reference check */
|
|
61
|
-
return selectableData.includes(item) ? item : undefined;
|
|
62
|
-
}, [data, userDefinedComparer]);
|
|
63
|
-
const setSelected = React__namespace.useCallback((item) => {
|
|
64
|
-
const sourceItem = getSourceItem(item);
|
|
65
|
-
setSelectedBase(sourceItem);
|
|
66
|
-
const { current: currentUserDefinedOnSelectChange } = userDefinedOnSelectChange;
|
|
67
|
-
if (typeof currentUserDefinedOnSelectChange === 'function') {
|
|
68
|
-
currentUserDefinedOnSelectChange(sourceItem);
|
|
69
|
-
}
|
|
70
|
-
}, [getSourceItem, userDefinedOnSelectChange]);
|
|
71
|
-
const clearSelected = React__namespace.useCallback(() => setSelected(undefined), [setSelected]);
|
|
72
|
-
const createSelectorHandler = React__namespace.useCallback((item) => () => setSelected(item), [setSelected]);
|
|
73
|
-
// ----
|
|
74
|
-
// Selected Assertion
|
|
75
|
-
// ----
|
|
76
|
-
React__namespace.useEffect(() => {
|
|
77
|
-
/** Get source item from data */
|
|
78
|
-
const sourceItem = getSourceItem(selected);
|
|
79
|
-
/** If selected exists, but source item not, remove selection */
|
|
80
|
-
if (selected && !sourceItem) {
|
|
81
|
-
setSelected(undefined);
|
|
82
|
-
}
|
|
83
|
-
/** If both items exist, but are different, replace selection */
|
|
84
|
-
else if (!!selected && !!sourceItem && selected !== sourceItem) {
|
|
85
|
-
setSelected(sourceItem);
|
|
86
|
-
}
|
|
87
|
-
}, [getSourceItem, setSelected, selected]);
|
|
88
|
-
// ----
|
|
89
|
-
// Clear Selected on Unmount
|
|
90
|
-
// ----
|
|
91
|
-
useUnmountEffect.useUnmountEffect(clearSelected);
|
|
92
|
-
// ----
|
|
93
|
-
// Return utilities
|
|
94
|
-
// ----
|
|
95
|
-
return {
|
|
96
|
-
clearSelected,
|
|
97
|
-
createSelectorHandler,
|
|
98
|
-
selected,
|
|
99
|
-
setSelected
|
|
100
|
-
};
|
|
101
|
-
}
|
|
26
|
+
/* --------
|
|
27
|
+
* Hook Definition
|
|
28
|
+
* -------- */
|
|
29
|
+
/**
|
|
30
|
+
* Create a set of utilities to select and item from an array source
|
|
31
|
+
* and to automatically assert the selected item is always valid
|
|
32
|
+
* @param data Array data source
|
|
33
|
+
* @param options
|
|
34
|
+
*/
|
|
35
|
+
function useDataSelector(data, options) {
|
|
36
|
+
// ----
|
|
37
|
+
// Internal State
|
|
38
|
+
// ----
|
|
39
|
+
const [selected, setSelectedBase] = React__namespace.useState(options?.defaultSelected);
|
|
40
|
+
// ----
|
|
41
|
+
// Internal Hooks
|
|
42
|
+
// ----
|
|
43
|
+
const userDefinedOnSelectChange = useSyncedRef.useSyncedRef(options?.onSelectedChange);
|
|
44
|
+
const userDefinedComparer = useSyncedRef.useSyncedRef(options?.comparer);
|
|
45
|
+
// ----
|
|
46
|
+
// Handlers & Callbacks
|
|
47
|
+
// ----
|
|
48
|
+
const getSourceItem = React__namespace.useCallback((item) => {
|
|
49
|
+
/** Undefined item could always be selected */
|
|
50
|
+
if (item === undefined) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
/** Assert selectable data is always an array */
|
|
54
|
+
const selectableData = Array.isArray(data) ? data : [];
|
|
55
|
+
/** If a user-defined function exists, use it to check if the item could be selected or not */
|
|
56
|
+
const { current: currentComparer } = userDefinedComparer;
|
|
57
|
+
if (typeof currentComparer === 'function') {
|
|
58
|
+
return selectableData.find((value) => currentComparer(item, value));
|
|
59
|
+
}
|
|
60
|
+
/** Else return a simple object reference check */
|
|
61
|
+
return selectableData.includes(item) ? item : undefined;
|
|
62
|
+
}, [data, userDefinedComparer]);
|
|
63
|
+
const setSelected = React__namespace.useCallback((item) => {
|
|
64
|
+
const sourceItem = getSourceItem(item);
|
|
65
|
+
setSelectedBase(sourceItem);
|
|
66
|
+
const { current: currentUserDefinedOnSelectChange } = userDefinedOnSelectChange;
|
|
67
|
+
if (typeof currentUserDefinedOnSelectChange === 'function') {
|
|
68
|
+
currentUserDefinedOnSelectChange(sourceItem);
|
|
69
|
+
}
|
|
70
|
+
}, [getSourceItem, userDefinedOnSelectChange]);
|
|
71
|
+
const clearSelected = React__namespace.useCallback(() => setSelected(undefined), [setSelected]);
|
|
72
|
+
const createSelectorHandler = React__namespace.useCallback((item) => () => setSelected(item), [setSelected]);
|
|
73
|
+
// ----
|
|
74
|
+
// Selected Assertion
|
|
75
|
+
// ----
|
|
76
|
+
React__namespace.useEffect(() => {
|
|
77
|
+
/** Get source item from data */
|
|
78
|
+
const sourceItem = getSourceItem(selected);
|
|
79
|
+
/** If selected exists, but source item not, remove selection */
|
|
80
|
+
if (selected && !sourceItem) {
|
|
81
|
+
setSelected(undefined);
|
|
82
|
+
}
|
|
83
|
+
/** If both items exist, but are different, replace selection */
|
|
84
|
+
else if (!!selected && !!sourceItem && selected !== sourceItem) {
|
|
85
|
+
setSelected(sourceItem);
|
|
86
|
+
}
|
|
87
|
+
}, [getSourceItem, setSelected, selected]);
|
|
88
|
+
// ----
|
|
89
|
+
// Clear Selected on Unmount
|
|
90
|
+
// ----
|
|
91
|
+
useUnmountEffect.useUnmountEffect(clearSelected);
|
|
92
|
+
// ----
|
|
93
|
+
// Return utilities
|
|
94
|
+
// ----
|
|
95
|
+
return {
|
|
96
|
+
clearSelected,
|
|
97
|
+
createSelectorHandler,
|
|
98
|
+
selected,
|
|
99
|
+
setSelected
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
102
|
useDataSelector.idComparer = (first, second) => (first.id === second.id);
|
|
103
103
|
|
|
104
104
|
exports.useDataSelector = useDataSelector;
|
|
@@ -23,40 +23,40 @@ function _interopNamespaceDefault(e) {
|
|
|
23
23
|
|
|
24
24
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
25
25
|
|
|
26
|
-
/**
|
|
27
|
-
* Return a wrapped Debounced variant function of the
|
|
28
|
-
* provided function.
|
|
29
|
-
* Interval param will delay the returned function call
|
|
30
|
-
* if a next invoking will be requested
|
|
31
|
-
*
|
|
32
|
-
* @param callback The function to call
|
|
33
|
-
* @param deps Array dependencies list
|
|
34
|
-
* @param interval The interval time
|
|
35
|
-
*/
|
|
36
|
-
function useDebouncedCallback(callback, deps, interval) {
|
|
37
|
-
/** Prepare the timeout ref */
|
|
38
|
-
const timeout = React__namespace.useRef();
|
|
39
|
-
const debounceCallback = useSyncedRef.useSyncedRef(callback);
|
|
40
|
-
/** Set up a function that could be used to clear timeout */
|
|
41
|
-
const clearTimeoutRef = React__namespace.useCallback(() => {
|
|
42
|
-
if (timeout.current) {
|
|
43
|
-
clearTimeout(timeout.current);
|
|
44
|
-
timeout.current = undefined;
|
|
45
|
-
}
|
|
46
|
-
}, []);
|
|
47
|
-
/** Cleanup the timeout on unmount */
|
|
48
|
-
useUnmountEffect.useUnmountEffect(() => {
|
|
49
|
-
clearTimeoutRef();
|
|
50
|
-
});
|
|
51
|
-
/** Return a new wrapped function that will set up the timeout on call */
|
|
52
|
-
return React__namespace.useCallback((...args) => {
|
|
53
|
-
/** If the timeout already exists, clear it */
|
|
54
|
-
clearTimeoutRef();
|
|
55
|
-
/** Set a new timeout to wait for function call */
|
|
56
|
-
timeout.current = setTimeout(() => {
|
|
57
|
-
debounceCallback.current(...args);
|
|
58
|
-
}, interval);
|
|
59
|
-
}, [clearTimeoutRef, interval, debounceCallback]);
|
|
26
|
+
/**
|
|
27
|
+
* Return a wrapped Debounced variant function of the
|
|
28
|
+
* provided function.
|
|
29
|
+
* Interval param will delay the returned function call
|
|
30
|
+
* if a next invoking will be requested
|
|
31
|
+
*
|
|
32
|
+
* @param callback The function to call
|
|
33
|
+
* @param deps Array dependencies list
|
|
34
|
+
* @param interval The interval time
|
|
35
|
+
*/
|
|
36
|
+
function useDebouncedCallback(callback, deps, interval) {
|
|
37
|
+
/** Prepare the timeout ref */
|
|
38
|
+
const timeout = React__namespace.useRef();
|
|
39
|
+
const debounceCallback = useSyncedRef.useSyncedRef(callback);
|
|
40
|
+
/** Set up a function that could be used to clear timeout */
|
|
41
|
+
const clearTimeoutRef = React__namespace.useCallback(() => {
|
|
42
|
+
if (timeout.current) {
|
|
43
|
+
clearTimeout(timeout.current);
|
|
44
|
+
timeout.current = undefined;
|
|
45
|
+
}
|
|
46
|
+
}, []);
|
|
47
|
+
/** Cleanup the timeout on unmount */
|
|
48
|
+
useUnmountEffect.useUnmountEffect(() => {
|
|
49
|
+
clearTimeoutRef();
|
|
50
|
+
});
|
|
51
|
+
/** Return a new wrapped function that will set up the timeout on call */
|
|
52
|
+
return React__namespace.useCallback((...args) => {
|
|
53
|
+
/** If the timeout already exists, clear it */
|
|
54
|
+
clearTimeoutRef();
|
|
55
|
+
/** Set a new timeout to wait for function call */
|
|
56
|
+
timeout.current = setTimeout(() => {
|
|
57
|
+
debounceCallback.current(...args);
|
|
58
|
+
}, interval);
|
|
59
|
+
}, [clearTimeoutRef, interval, debounceCallback]);
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
exports.useDebouncedCallback = useDebouncedCallback;
|
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
/* --------
|
|
4
|
-
* Hook Definition
|
|
5
|
-
* -------- */
|
|
6
|
-
/**
|
|
7
|
-
* Get the ElementType that must be used to render a Component
|
|
8
|
-
*
|
|
9
|
-
* @param Component The Component to render
|
|
10
|
-
* @param props Props received by Component
|
|
11
|
-
* @param defaultElementType A default element type to use
|
|
12
|
-
* @param getDefault A defined function used to compute the ElementType by props
|
|
13
|
-
*/
|
|
14
|
-
function useElementType(Component, props, defaultElementType, getDefault) {
|
|
15
|
-
/** Get default props directly from Component */
|
|
16
|
-
const { defaultProps: componentDefaultProps } = Component;
|
|
17
|
-
/** If received props contain the element type, use it to render the component */
|
|
18
|
-
if (props.as && props.as !== componentDefaultProps?.as) {
|
|
19
|
-
return props.as;
|
|
20
|
-
}
|
|
21
|
-
/** If a function has been provided to compute the element type, call it and return the element if exists */
|
|
22
|
-
if (typeof getDefault === 'function') {
|
|
23
|
-
const computedElementType = getDefault(props);
|
|
24
|
-
if (computedElementType) {
|
|
25
|
-
return computedElementType;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
/** If getDefault is an object than is a ElementType mapper using props, compute it and return */
|
|
29
|
-
if (typeof getDefault === 'object') {
|
|
30
|
-
return getDefault.using[props[getDefault.map]] || getDefault.default;
|
|
31
|
-
}
|
|
32
|
-
/** If props include a href key, return anchor element as the element type */
|
|
33
|
-
if (props.href) {
|
|
34
|
-
return 'a';
|
|
35
|
-
}
|
|
36
|
-
/** Return element type, falling back to a 'div' element */
|
|
37
|
-
return componentDefaultProps?.as || defaultElementType || 'div';
|
|
3
|
+
/* --------
|
|
4
|
+
* Hook Definition
|
|
5
|
+
* -------- */
|
|
6
|
+
/**
|
|
7
|
+
* Get the ElementType that must be used to render a Component
|
|
8
|
+
*
|
|
9
|
+
* @param Component The Component to render
|
|
10
|
+
* @param props Props received by Component
|
|
11
|
+
* @param defaultElementType A default element type to use
|
|
12
|
+
* @param getDefault A defined function used to compute the ElementType by props
|
|
13
|
+
*/
|
|
14
|
+
function useElementType(Component, props, defaultElementType, getDefault) {
|
|
15
|
+
/** Get default props directly from Component */
|
|
16
|
+
const { defaultProps: componentDefaultProps } = Component;
|
|
17
|
+
/** If received props contain the element type, use it to render the component */
|
|
18
|
+
if (props.as && props.as !== componentDefaultProps?.as) {
|
|
19
|
+
return props.as;
|
|
20
|
+
}
|
|
21
|
+
/** If a function has been provided to compute the element type, call it and return the element if exists */
|
|
22
|
+
if (typeof getDefault === 'function') {
|
|
23
|
+
const computedElementType = getDefault(props);
|
|
24
|
+
if (computedElementType) {
|
|
25
|
+
return computedElementType;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** If getDefault is an object than is a ElementType mapper using props, compute it and return */
|
|
29
|
+
if (typeof getDefault === 'object') {
|
|
30
|
+
return getDefault.using[props[getDefault.map]] || getDefault.default;
|
|
31
|
+
}
|
|
32
|
+
/** If props include a href key, return anchor element as the element type */
|
|
33
|
+
if (props.href) {
|
|
34
|
+
return 'a';
|
|
35
|
+
}
|
|
36
|
+
/** Return element type, falling back to a 'div' element */
|
|
37
|
+
return componentDefaultProps?.as || defaultElementType || 'div';
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
exports.useElementType = useElementType;
|
|
@@ -22,14 +22,14 @@ function _interopNamespaceDefault(e) {
|
|
|
22
22
|
|
|
23
23
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
24
24
|
|
|
25
|
-
/**
|
|
26
|
-
* Return the best way to use the native
|
|
27
|
-
* useEffect React hook, getting the
|
|
28
|
-
* useLayoutEffect if current code is executing
|
|
29
|
-
* on a real browser client
|
|
30
|
-
*/
|
|
31
|
-
const useEnhancedEffect = utils.isBrowser
|
|
32
|
-
? React__namespace.useLayoutEffect
|
|
25
|
+
/**
|
|
26
|
+
* Return the best way to use the native
|
|
27
|
+
* useEffect React hook, getting the
|
|
28
|
+
* useLayoutEffect if current code is executing
|
|
29
|
+
* on a real browser client
|
|
30
|
+
*/
|
|
31
|
+
const useEnhancedEffect = utils.isBrowser
|
|
32
|
+
? React__namespace.useLayoutEffect
|
|
33
33
|
: React__namespace.useEffect;
|
|
34
34
|
|
|
35
35
|
exports.useEnhancedEffect = useEnhancedEffect;
|