@void/solid 0.22.0 → 0.23.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/README.md CHANGED
@@ -37,7 +37,7 @@ Main runtime APIs live at `@void/solid`:
37
37
  - `useParams()`
38
38
  - `useNavigation()`
39
39
  - `useShared()`
40
- - `useForm()`
40
+ - `Form` and `useForm()`
41
41
  - `useIslandForm()`
42
42
  - `action()`
43
43
 
@@ -113,6 +113,12 @@ Export `ssr = false` from a page's `.server.ts` file to skip server-rendered
113
113
  component HTML for that route while still running its loader on the first
114
114
  request.
115
115
 
116
+ ## Form
117
+
118
+ Use `Form` with `useForm()` to display action and validation errors automatically. It preserves controlled input after a failed submission and announces errors accessibly. Structured usage-limit responses display the application's message and an optional retry time. Submissions are blocked while pending and before that deadline; inputs remain editable and requests are never retried automatically.
119
+
120
+ `method` selects `post` (the default), `put`, `patch`, or `delete`. Use `renderError` to customize action-error presentation (a snippet in Svelte). Native forms remain available when you manage error presentation yourself.
121
+
116
122
  ## Tests
117
123
 
118
124
  - `test/integration/solid-pages-playground.test.ts`
@@ -1,8 +1,16 @@
1
1
  import { ErrorsContext, NavigationContext, ParamsContext, RouterContext, SharedContext } from "./context.mjs";
2
2
  import { JSX } from "solid-js";
3
- import { ActionResult, NavigationState, VisitOptions, VoidActionError, VoidRouter } from "void/pages-client";
3
+ import { ActionClient, FormMethod, FormModel, NavigationState, VisitOptions, VoidActionError, VoidRouter } from "void/pages-client";
4
4
  import { CloudContextVariables, DeferredState as Deferred, InferProps } from "void";
5
- import { ActionFormOptions, ActionUrl, ResolveActionBody, ResolveActionParams } from "void/routes";
5
+ import { ActionFormOptions, ActionUrl, ResolveActionBody } from "void/routes";
6
+ //#region src/runtime/form.d.ts
7
+ type FormProps = Omit<JSX.FormHTMLAttributes<HTMLFormElement>, "action" | "onSubmit" | "method" | "onError"> & {
8
+ form: FormModel;
9
+ method?: FormMethod;
10
+ renderError?: (error: VoidActionError) => JSX.Element;
11
+ };
12
+ export declare function Form(props: FormProps): JSX.Element;
13
+ //#endregion
6
14
  //#region src/runtime/link.d.ts
7
15
  type PrefetchStrategy = "hover" | "click" | "mount" | "visible";
8
16
  type LinkMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
