@timber-js/app 0.2.0-alpha.217 → 0.2.0-alpha.219
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/agent-skill.md +5 -5
- package/dist/_chunks/{actions-CUh3cClk.js → actions-BwkmAYXq.js} +8 -1
- package/dist/_chunks/actions-BwkmAYXq.js.map +1 -0
- package/dist/_chunks/cloudflare-Byxl8FWP.js.map +1 -1
- package/dist/_chunks/dev-server-B8zsT-ee.js.map +1 -1
- package/dist/_chunks/dist-DtrJyBbw.js.map +1 -1
- package/dist/_chunks/graph-cache-CYiUS2aS.js.map +1 -1
- package/dist/_chunks/use-query-states-I3JMng6J.js.map +1 -1
- package/dist/adapters/cloudflare.js +1 -2
- package/dist/adapters/nitro.js.map +1 -1
- package/dist/analyze/graph-command.js.map +1 -1
- package/dist/client/error-boundary.js +1 -2
- package/dist/client/form.d.ts +34 -37
- package/dist/client/form.d.ts.map +1 -1
- package/dist/client/index.d.ts +3 -3
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +40 -75
- package/dist/client/index.js.map +1 -1
- package/dist/client/{use-form-field.d.ts → use-field-state.d.ts} +10 -7
- package/dist/client/use-field-state.d.ts.map +1 -0
- package/dist/config-types.d.ts +15 -18
- package/dist/config-types.d.ts.map +1 -1
- package/dist/cookies/index.js.map +1 -1
- package/dist/index.js +29 -27
- package/dist/index.js.map +1 -1
- package/dist/plugins/build-manifest.d.ts.map +1 -1
- package/dist/plugins/shims.d.ts.map +1 -1
- package/dist/search-params/index.js.map +1 -1
- package/dist/server/action-client.d.ts +11 -172
- package/dist/server/action-client.d.ts.map +1 -1
- package/dist/server/action-handler.d.ts +0 -7
- package/dist/server/action-handler.d.ts.map +1 -1
- package/dist/server/action-schema.d.ts +126 -0
- package/dist/server/action-schema.d.ts.map +1 -0
- package/dist/server/action-types.d.ts +108 -0
- package/dist/server/action-types.d.ts.map +1 -0
- package/dist/server/form-data.d.ts +7 -0
- package/dist/server/form-data.d.ts.map +1 -1
- package/dist/server/index.d.ts +3 -2
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +511 -148
- package/dist/server/index.js.map +1 -1
- package/dist/server/internal.js +12 -3
- package/dist/server/internal.js.map +1 -1
- package/dist/server/pipeline-metadata.d.ts +9 -0
- package/dist/server/pipeline-metadata.d.ts.map +1 -1
- package/dist/server/pipeline-phases.d.ts.map +1 -1
- package/dist/server/rsc-entry/action-dispatcher.d.ts +0 -3
- package/dist/server/rsc-entry/action-dispatcher.d.ts.map +1 -1
- package/dist/server/rsc-entry/index.d.ts.map +1 -1
- package/dist/server/schema-introspection.d.ts +68 -0
- package/dist/server/schema-introspection.d.ts.map +1 -0
- package/dist/server/sensitive-fields.d.ts +40 -26
- package/dist/server/sensitive-fields.d.ts.map +1 -1
- package/dist/server/sitemap-handler.d.ts.map +1 -1
- package/docs/api/31-api-client.mdx +19 -18
- package/docs/api/34-api-config.mdx +1 -1
- package/docs/learn/08-forms-and-actions.mdx +57 -23
- package/package.json +8 -8
- package/src/client/browser-entry/index.ts +7 -1
- package/src/client/form.tsx +57 -108
- package/src/client/index.ts +3 -3
- package/src/client/{use-form-field.ts → use-field-state.ts} +20 -8
- package/src/config-types.ts +15 -18
- package/src/plugins/build-manifest.ts +12 -27
- package/src/plugins/shims.ts +0 -1
- package/src/server/action-client.ts +82 -307
- package/src/server/action-handler.ts +1 -42
- package/src/server/action-schema.ts +203 -0
- package/src/server/action-types.ts +145 -0
- package/src/server/form-data.ts +7 -0
- package/src/server/index.ts +5 -3
- package/src/server/pipeline-metadata.ts +11 -1
- package/src/server/pipeline-phases.ts +9 -2
- package/src/server/rsc-entry/action-dispatcher.ts +0 -4
- package/src/server/rsc-entry/index.ts +1 -2
- package/src/server/schema-introspection.ts +393 -0
- package/src/server/sensitive-fields.ts +115 -58
- package/src/server/sitemap-handler.ts +14 -21
- package/dist/_chunks/actions-CUh3cClk.js.map +0 -1
- package/dist/client/use-form-field.d.ts.map +0 -1
package/dist/client/form.d.ts
CHANGED
|
@@ -3,55 +3,52 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Forms use React's own `useActionState`: an action built with
|
|
5
5
|
* `createActionClient` already has the `(prevState, payload)` signature it
|
|
6
|
-
* calls, and the result is typed from it.
|
|
7
|
-
*
|
|
6
|
+
* calls, and the result is typed from it. An imperative call (a delete
|
|
7
|
+
* button) uses the same hook: `startTransition(() => dispatch(input))`.
|
|
8
|
+
* `readFormErrors` reads the errors out of that result.
|
|
8
9
|
*
|
|
9
|
-
* See design/08-forms-and-actions.md §"
|
|
10
|
+
* See design/08-forms-and-actions.md §"Form Errors via readFormErrors"
|
|
10
11
|
*/
|
|
11
|
-
import type {
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
* Delete
|
|
21
|
-
* </button>
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export declare function useFormAction<TData = unknown, TInput = unknown>(action: ActionFn<TData, TInput> | ((input: TInput) => Promise<ActionResult<TData>>)): [
|
|
25
|
-
(...args: undefined extends TInput ? [input?: InputHint<TInput>] : [input: InputHint<TInput>]) => Promise<ActionResult<TData>>,
|
|
26
|
-
boolean
|
|
27
|
-
];
|
|
28
|
-
/** What `parseFormErrors` reads out of an action result. */
|
|
29
|
-
export interface FormErrorsResult {
|
|
30
|
-
/** Per-field validation errors keyed by field name. */
|
|
31
|
-
fieldErrors: Record<string, string[]>;
|
|
32
|
-
/** Form-level errors (from `_root` key). */
|
|
33
|
-
formErrors: string[];
|
|
34
|
-
/** Server error if the action threw an ActionError. */
|
|
35
|
-
serverError: {
|
|
36
|
-
code: string;
|
|
37
|
-
data?: Record<string, unknown>;
|
|
38
|
-
} | null;
|
|
12
|
+
import type { ActionServerError } from '../server/action-types.ts';
|
|
13
|
+
/** What `readFormErrors` reads out of an action result. */
|
|
14
|
+
export interface FormErrors<TPath extends string = string> {
|
|
15
|
+
/** The first validation error for a field, or null. Paths are dot paths: `sets.0.name`. */
|
|
16
|
+
field(name: TPath): string | null;
|
|
17
|
+
/** Form-level validation errors (the `_root` key). */
|
|
18
|
+
form: readonly string[];
|
|
19
|
+
/** The server error, if the action threw (an `ActionError`, or `INTERNAL_ERROR`). */
|
|
20
|
+
server: ActionServerError | null;
|
|
39
21
|
/** Whether any errors are present. */
|
|
40
22
|
hasErrors: boolean;
|
|
41
|
-
/** Get the first error message for a field, or null. */
|
|
42
|
-
getFieldError: (field: string) => string | null;
|
|
43
23
|
}
|
|
24
|
+
/** The shape `readFormErrors` reads: any `ActionResult`. */
|
|
25
|
+
interface ResultWithErrors {
|
|
26
|
+
validationErrors?: {
|
|
27
|
+
[path: string]: string[] | undefined;
|
|
28
|
+
};
|
|
29
|
+
serverError?: ActionServerError;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The field paths of a result's validation errors, from its schema's input.
|
|
33
|
+
* Distributes over the result union: only the validation-failure member has
|
|
34
|
+
* a non-`never` `validationErrors`.
|
|
35
|
+
*/
|
|
36
|
+
type ErrorPaths<R> = R extends {
|
|
37
|
+
validationErrors?: infer V;
|
|
38
|
+
} ? [NonNullable<V>] extends [never] ? never : Exclude<keyof NonNullable<V>, '_root'> & string : never;
|
|
44
39
|
/**
|
|
45
40
|
* Read the errors out of an action result — the state `useActionState`
|
|
46
41
|
* returns. `_root` validation errors are form-level; every other key is a
|
|
47
|
-
* field.
|
|
42
|
+
* field. Field names are typed from the action's schema input, so a
|
|
43
|
+
* misspelled path is a type error.
|
|
48
44
|
*
|
|
49
45
|
* @example
|
|
50
46
|
* ```tsx
|
|
51
47
|
* const [result, action, isPending] = useActionState(createTodo, null)
|
|
52
|
-
* const errors =
|
|
53
|
-
* errors.
|
|
48
|
+
* const errors = readFormErrors(result)
|
|
49
|
+
* errors.field('title') // first message for the field, or null
|
|
54
50
|
* ```
|
|
55
51
|
*/
|
|
56
|
-
export declare function
|
|
52
|
+
export declare function readFormErrors<R extends ResultWithErrors>(result: R | null | undefined): FormErrors<ErrorPaths<R>>;
|
|
53
|
+
export {};
|
|
57
54
|
//# sourceMappingURL=form.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"form.d.ts","sourceRoot":"","sources":["../../src/client/form.tsx"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"form.d.ts","sourceRoot":"","sources":["../../src/client/form.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAInE,2DAA2D;AAC3D,MAAM,WAAW,UAAU,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM;IACvD,2FAA2F;IAC3F,KAAK,CAAC,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,IAAI,CAAC;IAClC,sDAAsD;IACtD,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;IACxB,qFAAqF;IACrF,MAAM,EAAE,iBAAiB,GAAG,IAAI,CAAC;IACjC,sCAAsC;IACtC,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,4DAA4D;AAC5D,UAAU,gBAAgB;IACxB,gBAAgB,CAAC,EAAE;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAA;KAAE,CAAC;IAC5D,WAAW,CAAC,EAAE,iBAAiB,CAAC;CACjC;AAED;;;;GAIG;AACH,KAAK,UAAU,CAAC,CAAC,IAAI,CAAC,SAAS;IAAE,gBAAgB,CAAC,EAAE,MAAM,CAAC,CAAA;CAAE,GACzD,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAC9B,KAAK,GACL,OAAO,CAAC,MAAM,WAAW,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG,MAAM,GACjD,KAAK,CAAC;AAWV;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,gBAAgB,EACvD,MAAM,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,GAC3B,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAkB3B"}
|
package/dist/client/index.d.ts
CHANGED
|
@@ -13,9 +13,9 @@ export type { NavigationTransitionType } from './router-types.ts';
|
|
|
13
13
|
export { usePathname } from './use-pathname.ts';
|
|
14
14
|
export { replaceUrl } from './shallow-url.ts';
|
|
15
15
|
export { useSelectedLayoutSegment, useSelectedLayoutSegments, } from './use-selected-layout-segment.ts';
|
|
16
|
-
export {
|
|
17
|
-
export {
|
|
18
|
-
export type {
|
|
16
|
+
export { readFormErrors } from './form.tsx';
|
|
17
|
+
export { useFieldState } from './use-field-state.ts';
|
|
18
|
+
export type { FormErrors } from './form.tsx';
|
|
19
19
|
export { useSegmentParams } from './use-segment-params.ts';
|
|
20
20
|
export { useQueryStates } from './use-query-states.ts';
|
|
21
21
|
export type { ClientCookieOptions, CookieSetter } from './use-cookie.ts';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,IAAI,EAAE,iBAAiB,EAAE,WAAW,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AACpG,OAAO,EAAE,0BAA0B,EAAE,MAAM,kCAAkC,CAAC;AAC9E,YAAY,EAAE,SAAS,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACnG,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AA4CxF,MAAM,WAAW,YAAY;CAG5B;AACD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACxE,YAAY,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAC1E,YAAY,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EACL,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,kCAAkC,CAAC;AAG1C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,IAAI,EAAE,iBAAiB,EAAE,WAAW,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AACpG,OAAO,EAAE,0BAA0B,EAAE,MAAM,kCAAkC,CAAC;AAC9E,YAAY,EAAE,SAAS,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACnG,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AA4CxF,MAAM,WAAW,YAAY;CAG5B;AACD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACxE,YAAY,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAC1E,YAAY,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EACL,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,kCAAkC,CAAC;AAG1C,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAK7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAY3D,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAIvD,YAAY,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/client/index.js
CHANGED
|
@@ -8,7 +8,7 @@ import { t as getSsrData } from "../_chunks/ssr-data-nA3I70_n.js";
|
|
|
8
8
|
import { t as getLinkCodec } from "../_chunks/codec-registry-oOUxugz3.js";
|
|
9
9
|
import { i as useNavigationContext, n as useSegmentContext } from "../_chunks/segment-context-xtPUGfdq.js";
|
|
10
10
|
import { n as useQueryStates } from "../_chunks/use-query-states-I3JMng6J.js";
|
|
11
|
-
import React, { createContext, useCallback, useContext, useEffect, useLayoutEffect, useRef, useState
|
|
11
|
+
import React, { createContext, useCallback, useContext, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
12
12
|
import { jsx } from "react/jsx-runtime";
|
|
13
13
|
//#region src/client/use-link-status.ts
|
|
14
14
|
/**
|
|
@@ -574,87 +574,45 @@ function useSelectedLayoutSegments(parallelRouteKey) {
|
|
|
574
574
|
}
|
|
575
575
|
//#endregion
|
|
576
576
|
//#region src/client/form.tsx
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
* out of that result.
|
|
584
|
-
*
|
|
585
|
-
* See design/08-forms-and-actions.md §"Client-Side Form Mechanics"
|
|
586
|
-
*/
|
|
587
|
-
/**
|
|
588
|
-
* Hook for calling a server action imperatively (not via a form).
|
|
589
|
-
* Returns [execute, isPending] where execute accepts the input directly.
|
|
590
|
-
*
|
|
591
|
-
* @example
|
|
592
|
-
* ```tsx
|
|
593
|
-
* const [deleteTodo, isPending] = useFormAction(deleteTodoAction)
|
|
594
|
-
* <button onClick={() => deleteTodo({ id: todo.id })} disabled={isPending}>
|
|
595
|
-
* Delete
|
|
596
|
-
* </button>
|
|
597
|
-
* ```
|
|
598
|
-
*/
|
|
599
|
-
function useFormAction(action) {
|
|
600
|
-
const [isPending, startTransition] = useTransition();
|
|
601
|
-
const execute = (input) => {
|
|
602
|
-
return new Promise((resolve, reject) => {
|
|
603
|
-
startTransition(async () => {
|
|
604
|
-
try {
|
|
605
|
-
resolve(await action(input));
|
|
606
|
-
} catch (error) {
|
|
607
|
-
reject(error);
|
|
608
|
-
}
|
|
609
|
-
});
|
|
610
|
-
});
|
|
611
|
-
};
|
|
612
|
-
return [execute, isPending];
|
|
613
|
-
}
|
|
577
|
+
var EMPTY = Object.freeze({
|
|
578
|
+
field: () => null,
|
|
579
|
+
form: Object.freeze([]),
|
|
580
|
+
server: null,
|
|
581
|
+
hasErrors: false
|
|
582
|
+
});
|
|
614
583
|
/**
|
|
615
584
|
* Read the errors out of an action result — the state `useActionState`
|
|
616
585
|
* returns. `_root` validation errors are form-level; every other key is a
|
|
617
|
-
* field.
|
|
586
|
+
* field. Field names are typed from the action's schema input, so a
|
|
587
|
+
* misspelled path is a type error.
|
|
618
588
|
*
|
|
619
589
|
* @example
|
|
620
590
|
* ```tsx
|
|
621
591
|
* const [result, action, isPending] = useActionState(createTodo, null)
|
|
622
|
-
* const errors =
|
|
623
|
-
* errors.
|
|
592
|
+
* const errors = readFormErrors(result)
|
|
593
|
+
* errors.field('title') // first message for the field, or null
|
|
624
594
|
* ```
|
|
625
595
|
*/
|
|
626
|
-
function
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
};
|
|
634
|
-
if (!result) return empty;
|
|
635
|
-
const validationErrors = result.validationErrors;
|
|
636
|
-
const serverError = result.serverError;
|
|
637
|
-
if (!validationErrors && !serverError) return empty;
|
|
638
|
-
const fieldErrors = {};
|
|
639
|
-
const formErrors = [];
|
|
640
|
-
if (validationErrors) for (const [key, messages] of Object.entries(validationErrors)) if (key === "_root") formErrors.push(...messages);
|
|
641
|
-
else fieldErrors[key] = messages;
|
|
642
|
-
const hasErrors = Object.keys(fieldErrors).length > 0 || formErrors.length > 0 || serverError != null;
|
|
596
|
+
function readFormErrors(result) {
|
|
597
|
+
if (!result) return EMPTY;
|
|
598
|
+
const validationErrors = result.validationErrors ?? {};
|
|
599
|
+
const server = result.serverError ?? null;
|
|
600
|
+
const form = validationErrors._root ?? [];
|
|
601
|
+
const hasErrors = server !== null || Object.values(validationErrors).some((messages) => messages?.length);
|
|
602
|
+
if (!hasErrors) return EMPTY;
|
|
643
603
|
return {
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
return errs && errs.length > 0 ? errs[0] : null;
|
|
651
|
-
}
|
|
604
|
+
field(name) {
|
|
605
|
+
return validationErrors[name]?.[0] ?? null;
|
|
606
|
+
},
|
|
607
|
+
form,
|
|
608
|
+
server,
|
|
609
|
+
hasErrors
|
|
652
610
|
};
|
|
653
611
|
}
|
|
654
612
|
//#endregion
|
|
655
|
-
//#region src/client/use-
|
|
613
|
+
//#region src/client/use-field-state.ts
|
|
656
614
|
/**
|
|
657
|
-
*
|
|
615
|
+
* useFieldState — state for a form field that needs JavaScript (a combobox, a
|
|
658
616
|
* chip list, a row list), made to behave like a native uncontrolled input.
|
|
659
617
|
*
|
|
660
618
|
* React resets a form after its action settles, so a native field with a
|
|
@@ -670,8 +628,11 @@ function parseFormErrors(result) {
|
|
|
670
628
|
* State for a field that follows its form's reset.
|
|
671
629
|
*
|
|
672
630
|
* Returns `[value, setValue, ref]`. Attach `ref` to any element inside the
|
|
673
|
-
* `<form>`,
|
|
674
|
-
*
|
|
631
|
+
* `<form>`, or to the `<form>` itself (the hook subscribes to
|
|
632
|
+
* `closest('form')`, which includes the element), and render the value into
|
|
633
|
+
* the form data yourself (usually hidden inputs). With the ref on the form,
|
|
634
|
+
* the hook can live above it and hold the whole draft as one object, for a
|
|
635
|
+
* live preview beside the form. `value` is `defaultValue` until `setValue` is called, and is
|
|
675
636
|
* `defaultValue` again after the form resets, unless the form cancels its
|
|
676
637
|
* `reset` event. An edit dispatches a bubbling `change` event from the `ref`
|
|
677
638
|
* element after it commits, so a native `change` listener on the form sees it
|
|
@@ -682,14 +643,14 @@ function parseFormErrors(result) {
|
|
|
682
643
|
* @example
|
|
683
644
|
* ```tsx
|
|
684
645
|
* // draft: the form's defaults, read from result?.submittedValues ?? pageData
|
|
685
|
-
* const [tags, setTags, ref] =
|
|
646
|
+
* const [tags, setTags, ref] = useFieldState(draft.tags);
|
|
686
647
|
* <div ref={ref}>
|
|
687
|
-
* <input type="hidden" name="tags" value={
|
|
648
|
+
* {tags.map((tag) => <input key={tag} type="hidden" name="tags" value={tag} />)}
|
|
688
649
|
* <TagPicker value={tags} onChange={setTags} />
|
|
689
650
|
* </div>
|
|
690
651
|
* ```
|
|
691
652
|
*/
|
|
692
|
-
function
|
|
653
|
+
function useFieldState(defaultValue) {
|
|
693
654
|
const [edit, setEdit] = useState(null);
|
|
694
655
|
const value = edit ? edit.value : defaultValue;
|
|
695
656
|
const defaultRef = useRef(defaultValue);
|
|
@@ -719,8 +680,12 @@ function useFormField(defaultValue) {
|
|
|
719
680
|
if (!form) return;
|
|
720
681
|
const onReset = (event) => {
|
|
721
682
|
pendingResets.current.push(event);
|
|
722
|
-
|
|
683
|
+
const apply = () => {
|
|
723
684
|
if (takeResets()) setEdit(null);
|
|
685
|
+
};
|
|
686
|
+
queueMicrotask(() => {
|
|
687
|
+
if (event.eventPhase === Event.NONE) apply();
|
|
688
|
+
else setTimeout(apply, 0);
|
|
724
689
|
});
|
|
725
690
|
};
|
|
726
691
|
form.addEventListener("reset", onReset);
|
|
@@ -829,6 +794,6 @@ function useSegmentParams(segmentPath) {
|
|
|
829
794
|
return resolveSegmentParams(paramsContext.params, paramsContext.slotParams, segmentPath);
|
|
830
795
|
}
|
|
831
796
|
//#endregion
|
|
832
|
-
export { Link, LinkStatusContext, buildLinkProps, interpolateParams, mergePreservedSearchParams,
|
|
797
|
+
export { Link, LinkStatusContext, buildLinkProps, interpolateParams, mergePreservedSearchParams, readFormErrors, replaceUrl, resolveHref, useFieldState, useLinkStatus, usePathname, usePendingNavigation, useQueryStates, useRouter, useSegmentParams, useSelectedLayoutSegment, useSelectedLayoutSegments, validateNavigationHref as validateLinkHref };
|
|
833
798
|
|
|
834
799
|
//# sourceMappingURL=index.js.map
|