@aiszlab/relax 1.0.22 → 1.0.24
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 +1 -5
- package/dist/hooks/use-controlled-state.js +6 -10
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/utils/is-null.d.ts +5 -0
- package/dist/utils/is-null.js +9 -0
- package/dist/utils/is-undefined.d.ts +5 -0
- package/dist/utils/is-undefined.js +9 -0
- package/dist/utils/is-void.d.ts +2 -2
- package/dist/utils/is-void.js +5 -2
- package/package.json +1 -1
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import { type Dispatch, type SetStateAction } from 'react';
|
|
2
2
|
import { type State } from '../utils/is-state-getter';
|
|
3
|
-
interface Props<T> {
|
|
4
|
-
defaultState?: State<T>;
|
|
5
|
-
}
|
|
6
3
|
/**
|
|
7
4
|
* @author murukal
|
|
8
5
|
*
|
|
9
6
|
* @description
|
|
10
7
|
* controlled state
|
|
11
8
|
*/
|
|
12
|
-
export declare const useControlledState: <T>(controlledState: State<T
|
|
13
|
-
export {};
|
|
9
|
+
export declare const useControlledState: <T>(controlledState: State<T>) => [T, Dispatch<SetStateAction<T>>];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useState, useEffect } from 'react';
|
|
2
2
|
import { isStateGetter } from '../utils/is-state-getter.js';
|
|
3
|
-
import {
|
|
3
|
+
import { isUndefined } from '../utils/is-undefined.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @author murukal
|
|
@@ -8,23 +8,19 @@ import { isVoid } from '../utils/is-void.js';
|
|
|
8
8
|
* @description
|
|
9
9
|
* controlled state
|
|
10
10
|
*/
|
|
11
|
-
const useControlledState = (controlledState
|
|
11
|
+
const useControlledState = (controlledState) => {
|
|
12
12
|
/// initialize state
|
|
13
13
|
const [state, setState] = useState(() => {
|
|
14
14
|
if (isStateGetter(controlledState))
|
|
15
15
|
return controlledState();
|
|
16
|
-
if (isVoid(controlledState)) {
|
|
17
|
-
if (isVoid(defaultState))
|
|
18
|
-
return controlledState;
|
|
19
|
-
if (isStateGetter(defaultState))
|
|
20
|
-
return defaultState();
|
|
21
|
-
return defaultState;
|
|
22
|
-
}
|
|
23
16
|
return controlledState;
|
|
24
17
|
});
|
|
25
18
|
useEffect(() => {
|
|
26
19
|
// when state is not controlled
|
|
27
|
-
if (
|
|
20
|
+
if (isUndefined(controlledState))
|
|
21
|
+
return;
|
|
22
|
+
// if input state is function, mean it is not controlled
|
|
23
|
+
if (isStateGetter(controlledState))
|
|
28
24
|
return;
|
|
29
25
|
// if state is equal with value
|
|
30
26
|
if (controlledState === state)
|
package/dist/index.d.ts
CHANGED
|
@@ -15,5 +15,7 @@ export { useOnceState } from './hooks/use-once-state';
|
|
|
15
15
|
* utils
|
|
16
16
|
*/
|
|
17
17
|
export { isRefable } from './utils/is-refable';
|
|
18
|
-
export {
|
|
18
|
+
export { isUndefined } from './utils/is-undefined';
|
|
19
19
|
export { isStateGetter } from './utils/is-state-getter';
|
|
20
|
+
export { isNull } from './utils/is-null';
|
|
21
|
+
export { isVoid } from './utils/is-void';
|
package/dist/index.js
CHANGED
|
@@ -7,5 +7,7 @@ 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
9
|
export { isRefable } from './utils/is-refable.js';
|
|
10
|
-
export {
|
|
10
|
+
export { isUndefined } from './utils/is-undefined.js';
|
|
11
11
|
export { isStateGetter } from './utils/is-state-getter.js';
|
|
12
|
+
export { isNull } from './utils/is-null.js';
|
|
13
|
+
export { isVoid } from './utils/is-void.js';
|
package/dist/utils/is-void.d.ts
CHANGED
package/dist/utils/is-void.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { isUndefined } from './is-undefined.js';
|
|
2
|
+
import { isNull } from './is-null.js';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* @description
|
|
3
|
-
* is undefined
|
|
6
|
+
* is null or undefined
|
|
4
7
|
*/
|
|
5
8
|
const isVoid = (value) => {
|
|
6
|
-
return value
|
|
9
|
+
return isNull(value) || isUndefined(value);
|
|
7
10
|
};
|
|
8
11
|
|
|
9
12
|
export { isVoid };
|