@tamagui/use-controllable-state 1.0.1-beta.122 → 1.0.1-beta.125
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/cjs/useControllableState.js +20 -26
- package/dist/cjs/useControllableState.js.map +2 -2
- package/dist/esm/useControllableState.js +21 -27
- package/dist/esm/useControllableState.js.map +2 -2
- package/dist/jsx/useControllableState.js +21 -27
- package/dist/jsx/useControllableState.js.map +2 -2
- package/package.json +3 -3
- package/src/useControllableState.ts +21 -30
- package/types/index.d.ts.map +0 -1
- package/types/useControllableState.d.ts.map +0 -1
|
@@ -30,36 +30,30 @@ function useControllableState({
|
|
|
30
30
|
strategy = "prop-wins",
|
|
31
31
|
preventUpdate
|
|
32
32
|
}) {
|
|
33
|
-
const [
|
|
34
|
-
const
|
|
33
|
+
const [state, setState] = (0, import_react.useState)(prop ?? defaultProp);
|
|
34
|
+
const previous = (0, import_react.useRef)(state);
|
|
35
|
+
const propWins = strategy === "prop-wins" && prop !== void 0;
|
|
36
|
+
const value = propWins ? prop : state;
|
|
35
37
|
const onChangeCb = (0, import_use_event.useEvent)(onChange || idFn);
|
|
36
38
|
(0, import_react.useEffect)(() => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
if (state !== previous.current) {
|
|
40
|
+
previous.current = state;
|
|
41
|
+
onChangeCb(state);
|
|
40
42
|
}
|
|
41
|
-
}, [
|
|
42
|
-
(0,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
propWins
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
})
|
|
55
|
-
];
|
|
43
|
+
}, [onChangeCb, state]);
|
|
44
|
+
const setter = (0, import_use_event.useEvent)((next) => {
|
|
45
|
+
if (preventUpdate)
|
|
46
|
+
return;
|
|
47
|
+
console.log("setting", propWins, prop, next);
|
|
48
|
+
if (propWins) {
|
|
49
|
+
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
50
|
+
onChangeCb(nextValue);
|
|
51
|
+
} else {
|
|
52
|
+
setState(next);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return [value, setter];
|
|
56
56
|
}
|
|
57
|
-
const getNextState = (prev, next) => {
|
|
58
|
-
if (prev === next || next === void 0) {
|
|
59
|
-
return prev;
|
|
60
|
-
}
|
|
61
|
-
return next;
|
|
62
|
-
};
|
|
63
57
|
const idFn = () => {
|
|
64
58
|
};
|
|
65
59
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useControllableState.ts"],
|
|
4
|
-
"sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n preventUpdate,\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ChangeCb<T>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: boolean\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const [
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAyB;AACzB,
|
|
4
|
+
"sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { useCallback, useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n preventUpdate,\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ChangeCb<T>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: boolean\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const [state, setState] = useState(prop ?? defaultProp)\n const previous = useRef<any>(state)\n const propWins = strategy === 'prop-wins' && prop !== undefined\n const value = propWins ? prop : state\n const onChangeCb = useEvent(onChange || idFn)\n\n useEffect(() => {\n if (state !== previous.current) {\n previous.current = state\n onChangeCb(state)\n }\n }, [onChangeCb, state])\n\n const setter = useEvent((next) => {\n if (preventUpdate) return\n console.log('setting', propWins, prop, next)\n if (propWins) {\n const nextValue = typeof next === 'function' ? next(previous.current) : next\n onChangeCb(nextValue)\n } else {\n setState(next)\n }\n })\n\n return [value as T, setter]\n}\n\nconst idFn = () => {}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAyB;AACzB,mBAAgE;AAOzD,8BAAiC;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AAAA,GAO+C;AAC/C,QAAM,CAAC,OAAO,YAAY,2BAAS,QAAQ,WAAW;AACtD,QAAM,WAAW,yBAAY,KAAK;AAClC,QAAM,WAAW,aAAa,eAAe,SAAS;AACtD,QAAM,QAAQ,WAAW,OAAO;AAChC,QAAM,aAAa,+BAAS,YAAY,IAAI;AAE5C,8BAAU,MAAM;AACd,QAAI,UAAU,SAAS,SAAS;AAC9B,eAAS,UAAU;AACnB,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,YAAY,KAAK,CAAC;AAEtB,QAAM,SAAS,+BAAS,CAAC,SAAS;AAChC,QAAI;AAAe;AACnB,YAAQ,IAAI,WAAW,UAAU,MAAM,IAAI;AAC3C,QAAI,UAAU;AACZ,YAAM,YAAY,OAAO,SAAS,aAAa,KAAK,SAAS,OAAO,IAAI;AACxE,iBAAW,SAAS;AAAA,IACtB,OAAO;AACL,eAAS,IAAI;AAAA,IACf;AAAA,EACF,CAAC;AAED,SAAO,CAAC,OAAY,MAAM;AAC5B;AAEA,MAAM,OAAO,MAAM;AAAC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEvent } from "@tamagui/use-event";
|
|
2
|
-
import { useEffect, useState } from "react";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
3
|
function useControllableState({
|
|
4
4
|
prop,
|
|
5
5
|
defaultProp,
|
|
@@ -7,36 +7,30 @@ function useControllableState({
|
|
|
7
7
|
strategy = "prop-wins",
|
|
8
8
|
preventUpdate
|
|
9
9
|
}) {
|
|
10
|
-
const [
|
|
11
|
-
const
|
|
10
|
+
const [state, setState] = useState(prop ?? defaultProp);
|
|
11
|
+
const previous = useRef(state);
|
|
12
|
+
const propWins = strategy === "prop-wins" && prop !== void 0;
|
|
13
|
+
const value = propWins ? prop : state;
|
|
12
14
|
const onChangeCb = useEvent(onChange || idFn);
|
|
13
15
|
useEffect(() => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
if (state !== previous.current) {
|
|
17
|
+
previous.current = state;
|
|
18
|
+
onChangeCb(state);
|
|
17
19
|
}
|
|
18
|
-
}, [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
propWins
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
})
|
|
32
|
-
];
|
|
20
|
+
}, [onChangeCb, state]);
|
|
21
|
+
const setter = useEvent((next) => {
|
|
22
|
+
if (preventUpdate)
|
|
23
|
+
return;
|
|
24
|
+
console.log("setting", propWins, prop, next);
|
|
25
|
+
if (propWins) {
|
|
26
|
+
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
27
|
+
onChangeCb(nextValue);
|
|
28
|
+
} else {
|
|
29
|
+
setState(next);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return [value, setter];
|
|
33
33
|
}
|
|
34
|
-
const getNextState = (prev, next) => {
|
|
35
|
-
if (prev === next || next === void 0) {
|
|
36
|
-
return prev;
|
|
37
|
-
}
|
|
38
|
-
return next;
|
|
39
|
-
};
|
|
40
34
|
const idFn = () => {
|
|
41
35
|
};
|
|
42
36
|
export {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useControllableState.ts"],
|
|
4
|
-
"sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n preventUpdate,\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ChangeCb<T>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: boolean\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const [
|
|
5
|
-
"mappings": "AAAA;AACA;AAOO,8BAAiC;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AAAA,GAO+C;AAC/C,QAAM,CAAC,
|
|
4
|
+
"sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { useCallback, useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n preventUpdate,\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ChangeCb<T>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: boolean\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const [state, setState] = useState(prop ?? defaultProp)\n const previous = useRef<any>(state)\n const propWins = strategy === 'prop-wins' && prop !== undefined\n const value = propWins ? prop : state\n const onChangeCb = useEvent(onChange || idFn)\n\n useEffect(() => {\n if (state !== previous.current) {\n previous.current = state\n onChangeCb(state)\n }\n }, [onChangeCb, state])\n\n const setter = useEvent((next) => {\n if (preventUpdate) return\n console.log('setting', propWins, prop, next)\n if (propWins) {\n const nextValue = typeof next === 'function' ? next(previous.current) : next\n onChangeCb(nextValue)\n } else {\n setState(next)\n }\n })\n\n return [value as T, setter]\n}\n\nconst idFn = () => {}\n"],
|
|
5
|
+
"mappings": "AAAA;AACA;AAOO,8BAAiC;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AAAA,GAO+C;AAC/C,QAAM,CAAC,OAAO,YAAY,SAAS,QAAQ,WAAW;AACtD,QAAM,WAAW,OAAY,KAAK;AAClC,QAAM,WAAW,aAAa,eAAe,SAAS;AACtD,QAAM,QAAQ,WAAW,OAAO;AAChC,QAAM,aAAa,SAAS,YAAY,IAAI;AAE5C,YAAU,MAAM;AACd,QAAI,UAAU,SAAS,SAAS;AAC9B,eAAS,UAAU;AACnB,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,YAAY,KAAK,CAAC;AAEtB,QAAM,SAAS,SAAS,CAAC,SAAS;AAChC,QAAI;AAAe;AACnB,YAAQ,IAAI,WAAW,UAAU,MAAM,IAAI;AAC3C,QAAI,UAAU;AACZ,YAAM,YAAY,OAAO,SAAS,aAAa,KAAK,SAAS,OAAO,IAAI;AACxE,iBAAW,SAAS;AAAA,IACtB,OAAO;AACL,eAAS,IAAI;AAAA,IACf;AAAA,EACF,CAAC;AAED,SAAO,CAAC,OAAY,MAAM;AAC5B;AAEA,MAAM,OAAO,MAAM;AAAC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEvent } from "@tamagui/use-event";
|
|
2
|
-
import { useEffect, useState } from "react";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
3
|
function useControllableState({
|
|
4
4
|
prop,
|
|
5
5
|
defaultProp,
|
|
@@ -7,36 +7,30 @@ function useControllableState({
|
|
|
7
7
|
strategy = "prop-wins",
|
|
8
8
|
preventUpdate
|
|
9
9
|
}) {
|
|
10
|
-
const [
|
|
11
|
-
const
|
|
10
|
+
const [state, setState] = useState(prop != null ? prop : defaultProp);
|
|
11
|
+
const previous = useRef(state);
|
|
12
|
+
const propWins = strategy === "prop-wins" && prop !== void 0;
|
|
13
|
+
const value = propWins ? prop : state;
|
|
12
14
|
const onChangeCb = useEvent(onChange || idFn);
|
|
13
15
|
useEffect(() => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
if (state !== previous.current) {
|
|
17
|
+
previous.current = state;
|
|
18
|
+
onChangeCb(state);
|
|
17
19
|
}
|
|
18
|
-
}, [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
propWins
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
})
|
|
32
|
-
];
|
|
20
|
+
}, [onChangeCb, state]);
|
|
21
|
+
const setter = useEvent((next) => {
|
|
22
|
+
if (preventUpdate)
|
|
23
|
+
return;
|
|
24
|
+
console.log("setting", propWins, prop, next);
|
|
25
|
+
if (propWins) {
|
|
26
|
+
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
27
|
+
onChangeCb(nextValue);
|
|
28
|
+
} else {
|
|
29
|
+
setState(next);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return [value, setter];
|
|
33
33
|
}
|
|
34
|
-
const getNextState = (prev, next) => {
|
|
35
|
-
if (prev === next || next === void 0) {
|
|
36
|
-
return prev;
|
|
37
|
-
}
|
|
38
|
-
return next;
|
|
39
|
-
};
|
|
40
34
|
const idFn = () => {
|
|
41
35
|
};
|
|
42
36
|
export {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useControllableState.ts"],
|
|
4
|
-
"sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n preventUpdate,\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ChangeCb<T>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: boolean\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const [
|
|
5
|
-
"mappings": "AAAA;AACA;AAOO,8BAAiC;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AAAA,GAO+C;AAC/C,QAAM,CAAC,
|
|
4
|
+
"sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport React, { useCallback, useEffect, useRef, useState } from 'react'\n\n// can configure to allow most-recent-wins or prop-wins\n// defaults to prop-wins\n\ntype ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>\n\nexport function useControllableState<T>({\n prop,\n defaultProp,\n onChange,\n strategy = 'prop-wins',\n preventUpdate,\n}: {\n prop?: T | undefined\n defaultProp: T\n onChange?: ChangeCb<T>\n strategy?: 'prop-wins' | 'most-recent-wins'\n preventUpdate?: boolean\n}): [T, React.Dispatch<React.SetStateAction<T>>] {\n const [state, setState] = useState(prop ?? defaultProp)\n const previous = useRef<any>(state)\n const propWins = strategy === 'prop-wins' && prop !== undefined\n const value = propWins ? prop : state\n const onChangeCb = useEvent(onChange || idFn)\n\n useEffect(() => {\n if (state !== previous.current) {\n previous.current = state\n onChangeCb(state)\n }\n }, [onChangeCb, state])\n\n const setter = useEvent((next) => {\n if (preventUpdate) return\n console.log('setting', propWins, prop, next)\n if (propWins) {\n const nextValue = typeof next === 'function' ? next(previous.current) : next\n onChangeCb(nextValue)\n } else {\n setState(next)\n }\n })\n\n return [value as T, setter]\n}\n\nconst idFn = () => {}\n"],
|
|
5
|
+
"mappings": "AAAA;AACA;AAOO,8BAAiC;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AAAA,GAO+C;AAC/C,QAAM,CAAC,OAAO,YAAY,SAAS,sBAAQ,WAAW;AACtD,QAAM,WAAW,OAAY,KAAK;AAClC,QAAM,WAAW,aAAa,eAAe,SAAS;AACtD,QAAM,QAAQ,WAAW,OAAO;AAChC,QAAM,aAAa,SAAS,YAAY,IAAI;AAE5C,YAAU,MAAM;AACd,QAAI,UAAU,SAAS,SAAS;AAC9B,eAAS,UAAU;AACnB,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,YAAY,KAAK,CAAC;AAEtB,QAAM,SAAS,SAAS,CAAC,SAAS;AAChC,QAAI;AAAe;AACnB,YAAQ,IAAI,WAAW,UAAU,MAAM,IAAI;AAC3C,QAAI,UAAU;AACZ,YAAM,YAAY,OAAO,SAAS,aAAa,KAAK,SAAS,OAAO,IAAI;AACxE,iBAAW,SAAS;AAAA,IACtB,OAAO;AACL,eAAS,IAAI;AAAA,IACf;AAAA,EACF,CAAC;AAED,SAAO,CAAC,OAAY,MAAM;AAC5B;AAEA,MAAM,OAAO,MAAM;AAAC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/use-controllable-state",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.125",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
"clean:build": "tamagui-build clean:build"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@tamagui/use-event": "^1.0.1-beta.
|
|
22
|
+
"@tamagui/use-event": "^1.0.1-beta.125"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"react": "*",
|
|
26
26
|
"react-dom": "*"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@tamagui/build": "^1.0.1-beta.
|
|
29
|
+
"@tamagui/build": "^1.0.1-beta.125",
|
|
30
30
|
"react": "*",
|
|
31
31
|
"react-dom": "*"
|
|
32
32
|
},
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEvent } from '@tamagui/use-event'
|
|
2
|
-
import React, { useEffect, useRef, useState } from 'react'
|
|
2
|
+
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
|
3
3
|
|
|
4
4
|
// can configure to allow most-recent-wins or prop-wins
|
|
5
5
|
// defaults to prop-wins
|
|
@@ -19,40 +19,31 @@ export function useControllableState<T>({
|
|
|
19
19
|
strategy?: 'prop-wins' | 'most-recent-wins'
|
|
20
20
|
preventUpdate?: boolean
|
|
21
21
|
}): [T, React.Dispatch<React.SetStateAction<T>>] {
|
|
22
|
-
const [
|
|
23
|
-
const
|
|
22
|
+
const [state, setState] = useState(prop ?? defaultProp)
|
|
23
|
+
const previous = useRef<any>(state)
|
|
24
|
+
const propWins = strategy === 'prop-wins' && prop !== undefined
|
|
25
|
+
const value = propWins ? prop : state
|
|
24
26
|
const onChangeCb = useEvent(onChange || idFn)
|
|
25
27
|
|
|
26
|
-
// sync prop to state
|
|
27
28
|
useEffect(() => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
if (state !== previous.current) {
|
|
30
|
+
previous.current = state
|
|
31
|
+
onChangeCb(state)
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return
|
|
45
|
-
}
|
|
46
|
-
setVal((prev) => getNextState(prev, typeof next === 'function' ? next(prev) : next))
|
|
47
|
-
}),
|
|
48
|
-
]
|
|
49
|
-
}
|
|
33
|
+
}, [onChangeCb, state])
|
|
34
|
+
|
|
35
|
+
const setter = useEvent((next) => {
|
|
36
|
+
if (preventUpdate) return
|
|
37
|
+
console.log('setting', propWins, prop, next)
|
|
38
|
+
if (propWins) {
|
|
39
|
+
const nextValue = typeof next === 'function' ? next(previous.current) : next
|
|
40
|
+
onChangeCb(nextValue)
|
|
41
|
+
} else {
|
|
42
|
+
setState(next)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
50
45
|
|
|
51
|
-
|
|
52
|
-
if (prev === next || next === undefined) {
|
|
53
|
-
return prev
|
|
54
|
-
}
|
|
55
|
-
return next
|
|
46
|
+
return [value as T, setter]
|
|
56
47
|
}
|
|
57
48
|
|
|
58
49
|
const idFn = () => {}
|
package/types/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useControllableState.d.ts","sourceRoot":"","sources":["../src/useControllableState.ts"],"names":[],"mappings":"AACA,OAAO,KAAsC,MAAM,OAAO,CAAA;AAK1D,aAAK,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAA;AAEhF,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,EACtC,IAAI,EACJ,WAAW,EACX,QAAQ,EACR,QAAsB,EACtB,aAAa,GACd,EAAE;IACD,IAAI,CAAC,EAAE,CAAC,GAAG,SAAS,CAAA;IACpB,WAAW,EAAE,CAAC,CAAA;IACd,QAAQ,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAA;IACtB,QAAQ,CAAC,EAAE,WAAW,GAAG,kBAAkB,CAAA;IAC3C,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CA4B/C"}
|