@proedis/react 1.2.4 → 1.3.1

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
@@ -4,154 +4,154 @@ import clsx from 'clsx';
4
4
  import kind_of from 'kind-of';
5
5
  import { isNil } from '@proedis/utils';
6
6
 
7
- /* --------
8
- * Utilities
9
- * -------- */
10
- /**
11
- * Starting from the provided Component, render the element
12
- * mapping the props using the Shorthand value argument
13
- *
14
- * @param Component The component to Render
15
- * @param mapValueToProps Function used to map the value to Component Props
16
- * @param computeComponentKey Optional function to use to compute the component key
17
- * @param value The value to use while rendering the component
18
- * @param options Component render options
19
- */
20
- function createComponentShorthand(Component, mapValueToProps, computeComponentKey, value, options) {
21
- /**
22
- * If provided value is nil, or boolean
23
- * avoid to render the component
24
- */
25
- if (isNil(value) || typeof value === 'boolean') {
26
- return null;
27
- }
28
- // ----
29
- // Computing value type and assert is valid
30
- // ----
31
- const valueIsString = typeof value === 'string';
32
- const valueIsNumber = typeof value === 'number' && !Number.isNaN(value);
33
- const valueIsReactElement = React.isValidElement(value);
34
- const valueIsPropObject = kind_of(value) === 'object';
35
- const valueIsPrimitiveValue = valueIsString || valueIsNumber || kind_of(value) === 'array';
36
- /** Check the validity of provided value, logging the error only in production */
37
- if (!valueIsReactElement && !valueIsPrimitiveValue && !valueIsPropObject) {
38
- if (process.env.NODE_ENV !== 'production') {
39
- console.error([
40
- 'Shorthand value must be a string|number|array|object|ReactElement|function.',
41
- 'Use null|undefined|boolean for none.',
42
- `Received ${kind_of(value)}`
43
- ].join(' '));
44
- }
45
- return null;
46
- }
47
- // ----
48
- // Component Props Building
49
- // ----
50
- /** Extract default props from options */
51
- const { defaultProps } = options;
52
- /** Get the user defined props, if the component is a valid React element */
53
- const userProps = (valueIsReactElement && value.props)
54
- || (valueIsPropObject && value)
55
- || (valueIsPrimitiveValue && mapValueToProps(value))
56
- || {};
57
- /** Extract the override props from options */
58
- let { overrideProps } = options;
59
- /** Compute override props function */
60
- if (typeof overrideProps === 'function') {
61
- overrideProps = overrideProps({
62
- ...defaultProps,
63
- ...userProps
64
- });
65
- }
66
- /** Merge final props to a single props object */
67
- const props = {
68
- ...defaultProps,
69
- ...userProps,
70
- ...overrideProps
71
- };
72
- /** Merge className from props */
73
- if (defaultProps?.className || overrideProps?.className || userProps.className) {
74
- const mergedClassNames = clsx(defaultProps?.className, userProps?.className, overrideProps?.className);
75
- props.className = Array.from(new Set(mergedClassNames.split(' ')).values()).join(' ');
76
- }
77
- /** Merge the style */
78
- if (defaultProps?.style || overrideProps?.style || userProps.style) {
79
- props.style = {
80
- ...defaultProps?.style,
81
- ...userProps.style,
82
- ...overrideProps?.style
83
- };
84
- }
85
- // ----
86
- // Component Key Computing
87
- // ----
88
- if (props.key == null) {
89
- /** Get options to generate the key */
90
- const { autoGenerateKey, childKey } = options;
91
- if (typeof computeComponentKey === 'function') {
92
- props.key = computeComponentKey(props);
93
- }
94
- else if (typeof childKey === 'function') {
95
- props.key = childKey(props);
96
- }
97
- else if (autoGenerateKey && (valueIsString || valueIsNumber)) {
98
- props.key = value;
99
- }
100
- }
101
- // ----
102
- // Component Render
103
- // ----
104
- /** If provided value is a valid React Element, wrap using Component */
105
- if (valueIsReactElement) {
106
- return React.cloneElement(value, props);
107
- }
108
- /** If the value is a primitive value, or a plain props object, use base Component */
109
- if (valueIsPrimitiveValue || valueIsPropObject) {
110
- return (jsx(Component, { ...props }));
111
- }
112
- /** Fallback to null */
113
- return null;
114
- }
115
- /* --------
116
- * Factory Methods
117
- * -------- */
118
- /**
119
- * Create a shorthand function that could be used to easily render component.
120
- *
121
- * @param Component The component to use
122
- * @param mapValueToProps A function to use to map value to component Props
123
- * @param computeComponentKey A custom function to compute the component key
124
- */
125
- function createShorthandFactory(Component, mapValueToProps, computeComponentKey) {
126
- return function createComponent(value, options) {
127
- return createComponentShorthand(Component, mapValueToProps, computeComponentKey, value, options);
128
- };
129
- }
130
- /**
131
- * Automatically add the 'create' shorthand method to the provided component
132
- * and return it strongly typed as CreatableComponent
133
- *
134
- * @param Component The component to use
135
- * @param mapValueToProps A function to use to map value to component Props
136
- * @param computeComponentKey A custom function to compute the component key
137
- */
138
- function creatableComponent(Component, mapValueToProps, computeComponentKey) {
139
- /** Attach create method to the provided Component function */
140
- Component.create =
141
- createShorthandFactory(Component, mapValueToProps, computeComponentKey);
142
- /** Return the Component */
143
- return Component;
144
- }
145
- /* --------
146
- * Render Shorthand Content
147
- * -------- */
148
- function renderShorthandContent({ children, content }) {
149
- /** Render children if defined */
150
- if (children === null || children === undefined || (Array.isArray(children) && children.length === 0)) {
151
- return content;
152
- }
153
- /** Fallback to content */
154
- return children;
7
+ /* --------
8
+ * Utilities
9
+ * -------- */
10
+ /**
11
+ * Starting from the provided Component, render the element
12
+ * mapping the props using the Shorthand value argument
13
+ *
14
+ * @param Component The component to Render
15
+ * @param mapValueToProps Function used to map the value to Component Props
16
+ * @param computeComponentKey Optional function to use to compute the component key
17
+ * @param value The value to use while rendering the component
18
+ * @param options Component render options
19
+ */
20
+ function createComponentShorthand(Component, mapValueToProps, computeComponentKey, value, options) {
21
+ /**
22
+ * If provided value is nil, or boolean
23
+ * avoid to render the component
24
+ */
25
+ if (isNil(value) || typeof value === 'boolean') {
26
+ return null;
27
+ }
28
+ // ----
29
+ // Computing value type and assert is valid
30
+ // ----
31
+ const valueIsString = typeof value === 'string';
32
+ const valueIsNumber = typeof value === 'number' && !Number.isNaN(value);
33
+ const valueIsReactElement = React.isValidElement(value);
34
+ const valueIsPropObject = kind_of(value) === 'object';
35
+ const valueIsPrimitiveValue = valueIsString || valueIsNumber || kind_of(value) === 'array';
36
+ /** Check the validity of provided value, logging the error only in production */
37
+ if (!valueIsReactElement && !valueIsPrimitiveValue && !valueIsPropObject) {
38
+ if (process.env.NODE_ENV !== 'production') {
39
+ console.error([
40
+ 'Shorthand value must be a string|number|array|object|ReactElement|function.',
41
+ 'Use null|undefined|boolean for none.',
42
+ `Received ${kind_of(value)}`
43
+ ].join(' '));
44
+ }
45
+ return null;
46
+ }
47
+ // ----
48
+ // Component Props Building
49
+ // ----
50
+ /** Extract default props from options */
51
+ const { defaultProps } = options;
52
+ /** Get the user defined props, if the component is a valid React element */
53
+ const userProps = (valueIsReactElement && value.props)
54
+ || (valueIsPropObject && value)
55
+ || (valueIsPrimitiveValue && mapValueToProps(value))
56
+ || {};
57
+ /** Extract the override props from options */
58
+ let { overrideProps } = options;
59
+ /** Compute override props function */
60
+ if (typeof overrideProps === 'function') {
61
+ overrideProps = overrideProps({
62
+ ...defaultProps,
63
+ ...userProps
64
+ });
65
+ }
66
+ /** Merge final props to a single props object */
67
+ const props = {
68
+ ...defaultProps,
69
+ ...userProps,
70
+ ...overrideProps
71
+ };
72
+ /** Merge className from props */
73
+ if (defaultProps?.className || overrideProps?.className || userProps.className) {
74
+ const mergedClassNames = clsx(defaultProps?.className, userProps?.className, overrideProps?.className);
75
+ props.className = Array.from(new Set(mergedClassNames.split(' ')).values()).join(' ');
76
+ }
77
+ /** Merge the style */
78
+ if (defaultProps?.style || overrideProps?.style || userProps.style) {
79
+ props.style = {
80
+ ...defaultProps?.style,
81
+ ...userProps.style,
82
+ ...overrideProps?.style
83
+ };
84
+ }
85
+ // ----
86
+ // Component Key Computing
87
+ // ----
88
+ if (props.key == null) {
89
+ /** Get options to generate the key */
90
+ const { autoGenerateKey, childKey } = options;
91
+ if (typeof computeComponentKey === 'function') {
92
+ props.key = computeComponentKey(props);
93
+ }
94
+ else if (typeof childKey === 'function') {
95
+ props.key = childKey(props);
96
+ }
97
+ else if (autoGenerateKey && (valueIsString || valueIsNumber)) {
98
+ props.key = value;
99
+ }
100
+ }
101
+ // ----
102
+ // Component Render
103
+ // ----
104
+ /** If provided value is a valid React Element, wrap using Component */
105
+ if (valueIsReactElement) {
106
+ return React.cloneElement(value, props);
107
+ }
108
+ /** If the value is a primitive value, or a plain props object, use base Component */
109
+ if (valueIsPrimitiveValue || valueIsPropObject) {
110
+ return (jsx(Component, { ...props }));
111
+ }
112
+ /** Fallback to null */
113
+ return null;
114
+ }
115
+ /* --------
116
+ * Factory Methods
117
+ * -------- */
118
+ /**
119
+ * Create a shorthand function that could be used to easily render component.
120
+ *
121
+ * @param Component The component to use
122
+ * @param mapValueToProps A function to use to map value to component Props
123
+ * @param computeComponentKey A custom function to compute the component key
124
+ */
125
+ function createShorthandFactory(Component, mapValueToProps, computeComponentKey) {
126
+ return function createComponent(value, options) {
127
+ return createComponentShorthand(Component, mapValueToProps, computeComponentKey, value, options);
128
+ };
129
+ }
130
+ /**
131
+ * Automatically add the 'create' shorthand method to the provided component
132
+ * and return it strongly typed as CreatableComponent
133
+ *
134
+ * @param Component The component to use
135
+ * @param mapValueToProps A function to use to map value to component Props
136
+ * @param computeComponentKey A custom function to compute the component key
137
+ */
138
+ function creatableComponent(Component, mapValueToProps, computeComponentKey) {
139
+ /** Attach create method to the provided Component function */
140
+ Component.create =
141
+ createShorthandFactory(Component, mapValueToProps, computeComponentKey);
142
+ /** Return the Component */
143
+ return Component;
144
+ }
145
+ /* --------
146
+ * Render Shorthand Content
147
+ * -------- */
148
+ function renderShorthandContent({ children, content }) {
149
+ /** Render children if defined */
150
+ if (children === null || children === undefined || (Array.isArray(children) && children.length === 0)) {
151
+ return content;
152
+ }
153
+ /** Fallback to content */
154
+ return children;
155
155
  }
156
156
 
157
157
  export { creatableComponent, createShorthandFactory, renderShorthandContent };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@proedis/react",
3
- "version": "1.2.4",
3
+ "version": "1.3.1",
4
4
  "description": "A set of React Hooks, that add UI or Core functionality to your Apps",
5
5
  "keywords": [
6
6
  "proedis",
@@ -8,16 +8,29 @@
8
8
  "hooks"
9
9
  ],
10
10
  "main": "./cjs/index.js",
11
+ "dependencies": {
12
+ "@proedis/types": "1.1.0",
13
+ "@proedis/utils": "1.3.1",
14
+ "clsx": "^1.2.1",
15
+ "kind-of": "^6.0.3"
16
+ },
17
+ "peerDependencies": {
18
+ "react": "^18.2.0"
19
+ },
20
+ "module": "./esm/index.js",
21
+ "types": "./types/index.d.ts",
11
22
  "license": "MIT",
23
+ "private": false,
12
24
  "author": {
13
- "name": "Marco Cavanna",
14
- "email": "m.cavanna@proedis.net"
25
+ "name": "Proedis S.r.l.",
26
+ "url": "https://proedis.net",
27
+ "email": "info@proedis.net"
15
28
  },
16
29
  "homepage": "https://github.com/proedis/Npm.Suite",
17
30
  "repository": {
18
31
  "type": "git",
19
32
  "url": "https://github.com/proedis/Npm.Suite.git",
20
- "directory": "packages/react-hooks"
33
+ "directory": "packages/react"
21
34
  },
22
35
  "bugs": {
23
36
  "url": "https://github.com/proedis/Npm.Suite/issues",
@@ -25,20 +38,5 @@
25
38
  },
26
39
  "publishConfig": {
27
40
  "access": "public"
28
- },
29
- "dependencies": {
30
- "@proedis/types": "1.0.11",
31
- "@proedis/utils": "1.2.4",
32
- "clsx": "^1.2.1",
33
- "kind-of": "^6.0.3",
34
- "qs": "^6.11.0"
35
- },
36
- "peerDependencies": {
37
- "react": "^18.2.0",
38
- "react-router-dom": "^6.8.1"
39
- },
40
- "gitHead": "e5cd5bc288cc6ba93f716278a9401c9a45abc0e8",
41
- "private": false,
42
- "module": "./esm/index.js",
43
- "types": "./types/index.d.ts"
44
- }
41
+ }
42
+ }