@azx-pbc/axiom-native 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 +95 -2
- package/dist/configurator.d.ts +39 -0
- package/dist/configurator.d.ts.map +1 -0
- package/dist/configurator.js +420 -0
- package/dist/configurator.js.map +1 -0
- package/dist/controls.d.ts +64 -0
- package/dist/controls.d.ts.map +1 -0
- package/dist/controls.js +8 -0
- package/dist/controls.js.map +1 -0
- package/dist/copy.d.ts +216 -0
- package/dist/copy.d.ts.map +1 -0
- package/dist/copy.js +2574 -0
- package/dist/copy.js.map +1 -0
- package/dist/fields.d.ts +9 -0
- package/dist/fields.d.ts.map +1 -0
- package/dist/fields.js +1365 -0
- package/dist/fields.js.map +1 -0
- package/dist/host.d.ts +90 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +8 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/input-chain.d.ts +28 -0
- package/dist/input-chain.d.ts.map +1 -0
- package/dist/input-chain.js +64 -0
- package/dist/input-chain.js.map +1 -0
- package/dist/render/layout.d.ts +47 -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/render/native.d.ts +85 -0
- package/dist/render/native.d.ts.map +1 -0
- package/dist/render/native.js +124 -0
- package/dist/render/native.js.map +1 -0
- package/dist/render/tree.d.ts +50 -0
- package/dist/render/tree.d.ts.map +1 -0
- package/dist/render/tree.js +636 -0
- package/dist/render/tree.js.map +1 -0
- package/dist/theme.d.ts +113 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +211 -0
- package/dist/theme.js.map +1 -0
- package/package.json +78 -3
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { freeNaturalKey, renderDisplayText, renderErrorSummary, renderLayout, renderRowName, renderValueText, useField, useFieldArray, useFormState, useIdPrefix, useLayout, useLayoutNode, useWizard, } from '@azx-pbc/axiom-react';
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, } from 'react';
|
|
4
|
+
import { AccessibilityInfo, Platform, Pressable, StyleSheet, Text, View, findNodeHandle, } from 'react-native';
|
|
5
|
+
import { useHostCopy } from '../copy.js';
|
|
6
|
+
import { ControlComponentsContext, useControlComponents } from '../controls.js';
|
|
7
|
+
import { Control } from '../fields.js';
|
|
8
|
+
import { InputChainContext, InputOrderContext, createInputChain } from '../input-chain.js';
|
|
9
|
+
import { NativeThemeProvider, useNativeTheme } from '../theme.js';
|
|
10
|
+
import { LayoutComponentsContext, useLayoutComponents } from './layout.js';
|
|
11
|
+
import { columnStyle, toNativeProps } from './native.js';
|
|
12
|
+
/**
|
|
13
|
+
* Projects heading accessibility props onto a `View`.
|
|
14
|
+
*
|
|
15
|
+
* `react-native-web` reads `accessibilityLevel` when `accessibilityRole` is `'header'` to emit
|
|
16
|
+
* `<h2>`..`<h6>` and `aria-level`. Real React Native on iOS and Android has no numeric heading
|
|
17
|
+
* rank in `ViewProps` (native header traits are rank-free), so native devices ignore the extra
|
|
18
|
+
* prop and announce the `'header'` role without rank.
|
|
19
|
+
*/
|
|
20
|
+
function headingViewProps(props, testID) {
|
|
21
|
+
return {
|
|
22
|
+
accessibilityRole: props.accessibilityRole,
|
|
23
|
+
accessibilityLabel: props.accessibilityLabel,
|
|
24
|
+
...(props.accessibilityLevel === undefined
|
|
25
|
+
? {}
|
|
26
|
+
: { accessibilityLevel: props.accessibilityLevel }),
|
|
27
|
+
...(testID === undefined ? {} : { testID }),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** The heading level a container slot receives: the node's own when stated, else the enclosing level. */
|
|
31
|
+
function slotLevelOf(node, enclosingLevel) {
|
|
32
|
+
switch (node.kind) {
|
|
33
|
+
case 'section':
|
|
34
|
+
case 'group':
|
|
35
|
+
case 'step':
|
|
36
|
+
case 'heading':
|
|
37
|
+
case 'sop_block':
|
|
38
|
+
case 'summary_card':
|
|
39
|
+
return node.level;
|
|
40
|
+
case 'wizard':
|
|
41
|
+
return node.children[0]?.level ?? enclosingLevel;
|
|
42
|
+
case 'record':
|
|
43
|
+
case 'records':
|
|
44
|
+
case 'callout':
|
|
45
|
+
case 'divider':
|
|
46
|
+
case 'error_summary':
|
|
47
|
+
return enclosingLevel;
|
|
48
|
+
default:
|
|
49
|
+
return assertNeverNode(node);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function LayoutTreeBody({ nodes, translate, }) {
|
|
53
|
+
const activeTheme = useNativeTheme();
|
|
54
|
+
const styles = treeStylesFor(activeTheme);
|
|
55
|
+
const parentChain = useContext(InputChainContext);
|
|
56
|
+
const parentOrder = useContext(InputOrderContext);
|
|
57
|
+
const [localChain] = useState(createInputChain);
|
|
58
|
+
const chain = parentChain ?? localChain;
|
|
59
|
+
return (_jsx(InputChainContext.Provider, { value: chain, children: _jsx(View, { style: styles.root, children: nodes.map((node, index) => (_jsx(NodeView, { node: node, translate: translate, enclosingLevel: 2, orderPath: parentOrder === undefined ? String(index) : `${parentOrder}.${String(index)}` }, node.nodeId))) }) }));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The React Native adapter: the same render tree, painted with native primitives.
|
|
63
|
+
*
|
|
64
|
+
* Every node takes its accessibility props from `toNativeProps`, so the header, label and invalid
|
|
65
|
+
* facts are the contract the node test asserts rather than a per-screen decision. The tree is the
|
|
66
|
+
* one `renderLayout` produced; nothing here asks which document it is in.
|
|
67
|
+
*/
|
|
68
|
+
export function LayoutTree({ nodes, translate, components, layout, theme: themeOverride, }) {
|
|
69
|
+
const outerComponents = useControlComponents();
|
|
70
|
+
const outerLayout = useLayoutComponents();
|
|
71
|
+
const mergedComponents = useMemo(() => components === undefined
|
|
72
|
+
? outerComponents
|
|
73
|
+
: outerComponents === undefined
|
|
74
|
+
? components
|
|
75
|
+
: { ...outerComponents, ...components }, [outerComponents, components]);
|
|
76
|
+
const mergedLayout = useMemo(() => layout === undefined
|
|
77
|
+
? outerLayout
|
|
78
|
+
: outerLayout === undefined
|
|
79
|
+
? layout
|
|
80
|
+
: { ...outerLayout, ...layout }, [outerLayout, layout]);
|
|
81
|
+
let tree = _jsx(LayoutTreeBody, { nodes: nodes, translate: translate });
|
|
82
|
+
if (layout !== undefined && mergedLayout !== undefined) {
|
|
83
|
+
tree = (_jsx(LayoutComponentsContext.Provider, { value: mergedLayout, children: tree }));
|
|
84
|
+
}
|
|
85
|
+
if (components !== undefined && mergedComponents !== undefined) {
|
|
86
|
+
tree = (_jsx(ControlComponentsContext.Provider, { value: mergedComponents, children: tree }));
|
|
87
|
+
}
|
|
88
|
+
if (themeOverride !== undefined) {
|
|
89
|
+
tree = _jsx(NativeThemeProvider, { theme: themeOverride, children: tree });
|
|
90
|
+
}
|
|
91
|
+
return tree;
|
|
92
|
+
}
|
|
93
|
+
function NodeView({ node, translate, enclosingLevel, orderPath, }) {
|
|
94
|
+
if (node.kind === 'field') {
|
|
95
|
+
return (_jsx(InputOrderContext.Provider, { value: orderPath, children: _jsx(Control, { node: node, translate: translate }) }));
|
|
96
|
+
}
|
|
97
|
+
return (_jsx(ContainerNodeView, { node: node, translate: translate, enclosingLevel: enclosingLevel, orderPath: orderPath }));
|
|
98
|
+
}
|
|
99
|
+
function ContainerNodeView({ node, translate, enclosingLevel, orderPath, }) {
|
|
100
|
+
const layoutComponents = useLayoutComponents();
|
|
101
|
+
const activeTheme = useNativeTheme();
|
|
102
|
+
const Custom = layoutComponents?.[node.kind];
|
|
103
|
+
if (Custom !== undefined) {
|
|
104
|
+
return (_jsx(Custom, { node: node, translate: translate, level: slotLevelOf(node, enclosingLevel), theme: activeTheme, children: defaultNodeView(node, translate, enclosingLevel, activeTheme, orderPath) }));
|
|
105
|
+
}
|
|
106
|
+
return defaultNodeView(node, translate, enclosingLevel, activeTheme, orderPath);
|
|
107
|
+
}
|
|
108
|
+
function defaultNodeView(node, translate, enclosingLevel, activeTheme, orderPath) {
|
|
109
|
+
switch (node.kind) {
|
|
110
|
+
case 'error_summary':
|
|
111
|
+
return _jsx(ErrorSummaryView, { node: node });
|
|
112
|
+
case 'section':
|
|
113
|
+
case 'group':
|
|
114
|
+
return _jsx(SectionView, { node: node, translate: translate, orderPath: orderPath });
|
|
115
|
+
case 'wizard':
|
|
116
|
+
return _jsx(WizardView, { node: node, translate: translate, orderPath: orderPath });
|
|
117
|
+
case 'step':
|
|
118
|
+
return _jsx(StepView, { node: node, translate: translate, orderPath: orderPath });
|
|
119
|
+
case 'record':
|
|
120
|
+
return (_jsx(RecordView, { node: node, translate: translate, enclosingLevel: enclosingLevel, orderPath: orderPath }));
|
|
121
|
+
case 'records':
|
|
122
|
+
return (_jsx(RecordsView, { node: node, translate: translate, enclosingLevel: enclosingLevel, orderPath: orderPath }));
|
|
123
|
+
case 'heading':
|
|
124
|
+
case 'sop_block': {
|
|
125
|
+
const styles = treeStylesFor(activeTheme);
|
|
126
|
+
const props = toNativeProps(node);
|
|
127
|
+
return (_jsxs(View, { ...headingViewProps(props), children: [_jsx(Text, { style: node.kind === 'heading' ? styles.heading : styles.subheading, children: node.title }), node.body === undefined ? null : _jsx(Text, { style: styles.body, children: node.body })] }));
|
|
128
|
+
}
|
|
129
|
+
case 'callout': {
|
|
130
|
+
const styles = treeStylesFor(activeTheme);
|
|
131
|
+
const props = toNativeProps(node);
|
|
132
|
+
return (_jsxs(View, { accessibilityRole: props.accessibilityRole, accessibilityLabel: props.accessibilityLabel, style: node.tone === 'danger' ? styles.calloutDanger : styles.callout, children: [node.title === undefined ? null : _jsx(Text, { style: styles.subheading, children: node.title }), _jsx(Text, { style: styles.body, children: node.body })] }));
|
|
133
|
+
}
|
|
134
|
+
case 'divider':
|
|
135
|
+
return _jsx(View, { style: treeStylesFor(activeTheme).divider });
|
|
136
|
+
case 'summary_card':
|
|
137
|
+
return _jsx(SummaryView, { node: node });
|
|
138
|
+
default:
|
|
139
|
+
return assertNeverNode(node);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function assertNeverNode(value) {
|
|
143
|
+
throw new Error(`unreachable render node: ${JSON.stringify(value)}`);
|
|
144
|
+
}
|
|
145
|
+
const WizardUiContext = createContext(undefined);
|
|
146
|
+
function SectionView({ node, translate, orderPath, }) {
|
|
147
|
+
const activeTheme = useNativeTheme();
|
|
148
|
+
const styles = treeStylesFor(activeTheme);
|
|
149
|
+
const props = toNativeProps(node);
|
|
150
|
+
const wizardUi = useContext(WizardUiContext);
|
|
151
|
+
const [localExpanded, setLocalExpanded] = useState(node.defaultExpanded ?? true);
|
|
152
|
+
const expanded = wizardUi?.expanded[node.nodeId] ?? localExpanded;
|
|
153
|
+
const shown = node.collapsible ? expanded : true;
|
|
154
|
+
const toggleExpanded = () => {
|
|
155
|
+
const next = !expanded;
|
|
156
|
+
if (wizardUi !== undefined)
|
|
157
|
+
wizardUi.setExpanded(node.nodeId, next);
|
|
158
|
+
else
|
|
159
|
+
setLocalExpanded(next);
|
|
160
|
+
};
|
|
161
|
+
// A group may state no title and a translated one may be empty: `toNativeProps` then answers
|
|
162
|
+
// no header role, and painting a `Text` heading would show a heading with nothing in it.
|
|
163
|
+
const heading = props.accessibilityRole === 'header' ? props.accessibilityLabel : undefined;
|
|
164
|
+
return (_jsxs(View, { ...headingViewProps(props), children: [heading === undefined ? null : _jsx(Text, { style: styles.sectionHeading, children: heading }), node.description === undefined ? null : _jsx(Text, { style: styles.muted, children: node.description }), node.collapsible ? (_jsx(SectionToggle, { nodeId: node.nodeId, heading: heading, expanded: expanded, onToggle: toggleExpanded, styles: styles })) : null, shown ? (_jsx(View, { style: styles.grid, children: node.children.map((child, index) => (_jsx(View, { style: columnStyle(node.columns, spanOf(child)), children: _jsx(NodeView, { node: child, translate: translate, enclosingLevel: node.level, orderPath: `${orderPath}.${String(index)}` }) }, child.nodeId))) })) : null] }));
|
|
165
|
+
}
|
|
166
|
+
/** The columns one node occupies in its parent's grid. */
|
|
167
|
+
function spanOf(node) {
|
|
168
|
+
switch (node.kind) {
|
|
169
|
+
case 'field':
|
|
170
|
+
case 'record':
|
|
171
|
+
case 'records':
|
|
172
|
+
return node.colSpan;
|
|
173
|
+
default:
|
|
174
|
+
return Number.POSITIVE_INFINITY;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/** The collapse toggle and the failure count it hides, subscribing on its own. */
|
|
178
|
+
function SectionToggle({ nodeId, heading, expanded, onToggle, styles, }) {
|
|
179
|
+
const slice = useLayoutNode(nodeId);
|
|
180
|
+
const copy = useHostCopy();
|
|
181
|
+
const name = copy.sectionToggle(heading, expanded);
|
|
182
|
+
return (_jsx(Pressable, { accessibilityRole: "button", accessibilityState: { expanded }, "aria-expanded": expanded, accessibilityLabel: name, onPress: onToggle, style: styles.option, children: _jsxs(Text, { style: styles.body, children: [name, slice.errorCount === 0 ? '' : ` (${String(slice.errorCount)})`] }) }));
|
|
183
|
+
}
|
|
184
|
+
function WizardView({ node, translate, orderPath, }) {
|
|
185
|
+
const activeTheme = useNativeTheme();
|
|
186
|
+
const styles = treeStylesFor(activeTheme);
|
|
187
|
+
const wizard = useWizard();
|
|
188
|
+
const current = node.children.find((step) => step.stepId === wizard.stepId) ?? node.children[0];
|
|
189
|
+
const [expanded, setExpandedMap] = useState({});
|
|
190
|
+
const [refusals, setRefusalMap] = useState({});
|
|
191
|
+
const setExpanded = useCallback((nodeId, next) => {
|
|
192
|
+
setExpandedMap((previous) => ({ ...previous, [nodeId]: next }));
|
|
193
|
+
}, []);
|
|
194
|
+
const setRefusal = useCallback((nodeId, next) => {
|
|
195
|
+
setRefusalMap((previous) => ({ ...previous, [nodeId]: next }));
|
|
196
|
+
}, []);
|
|
197
|
+
const uiState = useMemo(() => ({ expanded, setExpanded, refusals, setRefusal }), [expanded, setExpanded, refusals, setRefusal]);
|
|
198
|
+
return (_jsx(WizardUiContext.Provider, { value: uiState, children: _jsxs(View, { children: [_jsx(View, { style: styles.row, children: node.children.map((step) => {
|
|
199
|
+
const isCurrent = step.stepId === current?.stepId;
|
|
200
|
+
return (_jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: step.title, accessibilityState: { selected: isCurrent }, "aria-selected": isCurrent, onPress: () => wizard.setStepId(step.stepId), style: isCurrent ? styles.selected : styles.option, children: _jsx(Text, { style: styles.body, children: step.title }) }, step.stepId));
|
|
201
|
+
}) }), current === undefined ? null : (_jsx(NodeView, { node: current, translate: translate, enclosingLevel: current.level, orderPath: orderPath }))] }) }));
|
|
202
|
+
}
|
|
203
|
+
function StepView({ node, translate, orderPath, }) {
|
|
204
|
+
const activeTheme = useNativeTheme();
|
|
205
|
+
const styles = treeStylesFor(activeTheme);
|
|
206
|
+
const props = toNativeProps(node);
|
|
207
|
+
return (_jsxs(View, { ...headingViewProps(props, `step:${node.stepId}`), children: [_jsx(Text, { style: styles.subheading, children: node.title }), node.description === undefined ? null : _jsx(Text, { style: styles.muted, children: node.description }), _jsx(View, { style: styles.grid, children: node.children.map((child, index) => (_jsx(View, { style: columnStyle(node.columns, spanOf(child)), children: _jsx(NodeView, { node: child, translate: translate, enclosingLevel: node.level, orderPath: `${orderPath}.${String(index)}` }) }, child.nodeId))) })] }));
|
|
208
|
+
}
|
|
209
|
+
function SummaryView({ node, }) {
|
|
210
|
+
const activeTheme = useNativeTheme();
|
|
211
|
+
const styles = treeStylesFor(activeTheme);
|
|
212
|
+
const copy = useHostCopy();
|
|
213
|
+
const props = toNativeProps(node);
|
|
214
|
+
return (_jsxs(View, { accessibilityRole: props.accessibilityRole, accessibilityLabel: props.accessibilityLabel, style: styles.summaryCard, children: [_jsx(Text, { style: styles.subheading, children: node.title }), node.body === undefined ? null : _jsx(Text, { style: styles.body, children: node.body }), node.readouts.map((readout, index) => (_jsx(Text, { style: styles.body, children: `${readout.label}: ${renderDisplayText(renderValueText(readout.value, copy.display))}` }, `${String(index)}:${readout.label}`))), node.fields.map((field) => (_jsx(SummaryField, { fieldId: field.fieldId, label: field.label, styles: styles }, field.fieldId)))] }));
|
|
215
|
+
}
|
|
216
|
+
function SummaryField({ fieldId, label, styles, }) {
|
|
217
|
+
const field = useField(fieldId);
|
|
218
|
+
// `field.text` is the provider's display answer, so a boolean or list reads as the host
|
|
219
|
+
// states it; an undecided value uses the shared dash projection instead of painting blank.
|
|
220
|
+
return _jsx(Text, { style: styles.body, children: `${label}: ${renderDisplayText(field.text)}` });
|
|
221
|
+
}
|
|
222
|
+
/** A structural refusal's detail: the engine's own message, or the host's fallback sentence. */
|
|
223
|
+
function refusalDetail(error, copy) {
|
|
224
|
+
return error instanceof Error ? error.message : copy.rowRefused();
|
|
225
|
+
}
|
|
226
|
+
/** Names the array entry when a media constraint was raised by one, so the sentence is unambiguous. */
|
|
227
|
+
function mediaLabel(label, blocker) {
|
|
228
|
+
return blocker.index === undefined ? label : `${label} #${String(blocker.index + 1)}`;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Moves focus to a row control after a removal.
|
|
232
|
+
*
|
|
233
|
+
* A host element exposes `focus`; on a device a non-text control has only the platform's
|
|
234
|
+
* accessibility focus, which is addressed through the native tag.
|
|
235
|
+
*/
|
|
236
|
+
function focusRowControl(target) {
|
|
237
|
+
if (target === null)
|
|
238
|
+
return;
|
|
239
|
+
const focus = Reflect.get(target, 'focus');
|
|
240
|
+
if (typeof focus === 'function') {
|
|
241
|
+
Reflect.apply(focus, target, []);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (Platform.OS === 'web')
|
|
245
|
+
return;
|
|
246
|
+
const handle = findNodeHandle(target);
|
|
247
|
+
if (typeof handle === 'number')
|
|
248
|
+
AccessibilityInfo.setAccessibilityFocus(handle);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* A single record: one named group of its members' controls.
|
|
252
|
+
*
|
|
253
|
+
* A record has exactly one value, so there is no row key and no structural operation: each
|
|
254
|
+
* member binds by its own identifier and reads or writes its cell inside the container.
|
|
255
|
+
*/
|
|
256
|
+
function RecordView({ node, translate, enclosingLevel, orderPath, rowKey, }) {
|
|
257
|
+
const activeTheme = useNativeTheme();
|
|
258
|
+
const styles = treeStylesFor(activeTheme);
|
|
259
|
+
return (_jsxs(View, { accessibilityRole: "summary", accessibilityLabel: node.label, children: [_jsx(Text, { style: styles.subheading, children: node.label }), node.description === undefined ? null : _jsx(Text, { style: styles.muted, children: node.description }), node.members.map((member, index) => (_jsx(MemberView, { member: member, translate: translate, enclosingLevel: enclosingLevel, orderPath: `${orderPath}.${String(index)}`, ...(rowKey === undefined ? {} : { rowKey }) }, member.nodeId)))] }));
|
|
260
|
+
}
|
|
261
|
+
/** One record member: a control, a nested record, or a nested repeating record. */
|
|
262
|
+
function MemberView({ member, translate, enclosingLevel, orderPath, rowKey, }) {
|
|
263
|
+
if (member.kind === 'field') {
|
|
264
|
+
return (_jsx(InputOrderContext.Provider, { value: orderPath, children: _jsx(Control, { node: member, translate: translate, ...(rowKey === undefined ? {} : { rowKey }) }) }));
|
|
265
|
+
}
|
|
266
|
+
return (_jsx(ContainerMemberView, { member: member, translate: translate, enclosingLevel: enclosingLevel, orderPath: orderPath, ...(rowKey === undefined ? {} : { rowKey }) }));
|
|
267
|
+
}
|
|
268
|
+
function ContainerMemberView({ member, translate, enclosingLevel, orderPath, rowKey, }) {
|
|
269
|
+
const copy = useHostCopy();
|
|
270
|
+
const layoutComponents = useLayoutComponents();
|
|
271
|
+
const activeTheme = useNativeTheme();
|
|
272
|
+
switch (member.kind) {
|
|
273
|
+
case 'record': {
|
|
274
|
+
const content = (_jsx(RecordView, { node: member, translate: translate, enclosingLevel: enclosingLevel, orderPath: orderPath, ...(rowKey === undefined ? {} : { rowKey }) }));
|
|
275
|
+
const CustomRecord = layoutComponents?.record;
|
|
276
|
+
if (CustomRecord !== undefined) {
|
|
277
|
+
return (_jsx(CustomRecord, { node: member, translate: translate, level: enclosingLevel, theme: activeTheme, children: content }));
|
|
278
|
+
}
|
|
279
|
+
return content;
|
|
280
|
+
}
|
|
281
|
+
case 'records':
|
|
282
|
+
// v1 addresses no row for an array nested inside another array's row: the engine refuses
|
|
283
|
+
// the binding (`IDENTITY_UNSUPPORTED`), so there is no list a host could paint here. Say
|
|
284
|
+
// so in a localized sentence instead of throwing out of render.
|
|
285
|
+
return rowKey === undefined ? (_jsx(NodeView, { node: member, translate: translate, enclosingLevel: enclosingLevel, orderPath: orderPath })) : (_jsx(Text, { style: treeStylesFor(activeTheme).muted, testID: `nested-records:${member.fieldId}`, children: copy.nestedListRefused() }));
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/** Runs `array.remove` outside `RecordsView` so the component compiles with zero compiler bailouts. */
|
|
289
|
+
function tryRemoveRow(remove, rowKey, copy) {
|
|
290
|
+
try {
|
|
291
|
+
remove(rowKey);
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
return {
|
|
296
|
+
sentence: copy.rowRemoveFailed(),
|
|
297
|
+
detail: refusalDetail(error, copy),
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/** Runs `array.append` outside `RecordsView` so the component compiles with zero compiler bailouts. */
|
|
302
|
+
function tryAppendRow(append, row, copy) {
|
|
303
|
+
try {
|
|
304
|
+
append(row);
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
return {
|
|
309
|
+
sentence: copy.rowAddFailed(),
|
|
310
|
+
detail: refusalDetail(error, copy),
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* A repeating record: one row per committed identity, each member bound by its row key.
|
|
316
|
+
*
|
|
317
|
+
* The rows are the session's own identities, so an edit in one row re-renders that row and the
|
|
318
|
+
* list does not move. A row the container permits removing gets a control that names the row's
|
|
319
|
+
* own key, because a position is not an identity once a row has been removed. A structural
|
|
320
|
+
* refusal is painted rather than thrown out of the handler, and a successful removal moves
|
|
321
|
+
* accessibility focus to the next row's control and announces how many remain.
|
|
322
|
+
*/
|
|
323
|
+
function RecordsView({ node, translate, enclosingLevel, orderPath, }) {
|
|
324
|
+
const activeTheme = useNativeTheme();
|
|
325
|
+
const styles = treeStylesFor(activeTheme);
|
|
326
|
+
const array = useFieldArray(node.fieldId);
|
|
327
|
+
const copy = useHostCopy();
|
|
328
|
+
const wizardUi = useContext(WizardUiContext);
|
|
329
|
+
const [localRefusal, setLocalRefusal] = useState(undefined);
|
|
330
|
+
const refusal = wizardUi !== undefined ? wizardUi.refusals[node.nodeId] : localRefusal;
|
|
331
|
+
const setRefusal = (next) => {
|
|
332
|
+
if (wizardUi !== undefined)
|
|
333
|
+
wizardUi.setRefusal(node.nodeId, next);
|
|
334
|
+
else
|
|
335
|
+
setLocalRefusal(next);
|
|
336
|
+
};
|
|
337
|
+
const [status, setStatus] = useState(undefined);
|
|
338
|
+
const [focusTarget, setFocusTarget] = useState(undefined);
|
|
339
|
+
const removeButtons = useRef(null);
|
|
340
|
+
const addButton = useRef(null);
|
|
341
|
+
const props = toNativeProps(node);
|
|
342
|
+
useEffect(() => {
|
|
343
|
+
if (focusTarget === undefined)
|
|
344
|
+
return;
|
|
345
|
+
focusRowControl(focusTarget.kind === 'add'
|
|
346
|
+
? addButton.current
|
|
347
|
+
: (removeButtons.current?.get(String(focusTarget.rowKey)) ?? null));
|
|
348
|
+
}, [focusTarget]);
|
|
349
|
+
const removeRow = (rowKey, index) => {
|
|
350
|
+
const next = array.rows[index + 1] ?? array.rows[index - 1];
|
|
351
|
+
setRefusal(undefined);
|
|
352
|
+
const error = tryRemoveRow(array.remove, rowKey, copy);
|
|
353
|
+
if (error !== undefined) {
|
|
354
|
+
setRefusal(error);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const announcement = copy.rowRemoved(array.rows.length - 1);
|
|
358
|
+
setStatus(announcement);
|
|
359
|
+
AccessibilityInfo.announceForAccessibility(announcement);
|
|
360
|
+
setFocusTarget(next === undefined ? { kind: 'add' } : { kind: 'remove', rowKey: next.rowKey });
|
|
361
|
+
};
|
|
362
|
+
const appendRow = () => {
|
|
363
|
+
setRefusal(undefined);
|
|
364
|
+
const row = node.naturalKey === undefined ? {} : { [node.naturalKey]: freeNaturalKey(array.rows) };
|
|
365
|
+
const error = tryAppendRow(array.append, row, copy);
|
|
366
|
+
if (error !== undefined)
|
|
367
|
+
setRefusal(error);
|
|
368
|
+
};
|
|
369
|
+
return (_jsxs(View, { accessibilityRole: props.accessibilityRole, accessibilityLabel: props.accessibilityLabel, children: [_jsx(Text, { style: styles.subheading, children: node.label }), array.rows.length === 0 ? _jsx(Text, { style: styles.muted, children: copy.rowsEmpty() }) : null, array.rows.map((row, index) => {
|
|
370
|
+
const rowName = renderRowName(node.label, row.rowName, index);
|
|
371
|
+
return (_jsxs(View, { accessibilityLabel: rowName, testID: `row:${node.fieldId}:${String(row.rowKey)}`, children: [node.members.map((member, memberIndex) => (_jsx(MemberView, { member: member, rowKey: row.rowKey, enclosingLevel: enclosingLevel, orderPath: `${orderPath}.${String(index)}.${String(memberIndex)}`, translate: translate }, member.nodeId))), array.mutations.remove ? (_jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: copy.removeRow(rowName), style: styles.option, ref: (instance) => {
|
|
372
|
+
const key = String(row.rowKey);
|
|
373
|
+
if (instance === null) {
|
|
374
|
+
removeButtons.current?.delete(key);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (removeButtons.current === null) {
|
|
378
|
+
removeButtons.current = new Map();
|
|
379
|
+
}
|
|
380
|
+
removeButtons.current.set(key, instance);
|
|
381
|
+
}, onPress: () => removeRow(row.rowKey, index), children: _jsx(Text, { style: styles.body, children: copy.removeRow(rowName) }) })) : null] }, row.bindingKey));
|
|
382
|
+
}), array.mutations.append ? (_jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: copy.addRow(), style: styles.option, ref: (instance) => {
|
|
383
|
+
addButton.current = instance;
|
|
384
|
+
}, onPress: appendRow, children: _jsx(Text, { style: styles.body, children: copy.addRow() }) })) : null, refusal === undefined ? null : (_jsxs(View, { children: [_jsx(Text, { style: styles.danger, accessibilityRole: "alert", children: refusal.sentence }), refusal.detail === undefined ? null : _jsx(Text, { style: styles.muted, children: refusal.detail })] })), _jsx(Text, { style: styles.muted, accessibilityLiveRegion: "polite", children: status })] }));
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Stable keys for one derived list: the item's own identity, with an occurrence suffix only for
|
|
388
|
+
* items whose identities are identical (which are interchangeable, so the position never becomes
|
|
389
|
+
* the key).
|
|
390
|
+
*/
|
|
391
|
+
function withKeys(items, identity) {
|
|
392
|
+
const seen = new Map();
|
|
393
|
+
return items.map((item) => {
|
|
394
|
+
const base = identity(item);
|
|
395
|
+
const occurrence = seen.get(base) ?? 0;
|
|
396
|
+
seen.set(base, occurrence + 1);
|
|
397
|
+
return { item, key: occurrence === 0 ? base : `${base}\u0000${String(occurrence)}` };
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
function ErrorSummaryView({ node, }) {
|
|
401
|
+
const activeTheme = useNativeTheme();
|
|
402
|
+
const styles = treeStylesFor(activeTheme);
|
|
403
|
+
const props = toNativeProps(node);
|
|
404
|
+
return (_jsx(View, { accessibilityRole: props.accessibilityRole, accessibilityLabel: props.accessibilityLabel, style: styles.calloutDanger, children: withKeys(node.items, (item) => `${item.targetId}\u0000${item.message}`).map(({ item, key }) => (_jsx(Text, { style: styles.danger, children: item.label === undefined ? item.message : `${item.label}: ${item.message}` }, key))) }));
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* The chrome the layout states, and nothing that a value edit moves.
|
|
408
|
+
*
|
|
409
|
+
* The tree is built from the layout and memoised on its identity, so a keystroke that changes no
|
|
410
|
+
* visibility, message or readout does not rebuild it. The announced failures are their own
|
|
411
|
+
* component for the same reason.
|
|
412
|
+
*/
|
|
413
|
+
export function RenderedForm({ fields, translate, components, layout: layoutOverride, theme: themeOverride, }) {
|
|
414
|
+
const layout = useLayout();
|
|
415
|
+
const nodes = useMemo(() => renderLayout({ layout, fields, translate }), [layout, fields, translate]);
|
|
416
|
+
return (_jsx(LayoutTree, { nodes: nodes, translate: translate, ...(components === undefined ? {} : { components }), ...(layoutOverride === undefined ? {} : { layout: layoutOverride }), ...(themeOverride === undefined ? {} : { theme: themeOverride }) }));
|
|
417
|
+
}
|
|
418
|
+
/** The announced failures, above the form. */
|
|
419
|
+
export function ErrorSummary({ fields, translate, }) {
|
|
420
|
+
const summary = useFormState();
|
|
421
|
+
const copy = useHostCopy();
|
|
422
|
+
// The session's own prefix, so the announcement names this form's controls rather than the
|
|
423
|
+
// first provider's on a page that renders two.
|
|
424
|
+
const idPrefix = useIdPrefix();
|
|
425
|
+
const node = renderErrorSummary({
|
|
426
|
+
errors: blockingOf(summary.blockers, fields, translate, copy),
|
|
427
|
+
fields,
|
|
428
|
+
translate,
|
|
429
|
+
idPrefix,
|
|
430
|
+
});
|
|
431
|
+
return node === undefined ? null : (_jsx(NodeView, { node: node, translate: translate, enclosingLevel: 2, orderPath: "0" }));
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Every blocking reason as the summary states it, resolved to text.
|
|
435
|
+
*
|
|
436
|
+
* A rule or gate blocker carries a message key and no field name, so the tree is told which field
|
|
437
|
+
* it belongs to and prepends that field's label. A missing required value is a `required` blocker
|
|
438
|
+
* whose binding is named; the host writes the whole sentence in the selected locale, and naming
|
|
439
|
+
* the field again in the tree would announce "Full name: Full name is required", so it is
|
|
440
|
+
* announced from `missingRequiredFields` instead.
|
|
441
|
+
*/
|
|
442
|
+
function blockingOf(blockers, fields, translate, copy) {
|
|
443
|
+
const labels = new Map(fields.map((field) => [field.fieldId, field.labelKey]));
|
|
444
|
+
const items = [];
|
|
445
|
+
const required = [];
|
|
446
|
+
for (const blocker of blockers) {
|
|
447
|
+
// A required blocker states the host's own sentence, with the row name the runtime resolved.
|
|
448
|
+
if (blocker.kind === 'required') {
|
|
449
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
450
|
+
required.push({
|
|
451
|
+
fieldId: blocker.fieldId,
|
|
452
|
+
message: copy.required(labelKey === undefined ? blocker.fieldId : translate(labelKey), blocker.rowName),
|
|
453
|
+
});
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
const fieldId = 'fieldId' in blocker ? blocker.fieldId : undefined;
|
|
457
|
+
if (blocker.kind === 'cardinality') {
|
|
458
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
459
|
+
const label = labelKey === undefined ? blocker.fieldId : translate(labelKey);
|
|
460
|
+
const message = copy.cardinality(label, blocker.bound, blocker.count, blocker.limit);
|
|
461
|
+
if (message === undefined)
|
|
462
|
+
continue;
|
|
463
|
+
items.push({ fieldId: blocker.fieldId, message });
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
if (blocker.kind === 'media') {
|
|
467
|
+
// The artifact states no message for a capture constraint, so the host writes the sentence
|
|
468
|
+
// and names the field, the limit and the offending value (`MODEL-2.1.20`).
|
|
469
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
470
|
+
const label = labelKey === undefined ? blocker.fieldId : translate(labelKey);
|
|
471
|
+
const named = mediaLabel(label, blocker);
|
|
472
|
+
items.push({
|
|
473
|
+
fieldId: blocker.fieldId,
|
|
474
|
+
message: blocker.member === 'accept'
|
|
475
|
+
? copy.mediaConstraint(named, 'accept', blocker.limit, blocker.value)
|
|
476
|
+
: copy.mediaConstraint(named, blocker.member, blocker.limit, blocker.value),
|
|
477
|
+
});
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
if (blocker.kind === 'rowCount') {
|
|
481
|
+
// A `rowsFrom` count is host copy for the same reason: the declaration states an
|
|
482
|
+
// expectation, not a message (`MODEL-2.1.22`).
|
|
483
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
484
|
+
const label = labelKey === undefined ? blocker.fieldId : translate(labelKey);
|
|
485
|
+
items.push({
|
|
486
|
+
fieldId: blocker.fieldId,
|
|
487
|
+
message: copy.rowCount(label, blocker.expected, blocker.actual, blocker.source, blocker.reason, blocker.ceiling),
|
|
488
|
+
});
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
if (blocker.kind === 'invalidOption') {
|
|
492
|
+
// A held value the offered domain no longer lists carries no artifact message, so the
|
|
493
|
+
// host writes the sentence and names the control (`[DATA-5.1.3]`).
|
|
494
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
495
|
+
const label = labelKey === undefined ? blocker.fieldId : translate(labelKey);
|
|
496
|
+
items.push({ fieldId: blocker.fieldId, message: copy.invalidValue(label) });
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
if (blocker.kind === 'unavailableOption') {
|
|
500
|
+
// The domain offers the held code and this record may not choose it, which is a different
|
|
501
|
+
// cause from an unoffered value (`RULES-7.1.6`). A declaration or page that states its
|
|
502
|
+
// reason speaks it; otherwise the host writes the sentence.
|
|
503
|
+
const labelKey = labels.get(blocker.fieldId);
|
|
504
|
+
const label = labelKey === undefined ? blocker.fieldId : translate(labelKey);
|
|
505
|
+
items.push({
|
|
506
|
+
fieldId: blocker.fieldId,
|
|
507
|
+
message: blocker.messageKey === undefined
|
|
508
|
+
? copy.unavailableOption(label)
|
|
509
|
+
: translate(blocker.messageKey, blocker.params),
|
|
510
|
+
});
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
if (blocker.kind === 'scale') {
|
|
514
|
+
// The declaration requires `maxScaleMessageKey`, so the artifact states the sentence; a
|
|
515
|
+
// hand-built artifact that omitted it names the field rather than inventing host copy.
|
|
516
|
+
items.push({
|
|
517
|
+
fieldId: blocker.fieldId,
|
|
518
|
+
message: blocker.messageKey === undefined ? blocker.fieldId : translate(blocker.messageKey),
|
|
519
|
+
});
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
if (blocker.kind === 'screen') {
|
|
523
|
+
// A screen's decision is form-level; its declared sentence and reasons are the words.
|
|
524
|
+
items.push({ message: translate(blocker.messageKey ?? blocker.screenId) });
|
|
525
|
+
for (const reason of blocker.reasons) {
|
|
526
|
+
items.push({ message: translate(reason.messageKey, reason.params) });
|
|
527
|
+
}
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
items.push({
|
|
531
|
+
...(fieldId === undefined ? {} : { fieldId }),
|
|
532
|
+
message: translate(blocker.messageKey, blocker.params),
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
items.push(...required);
|
|
536
|
+
return items;
|
|
537
|
+
}
|
|
538
|
+
function createTreeStyles(activeTheme) {
|
|
539
|
+
return StyleSheet.create({
|
|
540
|
+
root: {
|
|
541
|
+
backgroundColor: activeTheme.color.background,
|
|
542
|
+
},
|
|
543
|
+
heading: {
|
|
544
|
+
fontSize: activeTheme.text.title,
|
|
545
|
+
fontWeight: activeTheme.weight.bold,
|
|
546
|
+
color: activeTheme.color.ink,
|
|
547
|
+
},
|
|
548
|
+
sectionHeading: {
|
|
549
|
+
fontSize: activeTheme.text.section,
|
|
550
|
+
fontWeight: activeTheme.weight.bold,
|
|
551
|
+
marginTop: activeTheme.space.large,
|
|
552
|
+
color: activeTheme.color.ink,
|
|
553
|
+
},
|
|
554
|
+
subheading: {
|
|
555
|
+
fontSize: activeTheme.text.section,
|
|
556
|
+
fontWeight: activeTheme.weight.semibold,
|
|
557
|
+
marginTop: activeTheme.space.medium,
|
|
558
|
+
color: activeTheme.color.ink,
|
|
559
|
+
},
|
|
560
|
+
body: {
|
|
561
|
+
fontSize: activeTheme.text.body,
|
|
562
|
+
color: activeTheme.color.ink,
|
|
563
|
+
},
|
|
564
|
+
muted: {
|
|
565
|
+
fontSize: activeTheme.text.caption,
|
|
566
|
+
color: activeTheme.color.muted,
|
|
567
|
+
},
|
|
568
|
+
danger: {
|
|
569
|
+
fontSize: activeTheme.text.body,
|
|
570
|
+
color: activeTheme.color.danger,
|
|
571
|
+
},
|
|
572
|
+
callout: {
|
|
573
|
+
padding: activeTheme.space.medium,
|
|
574
|
+
borderWidth: 1,
|
|
575
|
+
borderColor: activeTheme.color.border,
|
|
576
|
+
borderRadius: activeTheme.radius.panel,
|
|
577
|
+
backgroundColor: activeTheme.color.surface,
|
|
578
|
+
gap: activeTheme.space.tight,
|
|
579
|
+
},
|
|
580
|
+
calloutDanger: {
|
|
581
|
+
padding: activeTheme.space.medium,
|
|
582
|
+
borderWidth: 1,
|
|
583
|
+
borderColor: activeTheme.color.danger,
|
|
584
|
+
borderRadius: activeTheme.radius.panel,
|
|
585
|
+
backgroundColor: activeTheme.color.surface,
|
|
586
|
+
gap: activeTheme.space.tight,
|
|
587
|
+
},
|
|
588
|
+
summaryCard: {
|
|
589
|
+
padding: activeTheme.space.medium,
|
|
590
|
+
borderWidth: 1,
|
|
591
|
+
borderColor: activeTheme.color.border,
|
|
592
|
+
borderRadius: activeTheme.radius.panel,
|
|
593
|
+
backgroundColor: activeTheme.color.surface,
|
|
594
|
+
gap: activeTheme.space.tight,
|
|
595
|
+
},
|
|
596
|
+
divider: {
|
|
597
|
+
height: 1,
|
|
598
|
+
backgroundColor: activeTheme.color.border,
|
|
599
|
+
marginVertical: activeTheme.space.medium,
|
|
600
|
+
},
|
|
601
|
+
row: { flexDirection: 'row', flexWrap: 'wrap', gap: activeTheme.space.small },
|
|
602
|
+
grid: { flexDirection: 'row', flexWrap: 'wrap' },
|
|
603
|
+
option: {
|
|
604
|
+
paddingHorizontal: activeTheme.space.medium,
|
|
605
|
+
paddingVertical: activeTheme.space.tight,
|
|
606
|
+
minHeight: activeTheme.size.target,
|
|
607
|
+
minWidth: activeTheme.size.target,
|
|
608
|
+
justifyContent: 'center',
|
|
609
|
+
borderWidth: 1,
|
|
610
|
+
borderColor: activeTheme.color.border,
|
|
611
|
+
borderRadius: activeTheme.radius.chip,
|
|
612
|
+
backgroundColor: activeTheme.color.surface,
|
|
613
|
+
},
|
|
614
|
+
selected: {
|
|
615
|
+
paddingHorizontal: activeTheme.space.medium,
|
|
616
|
+
paddingVertical: activeTheme.space.tight,
|
|
617
|
+
minHeight: activeTheme.size.target,
|
|
618
|
+
minWidth: activeTheme.size.target,
|
|
619
|
+
justifyContent: 'center',
|
|
620
|
+
borderWidth: 2,
|
|
621
|
+
borderColor: activeTheme.color.accent,
|
|
622
|
+
borderRadius: activeTheme.radius.chip,
|
|
623
|
+
backgroundColor: activeTheme.color.surface,
|
|
624
|
+
},
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
const TREE_STYLES = new WeakMap();
|
|
628
|
+
function treeStylesFor(activeTheme) {
|
|
629
|
+
const cached = TREE_STYLES.get(activeTheme);
|
|
630
|
+
if (cached !== undefined)
|
|
631
|
+
return cached;
|
|
632
|
+
const created = createTreeStyles(activeTheme);
|
|
633
|
+
TREE_STYLES.set(activeTheme, created);
|
|
634
|
+
return created;
|
|
635
|
+
}
|
|
636
|
+
//# sourceMappingURL=tree.js.map
|