@aiszlab/relax 1.0.18 → 1.0.19

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.
@@ -0,0 +1,13 @@
1
+ import { type Dispatch, type SetStateAction } from 'react';
2
+ import type { State } from '../types/state';
3
+ interface Props<T> {
4
+ defaultState?: State<T>;
5
+ }
6
+ /**
7
+ * @author murukal
8
+ *
9
+ * @description
10
+ * controlled state
11
+ */
12
+ export declare const useControlledState: <T>(controlledState: State<T>, props?: Props<T> | undefined) => [T, Dispatch<SetStateAction<T>>];
13
+ export {};
@@ -1,4 +1,5 @@
1
1
  import { useState, useEffect } from 'react';
2
+ import { isFunction } from '../utils/state.js';
2
3
 
3
4
  /**
4
5
  * @author murukal
@@ -8,12 +9,18 @@ import { useState, useEffect } from 'react';
8
9
  */
9
10
  const useControlledState = (controlledState, props) => {
10
11
  const [state, setState] = useState(() => {
11
- if (controlledState === void 0) {
12
- return props === null || props === void 0 ? void 0 : props.defaultState;
13
- }
14
- if (typeof controlledState === 'function') {
12
+ if (isFunction(controlledState)) {
15
13
  return controlledState();
16
14
  }
15
+ if (controlledState === void 0) {
16
+ if ((props === null || props === void 0 ? void 0 : props.defaultState) === void 0) {
17
+ return controlledState;
18
+ }
19
+ if (isFunction(props.defaultState)) {
20
+ return props.defaultState();
21
+ }
22
+ return props.defaultState;
23
+ }
17
24
  return controlledState;
18
25
  });
19
26
  useEffect(() => {
@@ -0,0 +1,8 @@
1
+ import type { State } from '../types/state';
2
+ /**
3
+ * @author murukal
4
+ *
5
+ * @description
6
+ * state always be same after first render
7
+ */
8
+ export declare const useOnceState: <T>(initialState: State<T>) => T;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- export { useBoolean } from './use-boolean';
2
- export { useDebounceCallback } from './use-debounce-callback';
3
- export { useImageLoader } from './use-image-loader';
4
- export { useMount } from './use-mount';
5
- export { useMounted } from './use-mounted';
6
- export { useTimeout } from './use-timeout';
7
- export { useControlledState } from './use-controlled-state';
8
- export { useOnceState } from './use-once-state';
1
+ export { useBoolean } from './hooks/use-boolean';
2
+ export { useDebounceCallback } from './hooks/use-debounce-callback';
3
+ export { useImageLoader } from './hooks/use-image-loader';
4
+ export { useMount } from './hooks/use-mount';
5
+ export { useMounted } from './hooks/use-mounted';
6
+ export { useTimeout } from './hooks/use-timeout';
7
+ export { useControlledState } from './hooks/use-controlled-state';
8
+ export { useOnceState } from './hooks/use-once-state';
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
- export { useBoolean } from './use-boolean.js';
2
- export { useDebounceCallback } from './use-debounce-callback.js';
3
- export { useImageLoader } from './use-image-loader.js';
4
- export { useMount } from './use-mount.js';
5
- export { useMounted } from './use-mounted.js';
6
- export { useTimeout } from './use-timeout.js';
7
- export { useControlledState } from './use-controlled-state.js';
8
- export { useOnceState } from './use-once-state.js';
1
+ export { useBoolean } from './hooks/use-boolean.js';
2
+ export { useDebounceCallback } from './hooks/use-debounce-callback.js';
3
+ export { useImageLoader } from './hooks/use-image-loader.js';
4
+ export { useMount } from './hooks/use-mount.js';
5
+ export { useMounted } from './hooks/use-mounted.js';
6
+ export { useTimeout } from './hooks/use-timeout.js';
7
+ export { useControlledState } from './hooks/use-controlled-state.js';
8
+ export { useOnceState } from './hooks/use-once-state.js';
@@ -0,0 +1,8 @@
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>;
@@ -0,0 +1,6 @@
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>;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @description
3
+ * is function
4
+ */
5
+ const isFunction = (state) => {
6
+ return typeof state === 'function';
7
+ };
8
+
9
+ export { isFunction };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,12 +0,0 @@
1
- import { type Dispatch, type SetStateAction } from 'react';
2
- interface Props<T> {
3
- defaultState: T;
4
- }
5
- /**
6
- * @author murukal
7
- *
8
- * @description
9
- * controlled state
10
- */
11
- export declare const useControlledState: <T>(controlledState?: T | (() => T) | undefined, props?: Props<T> | undefined) => [T | undefined, Dispatch<SetStateAction<T | undefined>>];
12
- export {};
@@ -1,7 +0,0 @@
1
- /**
2
- * @author murukal
3
- *
4
- * @description
5
- * state always be same after first render
6
- */
7
- export declare const useOnceState: <T>(initialState: T | (() => T)) => T;
File without changes
File without changes
File without changes
File without changes
File without changes