@aiszlab/relax 1.0.23 → 1.0.25
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.
|
@@ -11,8 +11,10 @@ import { isUndefined } from '../utils/is-undefined.js';
|
|
|
11
11
|
const useControlledState = (controlledState, { defaultState } = {}) => {
|
|
12
12
|
/// initialize state
|
|
13
13
|
const [state, setState] = useState(() => {
|
|
14
|
+
// state function, run it for value
|
|
14
15
|
if (isStateGetter(controlledState))
|
|
15
16
|
return controlledState();
|
|
17
|
+
// not controlled use default prop
|
|
16
18
|
if (isUndefined(controlledState)) {
|
|
17
19
|
if (isUndefined(defaultState))
|
|
18
20
|
return controlledState;
|
|
@@ -20,16 +22,20 @@ const useControlledState = (controlledState, { defaultState } = {}) => {
|
|
|
20
22
|
return defaultState();
|
|
21
23
|
return defaultState;
|
|
22
24
|
}
|
|
25
|
+
// default use controlled state
|
|
23
26
|
return controlledState;
|
|
24
27
|
});
|
|
25
28
|
useEffect(() => {
|
|
26
29
|
// when state is not controlled
|
|
27
30
|
if (isUndefined(controlledState))
|
|
28
31
|
return;
|
|
32
|
+
// if input state is function, mean it is not controlled
|
|
33
|
+
if (isStateGetter(controlledState))
|
|
34
|
+
return;
|
|
29
35
|
// if state is equal with value
|
|
30
36
|
if (controlledState === state)
|
|
31
37
|
return;
|
|
32
|
-
|
|
38
|
+
// update inner state
|
|
33
39
|
setState(controlledState);
|
|
34
40
|
}, [controlledState]);
|
|
35
41
|
return [state, setState];
|