@void/react 1.6.1 → 1.7.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/index.d.mts +14 -5
- package/dist/index.mjs +226 -135
- package/dist/plugin.mjs +18 -13
- package/dist/runtime/pages-client.d.mts +1 -0
- package/dist/runtime/pages-client.mjs +1 -1
- package/package.json +6 -6
package/dist/index.d.mts
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
import { AnchorHTMLAttributes, ButtonHTMLAttributes, Context, FormHTMLAttributes, JSX, ReactElement, ReactNode } from "react";
|
|
2
|
-
import { ActionClient, BaseLinkProps, FormMethod, FormModel, FormState, IslandFormState, NavigationState, VoidActionError, VoidRouter } from "void/pages-client";
|
|
2
|
+
import { ActionClient, BaseLinkProps, FormMethod, FormModel, FormState, IslandFormState, NavigationState, VoidActionError, VoidActionError as VoidActionError$1, VoidRouter, isCallSiteActionError, isVoidActionError } from "void/pages-client";
|
|
3
|
+
import { ActionFormOptions, ActionUrl, PageParams, PagePath, ResolveActionBody } from "void/routes";
|
|
3
4
|
import { CloudContextVariables, InferProps } from "void";
|
|
4
|
-
import { ActionFormOptions, ActionUrl, ResolveActionBody } from "void/routes";
|
|
5
5
|
type FormProps = Omit<FormHTMLAttributes<HTMLFormElement>, "action" | "onSubmit" | "method" | "onError"> & {
|
|
6
6
|
form: FormModel<[FormData]>;
|
|
7
7
|
method?: FormMethod;
|
|
8
|
-
renderError?: (error: VoidActionError) => ReactNode;
|
|
8
|
+
renderError?: (error: VoidActionError$1) => ReactNode;
|
|
9
9
|
};
|
|
10
10
|
export declare function Form({ form, method, renderError, children, ...props }: FormProps): ReactElement;
|
|
11
11
|
type LinkProps = BaseLinkProps & Omit<AnchorHTMLAttributes<HTMLAnchorElement>, keyof BaseLinkProps> & Omit<ButtonHTMLAttributes<HTMLButtonElement>, keyof BaseLinkProps>;
|
|
12
12
|
export declare function Link({ href, method, data, preserveScroll, preserveState, replace, reloadDocument, viewTransition, prefetch, cacheFor, activeMatch, onNavigate, children, style, onClick: onClickProp, onMouseEnter: onMouseEnterProp, onMouseLeave: onMouseLeaveProp, onFocus: onFocusProp, onMouseDown: onMouseDownProp, onTouchStart: onTouchStartProp, ...rest }: LinkProps): JSX.Element;
|
|
13
13
|
export declare function useRouter(): VoidRouter;
|
|
14
|
+
/**
|
|
15
|
+
* Read the dynamic route params of the page being rendered.
|
|
16
|
+
*
|
|
17
|
+
* Pass the page's URL pattern as the type argument to type the result:
|
|
18
|
+
* `useParams<'/users/:id'>()` returns `{ id: string }`. This is a type-only
|
|
19
|
+
* check; the params are returned as they are.
|
|
20
|
+
*/
|
|
14
21
|
export declare function useParams<T extends Record<string, string> = Record<string, string>>(): T;
|
|
22
|
+
export declare function useParams<P extends PagePath>(): PageParams<P>;
|
|
23
|
+
export declare function useParams(): Record<string, string>;
|
|
15
24
|
export declare function useShared<T = CloudContextVariables["shared"]>(): T;
|
|
16
25
|
type FormReturn<TData> = FormState<TData, "setters"> & Readonly<{
|
|
17
26
|
setData: <K extends keyof TData & string>(field: K, value: TData[K]) => void;
|
|
@@ -25,7 +34,7 @@ type FormOptions = {
|
|
|
25
34
|
resetOnSuccess?: boolean;
|
|
26
35
|
preserveScroll?: boolean;
|
|
27
36
|
onSuccess?: () => void | Promise<void>;
|
|
28
|
-
onError?: (error: Record<string, string> | VoidActionError) => void | Promise<void>;
|
|
37
|
+
onError?: (error: Record<string, string> | VoidActionError$1) => void | Promise<void>;
|
|
29
38
|
};
|
|
30
39
|
export declare function useForm<U extends ActionUrl & string>(url: U, defaults: ResolveActionBody<U>, options?: ActionFormOptions<U> & Pick<FormOptions, "onSuccess" | "onError" | "preserveScroll">): FormReturn<ResolveActionBody<U>>;
|
|
31
40
|
export declare function useNavigation(): NavigationState;
|
|
@@ -46,4 +55,4 @@ export declare const RouterContext: Context<VoidRouter | null>;
|
|
|
46
55
|
export declare const NavigationContext: Context<NavigationState>;
|
|
47
56
|
export declare const FrontmatterContext: Context<Record<string, unknown>>;
|
|
48
57
|
export type Deferred<T> = Promise<T>;
|
|
49
|
-
export type
|
|
58
|
+
export { type InferProps, type VoidActionError, isCallSiteActionError, isVoidActionError };
|
package/dist/index.mjs
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
import { a as RouterContext, i as ParamsContext, n as FrontmatterContext, o as SharedContext, r as NavigationContext, t as ErrorsContext } from "./context-BQpawTZc.mjs";
|
|
2
2
|
import { n as setActionRouter, t as action } from "./action-CyCVdW12.mjs";
|
|
3
3
|
import { useActionState, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
-
import { cloneFormValue, formErrorMessage, formRetryMessage, formSubmissionBlocked, getValidationErrors, handleLinkClick, interpolateRouteParams, isAbortError, isEqualFormValue, linkActiveAttributes, linkPendingAttributes, linkPrefetchStrategies, observeLinkVisible, parseFormResetArgs, prefetchLink, resolveLink,
|
|
4
|
+
import { cloneFormValue, formErrorMessage, formRetryMessage, formSubmissionBlocked, getValidationErrors, handleLinkClick, interpolateRouteParams, isAbortError, isCallSiteActionError, isEqualFormValue, isVoidActionError, linkActiveAttributes, linkPendingAttributes, linkPrefetchStrategies, observeLinkVisible, parseFormResetArgs, prefetchLink, resolveLink, ssrProxy, submitAction, submitIslandAction } from "void/pages-client";
|
|
5
5
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
6
|
// src/runtime/form.tsx
|
|
7
7
|
function Form({ form, method = "post", renderError, children, ...props }) {
|
|
8
|
-
const [now, setNow] = useState(Date.now);
|
|
9
|
-
useEffect(() => scheduleFormRetry(form.error, () => setNow(Date.now())), [form.error]);
|
|
10
8
|
const message = formErrorMessage(form);
|
|
11
9
|
const retry = formRetryMessage(form.error);
|
|
12
|
-
const
|
|
10
|
+
const onSubmit = useCallback((event) => {
|
|
11
|
+
if (formSubmissionBlocked(form)) event.preventDefault();
|
|
12
|
+
}, [form]);
|
|
13
13
|
return /* @__PURE__ */ jsxs("form", {
|
|
14
14
|
...props,
|
|
15
15
|
action: form[method],
|
|
16
16
|
"aria-busy": form.pending,
|
|
17
|
-
onSubmit
|
|
18
|
-
if (blocked) event.preventDefault();
|
|
19
|
-
},
|
|
17
|
+
onSubmit,
|
|
20
18
|
children: [message && /* @__PURE__ */ jsxs("div", {
|
|
21
19
|
role: "alert",
|
|
22
20
|
"data-void-form-error": true,
|
|
@@ -209,37 +207,105 @@ function useShared() {
|
|
|
209
207
|
return shared;
|
|
210
208
|
}
|
|
211
209
|
// src/runtime/use-form.ts
|
|
210
|
+
const EMPTY_ERRORS$1 = {};
|
|
211
|
+
function resolveOutcomeErrors(outcome, edits) {
|
|
212
|
+
if (edits?.source === outcome && edits.errors !== void 0) return edits.errors;
|
|
213
|
+
return outcome.status === "validation" ? outcome.errors : EMPTY_ERRORS$1;
|
|
214
|
+
}
|
|
212
215
|
function useForm(url, defaults, options) {
|
|
213
216
|
const router = useContext(RouterContext);
|
|
214
217
|
const [data, setDataState] = useState(() => cloneFormValue(defaults));
|
|
215
|
-
const [
|
|
216
|
-
const [
|
|
217
|
-
const [
|
|
218
|
-
const [wasSuccessful, setWasSuccessful] = useState(false);
|
|
219
|
-
const [recentlySuccessful, setRecentlySuccessful] = useState(false);
|
|
220
|
-
const defaultsRef = useRef(defaultsState);
|
|
221
|
-
const initialRef = useRef(defaultsState);
|
|
218
|
+
const [initialDefaults] = useState(() => cloneFormValue(defaults));
|
|
219
|
+
const [errorEdits, setErrorEdits] = useState(null);
|
|
220
|
+
const [expiredOutcome, setExpiredOutcome] = useState(null);
|
|
222
221
|
const dataRef = useRef(data);
|
|
223
|
-
const
|
|
222
|
+
const callbackOutcomeRef = useRef(null);
|
|
223
|
+
const runErrorCallback = async (nextOutcome, errorArg) => {
|
|
224
|
+
callbackOutcomeRef.current = nextOutcome;
|
|
225
|
+
try {
|
|
226
|
+
await options?.onError?.(errorArg);
|
|
227
|
+
} finally {
|
|
228
|
+
if (callbackOutcomeRef.current === nextOutcome) callbackOutcomeRef.current = null;
|
|
229
|
+
}
|
|
230
|
+
return nextOutcome;
|
|
231
|
+
};
|
|
232
|
+
const [outcome, dispatchAction, pending] = useActionState(async (previousOutcome, payload) => {
|
|
233
|
+
if (!router) throw new Error("useForm(): requires the Void Router.");
|
|
234
|
+
let result;
|
|
235
|
+
try {
|
|
236
|
+
result = await submitAction(router, interpolateRouteParams(url, options?.params), {
|
|
237
|
+
method: payload.method,
|
|
238
|
+
data: payload.snapshot,
|
|
239
|
+
preserveScroll: options?.preserveScroll,
|
|
240
|
+
_resolveOnShell: true
|
|
241
|
+
});
|
|
242
|
+
} catch (submitError) {
|
|
243
|
+
if (isAbortError(submitError)) return {
|
|
244
|
+
status: "idle",
|
|
245
|
+
defaults: previousOutcome.defaults
|
|
246
|
+
};
|
|
247
|
+
throw submitError;
|
|
248
|
+
}
|
|
249
|
+
if (!result.ok) {
|
|
250
|
+
const validationErrors = getValidationErrors(result.error.body);
|
|
251
|
+
if (validationErrors) return runErrorCallback({
|
|
252
|
+
status: "validation",
|
|
253
|
+
defaults: previousOutcome.defaults,
|
|
254
|
+
errors: validationErrors
|
|
255
|
+
}, validationErrors);
|
|
256
|
+
return runErrorCallback({
|
|
257
|
+
status: "error",
|
|
258
|
+
defaults: previousOutcome.defaults,
|
|
259
|
+
error: result.error
|
|
260
|
+
}, result.error);
|
|
261
|
+
}
|
|
262
|
+
const nextOutcome = {
|
|
263
|
+
status: "success",
|
|
264
|
+
defaults: options?.resetOnSuccess ? cloneFormValue(initialDefaults) : cloneFormValue(payload.snapshot)
|
|
265
|
+
};
|
|
266
|
+
callbackOutcomeRef.current = nextOutcome;
|
|
267
|
+
try {
|
|
268
|
+
await options?.onSuccess?.();
|
|
269
|
+
} finally {
|
|
270
|
+
if (callbackOutcomeRef.current === nextOutcome) callbackOutcomeRef.current = null;
|
|
271
|
+
}
|
|
272
|
+
if (options?.resetOnSuccess && dataRef.current === payload.dispatchData) {
|
|
273
|
+
dataRef.current = cloneFormValue(initialDefaults);
|
|
274
|
+
setDataState(dataRef.current);
|
|
275
|
+
}
|
|
276
|
+
return nextOutcome;
|
|
277
|
+
}, {
|
|
278
|
+
status: "idle",
|
|
279
|
+
defaults: initialDefaults
|
|
280
|
+
});
|
|
281
|
+
const defaultsState = outcome.defaults;
|
|
224
282
|
const hasChanges = useMemo(() => {
|
|
225
283
|
return Object.keys(defaultsState).some((key) => !isEqualFormValue(data[key], defaultsState[key]));
|
|
226
284
|
}, [data, defaultsState]);
|
|
227
285
|
const setData = useCallback((field, value) => {
|
|
286
|
+
const previousValue = dataRef.current[field];
|
|
228
287
|
dataRef.current = {
|
|
229
288
|
...dataRef.current,
|
|
230
289
|
[field]: value
|
|
231
290
|
};
|
|
232
291
|
setDataState(dataRef.current);
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
292
|
+
if (isEqualFormValue(previousValue, value)) return;
|
|
293
|
+
const target = callbackOutcomeRef.current ?? outcome;
|
|
294
|
+
setErrorEdits((prev) => {
|
|
295
|
+
const current = resolveOutcomeErrors(target, prev);
|
|
296
|
+
if (!(field in current)) return prev;
|
|
297
|
+
const next = { ...current };
|
|
236
298
|
delete next[field];
|
|
237
|
-
return
|
|
299
|
+
return {
|
|
300
|
+
...prev?.source === target ? prev : { source: target },
|
|
301
|
+
errors: next
|
|
302
|
+
};
|
|
238
303
|
});
|
|
239
|
-
}, []);
|
|
304
|
+
}, [outcome]);
|
|
240
305
|
const reset = useCallback((...args) => {
|
|
241
306
|
const { initial, fields } = parseFormResetArgs(args);
|
|
242
|
-
const
|
|
307
|
+
const target = callbackOutcomeRef.current ?? outcome;
|
|
308
|
+
const source = initial ? initialDefaults : target.defaults;
|
|
243
309
|
if (fields.length === 0) dataRef.current = cloneFormValue(source);
|
|
244
310
|
else {
|
|
245
311
|
const next = { ...dataRef.current };
|
|
@@ -247,84 +313,57 @@ function useForm(url, defaults, options) {
|
|
|
247
313
|
dataRef.current = next;
|
|
248
314
|
}
|
|
249
315
|
setDataState(dataRef.current);
|
|
250
|
-
}, []);
|
|
316
|
+
}, [initialDefaults, outcome]);
|
|
251
317
|
const clearErrors = useCallback((...fields) => {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const
|
|
318
|
+
const target = callbackOutcomeRef.current ?? outcome;
|
|
319
|
+
setErrorEdits((prev) => {
|
|
320
|
+
const base = prev?.source === target ? prev : { source: target };
|
|
321
|
+
if (fields.length === 0) return {
|
|
322
|
+
...base,
|
|
323
|
+
errors: EMPTY_ERRORS$1
|
|
324
|
+
};
|
|
325
|
+
const next = { ...resolveOutcomeErrors(target, prev) };
|
|
255
326
|
for (const field of fields) delete next[field];
|
|
256
|
-
return
|
|
327
|
+
return {
|
|
328
|
+
...base,
|
|
329
|
+
errors: next
|
|
330
|
+
};
|
|
257
331
|
});
|
|
258
|
-
}, []);
|
|
332
|
+
}, [outcome]);
|
|
259
333
|
const setError = useCallback((field, message) => {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
334
|
+
const target = callbackOutcomeRef.current ?? outcome;
|
|
335
|
+
setErrorEdits((prev) => ({
|
|
336
|
+
...prev?.source === target ? prev : { source: target },
|
|
337
|
+
errors: {
|
|
338
|
+
...resolveOutcomeErrors(target, prev),
|
|
339
|
+
[field]: message
|
|
340
|
+
}
|
|
263
341
|
}));
|
|
264
|
-
}, []);
|
|
342
|
+
}, [outcome]);
|
|
265
343
|
const clearError = useCallback(() => {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
}
|
|
277
|
-
setErrors({});
|
|
278
|
-
setActionError(null);
|
|
279
|
-
let result;
|
|
280
|
-
try {
|
|
281
|
-
result = await submitAction(router, interpolateRouteParams(url, options?.params), {
|
|
282
|
-
method,
|
|
283
|
-
data: { ...dataRef.current },
|
|
284
|
-
preserveScroll: options?.preserveScroll,
|
|
285
|
-
_resolveOnShell: true
|
|
286
|
-
});
|
|
287
|
-
} catch (submitError) {
|
|
288
|
-
if (isAbortError(submitError)) return previousSuccessVersion;
|
|
289
|
-
throw submitError;
|
|
290
|
-
}
|
|
291
|
-
if (!result.ok) {
|
|
292
|
-
const validationErrors = getValidationErrors(result.error.body);
|
|
293
|
-
if (validationErrors) {
|
|
294
|
-
setErrors(validationErrors);
|
|
295
|
-
await options?.onError?.(validationErrors);
|
|
296
|
-
} else {
|
|
297
|
-
setActionError(result.error);
|
|
298
|
-
await options?.onError?.(result.error);
|
|
299
|
-
}
|
|
300
|
-
return previousSuccessVersion;
|
|
301
|
-
}
|
|
302
|
-
if (options?.resetOnSuccess) {
|
|
303
|
-
dataRef.current = cloneFormValue(initialRef.current);
|
|
304
|
-
setDataState(dataRef.current);
|
|
305
|
-
}
|
|
306
|
-
const nextDefaults = cloneFormValue(dataRef.current);
|
|
307
|
-
defaultsRef.current = nextDefaults;
|
|
308
|
-
setDefaultsState(nextDefaults);
|
|
309
|
-
setWasSuccessful(true);
|
|
310
|
-
setRecentlySuccessful(true);
|
|
311
|
-
successTimeoutRef.current = setTimeout(() => {
|
|
312
|
-
setRecentlySuccessful(false);
|
|
313
|
-
successTimeoutRef.current = null;
|
|
314
|
-
}, 2e3);
|
|
315
|
-
await options?.onSuccess?.();
|
|
316
|
-
return previousSuccessVersion + 1;
|
|
317
|
-
}, 0);
|
|
344
|
+
const target = callbackOutcomeRef.current ?? outcome;
|
|
345
|
+
setErrorEdits((prev) => ({
|
|
346
|
+
...prev?.source === target ? prev : { source: target },
|
|
347
|
+
dismissActionError: true
|
|
348
|
+
}));
|
|
349
|
+
}, [outcome]);
|
|
350
|
+
const errors = pending ? EMPTY_ERRORS$1 : resolveOutcomeErrors(outcome, errorEdits);
|
|
351
|
+
const error = pending || outcome.status !== "error" || errorEdits?.source === outcome && errorEdits.dismissActionError ? null : outcome.error;
|
|
352
|
+
const wasSuccessful = !pending && outcome.status === "success";
|
|
353
|
+
const recentlySuccessful = wasSuccessful && expiredOutcome !== outcome;
|
|
318
354
|
useEffect(() => {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
}, []);
|
|
355
|
+
if (outcome.status !== "success") return;
|
|
356
|
+
const timer = setTimeout(() => {
|
|
357
|
+
setExpiredOutcome(outcome);
|
|
358
|
+
}, 2e3);
|
|
359
|
+
return () => clearTimeout(timer);
|
|
360
|
+
}, [outcome]);
|
|
326
361
|
const dispatchMethodAction = useCallback((method) => (_formData) => {
|
|
327
|
-
dispatchAction({
|
|
362
|
+
dispatchAction({
|
|
363
|
+
method,
|
|
364
|
+
snapshot: cloneFormValue(dataRef.current),
|
|
365
|
+
dispatchData: dataRef.current
|
|
366
|
+
});
|
|
328
367
|
}, [dispatchAction]);
|
|
329
368
|
return {
|
|
330
369
|
data,
|
|
@@ -335,7 +374,7 @@ function useForm(url, defaults, options) {
|
|
|
335
374
|
hasChanges,
|
|
336
375
|
wasSuccessful,
|
|
337
376
|
recentlySuccessful,
|
|
338
|
-
post:
|
|
377
|
+
post: useMemo(() => dispatchMethodAction("POST"), [dispatchMethodAction]),
|
|
339
378
|
put: useMemo(() => dispatchMethodAction("PUT"), [dispatchMethodAction]),
|
|
340
379
|
patch: useMemo(() => dispatchMethodAction("PATCH"), [dispatchMethodAction]),
|
|
341
380
|
delete: useMemo(() => dispatchMethodAction("DELETE"), [dispatchMethodAction]),
|
|
@@ -350,75 +389,127 @@ function useNavigation() {
|
|
|
350
389
|
return useContext(NavigationContext);
|
|
351
390
|
}
|
|
352
391
|
// src/runtime/use-island-form.ts
|
|
392
|
+
const EMPTY_ERRORS = {};
|
|
353
393
|
/**
|
|
354
394
|
* Form hook for island pages. Uses fetch + page reload instead of the
|
|
355
395
|
* Void Router (which is not available in island mode).
|
|
356
396
|
*/
|
|
357
397
|
function useIslandForm(defaults) {
|
|
358
|
-
const [data, setDataState] = useState(() => (
|
|
359
|
-
const [
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
const [
|
|
364
|
-
const
|
|
365
|
-
const
|
|
366
|
-
const
|
|
398
|
+
const [data, setDataState] = useState(() => cloneFormValue(defaults));
|
|
399
|
+
const [outcome, setOutcome] = useState(() => ({
|
|
400
|
+
status: "idle",
|
|
401
|
+
defaults: cloneFormValue(defaults)
|
|
402
|
+
}));
|
|
403
|
+
const [expiredOutcome, setExpiredOutcome] = useState(null);
|
|
404
|
+
const dataRef = useRef(data);
|
|
405
|
+
const latestRequestIdRef = useRef(0);
|
|
406
|
+
const defaultsState = outcome.defaults;
|
|
407
|
+
const pending = outcome.status === "pending";
|
|
408
|
+
const errors = outcome.status === "validation" ? outcome.errors : EMPTY_ERRORS;
|
|
409
|
+
const error = outcome.status === "error" ? outcome.error : null;
|
|
410
|
+
const wasSuccessful = outcome.status === "success";
|
|
411
|
+
const recentlySuccessful = wasSuccessful && expiredOutcome !== outcome;
|
|
367
412
|
const hasChanges = useMemo(() => {
|
|
368
413
|
return Object.keys(defaultsState).some((key) => !isEqualFormValue(data[key], defaultsState[key]));
|
|
369
414
|
}, [data, defaultsState]);
|
|
415
|
+
useEffect(() => {
|
|
416
|
+
if (outcome.status !== "success") return;
|
|
417
|
+
const timer = setTimeout(() => setExpiredOutcome(outcome), 2e3);
|
|
418
|
+
return () => clearTimeout(timer);
|
|
419
|
+
}, [outcome]);
|
|
370
420
|
const setData = useCallback((field, value) => {
|
|
371
|
-
|
|
372
|
-
|
|
421
|
+
const previousValue = dataRef.current[field];
|
|
422
|
+
dataRef.current = {
|
|
423
|
+
...dataRef.current,
|
|
373
424
|
[field]: value
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
const next = { ...prev };
|
|
380
|
-
|
|
381
|
-
return
|
|
425
|
+
};
|
|
426
|
+
setDataState(dataRef.current);
|
|
427
|
+
if (isEqualFormValue(previousValue, value)) return;
|
|
428
|
+
setOutcome((prev) => {
|
|
429
|
+
if (prev.status !== "validation" || !(field in prev.errors)) return prev;
|
|
430
|
+
const next = { ...prev.errors };
|
|
431
|
+
delete next[field];
|
|
432
|
+
return {
|
|
433
|
+
status: "validation",
|
|
434
|
+
defaults: prev.defaults,
|
|
435
|
+
errors: next
|
|
436
|
+
};
|
|
382
437
|
});
|
|
383
438
|
}, []);
|
|
439
|
+
const reset = useCallback((...fields) => {
|
|
440
|
+
if (fields.length === 0) dataRef.current = cloneFormValue(defaultsState);
|
|
441
|
+
else {
|
|
442
|
+
const next = { ...dataRef.current };
|
|
443
|
+
for (const field of fields) next[field] = cloneFormValue(defaultsState[field]);
|
|
444
|
+
dataRef.current = next;
|
|
445
|
+
}
|
|
446
|
+
setDataState(dataRef.current);
|
|
447
|
+
}, [defaultsState]);
|
|
384
448
|
const clearErrors = useCallback((...fields) => {
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
449
|
+
setOutcome((prev) => {
|
|
450
|
+
if (prev.status !== "validation") return prev;
|
|
451
|
+
if (fields.length === 0) return {
|
|
452
|
+
status: "idle",
|
|
453
|
+
defaults: prev.defaults
|
|
454
|
+
};
|
|
455
|
+
const next = { ...prev.errors };
|
|
388
456
|
for (const field of fields) delete next[field];
|
|
389
|
-
return
|
|
457
|
+
return {
|
|
458
|
+
status: "validation",
|
|
459
|
+
defaults: prev.defaults,
|
|
460
|
+
errors: next
|
|
461
|
+
};
|
|
390
462
|
});
|
|
391
463
|
}, []);
|
|
392
464
|
const clearError = useCallback(() => {
|
|
393
|
-
|
|
465
|
+
setOutcome((prev) => prev.status === "error" ? {
|
|
466
|
+
status: "idle",
|
|
467
|
+
defaults: prev.defaults
|
|
468
|
+
} : prev);
|
|
394
469
|
}, []);
|
|
395
470
|
const submit = useCallback(async (url, method) => {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
471
|
+
const requestId = ++latestRequestIdRef.current;
|
|
472
|
+
const submittedData = cloneFormValue(dataRef.current);
|
|
473
|
+
setOutcome((prev) => ({
|
|
474
|
+
status: "pending",
|
|
475
|
+
defaults: prev.defaults,
|
|
476
|
+
requestId
|
|
477
|
+
}));
|
|
402
478
|
try {
|
|
403
|
-
const result = await submitIslandAction(url, method,
|
|
479
|
+
const result = await submitIslandAction(url, method, submittedData);
|
|
480
|
+
if (requestId !== latestRequestIdRef.current) return;
|
|
404
481
|
if (result.kind === "redirect") {
|
|
482
|
+
setOutcome((prev) => ({
|
|
483
|
+
status: "idle",
|
|
484
|
+
defaults: prev.defaults
|
|
485
|
+
}));
|
|
405
486
|
window.location.href = result.url;
|
|
406
487
|
return;
|
|
407
488
|
}
|
|
408
489
|
if (result.kind === "success") {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
setDefaultsState(nextDefaults);
|
|
414
|
-
successTimeoutRef.current = setTimeout(() => setRecentlySuccessful(false), 2e3);
|
|
490
|
+
setOutcome({
|
|
491
|
+
status: "success",
|
|
492
|
+
defaults: submittedData
|
|
493
|
+
});
|
|
415
494
|
window.location.reload();
|
|
416
|
-
} else if (result.kind === "validation")
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
495
|
+
} else if (result.kind === "validation") setOutcome((prev) => ({
|
|
496
|
+
status: "validation",
|
|
497
|
+
defaults: prev.defaults,
|
|
498
|
+
errors: result.errors
|
|
499
|
+
}));
|
|
500
|
+
else setOutcome((prev) => ({
|
|
501
|
+
status: "error",
|
|
502
|
+
defaults: prev.defaults,
|
|
503
|
+
error: result.error
|
|
504
|
+
}));
|
|
505
|
+
} catch (submitError) {
|
|
506
|
+
if (requestId === latestRequestIdRef.current) setOutcome((prev) => prev.status === "pending" && prev.requestId === requestId ? {
|
|
507
|
+
status: "idle",
|
|
508
|
+
defaults: prev.defaults
|
|
509
|
+
} : prev);
|
|
510
|
+
throw submitError;
|
|
420
511
|
}
|
|
421
|
-
}, [
|
|
512
|
+
}, []);
|
|
422
513
|
return {
|
|
423
514
|
data,
|
|
424
515
|
setData,
|
|
@@ -437,4 +528,4 @@ function useIslandForm(defaults) {
|
|
|
437
528
|
delete: (url) => submit(url, "DELETE")
|
|
438
529
|
};
|
|
439
530
|
}
|
|
440
|
-
export { ErrorsContext, Form, FrontmatterContext, Link, NavigationContext, ParamsContext, RouterContext, SharedContext, action, setActionRouter, useForm, useIslandForm, useNavigation, useParams, useRouter, useShared };
|
|
531
|
+
export { ErrorsContext, Form, FrontmatterContext, Link, NavigationContext, ParamsContext, RouterContext, SharedContext, action, isCallSiteActionError, isVoidActionError, setActionRouter, useForm, useIslandForm, useNavigation, useParams, useRouter, useShared };
|
package/dist/plugin.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import { createPagesPlugin, generateComponentManifest, resolveEffectiveLayoutCha
|
|
|
3
3
|
import { existsSync, readFileSync } from "node:fs";
|
|
4
4
|
import nodePath from "node:path";
|
|
5
5
|
import { ISLANDS_CLIENT_ENTRY_ID, ISLANDS_RENDERER_ID, computePagesNeedingClientJS, createIslandManifest, editIslandSource, findIslandImports, generateIslandClientEntry, islandCssFiles, islandStylesPlugin, islandsBuildConfig, mdClientScriptIds, recordIslandStyles, recordedIslandStyles, resolveSpecifier, watchPagesForRescan } from "void/pages-islands-plugin";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
6
7
|
// ../../node_modules/.pnpm/pathslash@0.1.0/node_modules/pathslash/dist/index.mjs
|
|
7
8
|
const BACKSLASH_RE = /\\/g;
|
|
8
9
|
const isWindows = process.platform === "win32";
|
|
@@ -52,11 +53,8 @@ const REACT_OPTIMIZE_DEPS = [
|
|
|
52
53
|
"react-dom"
|
|
53
54
|
];
|
|
54
55
|
const CLIENT_OPTIMIZE_DEPS = [...REACT_OPTIMIZE_DEPS, "react-dom/client"];
|
|
55
|
-
const WORKER_OPTIMIZE_DEPS = [
|
|
56
|
-
|
|
57
|
-
"react-dom/server",
|
|
58
|
-
"void/live/client"
|
|
59
|
-
];
|
|
56
|
+
const WORKER_OPTIMIZE_DEPS = [...REACT_OPTIMIZE_DEPS, "react-dom/server"];
|
|
57
|
+
const NODE_MODULES_SEGMENT = /[\\/]node_modules[\\/]/;
|
|
60
58
|
const REACT_REFRESH_PREAMBLE = `
|
|
61
59
|
<script type="module">
|
|
62
60
|
import RefreshRuntime from "/@react-refresh"
|
|
@@ -83,17 +81,13 @@ function pagesPlugin(pagesDir, options) {
|
|
|
83
81
|
optimizeDeps: {
|
|
84
82
|
exclude: ["@void/react"],
|
|
85
83
|
entries: clientOptimizeEntries(await scan(), pagesDir, root),
|
|
86
|
-
include: [
|
|
87
|
-
...CLIENT_OPTIMIZE_DEPS,
|
|
88
|
-
"void/pages-client",
|
|
89
|
-
"void/pages-prefetch"
|
|
90
|
-
]
|
|
84
|
+
include: [...CLIENT_OPTIMIZE_DEPS, ...installedDependencies(["void/pages-client", "void/pages-prefetch"])]
|
|
91
85
|
},
|
|
92
86
|
ssr: { optimizeDeps: { include: [...CLIENT_OPTIMIZE_DEPS, "react-dom/server"] } },
|
|
93
87
|
environments: { [WORKER_ENVIRONMENT_NAME]: { optimizeDeps: {
|
|
94
88
|
exclude: ["@void/react"],
|
|
95
89
|
entries: [pagesOptimizeEntries],
|
|
96
|
-
include: WORKER_OPTIMIZE_DEPS
|
|
90
|
+
include: [...WORKER_OPTIMIZE_DEPS, ...installedDependencies(["void/live/client"])]
|
|
97
91
|
} } }
|
|
98
92
|
};
|
|
99
93
|
if (!(await scan())?.pages.some((page) => !page.island)) return;
|
|
@@ -107,6 +101,17 @@ function pagesPlugin(pagesDir, options) {
|
|
|
107
101
|
}
|
|
108
102
|
});
|
|
109
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Keeps the specifiers that resolve into `node_modules`. Vite serves a linked
|
|
106
|
+
* package, such as `void` in a workspace, as source unless
|
|
107
|
+
* `optimizeDeps.include` lists it. A listed linked package is pre-bundled into
|
|
108
|
+
* a cache keyed by the lockfile and config rather than the package's files, so
|
|
109
|
+
* dev would keep serving that copy after the package is rebuilt. A client built
|
|
110
|
+
* against the new exports then fails to load and never hydrates.
|
|
111
|
+
*/
|
|
112
|
+
function installedDependencies(specifiers, resolveFile = (specifier) => fileURLToPath(import.meta.resolve(specifier))) {
|
|
113
|
+
return specifiers.filter((specifier) => NODE_MODULES_SEGMENT.test(resolveFile(specifier)));
|
|
114
|
+
}
|
|
110
115
|
function clientOptimizeEntries(scan, pagesDir, root) {
|
|
111
116
|
if (!scan) return [];
|
|
112
117
|
const browserFiles = /* @__PURE__ */ new Set();
|
|
@@ -285,10 +290,10 @@ function islandsPlugin(pagesDir, options) {
|
|
|
285
290
|
end: islandImport.end,
|
|
286
291
|
text: replacement
|
|
287
292
|
});
|
|
288
|
-
wrappers.push(`var ${name} = function ${name}(props) { return _React_island.createElement("div", { "data-island": ${JSON.stringify(islandId)}, "data-props":
|
|
293
|
+
wrappers.push(`var ${name} = function ${name}(props) { return _React_island.createElement("div", { "data-island": ${JSON.stringify(islandId)}, "data-props": _serializeIslandProps(props), "data-hydrate": ${JSON.stringify(strategy)} }, _React_island.createElement(${rawName}, props)); };`);
|
|
289
294
|
}
|
|
290
295
|
if (wrappers.length > 0) return editIslandSource(code, edits, {
|
|
291
|
-
prepend: "import _React_island from \"react\";\n",
|
|
296
|
+
prepend: "import _React_island from \"react\";\nimport { serializePageData as _serializeIslandProps } from \"void/pages-server\";\n",
|
|
292
297
|
append: "\n" + wrappers.join("\n")
|
|
293
298
|
});
|
|
294
299
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as RouterContext, i as ParamsContext, o as SharedContext, r as NavigationContext, t as ErrorsContext } from "../context-BQpawTZc.mjs";
|
|
2
1
|
import { n as setActionRouter } from "../action-CyCVdW12.mjs";
|
|
2
|
+
import { a as RouterContext, i as ParamsContext, o as SharedContext, r as NavigationContext, t as ErrorsContext } from "../context-BQpawTZc.mjs";
|
|
3
3
|
import { n as DEFERRED_SHELL_END_ID } from "../deferred-shell-D5T32zXz.mjs";
|
|
4
4
|
import React, { Suspense, startTransition, use, useCallback, useEffect, useInsertionEffect, useLayoutEffect, useMemo, useReducer, useState, useTransition } from "react";
|
|
5
5
|
import { createPageNavigationEngine, createRouterFacade, idleNavigationState, isAbortError, isCallSiteActionError, refreshPage, toUrlString } from "void/pages-client";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@void/react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "React pages, server rendering, forms, and navigation for Void.",
|
|
5
5
|
"homepage": "https://void.cloud",
|
|
6
6
|
"bugs": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@vitejs/plugin-react": "^6.1
|
|
41
|
+
"@vitejs/plugin-react": "^6.2.1"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@types/node": "^26.6.6",
|
|
@@ -48,15 +48,15 @@
|
|
|
48
48
|
"pathslash": "^0.1.0",
|
|
49
49
|
"react": "^19.3.1",
|
|
50
50
|
"react-dom": "^19.3.1",
|
|
51
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@1.
|
|
52
|
-
"vite-plus": "1.
|
|
53
|
-
"void": "1.
|
|
51
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.5.0",
|
|
52
|
+
"vite-plus": "1.5.0",
|
|
53
|
+
"void": "1.7.0"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
56
|
"react": "^19.0.0",
|
|
57
57
|
"react-dom": "^19.0.0",
|
|
58
58
|
"vite": "^8.0.0",
|
|
59
|
-
"void": "1.
|
|
59
|
+
"void": "1.7.0"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "vp pack",
|