@azx-pbc/axiom-web 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 +161 -2
- package/dist/address.d.ts +32 -0
- package/dist/address.d.ts.map +1 -0
- package/dist/address.js +146 -0
- package/dist/address.js.map +1 -0
- package/dist/capture/MediaCapture.d.ts +45 -0
- package/dist/capture/MediaCapture.d.ts.map +1 -0
- package/dist/capture/MediaCapture.js +669 -0
- package/dist/capture/MediaCapture.js.map +1 -0
- package/dist/capture/SignatureCapture.d.ts +31 -0
- package/dist/capture/SignatureCapture.d.ts.map +1 -0
- package/dist/capture/SignatureCapture.js +329 -0
- package/dist/capture/SignatureCapture.js.map +1 -0
- package/dist/capture/copy.d.ts +101 -0
- package/dist/capture/copy.d.ts.map +1 -0
- package/dist/capture/copy.js +1369 -0
- package/dist/capture/copy.js.map +1 -0
- package/dist/capture/index.d.ts +19 -0
- package/dist/capture/index.d.ts.map +1 -0
- package/dist/capture/index.js +14 -0
- package/dist/capture/index.js.map +1 -0
- package/dist/capture/media-kind.d.ts +16 -0
- package/dist/capture/media-kind.d.ts.map +1 -0
- package/dist/capture/media-kind.js +64 -0
- package/dist/capture/media-kind.js.map +1 -0
- package/dist/capture/permissions.d.ts +109 -0
- package/dist/capture/permissions.d.ts.map +1 -0
- package/dist/capture/permissions.js +172 -0
- package/dist/capture/permissions.js.map +1 -0
- package/dist/capture/refusal.d.ts +13 -0
- package/dist/capture/refusal.d.ts.map +1 -0
- package/dist/capture/refusal.js +16 -0
- package/dist/capture/refusal.js.map +1 -0
- package/dist/controls.d.ts +64 -0
- package/dist/controls.d.ts.map +1 -0
- package/dist/controls.js +2524 -0
- package/dist/controls.js.map +1 -0
- package/dist/coordinate.d.ts +38 -0
- package/dist/coordinate.d.ts.map +1 -0
- package/dist/coordinate.js +220 -0
- package/dist/coordinate.js.map +1 -0
- package/dist/copy.d.ts +313 -0
- package/dist/copy.d.ts.map +1 -0
- package/dist/copy.js +2602 -0
- package/dist/copy.js.map +1 -0
- package/dist/countries.d.ts +14 -0
- package/dist/countries.d.ts.map +1 -0
- package/dist/countries.js +264 -0
- package/dist/countries.js.map +1 -0
- package/dist/format/calendar-locale.d.ts +19 -0
- package/dist/format/calendar-locale.d.ts.map +1 -0
- package/dist/format/calendar-locale.js +38 -0
- package/dist/format/calendar-locale.js.map +1 -0
- package/dist/format/country-names.d.ts +21 -0
- package/dist/format/country-names.d.ts.map +1 -0
- package/dist/format/country-names.js +2277 -0
- package/dist/format/country-names.js.map +1 -0
- package/dist/format/date.d.ts +58 -0
- package/dist/format/date.d.ts.map +1 -0
- package/dist/format/date.js +155 -0
- package/dist/format/date.js.map +1 -0
- package/dist/format/digits.d.ts +23 -0
- package/dist/format/digits.d.ts.map +1 -0
- package/dist/format/digits.js +46 -0
- package/dist/format/digits.js.map +1 -0
- package/dist/format/index.d.ts +28 -0
- package/dist/format/index.d.ts.map +1 -0
- package/dist/format/index.js +24 -0
- package/dist/format/index.js.map +1 -0
- package/dist/format/locale.d.ts +33 -0
- package/dist/format/locale.d.ts.map +1 -0
- package/dist/format/locale.js +39 -0
- package/dist/format/locale.js.map +1 -0
- package/dist/format/media-type.d.ts +8 -0
- package/dist/format/media-type.d.ts.map +1 -0
- package/dist/format/media-type.js +302 -0
- package/dist/format/media-type.js.map +1 -0
- package/dist/format/money.d.ts +52 -0
- package/dist/format/money.d.ts.map +1 -0
- package/dist/format/money.js +36 -0
- package/dist/format/money.js.map +1 -0
- package/dist/format/number.d.ts +28 -0
- package/dist/format/number.d.ts.map +1 -0
- package/dist/format/number.js +91 -0
- package/dist/format/number.js.map +1 -0
- package/dist/host.d.ts +122 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +20 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/media.d.ts +42 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +151 -0
- package/dist/media.js.map +1 -0
- package/dist/pickers/DatePicker.d.ts +35 -0
- package/dist/pickers/DatePicker.d.ts.map +1 -0
- package/dist/pickers/DatePicker.js +212 -0
- package/dist/pickers/DatePicker.js.map +1 -0
- package/dist/pickers/TimeSegments.d.ts +27 -0
- package/dist/pickers/TimeSegments.d.ts.map +1 -0
- package/dist/pickers/TimeSegments.js +82 -0
- package/dist/pickers/TimeSegments.js.map +1 -0
- package/dist/pickers/day-picker-locale.d.ts +15 -0
- package/dist/pickers/day-picker-locale.d.ts.map +1 -0
- package/dist/pickers/day-picker-locale.js +46 -0
- package/dist/pickers/day-picker-locale.js.map +1 -0
- package/dist/render/dom.d.ts +100 -0
- package/dist/render/dom.d.ts.map +1 -0
- package/dist/render/dom.js +882 -0
- package/dist/render/dom.js.map +1 -0
- package/dist/render/layout.d.ts +45 -0
- package/dist/render/layout.d.ts.map +1 -0
- package/dist/render/layout.js +8 -0
- package/dist/render/layout.js.map +1 -0
- package/dist/schedule/SchedulePicker.d.ts +46 -0
- package/dist/schedule/SchedulePicker.d.ts.map +1 -0
- package/dist/schedule/SchedulePicker.js +160 -0
- package/dist/schedule/SchedulePicker.js.map +1 -0
- package/dist/schedule/timeslot.d.ts +52 -0
- package/dist/schedule/timeslot.d.ts.map +1 -0
- package/dist/schedule/timeslot.js +265 -0
- package/dist/schedule/timeslot.js.map +1 -0
- package/dist/styles.css +2151 -0
- package/dist/theme/index.d.ts +12 -0
- package/dist/theme/index.d.ts.map +1 -0
- package/dist/theme/index.js +11 -0
- package/dist/theme/index.js.map +1 -0
- package/dist/theme/presets.d.ts +93 -0
- package/dist/theme/presets.d.ts.map +1 -0
- package/dist/theme/presets.js +200 -0
- package/dist/theme/presets.js.map +1 -0
- package/dist/tokens.css +234 -0
- package/dist/wizard-checkpoints.d.ts +18 -0
- package/dist/wizard-checkpoints.d.ts.map +1 -0
- package/dist/wizard-checkpoints.js +56 -0
- package/dist/wizard-checkpoints.js.map +1 -0
- package/package.json +76 -3
|
@@ -0,0 +1,882 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { fieldDomId, freeNaturalKey, renderDisplayText, renderLayout, renderRowName, renderValueText, useField, useIdPrefix, useFieldArray, useFormState, useLayout, useLayoutNode, useWizard, } from '@azx-pbc/axiom-react';
|
|
3
|
+
import { directionOf, fieldKey, isRecord } from '@azx-pbc/axiom-runtime';
|
|
4
|
+
import { createElement, useEffect, useMemo, useRef, useState } from 'react';
|
|
5
|
+
import { Control, ControlComponentsContext } from '../controls.js';
|
|
6
|
+
import { useHostCopy } from '../copy.js';
|
|
7
|
+
import { isDecimalString } from '../format/number.js';
|
|
8
|
+
import { LocaleContext } from '../host.js';
|
|
9
|
+
import { LayoutComponentsContext, useLayoutComponents } from './layout.js';
|
|
10
|
+
import { WizardCheckpoints } from '../wizard-checkpoints.js';
|
|
11
|
+
/**
|
|
12
|
+
* The DOM adapter: one node kind, one piece of semantic markup.
|
|
13
|
+
*
|
|
14
|
+
* A section and a group are landmark containers with a heading at the depth the tree states, a
|
|
15
|
+
* control is a label bound to its input, a failure is text the input is described by, and the
|
|
16
|
+
* summary of every blocking reason is an alert that takes focus after a refused submit. What is
|
|
17
|
+
* painted is exactly the tree `renderLayout` produced; nothing here asks which document it is in.
|
|
18
|
+
*
|
|
19
|
+
* Every element carries an `.ax-*` hook, and every element id is prefixed with the session's own
|
|
20
|
+
* prefix, so two forms over one artifact on one page cannot collide on a heading, a section body
|
|
21
|
+
* or a records container. The hooks are documented in `theme/README.md`; the sheet that styles
|
|
22
|
+
* them is `theme/base.css`.
|
|
23
|
+
*/
|
|
24
|
+
/** The suffix the summary's element id carries, after the session's own prefix. */
|
|
25
|
+
export const ERROR_SUMMARY_SUFFIX = 'error-summary';
|
|
26
|
+
/** The heading level a container slot receives: the node's own when stated, else the enclosing level. */
|
|
27
|
+
function slotLevelOf(node, enclosingLevel) {
|
|
28
|
+
switch (node.kind) {
|
|
29
|
+
case 'section':
|
|
30
|
+
case 'group':
|
|
31
|
+
case 'step':
|
|
32
|
+
case 'heading':
|
|
33
|
+
case 'sop_block':
|
|
34
|
+
case 'summary_card':
|
|
35
|
+
return node.level;
|
|
36
|
+
case 'wizard':
|
|
37
|
+
return node.children[0]?.level ?? enclosingLevel;
|
|
38
|
+
case 'record':
|
|
39
|
+
case 'records':
|
|
40
|
+
case 'callout':
|
|
41
|
+
case 'divider':
|
|
42
|
+
case 'error_summary':
|
|
43
|
+
return enclosingLevel;
|
|
44
|
+
default:
|
|
45
|
+
return assertNeverNode(node);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** One structural element id, namespaced by the form session and the layout node that owns it. */
|
|
49
|
+
function structuralId(idPrefix, nodeId, suffix) {
|
|
50
|
+
return `${idPrefix}-${nodeId}${suffix}`;
|
|
51
|
+
}
|
|
52
|
+
/** The title a container announces, or `undefined` when it has nothing to announce. */
|
|
53
|
+
function titledOf(title) {
|
|
54
|
+
return title === undefined || title.trim() === '' ? undefined : title;
|
|
55
|
+
}
|
|
56
|
+
function heading(level, id, children, focusRef) {
|
|
57
|
+
// A step heading is focusable (`tabIndex -1`) so arriving at a step can move focus to its
|
|
58
|
+
// title without putting every heading in the tab order.
|
|
59
|
+
return createElement(`h${String(level)}`, focusRef === undefined ? { id } : { id, ref: focusRef, tabIndex: -1 }, children);
|
|
60
|
+
}
|
|
61
|
+
/** The `data-width` value and the one inline size a numeric width needs. */
|
|
62
|
+
function formWidthOf(width) {
|
|
63
|
+
if (width === undefined)
|
|
64
|
+
return { dataWidth: 'measure', style: undefined };
|
|
65
|
+
if (typeof width === 'number') {
|
|
66
|
+
return { dataWidth: 'custom', style: { maxInlineSize: `${String(width)}rem` } };
|
|
67
|
+
}
|
|
68
|
+
return { dataWidth: width, style: undefined };
|
|
69
|
+
}
|
|
70
|
+
export function LayoutTree({ nodes, translate, width, components, layout, lang, direction, }) {
|
|
71
|
+
const sizing = formWidthOf(width);
|
|
72
|
+
return (_jsx(ControlComponentsContext.Provider, { value: components, children: _jsx(LayoutComponentsContext.Provider, { value: layout, children: _jsx("div", { "data-render-tree": "", className: "ax-form", "data-width": sizing.dataWidth, ...(lang === undefined ? {} : { lang }), ...(direction === undefined ? {} : { dir: direction }), ...(sizing.style === undefined ? {} : { style: sizing.style }), children: nodes.map((node) => (_jsx(NodeView, { node: node, translate: translate, enclosingLevel: 2 }, node.nodeId))) }) }) }));
|
|
73
|
+
}
|
|
74
|
+
function NodeView({ node, translate, enclosingLevel, }) {
|
|
75
|
+
const layoutComponents = useLayoutComponents();
|
|
76
|
+
const idPrefix = useIdPrefix();
|
|
77
|
+
if (node.kind !== 'field') {
|
|
78
|
+
const Custom = layoutComponents?.[node.kind];
|
|
79
|
+
if (Custom !== undefined) {
|
|
80
|
+
// The default tree is the slot's `children`: a host returns it to keep the adapter's
|
|
81
|
+
// markup, wraps it to add its own chrome, or ignores it to paint the node itself.
|
|
82
|
+
return (_jsx(Custom, { node: node, translate: translate, level: slotLevelOf(node, enclosingLevel), children: defaultNodeView(node, translate, idPrefix) }));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return defaultNodeView(node, translate, idPrefix);
|
|
86
|
+
}
|
|
87
|
+
/** The adapter's own markup for one node, before a host container slot gets a say. */
|
|
88
|
+
function defaultNodeView(node, translate, idPrefix) {
|
|
89
|
+
switch (node.kind) {
|
|
90
|
+
case 'error_summary':
|
|
91
|
+
return (_jsx(ErrorSummaryView, { items: node.items, failed: node.items.length > 0, summaryId: structuralId(idPrefix, ERROR_SUMMARY_SUFFIX, '') }));
|
|
92
|
+
case 'section':
|
|
93
|
+
case 'group':
|
|
94
|
+
return _jsx(SectionView, { node: node, translate: translate });
|
|
95
|
+
case 'wizard':
|
|
96
|
+
return _jsx(WizardView, { node: node, translate: translate });
|
|
97
|
+
case 'step':
|
|
98
|
+
return _jsx(StepView, { node: node, translate: translate });
|
|
99
|
+
case 'field':
|
|
100
|
+
return _jsx(Control, { node: node, translate: translate });
|
|
101
|
+
case 'record':
|
|
102
|
+
return _jsx(RecordView, { node: node, translate: translate });
|
|
103
|
+
case 'records':
|
|
104
|
+
return _jsx(RecordsView, { node: node, translate: translate });
|
|
105
|
+
case 'heading':
|
|
106
|
+
return (_jsxs("header", { children: [heading(node.level, structuralId(idPrefix, node.nodeId, '.title'), node.title), node.body === undefined ? null : _jsx("p", { children: node.body })] }));
|
|
107
|
+
case 'callout':
|
|
108
|
+
return (_jsxs("aside", { className: "ax-callout", role: "note", "data-tone": node.tone, children: [node.title === undefined ? null : _jsx("p", { children: node.title }), _jsx("p", { children: node.body })] }));
|
|
109
|
+
case 'divider':
|
|
110
|
+
return _jsx("hr", {});
|
|
111
|
+
case 'sop_block':
|
|
112
|
+
return (_jsxs("section", { className: "ax-sop", "aria-labelledby": structuralId(idPrefix, node.nodeId, '.title'), children: [heading(node.level, structuralId(idPrefix, node.nodeId, '.title'), node.title), node.body === undefined ? null : _jsx("p", { children: node.body })] }));
|
|
113
|
+
case 'summary_card':
|
|
114
|
+
return _jsx(SummaryView, { node: node });
|
|
115
|
+
default:
|
|
116
|
+
return assertNeverNode(node);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/** A union arm the compiler proves unreachable. */
|
|
120
|
+
function assertNeverNode(value) {
|
|
121
|
+
throw new Error(`unreachable render node: ${JSON.stringify(value)}`);
|
|
122
|
+
}
|
|
123
|
+
function Children({ nodes, translate, enclosingLevel, }) {
|
|
124
|
+
return (_jsx(_Fragment, { children: nodes.map((child) => (_jsx("div", { className: "axiom-grid-item", style: { gridColumn: spanOf(child) }, children: _jsx(NodeView, { node: child, translate: translate, enclosingLevel: enclosingLevel }) }, child.nodeId))) }));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* How many of its parent's columns one node occupies.
|
|
128
|
+
*
|
|
129
|
+
* A control, a record group and a record list state their own `colSpan`; every other node is
|
|
130
|
+
* chrome that spans the row, because a callout or a divider squeezed into one column of a
|
|
131
|
+
* two-column section would read as part of the field beside it.
|
|
132
|
+
*/
|
|
133
|
+
function spanOf(node) {
|
|
134
|
+
switch (node.kind) {
|
|
135
|
+
case 'field':
|
|
136
|
+
case 'record':
|
|
137
|
+
case 'records':
|
|
138
|
+
return `span ${String(node.colSpan)}`;
|
|
139
|
+
default:
|
|
140
|
+
return '1 / -1';
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* A section or a group: a heading, an optional collapsed state with the failure count it hides,
|
|
145
|
+
* and its children. The count is read by the toggle itself, so a failure appearing inside the
|
|
146
|
+
* container repaints the toggle and not the controls it holds.
|
|
147
|
+
*
|
|
148
|
+
* A group may state no title at all, and a translated title may be empty. Neither is announced:
|
|
149
|
+
* a heading with no name reads as a heading and says nothing, and a region labelled by one is an
|
|
150
|
+
* unnamed landmark. The container is then a plain box, and the toggle names only its action.
|
|
151
|
+
*/
|
|
152
|
+
function SectionView({ node, translate, }) {
|
|
153
|
+
const idPrefix = useIdPrefix();
|
|
154
|
+
const [expanded, setExpanded] = useState(node.defaultExpanded ?? true);
|
|
155
|
+
const bodyId = structuralId(idPrefix, node.nodeId, '.children');
|
|
156
|
+
const titleId = structuralId(idPrefix, node.nodeId, '.title');
|
|
157
|
+
const title = titledOf(node.title);
|
|
158
|
+
const className = node.kind === 'section' ? 'ax-section' : 'ax-group';
|
|
159
|
+
// `hidden` alone does not collapse this: the UA rule `[hidden] { display: none }` is a
|
|
160
|
+
// presentational hint, and the inline `display` below outranks it. The two have to agree, or a
|
|
161
|
+
// toggle that reports `aria-expanded="false"` leaves its section on screen.
|
|
162
|
+
const collapsed = node.collapsible && !expanded;
|
|
163
|
+
const body = (_jsx("div", { id: bodyId, className: "ax-grid", hidden: collapsed, style: {
|
|
164
|
+
display: collapsed ? 'none' : 'grid',
|
|
165
|
+
gridTemplateColumns: `repeat(${String(node.columns)}, minmax(0, 1fr))`,
|
|
166
|
+
}, children: _jsx(Children, { nodes: node.children, translate: translate, enclosingLevel: node.level }) }));
|
|
167
|
+
const toggle = node.collapsible ? (_jsx(SectionToggle, { nodeId: node.nodeId, bodyId: bodyId, title: title, expanded: expanded, onToggle: () => setExpanded((open) => !open) })) : null;
|
|
168
|
+
const contents = (_jsxs(_Fragment, { children: [title === undefined ? null : heading(node.level, titleId, title), node.description === undefined ? null : _jsx("p", { className: "ax-hint", children: node.description }), toggle, body] }));
|
|
169
|
+
return title === undefined ? (_jsx("div", { className: className, "data-node": node.nodeId, children: contents })) : (_jsx("section", { className: className, "aria-labelledby": titleId, "data-node": node.nodeId, children: contents }));
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The collapse toggle and the failure count it hides.
|
|
173
|
+
*
|
|
174
|
+
* It is the subscription, and it is a leaf: a failure appearing in the section repaints the
|
|
175
|
+
* count and leaves the controls inside the section alone.
|
|
176
|
+
*/
|
|
177
|
+
function SectionToggle({ nodeId, bodyId, title, expanded, onToggle, }) {
|
|
178
|
+
const slice = useLayoutNode(nodeId);
|
|
179
|
+
const copy = useHostCopy();
|
|
180
|
+
return (_jsxs("button", { type: "button", "aria-expanded": expanded, "aria-controls": bodyId, onClick: onToggle, children: [copy.sectionToggle(title, expanded), slice.errorCount === 0 ? '' : ` (${String(slice.errorCount)})`] }));
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* One wizard page at a time.
|
|
184
|
+
*
|
|
185
|
+
* The cursor is the session's, read through `useWizard`, so the renderer, the checkpoint bar
|
|
186
|
+
* and any host footer share one step without a prop carrying it through the tree. A host that
|
|
187
|
+
* gates its own navigation calls the same hook and reads the same cursor.
|
|
188
|
+
*/
|
|
189
|
+
function WizardView({ node, translate, }) {
|
|
190
|
+
const wizard = useWizard();
|
|
191
|
+
const copy = useHostCopy();
|
|
192
|
+
const current = node.children.find((step) => step.stepId === wizard.stepId) ?? node.children[0];
|
|
193
|
+
const position = current === undefined ? -1 : node.children.findIndex((step) => step.stepId === current.stepId);
|
|
194
|
+
const titles = new Map(node.children.map((step) => [step.stepId, step.title]));
|
|
195
|
+
// The page announces a step change itself: the library moves a host-owned cursor and states
|
|
196
|
+
// nothing in the live region, so without this a screen reader never learns the page moved.
|
|
197
|
+
const announcement = current === undefined || position < 0
|
|
198
|
+
? ''
|
|
199
|
+
: copy.stepAnnouncement(position + 1, node.children.length, current.title);
|
|
200
|
+
return (_jsxs("div", { className: "ax-wizard", "data-wizard": "", children: [_jsx(WizardCheckpoints, { wizard: wizard, titles: titles }), _jsx("p", { role: "status", "data-step-status": "", children: announcement }), current === undefined ? null : _jsx(StepView, { node: current, translate: translate })] }));
|
|
201
|
+
}
|
|
202
|
+
function StepView({ node, translate, }) {
|
|
203
|
+
const idPrefix = useIdPrefix();
|
|
204
|
+
const titleId = structuralId(idPrefix, node.nodeId, '.title');
|
|
205
|
+
const title = useRef(null);
|
|
206
|
+
// The step this instance last showed. Arriving at another step moves focus to its heading, so
|
|
207
|
+
// the next Tab starts inside the page the reader just moved to; the first paint keeps the
|
|
208
|
+
// focus the host (or the fixture button) already set.
|
|
209
|
+
const shown = useRef(node.stepId);
|
|
210
|
+
useEffect(() => {
|
|
211
|
+
if (shown.current === node.stepId)
|
|
212
|
+
return;
|
|
213
|
+
shown.current = node.stepId;
|
|
214
|
+
title.current?.focus();
|
|
215
|
+
}, [node.stepId]);
|
|
216
|
+
return (_jsxs("section", { className: "ax-step", "aria-labelledby": titleId, "data-step": node.stepId, children: [heading(node.level, titleId, node.title, title), node.description === undefined ? null : _jsx("p", { className: "ax-hint", children: node.description }), _jsx("div", { className: "ax-grid", style: {
|
|
217
|
+
display: 'grid',
|
|
218
|
+
gridTemplateColumns: `repeat(${String(node.columns)}, minmax(0, 1fr))`,
|
|
219
|
+
}, children: _jsx(Children, { nodes: node.children, translate: translate, enclosingLevel: node.level }) })] }));
|
|
220
|
+
}
|
|
221
|
+
/** The direction a readout's own value runs in: a number, decimal, or money amount is a left-to-right run. */
|
|
222
|
+
function readoutDirection(value) {
|
|
223
|
+
if (typeof value === 'number' || isDecimalString(value))
|
|
224
|
+
return 'ltr';
|
|
225
|
+
if (isRecord(value) && typeof value['minor'] === 'string')
|
|
226
|
+
return 'ltr';
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
/** A decided readout's text; a value the engine left undecided is a dash, never a blank cell. */
|
|
230
|
+
function readoutTextOf(value, display) {
|
|
231
|
+
return renderDisplayText(renderValueText(value, display));
|
|
232
|
+
}
|
|
233
|
+
/** A summary card: decided readouts, then the declared fields it lists, read live. */
|
|
234
|
+
function SummaryView({ node, }) {
|
|
235
|
+
const copy = useHostCopy();
|
|
236
|
+
const idPrefix = useIdPrefix();
|
|
237
|
+
const titleId = structuralId(idPrefix, node.nodeId, '.title');
|
|
238
|
+
return (_jsxs("section", { className: "ax-readout", "aria-labelledby": titleId, "data-summary": "", children: [heading(node.level, titleId, node.title), node.body === undefined ? null : _jsx("p", { children: node.body }), _jsxs("dl", { children: [node.readouts.map((readout, index) => (_jsxs("div", { children: [_jsx("dt", { children: readout.label }), _jsx("dd", { children: _jsx("bdi", { ...(readoutDirection(readout.value) === undefined ? {} : { dir: 'ltr' }), children: readoutTextOf(readout.value, copy.display) }) })] }, `${String(index)}:${readout.label}`))), node.fields.map((field) => (_jsx(SummaryField, { fieldId: field.fieldId, label: field.label }, field.fieldId)))] })] }));
|
|
239
|
+
}
|
|
240
|
+
function SummaryField({ fieldId, label, }) {
|
|
241
|
+
const field = useField(fieldId);
|
|
242
|
+
// A computed total the engine cannot decide (a row was just added with no quantity) has no
|
|
243
|
+
// text. A blank readout reads as "this total does not exist"; the dash says the same thing
|
|
244
|
+
// without the empty cell, and the label above it still names the total.
|
|
245
|
+
const text = renderDisplayText(field.text);
|
|
246
|
+
return (_jsxs("div", { children: [_jsx("dt", { children: label }), _jsx("dd", { children: _jsx("bdi", { ...(field.descriptor.primitive === 'integer' ||
|
|
247
|
+
field.descriptor.primitive === 'decimal' ||
|
|
248
|
+
field.descriptor.primitive === 'money' ||
|
|
249
|
+
field.descriptor.primitive === 'instant'
|
|
250
|
+
? { dir: 'ltr' }
|
|
251
|
+
: {}), children: text }) })] }));
|
|
252
|
+
}
|
|
253
|
+
/** A structural refusal as text; an engine refusal is expected, and the control paints it. */
|
|
254
|
+
function rowRefusalText(error, copy) {
|
|
255
|
+
return error instanceof Error ? error.message : copy.rowRefused();
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* The name a row's group announces.
|
|
259
|
+
*
|
|
260
|
+
* The position is part of the name so two rows of one record can never share it, and the row's
|
|
261
|
+
* own name follows when it holds one: a reader hears "Row 2: kitchen" and knows both where they
|
|
262
|
+
* are and which row they are in.
|
|
263
|
+
*/
|
|
264
|
+
function rowLabelOf(rowName, index, copy) {
|
|
265
|
+
return copy.rowHeading(index + 1, rowName ?? '');
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* A single, non-repeating record: one group whose members are controls.
|
|
269
|
+
*
|
|
270
|
+
* A record has exactly one value, so there is no row list, no add button and no row key: each
|
|
271
|
+
* member binds by its own identifier and reads or writes its cell inside the container's value.
|
|
272
|
+
* The `<fieldset>`/`<legend>` pair is the group the record is, and the `<legend>` is the place
|
|
273
|
+
* its heading comes from — the declaration's own label, beneath the section or group heading
|
|
274
|
+
* the layout already placed it in.
|
|
275
|
+
*/
|
|
276
|
+
function RecordView({ node, translate, rowKey, }) {
|
|
277
|
+
const idPrefix = useIdPrefix();
|
|
278
|
+
const binding = rowKey === undefined ? node.fieldId : fieldKey({ fieldId: node.fieldId, rowKey });
|
|
279
|
+
return (_jsxs("fieldset", { id: fieldDomId(idPrefix, binding), "data-record": node.fieldId,
|
|
280
|
+
// A summary item about the record as a whole links at this element, so it has to be a
|
|
281
|
+
// focus destination: `-1` is programmatic focus without a new tab stop.
|
|
282
|
+
tabIndex: -1, children: [_jsx("legend", { children: node.label }), node.description === undefined ? null : _jsx("p", { className: "ax-hint", children: node.description }), node.members.map((member) => (_jsx(MemberView, { member: member, translate: translate, ...(rowKey === undefined ? {} : { rowKey }) }, member.nodeId)))] }));
|
|
283
|
+
}
|
|
284
|
+
/** One record member: a control, a nested record, or a nested repeating record. */
|
|
285
|
+
function MemberView({ member, translate, rowKey, }) {
|
|
286
|
+
const copy = useHostCopy();
|
|
287
|
+
switch (member.kind) {
|
|
288
|
+
case 'field':
|
|
289
|
+
return (_jsx(Control, { node: member, translate: translate, ...(rowKey === undefined ? {} : { rowKey }) }));
|
|
290
|
+
case 'record':
|
|
291
|
+
return (_jsx(RecordView, { node: member, translate: translate, ...(rowKey === undefined ? {} : { rowKey }) }));
|
|
292
|
+
case 'records':
|
|
293
|
+
// v1 addresses no row for an array nested inside another array's row: the engine refuses
|
|
294
|
+
// the binding (`IDENTITY_UNSUPPORTED`), so there is no list a host could paint here. Say
|
|
295
|
+
// so in a sentence instead of throwing the form away.
|
|
296
|
+
return rowKey === undefined ? (_jsx(RecordsView, { node: member, translate: translate })) : (_jsx("p", { className: "ax-hint", "data-nested-records": member.fieldId, children: copy.nestedListRefused() }));
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* A repeating record: one row per committed identity, each member bound by its row key.
|
|
301
|
+
*
|
|
302
|
+
* The rows are the session's own identities, so an edit in one row re-renders that row and the
|
|
303
|
+
* list does not move. A row the container permits removing gets a button that names the row's
|
|
304
|
+
* own key, because a position is not an identity once a row has been removed. A structural
|
|
305
|
+
* refusal is painted rather than thrown out of the handler, and a successful removal moves focus
|
|
306
|
+
* to the next row's control and announces how many remain, so focus never falls to the body.
|
|
307
|
+
*/
|
|
308
|
+
function RecordsView({ node, translate, }) {
|
|
309
|
+
const array = useFieldArray(node.fieldId);
|
|
310
|
+
const idPrefix = useIdPrefix();
|
|
311
|
+
const copy = useHostCopy();
|
|
312
|
+
const [refusal, setRefusal] = useState(undefined);
|
|
313
|
+
const [status, setStatus] = useState(undefined);
|
|
314
|
+
const [focusTarget, setFocusTarget] = useState(undefined);
|
|
315
|
+
const removeButtons = useRef(new Map());
|
|
316
|
+
const addButton = useRef(null);
|
|
317
|
+
const rowsList = useRef(null);
|
|
318
|
+
// The row and control the reader was editing. A Remove button is a control the pointer uses,
|
|
319
|
+
// not the row being edited, so focus entering one does not move this place.
|
|
320
|
+
const lastEdited = useRef(undefined);
|
|
321
|
+
useEffect(() => {
|
|
322
|
+
if (focusTarget === undefined)
|
|
323
|
+
return;
|
|
324
|
+
if (focusTarget.kind === 'add') {
|
|
325
|
+
addButton.current?.focus();
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (focusTarget.kind === 'control') {
|
|
329
|
+
const rows = rowsList.current?.querySelectorAll('[data-row]');
|
|
330
|
+
const row = [...(rows ?? [])].find((entry) => entry.getAttribute('data-row') === String(focusTarget.rowKey));
|
|
331
|
+
const binding = row?.querySelector(`[data-binding="${focusTarget.binding}"]`);
|
|
332
|
+
const control = binding?.querySelector('input:not([disabled]), textarea:not([disabled]), select:not([disabled])');
|
|
333
|
+
if (control !== undefined && control !== null) {
|
|
334
|
+
control.focus({ preventScroll: true });
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
// The row survived but its control cannot take focus; leave the reader on a control that
|
|
338
|
+
// is still in this list rather than on the body.
|
|
339
|
+
const fallback = rowsList.current?.querySelector('.ax-actions button');
|
|
340
|
+
(fallback ?? addButton.current)?.focus();
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
if (focusTarget.kind === 'appended') {
|
|
344
|
+
// The appended row is the last one painted. Reading the DOM instead of the captured
|
|
345
|
+
// `array.rows` is what makes this the row the append produced, not the list before it.
|
|
346
|
+
const rows = rowsList.current?.querySelectorAll('[data-row]');
|
|
347
|
+
const last = rows === undefined ? undefined : rows[rows.length - 1];
|
|
348
|
+
// The announcement is a fact about the list after the commit, so the count is read from
|
|
349
|
+
// the same place the reader's eye is: the rows on screen.
|
|
350
|
+
if (last !== undefined && rows !== undefined) {
|
|
351
|
+
setStatus(copy.rowAdded(rows.length, rows.length));
|
|
352
|
+
// `scrollIntoView` is a browser behaviour; a DOM without layout (jsdom) has none.
|
|
353
|
+
last.scrollIntoView?.({ block: 'nearest' });
|
|
354
|
+
}
|
|
355
|
+
const control = last?.querySelector('input:not([disabled]), textarea:not([disabled]), select:not([disabled]), button');
|
|
356
|
+
control?.focus({ preventScroll: true });
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const target = removeButtons.current.get(String(focusTarget.rowKey));
|
|
360
|
+
if (target !== undefined)
|
|
361
|
+
target.focus();
|
|
362
|
+
}, [focusTarget, copy]);
|
|
363
|
+
const removeRow = (rowKey, index) => {
|
|
364
|
+
const next = array.rows[index + 1] ?? array.rows[index - 1];
|
|
365
|
+
const editing = lastEdited.current;
|
|
366
|
+
setRefusal(undefined);
|
|
367
|
+
try {
|
|
368
|
+
array.remove(rowKey);
|
|
369
|
+
}
|
|
370
|
+
catch (error) {
|
|
371
|
+
setRefusal({
|
|
372
|
+
sentence: copy.rowRemoveFailed(),
|
|
373
|
+
detail: rowRefusalText(error, copy),
|
|
374
|
+
});
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
setStatus(copy.rowRemoved(array.rows.length - 1));
|
|
378
|
+
if (editing !== undefined && String(editing.rowKey) !== String(rowKey)) {
|
|
379
|
+
// A different row was removed: the reader keeps their place in the row they were using.
|
|
380
|
+
setFocusTarget({ kind: 'control', rowKey: editing.rowKey, binding: editing.binding });
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
// The removed row is the one they were editing, so its own control is gone; the next row's
|
|
384
|
+
// control (or Add row) is the sensible place.
|
|
385
|
+
if (editing !== undefined)
|
|
386
|
+
lastEdited.current = undefined;
|
|
387
|
+
setFocusTarget(next === undefined ? { kind: 'add' } : { kind: 'remove', rowKey: next.rowKey });
|
|
388
|
+
};
|
|
389
|
+
/** Remember the editable control the reader is using, so a removal elsewhere can restore it. */
|
|
390
|
+
const rememberRowFocus = (event) => {
|
|
391
|
+
const target = event.target;
|
|
392
|
+
if (!(target instanceof HTMLElement))
|
|
393
|
+
return;
|
|
394
|
+
if (!target.matches('input, textarea, select'))
|
|
395
|
+
return;
|
|
396
|
+
const row = target.closest('[data-row]');
|
|
397
|
+
const binding = target.closest('[data-binding]')?.getAttribute('data-binding');
|
|
398
|
+
if (row === null || binding === null || binding === undefined)
|
|
399
|
+
return;
|
|
400
|
+
const rowKey = row.getAttribute('data-row');
|
|
401
|
+
if (rowKey === null)
|
|
402
|
+
return;
|
|
403
|
+
lastEdited.current = { rowKey, binding };
|
|
404
|
+
};
|
|
405
|
+
const appendRow = () => {
|
|
406
|
+
setRefusal(undefined);
|
|
407
|
+
// The conditional is hoisted out of the `try`: React Compiler refuses a value block inside
|
|
408
|
+
// a `try`/`catch`, and this payload is an ordinary value the engine may refuse.
|
|
409
|
+
const payload = node.naturalKey === undefined ? {} : { [node.naturalKey]: freeNaturalKey(array.rows) };
|
|
410
|
+
try {
|
|
411
|
+
array.append(payload);
|
|
412
|
+
}
|
|
413
|
+
catch (error) {
|
|
414
|
+
setRefusal({
|
|
415
|
+
sentence: copy.rowAddFailed(),
|
|
416
|
+
detail: rowRefusalText(error, copy),
|
|
417
|
+
});
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
// Moving into the row, scrolling it into view and announcing the new count are one act:
|
|
421
|
+
// a row that appears below the fold with focus left on the add button is a row the reader
|
|
422
|
+
// will not find.
|
|
423
|
+
setFocusTarget({ kind: 'appended' });
|
|
424
|
+
};
|
|
425
|
+
return (_jsxs("fieldset", { id: fieldDomId(idPrefix, node.fieldId), "data-records": node.fieldId,
|
|
426
|
+
// The cardinality/screen/media items target the container itself; without `-1` the link
|
|
427
|
+
// scrolls and leaves focus on the anchor, i.e. the item points at a ghost.
|
|
428
|
+
tabIndex: -1, children: [_jsx("legend", { children: node.label }), node.description === undefined ? null : _jsx("p", { className: "ax-hint", children: node.description }), array.rows.length === 0 ? _jsx("p", { children: copy.rowsEmpty() }) : null, array.rows.length === 0 ? null : (_jsx("ul", { className: "ax-rows", "data-rows": "", ref: rowsList, onFocus: rememberRowFocus, children: array.rows.map((row, index) => {
|
|
429
|
+
const labelId = structuralId(idPrefix, node.nodeId, `.row.${String(index)}`);
|
|
430
|
+
const rowName = renderRowName(node.label, row.rowName, index);
|
|
431
|
+
// Only an authored row name joins the group heading; assigned rows and rows carrying
|
|
432
|
+
// a `freeNaturalKey` placeholder announce their position without leaking the key.
|
|
433
|
+
const headingKey = row.rowName !== undefined && row.rowName === rowName ? rowName : '';
|
|
434
|
+
return (_jsx("li", { className: "ax-row", "data-row": String(row.rowKey), children: _jsxs("div", { role: "group", "aria-labelledby": labelId, children: [_jsx("span", { id: labelId, className: "ax-visually-hidden axiom-visually-hidden", children: rowLabelOf(headingKey, index, copy) }), _jsx("span", { className: "ax-row-key", "aria-hidden": "true", children: _jsx("bdi", { children: rowName }) }), node.members.map((member) => (_jsx(MemberView, { member: member, rowKey: row.rowKey, translate: translate }, member.nodeId))), array.mutations.remove ? (_jsx("div", { className: "ax-actions", children: _jsx("button", { type: "button", ref: (element) => {
|
|
435
|
+
const key = String(row.rowKey);
|
|
436
|
+
if (element === null)
|
|
437
|
+
removeButtons.current.delete(key);
|
|
438
|
+
else
|
|
439
|
+
removeButtons.current.set(key, element);
|
|
440
|
+
}, onClick: () => removeRow(row.rowKey, index), children: copy.removeRow(rowName) }) })) : null] }) }, row.bindingKey));
|
|
441
|
+
}) })), array.mutations.append ? (_jsx("div", { className: "ax-actions", children: _jsx("button", { type: "button", ref: (element) => {
|
|
442
|
+
addButton.current = element;
|
|
443
|
+
}, onClick: appendRow, children: copy.addRow() }) })) : null, refusal === undefined ? null : (_jsxs("div", { className: "ax-error", role: "alert", children: [_jsx("p", { children: refusal.sentence }), refusal.detail === undefined ? null : _jsx("p", { className: "ax-hint", children: refusal.detail })] })), _jsx("p", { "aria-live": "polite", "data-rows-status": "", children: status })] }));
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Stable keys for one derived list.
|
|
447
|
+
*
|
|
448
|
+
* The item's own identity is the key, so a reorder moves the existing node rather than painting a
|
|
449
|
+
* different reason into it. Two items that are identical in identity are interchangeable, so the
|
|
450
|
+
* second gets an occurrence suffix instead of the position: a reorder among distinct items does
|
|
451
|
+
* not renumber anything.
|
|
452
|
+
*/
|
|
453
|
+
function withKeys(items, identity) {
|
|
454
|
+
const seen = new Map();
|
|
455
|
+
return items.map((item) => {
|
|
456
|
+
const base = identity(item);
|
|
457
|
+
const occurrence = seen.get(base) ?? 0;
|
|
458
|
+
seen.set(base, occurrence + 1);
|
|
459
|
+
return { item, key: occurrence === 0 ? base : `${base}\u0000${String(occurrence)}` };
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
/** Every blocking reason, announced, and the element a refused submit moves focus to. */
|
|
463
|
+
function ErrorSummaryView({ items, failed, summaryId, }) {
|
|
464
|
+
const copy = useHostCopy();
|
|
465
|
+
return (_jsxs("div", { id: summaryId, className: "ax-summary",
|
|
466
|
+
// A painted failure is an alert; a list of questions still waiting is a polite status.
|
|
467
|
+
// Both are focusable so a refused submit always has somewhere to move the user.
|
|
468
|
+
role: failed ? 'alert' : 'status', tabIndex: -1, "data-error-summary": "", "aria-live": failed ? 'assertive' : 'polite', children: [failed ? _jsx("p", { className: "ax-summary-title", children: copy.summaryHeading() }) : null, _jsx("ul", { children: withKeys(items, (item) => `${item.targetId ?? ''}\u0000${item.message}`).map(({ item, key }) => {
|
|
469
|
+
// A reason about the record has no control to reach; linking it at this element
|
|
470
|
+
// would be a link to itself, so it is painted as plain text and the reader stays
|
|
471
|
+
// where they are.
|
|
472
|
+
const href = item.targetId === undefined || item.targetId === summaryId
|
|
473
|
+
? undefined
|
|
474
|
+
: `#${item.targetId}`;
|
|
475
|
+
const text = item.message;
|
|
476
|
+
return (_jsx("li", { className: "ax-summary-item", children: href === undefined ? text : _jsx("a", { href: href, children: text }) }, key));
|
|
477
|
+
}) })] }));
|
|
478
|
+
}
|
|
479
|
+
export function useIssueList(fields, translate) {
|
|
480
|
+
const summary = useFormState();
|
|
481
|
+
const copy = useHostCopy();
|
|
482
|
+
const idPrefix = useIdPrefix();
|
|
483
|
+
const summaryId = structuralId(idPrefix, ERROR_SUMMARY_SUFFIX, '');
|
|
484
|
+
const failures = useMemo(() => blockingOf(
|
|
485
|
+
// The painted failures, not every fact the record carries: the session's reveal policy
|
|
486
|
+
// has already decided which of them a control is announcing now.
|
|
487
|
+
summary.visibleBlockers, summary.visibleBlockingErrors, fields, translate, copy, idPrefix), [summary, fields, translate, copy, idPrefix]);
|
|
488
|
+
const waiting = useMemo(() => summary.hasUnansweredBlockers
|
|
489
|
+
? unansweredOf(summary.unansweredBlockers, fields, translate, copy, idPrefix)
|
|
490
|
+
: [], [summary, fields, translate, copy, idPrefix]);
|
|
491
|
+
const items = useMemo(() => [...failures, ...waiting], [failures, waiting]);
|
|
492
|
+
useInvalidMarking(items);
|
|
493
|
+
return { summaryId, items, failed: failures.length > 0, count: items.length };
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* The controls the issue list names, marked `aria-invalid` for as long as they are named.
|
|
497
|
+
*
|
|
498
|
+
* The binding's own `aria-invalid` answers a decided field error; the list additionally names a
|
|
499
|
+
* requirement that is merely unanswered, and a reader who is sent to that control must be told
|
|
500
|
+
* it is what stands in the way. The marker is removed when the item leaves the list, so the
|
|
501
|
+
* attribute is a state of the announcement and never a permanent accusation.
|
|
502
|
+
*/
|
|
503
|
+
function useInvalidMarking(items) {
|
|
504
|
+
useEffect(() => {
|
|
505
|
+
const marked = [];
|
|
506
|
+
for (const item of items) {
|
|
507
|
+
if (item.targetId === undefined)
|
|
508
|
+
continue;
|
|
509
|
+
const element = document.getElementById(item.targetId);
|
|
510
|
+
if (element === null)
|
|
511
|
+
continue;
|
|
512
|
+
const control = element.matches('input, textarea, select')
|
|
513
|
+
? element
|
|
514
|
+
: element.querySelector('input, textarea, select');
|
|
515
|
+
if (control === null || control.dataset['summaryInvalid'] === '')
|
|
516
|
+
continue;
|
|
517
|
+
if (control.getAttribute('aria-invalid') === 'true')
|
|
518
|
+
continue;
|
|
519
|
+
control.setAttribute('aria-invalid', 'true');
|
|
520
|
+
control.dataset['summaryInvalid'] = '';
|
|
521
|
+
marked.push(control);
|
|
522
|
+
}
|
|
523
|
+
return () => {
|
|
524
|
+
for (const control of marked) {
|
|
525
|
+
control.removeAttribute('aria-invalid');
|
|
526
|
+
delete control.dataset['summaryInvalid'];
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
}, [items]);
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* The form itself: the chrome the layout states, and nothing that a value edit moves.
|
|
533
|
+
*
|
|
534
|
+
* The tree is built from the layout and memoised on its identity, so a keystroke that changes no
|
|
535
|
+
* visibility, message or readout does not rebuild it — the control that owns the edited value
|
|
536
|
+
* re-renders through its own binding and its siblings do not. The announced failures are a
|
|
537
|
+
* separate component for the same reason.
|
|
538
|
+
*/
|
|
539
|
+
export function RenderedForm({ fields, translate, locale, width, components, layout, }) {
|
|
540
|
+
// A pure function of the locale, not a subscribing hook: a direction that re-rendered the
|
|
541
|
+
// form on every value change would defeat the per-binding isolation the render tree exists for.
|
|
542
|
+
// Only a locale the host actually named becomes a `dir` on the tree: with no request there is
|
|
543
|
+
// no language fact to state, and a host that owns direction on an ancestor (a page holding one
|
|
544
|
+
// RTL form beside a Latin one) must not have it overridden with an invented `ltr`.
|
|
545
|
+
const direction = locale === undefined ? undefined : directionOf(locale);
|
|
546
|
+
const documentLayout = useLayout();
|
|
547
|
+
const nodes = useMemo(() => renderLayout({ layout: documentLayout, fields, translate }), [documentLayout, fields, translate]);
|
|
548
|
+
return (_jsx(LocaleContext.Provider, { value: locale, children: _jsx(LayoutTree, { nodes: nodes, translate: translate, ...(width === undefined ? {} : { width }), ...(components === undefined ? {} : { components }), ...(layout === undefined ? {} : { layout }), ...(locale === undefined ? {} : { lang: locale }), ...(direction === undefined ? {} : { direction }) }) }));
|
|
549
|
+
}
|
|
550
|
+
/** The announced failures, above the form, and the element a refused submit moves focus to. */
|
|
551
|
+
export function ErrorSummary({ fields, translate, extraItems, }) {
|
|
552
|
+
const summary = useFormState();
|
|
553
|
+
const issues = useIssueList(fields, translate);
|
|
554
|
+
const extras = extraItems ?? [];
|
|
555
|
+
// Appended on every render rather than memoised: `extras` is the caller's list and a fresh
|
|
556
|
+
// array is cheaper than a memo whose dependency changes identity anyway.
|
|
557
|
+
const items = extras.length === 0 ? issues.items : [...issues.items, ...extras];
|
|
558
|
+
const failed = issues.failed || extras.length > 0;
|
|
559
|
+
const hasStatement = items.length > 0;
|
|
560
|
+
// The submit this effect has already answered. A refusal is handled once; a later keystroke
|
|
561
|
+
// that changes `failed` or the verdict must not move focus out of the control being typed in.
|
|
562
|
+
const handledSubmit = useRef(0);
|
|
563
|
+
const idPrefix = useIdPrefix();
|
|
564
|
+
useEffect(() => {
|
|
565
|
+
if (summary.submitCount === 0 || summary.submitCount === handledSubmit.current)
|
|
566
|
+
return;
|
|
567
|
+
handledSubmit.current = summary.submitCount;
|
|
568
|
+
// A submit that succeeded is not a refusal, even if a non-blocking failure remains. The
|
|
569
|
+
// verdict is the summary's one answer to "may this be sent"; there is no second boolean.
|
|
570
|
+
if (summary.verdict === 'SUBMIT')
|
|
571
|
+
return;
|
|
572
|
+
// A refused submit moves the user to the announcement once the pass that produced it has
|
|
573
|
+
// committed; the element is this component's own, so the id is the only handle needed.
|
|
574
|
+
// A refused submit moves the reader to the first control that needs them, and the summary
|
|
575
|
+
// states the rest through its live region. A refusal with no control to move to (a record
|
|
576
|
+
// blocker, a gate) still lands on the announcement rather than on the submit button.
|
|
577
|
+
const firstInvalid = firstInvalidControl(idPrefix);
|
|
578
|
+
if (firstInvalid !== undefined) {
|
|
579
|
+
firstInvalid.focus();
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
document.getElementById(issues.summaryId)?.focus();
|
|
583
|
+
}, [summary.submitCount, summary.verdict, issues.summaryId, idPrefix]);
|
|
584
|
+
return hasStatement ? (_jsx(ErrorSummaryView, { items: items, failed: failed, summaryId: issues.summaryId })) : null;
|
|
585
|
+
}
|
|
586
|
+
/** The first control of one session the browser reports as invalid, in document order. */
|
|
587
|
+
function firstInvalidControl(idPrefix) {
|
|
588
|
+
for (const element of document.querySelectorAll('[aria-invalid="true"]')) {
|
|
589
|
+
if (element.id.startsWith(`${idPrefix}-`))
|
|
590
|
+
return element;
|
|
591
|
+
}
|
|
592
|
+
return undefined;
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Every blocking reason as the summary states it, resolved to text.
|
|
596
|
+
*
|
|
597
|
+
* Three sources make one list, because readiness is the union of them: a rule or gate blocker
|
|
598
|
+
* carries a message key and no field name, so the tree is told which field it belongs to and
|
|
599
|
+
* prepends that field's label; a missing required value is a `required` blocker whose field and
|
|
600
|
+
* resolved row name are named (the host writes the whole sentence in the selected locale, and
|
|
601
|
+
* naming the field again in the tree would announce "Full name: Full name is required"); and the
|
|
602
|
+
* binding errors state the two reasons no blocker shape carries — a refusal by the field's option
|
|
603
|
+
* domain, and a blocking screen.
|
|
604
|
+
*
|
|
605
|
+
* A reason that appears in more than one list is stated once. A `required` blocker is collected
|
|
606
|
+
* after the rule and gate reasons so the announcement keeps its order; a `validate` error is the
|
|
607
|
+
* same failure a `rule` blocker already carries under its `(ruleId, messageKey)`; and
|
|
608
|
+
* `access`/`row-key` never block.
|
|
609
|
+
*/
|
|
610
|
+
function blockingOf(blockers, errors, fields, translate, copy, idPrefix) {
|
|
611
|
+
const labels = labelKeysOf(fields);
|
|
612
|
+
const items = [];
|
|
613
|
+
const required = [];
|
|
614
|
+
const ruleKeys = new Set();
|
|
615
|
+
const gateKeys = new Set();
|
|
616
|
+
for (const blocker of blockers) {
|
|
617
|
+
// A required blocker states the host's own sentence, and its row name was resolved where the
|
|
618
|
+
// declaration's addressing is known. It is collected here and appended after the rule and
|
|
619
|
+
// gate reasons so the announcement keeps the order it had before the two lists were merged.
|
|
620
|
+
if (blocker.kind === 'required') {
|
|
621
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
622
|
+
required.push({
|
|
623
|
+
targetId: blocker.rowKey === undefined
|
|
624
|
+
? fieldDomId(idPrefix, blocker.fieldId)
|
|
625
|
+
: fieldDomId(idPrefix, fieldKey({ fieldId: blocker.fieldId, rowKey: blocker.rowKey })),
|
|
626
|
+
message: copy.required(labelKey === undefined ? blocker.fieldId : translate(labelKey), blocker.rowName),
|
|
627
|
+
});
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
// A cardinality failure carries no artifact message either, so the host states the count and
|
|
631
|
+
// the limit in its own words, naming the record they belong to.
|
|
632
|
+
if (blocker.kind === 'cardinality') {
|
|
633
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
634
|
+
const message = copy.cardinality(labelKey === undefined ? blocker.fieldId : translate(labelKey), blocker.bound, blocker.count, blocker.limit);
|
|
635
|
+
if (message === undefined)
|
|
636
|
+
continue;
|
|
637
|
+
// The sentence already names the field (the host's copy takes the label), so the item does
|
|
638
|
+
// not state `fieldId`: a `renderErrorSummary` given both would prepend the label a second
|
|
639
|
+
// time. The target is the records container, whose `fieldset` carries this same id.
|
|
640
|
+
items.push({ targetId: fieldDomId(idPrefix, blocker.fieldId), message });
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
if (blocker.kind === 'scale') {
|
|
644
|
+
// The declaration requires `maxScaleMessageKey`, so the artifact states the sentence; a
|
|
645
|
+
// hand-built artifact that omitted it names the field rather than inventing host copy.
|
|
646
|
+
items.push({
|
|
647
|
+
targetId: blocker.rowKey === undefined
|
|
648
|
+
? fieldDomId(idPrefix, blocker.fieldId)
|
|
649
|
+
: fieldDomId(idPrefix, fieldKey({ fieldId: blocker.fieldId, rowKey: blocker.rowKey })),
|
|
650
|
+
message: blocker.messageKey === undefined ? blocker.fieldId : translate(blocker.messageKey),
|
|
651
|
+
});
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
654
|
+
if (blocker.kind === 'media') {
|
|
655
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
656
|
+
const label = labelKey === undefined ? blocker.fieldId : translate(labelKey);
|
|
657
|
+
items.push({
|
|
658
|
+
targetId: fieldDomId(idPrefix, blocker.fieldId),
|
|
659
|
+
message: copy.mediaConstraint(label, blocker.member, blocker.limit, blocker.value),
|
|
660
|
+
});
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
if (blocker.kind === 'rowCount') {
|
|
664
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
665
|
+
const label = labelKey === undefined ? blocker.fieldId : translate(labelKey);
|
|
666
|
+
items.push({
|
|
667
|
+
targetId: fieldDomId(idPrefix, blocker.fieldId),
|
|
668
|
+
message: copy.rowCount(label, blocker.expected, blocker.actual, blocker.source, blocker.reason, blocker.ceiling),
|
|
669
|
+
});
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
if (blocker.kind === 'invalidOption') {
|
|
673
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
674
|
+
const label = labelKey === undefined ? blocker.fieldId : translate(labelKey);
|
|
675
|
+
items.push({
|
|
676
|
+
targetId: fieldDomId(idPrefix, blocker.fieldId),
|
|
677
|
+
message: copy.invalidValue(label),
|
|
678
|
+
});
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
if (blocker.kind === 'unavailableOption') {
|
|
682
|
+
const targetId = fieldDomId(idPrefix, blocker.rowKey === undefined
|
|
683
|
+
? blocker.fieldId
|
|
684
|
+
: fieldKey({ fieldId: blocker.fieldId, rowKey: blocker.rowKey }));
|
|
685
|
+
if (blocker.messageKey !== undefined) {
|
|
686
|
+
items.push({
|
|
687
|
+
targetId,
|
|
688
|
+
message: translate(blocker.messageKey, blocker.params),
|
|
689
|
+
});
|
|
690
|
+
continue;
|
|
691
|
+
}
|
|
692
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
693
|
+
items.push({
|
|
694
|
+
targetId,
|
|
695
|
+
message: copy.unavailableOption(labelKey === undefined ? blocker.fieldId : translate(labelKey)),
|
|
696
|
+
});
|
|
697
|
+
continue;
|
|
698
|
+
}
|
|
699
|
+
if (blocker.kind === 'screen') {
|
|
700
|
+
items.push({ message: translate(blocker.messageKey ?? blocker.screenId) });
|
|
701
|
+
for (const reason of blocker.reasons) {
|
|
702
|
+
items.push({ message: translate(reason.messageKey, reason.params) });
|
|
703
|
+
}
|
|
704
|
+
continue;
|
|
705
|
+
}
|
|
706
|
+
if (blocker.kind === 'rule')
|
|
707
|
+
ruleKeys.add(ruleKeyOf(blocker.ruleId, blocker.messageKey));
|
|
708
|
+
else
|
|
709
|
+
gateKeys.add(gateKeyOf(blocker.gateId, blocker.itemId));
|
|
710
|
+
const fieldId = 'fieldId' in blocker ? blocker.fieldId : undefined;
|
|
711
|
+
// A row-scoped rule names its row, and the row's control is the element the item is about.
|
|
712
|
+
const rowKey = 'rowKey' in blocker ? blocker.rowKey : undefined;
|
|
713
|
+
// The element id, not the binding key: a natural row key is user text and an IDREF is not.
|
|
714
|
+
const targetId = fieldId === undefined || rowKey === undefined
|
|
715
|
+
? undefined
|
|
716
|
+
: fieldDomId(idPrefix, fieldKey({ fieldId, rowKey }));
|
|
717
|
+
items.push({
|
|
718
|
+
...(fieldId === undefined ? {} : { fieldId }),
|
|
719
|
+
...(targetId === undefined ? {} : { targetId }),
|
|
720
|
+
message: translate(blocker.messageKey, blocker.params),
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
for (const error of errors) {
|
|
724
|
+
switch (error.kind) {
|
|
725
|
+
case 'required':
|
|
726
|
+
// Already stated by the host's own sentence in `missingRequired`.
|
|
727
|
+
break;
|
|
728
|
+
case 'cardinality':
|
|
729
|
+
// Already stated from the blocker list, where the field it belongs to is named.
|
|
730
|
+
break;
|
|
731
|
+
case 'validate':
|
|
732
|
+
case 'deferred':
|
|
733
|
+
if (ruleKeys.has(ruleKeyOf(error.ruleId, error.messageKey)))
|
|
734
|
+
break;
|
|
735
|
+
items.push({ message: translate(error.messageKey, error.params) });
|
|
736
|
+
break;
|
|
737
|
+
case 'invalid-value':
|
|
738
|
+
// The summary has no binding for this error, so it uses the host's form-level sentence.
|
|
739
|
+
items.push({ message: copy.invalidSelection() });
|
|
740
|
+
break;
|
|
741
|
+
case 'screen':
|
|
742
|
+
// The artifact states the sentence and, for each reason its screen declared, the words
|
|
743
|
+
// to show. The screen id is identity, not text: a reader sees the authored copy, and a
|
|
744
|
+
// screen with no message falls back to its id in the projection rather than to a blank.
|
|
745
|
+
items.push({ message: translate(error.messageKey) });
|
|
746
|
+
for (const reason of error.reasons) {
|
|
747
|
+
items.push({ message: translate(reason.messageKey, reason.params) });
|
|
748
|
+
}
|
|
749
|
+
break;
|
|
750
|
+
case 'gate':
|
|
751
|
+
if (gateKeys.has(gateKeyOf(error.gateId, error.itemId)))
|
|
752
|
+
break;
|
|
753
|
+
items.push({ message: translate(error.messageKey, error.params) });
|
|
754
|
+
break;
|
|
755
|
+
case 'access':
|
|
756
|
+
case 'row-key':
|
|
757
|
+
// UI consistency, never a refusal: neither stops a submission.
|
|
758
|
+
break;
|
|
759
|
+
default:
|
|
760
|
+
assertNeverErrorKind(error);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
items.push(...required);
|
|
764
|
+
return items;
|
|
765
|
+
}
|
|
766
|
+
/**
|
|
767
|
+
* The questions still waiting for an answer, one line each.
|
|
768
|
+
*
|
|
769
|
+
* A requirement nobody has answered is a question, not a failure, so each line states that in
|
|
770
|
+
* the host's own words and links to the control that would answer it. A blocker that names no
|
|
771
|
+
* control (a gate item, a screen) is stated as its own sentence with no link.
|
|
772
|
+
*/
|
|
773
|
+
function unansweredOf(blockers, fields, translate, copy, idPrefix) {
|
|
774
|
+
const labels = labelKeysOf(fields);
|
|
775
|
+
const labelOf = (fieldId) => {
|
|
776
|
+
const key = labels.get(fieldId);
|
|
777
|
+
return key === undefined ? fieldId : translate(key);
|
|
778
|
+
};
|
|
779
|
+
const targetOf = (fieldId, rowKey) => fieldDomId(idPrefix, rowKey === undefined ? fieldKey({ fieldId }) : fieldKey({ fieldId, rowKey }));
|
|
780
|
+
return blockers.flatMap((blocker) => {
|
|
781
|
+
switch (blocker.kind) {
|
|
782
|
+
case 'required':
|
|
783
|
+
return [
|
|
784
|
+
{
|
|
785
|
+
targetId: targetOf(blocker.fieldId, blocker.rowKey),
|
|
786
|
+
// The artifact's own sentence wins when the requirement declared one; the host's
|
|
787
|
+
// generic sentence exists for a requirement the artifact states no message for.
|
|
788
|
+
message: blocker.messageKey === undefined
|
|
789
|
+
? copy.notAnswered(labelOf(blocker.fieldId), blocker.rowName)
|
|
790
|
+
: translate(blocker.messageKey, blocker.params),
|
|
791
|
+
},
|
|
792
|
+
];
|
|
793
|
+
case 'invalidOption':
|
|
794
|
+
return [
|
|
795
|
+
{
|
|
796
|
+
targetId: targetOf(blocker.fieldId, blocker.rowKey),
|
|
797
|
+
message: copy.invalidValue(labelOf(blocker.fieldId)),
|
|
798
|
+
},
|
|
799
|
+
];
|
|
800
|
+
case 'unavailableOption':
|
|
801
|
+
return [
|
|
802
|
+
{
|
|
803
|
+
targetId: targetOf(blocker.fieldId, blocker.rowKey),
|
|
804
|
+
message: blocker.messageKey === undefined
|
|
805
|
+
? copy.unavailableOption(labelOf(blocker.fieldId))
|
|
806
|
+
: translate(blocker.messageKey, blocker.params),
|
|
807
|
+
},
|
|
808
|
+
];
|
|
809
|
+
case 'cardinality': {
|
|
810
|
+
const message = copy.cardinality(labelOf(blocker.fieldId), blocker.bound, blocker.count, blocker.limit);
|
|
811
|
+
return message === undefined
|
|
812
|
+
? []
|
|
813
|
+
: [{ targetId: targetOf(blocker.fieldId, undefined), message }];
|
|
814
|
+
}
|
|
815
|
+
case 'scale':
|
|
816
|
+
return [
|
|
817
|
+
{
|
|
818
|
+
targetId: targetOf(blocker.fieldId, blocker.rowKey),
|
|
819
|
+
message: blocker.messageKey === undefined ? blocker.fieldId : translate(blocker.messageKey),
|
|
820
|
+
},
|
|
821
|
+
];
|
|
822
|
+
case 'media':
|
|
823
|
+
return [
|
|
824
|
+
{
|
|
825
|
+
targetId: targetOf(blocker.fieldId, undefined),
|
|
826
|
+
message: copy.mediaConstraint(labelOf(blocker.fieldId), blocker.member, blocker.limit, blocker.value),
|
|
827
|
+
},
|
|
828
|
+
];
|
|
829
|
+
case 'rowCount':
|
|
830
|
+
return [
|
|
831
|
+
{
|
|
832
|
+
targetId: targetOf(blocker.fieldId, undefined),
|
|
833
|
+
message: copy.rowCount(labelOf(blocker.fieldId), blocker.expected, blocker.actual, blocker.source, blocker.reason, blocker.ceiling),
|
|
834
|
+
},
|
|
835
|
+
];
|
|
836
|
+
case 'rule':
|
|
837
|
+
return [
|
|
838
|
+
{
|
|
839
|
+
...(blocker.fieldId === undefined
|
|
840
|
+
? {}
|
|
841
|
+
: { targetId: targetOf(blocker.fieldId, blocker.rowKey) }),
|
|
842
|
+
message: translate(blocker.messageKey, blocker.params),
|
|
843
|
+
},
|
|
844
|
+
];
|
|
845
|
+
case 'gate':
|
|
846
|
+
return [{ message: translate(blocker.messageKey, blocker.params) }];
|
|
847
|
+
case 'screen':
|
|
848
|
+
return [
|
|
849
|
+
{
|
|
850
|
+
message: blocker.messageKey === undefined ? blocker.screenId : translate(blocker.messageKey),
|
|
851
|
+
},
|
|
852
|
+
];
|
|
853
|
+
}
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
/** Every declared field's label key, a repeating record's members included. */
|
|
857
|
+
function labelKeysOf(fields) {
|
|
858
|
+
const labels = new Map();
|
|
859
|
+
const visit = (declarations) => {
|
|
860
|
+
for (const field of declarations) {
|
|
861
|
+
if (field.labelKey !== undefined)
|
|
862
|
+
labels.set(field.fieldId, field.labelKey);
|
|
863
|
+
if (field.fields !== undefined)
|
|
864
|
+
visit(field.fields);
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
visit(fields);
|
|
868
|
+
return labels;
|
|
869
|
+
}
|
|
870
|
+
/** The identity a rule failure shares between the blocker list and the binding errors. */
|
|
871
|
+
function ruleKeyOf(ruleId, messageKey) {
|
|
872
|
+
return `${ruleId}\u0000${messageKey}`;
|
|
873
|
+
}
|
|
874
|
+
/** The identity a gate item shares between the blocker list and the binding errors. */
|
|
875
|
+
function gateKeyOf(gateId, itemId) {
|
|
876
|
+
return `${gateId}\u0000${itemId}`;
|
|
877
|
+
}
|
|
878
|
+
/** A union arm the compiler proves unreachable. */
|
|
879
|
+
function assertNeverErrorKind(value) {
|
|
880
|
+
throw new Error(`unreachable field error: ${JSON.stringify(value)}`);
|
|
881
|
+
}
|
|
882
|
+
//# sourceMappingURL=dom.js.map
|