@aiszlab/relax 1.0.19 → 1.0.20

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,5 +1,5 @@
1
1
  import { type Dispatch, type SetStateAction } from 'react';
2
- import type { State } from '../types/state';
2
+ import { type State } from '../utils/is-state-getter';
3
3
  interface Props<T> {
4
4
  defaultState?: State<T>;
5
5
  }
@@ -9,5 +9,5 @@ interface Props<T> {
9
9
  * @description
10
10
  * controlled state
11
11
  */
12
- export declare const useControlledState: <T>(controlledState: State<T>, props?: Props<T> | undefined) => [T, Dispatch<SetStateAction<T>>];
12
+ export declare const useControlledState: <T>(controlledState: State<T>, { defaultState }?: Props<T>) => [T, Dispatch<SetStateAction<T>>];
13
13
  export {};
@@ -1,5 +1,6 @@
1
1
  import { useState, useEffect } from 'react';
2
- import { isFunction } from '../utils/state.js';
2
+ import { isStateGetter } from '../utils/is-state-getter.js';
3
+ import { isVoid } from '../utils/is-void.js';
3
4
 
4
5
  /**
5
6
  * @author murukal
@@ -7,31 +8,28 @@ import { isFunction } from '../utils/state.js';
7
8
  * @description
8
9
  * controlled state
9
10
  */
10
- const useControlledState = (controlledState, props) => {
11
+ const useControlledState = (controlledState, { defaultState } = {}) => {
12
+ /// initialize state
11
13
  const [state, setState] = useState(() => {
12
- if (isFunction(controlledState)) {
14
+ if (isStateGetter(controlledState))
13
15
  return controlledState();
14
- }
15
- if (controlledState === void 0) {
16
- if ((props === null || props === void 0 ? void 0 : props.defaultState) === void 0) {
16
+ if (isVoid(controlledState)) {
17
+ if (isVoid(defaultState))
17
18
  return controlledState;
18
- }
19
- if (isFunction(props.defaultState)) {
20
- return props.defaultState();
21
- }
22
- return props.defaultState;
19
+ if (isStateGetter(defaultState))
20
+ return defaultState();
21
+ return defaultState;
23
22
  }
24
23
  return controlledState;
25
24
  });
26
25
  useEffect(() => {
27
26
  // when state is not controlled
28
- if (controlledState === void 0) {
27
+ if (isVoid(controlledState))
29
28
  return;
30
- }
31
29
  // if state is equal with value
32
- if (controlledState === state) {
30
+ if (controlledState === state)
33
31
  return;
34
- }
32
+ /// update inner state
35
33
  setState(controlledState);
36
34
  }, [controlledState]);
37
35
  return [state, setState];
@@ -1,4 +1,3 @@
1
- import type { State } from '../types/state';
2
1
  /**
3
2
  * @author murukal
4
3
  *
package/dist/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @description
3
+ * hooks
4
+ */
1
5
  export { useBoolean } from './hooks/use-boolean';
2
6
  export { useDebounceCallback } from './hooks/use-debounce-callback';
3
7
  export { useImageLoader } from './hooks/use-image-loader';
@@ -6,3 +10,10 @@ export { useMounted } from './hooks/use-mounted';
6
10
  export { useTimeout } from './hooks/use-timeout';
7
11
  export { useControlledState } from './hooks/use-controlled-state';
8
12
  export { useOnceState } from './hooks/use-once-state';
13
+ /**
14
+ * @description
15
+ * utils
16
+ */
17
+ export { isRefable } from './utils/is-refable';
18
+ export { isVoid } from './utils/is-void';
19
+ export { isStateGetter } from './utils/is-state-getter';
package/dist/index.js CHANGED
@@ -6,3 +6,6 @@ export { useMounted } from './hooks/use-mounted.js';
6
6
  export { useTimeout } from './hooks/use-timeout.js';
7
7
  export { useControlledState } from './hooks/use-controlled-state.js';
8
8
  export { useOnceState } from './hooks/use-once-state.js';
9
+ export { isRefable } from './utils/is-refable.js';
10
+ export { isVoid } from './utils/is-void.js';
11
+ export { isStateGetter } from './utils/is-state-getter.js';
@@ -0,0 +1,5 @@
1
+ import { type ReactNode } from 'react';
2
+ /**
3
+ * @description
4
+ */
5
+ export declare const isRefable: (node: ReactNode) => boolean;
@@ -0,0 +1,34 @@
1
+ import { isValidElement } from 'react';
2
+ import { isFragment, isMemo } from 'react-is';
3
+
4
+ /**
5
+ * @description
6
+ */
7
+ const isRefable = (node) => {
8
+ if (!isValidElement(node)) {
9
+ return false;
10
+ }
11
+ if (isFragment(node)) {
12
+ return false;
13
+ }
14
+ return _RefableElement(node);
15
+ };
16
+ /**
17
+ * @description
18
+ * refable element
19
+ */
20
+ const _RefableElement = (element) => {
21
+ var _a, _b;
22
+ const type = isMemo(element) ? element.type.type : element.type;
23
+ // Function component node
24
+ if (typeof type === 'function' && !((_a = type.prototype) === null || _a === void 0 ? void 0 : _a.render)) {
25
+ return false;
26
+ }
27
+ // Class component
28
+ if (typeof element === 'function' && !((_b = element.prototype) === null || _b === void 0 ? void 0 : _b.render)) {
29
+ return false;
30
+ }
31
+ return true;
32
+ };
33
+
34
+ export { isRefable };
@@ -0,0 +1,14 @@
1
+ type StateGetter<T> = () => T;
2
+ /**
3
+ * @description
4
+ * state setter
5
+ *
6
+ * used by initial state, default state, controlled state
7
+ */
8
+ export type State<T> = T | StateGetter<T>;
9
+ /**
10
+ * @description
11
+ * is state getter
12
+ */
13
+ export declare const isStateGetter: <T>(state: State<T>) => state is StateGetter<T>;
14
+ export {};
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @description
3
+ * is state getter
4
+ */
5
+ const isStateGetter = (state) => {
6
+ return typeof state === 'function';
7
+ };
8
+
9
+ export { isStateGetter };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @description
3
+ * is undefined
4
+ */
5
+ export declare const isVoid: (value: unknown) => value is undefined;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @description
3
+ * is undefined
4
+ */
5
+ const isVoid = (value) => {
6
+ return value === void 0;
7
+ };
8
+
9
+ export { isVoid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.0.19",
3
+ "version": "1.0.20",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -11,6 +11,7 @@
11
11
  "pub": "npm run clean:build && npm run build && npm publish"
12
12
  },
13
13
  "dependencies": {
14
+ "react-is": "^18.2.0",
14
15
  "rxjs": "^7.8.1"
15
16
  },
16
17
  "devDependencies": {
@@ -22,6 +23,7 @@
22
23
  "@rollup/plugin-typescript": "^11.1.1",
23
24
  "@types/react": "^18.2.12",
24
25
  "@types/react-dom": "^18.2.5",
26
+ "@types/react-is": "^18.2.2",
25
27
  "rollup": "^3.27.0",
26
28
  "typescript": "^5.1.3"
27
29
  },
@@ -1,8 +0,0 @@
1
- export type StateGetter<T> = () => T;
2
- /**
3
- * @description
4
- * state setter
5
- *
6
- * used by initial state, default state, controlled state
7
- */
8
- export type State<T> = T | StateGetter<T>;
@@ -1,6 +0,0 @@
1
- import type { State, StateGetter } from '../types/state';
2
- /**
3
- * @description
4
- * is function
5
- */
6
- export declare const isFunction: <T>(state: State<T>) => state is StateGetter<T>;
@@ -1,9 +0,0 @@
1
- /**
2
- * @description
3
- * is function
4
- */
5
- const isFunction = (state) => {
6
- return typeof state === 'function';
7
- };
8
-
9
- export { isFunction };