@tamagui/use-controllable-state 1.0.1-beta.99 → 1.0.1-rc.0.1
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/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/useControllableState.js +34 -29
- package/dist/cjs/useControllableState.js.map +2 -2
- package/dist/esm/index.js +0 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/useControllableState.js +34 -30
- package/dist/esm/useControllableState.js.map +2 -2
- package/dist/jsx/index.js +0 -0
- package/dist/jsx/index.js.map +1 -1
- package/dist/jsx/useControllableState.js +34 -30
- package/dist/jsx/useControllableState.js.map +2 -2
- package/package.json +9 -8
- package/src/useControllableState.ts +36 -38
- package/types/index.d.ts +0 -0
- package/types/useControllableState.d.ts +5 -2
- package/types/useControllableState.d.ts.map +0 -1
package/dist/cjs/index.js
CHANGED
package/dist/cjs/index.js.map
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
@@ -20,43 +21,47 @@ __export(useControllableState_exports, {
|
|
|
20
21
|
useControllableState: () => useControllableState
|
|
21
22
|
});
|
|
22
23
|
module.exports = __toCommonJS(useControllableState_exports);
|
|
23
|
-
var
|
|
24
|
+
var import_use_event = require("@tamagui/use-event");
|
|
24
25
|
var import_react = require("react");
|
|
25
26
|
function useControllableState({
|
|
26
27
|
prop,
|
|
27
28
|
defaultProp,
|
|
28
29
|
onChange,
|
|
29
|
-
strategy = "prop-wins"
|
|
30
|
+
strategy = "prop-wins",
|
|
31
|
+
preventUpdate
|
|
30
32
|
}) {
|
|
31
|
-
const
|
|
32
|
-
const
|
|
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;
|
|
37
|
+
const onChangeCb = (0, import_use_event.useEvent)(onChange || idFn);
|
|
35
38
|
(0, import_react.useEffect)(() => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}, [
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
39
|
+
if (prop === void 0)
|
|
40
|
+
return;
|
|
41
|
+
previous.current = prop;
|
|
42
|
+
setState(prop);
|
|
43
|
+
}, [prop]);
|
|
44
|
+
(0, import_react.useEffect)(() => {
|
|
45
|
+
if (propWins)
|
|
46
|
+
return;
|
|
47
|
+
if (state !== previous.current) {
|
|
48
|
+
previous.current = state;
|
|
49
|
+
onChangeCb(state);
|
|
50
|
+
}
|
|
51
|
+
}, [onChangeCb, state, propWins]);
|
|
52
|
+
const setter = (0, import_use_event.useEvent)((next) => {
|
|
53
|
+
if (preventUpdate)
|
|
54
|
+
return;
|
|
55
|
+
if (propWins) {
|
|
56
|
+
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
57
|
+
onChangeCb(nextValue);
|
|
58
|
+
} else {
|
|
59
|
+
setState(next);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return [value, setter];
|
|
53
63
|
}
|
|
54
|
-
const
|
|
55
|
-
if (prev === next || next === void 0) {
|
|
56
|
-
return prev;
|
|
57
|
-
}
|
|
58
|
-
handleChange(next);
|
|
59
|
-
return next;
|
|
64
|
+
const idFn = () => {
|
|
60
65
|
};
|
|
61
66
|
// Annotate the CommonJS export names for ESM import in node:
|
|
62
67
|
0 && (module.exports = {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useControllableState.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "
|
|
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 (prop === undefined) return\n previous.current = prop\n setState(prop)\n }, [prop])\n\n useEffect(() => {\n if (propWins) return\n if (state !== previous.current) {\n previous.current = state\n onChangeCb(state)\n }\n }, [onChangeCb, state, propWins])\n\n const setter = useEvent((next) => {\n if (preventUpdate) return\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,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AACF,GAMiD;AAC/C,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAS,QAAQ,WAAW;AACtD,QAAM,eAAW,qBAAY,KAAK;AAClC,QAAM,WAAW,aAAa,eAAe,SAAS;AACtD,QAAM,QAAQ,WAAW,OAAO;AAChC,QAAM,iBAAa,2BAAS,YAAY,IAAI;AAE5C,8BAAU,MAAM;AACd,QAAI,SAAS;AAAW;AACxB,aAAS,UAAU;AACnB,aAAS,IAAI;AAAA,EACf,GAAG,CAAC,IAAI,CAAC;AAET,8BAAU,MAAM;AACd,QAAI;AAAU;AACd,QAAI,UAAU,SAAS,SAAS;AAC9B,eAAS,UAAU;AACnB,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,YAAY,OAAO,QAAQ,CAAC;AAEhC,QAAM,aAAS,2BAAS,CAAC,SAAS;AAChC,QAAI;AAAe;AACnB,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/dist/esm/index.js
CHANGED
|
File without changes
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1,40 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useEvent } from "@tamagui/use-event";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
3
|
function useControllableState({
|
|
4
4
|
prop,
|
|
5
5
|
defaultProp,
|
|
6
6
|
onChange,
|
|
7
|
-
strategy = "prop-wins"
|
|
7
|
+
strategy = "prop-wins",
|
|
8
|
+
preventUpdate
|
|
8
9
|
}) {
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
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;
|
|
14
|
+
const onChangeCb = useEvent(onChange || idFn);
|
|
13
15
|
useEffect(() => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}, [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
if (prop === void 0)
|
|
17
|
+
return;
|
|
18
|
+
previous.current = prop;
|
|
19
|
+
setState(prop);
|
|
20
|
+
}, [prop]);
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (propWins)
|
|
23
|
+
return;
|
|
24
|
+
if (state !== previous.current) {
|
|
25
|
+
previous.current = state;
|
|
26
|
+
onChangeCb(state);
|
|
27
|
+
}
|
|
28
|
+
}, [onChangeCb, state, propWins]);
|
|
29
|
+
const setter = useEvent((next) => {
|
|
30
|
+
if (preventUpdate)
|
|
31
|
+
return;
|
|
32
|
+
if (propWins) {
|
|
33
|
+
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
34
|
+
onChangeCb(nextValue);
|
|
35
|
+
} else {
|
|
36
|
+
setState(next);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return [value, setter];
|
|
31
40
|
}
|
|
32
|
-
const
|
|
33
|
-
if (prev === next || next === void 0) {
|
|
34
|
-
return prev;
|
|
35
|
-
}
|
|
36
|
-
handleChange(next);
|
|
37
|
-
return next;
|
|
41
|
+
const idFn = () => {
|
|
38
42
|
};
|
|
39
43
|
export {
|
|
40
44
|
useControllableState
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useControllableState.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "AAAA;
|
|
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 (prop === undefined) return\n previous.current = prop\n setState(prop)\n }, [prop])\n\n useEffect(() => {\n if (propWins) return\n if (state !== previous.current) {\n previous.current = state\n onChangeCb(state)\n }\n }, [onChangeCb, state, propWins])\n\n const setter = useEvent((next) => {\n if (preventUpdate) return\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,SAAS,gBAAgB;AACzB,SAA6B,WAAW,QAAQ,gBAAgB;AAOzD,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AACF,GAMiD;AAC/C,QAAM,CAAC,OAAO,QAAQ,IAAI,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,SAAS;AAAW;AACxB,aAAS,UAAU;AACnB,aAAS,IAAI;AAAA,EACf,GAAG,CAAC,IAAI,CAAC;AAET,YAAU,MAAM;AACd,QAAI;AAAU;AACd,QAAI,UAAU,SAAS,SAAS;AAC9B,eAAS,UAAU;AACnB,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,YAAY,OAAO,QAAQ,CAAC;AAEhC,QAAM,SAAS,SAAS,CAAC,SAAS;AAChC,QAAI;AAAe;AACnB,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/dist/jsx/index.js
CHANGED
|
File without changes
|
package/dist/jsx/index.js.map
CHANGED
|
@@ -1,40 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useEvent } from "@tamagui/use-event";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
3
|
function useControllableState({
|
|
4
4
|
prop,
|
|
5
5
|
defaultProp,
|
|
6
6
|
onChange,
|
|
7
|
-
strategy = "prop-wins"
|
|
7
|
+
strategy = "prop-wins",
|
|
8
|
+
preventUpdate
|
|
8
9
|
}) {
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
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;
|
|
14
|
+
const onChangeCb = useEvent(onChange || idFn);
|
|
13
15
|
useEffect(() => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}, [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
if (prop === void 0)
|
|
17
|
+
return;
|
|
18
|
+
previous.current = prop;
|
|
19
|
+
setState(prop);
|
|
20
|
+
}, [prop]);
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (propWins)
|
|
23
|
+
return;
|
|
24
|
+
if (state !== previous.current) {
|
|
25
|
+
previous.current = state;
|
|
26
|
+
onChangeCb(state);
|
|
27
|
+
}
|
|
28
|
+
}, [onChangeCb, state, propWins]);
|
|
29
|
+
const setter = useEvent((next) => {
|
|
30
|
+
if (preventUpdate)
|
|
31
|
+
return;
|
|
32
|
+
if (propWins) {
|
|
33
|
+
const nextValue = typeof next === "function" ? next(previous.current) : next;
|
|
34
|
+
onChangeCb(nextValue);
|
|
35
|
+
} else {
|
|
36
|
+
setState(next);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return [value, setter];
|
|
31
40
|
}
|
|
32
|
-
const
|
|
33
|
-
if (prev === next || next === void 0) {
|
|
34
|
-
return prev;
|
|
35
|
-
}
|
|
36
|
-
handleChange(next);
|
|
37
|
-
return next;
|
|
41
|
+
const idFn = () => {
|
|
38
42
|
};
|
|
39
43
|
export {
|
|
40
44
|
useControllableState
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useControllableState.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "AAAA;
|
|
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 (prop === undefined) return\n previous.current = prop\n setState(prop)\n }, [prop])\n\n useEffect(() => {\n if (propWins) return\n if (state !== previous.current) {\n previous.current = state\n onChangeCb(state)\n }\n }, [onChangeCb, state, propWins])\n\n const setter = useEvent((next) => {\n if (preventUpdate) return\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,SAAS,gBAAgB;AACzB,SAA6B,WAAW,QAAQ,gBAAgB;AAOzD,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AACF,GAMiD;AAC/C,QAAM,CAAC,OAAO,QAAQ,IAAI,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,SAAS;AAAW;AACxB,aAAS,UAAU;AACnB,aAAS,IAAI;AAAA,EACf,GAAG,CAAC,IAAI,CAAC;AAET,YAAU,MAAM;AACd,QAAI;AAAU;AACd,QAAI,UAAU,SAAS,SAAS;AAC9B,eAAS,UAAU;AACnB,iBAAW,KAAK;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,YAAY,OAAO,QAAQ,CAAC;AAEhC,QAAM,SAAS,SAAS,CAAC,SAAS;AAChC,QAAI;AAAe;AACnB,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,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/use-controllable-state",
|
|
3
|
-
"version": "1.0.1-
|
|
3
|
+
"version": "1.0.1-rc.0.1",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
7
7
|
"main": "dist/cjs",
|
|
8
8
|
"module": "dist/esm",
|
|
9
|
-
"module:jsx": "dist/jsx",
|
|
10
9
|
"files": [
|
|
11
10
|
"src",
|
|
12
11
|
"types",
|
|
@@ -15,20 +14,22 @@
|
|
|
15
14
|
"scripts": {
|
|
16
15
|
"build": "tamagui-build",
|
|
17
16
|
"watch": "tamagui-build --watch",
|
|
17
|
+
"lint": "eslint src",
|
|
18
|
+
"lint:fix": "yarn lint --fix",
|
|
18
19
|
"clean": "tamagui-build clean",
|
|
19
20
|
"clean:build": "tamagui-build clean:build"
|
|
20
21
|
},
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"@
|
|
23
|
+
"@tamagui/use-event": "^1.0.1-rc.0.1"
|
|
23
24
|
},
|
|
24
25
|
"peerDependencies": {
|
|
25
|
-
"react": "
|
|
26
|
-
"react-dom": "
|
|
26
|
+
"react": ">=18",
|
|
27
|
+
"react-dom": ">=18"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"@tamagui/build": "^1.0.1-
|
|
30
|
-
"react": "
|
|
31
|
-
"react-dom": "
|
|
30
|
+
"@tamagui/build": "^1.0.1-rc.0.1",
|
|
31
|
+
"react": ">=18",
|
|
32
|
+
"react-dom": ">=18"
|
|
32
33
|
},
|
|
33
34
|
"publishConfig": {
|
|
34
35
|
"access": "public"
|
|
@@ -1,57 +1,55 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useEvent } from '@tamagui/use-event'
|
|
2
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
|
|
6
6
|
|
|
7
|
+
type ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>
|
|
8
|
+
|
|
7
9
|
export function useControllableState<T>({
|
|
8
10
|
prop,
|
|
9
11
|
defaultProp,
|
|
10
12
|
onChange,
|
|
11
13
|
strategy = 'prop-wins',
|
|
14
|
+
preventUpdate,
|
|
12
15
|
}: {
|
|
13
16
|
prop?: T | undefined
|
|
14
17
|
defaultProp: T
|
|
15
|
-
onChange?:
|
|
18
|
+
onChange?: ChangeCb<T>
|
|
16
19
|
strategy?: 'prop-wins' | 'most-recent-wins'
|
|
20
|
+
preventUpdate?: boolean
|
|
17
21
|
}): [T, React.Dispatch<React.SetStateAction<T>>] {
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
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
|
|
26
|
+
const onChangeCb = useEvent(onChange || idFn)
|
|
22
27
|
|
|
23
28
|
useEffect(() => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
[propWins, handleChange]
|
|
47
|
-
),
|
|
48
|
-
]
|
|
49
|
-
}
|
|
29
|
+
if (prop === undefined) return
|
|
30
|
+
previous.current = prop
|
|
31
|
+
setState(prop)
|
|
32
|
+
}, [prop])
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (propWins) return
|
|
36
|
+
if (state !== previous.current) {
|
|
37
|
+
previous.current = state
|
|
38
|
+
onChangeCb(state)
|
|
39
|
+
}
|
|
40
|
+
}, [onChangeCb, state, propWins])
|
|
41
|
+
|
|
42
|
+
const setter = useEvent((next) => {
|
|
43
|
+
if (preventUpdate) return
|
|
44
|
+
if (propWins) {
|
|
45
|
+
const nextValue = typeof next === 'function' ? next(previous.current) : next
|
|
46
|
+
onChangeCb(nextValue)
|
|
47
|
+
} else {
|
|
48
|
+
setState(next)
|
|
49
|
+
}
|
|
50
|
+
})
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
if (prev === next || next === undefined) {
|
|
53
|
-
return prev
|
|
54
|
-
}
|
|
55
|
-
handleChange(next)
|
|
56
|
-
return next
|
|
52
|
+
return [value as T, setter]
|
|
57
53
|
}
|
|
54
|
+
|
|
55
|
+
const idFn = () => {}
|
package/types/index.d.ts
CHANGED
|
File without changes
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
|
|
2
|
+
declare type ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>;
|
|
3
|
+
export declare function useControllableState<T>({ prop, defaultProp, onChange, strategy, preventUpdate, }: {
|
|
3
4
|
prop?: T | undefined;
|
|
4
5
|
defaultProp: T;
|
|
5
|
-
onChange?:
|
|
6
|
+
onChange?: ChangeCb<T>;
|
|
6
7
|
strategy?: 'prop-wins' | 'most-recent-wins';
|
|
8
|
+
preventUpdate?: boolean;
|
|
7
9
|
}): [T, React.Dispatch<React.SetStateAction<T>>];
|
|
10
|
+
export {};
|
|
8
11
|
//# sourceMappingURL=useControllableState.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useControllableState.d.ts","sourceRoot":"","sources":["../src/useControllableState.ts"],"names":[],"mappings":"AACA,OAAO,KAAmD,MAAM,OAAO,CAAA;AAKvE,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,EACtC,IAAI,EACJ,WAAW,EACX,QAAQ,EACR,QAAsB,GACvB,EAAE;IACD,IAAI,CAAC,EAAE,CAAC,GAAG,SAAS,CAAA;IACpB,WAAW,EAAE,CAAC,CAAA;IACd,QAAQ,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAA;IACxE,QAAQ,CAAC,EAAE,WAAW,GAAG,kBAAkB,CAAA;CAC5C,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAgC/C"}
|