@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,70 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useSyncExternalStore } from 'react';
|
|
2
|
+
import { asyncOptionListOf } from './store/option-pages.js';
|
|
3
|
+
import { useFormSession } from './useField.js';
|
|
4
|
+
/** Pages a picker requests when the host states none; a bounded page, never the whole domain. */
|
|
5
|
+
export const DEFAULT_OPTION_PAGE_SIZE = 50;
|
|
6
|
+
/**
|
|
7
|
+
* A host-loaded option domain for one field or one row of an array, `[MODEL-3.1.8]`.
|
|
8
|
+
*
|
|
9
|
+
* The pages are keyed by the basis the artifact declares — the source, the current values of
|
|
10
|
+
* its `dependsOn` fields, the session record date and the access version — plus the binding's
|
|
11
|
+
* row identity, so editing one row's dependency loads that row and no sibling, and a reorder
|
|
12
|
+
* loads nothing. A change to any member of the basis starts exactly one load through the same
|
|
13
|
+
* path a manual `reload` takes. The reader's search text is the one key `search` owns: it is
|
|
14
|
+
* published at the edit, drops the pages it narrows, and asks the loader once the text settles.
|
|
15
|
+
*
|
|
16
|
+
* Staleness has one answer: beginning a request cancels the previous signal, so a response
|
|
17
|
+
* that arrives after a newer request began — or after unmount — commits nothing on either the
|
|
18
|
+
* resolve or the reject arm. Eligibility is not this hook's answer; the selected value's
|
|
19
|
+
* `invalidValue` is derived once by the session from the loaded domain.
|
|
20
|
+
*/
|
|
21
|
+
export function useAsyncOptions(fieldId, rowKey, pageSize = DEFAULT_OPTION_PAGE_SIZE) {
|
|
22
|
+
const { store } = useFormSession('useAsyncOptions');
|
|
23
|
+
// The controls one list is handed, created once per binding without touching the store:
|
|
24
|
+
// the memo only closes over the binding, so no render assigns into the store and every
|
|
25
|
+
// render reads one identity. The live entry controls resolve when the host calls
|
|
26
|
+
// load-more or reload — before the retain effect runs there is no entry, so those calls
|
|
27
|
+
// no-op through the same guards a held list would answer with.
|
|
28
|
+
const controls = useMemo(() => ({
|
|
29
|
+
loadMore: () => {
|
|
30
|
+
store.optionControls(fieldId, rowKey).loadMore();
|
|
31
|
+
},
|
|
32
|
+
reload: () => {
|
|
33
|
+
store.optionControls(fieldId, rowKey).reload();
|
|
34
|
+
},
|
|
35
|
+
search: (query) => {
|
|
36
|
+
store.searchOptions(fieldId, rowKey, query);
|
|
37
|
+
},
|
|
38
|
+
}), [store, fieldId, rowKey]);
|
|
39
|
+
const subscribe = useCallback((onStoreChange) => {
|
|
40
|
+
// Every value the page key depends on wakes this consumer, so a dependency change
|
|
41
|
+
// re-renders the picker even when no page has arrived yet. The record date and the
|
|
42
|
+
// access version are in the same key and belong to no binding, so the session states
|
|
43
|
+
// their move separately.
|
|
44
|
+
const releases = store
|
|
45
|
+
.optionDependencyRefs(fieldId, rowKey)
|
|
46
|
+
.map((ref) => store.subscribe(ref.fieldId, ref.rowKey, onStoreChange));
|
|
47
|
+
releases.push(store.subscribeOptionBasis(onStoreChange));
|
|
48
|
+
releases.push(store.subscribeOptionPages(onStoreChange));
|
|
49
|
+
return () => {
|
|
50
|
+
for (const release of releases)
|
|
51
|
+
release();
|
|
52
|
+
};
|
|
53
|
+
}, [store, fieldId, rowKey]);
|
|
54
|
+
// The snapshot reads the basis itself rather than closing over the render's copy: a
|
|
55
|
+
// subscription notification must be able to answer with the basis that moved, and a captured
|
|
56
|
+
// one would return the object it already handed to React and bail out of the re-render.
|
|
57
|
+
const getSnapshot = useCallback(() => {
|
|
58
|
+
const basis = store.optionBasis(fieldId, rowKey);
|
|
59
|
+
return asyncOptionListOf(store.optionState(fieldId, rowKey), controls, basis.signature, basis.readable);
|
|
60
|
+
}, [store, fieldId, rowKey, controls]);
|
|
61
|
+
const list = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
62
|
+
useEffect(() => store.retainOptions(fieldId, rowKey), [store, fieldId, rowKey]);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
// Every committed picker render ensures the current basis. The page machine no-ops while it
|
|
65
|
+
// holds, so page/loading renders do not start another request.
|
|
66
|
+
store.ensureOptions(fieldId, rowKey, pageSize);
|
|
67
|
+
});
|
|
68
|
+
return list;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=useAsyncOptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAsyncOptions.js","sourceRoot":"","sources":["../src/useAsyncOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAC9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,iGAAiG;AACjG,MAAM,CAAC,MAAM,wBAAwB,GAAG,EAAE,CAAC;AAE3C;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,eAAe,CAC7B,OAAe,EACf,MAAwB,EACxB,QAAQ,GAAW,wBAAwB;IAE3C,MAAM,EAAE,KAAK,EAAE,GAAG,cAAc,CAAC,iBAAiB,CAAC,CAAC;IACpD,wFAAwF;IACxF,uFAAuF;IACvF,iFAAiF;IACjF,wFAAwF;IACxF,+DAA+D;IAC/D,MAAM,QAAQ,GAAG,OAAO,CACtB,GAAmB,EAAE,CAAC,CAAC;QACrB,QAAQ,EAAE,GAAS,EAAE;YACnB,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,QAAQ,EAAE,CAAC;QACnD,CAAC;QACD,MAAM,EAAE,GAAS,EAAE;YACjB,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,MAAM,EAAE,CAAC;QACjD,CAAC;QACD,MAAM,EAAE,CAAC,KAAa,EAAQ,EAAE;YAC9B,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;QAC9C,CAAC;KACF,CAAC,EACF,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,CACzB,CAAC;IACF,MAAM,SAAS,GAAG,WAAW,CAC3B,CAAC,aAAyB,E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E,MAAM,CAAC,CAAC,CAAC;IAChF,SAAS,CAAC,GAAG,EAAE;QACb,4FAA4F;QAC5F,+DAA+D;QAC/D,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC,CAAC,CAAC;IACH,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import { useCallback, useEffect, useMemo, useSyncExternalStore } from 'react';\nimport { asyncOptionListOf } from './store/option-pages.js';\nimport type { AsyncOptionList, OptionControls } from './store/option-pages.js';\nimport { useFormSession } from './useField.js';\n\n/** Pages a picker requests when the host states none; a bounded page, never the whole domain. */\nexport const DEFAULT_OPTION_PAGE_SIZE = 50;\n\n/**\n * A host-loaded option domain for one field or one row of an array, `[MODEL-3.1.8]`.\n *\n * The pages are keyed by the basis the artifact declares — the source, the current values of\n * its `dependsOn` fields, the session record date and the access version — plus the binding's\n * row identity, so editing one row's dependency loads that row and no sibling, and a reorder\n * loads nothing. A change to any member of the basis starts exactly one load through the same\n * path a manual `reload` takes. The reader's search text is the one key `search` owns: it is\n * published at the edit, drops the pages it narrows, and asks the loader once the text settles.\n *\n * Staleness has one answer: beginning a request cancels the previous signal, so a response\n * that arrives after a newer request began — or after unmount — commits nothing on either the\n * resolve or the reject arm. Eligibility is not this hook's answer; the selected value's\n * `invalidValue` is derived once by the session from the loaded domain.\n */\nexport function useAsyncOptions(\n fieldId: string,\n rowKey?: string | number,\n pageSize: number = DEFAULT_OPTION_PAGE_SIZE,\n): AsyncOptionList {\n const { store } = useFormSession('useAsyncOptions');\n // The controls one list is handed, created once per binding without touching the store:\n // the memo only closes over the binding, so no render assigns into the store and every\n // render reads one identity. The live entry controls resolve when the host calls\n // load-more or reload — before the retain effect runs there is no entry, so those calls\n // no-op through the same guards a held list would answer with.\n const controls = useMemo(\n (): OptionControls => ({\n loadMore: (): void => {\n store.optionControls(fieldId, rowKey).loadMore();\n },\n reload: (): void => {\n store.optionControls(fieldId, rowKey).reload();\n },\n search: (query: string): void => {\n store.searchOptions(fieldId, rowKey, query);\n },\n }),\n [store, fieldId, rowKey],\n );\n const subscribe = useCallback(\n (onStoreChange: () => void): (() => void) => {\n // Every value the page key depends on wakes this consumer, so a dependency change\n // re-renders the picker even when no page has arrived yet. The record date and the\n // access version are in the same key and belong to no binding, so the session states\n // their move separately.\n const releases = store\n .optionDependencyRefs(fieldId, rowKey)\n .map((ref) => store.subscribe(ref.fieldId, ref.rowKey, onStoreChange));\n releases.push(store.subscribeOptionBasis(onStoreChange));\n releases.push(store.subscribeOptionPages(onStoreChange));\n return (): void => {\n for (const release of releases) release();\n };\n },\n [store, fieldId, rowKey],\n );\n // The snapshot reads the basis itself rather than closing over the render's copy: a\n // subscription notification must be able to answer with the basis that moved, and a captured\n // one would return the object it already handed to React and bail out of the re-render.\n const getSnapshot = useCallback((): AsyncOptionList => {\n const basis = store.optionBasis(fieldId, rowKey);\n return asyncOptionListOf(\n store.optionState(fieldId, rowKey),\n controls,\n basis.signature,\n basis.readable,\n );\n }, [store, fieldId, rowKey, controls]);\n const list = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n useEffect(() => store.retainOptions(fieldId, rowKey), [store, fieldId, rowKey]);\n useEffect(() => {\n // Every committed picker render ensures the current basis. The page machine no-ops while it\n // holds, so page/loading renders do not start another request.\n store.ensureOptions(fieldId, rowKey, pageSize);\n });\n return list;\n}\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { BufferedFormat, BufferRefusalReason, NumericSeparators } from './store/parse-print.js';
|
|
2
|
+
/** What a buffered control renders, and the ways it hands the session a value. */
|
|
3
|
+
export interface BufferedInput {
|
|
4
|
+
/** The text on screen: the user's entry, or the committed value printed by the format. */
|
|
5
|
+
readonly text: string;
|
|
6
|
+
/** Whether the text holds an entry the committed value does not state. */
|
|
7
|
+
readonly dirty: boolean;
|
|
8
|
+
readonly parseKind: 'value' | 'partial' | 'invalid';
|
|
9
|
+
/**
|
|
10
|
+
* A stable message key for why the entry cannot be a value, or `undefined` when it can.
|
|
11
|
+
*
|
|
12
|
+
* The layer has no locale, so this is a key a host translates through its own catalogue —
|
|
13
|
+
* never an English sentence. `parseErrorParams` carries the values the message interpolates.
|
|
14
|
+
*/
|
|
15
|
+
readonly parseError: BufferRefusalReason | undefined;
|
|
16
|
+
/** The values `parseError` interpolates, when it names any. */
|
|
17
|
+
readonly parseErrorParams: Readonly<Record<string, string | number>> | undefined;
|
|
18
|
+
/** Takes one keystroke: a complete entry writes immediately, and commit reconciles a partial or invalid draft. */
|
|
19
|
+
readonly setText: (text: string) => void;
|
|
20
|
+
/** Writes the entry now if it is a value; a partial or refused one writes nothing. */
|
|
21
|
+
readonly commit: () => void;
|
|
22
|
+
/**
|
|
23
|
+
* Commits the entry and records that the binding was visited.
|
|
24
|
+
*
|
|
25
|
+
* A control binds this to `onBlur` so the host's `onBlur`/`onTouched` display rule can
|
|
26
|
+
* reveal the failure this control already holds. `commit` stays the Enter key's answer: a
|
|
27
|
+
* confirmed entry is written without claiming the user left the field.
|
|
28
|
+
*/
|
|
29
|
+
readonly onBlur: () => void;
|
|
30
|
+
/** Forgets the entry and shows the committed value again. */
|
|
31
|
+
readonly reset: () => void;
|
|
32
|
+
}
|
|
33
|
+
/** The host's options for one buffered control. */
|
|
34
|
+
export interface BufferedInputOptions {
|
|
35
|
+
/**
|
|
36
|
+
* The locale whose decimal and grouping marks the entry uses.
|
|
37
|
+
*
|
|
38
|
+
* It changes only which text a user's keystroke is read as: `125,50` is one hundred and
|
|
39
|
+
* twenty-five and a half under `es` and `de`, while `125.5` is the same amount under `en`.
|
|
40
|
+
* The value written is always the canonical wire spelling. The marks are the layer's own
|
|
41
|
+
* vendored table, not the host's locale database. Omit it for the canonical `.` decimal and
|
|
42
|
+
* `,` group.
|
|
43
|
+
*/
|
|
44
|
+
readonly locale?: string;
|
|
45
|
+
/** The marks to use when the host has a convention the vendored table does not carry. */
|
|
46
|
+
readonly separators?: NumericSeparators;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A text control whose raw keystrokes are not the value, `[MODEL-7.1.1]`.
|
|
50
|
+
*
|
|
51
|
+
* The committed wire value is authoritative. `12.`, `-`, `+` and `2026-0` are entries a
|
|
52
|
+
* control passes through and the wire cannot hold, so they are kept as text and never written
|
|
53
|
+
* as `null` or `0`; a complete entry is parsed and written canonically. An external update
|
|
54
|
+
* resyncs the text when the buffer was clean and leaves a dirty entry exactly as typed.
|
|
55
|
+
*
|
|
56
|
+
* The format is the field's: `money` reads its `(currency, scale)` from the declaration, so a
|
|
57
|
+
* host supplies no per-field format and two currencies cannot share a parser. `options.locale`
|
|
58
|
+
* names the punctuation the control reads; it never changes the value's canonical form.
|
|
59
|
+
*
|
|
60
|
+
* The buffer belongs to the binding, not the component instance: a control reused for another
|
|
61
|
+
* field or row starts from that binding's committed value rather than carrying the previous
|
|
62
|
+
* field's text into it. A complete entry is already written, and an uncommitted partial or
|
|
63
|
+
* invalid draft is abandoned on unmount.
|
|
64
|
+
*
|
|
65
|
+
* It is never the answer for a `masked` binding: there is no value to start from, and every
|
|
66
|
+
* keystroke would be refused. A control renders `field.maskedText` as static text instead.
|
|
67
|
+
*/
|
|
68
|
+
export declare function useBufferedInput(fieldId: string, format: BufferedFormat, rowKey?: string | number, options?: BufferedInputOptions): BufferedInput;
|
|
69
|
+
//# sourceMappingURL=useBufferedInput.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useBufferedInput.d.ts","sourceRoot":"","sources":["../src/useBufferedInput.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EACV,cAAc,EACd,mBAAmB,EAEnB,iBAAiB,EAClB,MAAM,wBAAwB,CAAC;AAIhC,kFAAkF;AAClF,MAAM,WAAW,aAAa;IAC5B,0FAA0F;IAC1F,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0EAA0E;IAC1E,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS,CAAC;IACpD;;;;;OAKG;IACH,QAAQ,CAAC,UAAU,EAAE,mBAAmB,GAAG,SAAS,CAAC;IACrD,+DAA+D;IAC/D,QAAQ,CAAC,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC;IACjF,kHAAkH;IAClH,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IACzC,sFAAsF;IACtF,QAAQ,CAAC,MAAM,EAAE,MAAM,IAAI,CAAC;IAC5B;;;;;;OAMG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,IAAI,CAAC;IAC5B,6DAA6D;IAC7D,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAC;CAC5B;AAwDD,mDAAmD;AACnD,MAAM,WAAW,oBAAoB;IACnC;;;;;;;;OAQG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,yFAAyF;IACzF,QAAQ,CAAC,UAAU,CAAC,EAAE,iBAAiB,CAAC;CACzC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,cAAc,EACtB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EACxB,OAAO,CAAC,EAAE,oBAAoB,GAC7B,aAAa,CA8Ef"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from 'react';
|
|
2
|
+
import { bufferParse, bufferText, editBuffer, entryDirty, startBuffer, } from './store/buffer-state.js';
|
|
3
|
+
import { bindingKey } from './store/bindings.js';
|
|
4
|
+
import { formatContractOf, parseEntry } from './store/parse-print.js';
|
|
5
|
+
import { useField } from './useField.js';
|
|
6
|
+
function createBufferStore(key, value, contract) {
|
|
7
|
+
let held = { key, state: startBuffer(value, contract) };
|
|
8
|
+
const listeners = new Set();
|
|
9
|
+
return {
|
|
10
|
+
subscribe: (listener) => {
|
|
11
|
+
listeners.add(listener);
|
|
12
|
+
return () => {
|
|
13
|
+
listeners.delete(listener);
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
snapshot: () => held,
|
|
17
|
+
retire: (nextKey) => {
|
|
18
|
+
// A keystroke may have landed in the new binding before this commit's effect ran; the
|
|
19
|
+
// entry it wrote already names `nextKey` and is the one to keep.
|
|
20
|
+
if (held === null || held.key === nextKey)
|
|
21
|
+
return;
|
|
22
|
+
held = null;
|
|
23
|
+
for (const listener of listeners)
|
|
24
|
+
listener();
|
|
25
|
+
},
|
|
26
|
+
put: (next) => {
|
|
27
|
+
held = next;
|
|
28
|
+
for (const listener of listeners)
|
|
29
|
+
listener();
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A text control whose raw keystrokes are not the value, `[MODEL-7.1.1]`.
|
|
35
|
+
*
|
|
36
|
+
* The committed wire value is authoritative. `12.`, `-`, `+` and `2026-0` are entries a
|
|
37
|
+
* control passes through and the wire cannot hold, so they are kept as text and never written
|
|
38
|
+
* as `null` or `0`; a complete entry is parsed and written canonically. An external update
|
|
39
|
+
* resyncs the text when the buffer was clean and leaves a dirty entry exactly as typed.
|
|
40
|
+
*
|
|
41
|
+
* The format is the field's: `money` reads its `(currency, scale)` from the declaration, so a
|
|
42
|
+
* host supplies no per-field format and two currencies cannot share a parser. `options.locale`
|
|
43
|
+
* names the punctuation the control reads; it never changes the value's canonical form.
|
|
44
|
+
*
|
|
45
|
+
* The buffer belongs to the binding, not the component instance: a control reused for another
|
|
46
|
+
* field or row starts from that binding's committed value rather than carrying the previous
|
|
47
|
+
* field's text into it. A complete entry is already written, and an uncommitted partial or
|
|
48
|
+
* invalid draft is abandoned on unmount.
|
|
49
|
+
*
|
|
50
|
+
* It is never the answer for a `masked` binding: there is no value to start from, and every
|
|
51
|
+
* keystroke would be refused. A control renders `field.maskedText` as static text instead.
|
|
52
|
+
*/
|
|
53
|
+
export function useBufferedInput(fieldId, format, rowKey, options) {
|
|
54
|
+
const field = useField(fieldId, rowKey);
|
|
55
|
+
const contract = formatContractOf(field.descriptor, format, options?.locale, options?.separators);
|
|
56
|
+
const key = bindingKey(fieldId, rowKey);
|
|
57
|
+
const [store] = useState(() => createBufferStore(key, field.value, contract));
|
|
58
|
+
const held = useSyncExternalStore(store.subscribe, store.snapshot, store.snapshot);
|
|
59
|
+
// The binding itself answers whether the held entry is still the one to show. A changed
|
|
60
|
+
// binding is a fresh start derived during render, so no state is written while rendering.
|
|
61
|
+
const state = held !== null && held.key === key ? held.state : startBuffer(field.value, contract);
|
|
62
|
+
// The entry the previous binding owned is retired once the changed binding commits, so
|
|
63
|
+
// returning to it shows its committed value rather than the abandoned entry. The render above
|
|
64
|
+
// already shows the current binding from its committed value; this keeps the stored entry
|
|
65
|
+
// consistent with it.
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
store.retire(key);
|
|
68
|
+
}, [store, key]);
|
|
69
|
+
// Call-time reads for the callbacks below. A submit handler a host captured on an earlier
|
|
70
|
+
// render acts on the entry and the committed value as they stand when invoked, not on the
|
|
71
|
+
// render that created the callback, so the callbacks read the store and this record
|
|
72
|
+
// instead of closing over the render. The writers ride in the same record, while the
|
|
73
|
+
// binding key still scopes each callback: one captured for another binding commits nothing.
|
|
74
|
+
const callTime = {
|
|
75
|
+
committed: field.value,
|
|
76
|
+
contract,
|
|
77
|
+
writeValue: field.setValue,
|
|
78
|
+
markVisited: field.onBlur,
|
|
79
|
+
};
|
|
80
|
+
const latest = useRef(callTime);
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
latest.current = callTime;
|
|
83
|
+
});
|
|
84
|
+
const text = bufferText(state, field.value, contract);
|
|
85
|
+
const parsed = bufferParse(state, field.value, contract);
|
|
86
|
+
const commit = useCallback(() => {
|
|
87
|
+
const current = store.snapshot();
|
|
88
|
+
if (current === null || current.key !== key)
|
|
89
|
+
return;
|
|
90
|
+
const { committed, contract: currentContract, writeValue } = latest.current;
|
|
91
|
+
const currentText = bufferText(current.state, committed, currentContract);
|
|
92
|
+
const entry = parseEntry(currentText, currentContract);
|
|
93
|
+
if (entry.kind !== 'value' || !entryDirty(entry, committed))
|
|
94
|
+
return;
|
|
95
|
+
writeValue(entry.value);
|
|
96
|
+
store.put({ key, state: editBuffer(currentText, entry.value, currentContract) });
|
|
97
|
+
}, [store, key]);
|
|
98
|
+
return {
|
|
99
|
+
text,
|
|
100
|
+
dirty: entryDirty(parsed, field.value),
|
|
101
|
+
parseKind: parsed.kind,
|
|
102
|
+
parseError: parsed.kind === 'invalid' ? parsed.reason : undefined,
|
|
103
|
+
parseErrorParams: parsed.kind === 'invalid' ? parsed.params : undefined,
|
|
104
|
+
setText: useCallback((next) => {
|
|
105
|
+
const { committed, contract: currentContract, writeValue } = latest.current;
|
|
106
|
+
const entry = parseEntry(next, currentContract);
|
|
107
|
+
if (entry.kind === 'value') {
|
|
108
|
+
if (entryDirty(entry, committed))
|
|
109
|
+
writeValue(entry.value);
|
|
110
|
+
// The value the write installed is the buffer's new baseline, so a later external
|
|
111
|
+
// update is a change to a clean buffer and replaces the text rather than being
|
|
112
|
+
// invisible until the next blur writes the stale entry back.
|
|
113
|
+
store.put({ key, state: editBuffer(next, entry.value, currentContract) });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
store.put({ key, state: editBuffer(next, committed, currentContract) });
|
|
117
|
+
}, [store, key]),
|
|
118
|
+
commit,
|
|
119
|
+
onBlur: useCallback(() => {
|
|
120
|
+
// A blur both flushes the entry and records the visit: the second is what the host's
|
|
121
|
+
// `onBlur`/`onTouched` display rule reads, and the field's own handler was replaced by
|
|
122
|
+
// this control's `onBlur`.
|
|
123
|
+
commit();
|
|
124
|
+
latest.current.markVisited();
|
|
125
|
+
}, [commit]),
|
|
126
|
+
reset: useCallback(() => {
|
|
127
|
+
const { committed, contract: currentContract } = latest.current;
|
|
128
|
+
store.put({ key, state: startBuffer(committed, currentContract) });
|
|
129
|
+
}, [store, key]),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=useBufferedInput.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useBufferedInput.js","sourceRoot":"","sources":["../src/useBufferedInput.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AACvF,OAAO,EACL,WAAW,EACX,UAAU,EACV,UAAU,EACV,UAAU,EACV,WAAW,GACZ,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAQtE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AA2DzC,SAAS,iBAAiB,CACxB,GAAW,EACX,KAA4B,EAC5B,QAAwB;IAExB,IAAI,IAAI,GAAsB,EAAE,GAAG,EAAE,KAAK,EAAE,WAAW,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,CAAC;IAC3E,MAAM,SAAS,GAAG,IAAI,GAAG,EAAc,CAAC;IACxC,OAAO;QACL,SAAS,EAAE,CAAC,QAAQ,EAAE,EAAE;YACtB,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YACxB,OAAO,GAAS,EAAE;gBAChB,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAC7B,CAAC,CAAC;QACJ,CAAC;QACD,QAAQ,EAAE,GAAG,EAAE,CAAC,IAAI;QACpB,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE;YAClB,sFAAsF;YACtF,iEAAiE;YACjE,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,GAAG,KAAK,OAA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E,UAAU,CAAC,IAAI,EAAE,SAAS,EAAE,eAAe,CAAC,EAAE,CAAC,CAAC;QAC1E,CAAC,EACD,CAAC,KAAK,EAAE,GAAG,CAAC,CACb;QACD,MAAM;QACN,MAAM,EAAE,WAAW,CAAC,GAAS,EAAE;YAC7B,qFAAqF;YACrF,uFAAuF;YACvF,2BAA2B;YAC3B,MAAM,EAAE,CAAC;YACT,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;QAC/B,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC;QACZ,KAAK,EAAE,WAAW,CAAC,GAAS,EAAE;YAC5B,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,eAAe,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC;YAChE,KAAK,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,WAAW,CAAC,SAAS,EAAE,eAAe,CAAC,EAAE,CAAC,CAAC;QACrE,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;KACjB,CAAC;AACJ,CAAC","sourcesContent":["import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from 'react';\nimport {\n bufferParse,\n bufferText,\n editBuffer,\n entryDirty,\n startBuffer,\n} from './store/buffer-state.js';\nimport type { BufferState } from './store/buffer-state.js';\nimport { bindingKey } from './store/bindings.js';\nimport { formatContractOf, parseEntry } from './store/parse-print.js';\nimport type {\n BufferedFormat,\n BufferRefusalReason,\n FormatContract,\n NumericSeparators,\n} from './store/parse-print.js';\nimport type { WireValue } from '@azx-pbc/axiom-runtime';\nimport { useField } from './useField.js';\n\n/** What a buffered control renders, and the ways it hands the session a value. */\nexport interface BufferedInput {\n /** The text on screen: the user's entry, or the committed value printed by the format. */\n readonly text: string;\n /** Whether the text holds an entry the committed value does not state. */\n readonly dirty: boolean;\n readonly parseKind: 'value' | 'partial' | 'invalid';\n /**\n * A stable message key for why the entry cannot be a value, or `undefined` when it can.\n *\n * The layer has no locale, so this is a key a host translates through its own catalogue —\n * never an English sentence. `parseErrorParams` carries the values the message interpolates.\n */\n readonly parseError: BufferRefusalReason | undefined;\n /** The values `parseError` interpolates, when it names any. */\n readonly parseErrorParams: Readonly<Record<string, string | number>> | undefined;\n /** Takes one keystroke: a complete entry writes immediately, and commit reconciles a partial or invalid draft. */\n readonly setText: (text: string) => void;\n /** Writes the entry now if it is a value; a partial or refused one writes nothing. */\n readonly commit: () => void;\n /**\n * Commits the entry and records that the binding was visited.\n *\n * A control binds this to `onBlur` so the host's `onBlur`/`onTouched` display rule can\n * reveal the failure this control already holds. `commit` stays the Enter key's answer: a\n * confirmed entry is written without claiming the user left the field.\n */\n readonly onBlur: () => void;\n /** Forgets the entry and shows the committed value again. */\n readonly reset: () => void;\n}\n\n/** The buffer one binding owns, held beside the binding it was started for. */\ninterface HeldBuffer {\n readonly key: string;\n readonly state: BufferState;\n}\n\n/**\n * The entry one control holds, and the binding it was entered for.\n *\n * The entry is kept outside React state so a binding change can retire it after the commit:\n * render stays read-only, and an entry kept in state would be shown again when the control\n * returned to the binding it was typed for.\n */\ninterface BufferStore {\n readonly subscribe: (listener: () => void) => () => void;\n readonly snapshot: () => HeldBuffer | null;\n /**\n * Forgets an entry that names another binding, so leaving a binding abandons the entry typed\n * for it and coming back starts from that binding's committed value.\n */\n readonly retire: (key: string) => void;\n /** Keeps an entry for the binding the control currently names. */\n readonly put: (held: HeldBuffer) => void;\n}\n\nfunction createBufferStore(\n key: string,\n value: WireValue | undefined,\n contract: FormatContract,\n): BufferStore {\n let held: HeldBuffer | null = { key, state: startBuffer(value, contract) };\n const listeners = new Set<() => void>();\n return {\n subscribe: (listener) => {\n listeners.add(listener);\n return (): void => {\n listeners.delete(listener);\n };\n },\n snapshot: () => held,\n retire: (nextKey) => {\n // A keystroke may have landed in the new binding before this commit's effect ran; the\n // entry it wrote already names `nextKey` and is the one to keep.\n if (held === null || held.key === nextKey) return;\n held = null;\n for (const listener of listeners) listener();\n },\n put: (next) => {\n held = next;\n for (const listener of listeners) listener();\n },\n };\n}\n\n/** The host's options for one buffered control. */\nexport interface BufferedInputOptions {\n /**\n * The locale whose decimal and grouping marks the entry uses.\n *\n * It changes only which text a user's keystroke is read as: `125,50` is one hundred and\n * twenty-five and a half under `es` and `de`, while `125.5` is the same amount under `en`.\n * The value written is always the canonical wire spelling. The marks are the layer's own\n * vendored table, not the host's locale database. Omit it for the canonical `.` decimal and\n * `,` group.\n */\n readonly locale?: string;\n /** The marks to use when the host has a convention the vendored table does not carry. */\n readonly separators?: NumericSeparators;\n}\n\n/**\n * A text control whose raw keystrokes are not the value, `[MODEL-7.1.1]`.\n *\n * The committed wire value is authoritative. `12.`, `-`, `+` and `2026-0` are entries a\n * control passes through and the wire cannot hold, so they are kept as text and never written\n * as `null` or `0`; a complete entry is parsed and written canonically. An external update\n * resyncs the text when the buffer was clean and leaves a dirty entry exactly as typed.\n *\n * The format is the field's: `money` reads its `(currency, scale)` from the declaration, so a\n * host supplies no per-field format and two currencies cannot share a parser. `options.locale`\n * names the punctuation the control reads; it never changes the value's canonical form.\n *\n * The buffer belongs to the binding, not the component instance: a control reused for another\n * field or row starts from that binding's committed value rather than carrying the previous\n * field's text into it. A complete entry is already written, and an uncommitted partial or\n * invalid draft is abandoned on unmount.\n *\n * It is never the answer for a `masked` binding: there is no value to start from, and every\n * keystroke would be refused. A control renders `field.maskedText` as static text instead.\n */\nexport function useBufferedInput(\n fieldId: string,\n format: BufferedFormat,\n rowKey?: string | number,\n options?: BufferedInputOptions,\n): BufferedInput {\n const field = useField(fieldId, rowKey);\n const contract = formatContractOf(field.descriptor, format, options?.locale, options?.separators);\n const key = bindingKey(fieldId, rowKey);\n const [store] = useState(() => createBufferStore(key, field.value, contract));\n const held = useSyncExternalStore(store.subscribe, store.snapshot, store.snapshot);\n // The binding itself answers whether the held entry is still the one to show. A changed\n // binding is a fresh start derived during render, so no state is written while rendering.\n const state = held !== null && held.key === key ? held.state : startBuffer(field.value, contract);\n // The entry the previous binding owned is retired once the changed binding commits, so\n // returning to it shows its committed value rather than the abandoned entry. The render above\n // already shows the current binding from its committed value; this keeps the stored entry\n // consistent with it.\n useEffect(() => {\n store.retire(key);\n }, [store, key]);\n // Call-time reads for the callbacks below. A submit handler a host captured on an earlier\n // render acts on the entry and the committed value as they stand when invoked, not on the\n // render that created the callback, so the callbacks read the store and this record\n // instead of closing over the render. The writers ride in the same record, while the\n // binding key still scopes each callback: one captured for another binding commits nothing.\n const callTime = {\n committed: field.value,\n contract,\n writeValue: field.setValue,\n markVisited: field.onBlur,\n };\n const latest = useRef(callTime);\n useEffect(() => {\n latest.current = callTime;\n });\n const text = bufferText(state, field.value, contract);\n const parsed = bufferParse(state, field.value, contract);\n const commit = useCallback((): void => {\n const current = store.snapshot();\n if (current === null || current.key !== key) return;\n const { committed, contract: currentContract, writeValue } = latest.current;\n const currentText = bufferText(current.state, committed, currentContract);\n const entry = parseEntry(currentText, currentContract);\n if (entry.kind !== 'value' || !entryDirty(entry, committed)) return;\n writeValue(entry.value);\n store.put({ key, state: editBuffer(currentText, entry.value, currentContract) });\n }, [store, key]);\n return {\n text,\n dirty: entryDirty(parsed, field.value),\n parseKind: parsed.kind,\n parseError: parsed.kind === 'invalid' ? parsed.reason : undefined,\n parseErrorParams: parsed.kind === 'invalid' ? parsed.params : undefined,\n setText: useCallback(\n (next: string): void => {\n const { committed, contract: currentContract, writeValue } = latest.current;\n const entry = parseEntry(next, currentContract);\n if (entry.kind === 'value') {\n if (entryDirty(entry, committed)) writeValue(entry.value);\n // The value the write installed is the buffer's new baseline, so a later external\n // update is a change to a clean buffer and replaces the text rather than being\n // invisible until the next blur writes the stale entry back.\n store.put({ key, state: editBuffer(next, entry.value, currentContract) });\n return;\n }\n store.put({ key, state: editBuffer(next, committed, currentContract) });\n },\n [store, key],\n ),\n commit,\n onBlur: useCallback((): void => {\n // A blur both flushes the entry and records the visit: the second is what the host's\n // `onBlur`/`onTouched` display rule reads, and the field's own handler was replaced by\n // this control's `onBlur`.\n commit();\n latest.current.markVisited();\n }, [commit]),\n reset: useCallback((): void => {\n const { committed, contract: currentContract } = latest.current;\n store.put({ key, state: startBuffer(committed, currentContract) });\n }, [store, key]),\n };\n}\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ArtifactShape, FieldIdsOf, TypedArtifact } from '@azx-pbc/axiom-runtime';
|
|
2
|
+
import type { PublishedFieldProps } from './field-props.js';
|
|
3
|
+
import type { FormSession } from './form-context.js';
|
|
4
|
+
import type { FieldBindingArgs, TypedFieldProps } from './typed.js';
|
|
5
|
+
/**
|
|
6
|
+
* The session the nearest provider owns, refusing a hook called outside one rather than
|
|
7
|
+
* inventing a default field state.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useFormSession(hook: string): FormSession;
|
|
10
|
+
/**
|
|
11
|
+
* The session a typed binding reads, refusing a definition the provider did not compile.
|
|
12
|
+
*
|
|
13
|
+
* The typed door's promise — this id is declared, this row key is the right kind — is read from
|
|
14
|
+
* the definition's artifact, so a provider rendering a different document would make the type a
|
|
15
|
+
* claim about the wrong schema. The ids are matched by `schemaId` rather than object identity: a
|
|
16
|
+
* provider may be handed any compile of the same document, and a recompiled artifact keeps its id.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useFormSessionFor(definition: TypedArtifact<ArtifactShape> | undefined, hook: string): FormSession;
|
|
19
|
+
/**
|
|
20
|
+
* The evaluated state of one binding — a field, or one row of a field — re-read whenever that
|
|
21
|
+
* binding's state changes.
|
|
22
|
+
*
|
|
23
|
+
* The props come from the candidate the current render derived, so the access, enabled and
|
|
24
|
+
* value a control shows are the ones its inputs produce before the commit adopts them. Once a
|
|
25
|
+
* write moves the session past that candidate, the committed store answers instead, which is
|
|
26
|
+
* how a control woken by a store notification sees the committed value.
|
|
27
|
+
*
|
|
28
|
+
* `useSyncExternalStore` compares snapshots by identity, and the store returns the same
|
|
29
|
+
* `FieldProps` object until that binding's published state changes. That identity is what stops
|
|
30
|
+
* the React 19 StrictMode double render from looping, and it keeps a write to one binding from
|
|
31
|
+
* re-rendering a control bound to another.
|
|
32
|
+
*
|
|
33
|
+
* Two doors share this hook. The typed door takes the artifact `defineDocument` returns and
|
|
34
|
+
* accepts only a declared field id; a row key is stated exactly where the declaration holds rows.
|
|
35
|
+
* The untyped door — `useField(fieldId, rowKey)` — is unchanged, for a host that holds a runtime
|
|
36
|
+
* artifact and names no types. Both read the same binding, and the runtime judges both: an
|
|
37
|
+
* undeclared id, a missing row key on an array member and a row key on a field without rows are
|
|
38
|
+
* refusals either way.
|
|
39
|
+
*/
|
|
40
|
+
export declare function useField<Shape extends ArtifactShape, Id extends FieldIdsOf<Shape>>(definition: TypedArtifact<Shape>, fieldId: Id, ...rowKey: FieldBindingArgs<Shape, Id>): TypedFieldProps<Shape, Id>;
|
|
41
|
+
export declare function useField(fieldId: string, rowKey?: string | number): PublishedFieldProps;
|
|
42
|
+
//# sourceMappingURL=useField.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useField.d.ts","sourceRoot":"","sources":["../src/useField.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvF,OAAO,KAAK,EAAc,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAExE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAEpE;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAMxD;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,UAAU,EAAE,aAAa,CAAC,aAAa,CAAC,GAAG,SAAS,EACpD,IAAI,EAAE,MAAM,GACX,WAAW,CAeb;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,QAAQ,CAAC,KAAK,SAAS,aAAa,EAAE,EAAE,SAAS,UAAU,CAAC,KAAK,CAAC,EAChF,UAAU,EAAE,aAAa,CAAC,KAAK,CAAC,EAChC,OAAO,EAAE,EAAE,EACX,GAAG,MAAM,EAAE,gBAAgB,CAAC,KAAK,EAAE,EAAE,CAAC,GACrC,eAAe,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;AAC9B,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,mBAAmB,CAAC"}
|
package/dist/useField.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useCallback, useContext, useSyncExternalStore } from 'react';
|
|
2
|
+
import { AxiomError } from '@azx-pbc/axiom-runtime';
|
|
3
|
+
import { FormContext, isDevelopmentBuild } from './form-context.js';
|
|
4
|
+
/**
|
|
5
|
+
* The session the nearest provider owns, refusing a hook called outside one rather than
|
|
6
|
+
* inventing a default field state.
|
|
7
|
+
*/
|
|
8
|
+
export function useFormSession(hook) {
|
|
9
|
+
const session = useContext(FormContext);
|
|
10
|
+
if (session === null) {
|
|
11
|
+
throw new Error(`@azx-pbc/axiom-react: ${hook} must be called inside a FormProvider`);
|
|
12
|
+
}
|
|
13
|
+
return session;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The session a typed binding reads, refusing a definition the provider did not compile.
|
|
17
|
+
*
|
|
18
|
+
* The typed door's promise — this id is declared, this row key is the right kind — is read from
|
|
19
|
+
* the definition's artifact, so a provider rendering a different document would make the type a
|
|
20
|
+
* claim about the wrong schema. The ids are matched by `schemaId` rather than object identity: a
|
|
21
|
+
* provider may be handed any compile of the same document, and a recompiled artifact keeps its id.
|
|
22
|
+
*/
|
|
23
|
+
export function useFormSessionFor(definition, hook) {
|
|
24
|
+
const session = useFormSession(hook);
|
|
25
|
+
if (definition === undefined)
|
|
26
|
+
return session;
|
|
27
|
+
const rendered = session.store.compiled;
|
|
28
|
+
if (definition.schemaId !== rendered.schemaId ||
|
|
29
|
+
definition.schemaVersion !== rendered.schemaVersion) {
|
|
30
|
+
throw new AxiomError('ENGINE_ERROR', `${hook} is bound to schema ${definition.schemaId} version ${String(definition.schemaVersion)}, but the provider renders ${rendered.schemaId} version ${String(rendered.schemaVersion)}; mount the FormProvider with the definition the bindings name`, 'schemaId');
|
|
31
|
+
}
|
|
32
|
+
return session;
|
|
33
|
+
}
|
|
34
|
+
export function useField(definitionOrFieldId, fieldIdOrRowKey, rowKey) {
|
|
35
|
+
const typed = typeof definitionOrFieldId !== 'string';
|
|
36
|
+
const fieldId = typed ? fieldIdOrRowKey : definitionOrFieldId;
|
|
37
|
+
const bindingRowKey = typed ? rowKey : fieldIdOrRowKey;
|
|
38
|
+
if (typeof fieldId !== 'string') {
|
|
39
|
+
throw new Error('@azx-pbc/axiom-react: useField must name a field id');
|
|
40
|
+
}
|
|
41
|
+
return useFieldBinding(fieldId, bindingRowKey, typed ? definitionOrFieldId : undefined);
|
|
42
|
+
}
|
|
43
|
+
/** The one binding implementation both doors read. */
|
|
44
|
+
function useFieldBinding(fieldId, rowKey, definition) {
|
|
45
|
+
const { store, candidate } = useFormSessionFor(definition, 'useField');
|
|
46
|
+
if (definition === undefined &&
|
|
47
|
+
rowKey !== undefined &&
|
|
48
|
+
typeof rowKey === 'string' &&
|
|
49
|
+
isDevelopmentBuild() &&
|
|
50
|
+
store.declaresField(fieldId) &&
|
|
51
|
+
!store.declaresRows(fieldId) &&
|
|
52
|
+
store.declaresField(rowKey)) {
|
|
53
|
+
throw new Error(`@azx-pbc/axiom-react: useField(${JSON.stringify(fieldId)}, ${JSON.stringify(rowKey)}) passes a declared field id where a row key belongs; the untyped door reads (fieldId, rowKey), so either drop the row key or call the typed door useField(definition, fieldId, rowKey)`);
|
|
54
|
+
}
|
|
55
|
+
const subscribe = useCallback((onStoreChange) => store.subscribe(fieldId, rowKey, onStoreChange), [store, fieldId, rowKey]);
|
|
56
|
+
const getSnapshot = useCallback(() => store.isCurrentCandidate(candidate)
|
|
57
|
+
? store.getCandidateFieldProps(candidate, fieldId, rowKey)
|
|
58
|
+
: store.getFieldProps(fieldId, rowKey), [store, fieldId, rowKey, candidate]);
|
|
59
|
+
// The store exists during a server render too, so the same cached snapshot serves it;
|
|
60
|
+
// without a server snapshot React refuses to render the tree on a server at all.
|
|
61
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=useField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useField.js","sourceRoot":"","sources":["../src/useField.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAGpD,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAIpE;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,MAAM,OAAO,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IACxC,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,uCAAuC,CAAC,CAAC;IACxF,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAC/B,UAAoD,EACpD,IAAY;IAEZ,MAAM,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IACrC,IAAI,UAAU,KAAK,SAAS;QAAE,OAAO,OAAO,CAAC;IAC7C,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC;IACxC,IACE,UAAU,CAAC,QAAQ,KAAK,QAAQ,CAAC,QAAQ;QACzC,UAAU,CAAC,aAAa,KAAK,QAAQ,CAAC,aAAa,EACnD,CAAC;QACD,MAAM,IAAI,UAAU,CAClB,cAAc,EACd,GAAG,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AS,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,yLAAyL,CAC9Q,CAAC;IACJ,CAAC;IACD,MAAM,SAAS,GAAG,WAAW,CAC3B,CAAC,aAAyB,EAAgB,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,aAAa,CAAC,EAC5F,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,CACzB,CAAC;IACF,MAAM,WAAW,GAAG,WAAW,CAC7B,GAAwB,EAAE,CACxB,KAAK,CAAC,kBAAkB,CAAC,SAAS,CAAC;QACjC,CAAC,CAAC,KAAK,CAAC,sBAAsB,CAAC,SAAS,EAAE,OAAO,EAAE,MAAM,CAAC;QAC1D,CAAC,CAAC,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,EAC1C,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,CACpC,CAAC;IACF,sFAAsF;IACtF,iFAAiF;IACjF,OAAO,oBAAoB,CAAC,SAAS,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import { useCallback, useContext, useSyncExternalStore } from 'react';\nimport { AxiomError } from '@azx-pbc/axiom-runtime';\nimport type { ArtifactShape, FieldIdsOf, TypedArtifact } from '@azx-pbc/axiom-runtime';\nimport type { FieldProps, PublishedFieldProps } from './field-props.js';\nimport { FormContext, isDevelopmentBuild } from './form-context.js';\nimport type { FormSession } from './form-context.js';\nimport type { FieldBindingArgs, TypedFieldProps } from './typed.js';\n\n/**\n * The session the nearest provider owns, refusing a hook called outside one rather than\n * inventing a default field state.\n */\nexport function useFormSession(hook: string): FormSession {\n const session = useContext(FormContext);\n if (session === null) {\n throw new Error(`@azx-pbc/axiom-react: ${hook} must be called inside a FormProvider`);\n }\n return session;\n}\n\n/**\n * The session a typed binding reads, refusing a definition the provider did not compile.\n *\n * The typed door's promise — this id is declared, this row key is the right kind — is read from\n * the definition's artifact, so a provider rendering a different document would make the type a\n * claim about the wrong schema. The ids are matched by `schemaId` rather than object identity: a\n * provider may be handed any compile of the same document, and a recompiled artifact keeps its id.\n */\nexport function useFormSessionFor(\n definition: TypedArtifact<ArtifactShape> | undefined,\n hook: string,\n): FormSession {\n const session = useFormSession(hook);\n if (definition === undefined) return session;\n const rendered = session.store.compiled;\n if (\n definition.schemaId !== rendered.schemaId ||\n definition.schemaVersion !== rendered.schemaVersion\n ) {\n throw new AxiomError(\n 'ENGINE_ERROR',\n `${hook} is bound to schema ${definition.schemaId} version ${String(definition.schemaVersion)}, but the provider renders ${rendered.schemaId} version ${String(rendered.schemaVersion)}; mount the FormProvider with the definition the bindings name`,\n 'schemaId',\n );\n }\n return session;\n}\n\n/**\n * The evaluated state of one binding — a field, or one row of a field — re-read whenever that\n * binding's state changes.\n *\n * The props come from the candidate the current render derived, so the access, enabled and\n * value a control shows are the ones its inputs produce before the commit adopts them. Once a\n * write moves the session past that candidate, the committed store answers instead, which is\n * how a control woken by a store notification sees the committed value.\n *\n * `useSyncExternalStore` compares snapshots by identity, and the store returns the same\n * `FieldProps` object until that binding's published state changes. That identity is what stops\n * the React 19 StrictMode double render from looping, and it keeps a write to one binding from\n * re-rendering a control bound to another.\n *\n * Two doors share this hook. The typed door takes the artifact `defineDocument` returns and\n * accepts only a declared field id; a row key is stated exactly where the declaration holds rows.\n * The untyped door — `useField(fieldId, rowKey)` — is unchanged, for a host that holds a runtime\n * artifact and names no types. Both read the same binding, and the runtime judges both: an\n * undeclared id, a missing row key on an array member and a row key on a field without rows are\n * refusals either way.\n */\nexport function useField<Shape extends ArtifactShape, Id extends FieldIdsOf<Shape>>(\n definition: TypedArtifact<Shape>,\n fieldId: Id,\n ...rowKey: FieldBindingArgs<Shape, Id>\n): TypedFieldProps<Shape, Id>;\nexport function useField(fieldId: string, rowKey?: string | number): PublishedFieldProps;\nexport function useField(\n definitionOrFieldId: TypedArtifact<ArtifactShape> | string,\n fieldIdOrRowKey?: string | number,\n rowKey?: string | number,\n): FieldProps {\n const typed = typeof definitionOrFieldId !== 'string';\n const fieldId = typed ? fieldIdOrRowKey : definitionOrFieldId;\n const bindingRowKey = typed ? rowKey : fieldIdOrRowKey;\n if (typeof fieldId !== 'string') {\n throw new Error('@azx-pbc/axiom-react: useField must name a field id');\n }\n return useFieldBinding(fieldId, bindingRowKey, typed ? definitionOrFieldId : undefined);\n}\n\n/** The one binding implementation both doors read. */\nfunction useFieldBinding(\n fieldId: string,\n rowKey: string | number | undefined,\n definition?: TypedArtifact<ArtifactShape>,\n): PublishedFieldProps {\n const { store, candidate } = useFormSessionFor(definition, 'useField');\n if (\n definition === undefined &&\n rowKey !== undefined &&\n typeof rowKey === 'string' &&\n isDevelopmentBuild() &&\n store.declaresField(fieldId) &&\n !store.declaresRows(fieldId) &&\n store.declaresField(rowKey)\n ) {\n throw new Error(\n `@azx-pbc/axiom-react: useField(${JSON.stringify(fieldId)}, ${JSON.stringify(rowKey)}) passes a declared field id where a row key belongs; the untyped door reads (fieldId, rowKey), so either drop the row key or call the typed door useField(definition, fieldId, rowKey)`,\n );\n }\n const subscribe = useCallback(\n (onStoreChange: () => void): (() => void) => store.subscribe(fieldId, rowKey, onStoreChange),\n [store, fieldId, rowKey],\n );\n const getSnapshot = useCallback(\n (): PublishedFieldProps =>\n store.isCurrentCandidate(candidate)\n ? store.getCandidateFieldProps(candidate, fieldId, rowKey)\n : store.getFieldProps(fieldId, rowKey),\n [store, fieldId, rowKey, candidate],\n );\n // The store exists during a server render too, so the same cached snapshot serves it;\n // without a server snapshot React refuses to render the tree on a server at all.\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n}\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ArtifactShape, TypedArtifact } from '@azx-pbc/axiom-runtime';
|
|
2
|
+
import type { FieldArray } from './store/array-bindings.js';
|
|
3
|
+
import type { ArrayFieldIdsOf } from './typed.js';
|
|
4
|
+
/**
|
|
5
|
+
* One array binding's structure: its rows in order and the operations that change them.
|
|
6
|
+
*
|
|
7
|
+
* The rows are identity-stable: an entry keeps its object while the row exists, so a React
|
|
8
|
+
* `key` and a memo boundary survive a move, and the list itself is only republished when the
|
|
9
|
+
* ordered identity or the mutation permissions changed. A control below the list binds its own
|
|
10
|
+
* row with `useField`, so a keystroke in row three re-renders row three — not the list, and not
|
|
11
|
+
* its other rows.
|
|
12
|
+
*
|
|
13
|
+
* A structural call refuses by throwing the engine's own write error, exactly as a value write
|
|
14
|
+
* does; `mutations` states which of them the container's committed access and enabled answer
|
|
15
|
+
* permit, so a host can hide the ones it cannot offer.
|
|
16
|
+
*
|
|
17
|
+
* Two doors share this hook. The typed door takes the artifact `defineDocument` returns and
|
|
18
|
+
* accepts only a declared array field, so a scalar or a row member is refused by the compiler;
|
|
19
|
+
* the engine still judges the call. The untyped door — `useFieldArray(fieldId)` — is
|
|
20
|
+
* unchanged, for a host that holds a runtime artifact and names no types. The view is the
|
|
21
|
+
* container's either way: rows bind with `useField`, and a member id names no list.
|
|
22
|
+
*/
|
|
23
|
+
export declare function useFieldArray<Shape extends ArtifactShape, Id extends ArrayFieldIdsOf<Shape>>(definition: TypedArtifact<Shape>, fieldId: Id): FieldArray;
|
|
24
|
+
export declare function useFieldArray(fieldId: string): FieldArray;
|
|
25
|
+
//# sourceMappingURL=useFieldArray.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFieldArray.d.ts","sourceRoot":"","sources":["../src/useFieldArray.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAE3E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAAC,KAAK,SAAS,aAAa,EAAE,EAAE,SAAS,eAAe,CAAC,KAAK,CAAC,EAC1F,UAAU,EAAE,aAAa,CAAC,KAAK,CAAC,EAChC,OAAO,EAAE,EAAE,GACV,UAAU,CAAC;AACd,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useCallback, useSyncExternalStore } from 'react';
|
|
2
|
+
import { fieldArrayOf } from './store/array-bindings.js';
|
|
3
|
+
import { useFormSessionFor } from './useField.js';
|
|
4
|
+
export function useFieldArray(definitionOrFieldId, fieldIdOrRowKey) {
|
|
5
|
+
const typed = typeof definitionOrFieldId !== 'string';
|
|
6
|
+
const fieldId = typed ? fieldIdOrRowKey : definitionOrFieldId;
|
|
7
|
+
if (typeof fieldId !== 'string') {
|
|
8
|
+
throw new Error('@azx-pbc/axiom-react: useFieldArray must name a field id');
|
|
9
|
+
}
|
|
10
|
+
const { store, candidate } = useFormSessionFor(typed ? definitionOrFieldId : undefined, 'useFieldArray');
|
|
11
|
+
const subscribe = useCallback((onStoreChange) => store.subscribeArray(fieldId, onStoreChange), [store, fieldId]);
|
|
12
|
+
const controls = store.arrayControls(fieldId);
|
|
13
|
+
const getSnapshot = useCallback(() => fieldArrayOf(store.isCurrentCandidate(candidate)
|
|
14
|
+
? store.getCandidateArrayView(candidate, fieldId)
|
|
15
|
+
: store.getArrayView(fieldId), controls), [store, fieldId, candidate, controls]);
|
|
16
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=useFieldArray.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFieldArray.js","sourceRoot":"","sources":["../src/useFieldArray.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AA2BlD,MAAM,UAAU,aAAa,CAC3B,mBAA0D,EAC1D,eAAwB;IAExB,MAAM,KAAK,GAAG,OAAO,mBAAmB,KAAK,QAAQ,CAAC;IACtD,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,mBAAmB,CAAC;IAC9D,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;IAC9E,CAAC;IACD,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,iBAAiB,CAC5C,KAAK,CAAC,CAAC,CAAC,mBAAmB,CAAC,CAAC,CAAC,SAAS,EACvC,eAAe,CAChB,CAAC;IACF,MAAM,SAAS,GAAG,WAAW,CAC3B,CAAC,aAAyB,EAAgB,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE,aAAa,CAAC,EACzF,CAAC,KAAK,EAAE,OAAO,CAAC,CACjB,CAAC;IACF,MAAM,QAAQ,GAAG,KAAK,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAC9C,MAAM,WAAW,GAAG,WAAW,CAC7B,GAAe,EAAE,CACf,YAAY,CACV,KAAK,CAAC,kBAAkB,CAAC,SAAS,CAAC;QACjC,CAAC,CAAC,KAAK,CAAC,qBAAqB,CAAC,SAAS,EAAE,OAAO,CAAC;QACjD,CAAC,CAAC,KAAK,CAAC,YAAY,CAAC,OAAO,CAAC,EAC/B,QAAQ,CACT,EACH,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,CAAC,CACtC,CAAC;IACF,OAAO,oBAAoB,CAAC,SAAS,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import { useCallback, useSyncExternalStore } from 'react';\nimport type { ArtifactShape, TypedArtifact } from '@azx-pbc/axiom-runtime';\nimport { fieldArrayOf } from './store/array-bindings.js';\nimport type { FieldArray } from './store/array-bindings.js';\nimport { useFormSessionFor } from './useField.js';\nimport type { ArrayFieldIdsOf } from './typed.js';\n\n/**\n * One array binding's structure: its rows in order and the operations that change them.\n *\n * The rows are identity-stable: an entry keeps its object while the row exists, so a React\n * `key` and a memo boundary survive a move, and the list itself is only republished when the\n * ordered identity or the mutation permissions changed. A control below the list binds its own\n * row with `useField`, so a keystroke in row three re-renders row three — not the list, and not\n * its other rows.\n *\n * A structural call refuses by throwing the engine's own write error, exactly as a value write\n * does; `mutations` states which of them the container's committed access and enabled answer\n * permit, so a host can hide the ones it cannot offer.\n *\n * Two doors share this hook. The typed door takes the artifact `defineDocument` returns and\n * accepts only a declared array field, so a scalar or a row member is refused by the compiler;\n * the engine still judges the call. The untyped door — `useFieldArray(fieldId)` — is\n * unchanged, for a host that holds a runtime artifact and names no types. The view is the\n * container's either way: rows bind with `useField`, and a member id names no list.\n */\nexport function useFieldArray<Shape extends ArtifactShape, Id extends ArrayFieldIdsOf<Shape>>(\n definition: TypedArtifact<Shape>,\n fieldId: Id,\n): FieldArray;\nexport function useFieldArray(fieldId: string): FieldArray;\nexport function useFieldArray(\n definitionOrFieldId: TypedArtifact<ArtifactShape> | string,\n fieldIdOrRowKey?: string,\n): FieldArray {\n const typed = typeof definitionOrFieldId !== 'string';\n const fieldId = typed ? fieldIdOrRowKey : definitionOrFieldId;\n if (typeof fieldId !== 'string') {\n throw new Error('@azx-pbc/axiom-react: useFieldArray must name a field id');\n }\n const { store, candidate } = useFormSessionFor(\n typed ? definitionOrFieldId : undefined,\n 'useFieldArray',\n );\n const subscribe = useCallback(\n (onStoreChange: () => void): (() => void) => store.subscribeArray(fieldId, onStoreChange),\n [store, fieldId],\n );\n const controls = store.arrayControls(fieldId);\n const getSnapshot = useCallback(\n (): FieldArray =>\n fieldArrayOf(\n store.isCurrentCandidate(candidate)\n ? store.getCandidateArrayView(candidate, fieldId)\n : store.getArrayView(fieldId),\n controls,\n ),\n [store, fieldId, candidate, controls],\n );\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ArtifactShape, TypedArtifact } from '@azx-pbc/axiom-runtime';
|
|
2
|
+
import type { FormHandle } from './form-store.js';
|
|
3
|
+
import type { TypedFormHandle } from './typed.js';
|
|
4
|
+
/**
|
|
5
|
+
* The imperative form handle: submit, reset, export and the session operations.
|
|
6
|
+
*
|
|
7
|
+
* The handle never subscribes, and the same object is returned for the whole life of the
|
|
8
|
+
* provider, so a component that destructures `handleSubmit` renders once across any number of
|
|
9
|
+
* edits. Its methods read committed state when they are called, so a handler captured by an
|
|
10
|
+
* older render still acts on the session as it stands.
|
|
11
|
+
*
|
|
12
|
+
* The typed door takes the `defineDocument` artifact, so `handleSubmit`'s `onValid` receives the
|
|
13
|
+
* definition's record type instead of `Record<string, unknown>`, and `exportSubmission` and
|
|
14
|
+
* `getState` carry the same shape. The untyped door takes no argument, as before.
|
|
15
|
+
*/
|
|
16
|
+
export declare function useFormHandle<Shape extends ArtifactShape>(definition: TypedArtifact<Shape>): TypedFormHandle<Shape>;
|
|
17
|
+
export declare function useFormHandle(): FormHandle;
|
|
18
|
+
/** The handle under the name a React Hook Form user reaches for; the same hook. */
|
|
19
|
+
export declare const useForm: typeof useFormHandle;
|
|
20
|
+
//# sourceMappingURL=useForm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useForm.d.ts","sourceRoot":"","sources":["../src/useForm.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC3E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,KAAK,SAAS,aAAa,EACvD,UAAU,EAAE,aAAa,CAAC,KAAK,CAAC,GAC/B,eAAe,CAAC,KAAK,CAAC,CAAC;AAC1B,wBAAgB,aAAa,IAAI,UAAU,CAAC;AAM5C,mFAAmF;AACnF,eAAO,MAAM,OAAO,EAAE,OAAO,aAA6B,CAAC"}
|
package/dist/useForm.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useFormSessionFor } from './useField.js';
|
|
3
|
+
export function useFormHandle(definition) {
|
|
4
|
+
const { store } = useFormSessionFor(definition, 'useForm');
|
|
5
|
+
return useMemo(() => store.handle(), [store]);
|
|
6
|
+
}
|
|
7
|
+
/** The handle under the name a React Hook Form user reaches for; the same hook. */
|
|
8
|
+
export const useForm = useFormHandle;
|
|
9
|
+
//# sourceMappingURL=useForm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useForm.js","sourceRoot":"","sources":["../src/useForm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAGhC,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAmBlD,MAAM,UAAU,aAAa,CAAC,UAAyC;IACrE,MAAM,EAAE,KAAK,EAAE,GAAG,iBAAiB,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;IAC3D,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;AAChD,CAAC;AAED,mFAAmF;AACnF,MAAM,CAAC,MAAM,OAAO,GAAyB,aAAa,CAAC","sourcesContent":["import { useMemo } from 'react';\nimport type { ArtifactShape, TypedArtifact } from '@azx-pbc/axiom-runtime';\nimport type { FormHandle } from './form-store.js';\nimport { useFormSessionFor } from './useField.js';\nimport type { TypedFormHandle } from './typed.js';\n\n/**\n * The imperative form handle: submit, reset, export and the session operations.\n *\n * The handle never subscribes, and the same object is returned for the whole life of the\n * provider, so a component that destructures `handleSubmit` renders once across any number of\n * edits. Its methods read committed state when they are called, so a handler captured by an\n * older render still acts on the session as it stands.\n *\n * The typed door takes the `defineDocument` artifact, so `handleSubmit`'s `onValid` receives the\n * definition's record type instead of `Record<string, unknown>`, and `exportSubmission` and\n * `getState` carry the same shape. The untyped door takes no argument, as before.\n */\nexport function useFormHandle<Shape extends ArtifactShape>(\n definition: TypedArtifact<Shape>,\n): TypedFormHandle<Shape>;\nexport function useFormHandle(): FormHandle;\nexport function useFormHandle(definition?: TypedArtifact<ArtifactShape>): FormHandle {\n const { store } = useFormSessionFor(definition, 'useForm');\n return useMemo(() => store.handle(), [store]);\n}\n\n/** The handle under the name a React Hook Form user reaches for; the same hook. */\nexport const useForm: typeof useFormHandle = useFormHandle;\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ArtifactShape, TypedArtifact } from '@azx-pbc/axiom-runtime';
|
|
2
|
+
import type { FormSummary } from './form-store.js';
|
|
3
|
+
import type { TypedFormSummary } from './typed.js';
|
|
4
|
+
/**
|
|
5
|
+
* The form-level summary: dirty and touched bindings, validity, readiness, the submit counters
|
|
6
|
+
* and the blocking errors.
|
|
7
|
+
*
|
|
8
|
+
* It subscribes on its own channel, so a control editing a value does not re-render this
|
|
9
|
+
* subscriber unless a fact in the summary actually changed: the first keystroke that makes the
|
|
10
|
+
* form dirty renders it once, and the second changes nothing it reports and renders it not at
|
|
11
|
+
* all. `isValid`, `ready` and a gate's `open` are facts behind `verdict`; gate a submission
|
|
12
|
+
* on `verdict === 'SUBMIT'`, and remember the server decides again.
|
|
13
|
+
*
|
|
14
|
+
* The typed door takes the `defineDocument` artifact and states the shape's binding keys in
|
|
15
|
+
* `dirtyFields`, `touchedFields` and `missingRequiredFields`. The summary carries no record
|
|
16
|
+
* value by design — it does not observe values, so it cannot go stale — and the record type
|
|
17
|
+
* belongs to the submit door, `useForm(def).handleSubmit`. `pendingProposal` follows the same
|
|
18
|
+
* rule: it is a value-free projection naming the waiting sequence and the binding it edits, not
|
|
19
|
+
* the proposal itself. The untyped door is unchanged.
|
|
20
|
+
*/
|
|
21
|
+
export declare function useFormState<Shape extends ArtifactShape>(definition: TypedArtifact<Shape>): TypedFormSummary<Shape>;
|
|
22
|
+
export declare function useFormState(): FormSummary;
|
|
23
|
+
//# sourceMappingURL=useFormState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormState.d.ts","sourceRoot":"","sources":["../src/useFormState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC3E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CAAC,KAAK,SAAS,aAAa,EACtD,UAAU,EAAE,aAAa,CAAC,KAAK,CAAC,GAC/B,gBAAgB,CAAC,KAAK,CAAC,CAAC;AAC3B,wBAAgB,YAAY,IAAI,WAAW,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useCallback, useSyncExternalStore } from 'react';
|
|
2
|
+
import { useFormSessionFor } from './useField.js';
|
|
3
|
+
export function useFormState(definition) {
|
|
4
|
+
const { store, candidate } = useFormSessionFor(definition, 'useFormState');
|
|
5
|
+
const subscribe = useCallback((onStoreChange) => store.subscribeForm(onStoreChange), [store]);
|
|
6
|
+
const getSnapshot = useCallback(() => store.isCurrentCandidate(candidate)
|
|
7
|
+
? store.getCandidateFormSummary(candidate)
|
|
8
|
+
: store.getFormSummary(), [store, candidate]);
|
|
9
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=useFormState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFormState.js","sourceRoot":"","sources":["../src/useFormState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAG1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAwBlD,MAAM,UAAU,YAAY,CAAC,UAAyC;IACpE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,iBAAiB,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;IAC3E,MAAM,SAAS,GAAG,WAAW,CAC3B,CAAC,aAAyB,EAAgB,EAAE,CAAC,KAAK,CAAC,aAAa,CAAC,aAAa,CAAC,EAC/E,CAAC,KAAK,CAAC,CACR,CAAC;IACF,MAAM,WAAW,GAAG,WAAW,CAC7B,GAAgB,EAAE,CAChB,KAAK,CAAC,kBAAkB,CAAC,SAAS,CAAC;QACjC,CAAC,CAAC,KAAK,CAAC,uBAAuB,CAAC,SAAS,CAAC;QAC1C,CAAC,CAAC,KAAK,CAAC,cAAc,EAAE,EAC5B,CAAC,KAAK,EAAE,SAAS,CAAC,CACnB,CAAC;IACF,OAAO,oBAAoB,CAAC,SAAS,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import { useCallback, useSyncExternalStore } from 'react';\nimport type { ArtifactShape, TypedArtifact } from '@azx-pbc/axiom-runtime';\nimport type { FormSummary } from './form-store.js';\nimport { useFormSessionFor } from './useField.js';\nimport type { TypedFormSummary } from './typed.js';\n\n/**\n * The form-level summary: dirty and touched bindings, validity, readiness, the submit counters\n * and the blocking errors.\n *\n * It subscribes on its own channel, so a control editing a value does not re-render this\n * subscriber unless a fact in the summary actually changed: the first keystroke that makes the\n * form dirty renders it once, and the second changes nothing it reports and renders it not at\n * all. `isValid`, `ready` and a gate's `open` are facts behind `verdict`; gate a submission\n * on `verdict === 'SUBMIT'`, and remember the server decides again.\n *\n * The typed door takes the `defineDocument` artifact and states the shape's binding keys in\n * `dirtyFields`, `touchedFields` and `missingRequiredFields`. The summary carries no record\n * value by design — it does not observe values, so it cannot go stale — and the record type\n * belongs to the submit door, `useForm(def).handleSubmit`. `pendingProposal` follows the same\n * rule: it is a value-free projection naming the waiting sequence and the binding it edits, not\n * the proposal itself. The untyped door is unchanged.\n */\nexport function useFormState<Shape extends ArtifactShape>(\n definition: TypedArtifact<Shape>,\n): TypedFormSummary<Shape>;\nexport function useFormState(): FormSummary;\nexport function useFormState(definition?: TypedArtifact<ArtifactShape>): FormSummary {\n const { store, candidate } = useFormSessionFor(definition, 'useFormState');\n const subscribe = useCallback(\n (onStoreChange: () => void): (() => void) => store.subscribeForm(onStoreChange),\n [store],\n );\n const getSnapshot = useCallback(\n (): FormSummary =>\n store.isCurrentCandidate(candidate)\n ? store.getCandidateFormSummary(candidate)\n : store.getFormSummary(),\n [store, candidate],\n );\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n}\n"]}
|