@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.
@@ -1,17 +1,19 @@
1
1
  import { type Dispatch, type SetStateAction } from "react";
2
2
  import type { State } from "../types";
3
- interface Props<T> {
3
+ type UseControlledStateBy<R> = {
4
4
  /**
5
5
  * @description
6
6
  * default value
7
7
  */
8
- defaultState?: State<T>;
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 }?: Props<T>) => [T, Dispatch<SetStateAction<T>>];
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
- /// initialize state
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 prop
23
- if (isUndefined(defaultState)) return controlledState;
24
- if (isStateGetter(defaultState)) return defaultState();
25
- return defaultState;
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
- /// sync value back to `undefined` when it from control to un-control
31
+ // sync value back to `undefined` when it from control to un-control
31
32
  useUpdateEffect(function () {
32
- if (!isUndefined(controlledState)) return;
33
- _setState(controlledState !== null && controlledState !== void 0 ? controlledState : defaultState);
33
+ if (!isUndefined(controlledState)) {
34
+ return;
35
+ }
36
+ _setState(defaultState !== null && defaultState !== void 0 ? defaultState : controlledState);
34
37
  }, [controlledState]);
35
- /// use controlled
36
- var state = !isUndefined(controlledState) ? controlledState : _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) => [string, () => string];
6
+ export declare const useIdentity: (signal?: string) => UsedIdentity;
7
+ export {};
@@ -0,0 +1,2 @@
1
+ import { type UseStorageBy } from "./use-storage";
2
+ export declare const useLocalStorage: (key: string, useBy?: UseStorageBy) => import("./use-storage").UsedStorage;
@@ -2,4 +2,4 @@
2
2
  * @description
3
3
  * network
4
4
  */
5
- export declare const useNetwork: () => void;
5
+ export declare const useNetwork: () => boolean;
@@ -0,0 +1,2 @@
1
+ import { type UseStorageBy } from "./use-storage";
2
+ export declare const useSessionStorage: (key: string, useBy?: UseStorageBy) => import("./use-storage").UsedStorage;
@@ -0,0 +1,5 @@
1
+ export type UsedStorage = [string | null, (value: string | null) => void];
2
+ export type UseStorageBy = {
3
+ listen?: boolean;
4
+ };
5
+ export declare const useStorage: (key: string, storage: Storage, { listen }?: UseStorageBy) => UsedStorage;
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';
@@ -7,3 +7,4 @@ export type { First } from "./first";
7
7
  export type { Last } from "./last";
8
8
  export type { State, StateGetter } from "./state";
9
9
  export type { Arrayable } from "./arrayable";
10
+ export type { RequiredTo } from "./required-to";
@@ -0,0 +1 @@
1
+ export type RequiredTo<T> = Exclude<T, undefined>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.2.69",
3
+ "version": "1.2.71",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,6 +0,0 @@
1
- import type { State, StateGetter } from "../types";
2
- /**
3
- * @description
4
- * is state getter
5
- */
6
- export declare const isStateGetter: <T>(state: State<T>) => state is StateGetter<T>;
@@ -1,9 +0,0 @@
1
- /**
2
- * @description
3
- * is state getter
4
- */
5
- var isStateGetter = function isStateGetter(state) {
6
- return typeof state === "function";
7
- };
8
-
9
- export { isStateGetter };