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