@aiszlab/relax 1.0.19 → 1.0.20
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/dist/hooks/use-controlled-state.d.ts +2 -2
- package/dist/hooks/use-controlled-state.js +13 -15
- package/dist/hooks/use-once-state.d.ts +0 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.js +3 -0
- package/dist/utils/is-refable.d.ts +5 -0
- package/dist/utils/is-refable.js +34 -0
- package/dist/utils/is-state-getter.d.ts +14 -0
- package/dist/utils/is-state-getter.js +9 -0
- package/dist/utils/is-void.d.ts +5 -0
- package/dist/utils/is-void.js +9 -0
- package/package.json +3 -1
- package/dist/types/state.d.ts +0 -8
- package/dist/utils/state.d.ts +0 -6
- package/dist/utils/state.js +0 -9
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Dispatch, type SetStateAction } from 'react';
|
|
2
|
-
import type
|
|
2
|
+
import { type State } from '../utils/is-state-getter';
|
|
3
3
|
interface Props<T> {
|
|
4
4
|
defaultState?: State<T>;
|
|
5
5
|
}
|
|
@@ -9,5 +9,5 @@ interface Props<T> {
|
|
|
9
9
|
* @description
|
|
10
10
|
* controlled state
|
|
11
11
|
*/
|
|
12
|
-
export declare const useControlledState: <T>(controlledState: State<T>,
|
|
12
|
+
export declare const useControlledState: <T>(controlledState: State<T>, { defaultState }?: Props<T>) => [T, Dispatch<SetStateAction<T>>];
|
|
13
13
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useState, useEffect } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { isStateGetter } from '../utils/is-state-getter.js';
|
|
3
|
+
import { isVoid } from '../utils/is-void.js';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* @author murukal
|
|
@@ -7,31 +8,28 @@ import { isFunction } from '../utils/state.js';
|
|
|
7
8
|
* @description
|
|
8
9
|
* controlled state
|
|
9
10
|
*/
|
|
10
|
-
const useControlledState = (controlledState,
|
|
11
|
+
const useControlledState = (controlledState, { defaultState } = {}) => {
|
|
12
|
+
/// initialize state
|
|
11
13
|
const [state, setState] = useState(() => {
|
|
12
|
-
if (
|
|
14
|
+
if (isStateGetter(controlledState))
|
|
13
15
|
return controlledState();
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if ((props === null || props === void 0 ? void 0 : props.defaultState) === void 0) {
|
|
16
|
+
if (isVoid(controlledState)) {
|
|
17
|
+
if (isVoid(defaultState))
|
|
17
18
|
return controlledState;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
return props.defaultState;
|
|
19
|
+
if (isStateGetter(defaultState))
|
|
20
|
+
return defaultState();
|
|
21
|
+
return defaultState;
|
|
23
22
|
}
|
|
24
23
|
return controlledState;
|
|
25
24
|
});
|
|
26
25
|
useEffect(() => {
|
|
27
26
|
// when state is not controlled
|
|
28
|
-
if (controlledState
|
|
27
|
+
if (isVoid(controlledState))
|
|
29
28
|
return;
|
|
30
|
-
}
|
|
31
29
|
// if state is equal with value
|
|
32
|
-
if (controlledState === state)
|
|
30
|
+
if (controlledState === state)
|
|
33
31
|
return;
|
|
34
|
-
|
|
32
|
+
/// update inner state
|
|
35
33
|
setState(controlledState);
|
|
36
34
|
}, [controlledState]);
|
|
37
35
|
return [state, setState];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description
|
|
3
|
+
* hooks
|
|
4
|
+
*/
|
|
1
5
|
export { useBoolean } from './hooks/use-boolean';
|
|
2
6
|
export { useDebounceCallback } from './hooks/use-debounce-callback';
|
|
3
7
|
export { useImageLoader } from './hooks/use-image-loader';
|
|
@@ -6,3 +10,10 @@ export { useMounted } from './hooks/use-mounted';
|
|
|
6
10
|
export { useTimeout } from './hooks/use-timeout';
|
|
7
11
|
export { useControlledState } from './hooks/use-controlled-state';
|
|
8
12
|
export { useOnceState } from './hooks/use-once-state';
|
|
13
|
+
/**
|
|
14
|
+
* @description
|
|
15
|
+
* utils
|
|
16
|
+
*/
|
|
17
|
+
export { isRefable } from './utils/is-refable';
|
|
18
|
+
export { isVoid } from './utils/is-void';
|
|
19
|
+
export { isStateGetter } from './utils/is-state-getter';
|
package/dist/index.js
CHANGED
|
@@ -6,3 +6,6 @@ export { useMounted } from './hooks/use-mounted.js';
|
|
|
6
6
|
export { useTimeout } from './hooks/use-timeout.js';
|
|
7
7
|
export { useControlledState } from './hooks/use-controlled-state.js';
|
|
8
8
|
export { useOnceState } from './hooks/use-once-state.js';
|
|
9
|
+
export { isRefable } from './utils/is-refable.js';
|
|
10
|
+
export { isVoid } from './utils/is-void.js';
|
|
11
|
+
export { isStateGetter } from './utils/is-state-getter.js';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isValidElement } from 'react';
|
|
2
|
+
import { isFragment, isMemo } from 'react-is';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
*/
|
|
7
|
+
const isRefable = (node) => {
|
|
8
|
+
if (!isValidElement(node)) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
if (isFragment(node)) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return _RefableElement(node);
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* @description
|
|
18
|
+
* refable element
|
|
19
|
+
*/
|
|
20
|
+
const _RefableElement = (element) => {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
const type = isMemo(element) ? element.type.type : element.type;
|
|
23
|
+
// Function component node
|
|
24
|
+
if (typeof type === 'function' && !((_a = type.prototype) === null || _a === void 0 ? void 0 : _a.render)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
// Class component
|
|
28
|
+
if (typeof element === 'function' && !((_b = element.prototype) === null || _b === void 0 ? void 0 : _b.render)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { isRefable };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type StateGetter<T> = () => T;
|
|
2
|
+
/**
|
|
3
|
+
* @description
|
|
4
|
+
* state setter
|
|
5
|
+
*
|
|
6
|
+
* used by initial state, default state, controlled state
|
|
7
|
+
*/
|
|
8
|
+
export type State<T> = T | StateGetter<T>;
|
|
9
|
+
/**
|
|
10
|
+
* @description
|
|
11
|
+
* is state getter
|
|
12
|
+
*/
|
|
13
|
+
export declare const isStateGetter: <T>(state: State<T>) => state is StateGetter<T>;
|
|
14
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiszlab/relax",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.20",
|
|
4
4
|
"description": "react utils collection",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"pub": "npm run clean:build && npm run build && npm publish"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
+
"react-is": "^18.2.0",
|
|
14
15
|
"rxjs": "^7.8.1"
|
|
15
16
|
},
|
|
16
17
|
"devDependencies": {
|
|
@@ -22,6 +23,7 @@
|
|
|
22
23
|
"@rollup/plugin-typescript": "^11.1.1",
|
|
23
24
|
"@types/react": "^18.2.12",
|
|
24
25
|
"@types/react-dom": "^18.2.5",
|
|
26
|
+
"@types/react-is": "^18.2.2",
|
|
25
27
|
"rollup": "^3.27.0",
|
|
26
28
|
"typescript": "^5.1.3"
|
|
27
29
|
},
|
package/dist/types/state.d.ts
DELETED
package/dist/utils/state.d.ts
DELETED