@aiszlab/relax 1.2.11 → 1.2.12
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,6 +1,6 @@
|
|
|
1
1
|
import { type Dispatch, type SetStateAction } from 'react';
|
|
2
2
|
import { type State } from '../is/is-state-getter';
|
|
3
|
-
interface
|
|
3
|
+
interface Dependencies<T> {
|
|
4
4
|
defaultState?: State<T>;
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
@@ -9,5 +9,5 @@ interface Props<T> {
|
|
|
9
9
|
* @description
|
|
10
10
|
* controlled state
|
|
11
11
|
*/
|
|
12
|
-
export declare const useControlledState: <T>(controlledState:
|
|
12
|
+
export declare const useControlledState: <T>(controlledState: T, { defaultState }?: Dependencies<T>) => [T, Dispatch<SetStateAction<T>>];
|
|
13
13
|
export {};
|
|
@@ -10,35 +10,27 @@ import { isUndefined } from '../is/is-undefined.js';
|
|
|
10
10
|
*/
|
|
11
11
|
const useControlledState = (controlledState, { defaultState } = {}) => {
|
|
12
12
|
/// initialize state
|
|
13
|
-
const [
|
|
14
|
-
// state function, run it for value
|
|
15
|
-
if (isStateGetter(controlledState))
|
|
16
|
-
return controlledState();
|
|
17
|
-
// not controlled use default prop
|
|
18
|
-
if (isUndefined(controlledState)) {
|
|
19
|
-
if (isUndefined(defaultState))
|
|
20
|
-
return controlledState;
|
|
21
|
-
if (isStateGetter(defaultState))
|
|
22
|
-
return defaultState();
|
|
23
|
-
return defaultState;
|
|
24
|
-
}
|
|
13
|
+
const [_state, _setState] = useState(() => {
|
|
25
14
|
// default use controlled state
|
|
26
|
-
|
|
15
|
+
if (!isUndefined(controlledState)) {
|
|
16
|
+
return controlledState;
|
|
17
|
+
}
|
|
18
|
+
// not controlled use default prop
|
|
19
|
+
if (isUndefined(defaultState))
|
|
20
|
+
return controlledState;
|
|
21
|
+
if (isStateGetter(defaultState))
|
|
22
|
+
return defaultState();
|
|
23
|
+
return defaultState;
|
|
27
24
|
});
|
|
25
|
+
/// sync value back to `undefined` when it from control to un-control
|
|
28
26
|
useEffect(() => {
|
|
29
|
-
|
|
30
|
-
if (isUndefined(controlledState))
|
|
31
|
-
return;
|
|
32
|
-
// if input state is function, mean it is not controlled
|
|
33
|
-
if (isStateGetter(controlledState))
|
|
34
|
-
return;
|
|
35
|
-
// if state is equal with value
|
|
36
|
-
if (controlledState === state)
|
|
27
|
+
if (!isUndefined(controlledState))
|
|
37
28
|
return;
|
|
38
|
-
|
|
39
|
-
setState(controlledState);
|
|
29
|
+
_setState(controlledState);
|
|
40
30
|
}, [controlledState]);
|
|
41
|
-
|
|
31
|
+
/// use controlled
|
|
32
|
+
const state = !isUndefined(controlledState) ? controlledState : _state;
|
|
33
|
+
return [state, _setState];
|
|
42
34
|
};
|
|
43
35
|
|
|
44
36
|
export { useControlledState };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiszlab/relax",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.12",
|
|
4
4
|
"description": "react utils collection",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"@rollup/plugin-babel": "^6.0.3",
|
|
25
25
|
"@rollup/plugin-node-resolve": "^15.1.0",
|
|
26
26
|
"@rollup/plugin-typescript": "^11.1.1",
|
|
27
|
-
"@types/react": "^18
|
|
28
|
-
"@types/react-dom": "^18
|
|
29
|
-
"@types/react-is": "^18
|
|
27
|
+
"@types/react": "^18",
|
|
28
|
+
"@types/react-dom": "^18",
|
|
29
|
+
"@types/react-is": "^18",
|
|
30
30
|
"rollup": "^3.27.0",
|
|
31
31
|
"typescript": "^5.1.3"
|
|
32
32
|
},
|