@azx-pbc/axiom-react 0.0.0-stage → 0.0.6
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/LICENSE +21 -0
- package/README.md +604 -2
- package/dist/FormErrorBoundary.d.ts +37 -0
- package/dist/FormErrorBoundary.d.ts.map +1 -0
- package/dist/FormErrorBoundary.js +54 -0
- package/dist/FormErrorBoundary.js.map +1 -0
- package/dist/FormProvider.d.ts +197 -0
- package/dist/FormProvider.d.ts.map +1 -0
- package/dist/FormProvider.js +149 -0
- package/dist/FormProvider.js.map +1 -0
- package/dist/field-error.d.ts +154 -0
- package/dist/field-error.d.ts.map +1 -0
- package/dist/field-error.js +2 -0
- package/dist/field-error.js.map +1 -0
- package/dist/field-props.d.ts +362 -0
- package/dist/field-props.d.ts.map +1 -0
- package/dist/field-props.js +64 -0
- package/dist/field-props.js.map +1 -0
- package/dist/form-context.d.ts +31 -0
- package/dist/form-context.d.ts.map +1 -0
- package/dist/form-context.js +13 -0
- package/dist/form-context.js.map +1 -0
- package/dist/form-store.d.ts +784 -0
- package/dist/form-store.d.ts.map +1 -0
- package/dist/form-store.js +2179 -0
- package/dist/form-store.js.map +1 -0
- package/dist/hints.d.ts +60 -0
- package/dist/hints.d.ts.map +1 -0
- package/dist/hints.js +158 -0
- package/dist/hints.js.map +1 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/render/field-value.d.ts +92 -0
- package/dist/render/field-value.d.ts.map +1 -0
- package/dist/render/field-value.js +124 -0
- package/dist/render/field-value.js.map +1 -0
- package/dist/render/nodes.d.ts +303 -0
- package/dist/render/nodes.d.ts.map +1 -0
- package/dist/render/nodes.js +325 -0
- package/dist/render/nodes.js.map +1 -0
- package/dist/render/schema.d.ts +30 -0
- package/dist/render/schema.d.ts.map +1 -0
- package/dist/render/schema.js +40 -0
- package/dist/render/schema.js.map +1 -0
- package/dist/render/value-text.d.ts +59 -0
- package/dist/render/value-text.d.ts.map +1 -0
- package/dist/render/value-text.js +104 -0
- package/dist/render/value-text.js.map +1 -0
- package/dist/store/array-bindings.d.ts +110 -0
- package/dist/store/array-bindings.d.ts.map +1 -0
- package/dist/store/array-bindings.js +181 -0
- package/dist/store/array-bindings.js.map +1 -0
- package/dist/store/bindings.d.ts +12 -0
- package/dist/store/bindings.d.ts.map +1 -0
- package/dist/store/bindings.js +16 -0
- package/dist/store/bindings.js.map +1 -0
- package/dist/store/buffer-state.d.ts +42 -0
- package/dist/store/buffer-state.d.ts.map +1 -0
- package/dist/store/buffer-state.js +51 -0
- package/dist/store/buffer-state.js.map +1 -0
- package/dist/store/configure.d.ts +19 -0
- package/dist/store/configure.d.ts.map +1 -0
- package/dist/store/configure.js +28 -0
- package/dist/store/configure.js.map +1 -0
- package/dist/store/derive.d.ts +106 -0
- package/dist/store/derive.d.ts.map +1 -0
- package/dist/store/derive.js +417 -0
- package/dist/store/derive.js.map +1 -0
- package/dist/store/errors.d.ts +20 -0
- package/dist/store/errors.d.ts.map +1 -0
- package/dist/store/errors.js +101 -0
- package/dist/store/errors.js.map +1 -0
- package/dist/store/fields.d.ts +35 -0
- package/dist/store/fields.d.ts.map +1 -0
- package/dist/store/fields.js +81 -0
- package/dist/store/fields.js.map +1 -0
- package/dist/store/form-channel.d.ts +174 -0
- package/dist/store/form-channel.d.ts.map +1 -0
- package/dist/store/form-channel.js +411 -0
- package/dist/store/form-channel.js.map +1 -0
- package/dist/store/gates.d.ts +22 -0
- package/dist/store/gates.d.ts.map +1 -0
- package/dist/store/gates.js +44 -0
- package/dist/store/gates.js.map +1 -0
- package/dist/store/ids.d.ts +20 -0
- package/dist/store/ids.d.ts.map +1 -0
- package/dist/store/ids.js +28 -0
- package/dist/store/ids.js.map +1 -0
- package/dist/store/inputs.d.ts +114 -0
- package/dist/store/inputs.d.ts.map +1 -0
- package/dist/store/inputs.js +202 -0
- package/dist/store/inputs.js.map +1 -0
- package/dist/store/layout.d.ts +81 -0
- package/dist/store/layout.d.ts.map +1 -0
- package/dist/store/layout.js +285 -0
- package/dist/store/layout.js.map +1 -0
- package/dist/store/messages.d.ts +56 -0
- package/dist/store/messages.d.ts.map +1 -0
- package/dist/store/messages.js +157 -0
- package/dist/store/messages.js.map +1 -0
- package/dist/store/option-pages.d.ts +193 -0
- package/dist/store/option-pages.d.ts.map +1 -0
- package/dist/store/option-pages.js +598 -0
- package/dist/store/option-pages.js.map +1 -0
- package/dist/store/parse-print.d.ts +105 -0
- package/dist/store/parse-print.d.ts.map +1 -0
- package/dist/store/parse-print.js +415 -0
- package/dist/store/parse-print.js.map +1 -0
- package/dist/store/proposals.d.ts +114 -0
- package/dist/store/proposals.d.ts.map +1 -0
- package/dist/store/proposals.js +90 -0
- package/dist/store/proposals.js.map +1 -0
- package/dist/store/publish.d.ts +48 -0
- package/dist/store/publish.d.ts.map +1 -0
- package/dist/store/publish.js +96 -0
- package/dist/store/publish.js.map +1 -0
- package/dist/store/snapshots.d.ts +63 -0
- package/dist/store/snapshots.d.ts.map +1 -0
- package/dist/store/snapshots.js +154 -0
- package/dist/store/snapshots.js.map +1 -0
- package/dist/store/steps.d.ts +13 -0
- package/dist/store/steps.d.ts.map +1 -0
- package/dist/store/steps.js +31 -0
- package/dist/store/steps.js.map +1 -0
- package/dist/store/view.d.ts +124 -0
- package/dist/store/view.d.ts.map +1 -0
- package/dist/store/view.js +301 -0
- package/dist/store/view.js.map +1 -0
- package/dist/store/visibility.d.ts +68 -0
- package/dist/store/visibility.d.ts.map +1 -0
- package/dist/store/visibility.js +231 -0
- package/dist/store/visibility.js.map +1 -0
- package/dist/store/wizard-cursor.d.ts +43 -0
- package/dist/store/wizard-cursor.d.ts.map +1 -0
- package/dist/store/wizard-cursor.js +67 -0
- package/dist/store/wizard-cursor.js.map +1 -0
- package/dist/store/writes.d.ts +50 -0
- package/dist/store/writes.d.ts.map +1 -0
- package/dist/store/writes.js +91 -0
- package/dist/store/writes.js.map +1 -0
- package/dist/typed.d.ts +134 -0
- package/dist/typed.d.ts.map +1 -0
- package/dist/typed.js +74 -0
- package/dist/typed.js.map +1 -0
- package/dist/useAsyncOptions.d.ts +20 -0
- package/dist/useAsyncOptions.d.ts.map +1 -0
- package/dist/useAsyncOptions.js +70 -0
- package/dist/useAsyncOptions.js.map +1 -0
- package/dist/useBufferedInput.d.ts +69 -0
- package/dist/useBufferedInput.d.ts.map +1 -0
- package/dist/useBufferedInput.js +132 -0
- package/dist/useBufferedInput.js.map +1 -0
- package/dist/useField.d.ts +42 -0
- package/dist/useField.d.ts.map +1 -0
- package/dist/useField.js +63 -0
- package/dist/useField.js.map +1 -0
- package/dist/useFieldArray.d.ts +25 -0
- package/dist/useFieldArray.d.ts.map +1 -0
- package/dist/useFieldArray.js +18 -0
- package/dist/useFieldArray.js.map +1 -0
- package/dist/useForm.d.ts +20 -0
- package/dist/useForm.d.ts.map +1 -0
- package/dist/useForm.js +9 -0
- package/dist/useForm.js.map +1 -0
- package/dist/useFormState.d.ts +23 -0
- package/dist/useFormState.d.ts.map +1 -0
- package/dist/useFormState.js +11 -0
- package/dist/useFormState.js.map +1 -0
- package/dist/useGate.d.ts +15 -0
- package/dist/useGate.d.ts.map +1 -0
- package/dist/useGate.js +21 -0
- package/dist/useGate.js.map +1 -0
- package/dist/useLayout.d.ts +29 -0
- package/dist/useLayout.d.ts.map +1 -0
- package/dist/useLayout.js +59 -0
- package/dist/useLayout.js.map +1 -0
- package/dist/useMessage.d.ts +58 -0
- package/dist/useMessage.d.ts.map +1 -0
- package/dist/useMessage.js +62 -0
- package/dist/useMessage.js.map +1 -0
- package/dist/useWatch.d.ts +19 -0
- package/dist/useWatch.d.ts.map +1 -0
- package/dist/useWatch.js +17 -0
- package/dist/useWatch.js.map +1 -0
- package/dist/useWizard.d.ts +166 -0
- package/dist/useWizard.d.ts.map +1 -0
- package/dist/useWizard.js +554 -0
- package/dist/useWizard.js.map +1 -0
- package/package.json +62 -3
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Component } from 'react';
|
|
2
|
+
import type { ErrorInfo, ReactNode } from 'react';
|
|
3
|
+
/** What a host may render in place of a tree that threw while painting. */
|
|
4
|
+
export type FormRenderError = (error: unknown) => ReactNode;
|
|
5
|
+
/** The props a provider passes to its render boundary. */
|
|
6
|
+
export interface FormErrorBoundaryProps {
|
|
7
|
+
readonly children: ReactNode;
|
|
8
|
+
/**
|
|
9
|
+
* The host's fallback for a render that threw.
|
|
10
|
+
*
|
|
11
|
+
* A host that paints a sentence in its own locale, or a retry affordance, states it here. The
|
|
12
|
+
* session is still mounted either way: the values already entered belong to the provider, not
|
|
13
|
+
* to the subtree an error unmounted, so they are not discarded with it.
|
|
14
|
+
*/
|
|
15
|
+
readonly renderError?: FormRenderError;
|
|
16
|
+
}
|
|
17
|
+
interface State {
|
|
18
|
+
readonly failed: boolean;
|
|
19
|
+
readonly error: unknown;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The last line of defence between a control's render and the page around it.
|
|
23
|
+
*
|
|
24
|
+
* A class is the only way React exposes an error boundary, and this is the lifecycle it exists
|
|
25
|
+
* for. The boundary sits *inside* the provider, so the session — the record, the touched facts,
|
|
26
|
+
* the subscription caches — survives a subtree that threw: only the painted tree is replaced.
|
|
27
|
+
* It is not a substitute for a formatter that refuses to throw; it is what keeps one unexpected
|
|
28
|
+
* message or value from taking the whole page with it.
|
|
29
|
+
*/
|
|
30
|
+
export declare class FormErrorBoundary extends Component<FormErrorBoundaryProps, State> {
|
|
31
|
+
state: State;
|
|
32
|
+
static getDerivedStateFromError(error: unknown): State;
|
|
33
|
+
componentDidCatch(_error: unknown, _info: ErrorInfo): void;
|
|
34
|
+
render(): ReactNode;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=FormErrorBoundary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormErrorBoundary.d.ts","sourceRoot":"","sources":["../src/FormErrorBoundary.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAClC,OAAO,KAAK,EAAE,SAAS,EAAgB,SAAS,EAAE,MAAM,OAAO,CAAC;AAGhE,2EAA2E;AAC3E,MAAM,MAAM,eAAe,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,SAAS,CAAC;AAE5D,0DAA0D;AAC1D,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC7B;;;;;;OAMG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,eAAe,CAAC;CACxC;AAED,UAAU,KAAK;IACb,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACzB;AAyBD;;;;;;;;GAQG;AACH,qBAAa,iBAAkB,SAAQ,SAAS,CAAC,sBAAsB,EAAE,KAAK,CAAC;IACpE,KAAK,EAAE,KAAK,CAAuC;IAE5D,MAAM,CAAC,wBAAwB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,CAErD;IAEQ,iBAAiB,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI,CAGlE;IAEQ,MAAM,IAAI,SAAS,CAW3B;CACF"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Component } from 'react';
|
|
3
|
+
import { AxiomError } from '@azx-pbc/axiom-runtime';
|
|
4
|
+
/** The fallback a host that supplies none gets: a diagnostic, never a blank page. */
|
|
5
|
+
function defaultRenderError(error) {
|
|
6
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
7
|
+
return (_jsxs("div", { role: "alert", "data-form-render-error": "", children: ["This form could not be displayed. The answers already entered are still held by this page.", _jsxs("span", { "data-form-render-error-detail": "", children: [" ", detail] })] }));
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Whether the throw is one of this layer's stated refusals rather than a paint failure.
|
|
11
|
+
*
|
|
12
|
+
* A runtime `AxiomError` and the bindings' own named errors are the contract a host programs
|
|
13
|
+
* against; swallowing them would turn a wrong field id into a painted apology. Everything else
|
|
14
|
+
* is an unexpected render error, which the boundary is for.
|
|
15
|
+
*/
|
|
16
|
+
function isBindingRefusal(error) {
|
|
17
|
+
if (error instanceof AxiomError)
|
|
18
|
+
return true;
|
|
19
|
+
return error instanceof Error && error.message.startsWith('@azx-pbc/axiom-react:');
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The last line of defence between a control's render and the page around it.
|
|
23
|
+
*
|
|
24
|
+
* A class is the only way React exposes an error boundary, and this is the lifecycle it exists
|
|
25
|
+
* for. The boundary sits *inside* the provider, so the session — the record, the touched facts,
|
|
26
|
+
* the subscription caches — survives a subtree that threw: only the painted tree is replaced.
|
|
27
|
+
* It is not a substitute for a formatter that refuses to throw; it is what keeps one unexpected
|
|
28
|
+
* message or value from taking the whole page with it.
|
|
29
|
+
*/
|
|
30
|
+
export class FormErrorBoundary extends Component {
|
|
31
|
+
state = { failed: false, error: undefined };
|
|
32
|
+
static getDerivedStateFromError(error) {
|
|
33
|
+
return { failed: true, error };
|
|
34
|
+
}
|
|
35
|
+
componentDidCatch(_error, _info) {
|
|
36
|
+
// React has already been told, and the fallback is the diagnostic. Logging here would
|
|
37
|
+
// duplicate a host's own reporting and would make the library own a channel it does not.
|
|
38
|
+
}
|
|
39
|
+
render() {
|
|
40
|
+
if (!this.state.failed)
|
|
41
|
+
return this.props.children;
|
|
42
|
+
const error = this.state.error;
|
|
43
|
+
// A documented refusal is the binding contract, not a paint failure: an undeclared field, a
|
|
44
|
+
// layout node the artifact does not state, a strict message key. Those propagate to the host
|
|
45
|
+
// that made the mistake, exactly as before. The boundary exists for an unexpected render
|
|
46
|
+
// throw — a host formatter, a value path — where replacing the subtree beats blanking the page.
|
|
47
|
+
if (isBindingRefusal(error))
|
|
48
|
+
throw error;
|
|
49
|
+
return this.props.renderError === undefined
|
|
50
|
+
? defaultRenderError(error)
|
|
51
|
+
: this.props.renderError(error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=FormErrorBoundary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormErrorBoundary.js","sourceRoot":"","sources":["../src/FormErrorBoundary.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAuBpD,qFAAqF;AACrF,SAAS,kBAAkB,CAAC,KAAc;IACxC,MAAM,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACtE,OAAO,CACL,eAAK,IAAI,EAAC,OAAO,4BAAwB,EAAE,2GAEzC,iDAAoC,EAAE,kBAAG,MAAM,IAAQ,IACnD,CACP,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,gBAAgB,CAAC,KAAc;IACtC,IAAI,KAAK,YAAY,UAAU;QAAE,OAAO,IAAI,CAAC;IAC7C,OAAO,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,uBAAuB,CAAC,CAAC;AACrF,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAAwC;IACpE,KAAK,GAAU,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;IAE5D,MAAM,CAAC,wBAAwB,CAAC,KAAc;QAC5C,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IACjC,CAAC;IAEQ,iBAAiB,CAAC,MAAe,EAAE,KAAgB;QAC1D,sFAAsF;QACtF,yFAAyF;IAC3F,CAAC;IAEQ,MAAM;QACb,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;QACnD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC;QAC/B,4FAA4F;QAC5F,6FAA6F;QAC7F,yFAAyF;QACzF,gGAAgG;QAChG,IAAI,gBAAgB,CAAC,KAAK,CAAC;YAAE,MAAM,KAAK,CAAC;QACzC,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,KAAK,SAAS;YACzC,CAAC,CAAC,kBAAkB,CAAC,KAAK,CAAC;YAC3B,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;CACF","sourcesContent":["import { Component } from 'react';\nimport type { ErrorInfo, ReactElement, ReactNode } from 'react';\nimport { AxiomError } from '@azx-pbc/axiom-runtime';\n\n/** What a host may render in place of a tree that threw while painting. */\nexport type FormRenderError = (error: unknown) => ReactNode;\n\n/** The props a provider passes to its render boundary. */\nexport interface FormErrorBoundaryProps {\n readonly children: ReactNode;\n /**\n * The host's fallback for a render that threw.\n *\n * A host that paints a sentence in its own locale, or a retry affordance, states it here. The\n * session is still mounted either way: the values already entered belong to the provider, not\n * to the subtree an error unmounted, so they are not discarded with it.\n */\n readonly renderError?: FormRenderError;\n}\n\ninterface State {\n readonly failed: boolean;\n readonly error: unknown;\n}\n\n/** The fallback a host that supplies none gets: a diagnostic, never a blank page. */\nfunction defaultRenderError(error: unknown): ReactElement {\n const detail = error instanceof Error ? error.message : String(error);\n return (\n <div role=\"alert\" data-form-render-error=\"\">\n This form could not be displayed. The answers already entered are still held by this page.\n <span data-form-render-error-detail=\"\"> {detail}</span>\n </div>\n );\n}\n\n/**\n * Whether the throw is one of this layer's stated refusals rather than a paint failure.\n *\n * A runtime `AxiomError` and the bindings' own named errors are the contract a host programs\n * against; swallowing them would turn a wrong field id into a painted apology. Everything else\n * is an unexpected render error, which the boundary is for.\n */\nfunction isBindingRefusal(error: unknown): boolean {\n if (error instanceof AxiomError) return true;\n return error instanceof Error && error.message.startsWith('@azx-pbc/axiom-react:');\n}\n\n/**\n * The last line of defence between a control's render and the page around it.\n *\n * A class is the only way React exposes an error boundary, and this is the lifecycle it exists\n * for. The boundary sits *inside* the provider, so the session — the record, the touched facts,\n * the subscription caches — survives a subtree that threw: only the painted tree is replaced.\n * It is not a substitute for a formatter that refuses to throw; it is what keeps one unexpected\n * message or value from taking the whole page with it.\n */\nexport class FormErrorBoundary extends Component<FormErrorBoundaryProps, State> {\n override state: State = { failed: false, error: undefined };\n\n static getDerivedStateFromError(error: unknown): State {\n return { failed: true, error };\n }\n\n override componentDidCatch(_error: unknown, _info: ErrorInfo): void {\n // React has already been told, and the fallback is the diagnostic. Logging here would\n // duplicate a host's own reporting and would make the library own a channel it does not.\n }\n\n override render(): ReactNode {\n if (!this.state.failed) return this.props.children;\n const error = this.state.error;\n // A documented refusal is the binding contract, not a paint failure: an undeclared field, a\n // layout node the artifact does not state, a strict message key. Those propagate to the host\n // that made the mistake, exactly as before. The boundary exists for an unexpected render\n // throw — a host formatter, a value path — where replacing the subtree beats blanking the page.\n if (isBindingRefusal(error)) throw error;\n return this.props.renderError === undefined\n ? defaultRenderError(error)\n : this.props.renderError(error);\n }\n}\n"]}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import type { ReactElement, ReactNode } from 'react';
|
|
2
|
+
import type { AccessSource, CancellationPort, CompiledSchema, EvaluationEnv, FieldRef, IdFactory, WireValue } from '@azx-pbc/axiom-runtime';
|
|
3
|
+
import type { OptionLoader, QueryDefer } from './store/option-pages.js';
|
|
4
|
+
import type { FormRenderError } from './FormErrorBoundary.js';
|
|
5
|
+
import type { PublishTrace, WriteProposal } from './form-store.js';
|
|
6
|
+
import type { ShowErrorsPolicy } from './store/visibility.js';
|
|
7
|
+
import type { ValueDisplay } from './render/value-text.js';
|
|
8
|
+
/** The provider props that do not decide the access source. */
|
|
9
|
+
interface FormProviderBaseProps {
|
|
10
|
+
/** The compiled artifact every field, rule and message comes from. */
|
|
11
|
+
readonly compiled: CompiledSchema;
|
|
12
|
+
/**
|
|
13
|
+
* The values the session starts with. The provider owns them from then on.
|
|
14
|
+
*
|
|
15
|
+
* Read once at mount, like the artifact: a new object on a later render is not adopted, and
|
|
16
|
+
* the session keeps the record it opened with. Mount a new provider (a new key) to start a
|
|
17
|
+
* session from the new record, or re-baseline the open session with `reset(values)`.
|
|
18
|
+
*/
|
|
19
|
+
readonly initialValues: Readonly<Record<string, WireValue>>;
|
|
20
|
+
/**
|
|
21
|
+
* The host's source of assigned row ids, `[IDENT-2.1.2]`.
|
|
22
|
+
*
|
|
23
|
+
* Required when the artifact declares an `assigned` array, because Axiom mints no row
|
|
24
|
+
* identity: a constant prefix would make two independent sessions collide when their records
|
|
25
|
+
* are merged, and a process counter would make two renders of one record disagree — an SSR
|
|
26
|
+
* hydration mismatch and a different `reportHash` for the same input. Return an id that has
|
|
27
|
+
* never named a row; the engine refuses a session whose rows share one.
|
|
28
|
+
*
|
|
29
|
+
* `seededIdFactory(seed)` is the deterministic source for a server render: build one over the
|
|
30
|
+
* same seed on the server and in each client render, so a discarded StrictMode mount attempt
|
|
31
|
+
* cannot advance the factory the committed attempt uses. Read once at mount, like
|
|
32
|
+
* `initialValues`.
|
|
33
|
+
*/
|
|
34
|
+
readonly idFactory?: IdFactory;
|
|
35
|
+
/**
|
|
36
|
+
* The identity manifest a resumed record carries, keyed by array `fieldId`.
|
|
37
|
+
*
|
|
38
|
+
* Replaying it keeps the row keys the record was written with, so a draft loaded here
|
|
39
|
+
* addresses the same rows — and seals the same `reportHash` — rather than acquiring fresh
|
|
40
|
+
* ids. Checked against `initialValues`: the engine refuses a manifest that names an id twice
|
|
41
|
+
* or a count the record does not hold. Another session reopening this same record replays the
|
|
42
|
+
* same manifest; an independent record needs distinct ids, or a later merge collides. Read once
|
|
43
|
+
* at mount.
|
|
44
|
+
*/
|
|
45
|
+
readonly rowManifest?: Readonly<Record<string, readonly string[]>>;
|
|
46
|
+
/**
|
|
47
|
+
* The prefix every element id this form emits begins with.
|
|
48
|
+
*
|
|
49
|
+
* A control's `id`, its label's `htmlFor` and its `aria-describedby` come from the binding
|
|
50
|
+
* (`field.a11y.controlId` and friends), and `bindingKey` itself is the same in every provider
|
|
51
|
+
* over one artifact — so the session needs an identity of its own. This defaults to the id
|
|
52
|
+
* `useId()` mints for this mount, which is unique per provider and stable across a server
|
|
53
|
+
* render and hydration. Pass your own when you want readable ids (`"claim-form"`), or when the
|
|
54
|
+
* page has more than one React root (whose `useId()` sequences can otherwise overlap; React's
|
|
55
|
+
* `identifierPrefix` is the other half of that answer). Pass it for a server render that has to
|
|
56
|
+
* match markup produced elsewhere.
|
|
57
|
+
*
|
|
58
|
+
* Read once at mount, like `compiled` and `initialValues`: changing it would re-key every
|
|
59
|
+
* control's id under a label that still names the old one. Mount a new provider (a new `key`) to
|
|
60
|
+
* start a session under a different prefix.
|
|
61
|
+
*/
|
|
62
|
+
readonly idPrefix?: string;
|
|
63
|
+
/** Declared context roots keyed without the `context.` prefix. */
|
|
64
|
+
readonly context?: Readonly<Record<string, unknown>>;
|
|
65
|
+
/** Values the server withheld from this device. */
|
|
66
|
+
readonly redacted?: readonly FieldRef[];
|
|
67
|
+
/** Values written in this session, which a `write_only` field may still read. */
|
|
68
|
+
readonly sessionWrites?: readonly FieldRef[];
|
|
69
|
+
/** The tier and access-overlay version this session evaluates at. */
|
|
70
|
+
readonly env?: EvaluationEnv;
|
|
71
|
+
/**
|
|
72
|
+
* When a control reveals the failures it already evaluated, as one policy for the session.
|
|
73
|
+
*
|
|
74
|
+
* `'touched'` (the default) paints a failure only after the user has visited the binding — a
|
|
75
|
+
* blur or a deliberate write — and only once every operand the rule reads has been supplied.
|
|
76
|
+
* A painted failure stays painted until it is corrected or the session resets. `'always'`
|
|
77
|
+
* paints every decided failure immediately. `'submit'` paints nothing until a submission is
|
|
78
|
+
* attempted, then everything.
|
|
79
|
+
*
|
|
80
|
+
* Display only: every rule is evaluated on every pass in every policy, and the engine's
|
|
81
|
+
* `verdict` is unchanged — a submission is still refused while a failure is still hidden.
|
|
82
|
+
*/
|
|
83
|
+
readonly showErrors?: ShowErrorsPolicy;
|
|
84
|
+
/**
|
|
85
|
+
* The text a host shows in place of a `masked` value, keyed by `fieldId`. It is the host's
|
|
86
|
+
* answer — never derived from the value or its length, which would disclose both.
|
|
87
|
+
*/
|
|
88
|
+
readonly maskedDisplay?: Readonly<Record<string, string>>;
|
|
89
|
+
/**
|
|
90
|
+
* The text a host shows for a value no control edits — a read-only field, a summary readout.
|
|
91
|
+
*
|
|
92
|
+
* It is the host's locale answer for a boolean, a list, a money amount or any other value whose
|
|
93
|
+
* canonical spelling is not display text. The library ships no English fallback: without this
|
|
94
|
+
* port a boolean and a list render as empty text.
|
|
95
|
+
*
|
|
96
|
+
* Give it a stable identity (a module function or `useCallback`), as with any display input: a
|
|
97
|
+
* new function on every render re-derives each binding.
|
|
98
|
+
*/
|
|
99
|
+
readonly valueDisplay?: ValueDisplay;
|
|
100
|
+
/**
|
|
101
|
+
* The host loaders an `optionSource` names, by `sourceId`. A loader is host code acting for
|
|
102
|
+
* the current principal: the engine keys the request by the source, the declared
|
|
103
|
+
* dependencies, the row identity, the record date and the access version, and the API that
|
|
104
|
+
* answers must return only the options this principal may select.
|
|
105
|
+
*
|
|
106
|
+
* Read when the session opens, like the artifact and the starting record: a page list
|
|
107
|
+
* belongs to the session that started it.
|
|
108
|
+
*/
|
|
109
|
+
readonly optionSources?: Readonly<Record<string, OptionLoader>>;
|
|
110
|
+
/**
|
|
111
|
+
* Creates one cancellation port per option page list. The structural default cancels the
|
|
112
|
+
* previous request when the next begins; a host with its own primitive supplies an adapter.
|
|
113
|
+
*/
|
|
114
|
+
readonly cancellation?: () => CancellationPort;
|
|
115
|
+
/**
|
|
116
|
+
* The host's timer, for a search that waits for the reader to stop typing, `[MODEL-3.1.8]`.
|
|
117
|
+
*
|
|
118
|
+
* The bindings read no clock: the same code renders on a server, in a browser and on a device,
|
|
119
|
+
* so the wait between the last edit and the loader call belongs to whoever has one. A search
|
|
120
|
+
* attempted without one is refused before the pages move or a loader is called; a host whose
|
|
121
|
+
* controls never search needs none. Read when the session opens, like `optionSources`.
|
|
122
|
+
*/
|
|
123
|
+
readonly defer?: QueryDefer;
|
|
124
|
+
/**
|
|
125
|
+
* Reports what each publish delivered and what the pass behind it evaluated. A development
|
|
126
|
+
* tool or a test reads this instead of a private counter.
|
|
127
|
+
*/
|
|
128
|
+
readonly onPublish?: (trace: PublishTrace) => void;
|
|
129
|
+
/**
|
|
130
|
+
* Confirms a user write that would add a blocker offering alternatives, `[ENGINE-8.2.1]`.
|
|
131
|
+
*
|
|
132
|
+
* Supplying it turns every user write into a preview: a write whose change set adds a blocker
|
|
133
|
+
* that carries `suggestions` is staged and reported here instead of committed, and the host
|
|
134
|
+
* answers the proposal with `applyProposal` or `discardProposal` from the form handle. A write
|
|
135
|
+
* that adds no such blocker commits immediately, as every write does when this prop is absent.
|
|
136
|
+
*
|
|
137
|
+
* One proposal is pending at a time: a newer qualifying write is reported over the one it
|
|
138
|
+
* supersedes, and the older proposal is then refused with `PROPOSAL_SUPERSEDED`. `useFormState()`
|
|
139
|
+
* states a value-free projection of the pending confirmation, `{ sequence, binding }`, so any
|
|
140
|
+
* component can paint "awaiting confirmation", disable submit and offer a cancel without keeping
|
|
141
|
+
* a second copy; the full proposal to answer is the one this callback delivered. Applying,
|
|
142
|
+
* declining, resetting, unmounting or a newer committing write clears it. The current callback
|
|
143
|
+
* is installed on every commit, so replacing or removing this prop changes the policy the next
|
|
144
|
+
* write meets.
|
|
145
|
+
*/
|
|
146
|
+
readonly onWriteProposed?: (proposal: WriteProposal) => void;
|
|
147
|
+
/**
|
|
148
|
+
* The host's fallback for a descendant that threw while painting.
|
|
149
|
+
*
|
|
150
|
+
* The boundary is inside this provider, so the session keeps the record and the values already
|
|
151
|
+
* typed; only the painted subtree is replaced. A host that supplies none gets a `role="alert"`
|
|
152
|
+
* diagnostic rather than a blank page. This is a safety net: the library's own message and
|
|
153
|
+
* value paths refuse to throw for data the record can legally hold.
|
|
154
|
+
*/
|
|
155
|
+
/**
|
|
156
|
+
* The starting step id for a wizard layout.
|
|
157
|
+
*
|
|
158
|
+
* Seeds the session's shared wizard cursor at construction (render 0 on both server and client),
|
|
159
|
+
* so `<RenderedForm>` and every sibling `useWizard()` caller agree on the initial step in SSR
|
|
160
|
+
* HTML without waiting for a post-hydration layout effect. Read once at mount, like
|
|
161
|
+
* `initialValues`.
|
|
162
|
+
*/
|
|
163
|
+
readonly initialStepId?: string | undefined;
|
|
164
|
+
readonly renderError?: FormRenderError;
|
|
165
|
+
readonly children?: ReactNode;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The one access source a provider may state.
|
|
169
|
+
*
|
|
170
|
+
* A host resolves access once, either into the runtime's 4-level `overlay` or into the flattened
|
|
171
|
+
* per-reference `access` projection, and both answer the same question. Stating both leaves two
|
|
172
|
+
* answers that cannot be reconciled — either could be the stale one — so this is a union rather
|
|
173
|
+
* than two optional members: a TypeScript host that supplies both is refused by the compiler, and
|
|
174
|
+
* `assertOneAccessSource` refuses the same call from JavaScript. A provider that states neither
|
|
175
|
+
* keeps the fail-closed default in which every field resolves to `read_only`.
|
|
176
|
+
*
|
|
177
|
+
* It is the runtime's `AccessSource`, the same declaration `EvaluationInput` and
|
|
178
|
+
* `SubmissionAuthority` use, so the rule has one spelling across the two packages.
|
|
179
|
+
*/
|
|
180
|
+
type FormProviderAccess = AccessSource;
|
|
181
|
+
/** Everything a form session is opened with. */
|
|
182
|
+
export type FormProviderProps = FormProviderBaseProps & FormProviderAccess;
|
|
183
|
+
/**
|
|
184
|
+
* Opens one form session and makes it available to the hooks below it.
|
|
185
|
+
*
|
|
186
|
+
* The session is created once per mount: values written by a control belong to it, so a host
|
|
187
|
+
* that needs a different starting record mounts a new provider — a new `key` is the React way to
|
|
188
|
+
* say so. Every other input is a live prop; changing access, redaction, session writes, context,
|
|
189
|
+
* environment or the display rules re-derives the session and keeps the values entered so far.
|
|
190
|
+
*
|
|
191
|
+
* The render derives a candidate and provides it; the commit phase adopts it. A render React
|
|
192
|
+
* discards — a suspended transition, a superseded update — therefore mutates nothing, and a
|
|
193
|
+
* write issued from props captured before that commit is judged by the committed session.
|
|
194
|
+
*/
|
|
195
|
+
export declare function FormProvider(props: FormProviderProps): ReactElement;
|
|
196
|
+
export {};
|
|
197
|
+
//# sourceMappingURL=FormProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormProvider.d.ts","sourceRoot":"","sources":["../src/FormProvider.tsx"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACrD,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EAChB,cAAc,EACd,aAAa,EACb,QAAQ,EACR,SAAS,EACT,SAAS,EACV,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAIxE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAE9D,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAKnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D,+DAA+D;AAC/D,UAAU,qBAAqB;IAC7B,sEAAsE;IACtE,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;IAClC;;;;;;OAMG;IACH,QAAQ,CAAC,aAAa,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IAC5D;;;;;;;;;;;;;OAaG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,C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AAQ,EAAE,aAAa,KAAK,IAAI,CAAC;IAC7D;;;;;;;OAOG;IACH;;;;;;;OAOG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5C,QAAQ,CAAC,WAAW,CAAC,EAAE,eAAe,CAAC;IACvC,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC;CAC/B;AAED;;;;;;;;;;;;GAYG;AACH,KAAK,kBAAkB,GAAG,YAAY,CAAC;AAEvC,gDAAgD;AAChD,MAAM,MAAM,iBAAiB,GAAG,qBAAqB,GAAG,kBAAkB,CAAC;AAE3E;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,YAAY,CAmGnE"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useId, useInsertionEffect, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
3
|
+
import { FormContext } from './form-context.js';
|
|
4
|
+
import { FormErrorBoundary } from './FormErrorBoundary.js';
|
|
5
|
+
import { FormStore } from './form-store.js';
|
|
6
|
+
import { WizardCursor } from './store/wizard-cursor.js';
|
|
7
|
+
import { assertOneAccessSource } from './store/inputs.js';
|
|
8
|
+
import { DEFAULT_POLICY } from './store/visibility.js';
|
|
9
|
+
/**
|
|
10
|
+
* Opens one form session and makes it available to the hooks below it.
|
|
11
|
+
*
|
|
12
|
+
* The session is created once per mount: values written by a control belong to it, so a host
|
|
13
|
+
* that needs a different starting record mounts a new provider — a new `key` is the React way to
|
|
14
|
+
* say so. Every other input is a live prop; changing access, redaction, session writes, context,
|
|
15
|
+
* environment or the display rules re-derives the session and keeps the values entered so far.
|
|
16
|
+
*
|
|
17
|
+
* The render derives a candidate and provides it; the commit phase adopts it. A render React
|
|
18
|
+
* discards — a suspended transition, a superseded update — therefore mutates nothing, and a
|
|
19
|
+
* write issued from props captured before that commit is judged by the committed session.
|
|
20
|
+
*/
|
|
21
|
+
export function FormProvider(props) {
|
|
22
|
+
// One access source, checked before a session is opened: either could be the stale answer.
|
|
23
|
+
assertOneAccessSource(props.overlay, props.access);
|
|
24
|
+
// One element-id prefix per mount: the host's when it names one, otherwise the id React mints
|
|
25
|
+
// for this component instance. `useId()` is the measured hydration-safe source (the server HTML
|
|
26
|
+
// and the hydrated client agree byte for byte), and it is unique per provider, which is what
|
|
27
|
+
// keeps two forms over one artifact from emitting the same `id`/`htmlFor`/`aria-describedby`.
|
|
28
|
+
const generatedId = useId();
|
|
29
|
+
const [idPrefix] = useState(() => props.idPrefix ?? generatedId);
|
|
30
|
+
// The initialiser runs once, so the store keeps the session's values across re-renders of the
|
|
31
|
+
// provider while the controls below it subscribe to that one store.
|
|
32
|
+
const [store] = useState(() => new FormStore(storeConfig(props), idPrefix));
|
|
33
|
+
// One cursor per session, not per `useWizard` call: a header and a footer that both ask the
|
|
34
|
+
// provider for the wizard are two readers of one step, one history and one answer. Seeding at
|
|
35
|
+
// construction guarantees SSR HTML and client render 0 agree across all sibling `useWizard` calls;
|
|
36
|
+
// an undeclared or stale `initialStepId` is ignored so callers never split-brain or get stuck.
|
|
37
|
+
const [wizard] = useState(() => new WizardCursor(validatedInitialStepId(props.compiled.layout, props.initialStepId)));
|
|
38
|
+
if (store.compiled !== props.compiled) {
|
|
39
|
+
// A different artifact has different fields and rules, so the values entered against the old
|
|
40
|
+
// one are not a record of the new one. Failing here names the fix instead of silently
|
|
41
|
+
// evaluating the host's new props against the artifact the session holds.
|
|
42
|
+
throw new Error('@azx-pbc/axiom-react: FormProvider received a different compiled artifact; mount a new provider (a new key) so the session starts from the new artifact');
|
|
43
|
+
}
|
|
44
|
+
const display = useMemo(() => ({
|
|
45
|
+
reveal: props.showErrors ?? DEFAULT_POLICY,
|
|
46
|
+
maskedDisplay: props.maskedDisplay,
|
|
47
|
+
valueDisplay: props.valueDisplay,
|
|
48
|
+
idPrefix,
|
|
49
|
+
}), [props.showErrors, props.maskedDisplay, props.valueDisplay, idPrefix]);
|
|
50
|
+
// Derived during this render, before the controls below read their snapshots: a control that
|
|
51
|
+
// renders with the previous access is a form that only looks locked. No committed state is
|
|
52
|
+
// assigned, so a render React abandons leaves the committed session untouched.
|
|
53
|
+
const candidate = store.preview({
|
|
54
|
+
...(props.context === undefined ? {} : { context: props.context }),
|
|
55
|
+
...(props.overlay === undefined ? {} : { overlay: props.overlay }),
|
|
56
|
+
...(props.access === undefined ? {} : { access: props.access }),
|
|
57
|
+
...(props.redacted === undefined ? {} : { redacted: props.redacted }),
|
|
58
|
+
...(props.sessionWrites === undefined ? {} : { sessionWrites: props.sessionWrites }),
|
|
59
|
+
...(props.env === undefined ? {} : { env: props.env }),
|
|
60
|
+
}, display);
|
|
61
|
+
// Insertion effects run before child layout effects. Adopting the candidate and installing the
|
|
62
|
+
// interceptor here is what makes a child that writes from its own layout effect answer to this
|
|
63
|
+
// commit's access and confirmation policy, rather than the previous commit's. No notification
|
|
64
|
+
// is scheduled in this phase.
|
|
65
|
+
const publishOnLayout = useRef(false);
|
|
66
|
+
useInsertionEffect(() => {
|
|
67
|
+
publishOnLayout.current = store.commit(candidate);
|
|
68
|
+
store.interceptWrites(props.onWriteProposed);
|
|
69
|
+
return () => {
|
|
70
|
+
store.interceptWrites(undefined);
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
// Delivered after the commit and before paint. The observer is installed first, so the publish
|
|
74
|
+
// this render causes reaches *this* render's callback rather than the previous render's. The
|
|
75
|
+
// publish tells a control that bailed out of this render to re-read its snapshot in time to
|
|
76
|
+
// replace a value the host just revoked. A commit that adopted nothing publishes nothing, so an
|
|
77
|
+
// unrelated re-render does not re-derive every control's props. The observer stays registered
|
|
78
|
+
// after the commit because a write inside an event handler publishes before the tree renders again.
|
|
79
|
+
useLayoutEffect(() => {
|
|
80
|
+
store.observePublishes(props.onPublish);
|
|
81
|
+
const shouldPublish = publishOnLayout.current;
|
|
82
|
+
publishOnLayout.current = false;
|
|
83
|
+
if (shouldPublish)
|
|
84
|
+
store.publish();
|
|
85
|
+
return () => {
|
|
86
|
+
store.observePublishes(undefined);
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
// The session's mount state, so a submission that settles after unmount publishes nothing.
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
store.mount();
|
|
92
|
+
return () => {
|
|
93
|
+
store.unmount();
|
|
94
|
+
};
|
|
95
|
+
}, [store]);
|
|
96
|
+
// The session identity is observed: a new object would re-render every context consumer even
|
|
97
|
+
// when neither the store nor the candidate changed.
|
|
98
|
+
const session = useMemo(() => ({ store, candidate, wizard }), [store, candidate, wizard]);
|
|
99
|
+
return (_jsx(FormContext, { value: session, children: _jsx(FormErrorBoundary, { ...(props.renderError === undefined ? {} : { renderError: props.renderError }), children: props.children }) }));
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The provider's props as the store's opening config, with the one access arm that is present.
|
|
103
|
+
*
|
|
104
|
+
* The runtime spells an absent access source `?: undefined` rather than `?: never`, which is the
|
|
105
|
+
* same absence to every reader and what a JavaScript caller writes when it spreads an optional
|
|
106
|
+
* prop. Under `exactOptionalPropertyTypes` that spelling is not assignable to the store's optional
|
|
107
|
+
* `access?: readonly AccessEntry[]`, though: an explicitly-undefined member is not an absent one.
|
|
108
|
+
* The provider is the boundary that owns the union, so it states the arm the host supplied and
|
|
109
|
+
* omits the other rather than handing the store a member it cannot hold.
|
|
110
|
+
*/
|
|
111
|
+
function storeConfig(props) {
|
|
112
|
+
const base = {
|
|
113
|
+
compiled: props.compiled,
|
|
114
|
+
initialValues: props.initialValues,
|
|
115
|
+
...(props.idFactory === undefined ? {} : { idFactory: props.idFactory }),
|
|
116
|
+
...(props.rowManifest === undefined ? {} : { rowManifest: props.rowManifest }),
|
|
117
|
+
...(props.context === undefined ? {} : { context: props.context }),
|
|
118
|
+
...(props.redacted === undefined ? {} : { redacted: props.redacted }),
|
|
119
|
+
...(props.sessionWrites === undefined ? {} : { sessionWrites: props.sessionWrites }),
|
|
120
|
+
...(props.env === undefined ? {} : { env: props.env }),
|
|
121
|
+
...(props.optionSources === undefined ? {} : { optionSources: props.optionSources }),
|
|
122
|
+
...(props.cancellation === undefined ? {} : { cancellation: props.cancellation }),
|
|
123
|
+
...(props.defer === undefined ? {} : { defer: props.defer }),
|
|
124
|
+
};
|
|
125
|
+
if (props.overlay !== undefined)
|
|
126
|
+
return { ...base, overlay: props.overlay };
|
|
127
|
+
if (props.access !== undefined)
|
|
128
|
+
return { ...base, access: props.access };
|
|
129
|
+
return base;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Returns `initialStepId` only when the artifact's wizard declares it, or `undefined` otherwise.
|
|
133
|
+
*
|
|
134
|
+
* Ignoring undeclared or stale step ids keeps the shared cursor unseeded so sibling `useWizard()`
|
|
135
|
+
* callers agree on the fallback step and `next()` / `previous()` never read a non-existent origin.
|
|
136
|
+
*/
|
|
137
|
+
function validatedInitialStepId(layout, initialStepId) {
|
|
138
|
+
if (initialStepId === undefined || layout === undefined)
|
|
139
|
+
return undefined;
|
|
140
|
+
for (const node of layout) {
|
|
141
|
+
if (node.kind === 'wizard') {
|
|
142
|
+
return node.children.some((step) => step.stepId === initialStepId)
|
|
143
|
+
? initialStepId
|
|
144
|
+
: undefined;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=FormProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormProvider.js","sourceRoot":"","sources":["../src/FormProvider.tsx"],"names":[],"mappings":";AAAA,OAAO,EACL,SAAS,EACT,KAAK,EACL,kBAAkB,EAClB,eAAe,EACf,OAAO,EACP,MAAM,EACN,QAAQ,GACT,MAAM,OAAO,CAAC;AAYf,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AA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ffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport type { ReactElement, ReactNode } from 'react';\nimport type {\n AccessSource,\n CancellationPort,\n CompiledSchema,\n EvaluationEnv,\n FieldRef,\n IdFactory,\n WireValue,\n} from '@azx-pbc/axiom-runtime';\nimport type { OptionLoader, QueryDefer } from './store/option-pages.js';\nimport { FormContext } from './form-context.js';\nimport type { FormSession } from './form-context.js';\nimport { FormErrorBoundary } from './FormErrorBoundary.js';\nimport type { FormRenderError } from './FormErrorBoundary.js';\nimport { FormStore } from './form-store.js';\nimport type { PublishTrace, WriteProposal } from './form-store.js';\nimport { WizardCursor } from './store/wizard-cursor.js';\nimport { assertOneAccessSource } from './store/inputs.js';\nimport type { FormStoreConfig } from './store/inputs.js';\nimport type { DisplayOptions } from './store/derive.js';\nimport type { ShowErrorsPolicy } from './store/visibility.js';\nimport { DEFAULT_POLICY } from './store/visibility.js';\nimport type { ValueDisplay } from './render/value-text.js';\n\n/** The provider props that do not decide the access source. */\ninterface FormProviderBaseProps {\n /** The compiled artifact every field, rule and message comes from. */\n readonly compiled: CompiledSchema;\n /**\n * The values the session starts with. The provider owns them from then on.\n *\n * Read once at mount, like the artifact: a new object on a later render is not adopted, and\n * the session keeps the record it opened with. Mount a new provider (a new key) to start a\n * session from the new record, or re-baseline the open session with `reset(values)`.\n */\n readonly initialValues: Readonly<Record<string, WireValue>>;\n /**\n * The host's source of assigned row ids, `[IDENT-2.1.2]`.\n *\n * Required when the artifact declares an `assigned` array, because Axiom mints no row\n * identity: a constant prefix would make two independent sessions collide when their records\n * are merged, and a process counter would make two renders of one record disagree — an SSR\n * hydration mismatch and a different `reportHash` for the same input. Return an id that has\n * never named a row; the engine refuses a session whose rows share one.\n *\n * `seededIdFactory(seed)` is the deterministic source for a server render: build one over the\n * same seed on the server and in each client render, so a discarded StrictMode mount attempt\n * cannot advance the factory the committed attempt uses. Read once at mount, like\n * `initialValues`.\n */\n readonly idFactory?: IdFactory;\n /**\n * The identity manifest a resumed record carries, keyed by array `fieldId`.\n *\n * Replaying it keeps the row keys the record was written with, so a draft loaded here\n * addresses the same rows — and seals the same `reportHash` — rather than acquiring fresh\n * ids. Checked against `initialValues`: the engine refuses a manifest that names an id twice\n * or a count the record does not hold. Another session reopening this same record replays the\n * same manifest; an independent record needs distinct ids, or a later merge collides. Read once\n * at mount.\n */\n readonly rowManifest?: Readonly<Record<string, readonly string[]>>;\n /**\n * The prefix every element id this form emits begins with.\n *\n * A control's `id`, its label's `htmlFor` and its `aria-describedby` come from the binding\n * (`field.a11y.controlId` and friends), and `bindingKey` itself is the same in every provider\n * over one artifact — so the session needs an identity of its own. This defaults to the id\n * `useId()` mints for this mount, which is unique per provider and stable across a server\n * render and hydration. Pass your own when you want readable ids (`\"claim-form\"`), or when the\n * page has more than one React root (whose `useId()` sequences can otherwise overlap; React's\n * `identifierPrefix` is the other half of that answer). Pass it for a server render that has to\n * match markup produced elsewhere.\n *\n * Read once at mount, like `compiled` and `initialValues`: changing it would re-key every\n * control's id under a label that still names the old one. Mount a new provider (a new `key`) to\n * start a session under a different prefix.\n */\n readonly idPrefix?: string;\n /** Declared context roots keyed without the `context.` prefix. */\n readonly context?: Readonly<Record<string, unknown>>;\n /** Values the server withheld from this device. */\n readonly redacted?: readonly FieldRef[];\n /** Values written in this session, which a `write_only` field may still read. */\n readonly sessionWrites?: readonly FieldRef[];\n /** The tier and access-overlay version this session evaluates at. */\n readonly env?: EvaluationEnv;\n /**\n * When a control reveals the failures it already evaluated, as one policy for the session.\n *\n * `'touched'` (the default) paints a failure only after the user has visited the binding — a\n * blur or a deliberate write — and only once every operand the rule reads has been supplied.\n * A painted failure stays painted until it is corrected or the session resets. `'always'`\n * paints every decided failure immediately. `'submit'` paints nothing until a submission is\n * attempted, then everything.\n *\n * Display only: every rule is evaluated on every pass in every policy, and the engine's\n * `verdict` is unchanged — a submission is still refused while a failure is still hidden.\n */\n readonly showErrors?: ShowErrorsPolicy;\n /**\n * The text a host shows in place of a `masked` value, keyed by `fieldId`. It is the host's\n * answer — never derived from the value or its length, which would disclose both.\n */\n readonly maskedDisplay?: Readonly<Record<string, string>>;\n /**\n * The text a host shows for a value no control edits — a read-only field, a summary readout.\n *\n * It is the host's locale answer for a boolean, a list, a money amount or any other value whose\n * canonical spelling is not display text. The library ships no English fallback: without this\n * port a boolean and a list render as empty text.\n *\n * Give it a stable identity (a module function or `useCallback`), as with any display input: a\n * new function on every render re-derives each binding.\n */\n readonly valueDisplay?: ValueDisplay;\n /**\n * The host loaders an `optionSource` names, by `sourceId`. A loader is host code acting for\n * the current principal: the engine keys the request by the source, the declared\n * dependencies, the row identity, the record date and the access version, and the API that\n * answers must return only the options this principal may select.\n *\n * Read when the session opens, like the artifact and the starting record: a page list\n * belongs to the session that started it.\n */\n readonly optionSources?: Readonly<Record<string, OptionLoader>>;\n /**\n * Creates one cancellation port per option page list. The structural default cancels the\n * previous request when the next begins; a host with its own primitive supplies an adapter.\n */\n readonly cancellation?: () => CancellationPort;\n /**\n * The host's timer, for a search that waits for the reader to stop typing, `[MODEL-3.1.8]`.\n *\n * The bindings read no clock: the same code renders on a server, in a browser and on a device,\n * so the wait between the last edit and the loader call belongs to whoever has one. A search\n * attempted without one is refused before the pages move or a loader is called; a host whose\n * controls never search needs none. Read when the session opens, like `optionSources`.\n */\n readonly defer?: QueryDefer;\n /**\n * Reports what each publish delivered and what the pass behind it evaluated. A development\n * tool or a test reads this instead of a private counter.\n */\n readonly onPublish?: (trace: PublishTrace) => void;\n /**\n * Confirms a user write that would add a blocker offering alternatives, `[ENGINE-8.2.1]`.\n *\n * Supplying it turns every user write into a preview: a write whose change set adds a blocker\n * that carries `suggestions` is staged and reported here instead of committed, and the host\n * answers the proposal with `applyProposal` or `discardProposal` from the form handle. A write\n * that adds no such blocker commits immediately, as every write does when this prop is absent.\n *\n * One proposal is pending at a time: a newer qualifying write is reported over the one it\n * supersedes, and the older proposal is then refused with `PROPOSAL_SUPERSEDED`. `useFormState()`\n * states a value-free projection of the pending confirmation, `{ sequence, binding }`, so any\n * component can paint \"awaiting confirmation\", disable submit and offer a cancel without keeping\n * a second copy; the full proposal to answer is the one this callback delivered. Applying,\n * declining, resetting, unmounting or a newer committing write clears it. The current callback\n * is installed on every commit, so replacing or removing this prop changes the policy the next\n * write meets.\n */\n readonly onWriteProposed?: (proposal: WriteProposal) => void;\n /**\n * The host's fallback for a descendant that threw while painting.\n *\n * The boundary is inside this provider, so the session keeps the record and the values already\n * typed; only the painted subtree is replaced. A host that supplies none gets a `role=\"alert\"`\n * diagnostic rather than a blank page. This is a safety net: the library's own message and\n * value paths refuse to throw for data the record can legally hold.\n */\n /**\n * The starting step id for a wizard layout.\n *\n * Seeds the session's shared wizard cursor at construction (render 0 on both server and client),\n * so `<RenderedForm>` and every sibling `useWizard()` caller agree on the initial step in SSR\n * HTML without waiting for a post-hydration layout effect. Read once at mount, like\n * `initialValues`.\n */\n readonly initialStepId?: string | undefined;\n readonly renderError?: FormRenderError;\n readonly children?: ReactNode;\n}\n\n/**\n * The one access source a provider may state.\n *\n * A host resolves access once, either into the runtime's 4-level `overlay` or into the flattened\n * per-reference `access` projection, and both answer the same question. Stating both leaves two\n * answers that cannot be reconciled — either could be the stale one — so this is a union rather\n * than two optional members: a TypeScript host that supplies both is refused by the compiler, and\n * `assertOneAccessSource` refuses the same call from JavaScript. A provider that states neither\n * keeps the fail-closed default in which every field resolves to `read_only`.\n *\n * It is the runtime's `AccessSource`, the same declaration `EvaluationInput` and\n * `SubmissionAuthority` use, so the rule has one spelling across the two packages.\n */\ntype FormProviderAccess = AccessSource;\n\n/** Everything a form session is opened with. */\nexport type FormProviderProps = FormProviderBaseProps & FormProviderAccess;\n\n/**\n * Opens one form session and makes it available to the hooks below it.\n *\n * The session is created once per mount: values written by a control belong to it, so a host\n * that needs a different starting record mounts a new provider — a new `key` is the React way to\n * say so. Every other input is a live prop; changing access, redaction, session writes, context,\n * environment or the display rules re-derives the session and keeps the values entered so far.\n *\n * The render derives a candidate and provides it; the commit phase adopts it. A render React\n * discards — a suspended transition, a superseded update — therefore mutates nothing, and a\n * write issued from props captured before that commit is judged by the committed session.\n */\nexport function FormProvider(props: FormProviderProps): ReactElement {\n // One access source, checked before a session is opened: either could be the stale answer.\n assertOneAccessSource(props.overlay, props.access);\n // One element-id prefix per mount: the host's when it names one, otherwise the id React mints\n // for this component instance. `useId()` is the measured hydration-safe source (the server HTML\n // and the hydrated client agree byte for byte), and it is unique per provider, which is what\n // keeps two forms over one artifact from emitting the same `id`/`htmlFor`/`aria-describedby`.\n const generatedId = useId();\n const [idPrefix] = useState(() => props.idPrefix ?? generatedId);\n // The initialiser runs once, so the store keeps the session's values across re-renders of the\n // provider while the controls below it subscribe to that one store.\n const [store] = useState(() => new FormStore(storeConfig(props), idPrefix));\n // One cursor per session, not per `useWizard` call: a header and a footer that both ask the\n // provider for the wizard are two readers of one step, one history and one answer. Seeding at\n // construction guarantees SSR HTML and client render 0 agree across all sibling `useWizard` calls;\n // an undeclared or stale `initialStepId` is ignored so callers never split-brain or get stuck.\n const [wizard] = useState(\n () => new WizardCursor(validatedInitialStepId(props.compiled.layout, props.initialStepId)),\n );\n if (store.compiled !== props.compiled) {\n // A different artifact has different fields and rules, so the values entered against the old\n // one are not a record of the new one. Failing here names the fix instead of silently\n // evaluating the host's new props against the artifact the session holds.\n throw new Error(\n '@azx-pbc/axiom-react: FormProvider received a different compiled artifact; mount a new provider (a new key) so the session starts from the new artifact',\n );\n }\n const display: DisplayOptions = useMemo(\n () => ({\n reveal: props.showErrors ?? DEFAULT_POLICY,\n maskedDisplay: props.maskedDisplay,\n valueDisplay: props.valueDisplay,\n idPrefix,\n }),\n [props.showErrors, props.maskedDisplay, props.valueDisplay, idPrefix],\n );\n // Derived during this render, before the controls below read their snapshots: a control that\n // renders with the previous access is a form that only looks locked. No committed state is\n // assigned, so a render React abandons leaves the committed session untouched.\n const candidate = store.preview(\n {\n ...(props.context === undefined ? {} : { context: props.context }),\n ...(props.overlay === undefined ? {} : { overlay: props.overlay }),\n ...(props.access === undefined ? {} : { access: props.access }),\n ...(props.redacted === undefined ? {} : { redacted: props.redacted }),\n ...(props.sessionWrites === undefined ? {} : { sessionWrites: props.sessionWrites }),\n ...(props.env === undefined ? {} : { env: props.env }),\n },\n display,\n );\n // Insertion effects run before child layout effects. Adopting the candidate and installing the\n // interceptor here is what makes a child that writes from its own layout effect answer to this\n // commit's access and confirmation policy, rather than the previous commit's. No notification\n // is scheduled in this phase.\n const publishOnLayout = useRef(false);\n useInsertionEffect(() => {\n publishOnLayout.current = store.commit(candidate);\n store.interceptWrites(props.onWriteProposed);\n return () => {\n store.interceptWrites(undefined);\n };\n });\n // Delivered after the commit and before paint. The observer is installed first, so the publish\n // this render causes reaches *this* render's callback rather than the previous render's. The\n // publish tells a control that bailed out of this render to re-read its snapshot in time to\n // replace a value the host just revoked. A commit that adopted nothing publishes nothing, so an\n // unrelated re-render does not re-derive every control's props. The observer stays registered\n // after the commit because a write inside an event handler publishes before the tree renders again.\n useLayoutEffect(() => {\n store.observePublishes(props.onPublish);\n const shouldPublish = publishOnLayout.current;\n publishOnLayout.current = false;\n if (shouldPublish) store.publish();\n return () => {\n store.observePublishes(undefined);\n };\n });\n // The session's mount state, so a submission that settles after unmount publishes nothing.\n useEffect(() => {\n store.mount();\n return (): void => {\n store.unmount();\n };\n }, [store]);\n // The session identity is observed: a new object would re-render every context consumer even\n // when neither the store nor the candidate changed.\n const session: FormSession = useMemo(\n () => ({ store, candidate, wizard }),\n [store, candidate, wizard],\n );\n return (\n <FormContext value={session}>\n <FormErrorBoundary\n {...(props.renderError === undefined ? {} : { renderError: props.renderError })}\n >\n {props.children}\n </FormErrorBoundary>\n </FormContext>\n );\n}\n\n/**\n * The provider's props as the store's opening config, with the one access arm that is present.\n *\n * The runtime spells an absent access source `?: undefined` rather than `?: never`, which is the\n * same absence to every reader and what a JavaScript caller writes when it spreads an optional\n * prop. Under `exactOptionalPropertyTypes` that spelling is not assignable to the store's optional\n * `access?: readonly AccessEntry[]`, though: an explicitly-undefined member is not an absent one.\n * The provider is the boundary that owns the union, so it states the arm the host supplied and\n * omits the other rather than handing the store a member it cannot hold.\n */\nfunction storeConfig(props: FormProviderProps): FormStoreConfig {\n const base: FormStoreConfig = {\n compiled: props.compiled,\n initialValues: props.initialValues,\n ...(props.idFactory === undefined ? {} : { idFactory: props.idFactory }),\n ...(props.rowManifest === undefined ? {} : { rowManifest: props.rowManifest }),\n ...(props.context === undefined ? {} : { context: props.context }),\n ...(props.redacted === undefined ? {} : { redacted: props.redacted }),\n ...(props.sessionWrites === undefined ? {} : { sessionWrites: props.sessionWrites }),\n ...(props.env === undefined ? {} : { env: props.env }),\n ...(props.optionSources === undefined ? {} : { optionSources: props.optionSources }),\n ...(props.cancellation === undefined ? {} : { cancellation: props.cancellation }),\n ...(props.defer === undefined ? {} : { defer: props.defer }),\n };\n if (props.overlay !== undefined) return { ...base, overlay: props.overlay };\n if (props.access !== undefined) return { ...base, access: props.access };\n return base;\n}\n\n/**\n * Returns `initialStepId` only when the artifact's wizard declares it, or `undefined` otherwise.\n *\n * Ignoring undeclared or stale step ids keeps the shared cursor unseeded so sibling `useWizard()`\n * callers agree on the fallback step and `next()` / `previous()` never read a non-existent origin.\n */\nfunction validatedInitialStepId(\n layout: CompiledSchema['layout'],\n initialStepId: string | undefined,\n): string | undefined {\n if (initialStepId === undefined || layout === undefined) return undefined;\n for (const node of layout) {\n if (node.kind === 'wizard') {\n return node.children.some((step) => step.stepId === initialStepId)\n ? initialStepId\n : undefined;\n }\n }\n return undefined;\n}\n"]}
|