@aiszlab/relax 1.0.16 → 1.0.18
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.
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { type Dispatch, type SetStateAction } from 'react';
|
|
2
|
+
interface Props<T> {
|
|
3
|
+
defaultState: T;
|
|
4
|
+
}
|
|
2
5
|
/**
|
|
3
6
|
* @author murukal
|
|
4
7
|
*
|
|
5
8
|
* @description
|
|
6
9
|
* controlled state
|
|
7
10
|
*/
|
|
8
|
-
export declare const useControlledState: <T>(
|
|
11
|
+
export declare const useControlledState: <T>(controlledState?: T | (() => T) | undefined, props?: Props<T> | undefined) => [T | undefined, Dispatch<SetStateAction<T | undefined>>];
|
|
12
|
+
export {};
|
|
@@ -6,19 +6,27 @@ import { useState, useEffect } from 'react';
|
|
|
6
6
|
* @description
|
|
7
7
|
* controlled state
|
|
8
8
|
*/
|
|
9
|
-
const useControlledState = (
|
|
10
|
-
const [state, setState] = useState(
|
|
9
|
+
const useControlledState = (controlledState, props) => {
|
|
10
|
+
const [state, setState] = useState(() => {
|
|
11
|
+
if (controlledState === void 0) {
|
|
12
|
+
return props === null || props === void 0 ? void 0 : props.defaultState;
|
|
13
|
+
}
|
|
14
|
+
if (typeof controlledState === 'function') {
|
|
15
|
+
return controlledState();
|
|
16
|
+
}
|
|
17
|
+
return controlledState;
|
|
18
|
+
});
|
|
11
19
|
useEffect(() => {
|
|
12
20
|
// when state is not controlled
|
|
13
|
-
if (
|
|
21
|
+
if (controlledState === void 0) {
|
|
14
22
|
return;
|
|
15
23
|
}
|
|
16
24
|
// if state is equal with value
|
|
17
|
-
if (
|
|
25
|
+
if (controlledState === state) {
|
|
18
26
|
return;
|
|
19
27
|
}
|
|
20
|
-
setState(
|
|
21
|
-
}, [
|
|
28
|
+
setState(controlledState);
|
|
29
|
+
}, [controlledState]);
|
|
22
30
|
return [state, setState];
|
|
23
31
|
};
|
|
24
32
|
|
package/dist/use-once-state.d.ts
CHANGED
package/dist/use-once-state.js
CHANGED