@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
|
@@ -21,42 +21,42 @@ function _interopNamespaceDefault(e) {
|
|
|
21
21
|
|
|
22
22
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
23
23
|
|
|
24
|
-
/**
|
|
25
|
-
* Easy creation of a React Context providing the name to use
|
|
26
|
-
* and optionally an initial context.
|
|
27
|
-
* Object returned will reformat string and return well-named function and components
|
|
28
|
-
* @param name
|
|
29
|
-
* @param initialContext
|
|
30
|
-
*/
|
|
31
|
-
function contextBuilder(name, initialContext) {
|
|
32
|
-
/** Create the base context using provided data */
|
|
33
|
-
const BaseContext = React__namespace.createContext(initialContext);
|
|
34
|
-
/** Create the capitalized name */
|
|
35
|
-
const capitalizedName = `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
36
|
-
/** Create the hook function to return */
|
|
37
|
-
const useContextHook = () => {
|
|
38
|
-
/** Get the context value using built in hook */
|
|
39
|
-
const contextValue = React__namespace.useContext(BaseContext);
|
|
40
|
-
/** Assert the context value exists */
|
|
41
|
-
if (!contextValue) {
|
|
42
|
-
throw new Error(`use${capitalizedName}() hook must be invoked inside its right Context`);
|
|
43
|
-
}
|
|
44
|
-
/** Return extracted value */
|
|
45
|
-
return contextValue;
|
|
46
|
-
};
|
|
47
|
-
/** Assign name to Context objects */
|
|
48
|
-
BaseContext.displayName = capitalizedName;
|
|
49
|
-
/** Return context tools */
|
|
50
|
-
const context = { [`${capitalizedName}Context`]: BaseContext };
|
|
51
|
-
const contextHook = { [`use${capitalizedName}`]: useContextHook };
|
|
52
|
-
const contextProvider = { [`${capitalizedName}Provider`]: BaseContext.Provider };
|
|
53
|
-
const contextConsumer = { [`${capitalizedName}Consumer`]: BaseContext.Consumer };
|
|
54
|
-
return {
|
|
55
|
-
...context,
|
|
56
|
-
...contextHook,
|
|
57
|
-
...contextProvider,
|
|
58
|
-
...contextConsumer
|
|
59
|
-
};
|
|
24
|
+
/**
|
|
25
|
+
* Easy creation of a React Context providing the name to use
|
|
26
|
+
* and optionally an initial context.
|
|
27
|
+
* Object returned will reformat string and return well-named function and components
|
|
28
|
+
* @param name
|
|
29
|
+
* @param initialContext
|
|
30
|
+
*/
|
|
31
|
+
function contextBuilder(name, initialContext) {
|
|
32
|
+
/** Create the base context using provided data */
|
|
33
|
+
const BaseContext = React__namespace.createContext(initialContext);
|
|
34
|
+
/** Create the capitalized name */
|
|
35
|
+
const capitalizedName = `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
36
|
+
/** Create the hook function to return */
|
|
37
|
+
const useContextHook = () => {
|
|
38
|
+
/** Get the context value using built in hook */
|
|
39
|
+
const contextValue = React__namespace.useContext(BaseContext);
|
|
40
|
+
/** Assert the context value exists */
|
|
41
|
+
if (!contextValue) {
|
|
42
|
+
throw new Error(`use${capitalizedName}() hook must be invoked inside its right Context`);
|
|
43
|
+
}
|
|
44
|
+
/** Return extracted value */
|
|
45
|
+
return contextValue;
|
|
46
|
+
};
|
|
47
|
+
/** Assign name to Context objects */
|
|
48
|
+
BaseContext.displayName = capitalizedName;
|
|
49
|
+
/** Return context tools */
|
|
50
|
+
const context = { [`${capitalizedName}Context`]: BaseContext };
|
|
51
|
+
const contextHook = { [`use${capitalizedName}`]: useContextHook };
|
|
52
|
+
const contextProvider = { [`${capitalizedName}Provider`]: BaseContext.Provider };
|
|
53
|
+
const contextConsumer = { [`${capitalizedName}Consumer`]: BaseContext.Consumer };
|
|
54
|
+
return {
|
|
55
|
+
...context,
|
|
56
|
+
...contextHook,
|
|
57
|
+
...contextProvider,
|
|
58
|
+
...contextConsumer
|
|
59
|
+
};
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
exports.contextBuilder = contextBuilder;
|
package/cjs/utils/shorthands.js
CHANGED
|
@@ -25,154 +25,154 @@ function _interopNamespaceDefault(e) {
|
|
|
25
25
|
|
|
26
26
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
27
27
|
|
|
28
|
-
/* --------
|
|
29
|
-
* Utilities
|
|
30
|
-
* -------- */
|
|
31
|
-
/**
|
|
32
|
-
* Starting from the provided Component, render the element
|
|
33
|
-
* mapping the props using the Shorthand value argument
|
|
34
|
-
*
|
|
35
|
-
* @param Component The component to Render
|
|
36
|
-
* @param mapValueToProps Function used to map the value to Component Props
|
|
37
|
-
* @param computeComponentKey Optional function to use to compute the component key
|
|
38
|
-
* @param value The value to use while rendering the component
|
|
39
|
-
* @param options Component render options
|
|
40
|
-
*/
|
|
41
|
-
function createComponentShorthand(Component, mapValueToProps, computeComponentKey, value, options) {
|
|
42
|
-
/**
|
|
43
|
-
* If provided value is nil, or boolean
|
|
44
|
-
* avoid to render the component
|
|
45
|
-
*/
|
|
46
|
-
if (utils.isNil(value) || typeof value === 'boolean') {
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
// ----
|
|
50
|
-
// Computing value type and assert is valid
|
|
51
|
-
// ----
|
|
52
|
-
const valueIsString = typeof value === 'string';
|
|
53
|
-
const valueIsNumber = typeof value === 'number' && !Number.isNaN(value);
|
|
54
|
-
const valueIsReactElement = React__namespace.isValidElement(value);
|
|
55
|
-
const valueIsPropObject = kind_of(value) === 'object';
|
|
56
|
-
const valueIsPrimitiveValue = valueIsString || valueIsNumber || kind_of(value) === 'array';
|
|
57
|
-
/** Check the validity of provided value, logging the error only in production */
|
|
58
|
-
if (!valueIsReactElement && !valueIsPrimitiveValue && !valueIsPropObject) {
|
|
59
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
60
|
-
console.error([
|
|
61
|
-
'Shorthand value must be a string|number|array|object|ReactElement|function.',
|
|
62
|
-
'Use null|undefined|boolean for none.',
|
|
63
|
-
`Received ${kind_of(value)}`
|
|
64
|
-
].join(' '));
|
|
65
|
-
}
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
// ----
|
|
69
|
-
// Component Props Building
|
|
70
|
-
// ----
|
|
71
|
-
/** Extract default props from options */
|
|
72
|
-
const { defaultProps } = options;
|
|
73
|
-
/** Get the user defined props, if the component is a valid React element */
|
|
74
|
-
const userProps = (valueIsReactElement && value.props)
|
|
75
|
-
|| (valueIsPropObject && value)
|
|
76
|
-
|| (valueIsPrimitiveValue && mapValueToProps(value))
|
|
77
|
-
|| {};
|
|
78
|
-
/** Extract the override props from options */
|
|
79
|
-
let { overrideProps } = options;
|
|
80
|
-
/** Compute override props function */
|
|
81
|
-
if (typeof overrideProps === 'function') {
|
|
82
|
-
overrideProps = overrideProps({
|
|
83
|
-
...defaultProps,
|
|
84
|
-
...userProps
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
/** Merge final props to a single props object */
|
|
88
|
-
const props = {
|
|
89
|
-
...defaultProps,
|
|
90
|
-
...userProps,
|
|
91
|
-
...overrideProps
|
|
92
|
-
};
|
|
93
|
-
/** Merge className from props */
|
|
94
|
-
if (defaultProps?.className || overrideProps?.className || userProps.className) {
|
|
95
|
-
const mergedClassNames = clsx(defaultProps?.className, userProps?.className, overrideProps?.className);
|
|
96
|
-
props.className = Array.from(new Set(mergedClassNames.split(' ')).values()).join(' ');
|
|
97
|
-
}
|
|
98
|
-
/** Merge the style */
|
|
99
|
-
if (defaultProps?.style || overrideProps?.style || userProps.style) {
|
|
100
|
-
props.style = {
|
|
101
|
-
...defaultProps?.style,
|
|
102
|
-
...userProps.style,
|
|
103
|
-
...overrideProps?.style
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
// ----
|
|
107
|
-
// Component Key Computing
|
|
108
|
-
// ----
|
|
109
|
-
if (props.key == null) {
|
|
110
|
-
/** Get options to generate the key */
|
|
111
|
-
const { autoGenerateKey, childKey } = options;
|
|
112
|
-
if (typeof computeComponentKey === 'function') {
|
|
113
|
-
props.key = computeComponentKey(props);
|
|
114
|
-
}
|
|
115
|
-
else if (typeof childKey === 'function') {
|
|
116
|
-
props.key = childKey(props);
|
|
117
|
-
}
|
|
118
|
-
else if (autoGenerateKey && (valueIsString || valueIsNumber)) {
|
|
119
|
-
props.key = value;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
// ----
|
|
123
|
-
// Component Render
|
|
124
|
-
// ----
|
|
125
|
-
/** If provided value is a valid React Element, wrap using Component */
|
|
126
|
-
if (valueIsReactElement) {
|
|
127
|
-
return React__namespace.cloneElement(value, props);
|
|
128
|
-
}
|
|
129
|
-
/** If the value is a primitive value, or a plain props object, use base Component */
|
|
130
|
-
if (valueIsPrimitiveValue || valueIsPropObject) {
|
|
131
|
-
return (jsxRuntime.jsx(Component, { ...props }));
|
|
132
|
-
}
|
|
133
|
-
/** Fallback to null */
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
/* --------
|
|
137
|
-
* Factory Methods
|
|
138
|
-
* -------- */
|
|
139
|
-
/**
|
|
140
|
-
* Create a shorthand function that could be used to easily render component.
|
|
141
|
-
*
|
|
142
|
-
* @param Component The component to use
|
|
143
|
-
* @param mapValueToProps A function to use to map value to component Props
|
|
144
|
-
* @param computeComponentKey A custom function to compute the component key
|
|
145
|
-
*/
|
|
146
|
-
function createShorthandFactory(Component, mapValueToProps, computeComponentKey) {
|
|
147
|
-
return function createComponent(value, options) {
|
|
148
|
-
return createComponentShorthand(Component, mapValueToProps, computeComponentKey, value, options);
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Automatically add the 'create' shorthand method to the provided component
|
|
153
|
-
* and return it strongly typed as CreatableComponent
|
|
154
|
-
*
|
|
155
|
-
* @param Component The component to use
|
|
156
|
-
* @param mapValueToProps A function to use to map value to component Props
|
|
157
|
-
* @param computeComponentKey A custom function to compute the component key
|
|
158
|
-
*/
|
|
159
|
-
function creatableComponent(Component, mapValueToProps, computeComponentKey) {
|
|
160
|
-
/** Attach create method to the provided Component function */
|
|
161
|
-
Component.create =
|
|
162
|
-
createShorthandFactory(Component, mapValueToProps, computeComponentKey);
|
|
163
|
-
/** Return the Component */
|
|
164
|
-
return Component;
|
|
165
|
-
}
|
|
166
|
-
/* --------
|
|
167
|
-
* Render Shorthand Content
|
|
168
|
-
* -------- */
|
|
169
|
-
function renderShorthandContent({ children, content }) {
|
|
170
|
-
/** Render children if defined */
|
|
171
|
-
if (children === null || children === undefined || (Array.isArray(children) && children.length === 0)) {
|
|
172
|
-
return content;
|
|
173
|
-
}
|
|
174
|
-
/** Fallback to content */
|
|
175
|
-
return children;
|
|
28
|
+
/* --------
|
|
29
|
+
* Utilities
|
|
30
|
+
* -------- */
|
|
31
|
+
/**
|
|
32
|
+
* Starting from the provided Component, render the element
|
|
33
|
+
* mapping the props using the Shorthand value argument
|
|
34
|
+
*
|
|
35
|
+
* @param Component The component to Render
|
|
36
|
+
* @param mapValueToProps Function used to map the value to Component Props
|
|
37
|
+
* @param computeComponentKey Optional function to use to compute the component key
|
|
38
|
+
* @param value The value to use while rendering the component
|
|
39
|
+
* @param options Component render options
|
|
40
|
+
*/
|
|
41
|
+
function createComponentShorthand(Component, mapValueToProps, computeComponentKey, value, options) {
|
|
42
|
+
/**
|
|
43
|
+
* If provided value is nil, or boolean
|
|
44
|
+
* avoid to render the component
|
|
45
|
+
*/
|
|
46
|
+
if (utils.isNil(value) || typeof value === 'boolean') {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
// ----
|
|
50
|
+
// Computing value type and assert is valid
|
|
51
|
+
// ----
|
|
52
|
+
const valueIsString = typeof value === 'string';
|
|
53
|
+
const valueIsNumber = typeof value === 'number' && !Number.isNaN(value);
|
|
54
|
+
const valueIsReactElement = React__namespace.isValidElement(value);
|
|
55
|
+
const valueIsPropObject = kind_of(value) === 'object';
|
|
56
|
+
const valueIsPrimitiveValue = valueIsString || valueIsNumber || kind_of(value) === 'array';
|
|
57
|
+
/** Check the validity of provided value, logging the error only in production */
|
|
58
|
+
if (!valueIsReactElement && !valueIsPrimitiveValue && !valueIsPropObject) {
|
|
59
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
60
|
+
console.error([
|
|
61
|
+
'Shorthand value must be a string|number|array|object|ReactElement|function.',
|
|
62
|
+
'Use null|undefined|boolean for none.',
|
|
63
|
+
`Received ${kind_of(value)}`
|
|
64
|
+
].join(' '));
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
// ----
|
|
69
|
+
// Component Props Building
|
|
70
|
+
// ----
|
|
71
|
+
/** Extract default props from options */
|
|
72
|
+
const { defaultProps } = options;
|
|
73
|
+
/** Get the user defined props, if the component is a valid React element */
|
|
74
|
+
const userProps = (valueIsReactElement && value.props)
|
|
75
|
+
|| (valueIsPropObject && value)
|
|
76
|
+
|| (valueIsPrimitiveValue && mapValueToProps(value))
|
|
77
|
+
|| {};
|
|
78
|
+
/** Extract the override props from options */
|
|
79
|
+
let { overrideProps } = options;
|
|
80
|
+
/** Compute override props function */
|
|
81
|
+
if (typeof overrideProps === 'function') {
|
|
82
|
+
overrideProps = overrideProps({
|
|
83
|
+
...defaultProps,
|
|
84
|
+
...userProps
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/** Merge final props to a single props object */
|
|
88
|
+
const props = {
|
|
89
|
+
...defaultProps,
|
|
90
|
+
...userProps,
|
|
91
|
+
...overrideProps
|
|
92
|
+
};
|
|
93
|
+
/** Merge className from props */
|
|
94
|
+
if (defaultProps?.className || overrideProps?.className || userProps.className) {
|
|
95
|
+
const mergedClassNames = clsx(defaultProps?.className, userProps?.className, overrideProps?.className);
|
|
96
|
+
props.className = Array.from(new Set(mergedClassNames.split(' ')).values()).join(' ');
|
|
97
|
+
}
|
|
98
|
+
/** Merge the style */
|
|
99
|
+
if (defaultProps?.style || overrideProps?.style || userProps.style) {
|
|
100
|
+
props.style = {
|
|
101
|
+
...defaultProps?.style,
|
|
102
|
+
...userProps.style,
|
|
103
|
+
...overrideProps?.style
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
// ----
|
|
107
|
+
// Component Key Computing
|
|
108
|
+
// ----
|
|
109
|
+
if (props.key == null) {
|
|
110
|
+
/** Get options to generate the key */
|
|
111
|
+
const { autoGenerateKey, childKey } = options;
|
|
112
|
+
if (typeof computeComponentKey === 'function') {
|
|
113
|
+
props.key = computeComponentKey(props);
|
|
114
|
+
}
|
|
115
|
+
else if (typeof childKey === 'function') {
|
|
116
|
+
props.key = childKey(props);
|
|
117
|
+
}
|
|
118
|
+
else if (autoGenerateKey && (valueIsString || valueIsNumber)) {
|
|
119
|
+
props.key = value;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// ----
|
|
123
|
+
// Component Render
|
|
124
|
+
// ----
|
|
125
|
+
/** If provided value is a valid React Element, wrap using Component */
|
|
126
|
+
if (valueIsReactElement) {
|
|
127
|
+
return React__namespace.cloneElement(value, props);
|
|
128
|
+
}
|
|
129
|
+
/** If the value is a primitive value, or a plain props object, use base Component */
|
|
130
|
+
if (valueIsPrimitiveValue || valueIsPropObject) {
|
|
131
|
+
return (jsxRuntime.jsx(Component, { ...props }));
|
|
132
|
+
}
|
|
133
|
+
/** Fallback to null */
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
/* --------
|
|
137
|
+
* Factory Methods
|
|
138
|
+
* -------- */
|
|
139
|
+
/**
|
|
140
|
+
* Create a shorthand function that could be used to easily render component.
|
|
141
|
+
*
|
|
142
|
+
* @param Component The component to use
|
|
143
|
+
* @param mapValueToProps A function to use to map value to component Props
|
|
144
|
+
* @param computeComponentKey A custom function to compute the component key
|
|
145
|
+
*/
|
|
146
|
+
function createShorthandFactory(Component, mapValueToProps, computeComponentKey) {
|
|
147
|
+
return function createComponent(value, options) {
|
|
148
|
+
return createComponentShorthand(Component, mapValueToProps, computeComponentKey, value, options);
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Automatically add the 'create' shorthand method to the provided component
|
|
153
|
+
* and return it strongly typed as CreatableComponent
|
|
154
|
+
*
|
|
155
|
+
* @param Component The component to use
|
|
156
|
+
* @param mapValueToProps A function to use to map value to component Props
|
|
157
|
+
* @param computeComponentKey A custom function to compute the component key
|
|
158
|
+
*/
|
|
159
|
+
function creatableComponent(Component, mapValueToProps, computeComponentKey) {
|
|
160
|
+
/** Attach create method to the provided Component function */
|
|
161
|
+
Component.create =
|
|
162
|
+
createShorthandFactory(Component, mapValueToProps, computeComponentKey);
|
|
163
|
+
/** Return the Component */
|
|
164
|
+
return Component;
|
|
165
|
+
}
|
|
166
|
+
/* --------
|
|
167
|
+
* Render Shorthand Content
|
|
168
|
+
* -------- */
|
|
169
|
+
function renderShorthandContent({ children, content }) {
|
|
170
|
+
/** Render children if defined */
|
|
171
|
+
if (children === null || children === undefined || (Array.isArray(children) && children.length === 0)) {
|
|
172
|
+
return content;
|
|
173
|
+
}
|
|
174
|
+
/** Fallback to content */
|
|
175
|
+
return children;
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
exports.creatableComponent = creatableComponent;
|
|
@@ -2,75 +2,75 @@ import * as React from 'react';
|
|
|
2
2
|
import { useEnhancedEffect } from './useEnhancedEffect.js';
|
|
3
3
|
import { useSafeState } from './useSafeState.js';
|
|
4
4
|
|
|
5
|
-
/* --------
|
|
6
|
-
* Hook Function
|
|
7
|
-
* -------- */
|
|
8
|
-
/**
|
|
9
|
-
* Augmented useState with AutoControlled option.
|
|
10
|
-
* Use this hook if the controlling of this state
|
|
11
|
-
* will be optionally demanded externally and
|
|
12
|
-
* potentially controlled by a prop or an arbitrary value
|
|
13
|
-
*
|
|
14
|
-
* @param initialState The initial state of the AutoControlled State
|
|
15
|
-
* @param options Options to pass controlled value and default value
|
|
16
|
-
*/
|
|
17
|
-
function useAutoControlledState(initialState, options) {
|
|
18
|
-
/**
|
|
19
|
-
* Get the user defined options to check if
|
|
20
|
-
* the state will be auto-controlled or
|
|
21
|
-
* controlled externally by user
|
|
22
|
-
*/
|
|
23
|
-
const { value, defaultValue } = options || {};
|
|
24
|
-
/**
|
|
25
|
-
* To avoid the useCallback change every time
|
|
26
|
-
* the value change, and let invalidate the
|
|
27
|
-
* other hooks, depending on it, save the
|
|
28
|
-
* AutoControlled state into an internal ref
|
|
29
|
-
*/
|
|
30
|
-
const isAutoControlled = React.useRef(value === undefined);
|
|
31
|
-
/**
|
|
32
|
-
* Update the isAutoControlled value any time the
|
|
33
|
-
* value will change
|
|
34
|
-
*/
|
|
35
|
-
useEnhancedEffect(() => {
|
|
36
|
-
isAutoControlled.current = value === undefined;
|
|
37
|
-
}, [value]);
|
|
38
|
-
/**
|
|
39
|
-
* As this hook will be principally used on a component
|
|
40
|
-
* with value (like input, checkbox) or in component
|
|
41
|
-
* with an external controllable state (like modal),
|
|
42
|
-
* use the SafeState hook to avoid state change when the
|
|
43
|
-
* component is unmounted
|
|
44
|
-
*/
|
|
45
|
-
const [state, setState] = useSafeState(() => {
|
|
46
|
-
/** Get initial value from user props */
|
|
47
|
-
const initialProp = value === undefined ? defaultValue : undefined;
|
|
48
|
-
/** If initialProps is undefined, could return it */
|
|
49
|
-
if (initialProp !== undefined) {
|
|
50
|
-
return initialProp;
|
|
51
|
-
}
|
|
52
|
-
/** Else, return the initialState provided within the function */
|
|
53
|
-
return typeof initialState === 'function'
|
|
54
|
-
? initialState()
|
|
55
|
-
: initialState;
|
|
56
|
-
});
|
|
57
|
-
/**
|
|
58
|
-
* Create a new function that could be used to attempt to modify
|
|
59
|
-
* the state object, only if the state is not being controlled
|
|
60
|
-
* by the external property
|
|
61
|
-
*/
|
|
62
|
-
const trySetState = React.useCallback((newState) => {
|
|
63
|
-
/** Update the state only if current prop is not */
|
|
64
|
-
if (isAutoControlled.current) {
|
|
65
|
-
setState(newState);
|
|
66
|
-
}
|
|
67
|
-
}, [setState]);
|
|
68
|
-
/** Return the Hook Result with state and utilities */
|
|
69
|
-
return [
|
|
70
|
-
value === undefined ? state : value,
|
|
71
|
-
trySetState,
|
|
72
|
-
setState
|
|
73
|
-
];
|
|
5
|
+
/* --------
|
|
6
|
+
* Hook Function
|
|
7
|
+
* -------- */
|
|
8
|
+
/**
|
|
9
|
+
* Augmented useState with AutoControlled option.
|
|
10
|
+
* Use this hook if the controlling of this state
|
|
11
|
+
* will be optionally demanded externally and
|
|
12
|
+
* potentially controlled by a prop or an arbitrary value
|
|
13
|
+
*
|
|
14
|
+
* @param initialState The initial state of the AutoControlled State
|
|
15
|
+
* @param options Options to pass controlled value and default value
|
|
16
|
+
*/
|
|
17
|
+
function useAutoControlledState(initialState, options) {
|
|
18
|
+
/**
|
|
19
|
+
* Get the user defined options to check if
|
|
20
|
+
* the state will be auto-controlled or
|
|
21
|
+
* controlled externally by user
|
|
22
|
+
*/
|
|
23
|
+
const { value, defaultValue } = options || {};
|
|
24
|
+
/**
|
|
25
|
+
* To avoid the useCallback change every time
|
|
26
|
+
* the value change, and let invalidate the
|
|
27
|
+
* other hooks, depending on it, save the
|
|
28
|
+
* AutoControlled state into an internal ref
|
|
29
|
+
*/
|
|
30
|
+
const isAutoControlled = React.useRef(value === undefined);
|
|
31
|
+
/**
|
|
32
|
+
* Update the isAutoControlled value any time the
|
|
33
|
+
* value will change
|
|
34
|
+
*/
|
|
35
|
+
useEnhancedEffect(() => {
|
|
36
|
+
isAutoControlled.current = value === undefined;
|
|
37
|
+
}, [value]);
|
|
38
|
+
/**
|
|
39
|
+
* As this hook will be principally used on a component
|
|
40
|
+
* with value (like input, checkbox) or in component
|
|
41
|
+
* with an external controllable state (like modal),
|
|
42
|
+
* use the SafeState hook to avoid state change when the
|
|
43
|
+
* component is unmounted
|
|
44
|
+
*/
|
|
45
|
+
const [state, setState] = useSafeState(() => {
|
|
46
|
+
/** Get initial value from user props */
|
|
47
|
+
const initialProp = value === undefined ? defaultValue : undefined;
|
|
48
|
+
/** If initialProps is undefined, could return it */
|
|
49
|
+
if (initialProp !== undefined) {
|
|
50
|
+
return initialProp;
|
|
51
|
+
}
|
|
52
|
+
/** Else, return the initialState provided within the function */
|
|
53
|
+
return typeof initialState === 'function'
|
|
54
|
+
? initialState()
|
|
55
|
+
: initialState;
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* Create a new function that could be used to attempt to modify
|
|
59
|
+
* the state object, only if the state is not being controlled
|
|
60
|
+
* by the external property
|
|
61
|
+
*/
|
|
62
|
+
const trySetState = React.useCallback((newState) => {
|
|
63
|
+
/** Update the state only if current prop is not */
|
|
64
|
+
if (isAutoControlled.current) {
|
|
65
|
+
setState(newState);
|
|
66
|
+
}
|
|
67
|
+
}, [setState]);
|
|
68
|
+
/** Return the Hook Result with state and utilities */
|
|
69
|
+
return [
|
|
70
|
+
value === undefined ? state : value,
|
|
71
|
+
trySetState,
|
|
72
|
+
setState
|
|
73
|
+
];
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
export { useAutoControlledState };
|
|
@@ -1,60 +1,60 @@
|
|
|
1
1
|
import { useEnhancedEffect } from './useEnhancedEffect.js';
|
|
2
2
|
import { useSyncedRef } from './useSyncedRef.js';
|
|
3
3
|
|
|
4
|
-
/** The default events name to listen to */
|
|
5
|
-
const DEFAULT_EVENTS = [
|
|
6
|
-
'mousedown',
|
|
7
|
-
'touchstart'
|
|
8
|
-
];
|
|
9
|
-
/**
|
|
10
|
-
* Use this hook to fire an event every time a click occurs outside
|
|
11
|
-
* the provided target element
|
|
12
|
-
* @param target
|
|
13
|
-
* @param callback
|
|
14
|
-
* @param events
|
|
15
|
-
*/
|
|
16
|
-
function useClickOutside(target, callback, events = DEFAULT_EVENTS) {
|
|
17
|
-
/** Wrap dependent data into synced ref to maintain immutable */
|
|
18
|
-
const targetElement = useSyncedRef(target);
|
|
19
|
-
const callbackFunction = useSyncedRef(callback);
|
|
20
|
-
/** Use the Enhanced Effect to attach event listeners */
|
|
21
|
-
useEnhancedEffect(() => {
|
|
22
|
-
/**
|
|
23
|
-
* Internal function used to check if the event
|
|
24
|
-
* target received from the listener it is contained within the
|
|
25
|
-
* requested target element provided within the hook
|
|
26
|
-
*
|
|
27
|
-
* @param event HTML Event received
|
|
28
|
-
*/
|
|
29
|
-
function handler(event) {
|
|
30
|
-
/** Assert the target element exists */
|
|
31
|
-
if (!targetElement.current.current) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
/** Extract the event target */
|
|
35
|
-
const { target: eventTarget } = event;
|
|
36
|
-
const cb = callbackFunction.current;
|
|
37
|
-
if (!eventTarget
|
|
38
|
-
|| (!!eventTarget && !targetElement.current.current.contains(eventTarget))) {
|
|
39
|
-
/** Call the callback */
|
|
40
|
-
cb.call(this, event);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
/** Attach the events on all requested keys */
|
|
44
|
-
events.forEach((eventName) => {
|
|
45
|
-
/** List for event directly on document element */
|
|
46
|
-
document.addEventListener(eventName, handler, { passive: true });
|
|
47
|
-
});
|
|
48
|
-
/** On effect clear, remove all events */
|
|
49
|
-
return () => {
|
|
50
|
-
events.forEach((eventName) => {
|
|
51
|
-
/** List for event directly on document element */
|
|
52
|
-
document.removeEventListener(eventName, handler);
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
},
|
|
56
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
57
|
-
[callbackFunction, targetElement, ...events]);
|
|
4
|
+
/** The default events name to listen to */
|
|
5
|
+
const DEFAULT_EVENTS = [
|
|
6
|
+
'mousedown',
|
|
7
|
+
'touchstart'
|
|
8
|
+
];
|
|
9
|
+
/**
|
|
10
|
+
* Use this hook to fire an event every time a click occurs outside
|
|
11
|
+
* the provided target element
|
|
12
|
+
* @param target
|
|
13
|
+
* @param callback
|
|
14
|
+
* @param events
|
|
15
|
+
*/
|
|
16
|
+
function useClickOutside(target, callback, events = DEFAULT_EVENTS) {
|
|
17
|
+
/** Wrap dependent data into synced ref to maintain immutable */
|
|
18
|
+
const targetElement = useSyncedRef(target);
|
|
19
|
+
const callbackFunction = useSyncedRef(callback);
|
|
20
|
+
/** Use the Enhanced Effect to attach event listeners */
|
|
21
|
+
useEnhancedEffect(() => {
|
|
22
|
+
/**
|
|
23
|
+
* Internal function used to check if the event
|
|
24
|
+
* target received from the listener it is contained within the
|
|
25
|
+
* requested target element provided within the hook
|
|
26
|
+
*
|
|
27
|
+
* @param event HTML Event received
|
|
28
|
+
*/
|
|
29
|
+
function handler(event) {
|
|
30
|
+
/** Assert the target element exists */
|
|
31
|
+
if (!targetElement.current.current) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
/** Extract the event target */
|
|
35
|
+
const { target: eventTarget } = event;
|
|
36
|
+
const cb = callbackFunction.current;
|
|
37
|
+
if (!eventTarget
|
|
38
|
+
|| (!!eventTarget && !targetElement.current.current.contains(eventTarget))) {
|
|
39
|
+
/** Call the callback */
|
|
40
|
+
cb.call(this, event);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Attach the events on all requested keys */
|
|
44
|
+
events.forEach((eventName) => {
|
|
45
|
+
/** List for event directly on document element */
|
|
46
|
+
document.addEventListener(eventName, handler, { passive: true });
|
|
47
|
+
});
|
|
48
|
+
/** On effect clear, remove all events */
|
|
49
|
+
return () => {
|
|
50
|
+
events.forEach((eventName) => {
|
|
51
|
+
/** List for event directly on document element */
|
|
52
|
+
document.removeEventListener(eventName, handler);
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
57
|
+
[callbackFunction, targetElement, ...events]);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
export { useClickOutside };
|