@aiszlab/relax 1.0.16 → 1.0.18

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,8 +1,12 @@
1
1
  import { type Dispatch, type SetStateAction } from 'react';
2
+ interface Props<T> {
3
+ defaultState: T;
4
+ }
2
5
  /**
3
6
  * @author murukal
4
7
  *
5
8
  * @description
6
9
  * controlled state
7
10
  */
8
- export declare const useControlledState: <T>(value: T) => [T, Dispatch<SetStateAction<T>>];
11
+ export declare const useControlledState: <T>(controlledState?: T | (() => T) | undefined, props?: Props<T> | undefined) => [T | undefined, Dispatch<SetStateAction<T | undefined>>];
12
+ export {};
@@ -6,19 +6,27 @@ import { useState, useEffect } from 'react';
6
6
  * @description
7
7
  * controlled state
8
8
  */
9
- const useControlledState = (value) => {
10
- const [state, setState] = useState(value);
9
+ const useControlledState = (controlledState, props) => {
10
+ 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') {
15
+ return controlledState();
16
+ }
17
+ return controlledState;
18
+ });
11
19
  useEffect(() => {
12
20
  // when state is not controlled
13
- if (value === void 0) {
21
+ if (controlledState === void 0) {
14
22
  return;
15
23
  }
16
24
  // if state is equal with value
17
- if (value === state) {
25
+ if (controlledState === state) {
18
26
  return;
19
27
  }
20
- setState(value);
21
- }, [value]);
28
+ setState(controlledState);
29
+ }, [controlledState]);
22
30
  return [state, setState];
23
31
  };
24
32
 
@@ -4,4 +4,4 @@
4
4
  * @description
5
5
  * state always be same after first render
6
6
  */
7
- export declare const useOnceState: <T>(initialState: T | (() => T)) => T | (() => T);
7
+ export declare const useOnceState: <T>(initialState: T | (() => T)) => T;
@@ -7,7 +7,7 @@ import { useState } from 'react';
7
7
  * state always be same after first render
8
8
  */
9
9
  const useOnceState = (initialState) => {
10
- return useState(() => initialState)[0];
10
+ return useState(initialState)[0];
11
11
  };
12
12
 
13
13
  export { useOnceState };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",