@aiszlab/relax 1.2.69 → 1.2.71
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 +6 -4
- package/dist/hooks/use-controlled-state.js +14 -11
- package/dist/hooks/use-identity.d.ts +3 -1
- package/dist/hooks/use-local-storage.d.ts +2 -0
- package/dist/hooks/use-network.d.ts +1 -1
- package/dist/hooks/use-session-storage.d.ts +2 -0
- package/dist/hooks/use-storage.d.ts +5 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/required-to.d.ts +1 -0
- package/package.json +1 -1
- package/dist/is/is-state-getter.d.ts +0 -6
- package/dist/is/is-state-getter.js +0 -9
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { type Dispatch, type SetStateAction } from "react";
|
|
2
2
|
import type { State } from "../types";
|
|
3
|
-
|
|
3
|
+
type UseControlledStateBy<R> = {
|
|
4
4
|
/**
|
|
5
5
|
* @description
|
|
6
6
|
* default value
|
|
7
7
|
*/
|
|
8
|
-
defaultState?: State<
|
|
9
|
-
}
|
|
8
|
+
defaultState?: State<R>;
|
|
9
|
+
};
|
|
10
|
+
type UsedControlledState<T> = [T, Dispatch<SetStateAction<T>>];
|
|
11
|
+
type Requirable<T, P> = T extends undefined ? (P extends undefined ? T : Exclude<T, undefined>) : T;
|
|
10
12
|
/**
|
|
11
13
|
* @author murukal
|
|
12
14
|
*
|
|
13
15
|
* @description
|
|
14
16
|
* controlled state
|
|
15
17
|
*/
|
|
16
|
-
export declare const useControlledState: <T>(controlledState: T, { defaultState }?:
|
|
18
|
+
export declare const useControlledState: <T, P extends T = T>(controlledState: T, { defaultState }?: UseControlledStateBy<P>) => UsedControlledState<Requirable<T, P>>;
|
|
17
19
|
export {};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
2
2
|
import { useState } from 'react';
|
|
3
|
-
import { isStateGetter } from '../is/is-state-getter.js';
|
|
4
3
|
import { isUndefined } from '../is/is-undefined.js';
|
|
5
4
|
import { useUpdateEffect } from './use-update-effect.js';
|
|
5
|
+
import { isFunction } from '../is/is-function.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @author murukal
|
|
@@ -13,27 +13,30 @@ import { useUpdateEffect } from './use-update-effect.js';
|
|
|
13
13
|
var useControlledState = function useControlledState(controlledState) {
|
|
14
14
|
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
15
15
|
defaultState = _ref.defaultState;
|
|
16
|
-
|
|
16
|
+
// initialize state
|
|
17
17
|
var _useState = useState(function () {
|
|
18
18
|
// default use controlled state
|
|
19
19
|
if (!isUndefined(controlledState)) {
|
|
20
20
|
return controlledState;
|
|
21
21
|
}
|
|
22
|
-
// not controlled use default
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
// not controlled use default state
|
|
23
|
+
if (isFunction(defaultState)) {
|
|
24
|
+
return defaultState();
|
|
25
|
+
}
|
|
26
|
+
return defaultState !== null && defaultState !== void 0 ? defaultState : controlledState;
|
|
26
27
|
}),
|
|
27
28
|
_useState2 = _slicedToArray(_useState, 2),
|
|
28
29
|
_state = _useState2[0],
|
|
29
30
|
_setState = _useState2[1];
|
|
30
|
-
|
|
31
|
+
// sync value back to `undefined` when it from control to un-control
|
|
31
32
|
useUpdateEffect(function () {
|
|
32
|
-
if (!isUndefined(controlledState))
|
|
33
|
-
|
|
33
|
+
if (!isUndefined(controlledState)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
_setState(defaultState !== null && defaultState !== void 0 ? defaultState : controlledState);
|
|
34
37
|
}, [controlledState]);
|
|
35
|
-
|
|
36
|
-
var state =
|
|
38
|
+
// use controlled
|
|
39
|
+
var state = isUndefined(controlledState) ? _state : controlledState;
|
|
37
40
|
return [state, _setState];
|
|
38
41
|
};
|
|
39
42
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
type UsedIdentity = [string, () => string];
|
|
1
2
|
/**
|
|
2
3
|
* @description
|
|
3
4
|
* extends from react `useId`
|
|
4
5
|
*/
|
|
5
|
-
export declare const useIdentity: (signal?: string) =>
|
|
6
|
+
export declare const useIdentity: (signal?: string) => UsedIdentity;
|
|
7
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -36,7 +36,6 @@ export { useTimer } from "./hooks/use-timer";
|
|
|
36
36
|
*/
|
|
37
37
|
export { isRefable } from "./is/is-refable";
|
|
38
38
|
export { isUndefined } from "./is/is-undefined";
|
|
39
|
-
export { isStateGetter } from "./is/is-state-getter";
|
|
40
39
|
export { isNull } from "./is/is-null";
|
|
41
40
|
export { isVoid } from "./is/is-void";
|
|
42
41
|
export { isArray } from "./is/is-array";
|
package/dist/index.js
CHANGED
|
@@ -28,7 +28,6 @@ export { useUnmount } from './hooks/use-unmount.js';
|
|
|
28
28
|
export { useTimer } from './hooks/use-timer.js';
|
|
29
29
|
export { isRefable } from './is/is-refable.js';
|
|
30
30
|
export { isUndefined } from './is/is-undefined.js';
|
|
31
|
-
export { isStateGetter } from './is/is-state-getter.js';
|
|
32
31
|
export { isNull } from './is/is-null.js';
|
|
33
32
|
export { isVoid } from './is/is-void.js';
|
|
34
33
|
export { isArray } from './is/is-array.js';
|
package/dist/types/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type RequiredTo<T> = Exclude<T, undefined>;
|
package/package.json
CHANGED