@aiszlab/relax 1.3.7 → 1.3.9

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,19 +1,21 @@
1
1
  import { type Dispatch, type SetStateAction } from "react";
2
- import type { State } from "@aiszlab/relax/types";
3
- type UseControlledStateBy<R> = {
2
+ import type { Partialable, RequiredIn, State } from "@aiszlab/relax/types";
3
+ type UseControlledStateBy<S> = {
4
4
  /**
5
5
  * @description
6
6
  * default value
7
7
  */
8
- defaultState?: State<R>;
8
+ defaultState?: State<S>;
9
9
  };
10
10
  type UsedControlledState<T> = [T, Dispatch<SetStateAction<T>>];
11
- type Requirable<T, P> = T extends undefined ? (P extends undefined ? T : Exclude<T, undefined>) : T;
12
11
  /**
13
12
  * @author murukal
14
13
  *
15
14
  * @description
16
15
  * controlled state
17
16
  */
18
- export declare const useControlledState: <T, P extends T = T>(controlledState: T, { defaultState }?: UseControlledStateBy<P>) => UsedControlledState<Requirable<T, P>>;
19
- export {};
17
+ declare function useControlledState<T>(): UsedControlledState<Partialable<T>>;
18
+ declare function useControlledState<T>(controlledState: T): UsedControlledState<T>;
19
+ declare function useControlledState<T>(controlledState: T, useControlledStateBy: UseControlledStateBy<undefined>): UsedControlledState<T>;
20
+ declare function useControlledState<T>(controlledState: Partialable<T>, useControlledStateBy: RequiredIn<UseControlledStateBy<T>, "defaultState">): UsedControlledState<T>;
21
+ export { useControlledState };
@@ -4,13 +4,7 @@ import { isUndefined } from '../is/is-undefined.js';
4
4
  import { useUpdateEffect } from './use-update-effect.js';
5
5
  import { isFunction } from '../is/is-function.js';
6
6
 
7
- /**
8
- * @author murukal
9
- *
10
- * @description
11
- * controlled state
12
- */
13
- var useControlledState = function useControlledState(controlledState) {
7
+ function useControlledState(controlledState) {
14
8
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
15
9
  defaultState = _ref.defaultState;
16
10
  // initialize state
@@ -38,6 +32,6 @@ var useControlledState = function useControlledState(controlledState) {
38
32
  // use controlled
39
33
  var state = isUndefined(controlledState) ? _state : controlledState;
40
34
  return [state, _setState];
41
- };
35
+ }
42
36
 
43
37
  export { useControlledState };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @description
3
+ * check any value is controlled
4
+ * juse check `value` === `void 0`
5
+ */
6
+ declare const useIsControlled: (value: unknown) => boolean;
7
+ export { useIsControlled };
@@ -1,4 +1,4 @@
1
- type UseIsMountedProps = {
1
+ type UseIsMountedBy = {
2
2
  /**
3
3
  * @description
4
4
  * after mount, react will rerender current component
@@ -9,5 +9,5 @@ type UseIsMountedProps = {
9
9
  * @description
10
10
  * `useIsMounted`
11
11
  */
12
- export declare const useIsMounted: ({ rerender }?: UseIsMountedProps) => () => boolean;
12
+ export declare const useIsMounted: ({ rerender }?: UseIsMountedBy) => () => boolean;
13
13
  export {};
@@ -1,13 +1,24 @@
1
- import { useRef } from 'react';
1
+ import { useRef, useEffect } from 'react';
2
+ import { useDefault } from './use-default.js';
2
3
 
3
4
  var useMemorable = function useMemorable(getter, condition, shouldUpdate) {
4
- var cacheRef = useRef(null);
5
- if (cacheRef.current === null || shouldUpdate(cacheRef.current.condition, condition)) {
5
+ var isMounted = useRef(false);
6
+ var cacheRef = useRef({
7
+ value: useDefault(getter),
8
+ condition: condition
9
+ });
10
+ useEffect(function () {
11
+ // value has got in the first render, skip this render time
12
+ if (!isMounted.current) {
13
+ isMounted.current = true;
14
+ return;
15
+ }
16
+ if (!shouldUpdate(cacheRef.current.condition, condition)) return;
6
17
  cacheRef.current = {
7
18
  value: getter(),
8
19
  condition: condition
9
20
  };
10
- }
21
+ });
11
22
  return cacheRef.current.value;
12
23
  };
13
24
 
@@ -1,3 +1,3 @@
1
- type Value = string | number | Record<string, boolean> | Value[] | null;
1
+ type Value = string | number | Record<string, boolean> | Value[] | null | undefined;
2
2
  export declare const clsx: (...args: Value[]) => string;
3
3
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.3.7",
3
+ "version": "1.3.9",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "exports": {
@@ -45,8 +45,8 @@
45
45
  "jest-environment-jsdom": "^29.7.0",
46
46
  "react": "^18.3.1",
47
47
  "react-dom": "^18.3.1",
48
- "rollup": "^4.21.2",
49
- "typescript": "5.5.4"
48
+ "rollup": "^4.21.3",
49
+ "typescript": "5.6.2"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "react": "18",