@hanzogui/use-controllable-state 2.0.0
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/LICENSE +21 -0
- package/dist/cjs/index.cjs +18 -0
- package/dist/cjs/index.native.js +21 -0
- package/dist/cjs/index.native.js.map +1 -0
- package/dist/cjs/useControllableState.cjs +72 -0
- package/dist/cjs/useControllableState.native.js +78 -0
- package/dist/cjs/useControllableState.native.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/index.native.js +2 -0
- package/dist/esm/index.native.js.map +1 -0
- package/dist/esm/useControllableState.mjs +38 -0
- package/dist/esm/useControllableState.mjs.map +1 -0
- package/dist/esm/useControllableState.native.js +41 -0
- package/dist/esm/useControllableState.native.js.map +1 -0
- package/package.json +48 -0
- package/src/index.ts +1 -0
- package/src/useControllableState.ts +66 -0
- package/types/index.d.ts +2 -0
- package/types/index.d.ts.map +1 -0
- package/types/useControllableState.d.ts +12 -0
- package/types/useControllableState.d.ts.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Nate Wienert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
7
|
+
get: () => from[key],
|
|
8
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
9
|
+
});
|
|
10
|
+
return to;
|
|
11
|
+
},
|
|
12
|
+
__reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
13
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
14
|
+
value: !0
|
|
15
|
+
}), mod);
|
|
16
|
+
var index_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(index_exports);
|
|
18
|
+
__reExport(index_exports, require("./useControllableState.cjs"), module.exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __copyProps = (to, from, except, desc) => {
|
|
8
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
9
|
+
get: () => from[key],
|
|
10
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
11
|
+
});
|
|
12
|
+
return to;
|
|
13
|
+
},
|
|
14
|
+
__reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
16
|
+
value: !0
|
|
17
|
+
}), mod);
|
|
18
|
+
var index_exports = {};
|
|
19
|
+
module.exports = __toCommonJS(index_exports);
|
|
20
|
+
__reExport(index_exports, require("./useControllableState.native.js"), module.exports);
|
|
21
|
+
//# sourceMappingURL=index.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","index_exports","module","exports"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAP,YAAc,CAAAK,aAAA","ignoreList":[]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf,
|
|
6
|
+
__hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: !0
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
__copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
15
|
+
get: () => from[key],
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
26
|
+
value: mod,
|
|
27
|
+
enumerable: !0
|
|
28
|
+
}) : target, mod)),
|
|
29
|
+
__toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
30
|
+
value: !0
|
|
31
|
+
}), mod);
|
|
32
|
+
var useControllableState_exports = {};
|
|
33
|
+
__export(useControllableState_exports, {
|
|
34
|
+
useControllableState: () => useControllableState
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(useControllableState_exports);
|
|
37
|
+
var import_use_event = require("@hanzogui/use-event"),
|
|
38
|
+
React = __toESM(require("react"), 1),
|
|
39
|
+
import_start_transition = require("@hanzogui/start-transition");
|
|
40
|
+
const emptyCallbackFn = _ => _();
|
|
41
|
+
function useControllableState({
|
|
42
|
+
prop,
|
|
43
|
+
defaultProp,
|
|
44
|
+
onChange,
|
|
45
|
+
strategy = "prop-wins",
|
|
46
|
+
preventUpdate,
|
|
47
|
+
transition
|
|
48
|
+
}) {
|
|
49
|
+
const [state, setState] = React.useState(prop ?? defaultProp),
|
|
50
|
+
previous = React.useRef(state),
|
|
51
|
+
propWins = strategy === "prop-wins" && prop !== void 0,
|
|
52
|
+
value = propWins ? prop : state,
|
|
53
|
+
onChangeCb = (0, import_use_event.useEvent)(onChange || idFn),
|
|
54
|
+
transitionFn = transition ? import_start_transition.startTransition : emptyCallbackFn;
|
|
55
|
+
React.useEffect(() => {
|
|
56
|
+
prop !== void 0 && (previous.current = prop, transitionFn(() => {
|
|
57
|
+
setState(prop);
|
|
58
|
+
}));
|
|
59
|
+
}, [prop]), React.useEffect(() => {
|
|
60
|
+
propWins || state !== previous.current && (previous.current = state, onChangeCb(state));
|
|
61
|
+
}, [onChangeCb, state, propWins]);
|
|
62
|
+
const setter = (0, import_use_event.useEvent)(next => {
|
|
63
|
+
if (!preventUpdate) if (propWins) {
|
|
64
|
+
const nextValue = typeof next == "function" ? next(previous.current) : next;
|
|
65
|
+
onChangeCb(nextValue);
|
|
66
|
+
} else transitionFn(() => {
|
|
67
|
+
setState(next);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
return [value, setter];
|
|
71
|
+
}
|
|
72
|
+
const idFn = () => {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf,
|
|
8
|
+
__hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all) __defProp(target, name, {
|
|
11
|
+
get: all[name],
|
|
12
|
+
enumerable: !0
|
|
13
|
+
});
|
|
14
|
+
},
|
|
15
|
+
__copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
17
|
+
get: () => from[key],
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: !0
|
|
30
|
+
}) : target, mod)),
|
|
31
|
+
__toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
32
|
+
value: !0
|
|
33
|
+
}), mod);
|
|
34
|
+
var useControllableState_exports = {};
|
|
35
|
+
__export(useControllableState_exports, {
|
|
36
|
+
useControllableState: () => useControllableState
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(useControllableState_exports);
|
|
39
|
+
var import_use_event = require("@hanzogui/use-event"),
|
|
40
|
+
React = __toESM(require("react"), 1),
|
|
41
|
+
import_start_transition = require("@hanzogui/start-transition"),
|
|
42
|
+
emptyCallbackFn = function (_) {
|
|
43
|
+
return _();
|
|
44
|
+
};
|
|
45
|
+
function useControllableState(param) {
|
|
46
|
+
var {
|
|
47
|
+
prop,
|
|
48
|
+
defaultProp,
|
|
49
|
+
onChange,
|
|
50
|
+
strategy = "prop-wins",
|
|
51
|
+
preventUpdate,
|
|
52
|
+
transition
|
|
53
|
+
} = param,
|
|
54
|
+
[state, setState] = React.useState(prop ?? defaultProp),
|
|
55
|
+
previous = React.useRef(state),
|
|
56
|
+
propWins = strategy === "prop-wins" && prop !== void 0,
|
|
57
|
+
value = propWins ? prop : state,
|
|
58
|
+
onChangeCb = (0, import_use_event.useEvent)(onChange || idFn),
|
|
59
|
+
transitionFn = transition ? import_start_transition.startTransition : emptyCallbackFn;
|
|
60
|
+
React.useEffect(function () {
|
|
61
|
+
prop !== void 0 && (previous.current = prop, transitionFn(function () {
|
|
62
|
+
setState(prop);
|
|
63
|
+
}));
|
|
64
|
+
}, [prop]), React.useEffect(function () {
|
|
65
|
+
propWins || state !== previous.current && (previous.current = state, onChangeCb(state));
|
|
66
|
+
}, [onChangeCb, state, propWins]);
|
|
67
|
+
var setter = (0, import_use_event.useEvent)(function (next) {
|
|
68
|
+
if (!preventUpdate) if (propWins) {
|
|
69
|
+
var nextValue = typeof next == "function" ? next(previous.current) : next;
|
|
70
|
+
onChangeCb(nextValue);
|
|
71
|
+
} else transitionFn(function () {
|
|
72
|
+
setState(next);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
return [value, setter];
|
|
76
|
+
}
|
|
77
|
+
var idFn = function () {};
|
|
78
|
+
//# sourceMappingURL=useControllableState.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","useControllableState_exports","__export","useControllableState","module","exports","import_use_event","require","React","__toESM","import_start_transition","emptyCallbackFn","_","param","prop","defaultProp","onChange","strategy","preventUpdate","transition","state","setState","useState","previous","useRef","propWins","onChangeCb","useEvent","idFn","transitionFn","startTransition","useEffect","current","setter","next","nextValue"],"sources":["../../src/useControllableState.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAAA;EAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;IAAAC,KAAA;EAAA,IAAAH,GAAA;AAAA,IAAAI,4BAAA;AAAAC,QAAA,CAAAD,4BAAA;EAAAE,oBAAA,EAAAA,CAAA,KAAAA;AAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAT,YAAyB,CAAAK,4BACzB;AAQA,IAAAK,gBAAM,GAAAC,OAAmB,CAAM,qBAAE;EAAAC,KAAA,GAAAC,OAAA,CAAAF,OAAA;EAAAG,uBAAA,GAAAH,OAAA;EAAAI,eAAA,YAAAA,CAAAC,CAAA;IAE1B,OAASA,CAAA;EAAwB;AACtC,SACAT,qBAAAU,KAAA;EACA;MAAAC,IAAA;MAAAC,WAAA;MAAAC,QAAA;MAAAC,QAAA;MAAAC,aAAA;MAAAC;IAAA,IAAAN,KAAA;IAAA,CAAAO,KAAA,EAAAC,QAAA,IAAAb,KAAA,CAAAc,QAAA,CAAAR,IAAA,IAAAC,WAAA;IAAAQ,QAAA,GAAAf,KAAA,CAAAgB,MAAA,CAAAJ,KAAA;IAAAK,QAAA,GAAAR,QAAA,oBAAAH,IAAA;IAAAd,KAAA,GAAAyB,QAAA,GAAAX,IAAA,GAAAM,KAAA;IAAAM,UAAA,OAAApB,gBAAA,CAAAqB,QAAA,EAAAX,QAAA,IAAAY,IAAA;IAAAC,YAAA,GAAAV,UAAA,GAAAT,uBAAA,CAAAoB,eAAA,GAAAnB,eAAA;EACAH,KAAA,CAAAuB,SAAW;IACXjB,IAAA,gBAAAS,QAAA,CAAAS,OAAA,GAAAlB,IAAA,EAAAe,YAAA;MACAR,QAAA,CAAAP,IAAA;IAQ+C;EAC/C,IAQAA,IAAM,CACJ,CAAI,EAAAN,KAAA,CAAAuB,SAAS,aACJ;IAEPN,QAAA,IAASL,KAAI,KAAAG,QAAA,CAAAS,OAAA,KAAAT,QAAA,CAAAS,OAAA,GAAAZ,KAAA,EAAAM,UAAA,CAAAN,KAAA;EAAA,GACd,CACHM,UAEA,EACMN,KAAA,EAKNK,QAAI,CAEJ;EACE,IAAAQ,MAAI,OAAA3B,gBAAA,CAAAqB,QAAA,YAAAO,IAAA;IACJ,KAAAhB,aAAc,EACZ,IAAAO,QAAM;MACN,IAAAU,SAAW,UAASD,IAAA,iBAAAA,IAAA,CAAAX,QAAA,CAAAS,OAAA,IAAAE,IAAA;MACtBR,UAAA,CAAAS,SAAA;IACE,OACEN,YAAS,aAAI;MACdR,QAAA,CAAAa,IAAA;IAEJ;EAED;EACF,QAEAlC,KAAM,EAAciC,MAAA,C","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc","ignoreList":[]}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useEvent } from "@hanzogui/use-event";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { startTransition } from "@hanzogui/start-transition";
|
|
4
|
+
const emptyCallbackFn = _ => _();
|
|
5
|
+
function useControllableState({
|
|
6
|
+
prop,
|
|
7
|
+
defaultProp,
|
|
8
|
+
onChange,
|
|
9
|
+
strategy = "prop-wins",
|
|
10
|
+
preventUpdate,
|
|
11
|
+
transition
|
|
12
|
+
}) {
|
|
13
|
+
const [state, setState] = React.useState(prop ?? defaultProp),
|
|
14
|
+
previous = React.useRef(state),
|
|
15
|
+
propWins = strategy === "prop-wins" && prop !== void 0,
|
|
16
|
+
value = propWins ? prop : state,
|
|
17
|
+
onChangeCb = useEvent(onChange || idFn),
|
|
18
|
+
transitionFn = transition ? startTransition : emptyCallbackFn;
|
|
19
|
+
React.useEffect(() => {
|
|
20
|
+
prop !== void 0 && (previous.current = prop, transitionFn(() => {
|
|
21
|
+
setState(prop);
|
|
22
|
+
}));
|
|
23
|
+
}, [prop]), React.useEffect(() => {
|
|
24
|
+
propWins || state !== previous.current && (previous.current = state, onChangeCb(state));
|
|
25
|
+
}, [onChangeCb, state, propWins]);
|
|
26
|
+
const setter = useEvent(next => {
|
|
27
|
+
if (!preventUpdate) if (propWins) {
|
|
28
|
+
const nextValue = typeof next == "function" ? next(previous.current) : next;
|
|
29
|
+
onChangeCb(nextValue);
|
|
30
|
+
} else transitionFn(() => {
|
|
31
|
+
setState(next);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
return [value, setter];
|
|
35
|
+
}
|
|
36
|
+
const idFn = () => {};
|
|
37
|
+
export { useControllableState };
|
|
38
|
+
//# sourceMappingURL=useControllableState.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useEvent","React","startTransition","emptyCallbackFn","_","useControllableState","prop","defaultProp","onChange","strategy","preventUpdate","transition","state","setState","useState","previous","useRef","propWins","value","onChangeCb","idFn","transitionFn","useEffect","current","setter","next","nextValue"],"sources":["../../src/useControllableState.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,QAAA,QAAgB;AACzB,YAAYC,KAAA,MAAW;AACvB,SAASC,eAAA,QAAuB;AAOhC,MAAMC,eAAA,GAAmBC,CAAA,IAAMA,CAAA,CAAE;AAE1B,SAASC,qBAAwB;EACtCC,IAAA;EACAC,WAAA;EACAC,QAAA;EACAC,QAAA,GAAW;EACXC,aAAA;EACAC;AACF,GAOiD;EAC/C,MAAM,CAACC,KAAA,EAAOC,QAAQ,IAAIZ,KAAA,CAAMa,QAAA,CAASR,IAAA,IAAQC,WAAW;IACtDQ,QAAA,GAAWd,KAAA,CAAMe,MAAA,CAAYJ,KAAK;IAClCK,QAAA,GAAWR,QAAA,KAAa,eAAeH,IAAA,KAAS;IAChDY,KAAA,GAAQD,QAAA,GAAWX,IAAA,GAAOM,KAAA;IAC1BO,UAAA,GAAanB,QAAA,CAASQ,QAAA,IAAYY,IAAI;IAEtCC,YAAA,GAAeV,UAAA,GAAaT,eAAA,GAAkBC,eAAA;EAEpDF,KAAA,CAAMqB,SAAA,CAAU,MAAM;IAChBhB,IAAA,KAAS,WACbS,QAAA,CAASQ,OAAA,GAAUjB,IAAA,EACnBe,YAAA,CAAa,MAAM;MACjBR,QAAA,CAASP,IAAI;IACf,CAAC;EACH,GAAG,CAACA,IAAI,CAAC,GAETL,KAAA,CAAMqB,SAAA,CAAU,MAAM;IAChBL,QAAA,IACAL,KAAA,KAAUG,QAAA,CAASQ,OAAA,KACrBR,QAAA,CAASQ,OAAA,GAAUX,KAAA,EACnBO,UAAA,CAAWP,KAAK;EAEpB,GAAG,CAACO,UAAA,EAAYP,KAAA,EAAOK,QAAQ,CAAC;EAEhC,MAAMO,MAAA,GAASxB,QAAA,CAAUyB,IAAA,IAAS;IAChC,IAAI,CAAAf,aAAA,EACJ,IAAIO,QAAA,EAAU;MACZ,MAAMS,SAAA,GAAY,OAAOD,IAAA,IAAS,aAAaA,IAAA,CAAKV,QAAA,CAASQ,OAAO,IAAIE,IAAA;MACxEN,UAAA,CAAWO,SAAS;IACtB,OACEL,YAAA,CAAa,MAAM;MACjBR,QAAA,CAASY,IAAI;IACf,CAAC;EAEL,CAAC;EAED,OAAO,CAACP,KAAA,EAAYM,MAAM;AAC5B;AAEA,MAAMJ,IAAA,GAAOA,CAAA,KAAM,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useEvent } from "@hanzogui/use-event";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { startTransition } from "@hanzogui/start-transition";
|
|
4
|
+
var emptyCallbackFn = function (_) {
|
|
5
|
+
return _();
|
|
6
|
+
};
|
|
7
|
+
function useControllableState(param) {
|
|
8
|
+
var {
|
|
9
|
+
prop,
|
|
10
|
+
defaultProp,
|
|
11
|
+
onChange,
|
|
12
|
+
strategy = "prop-wins",
|
|
13
|
+
preventUpdate,
|
|
14
|
+
transition
|
|
15
|
+
} = param,
|
|
16
|
+
[state, setState] = React.useState(prop ?? defaultProp),
|
|
17
|
+
previous = React.useRef(state),
|
|
18
|
+
propWins = strategy === "prop-wins" && prop !== void 0,
|
|
19
|
+
value = propWins ? prop : state,
|
|
20
|
+
onChangeCb = useEvent(onChange || idFn),
|
|
21
|
+
transitionFn = transition ? startTransition : emptyCallbackFn;
|
|
22
|
+
React.useEffect(function () {
|
|
23
|
+
prop !== void 0 && (previous.current = prop, transitionFn(function () {
|
|
24
|
+
setState(prop);
|
|
25
|
+
}));
|
|
26
|
+
}, [prop]), React.useEffect(function () {
|
|
27
|
+
propWins || state !== previous.current && (previous.current = state, onChangeCb(state));
|
|
28
|
+
}, [onChangeCb, state, propWins]);
|
|
29
|
+
var setter = useEvent(function (next) {
|
|
30
|
+
if (!preventUpdate) if (propWins) {
|
|
31
|
+
var nextValue = typeof next == "function" ? next(previous.current) : next;
|
|
32
|
+
onChangeCb(nextValue);
|
|
33
|
+
} else transitionFn(function () {
|
|
34
|
+
setState(next);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
return [value, setter];
|
|
38
|
+
}
|
|
39
|
+
var idFn = function () {};
|
|
40
|
+
export { useControllableState };
|
|
41
|
+
//# sourceMappingURL=useControllableState.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useEvent","React","startTransition","emptyCallbackFn","_","useControllableState","param","prop","defaultProp","onChange","strategy","preventUpdate","transition","state","setState","useState","previous","useRef","propWins","value","onChangeCb","idFn","transitionFn","useEffect","current","setter","next","nextValue"],"sources":["../../src/useControllableState.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,QAAA,QAAgB;AACzB,YAAYC,KAAA,MAAW;AACvB,SAASC,eAAA,QAAuB;AAOhC,IAAAC,eAAM,YAAAA,CAAyBC,CAAA,EAAE;EAE1B,OAASA,CAAA;AAAwB;AACtC,SACAC,qBAAAC,KAAA;EACA;MAAAC,IAAA;MAAAC,WAAA;MAAAC,QAAA;MAAAC,QAAA;MAAAC,aAAA;MAAAC;IAAA,IAAAN,KAAA;IAAA,CAAAO,KAAA,EAAAC,QAAA,IAAAb,KAAA,CAAAc,QAAA,CAAAR,IAAA,IAAAC,WAAA;IAAAQ,QAAA,GAAAf,KAAA,CAAAgB,MAAA,CAAAJ,KAAA;IAAAK,QAAA,GAAAR,QAAA,oBAAAH,IAAA;IAAAY,KAAA,GAAAD,QAAA,GAAAX,IAAA,GAAAM,KAAA;IAAAO,UAAA,GAAApB,QAAA,CAAAS,QAAA,IAAAY,IAAA;IAAAC,YAAA,GAAAV,UAAA,GAAAV,eAAA,GAAAC,eAAA;EACAF,KAAA,CAAAsB,SAAW;IACXhB,IAAA,gBAAAS,QAAA,CAAAQ,OAAA,GAAAjB,IAAA,EAAAe,YAAA;MACAR,QAAA,CAAAP,IAAA;IAQ+C;EAC/C,IAQAA,IAAM,CACJ,CAAI,EAAAN,KAAA,CAAAsB,SAAS,aACJ;IAEPL,QAAA,IAASL,KAAI,KAAAG,QAAA,CAAAQ,OAAA,KAAAR,QAAA,CAAAQ,OAAA,GAAAX,KAAA,EAAAO,UAAA,CAAAP,KAAA;EAAA,GACd,CACHO,UAEA,EACMP,KAAA,EAKNK,QAAI,CAEJ;EACE,IAAAO,MAAI,GAAAzB,QAAA,WAAA0B,IAAA;IACJ,KAAAf,aAAc,EACZ,IAAAO,QAAM;MACN,IAAAS,SAAW,UAASD,IAAA,iBAAAA,IAAA,CAAAV,QAAA,CAAAQ,OAAA,IAAAE,IAAA;MACtBN,UAAA,CAAAO,SAAA;IACE,OACEL,YAAS,aAAI;MACdR,QAAA,CAAAY,IAAA;IAEJ;EAED;EACF,QAEAP,KAAM,EAAcM,MAAA,C","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hanzogui/use-controllable-state",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14",
|
|
5
|
+
"source": "src/index.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"src",
|
|
8
|
+
"types",
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"main": "dist/cjs",
|
|
14
|
+
"module": "dist/esm",
|
|
15
|
+
"types": "./types/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
"./package.json": "./package.json",
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./types/index.d.ts",
|
|
20
|
+
"react-native": "./dist/esm/index.native.js",
|
|
21
|
+
"browser": "./dist/esm/index.mjs",
|
|
22
|
+
"module": "./dist/esm/index.mjs",
|
|
23
|
+
"import": "./dist/esm/index.mjs",
|
|
24
|
+
"require": "./dist/cjs/index.cjs",
|
|
25
|
+
"default": "./dist/esm/index.mjs"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "hanzo-gui-build",
|
|
33
|
+
"watch": "hanzo-gui-build --watch",
|
|
34
|
+
"clean": "hanzo-gui-build clean",
|
|
35
|
+
"clean:build": "hanzo-gui-build clean:build"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@hanzogui/start-transition": "workspace:*",
|
|
39
|
+
"@hanzogui/use-event": "workspace:*"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@hanzogui/build": "workspace:*",
|
|
43
|
+
"react": ">=19"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"react": ">=19"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useControllableState'
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { useEvent } from '@hanzogui/use-event'
|
|
2
|
+
import * as React from 'react'
|
|
3
|
+
import { startTransition } from '@hanzogui/start-transition'
|
|
4
|
+
|
|
5
|
+
// can configure to allow most-recent-wins or prop-wins
|
|
6
|
+
// defaults to prop-wins
|
|
7
|
+
|
|
8
|
+
type ChangeCb<T> = (next: T) => void
|
|
9
|
+
|
|
10
|
+
const emptyCallbackFn = (_) => _()
|
|
11
|
+
|
|
12
|
+
export function useControllableState<T>({
|
|
13
|
+
prop,
|
|
14
|
+
defaultProp,
|
|
15
|
+
onChange,
|
|
16
|
+
strategy = 'prop-wins',
|
|
17
|
+
preventUpdate,
|
|
18
|
+
transition,
|
|
19
|
+
}: {
|
|
20
|
+
prop?: T | undefined
|
|
21
|
+
defaultProp: T
|
|
22
|
+
onChange?: ChangeCb<T>
|
|
23
|
+
strategy?: 'prop-wins' | 'most-recent-wins'
|
|
24
|
+
preventUpdate?: boolean
|
|
25
|
+
transition?: boolean
|
|
26
|
+
}): [T, React.Dispatch<React.SetStateAction<T>>] {
|
|
27
|
+
const [state, setState] = React.useState(prop ?? defaultProp)
|
|
28
|
+
const previous = React.useRef<any>(state)
|
|
29
|
+
const propWins = strategy === 'prop-wins' && prop !== undefined
|
|
30
|
+
const value = propWins ? prop : state
|
|
31
|
+
const onChangeCb = useEvent(onChange || idFn)
|
|
32
|
+
|
|
33
|
+
const transitionFn = transition ? startTransition : emptyCallbackFn
|
|
34
|
+
|
|
35
|
+
React.useEffect(() => {
|
|
36
|
+
if (prop === undefined) return
|
|
37
|
+
previous.current = prop
|
|
38
|
+
transitionFn(() => {
|
|
39
|
+
setState(prop)
|
|
40
|
+
})
|
|
41
|
+
}, [prop])
|
|
42
|
+
|
|
43
|
+
React.useEffect(() => {
|
|
44
|
+
if (propWins) return
|
|
45
|
+
if (state !== previous.current) {
|
|
46
|
+
previous.current = state
|
|
47
|
+
onChangeCb(state)
|
|
48
|
+
}
|
|
49
|
+
}, [onChangeCb, state, propWins])
|
|
50
|
+
|
|
51
|
+
const setter = useEvent((next) => {
|
|
52
|
+
if (preventUpdate) return
|
|
53
|
+
if (propWins) {
|
|
54
|
+
const nextValue = typeof next === 'function' ? next(previous.current) : next
|
|
55
|
+
onChangeCb(nextValue)
|
|
56
|
+
} else {
|
|
57
|
+
transitionFn(() => {
|
|
58
|
+
setState(next)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
return [value as T, setter]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const idFn = () => {}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
type ChangeCb<T> = (next: T) => void;
|
|
3
|
+
export declare function useControllableState<T>({ prop, defaultProp, onChange, strategy, preventUpdate, transition, }: {
|
|
4
|
+
prop?: T | undefined;
|
|
5
|
+
defaultProp: T;
|
|
6
|
+
onChange?: ChangeCb<T>;
|
|
7
|
+
strategy?: 'prop-wins' | 'most-recent-wins';
|
|
8
|
+
preventUpdate?: boolean;
|
|
9
|
+
transition?: boolean;
|
|
10
|
+
}): [T, React.Dispatch<React.SetStateAction<T>>];
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=useControllableState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useControllableState.d.ts","sourceRoot":"","sources":["../src/useControllableState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAM9B,KAAK,QAAQ,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAA;AAIpC,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,EACtC,IAAI,EACJ,WAAW,EACX,QAAQ,EACR,QAAsB,EACtB,aAAa,EACb,UAAU,GACX,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;IACvB,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAsC/C"}
|