@@ -85,19 +93,7 @@ type IslandFormReturn<T extends Record<string, unknown>> = Readonly<{
85
93
  export declare function useIslandForm<T extends Record<string, unknown>>(defaults: T): IslandFormReturn<T>;
86
94
  //#endregion
87
95
  //#region src/runtime/action.d.ts
88
- type ActionMethod = "POST" | "PUT" | "PATCH" | "DELETE";
89
- type HasParams<U extends string> = [ResolveActionParams<U>] extends [never] ? false : true;
90
- type ActionOptions<U extends string> = {
91
- data?: ResolveActionBody<U>;
92
- method?: ActionMethod;
93
- } & (HasParams<U> extends true ? {
94
- params: ResolveActionParams<U>;
95
- } : {
96
- params?: never;
97
- });
98
- /** Called during app initialization to store the router instance */
99
- export declare function setActionRouter(router: VoidRouter): void;
100
- /** Programmatic one-shot page action call with Inertia page update */
101
- export declare function action<U extends ActionUrl & string>(url: U, ...args: HasParams<U> extends true ? [options: ActionOptions<U>] : [options?: ActionOptions<U>]): Promise<ActionResult>;
96
+ export declare const action: ActionClient["action"];
97
+ export declare const setActionRouter: ActionClient["setActionRouter"];
102
98
  //#endregion
103
99
  export { type Deferred, ErrorsContext, type InferProps, type LinkProps, NavigationContext, ParamsContext, RouterContext, SharedContext };
@@ -1,7 +1,32 @@
1
1
  import { ErrorsContext, NavigationContext, ParamsContext, RouterContext, SharedContext } from "./context.mjs";
2
- import { createMemo, createSignal, onCleanup, onMount, splitProps, useContext } from "solid-js";
3
- import { VoidActionError, categorizeActionError, idleNavigationState, isAbortError, isEqualFormValue, ssrProxy, submitAction } from "void/pages-client";
2
+ import { createEffect, createMemo, createSignal, onCleanup, onMount, splitProps, useContext } from "solid-js";
3
+ import { VoidActionError, categorizeActionError, createActionClient, formErrorMessage, formRetryMessage, formSubmissionBlocked, idleNavigationState, isAbortError, isEqualFormValue, scheduleFormRetry, ssrProxy, submitAction } from "void/pages-client";
4
4
  import { createStore, reconcile } from "solid-js/store";
5
+ //#region src/runtime/form.tsx
6
+ function Form(props) {
7
+ const [local, rest] = splitProps(props, [
8
+ "form",
9
+ "method",
10
+ "renderError",
11
+ "children"
12
+ ]);
13
+ const [now, setNow] = createSignal(Date.now());
14
+ createEffect(() => {
15
+ const cancel = scheduleFormRetry(local.form.error, () => setNow(Date.now()));
16
+ onCleanup(cancel);
17
+ });
18
+ return <form {...rest} aria-busy={local.form.pending} onSubmit={async (event) => {
19
+ event.preventDefault();
20
+ if (!formSubmissionBlocked(local.form, now())) await local.form[local.method ?? "post"]();
21
+ }}>
22
+ {formErrorMessage(local.form) && <div role="alert" data-void-form-error>
23
+ {local.form.error && local.renderError ? local.renderError(local.form.error) : formErrorMessage(local.form)}
24
+ {formRetryMessage(local.form.error) && <p>{formRetryMessage(local.form.error)}</p>}
25
+ </div>}
26
+ {local.children}
27
+ </form>;
28
+ }
29
+ //#endregion
5
30
  //#region src/runtime/link.tsx
6
31
  function appendQueryValue(params, key, value) {
7
32
  if (value === null || value === void 0) return false;
@@ -498,30 +523,8 @@ function useIslandForm(defaults) {
498
523
  }
499
524
  //#endregion
500
525
  //#region src/runtime/action.ts
501
- let _router = null;
502
- /** Called during app initialization to store the router instance */
503
- function setActionRouter(router) {
504
- _router = router;
505
- }
506
- /** Programmatic one-shot page action call with Inertia page update */
507
- async function action(url, ...args) {
508
- if (!_router) throw new Error("action(): called before router initialization.");
509
- const options = args[0];
510
- const data = options?.data;
511
- const method = options?.method || "POST";
512
- let resolvedUrl = url;
513
- let actionQuery = "";
514
- const qIdx = resolvedUrl.indexOf("?");
515
- if (qIdx !== -1) {
516
- actionQuery = resolvedUrl.slice(qIdx);
517
- resolvedUrl = resolvedUrl.slice(0, qIdx);
518
- }
519
- if (options?.params) for (const [key, value] of Object.entries(options.params)) resolvedUrl = resolvedUrl.replace(`:${key}`, encodeURIComponent(value));
520
- resolvedUrl = resolvedUrl + actionQuery;
521
- return submitAction(_router, resolvedUrl, {
522
- method,
523
- data
524
- });
525
- }
526
+ const client = createActionClient();
527
+ const action = client.action;
528
+ const setActionRouter = client.setActionRouter;
526
529
  //#endregion
527
- export { ErrorsContext, Link, NavigationContext, ParamsContext, RouterContext, SharedContext, action, setActionRouter, useForm, useIslandForm, useNavigation, useParams, useRouter, useShared };
530
+ export { ErrorsContext, Form, Link, NavigationContext, ParamsContext, RouterContext, SharedContext, action, setActionRouter, useForm, useIslandForm, useNavigation, useParams, useRouter, useShared };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@void/solid",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/voidzero-dev/void.git",
@@ -33,12 +33,12 @@
33
33
  "solid-js": "^1.9.15",
34
34
  "vite": "npm:@voidzero-dev/vite-plus-core@1.0.0",
35
35
  "vite-plus": "1.0.0",
36
- "void": "0.22.0"
36
+ "void": "0.23.0"
37
37
  },
38
38
  "peerDependencies": {
39
39
  "solid-js": "^1.9.0",
40
40
  "vite": "^8.0.0",
41
- "void": "0.22.0"
41
+ "void": "0.23.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "vp pack",
@@ -1,50 +1,6 @@
1
- import { submitAction, type ActionResult, type VoidRouter } from 'void/pages-client';
2
- import type { ActionUrl, ResolveActionBody, ResolveActionParams } from 'void/routes';
1
+ import { createActionClient, type ActionClient } from 'void/pages-client';
3
2
 
4
- type ActionMethod = 'POST' | 'PUT' | 'PATCH' | 'DELETE';
3
+ const client = createActionClient();
5
4
 
6
- type HasParams<U extends string> = [ResolveActionParams<U>] extends [never] ? false : true;
7
-
8
- type ActionOptions<U extends string> = {
9
- data?: ResolveActionBody<U>;
10
- method?: ActionMethod;
11
- } & (HasParams<U> extends true ? { params: ResolveActionParams<U> } : { params?: never });
12
-
13
- let _router: VoidRouter | null = null;
14
-
15
- /** Called during app initialization to store the router instance */
16
- export function setActionRouter(router: VoidRouter): void {
17
- _router = router;
18
- }
19
-
20
- /** Programmatic one-shot page action call with Inertia page update */
21
- export async function action<U extends ActionUrl & string>(
22
- url: U,
23
- ...args: HasParams<U> extends true ? [options: ActionOptions<U>] : [options?: ActionOptions<U>]
24
- ): Promise<ActionResult> {
25
- if (!_router) {
26
- throw new Error('action(): called before router initialization.');
27
- }
28
-
29
- const options = args[0] as ActionOptions<U> | undefined;
30
- const data = options?.data;
31
- const method = options?.method || 'POST';
32
-
33
- let resolvedUrl: string = url;
34
- let actionQuery = '';
35
-
36
- const qIdx = resolvedUrl.indexOf('?');
37
- if (qIdx !== -1) {
38
- actionQuery = resolvedUrl.slice(qIdx);
39
- resolvedUrl = resolvedUrl.slice(0, qIdx);
40
- }
41
-
42
- if (options?.params) {
43
- for (const [key, value] of Object.entries(options.params)) {
44
- resolvedUrl = resolvedUrl.replace(`:${key}`, encodeURIComponent(value as string));
45
- }
46
- }
47
-
48
- resolvedUrl = resolvedUrl + actionQuery;
49
- return submitAction(_router, resolvedUrl, { method, data });
50
- }
5
+ export const action: ActionClient['action'] = client.action;
6
+ export const setActionRouter: ActionClient['setActionRouter'] = client.setActionRouter;
@@ -0,0 +1,49 @@
1
+ import { createEffect, createSignal, onCleanup, splitProps, type JSX } from 'solid-js';
2
+ import {
3
+ formErrorMessage,
4
+ formRetryMessage,
5
+ formSubmissionBlocked,
6
+ scheduleFormRetry,
7
+ type FormModel,
8
+ type FormMethod,
9
+ type VoidActionError,
10
+ } from 'void/pages-client';
11
+
12
+ export type FormProps = Omit<
13
+ JSX.FormHTMLAttributes<HTMLFormElement>,
14
+ 'action' | 'onSubmit' | 'method' | 'onError'
15
+ > & {
16
+ form: FormModel;
17
+ method?: FormMethod;
18
+ renderError?: (error: VoidActionError) => JSX.Element;
19
+ };
20
+ export function Form(props: FormProps): JSX.Element {
21
+ const [local, rest] = splitProps(props, ['form', 'method', 'renderError', 'children']);
22
+ const [now, setNow] = createSignal(Date.now());
23
+ createEffect(() => {
24
+ const cancel = scheduleFormRetry(local.form.error, () => setNow(Date.now()));
25
+ onCleanup(cancel);
26
+ });
27
+ return (
28
+ <form
29
+ {...rest}
30
+ aria-busy={local.form.pending}
31
+ onSubmit={async (event) => {
32
+ event.preventDefault();
33
+ if (!formSubmissionBlocked(local.form, now())) {
34
+ await local.form[local.method ?? 'post']();
35
+ }
36
+ }}
37
+ >
38
+ {formErrorMessage(local.form) && (
39
+ <div role="alert" data-void-form-error>
40
+ {local.form.error && local.renderError
41
+ ? local.renderError(local.form.error)
42
+ : formErrorMessage(local.form)}
43
+ {formRetryMessage(local.form.error) && <p>{formRetryMessage(local.form.error)}</p>}
44
+ </div>
45
+ )}
46
+ {local.children}
47
+ </form>
48
+ );
49
+ }
@@ -1,3 +1,4 @@
1
+ export { Form } from './form.tsx';
1
2
  export { Link, type LinkProps } from './link.tsx';
2
3
  export { useRouter } from './use-router.ts';
3
4
  export { useParams } from './use-params.ts';