@os-design/use-forwarded-state 1.0.29 → 1.0.31
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.
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -6
- package/package.json +4 -4
- package/src/index.ts +24 -14
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,cAAc,EAMpB,MAAM,OAAO,CAAC;AAEf,UAAU,sBAAsB,CAAC,CAAC;IAChC,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7B,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC/B;AAMD,QAAA,MAAM,iBAAiB,GAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,cAAc,EAMpB,MAAM,OAAO,CAAC;AAEf,UAAU,sBAAsB,CAAC,CAAC;IAChC,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7B,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC/B;AAMD,QAAA,MAAM,iBAAiB,GAAI,CAAC,EAAE,oCAI3B,sBAAsB,CAAC,CAAC,CAAC,KAAG,CAC7B,CAAC,GAAG,SAAS,EACb,QAAQ,CAAC,cAAc,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAsCxC,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -6,17 +6,29 @@ const useForwardedState = ({
|
|
|
6
6
|
onChange
|
|
7
7
|
}) => {
|
|
8
8
|
const [innerValue, setInnerValue] = useState(defaultValue);
|
|
9
|
+
const shouldUseInnerValue = useMemo(() => value === undefined, [value]);
|
|
10
|
+
const forwardedValue = useMemo(() => shouldUseInnerValue ? innerValue : value, [shouldUseInnerValue, innerValue, value]);
|
|
9
11
|
const onChangeRef = useRef(onChange);
|
|
10
12
|
useEffect(() => {
|
|
11
13
|
onChangeRef.current = onChange;
|
|
12
14
|
}, [onChange]);
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
+
const forwardedValueRef = useRef(forwardedValue);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
forwardedValueRef.current = forwardedValue;
|
|
18
|
+
}, [forwardedValue]);
|
|
19
|
+
const shouldUseInnerValueRef = useRef(shouldUseInnerValue);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
shouldUseInnerValueRef.current = shouldUseInnerValue;
|
|
22
|
+
}, [shouldUseInnerValue]);
|
|
15
23
|
const setForwardedValue = useCallback(v => {
|
|
16
|
-
const nextValue = isFunction(v) ? v(
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
|
|
24
|
+
const nextValue = isFunction(v) ? v(forwardedValueRef.current) : v;
|
|
25
|
+
if (shouldUseInnerValueRef.current) {
|
|
26
|
+
setInnerValue(nextValue);
|
|
27
|
+
}
|
|
28
|
+
if (onChangeRef.current && nextValue !== undefined) {
|
|
29
|
+
onChangeRef.current(nextValue);
|
|
30
|
+
}
|
|
31
|
+
}, []);
|
|
20
32
|
return [forwardedValue, setForwardedValue];
|
|
21
33
|
};
|
|
22
34
|
export default useForwardedState;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@os-design/use-forwarded-state",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.31",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"repository": "git@gitlab.com:os-team/libs/os-design.git",
|
|
6
6
|
"type": "module",
|
|
@@ -23,13 +23,13 @@
|
|
|
23
23
|
"build:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts,.tsx --out-dir dist",
|
|
24
24
|
"build:types": "tsc -p tsconfig.build.json --emitDeclarationOnly --declaration --declarationDir dist",
|
|
25
25
|
"build": "yarn clean && npm-run-all 'build:*'",
|
|
26
|
-
"ncu": "ncu -u"
|
|
26
|
+
"ncu": "ncu -u '/^(?!(react|@types/react)$).*$/'"
|
|
27
27
|
},
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"react": "
|
|
32
|
+
"react": "18"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "3388a0be685b906360094b92df493150162a666c"
|
|
35
35
|
}
|
package/src/index.ts
CHANGED
|
@@ -27,11 +27,6 @@ const useForwardedState = <T>({
|
|
|
27
27
|
Dispatch<SetStateAction<T | undefined>>,
|
|
28
28
|
] => {
|
|
29
29
|
const [innerValue, setInnerValue] = useState(defaultValue);
|
|
30
|
-
const onChangeRef = useRef(onChange);
|
|
31
|
-
|
|
32
|
-
useEffect(() => {
|
|
33
|
-
onChangeRef.current = onChange;
|
|
34
|
-
}, [onChange]);
|
|
35
30
|
|
|
36
31
|
const shouldUseInnerValue = useMemo(() => value === undefined, [value]);
|
|
37
32
|
const forwardedValue = useMemo(
|
|
@@ -39,17 +34,32 @@ const useForwardedState = <T>({
|
|
|
39
34
|
[shouldUseInnerValue, innerValue, value]
|
|
40
35
|
);
|
|
41
36
|
|
|
37
|
+
const onChangeRef = useRef(onChange);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
onChangeRef.current = onChange;
|
|
40
|
+
}, [onChange]);
|
|
41
|
+
|
|
42
|
+
const forwardedValueRef = useRef(forwardedValue);
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
forwardedValueRef.current = forwardedValue;
|
|
45
|
+
}, [forwardedValue]);
|
|
46
|
+
|
|
47
|
+
const shouldUseInnerValueRef = useRef(shouldUseInnerValue);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
shouldUseInnerValueRef.current = shouldUseInnerValue;
|
|
50
|
+
}, [shouldUseInnerValue]);
|
|
51
|
+
|
|
42
52
|
const setForwardedValue = useCallback<
|
|
43
53
|
Dispatch<SetStateAction<T | undefined>>
|
|
44
|
-
>(
|
|
45
|
-
(v)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
);
|
|
54
|
+
>((v) => {
|
|
55
|
+
const nextValue = isFunction(v) ? v(forwardedValueRef.current) : v;
|
|
56
|
+
if (shouldUseInnerValueRef.current) {
|
|
57
|
+
setInnerValue(nextValue);
|
|
58
|
+
}
|
|
59
|
+
if (onChangeRef.current && nextValue !== undefined) {
|
|
60
|
+
onChangeRef.current(nextValue);
|
|
61
|
+
}
|
|
62
|
+
}, []);
|
|
53
63
|
|
|
54
64
|
return [forwardedValue, setForwardedValue];
|
|
55
65
|
};
|