cross-state 0.53.3 → 0.54.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/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/diff-BQ8bB3Wk.d.cts +22 -0
- package/dist/diff-gZezL04N.d.ts +22 -0
- package/dist/extendedJson-BZkQBXEv.js +29 -0
- package/dist/extendedJson-BZkQBXEv.js.map +1 -0
- package/dist/extendedJson-Dn2F7Edo.cjs +53 -0
- package/dist/extendedJson-Dn2F7Edo.cjs.map +1 -0
- package/dist/hash-BV06P7va.js +19 -0
- package/dist/hash-BV06P7va.js.map +1 -0
- package/dist/hash-CJPl7SRj.cjs +30 -0
- package/dist/hash-CJPl7SRj.cjs.map +1 -0
- package/dist/index.cjs +49 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +59 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/mutative/index.cjs +3 -0
- package/dist/mutative/index.d.cts +3 -0
- package/dist/mutative/index.d.ts +3 -0
- package/dist/mutative/index.js +3 -0
- package/dist/mutative/register.cjs +10 -0
- package/dist/mutative/register.cjs.map +1 -0
- package/dist/mutative/register.d.cts +11 -0
- package/dist/mutative/register.d.ts +9 -0
- package/dist/mutative/register.js +10 -0
- package/dist/mutative/register.js.map +1 -0
- package/dist/mutativeMethods-6QzygG3W.d.cts +13 -0
- package/dist/mutativeMethods-Br3xN2Iq.d.ts +13 -0
- package/dist/mutativeMethods-CZKeRIDL.js +27 -0
- package/dist/mutativeMethods-CZKeRIDL.js.map +1 -0
- package/dist/mutativeMethods-CgXk_bMt.cjs +33 -0
- package/dist/mutativeMethods-CgXk_bMt.cjs.map +1 -0
- package/dist/patchMethods-C3L7Kh8T.d.cts +48 -0
- package/dist/patchMethods-D5YJwcpn.cjs +264 -0
- package/dist/patchMethods-D5YJwcpn.cjs.map +1 -0
- package/dist/patchMethods-DRPfkhAe.js +241 -0
- package/dist/patchMethods-DRPfkhAe.js.map +1 -0
- package/dist/patchMethods-Dxad6HF6.d.ts +48 -0
- package/dist/patches/index.cjs +5 -0
- package/dist/patches/index.d.cts +4 -0
- package/dist/patches/index.d.ts +4 -0
- package/dist/patches/index.js +5 -0
- package/dist/patches/register.cjs +11 -0
- package/dist/patches/register.cjs.map +1 -0
- package/dist/patches/register.d.cts +12 -0
- package/dist/patches/register.d.ts +10 -0
- package/dist/patches/register.js +11 -0
- package/dist/patches/register.js.map +1 -0
- package/dist/persist/register.cjs +13 -0
- package/dist/persist/register.cjs.map +1 -0
- package/dist/persist/register.d.cts +15 -0
- package/dist/persist/register.d.ts +13 -0
- package/dist/persist/register.js +13 -0
- package/dist/persist/register.js.map +1 -0
- package/dist/persist-BluRVcW6.js +258 -0
- package/dist/persist-BluRVcW6.js.map +1 -0
- package/dist/persist-CPjpg6D0.d.ts +61 -0
- package/dist/persist-CztClydq.cjs +263 -0
- package/dist/persist-CztClydq.cjs.map +1 -0
- package/dist/persist-D7MAsyyW.d.cts +61 -0
- package/dist/propAccess-ByqBjC19.cjs +173 -0
- package/dist/propAccess-ByqBjC19.cjs.map +1 -0
- package/dist/propAccess-DGchzVfy.js +113 -0
- package/dist/propAccess-DGchzVfy.js.map +1 -0
- package/dist/react/index.cjs +610 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +305 -0
- package/dist/react/index.d.ts +305 -0
- package/dist/react/index.js +590 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/register.cjs +16 -0
- package/dist/react/register.cjs.map +1 -0
- package/dist/react/register.d.cts +16 -0
- package/dist/react/register.d.ts +14 -0
- package/dist/react/register.js +16 -0
- package/dist/react/register.js.map +1 -0
- package/dist/scope-DI1OFHcr.d.ts +167 -0
- package/dist/scope-DO5vpLPV.d.cts +167 -0
- package/dist/scope-DU5dFP1T.js +322 -0
- package/dist/scope-DU5dFP1T.js.map +1 -0
- package/dist/scope-DvqvkIHB.cjs +369 -0
- package/dist/scope-DvqvkIHB.cjs.map +1 -0
- package/dist/store-BEsiS8y7.d.cts +242 -0
- package/dist/store-BWMWEUpu.cjs +819 -0
- package/dist/store-BWMWEUpu.cjs.map +1 -0
- package/dist/store-DKaeE840.d.ts +242 -0
- package/dist/store-Dr8gM3lq.js +730 -0
- package/dist/store-Dr8gM3lq.js.map +1 -0
- package/dist/storeMethods-AIJSwxHz.js +315 -0
- package/dist/storeMethods-AIJSwxHz.js.map +1 -0
- package/dist/storeMethods-BQwU2Uia.d.cts +107 -0
- package/dist/storeMethods-C4luym7C.d.ts +107 -0
- package/dist/storeMethods-DVVsmUg1.cjs +375 -0
- package/dist/storeMethods-DVVsmUg1.cjs.map +1 -0
- package/package.json +92 -135
- package/dist/cjs/hash.cjs +0 -30
- package/dist/cjs/hash.cjs.map +0 -1
- package/dist/cjs/index.cjs +0 -56
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index2.cjs +0 -300
- package/dist/cjs/index2.cjs.map +0 -1
- package/dist/cjs/mutative/index.cjs +0 -10
- package/dist/cjs/mutative/index.cjs.map +0 -1
- package/dist/cjs/mutative/register.cjs +0 -8
- package/dist/cjs/mutative/register.cjs.map +0 -1
- package/dist/cjs/mutativeMethods.cjs +0 -29
- package/dist/cjs/mutativeMethods.cjs.map +0 -1
- package/dist/cjs/patches/index.cjs +0 -10
- package/dist/cjs/patches/index.cjs.map +0 -1
- package/dist/cjs/patches/register.cjs +0 -8
- package/dist/cjs/patches/register.cjs.map +0 -1
- package/dist/cjs/persist/register.cjs +0 -11
- package/dist/cjs/persist/register.cjs.map +0 -1
- package/dist/cjs/persist.cjs +0 -302
- package/dist/cjs/persist.cjs.map +0 -1
- package/dist/cjs/propAccess.cjs +0 -162
- package/dist/cjs/propAccess.cjs.map +0 -1
- package/dist/cjs/react/index.cjs +0 -628
- package/dist/cjs/react/index.cjs.map +0 -1
- package/dist/cjs/react/register.cjs +0 -13
- package/dist/cjs/react/register.cjs.map +0 -1
- package/dist/cjs/scope.cjs +0 -400
- package/dist/cjs/scope.cjs.map +0 -1
- package/dist/cjs/store.cjs +0 -829
- package/dist/cjs/store.cjs.map +0 -1
- package/dist/cjs/storeMethods.cjs +0 -856
- package/dist/cjs/storeMethods.cjs.map +0 -1
- package/dist/cjs/urlStore.cjs +0 -120
- package/dist/cjs/urlStore.cjs.map +0 -1
- package/dist/es/hash.mjs +0 -27
- package/dist/es/hash.mjs.map +0 -1
- package/dist/es/index.mjs +0 -20
- package/dist/es/index.mjs.map +0 -1
- package/dist/es/index2.mjs +0 -291
- package/dist/es/index2.mjs.map +0 -1
- package/dist/es/mutative/index.mjs +0 -2
- package/dist/es/mutative/index.mjs.map +0 -1
- package/dist/es/mutative/register.mjs +0 -6
- package/dist/es/mutative/register.mjs.map +0 -1
- package/dist/es/mutativeMethods.mjs +0 -27
- package/dist/es/mutativeMethods.mjs.map +0 -1
- package/dist/es/patches/index.mjs +0 -2
- package/dist/es/patches/index.mjs.map +0 -1
- package/dist/es/patches/register.mjs +0 -6
- package/dist/es/patches/register.mjs.map +0 -1
- package/dist/es/persist/register.mjs +0 -9
- package/dist/es/persist/register.mjs.map +0 -1
- package/dist/es/persist.mjs +0 -300
- package/dist/es/persist.mjs.map +0 -1
- package/dist/es/propAccess.mjs +0 -151
- package/dist/es/propAccess.mjs.map +0 -1
- package/dist/es/react/index.mjs +0 -611
- package/dist/es/react/index.mjs.map +0 -1
- package/dist/es/react/register.mjs +0 -11
- package/dist/es/react/register.mjs.map +0 -1
- package/dist/es/scope.mjs +0 -391
- package/dist/es/scope.mjs.map +0 -1
- package/dist/es/store.mjs +0 -812
- package/dist/es/store.mjs.map +0 -1
- package/dist/es/storeMethods.mjs +0 -845
- package/dist/es/storeMethods.mjs.map +0 -1
- package/dist/es/urlStore.mjs +0 -116
- package/dist/es/urlStore.mjs.map +0 -1
- package/dist/types/core/cache.d.ts +0 -119
- package/dist/types/core/commonTypes.d.ts +0 -76
- package/dist/types/core/index.d.ts +0 -6
- package/dist/types/core/resourceGroup.d.ts +0 -16
- package/dist/types/core/scope.d.ts +0 -5
- package/dist/types/core/store.d.ts +0 -86
- package/dist/types/core/urlStore.d.ts +0 -21
- package/dist/types/index.d.ts +0 -14
- package/dist/types/lib/applyPatches.d.ts +0 -2
- package/dist/types/lib/autobind.d.ts +0 -1
- package/dist/types/lib/cacheState.d.ts +0 -20
- package/dist/types/lib/calcDuration.d.ts +0 -2
- package/dist/types/lib/calculatedValue.d.ts +0 -9
- package/dist/types/lib/castArray.d.ts +0 -1
- package/dist/types/lib/clone.d.ts +0 -1
- package/dist/types/lib/constrain.d.ts +0 -1
- package/dist/types/lib/debounce.d.ts +0 -12
- package/dist/types/lib/deferred.d.ts +0 -5
- package/dist/types/lib/diff.d.ts +0 -17
- package/dist/types/lib/disposable.d.ts +0 -3
- package/dist/types/lib/equals.d.ts +0 -3
- package/dist/types/lib/extendedJson.d.ts +0 -5
- package/dist/types/lib/forwardError.d.ts +0 -1
- package/dist/types/lib/hash.d.ts +0 -5
- package/dist/types/lib/helpers.d.ts +0 -2
- package/dist/types/lib/instanceCache.d.ts +0 -18
- package/dist/types/lib/isPromise.d.ts +0 -1
- package/dist/types/lib/makeSelector.d.ts +0 -2
- package/dist/types/lib/maybePromise.d.ts +0 -1
- package/dist/types/lib/path.d.ts +0 -41
- package/dist/types/lib/promiseChain.d.ts +0 -5
- package/dist/types/lib/promiseWithCancel.d.ts +0 -8
- package/dist/types/lib/promiseWithState.d.ts +0 -9
- package/dist/types/lib/propAccess.d.ts +0 -7
- package/dist/types/lib/queue.d.ts +0 -11
- package/dist/types/lib/standardMethods.d.ts +0 -20
- package/dist/types/lib/throttle.d.ts +0 -2
- package/dist/types/lib/trackingProxy.d.ts +0 -2
- package/dist/types/lib/trie.d.ts +0 -11
- package/dist/types/lib/typeHelpers.d.ts +0 -12
- package/dist/types/lib/unwrapPromise.d.ts +0 -1
- package/dist/types/lib/updateHelpers.d.ts +0 -1
- package/dist/types/lib/wildcardMatch.d.ts +0 -3
- package/dist/types/mutative/index.d.ts +0 -1
- package/dist/types/mutative/mutativeMethods.d.ts +0 -11
- package/dist/types/mutative/register.d.ts +0 -7
- package/dist/types/patches/index.d.ts +0 -1
- package/dist/types/patches/patchMethods.d.ts +0 -44
- package/dist/types/patches/register.d.ts +0 -7
- package/dist/types/persist/index.d.ts +0 -3
- package/dist/types/persist/persist.d.ts +0 -40
- package/dist/types/persist/persistPathHelpers.d.ts +0 -6
- package/dist/types/persist/persistStorage.d.ts +0 -17
- package/dist/types/persist/register.d.ts +0 -9
- package/dist/types/react/cacheMethods.d.ts +0 -5
- package/dist/types/react/form/customInput.d.ts +0 -6
- package/dist/types/react/form/form.d.ts +0 -98
- package/dist/types/react/form/formField.d.ts +0 -68
- package/dist/types/react/form/formForEach.d.ts +0 -19
- package/dist/types/react/form/index.d.ts +0 -3
- package/dist/types/react/form/useFormAutosave.d.ts +0 -9
- package/dist/types/react/index.d.ts +0 -11
- package/dist/types/react/loadingBoundary.d.ts +0 -21
- package/dist/types/react/register.d.ts +0 -15
- package/dist/types/react/scope.d.ts +0 -24
- package/dist/types/react/scopeMethods.d.ts +0 -20
- package/dist/types/react/storeMethods.d.ts +0 -15
- package/dist/types/react/useCache.d.ts +0 -34
- package/dist/types/react/useDecoupledState.d.ts +0 -7
- package/dist/types/react/useProp.d.ts +0 -8
- package/dist/types/react/useStore.d.ts +0 -16
- package/dist/types/react/useUrlParamScope.d.ts +0 -4
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
import { autobind, calcDuration, createStore, debounce, queue, throttle } from "../store-Dr8gM3lq.js";
|
|
2
|
+
import { castArrayPath, deepEqual, get, isObject, join, set } from "../propAccess-DGchzVfy.js";
|
|
3
|
+
import { simpleHash } from "../hash-BV06P7va.js";
|
|
4
|
+
import { fromExtendedJsonString, toExtendedJsonString } from "../extendedJson-BZkQBXEv.js";
|
|
5
|
+
import { LoadingBoundary, ScopeProvider, cacheMethods, scopeMethods, storeMethods, useCache, useLoadingBoundary, useProp, useScope, useStore } from "../storeMethods-AIJSwxHz.js";
|
|
6
|
+
import React, { Fragment, createContext, createElement, startTransition, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
7
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
|
|
9
|
+
//#region src/react/form/customInput.tsx
|
|
10
|
+
function CustomInput({ name, children,...props }) {
|
|
11
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
12
|
+
...props,
|
|
13
|
+
style: {
|
|
14
|
+
position: "relative",
|
|
15
|
+
...props.style
|
|
16
|
+
},
|
|
17
|
+
children: [children, /* @__PURE__ */ jsx("input", {
|
|
18
|
+
name,
|
|
19
|
+
style: {
|
|
20
|
+
position: "absolute",
|
|
21
|
+
top: 0,
|
|
22
|
+
left: 0,
|
|
23
|
+
opacity: 0,
|
|
24
|
+
width: "100%",
|
|
25
|
+
height: "100%",
|
|
26
|
+
pointerEvents: "none"
|
|
27
|
+
}
|
|
28
|
+
})]
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/lib/wildcardMatch.ts
|
|
34
|
+
function getWildCardMatches(object, path) {
|
|
35
|
+
const matches = {};
|
|
36
|
+
const [first, second, ...rest] = castArrayPath(path);
|
|
37
|
+
if (first === void 0) throw new Error("Path is empty");
|
|
38
|
+
if (!Array.isArray(object) && !isObject(object)) object = {};
|
|
39
|
+
for (const [key, value] of first !== "*" ? [[first, object[first]]] : Object.entries(object)) {
|
|
40
|
+
if (first !== "*" && first !== key) continue;
|
|
41
|
+
if (second === void 0) {
|
|
42
|
+
matches[key] = value;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
for (const [subKey, subValue] of Object.entries(getWildCardMatches(value, [second, ...rest]))) matches[`${key}.${subKey}`] = subValue;
|
|
46
|
+
}
|
|
47
|
+
return matches;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/react/form/formField.tsx
|
|
52
|
+
function FormField({ name = "", component, commitOnBlur, commitDebounce, render, inputFilter, defaultValue, serialize, deserialize = (x) => x, onChange, onBlur,...restProps }) {
|
|
53
|
+
const form = this.useForm();
|
|
54
|
+
const getFormState = () => ({
|
|
55
|
+
...form,
|
|
56
|
+
...getDerivedState(form)
|
|
57
|
+
});
|
|
58
|
+
const [localValue, setLocalValue] = useState();
|
|
59
|
+
const value = this.useFormState((form$1) => {
|
|
60
|
+
const value$1 = form$1.getField(name).value;
|
|
61
|
+
if (serialize) return serialize(value$1, getFormState());
|
|
62
|
+
if (value$1 !== void 0) return value$1;
|
|
63
|
+
return defaultValue;
|
|
64
|
+
});
|
|
65
|
+
const setValue = (x) => form.getField(name).setValue(deserialize(x, getFormState()));
|
|
66
|
+
const hasTriggeredValidations = this.useFormState((form$1) => form$1.hasTriggeredValidations);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (localValue === void 0 || !commitDebounce) return;
|
|
69
|
+
const timeout = setTimeout(() => {
|
|
70
|
+
setValue(localValue);
|
|
71
|
+
setLocalValue(void 0);
|
|
72
|
+
}, commitDebounce);
|
|
73
|
+
return () => clearTimeout(timeout);
|
|
74
|
+
}, [localValue, commitDebounce]);
|
|
75
|
+
const props = {
|
|
76
|
+
name,
|
|
77
|
+
value: localValue ?? value,
|
|
78
|
+
onChange: (event, ...moreArgs) => {
|
|
79
|
+
const value$1 = typeof event === "object" && event !== null && "target" in event ? event.target.value : event;
|
|
80
|
+
if (inputFilter && !inputFilter(value$1)) return;
|
|
81
|
+
if (commitOnBlur || commitDebounce) setLocalValue(value$1);
|
|
82
|
+
else setValue(value$1);
|
|
83
|
+
onChange?.(event, ...moreArgs);
|
|
84
|
+
},
|
|
85
|
+
onBlur(...args) {
|
|
86
|
+
if (localValue !== void 0) {
|
|
87
|
+
setValue(localValue);
|
|
88
|
+
setLocalValue(void 0);
|
|
89
|
+
}
|
|
90
|
+
onBlur?.(...args);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
if (render) return /* @__PURE__ */ jsx(Fragment$1, { children: render(props, {
|
|
94
|
+
...form.getField(name),
|
|
95
|
+
hasTriggeredValidations
|
|
96
|
+
}, form) ?? null });
|
|
97
|
+
if (component) return createElement(component, {
|
|
98
|
+
...restProps,
|
|
99
|
+
...props
|
|
100
|
+
});
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/react/form/formForEach.tsx
|
|
106
|
+
function FormForEach({ name, renderElement, children }) {
|
|
107
|
+
const form = this.useForm();
|
|
108
|
+
const names = this.useFormState(() => {
|
|
109
|
+
const field = form.getField(name);
|
|
110
|
+
return field.names;
|
|
111
|
+
});
|
|
112
|
+
const add = useCallback((...args) => {
|
|
113
|
+
const field = form.getField(name);
|
|
114
|
+
field.add(...args);
|
|
115
|
+
}, [form]);
|
|
116
|
+
const remove = useCallback((key) => {
|
|
117
|
+
const field = form.getField(name);
|
|
118
|
+
field.remove(key);
|
|
119
|
+
}, [form]);
|
|
120
|
+
const setValue = useCallback((value) => {
|
|
121
|
+
const field = form.getField(name);
|
|
122
|
+
field.setValue(value);
|
|
123
|
+
}, [form]);
|
|
124
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [renderElement && names.map((name$1, index) => {
|
|
125
|
+
const key = name$1.split(".").pop();
|
|
126
|
+
return /* @__PURE__ */ jsx(Fragment, { children: renderElement({
|
|
127
|
+
name: name$1,
|
|
128
|
+
key,
|
|
129
|
+
index,
|
|
130
|
+
remove: () => remove(key)
|
|
131
|
+
}) }, key);
|
|
132
|
+
}), children?.({
|
|
133
|
+
names,
|
|
134
|
+
add,
|
|
135
|
+
remove,
|
|
136
|
+
setValue
|
|
137
|
+
})] });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region src/react/form/useFormAutosave.ts
|
|
142
|
+
function useFormAutosave(form) {
|
|
143
|
+
const { formState, options, getDraft } = form;
|
|
144
|
+
const debounceTime = calcDuration(options.autoSave?.debounce ?? 2e3);
|
|
145
|
+
const latestRef = useRef({ options });
|
|
146
|
+
const lastValue = useRef(void 0);
|
|
147
|
+
const q = useMemo(() => queue(), []);
|
|
148
|
+
const run = useMemo(() => debounce(async () => {
|
|
149
|
+
const { options: options$1 } = latestRef.current;
|
|
150
|
+
const save = options$1.autoSave?.save;
|
|
151
|
+
const draft = getDraft();
|
|
152
|
+
lastValue.current = draft;
|
|
153
|
+
q.clear();
|
|
154
|
+
q(async () => {
|
|
155
|
+
try {
|
|
156
|
+
formState.set("saveInProgress", true);
|
|
157
|
+
await save?.(draft, form);
|
|
158
|
+
if (q.size === 0 && options$1.autoSave?.resetAfterSave) form.reset();
|
|
159
|
+
} finally {
|
|
160
|
+
formState.set("saveInProgress", false);
|
|
161
|
+
if (q.size === 0) formState.set("saveScheduled", false);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}, debounceTime), [formState, debounceTime]);
|
|
165
|
+
useEffect(() => {
|
|
166
|
+
if (!options.autoSave?.save) return;
|
|
167
|
+
return formState.map((state) => state.draft).subscribe(() => {
|
|
168
|
+
if (deepEqual(getDraft(), lastValue.current)) return;
|
|
169
|
+
run();
|
|
170
|
+
formState.set("saveScheduled", true);
|
|
171
|
+
}, { runNow: false });
|
|
172
|
+
}, [formState]);
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
latestRef.current = { options };
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/react/form/form.tsx
|
|
180
|
+
function FormContainer({ form,...formProps }) {
|
|
181
|
+
const formInstance = form.useForm();
|
|
182
|
+
const hasTriggeredValidations = form.useFormState((state) => state.hasTriggeredValidations);
|
|
183
|
+
const formRef = useRef(null);
|
|
184
|
+
function updateValidity(errors, buttonElement) {
|
|
185
|
+
const formElement = formRef.current;
|
|
186
|
+
if (!formElement) return;
|
|
187
|
+
const localizedErrors = new Map([...errors.entries()].map(([field, errors$1]) => [field, errors$1.map((error) => formInstance.options.localizeError?.(error, field) ?? error)]));
|
|
188
|
+
for (const element of Array.from(formElement.elements)) if ("name" in element && "setCustomValidity" in element) element.setCustomValidity(localizedErrors.get(element.name)?.join("\n") ?? "");
|
|
189
|
+
if (buttonElement && "setCustomValidity" in buttonElement) {
|
|
190
|
+
const errorString = [...errors.values()].flat().join("\n");
|
|
191
|
+
buttonElement.setCustomValidity(errorString);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
return formInstance.formState.map(() => formInstance.getErrors()).subscribe((errors) => updateValidity(errors));
|
|
196
|
+
}, []);
|
|
197
|
+
return /* @__PURE__ */ jsx("form", {
|
|
198
|
+
ref: formRef,
|
|
199
|
+
noValidate: true,
|
|
200
|
+
...formProps,
|
|
201
|
+
className: [formProps.className, hasTriggeredValidations ? formInstance.options.validatedClass ?? "validated" : void 0].filter(Boolean).join(" "),
|
|
202
|
+
onSubmit: async (event) => {
|
|
203
|
+
if (formInstance.saveInProgress()) return;
|
|
204
|
+
try {
|
|
205
|
+
formInstance.formState.set("saveInProgress", true);
|
|
206
|
+
event.preventDefault();
|
|
207
|
+
const formElement = event.currentTarget;
|
|
208
|
+
const buttonElement = event.nativeEvent instanceof SubmitEvent && event.nativeEvent.submitter instanceof HTMLButtonElement ? event.nativeEvent.submitter : void 0;
|
|
209
|
+
updateValidity(formInstance.getErrors(), buttonElement);
|
|
210
|
+
formElement.reportValidity();
|
|
211
|
+
const isValid = formInstance.validate();
|
|
212
|
+
if (isValid) await formProps.onSubmit?.(event, {
|
|
213
|
+
...formInstance,
|
|
214
|
+
...getDerivedState(formInstance)
|
|
215
|
+
});
|
|
216
|
+
} finally {
|
|
217
|
+
formInstance.formState.set("saveInProgress", false);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
function getField(form, path) {
|
|
223
|
+
const field = {
|
|
224
|
+
get originalValue() {
|
|
225
|
+
return form.original !== void 0 ? get(form.original, path) : void 0;
|
|
226
|
+
},
|
|
227
|
+
get value() {
|
|
228
|
+
const draft = form.getDraft();
|
|
229
|
+
return get(draft ?? form.original ?? form.options.defaultValue, path);
|
|
230
|
+
},
|
|
231
|
+
setValue(update) {
|
|
232
|
+
form.formState.set("draft", (draft = form.original ?? form.options.defaultValue) => {
|
|
233
|
+
if (update instanceof Function) update = update(get(draft, path));
|
|
234
|
+
return set(draft, path, update);
|
|
235
|
+
});
|
|
236
|
+
},
|
|
237
|
+
get hasChange() {
|
|
238
|
+
return !deepEqual(this.originalValue, this.value);
|
|
239
|
+
},
|
|
240
|
+
get errors() {
|
|
241
|
+
const errors = form.getErrors();
|
|
242
|
+
return errors.get(path) ?? [];
|
|
243
|
+
},
|
|
244
|
+
get names() {
|
|
245
|
+
const { value } = this;
|
|
246
|
+
if (Array.isArray(value)) return value.map((_, index) => join(path, String(index)));
|
|
247
|
+
if (isObject(value)) return Object.keys(value).map((key) => join(path, key));
|
|
248
|
+
return [];
|
|
249
|
+
},
|
|
250
|
+
add(...args) {
|
|
251
|
+
this.setValue((value) => {
|
|
252
|
+
if (!value) throw new Error(`Cannot add element to ${JSON.stringify(value)}`);
|
|
253
|
+
if (Array.isArray(value)) return [...value ?? [], args[0]];
|
|
254
|
+
if (isObject(value)) return {
|
|
255
|
+
...value,
|
|
256
|
+
[args[0]]: args[1]
|
|
257
|
+
};
|
|
258
|
+
throw new Error(`Cannot add element to ${JSON.stringify(value)}`);
|
|
259
|
+
});
|
|
260
|
+
},
|
|
261
|
+
remove(key) {
|
|
262
|
+
this.setValue((value) => {
|
|
263
|
+
if (!value) throw new Error(`Cannot remove element from ${JSON.stringify(value)}`);
|
|
264
|
+
if (Array.isArray(value)) return value.filter((_, index) => index !== Number(key));
|
|
265
|
+
if (isObject(value)) {
|
|
266
|
+
const { [key]: _,...rest } = value;
|
|
267
|
+
return rest;
|
|
268
|
+
}
|
|
269
|
+
throw new Error(`Cannot remove element from ${JSON.stringify(value)}`);
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
return field;
|
|
274
|
+
}
|
|
275
|
+
function getErrors(draft, original, validations) {
|
|
276
|
+
const errors = /* @__PURE__ */ new Map();
|
|
277
|
+
for (const [path, block] of Object.entries(validations ?? {})) for (const [validationName, validate] of Object.entries(block)) {
|
|
278
|
+
let matched = false;
|
|
279
|
+
for (const [field, value] of Object.entries(getWildCardMatches(draft, path))) {
|
|
280
|
+
matched = true;
|
|
281
|
+
if (!validate(value, {
|
|
282
|
+
draft,
|
|
283
|
+
original,
|
|
284
|
+
field
|
|
285
|
+
})) {
|
|
286
|
+
const fieldErrors = errors.get(field) ?? [];
|
|
287
|
+
fieldErrors.push(validationName);
|
|
288
|
+
errors.set(field, fieldErrors);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (!matched && !path.includes("*")) {
|
|
292
|
+
if (!validate(void 0, {
|
|
293
|
+
draft,
|
|
294
|
+
original,
|
|
295
|
+
field: path
|
|
296
|
+
})) {
|
|
297
|
+
const fieldErrors = errors.get(path) ?? [];
|
|
298
|
+
fieldErrors.push(validationName);
|
|
299
|
+
errors.set(path, fieldErrors);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return errors;
|
|
304
|
+
}
|
|
305
|
+
function getDerivedState(instance) {
|
|
306
|
+
return {
|
|
307
|
+
draft: instance.getDraft(),
|
|
308
|
+
hasTriggeredValidations: instance.hasTriggeredValidations(),
|
|
309
|
+
saveScheduled: instance.saveScheduled(),
|
|
310
|
+
saveInProgress: instance.saveInProgress(),
|
|
311
|
+
hasChanges: instance.hasChanges(),
|
|
312
|
+
errors: instance.getErrors(),
|
|
313
|
+
isValid: instance.isValid()
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
var Form = class Form {
|
|
317
|
+
constructor(options) {
|
|
318
|
+
this.options = options;
|
|
319
|
+
this.context = createContext(null);
|
|
320
|
+
autobind(Form);
|
|
321
|
+
}
|
|
322
|
+
useForm() {
|
|
323
|
+
const context = useContext(this.context);
|
|
324
|
+
if (!context) throw new Error("Form context not found");
|
|
325
|
+
return context;
|
|
326
|
+
}
|
|
327
|
+
useFormState(selector, useStoreOptions) {
|
|
328
|
+
const form = this.useForm();
|
|
329
|
+
return useStore(form.formState, () => selector({
|
|
330
|
+
...form,
|
|
331
|
+
...getDerivedState(form)
|
|
332
|
+
}), useStoreOptions);
|
|
333
|
+
}
|
|
334
|
+
useField(path, useStoreOptions) {
|
|
335
|
+
const form = this.useForm();
|
|
336
|
+
this.useFormState((form$1) => [form$1.getField(path).value, form$1.original], useStoreOptions);
|
|
337
|
+
return form.getField(path);
|
|
338
|
+
}
|
|
339
|
+
Form({ original, defaultValue, validations, localizeError, autoSave, transform, validatedClass,...formProps }) {
|
|
340
|
+
const options = {
|
|
341
|
+
defaultValue: {
|
|
342
|
+
...this.options.defaultValue,
|
|
343
|
+
...defaultValue
|
|
344
|
+
},
|
|
345
|
+
validations: {
|
|
346
|
+
...this.options.validations,
|
|
347
|
+
...validations
|
|
348
|
+
},
|
|
349
|
+
localizeError: localizeError ?? this.options.localizeError,
|
|
350
|
+
autoSave: autoSave ?? this.options.autoSave,
|
|
351
|
+
transform: transform ?? this.options.transform,
|
|
352
|
+
validatedClass: validatedClass ?? this.options.validatedClass
|
|
353
|
+
};
|
|
354
|
+
const formState = useMemo(() => {
|
|
355
|
+
return createStore({
|
|
356
|
+
draft: void 0,
|
|
357
|
+
hasTriggeredValidations: false,
|
|
358
|
+
saveScheduled: false,
|
|
359
|
+
saveInProgress: false
|
|
360
|
+
});
|
|
361
|
+
}, []);
|
|
362
|
+
let lastDraft;
|
|
363
|
+
const cache = /* @__PURE__ */ new Map();
|
|
364
|
+
function lazy(key, fn) {
|
|
365
|
+
if (lastDraft !== formState.get().draft) {
|
|
366
|
+
cache.clear();
|
|
367
|
+
lastDraft = formState.get().draft;
|
|
368
|
+
}
|
|
369
|
+
let value = cache.get(key);
|
|
370
|
+
if (!cache.has(key)) {
|
|
371
|
+
value = fn();
|
|
372
|
+
cache.set(key, value);
|
|
373
|
+
}
|
|
374
|
+
return value;
|
|
375
|
+
}
|
|
376
|
+
const context = {
|
|
377
|
+
formState,
|
|
378
|
+
options,
|
|
379
|
+
original,
|
|
380
|
+
getField() {
|
|
381
|
+
throw new Error("Not implemented");
|
|
382
|
+
},
|
|
383
|
+
getDraft() {
|
|
384
|
+
return formState.get().draft ?? original ?? options.defaultValue;
|
|
385
|
+
},
|
|
386
|
+
hasTriggeredValidations() {
|
|
387
|
+
return formState.get().hasTriggeredValidations;
|
|
388
|
+
},
|
|
389
|
+
saveScheduled() {
|
|
390
|
+
return formState.get().saveScheduled;
|
|
391
|
+
},
|
|
392
|
+
saveInProgress() {
|
|
393
|
+
return formState.get().saveInProgress;
|
|
394
|
+
},
|
|
395
|
+
hasChanges() {
|
|
396
|
+
return lazy("hasChanges", () => !deepEqual(this.getDraft(), original ?? options.defaultValue));
|
|
397
|
+
},
|
|
398
|
+
getErrors() {
|
|
399
|
+
return lazy("getErrors", () => getErrors(this.getDraft(), original, options.validations));
|
|
400
|
+
},
|
|
401
|
+
isValid() {
|
|
402
|
+
return lazy("isValid", () => this.getErrors().size === 0);
|
|
403
|
+
},
|
|
404
|
+
validate() {
|
|
405
|
+
formState.set("hasTriggeredValidations", true);
|
|
406
|
+
return this.isValid();
|
|
407
|
+
},
|
|
408
|
+
reset() {
|
|
409
|
+
formState.set("draft", void 0);
|
|
410
|
+
formState.set("hasTriggeredValidations", false);
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
context.getField = (path) => lazy(path, () => getField(context, path));
|
|
414
|
+
useFormAutosave(context);
|
|
415
|
+
return /* @__PURE__ */ jsx(this.context.Provider, {
|
|
416
|
+
value: context,
|
|
417
|
+
children: /* @__PURE__ */ jsx(FormContainer, {
|
|
418
|
+
...formProps,
|
|
419
|
+
form: this
|
|
420
|
+
})
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
FormState({ selector, children }) {
|
|
424
|
+
const selectedState = this.useFormState(selector);
|
|
425
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: children(selectedState) });
|
|
426
|
+
}
|
|
427
|
+
Field(props) {
|
|
428
|
+
return Reflect.apply(FormField, this, [{
|
|
429
|
+
component: "input",
|
|
430
|
+
...props
|
|
431
|
+
}]);
|
|
432
|
+
}
|
|
433
|
+
ForEach(props) {
|
|
434
|
+
return Reflect.apply(FormForEach, this, [props]);
|
|
435
|
+
}
|
|
436
|
+
withForm(Component$1, formProps) {
|
|
437
|
+
const { Form: Form$1 } = this;
|
|
438
|
+
return function FormWrapper(props) {
|
|
439
|
+
return /* @__PURE__ */ jsx(Form$1, {
|
|
440
|
+
...formProps,
|
|
441
|
+
children: /* @__PURE__ */ jsx(Component$1, { ...props })
|
|
442
|
+
});
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
function createForm(options) {
|
|
447
|
+
return new Form(options);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
//#endregion
|
|
451
|
+
//#region src/react/url/urlContext.tsx
|
|
452
|
+
const UrlContext = createContext(void 0);
|
|
453
|
+
function UrlProvider({ children,...props }) {
|
|
454
|
+
const location = "location" in props ? props.location : props.locationHook();
|
|
455
|
+
const navigate = "navigate" in props ? props.navigate : props.navigateHook();
|
|
456
|
+
return /* @__PURE__ */ jsx(UrlContext.Provider, {
|
|
457
|
+
value: {
|
|
458
|
+
location,
|
|
459
|
+
navigate
|
|
460
|
+
},
|
|
461
|
+
children
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
function useUrlContext() {
|
|
465
|
+
const context = useContext(UrlContext);
|
|
466
|
+
if (!context) throw new Error("useUrlContext must be used within a UrlContextProvider");
|
|
467
|
+
return context;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
//#endregion
|
|
471
|
+
//#region src/react/url/urlOptions.ts
|
|
472
|
+
function createUrlOptions(options) {
|
|
473
|
+
return {
|
|
474
|
+
...options,
|
|
475
|
+
defaultValue: options.defaultValue
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
//#endregion
|
|
480
|
+
//#region src/react/url/urlHelpers.ts
|
|
481
|
+
function defaultDeserializer(value) {
|
|
482
|
+
if (value === void 0) return void 0;
|
|
483
|
+
try {
|
|
484
|
+
return fromExtendedJsonString(value);
|
|
485
|
+
} catch {
|
|
486
|
+
return void 0;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
function defaultSerializer(value) {
|
|
490
|
+
return toExtendedJsonString(value);
|
|
491
|
+
}
|
|
492
|
+
function parseLocation(location) {
|
|
493
|
+
if (typeof location !== "string") location = `${location.pathname}${location.search}${location.hash}`;
|
|
494
|
+
return new URL(location, window.location.origin);
|
|
495
|
+
}
|
|
496
|
+
function createStorageKey(id, key) {
|
|
497
|
+
return `cross-state:url:${id}:${key}`;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
//#endregion
|
|
501
|
+
//#region src/react/url/useUrlParam.ts
|
|
502
|
+
function useUrlParam(input) {
|
|
503
|
+
const { key, type = "hash", serialize = defaultSerializer, deserialize = defaultDeserializer, defaultValue, writeDefaultValue, onCommit, persist } = "options" in input ? input.options : input;
|
|
504
|
+
const { location, navigate } = useUrlContext();
|
|
505
|
+
const url = parseLocation(location);
|
|
506
|
+
const params = new URLSearchParams(url[type].slice(1));
|
|
507
|
+
const urlValue = params.get(key);
|
|
508
|
+
const storageKey = persist && createStorageKey(persist.id, key);
|
|
509
|
+
const storageValue = storageKey ? localStorage.getItem(storageKey) : null;
|
|
510
|
+
const value = useMemo(() => urlValue !== null ? deserialize(urlValue) : storageValue !== null ? deserialize(storageValue) : defaultValue, [urlValue]);
|
|
511
|
+
function commit(value$1) {
|
|
512
|
+
const serializedValue = serialize(value$1);
|
|
513
|
+
navigate((location$1) => {
|
|
514
|
+
const url$1 = parseLocation(location$1);
|
|
515
|
+
const params$1 = new URLSearchParams(url$1[type].slice(1));
|
|
516
|
+
if (!writeDefaultValue && serializedValue === serialize(defaultValue)) params$1.delete(key);
|
|
517
|
+
else params$1.set(key, serializedValue);
|
|
518
|
+
url$1[type] = params$1.toString();
|
|
519
|
+
return url$1.toString().replace(window.location.origin, "");
|
|
520
|
+
});
|
|
521
|
+
if (storageKey) localStorage.setItem(storageKey, serializedValue);
|
|
522
|
+
onCommit?.(value$1);
|
|
523
|
+
}
|
|
524
|
+
function update(update$1) {
|
|
525
|
+
if (update$1 instanceof Function) update$1 = update$1(value);
|
|
526
|
+
commit(update$1);
|
|
527
|
+
}
|
|
528
|
+
useEffect(() => {
|
|
529
|
+
if (urlValue !== null) commit(deserialize(urlValue));
|
|
530
|
+
}, [urlValue]);
|
|
531
|
+
useEffect(() => {
|
|
532
|
+
if (urlValue === null && storageValue !== null) commit(deserialize(storageValue));
|
|
533
|
+
else if (urlValue === null && writeDefaultValue) commit(defaultValue);
|
|
534
|
+
}, []);
|
|
535
|
+
return [value, update];
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
//#endregion
|
|
539
|
+
//#region src/react/url/urlStore.ts
|
|
540
|
+
var UrlStore = class {
|
|
541
|
+
constructor(options) {
|
|
542
|
+
this.options = options;
|
|
543
|
+
}
|
|
544
|
+
useStore() {
|
|
545
|
+
return useUrlParam(this)[0];
|
|
546
|
+
}
|
|
547
|
+
useProp() {
|
|
548
|
+
return useUrlParam(this);
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
function createUrlStore(options) {
|
|
552
|
+
return new UrlStore(createUrlOptions(options));
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
//#endregion
|
|
556
|
+
//#region src/react/useDecoupledState.ts
|
|
557
|
+
function useDecoupledState(value, onChange, options = {}) {
|
|
558
|
+
const [dirty, setDirty] = useState();
|
|
559
|
+
const ref = useRef({
|
|
560
|
+
onChange,
|
|
561
|
+
onCommit: options.onCommit
|
|
562
|
+
});
|
|
563
|
+
useEffect(() => {
|
|
564
|
+
ref.current = {
|
|
565
|
+
onChange,
|
|
566
|
+
onCommit: options.onCommit
|
|
567
|
+
};
|
|
568
|
+
}, [onChange]);
|
|
569
|
+
const update = useMemo(() => {
|
|
570
|
+
const { onChange: onChange$1, onCommit } = ref.current;
|
|
571
|
+
const update$1 = (value$1) => {
|
|
572
|
+
onChange$1(value$1);
|
|
573
|
+
setDirty(void 0);
|
|
574
|
+
onCommit?.(value$1);
|
|
575
|
+
};
|
|
576
|
+
let delayedUpdate;
|
|
577
|
+
if (options.debounce) delayedUpdate = debounce(update$1, options.debounce);
|
|
578
|
+
else if (options.throttle) delayedUpdate = throttle(update$1, options.throttle);
|
|
579
|
+
else delayedUpdate = (value$1) => startTransition(() => update$1(value$1));
|
|
580
|
+
return (value$1) => {
|
|
581
|
+
setDirty({ v: value$1 });
|
|
582
|
+
delayedUpdate(value$1);
|
|
583
|
+
};
|
|
584
|
+
}, [simpleHash([options.debounce, options.throttle])]);
|
|
585
|
+
return [dirty ? dirty.v : value, update];
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
//#endregion
|
|
589
|
+
export { CustomInput, Form, LoadingBoundary, ScopeProvider, UrlContext, UrlProvider, cacheMethods, createForm, createUrlOptions, createUrlStore, scopeMethods, storeMethods, useCache, useDecoupledState, useLoadingBoundary, useProp, useScope, useStore, useUrlContext, useUrlParam };
|
|
590
|
+
//# sourceMappingURL=index.js.map
|