@tamagui/use-controllable-state 1.0.1-beta.19 → 1.0.1-beta.190
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 -28
- 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 +1 -0
- package/dist/jsx/index.js.map +7 -0
- package/dist/jsx/useControllableState.js +35 -30
- package/dist/jsx/useControllableState.js.map +7 -0
- package/package.json +7 -4
- package/src/index.ts +1 -0
- package/src/useControllableState.ts +55 -0
- package/types/useControllableState.d.ts +5 -2
- package/types/index.d.ts.map +0 -1
- package/types/useControllableState.d.ts.map +0 -1
package/dist/cjs/index.js
CHANGED
package/dist/cjs/index.js.map
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
6
|
var __export = (target, all) => {
|
|
7
7
|
for (var name in all)
|
|
8
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -21,42 +21,48 @@ __export(useControllableState_exports, {
|
|
|
21
21
|
useControllableState: () => useControllableState
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(useControllableState_exports);
|
|
24
|
-
var
|
|
24
|
+
var import_use_event = require("@tamagui/use-event");
|
|
25
25
|
var import_react = require("react");
|
|
26
26
|
function useControllableState({
|
|
27
27
|
prop,
|
|
28
28
|
defaultProp,
|
|
29
29
|
onChange,
|
|
30
|
-
strategy = "prop-wins"
|
|
30
|
+
strategy = "prop-wins",
|
|
31
|
+
preventUpdate
|
|
31
32
|
}) {
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
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);
|
|
36
38
|
(0, import_react.useEffect)(() => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
if (prop === void 0)
|
|
40
|
+
return;
|
|
41
|
+
previous.current = prop;
|
|
42
|
+
setState(prop);
|
|
39
43
|
}, [prop]);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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];
|
|
51
63
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (prev === next || next === void 0) {
|
|
55
|
-
return prev;
|
|
56
|
-
}
|
|
57
|
-
handleChange(next);
|
|
58
|
-
return next;
|
|
59
|
-
}, "getNextStateWithCallback");
|
|
64
|
+
const idFn = () => {
|
|
65
|
+
};
|
|
60
66
|
// Annotate the CommonJS export names for ESM import in node:
|
|
61
67
|
0 && (module.exports = {
|
|
62
68
|
useControllableState
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useControllableState.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;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;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,41 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
|
|
4
|
-
import { useCallback, useEffect, useRef, useState } from "react";
|
|
1
|
+
import { useEvent } from "@tamagui/use-event";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
5
3
|
function useControllableState({
|
|
6
4
|
prop,
|
|
7
5
|
defaultProp,
|
|
8
6
|
onChange,
|
|
9
|
-
strategy = "prop-wins"
|
|
7
|
+
strategy = "prop-wins",
|
|
8
|
+
preventUpdate
|
|
10
9
|
}) {
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
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);
|
|
15
15
|
useEffect(() => {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
if (prop === void 0)
|
|
17
|
+
return;
|
|
18
|
+
previous.current = prop;
|
|
19
|
+
setState(prop);
|
|
18
20
|
}, [prop]);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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];
|
|
30
40
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (prev === next || next === void 0) {
|
|
34
|
-
return prev;
|
|
35
|
-
}
|
|
36
|
-
handleChange(next);
|
|
37
|
-
return next;
|
|
38
|
-
}, "getNextStateWithCallback");
|
|
41
|
+
const idFn = () => {
|
|
42
|
+
};
|
|
39
43
|
export {
|
|
40
44
|
useControllableState
|
|
41
45
|
};
|
|
@@ -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,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
|
@@ -1,41 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
|
|
4
|
-
import { useCallback, useEffect, useRef, useState } from "react";
|
|
1
|
+
import { useEvent } from "@tamagui/use-event";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
5
3
|
function useControllableState({
|
|
6
4
|
prop,
|
|
7
5
|
defaultProp,
|
|
8
6
|
onChange,
|
|
9
|
-
strategy = "prop-wins"
|
|
7
|
+
strategy = "prop-wins",
|
|
8
|
+
preventUpdate
|
|
10
9
|
}) {
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
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);
|
|
15
15
|
useEffect(() => {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
if (prop === void 0)
|
|
17
|
+
return;
|
|
18
|
+
previous.current = prop;
|
|
19
|
+
setState(prop);
|
|
18
20
|
}, [prop]);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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];
|
|
30
40
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (prev === next || next === void 0) {
|
|
34
|
-
return prev;
|
|
35
|
-
}
|
|
36
|
-
handleChange(next);
|
|
37
|
-
return next;
|
|
38
|
-
}, "getNextStateWithCallback");
|
|
41
|
+
const idFn = () => {
|
|
42
|
+
};
|
|
39
43
|
export {
|
|
40
44
|
useControllableState
|
|
41
45
|
};
|
|
46
|
+
//# sourceMappingURL=useControllableState.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/useControllableState.ts"],
|
|
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
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,31 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/use-controllable-state",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.190",
|
|
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
9
|
"module:jsx": "dist/jsx",
|
|
10
10
|
"files": [
|
|
11
|
+
"src",
|
|
11
12
|
"types",
|
|
12
13
|
"dist"
|
|
13
14
|
],
|
|
14
15
|
"scripts": {
|
|
15
16
|
"build": "tamagui-build",
|
|
16
17
|
"watch": "tamagui-build --watch",
|
|
18
|
+
"lint": "eslint",
|
|
19
|
+
"lint:fix": "yarn lint --fix",
|
|
17
20
|
"clean": "tamagui-build clean",
|
|
18
21
|
"clean:build": "tamagui-build clean:build"
|
|
19
22
|
},
|
|
20
23
|
"dependencies": {
|
|
21
|
-
"@
|
|
24
|
+
"@tamagui/use-event": "^1.0.1-beta.190"
|
|
22
25
|
},
|
|
23
26
|
"peerDependencies": {
|
|
24
27
|
"react": "*",
|
|
25
28
|
"react-dom": "*"
|
|
26
29
|
},
|
|
27
30
|
"devDependencies": {
|
|
28
|
-
"@tamagui/build": "^1.0.1-beta.
|
|
31
|
+
"@tamagui/build": "^1.0.1-beta.190",
|
|
29
32
|
"react": "*",
|
|
30
33
|
"react-dom": "*"
|
|
31
34
|
},
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useControllableState'
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useEvent } from '@tamagui/use-event'
|
|
2
|
+
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
|
3
|
+
|
|
4
|
+
// can configure to allow most-recent-wins or prop-wins
|
|
5
|
+
// defaults to prop-wins
|
|
6
|
+
|
|
7
|
+
type ChangeCb<T> = ((next: T) => void) | React.Dispatch<React.SetStateAction<T>>
|
|
8
|
+
|
|
9
|
+
export function useControllableState<T>({
|
|
10
|
+
prop,
|
|
11
|
+
defaultProp,
|
|
12
|
+
onChange,
|
|
13
|
+
strategy = 'prop-wins',
|
|
14
|
+
preventUpdate,
|
|
15
|
+
}: {
|
|
16
|
+
prop?: T | undefined
|
|
17
|
+
defaultProp: T
|
|
18
|
+
onChange?: ChangeCb<T>
|
|
19
|
+
strategy?: 'prop-wins' | 'most-recent-wins'
|
|
20
|
+
preventUpdate?: boolean
|
|
21
|
+
}): [T, React.Dispatch<React.SetStateAction<T>>] {
|
|
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)
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
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
|
+
})
|
|
51
|
+
|
|
52
|
+
return [value as T, setter]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const idFn = () => {}
|
|
@@ -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
|
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,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,CA6B/C"}
|