@azx-pbc/axiom-web 0.0.0-stage → 0.0.1
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/invariant.d.ts +8 -0
- package/dist/format/invariant.d.ts.map +1 -0
- package/dist/format/invariant.js +10 -0
- package/dist/format/invariant.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
package/dist/controls.js
ADDED
|
@@ -0,0 +1,2524 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { errorKey, renderDisplayText, renderFieldText, renderValueText, useAsyncOptions, useBufferedInput, useField, useFormState, } from '@azx-pbc/axiom-react';
|
|
3
|
+
import { AxiomError, WriteRefusedError, canonicalizeRestricted, isRecord, mediaAcceptMatches, optionCodeValue, sameWireValue, } from '@azx-pbc/axiom-runtime';
|
|
4
|
+
import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react';
|
|
5
|
+
import { formCopy, useHostCopy } from './copy.js';
|
|
6
|
+
import { useHostActions, useLocale } from './host.js';
|
|
7
|
+
import { AddressEditor } from './address.js';
|
|
8
|
+
import { CoordinateEditor } from './coordinate.js';
|
|
9
|
+
import { DatePicker } from './pickers/DatePicker.js';
|
|
10
|
+
import { TimeSegments } from './pickers/TimeSegments.js';
|
|
11
|
+
import { ScheduleField } from './schedule/SchedulePicker.js';
|
|
12
|
+
import { scheduleText } from './schedule/timeslot.js';
|
|
13
|
+
import { friendlyMediaType, humanSize, isImageMedia, mediaBytes, mediaDisplayLine, mediaLine, mediaMime, } from './media.js';
|
|
14
|
+
import { isCanonicalMoneyText } from './format/money.js';
|
|
15
|
+
import { formatDecimal, groupCanonicalNumericText, isDecimalString } from './format/number.js';
|
|
16
|
+
import { DEFAULT_DIGIT_POLICY } from './format/digits.js';
|
|
17
|
+
import { resolveLocale } from './format/locale.js';
|
|
18
|
+
/** The map in force for the tree; absent means every control uses the adapter's default. */
|
|
19
|
+
export const ControlComponentsContext = createContext(undefined);
|
|
20
|
+
/**
|
|
21
|
+
* The control-to-affordance table, keyed by the primitive and the hint together.
|
|
22
|
+
*
|
|
23
|
+
* The hint alone is not enough: the wire refuses a value of the wrong shape, and `writeTo`
|
|
24
|
+
* absorbs that refusal as the documented "the record would not take it" case. A plan keyed only
|
|
25
|
+
* by `control` therefore loses data silently for the two pairs where the hint's affordance and
|
|
26
|
+
* the primitive disagree — an integer `percentage` and a boolean `select`/`radio`. Each branch
|
|
27
|
+
* below states the wire type its affordance writes.
|
|
28
|
+
*
|
|
29
|
+
* A hint the compiler admits on a primitive it cannot write (a `checkbox` on a string) falls back
|
|
30
|
+
* to the primitive's own entry rather than an affordance that would be refused. `datetime`,
|
|
31
|
+
* `media_capture` and `timeslot` stay static/paged because the reference host has no capture
|
|
32
|
+
* provider for them; that is a documented capability limit, not a silent refusal.
|
|
33
|
+
*/
|
|
34
|
+
function planOf(node) {
|
|
35
|
+
const { primitive } = node.declaration;
|
|
36
|
+
switch (node.control) {
|
|
37
|
+
case 'percentage':
|
|
38
|
+
return primitive === 'integer'
|
|
39
|
+
? { family: 'buffered', format: { kind: 'integer' } }
|
|
40
|
+
: { family: 'buffered', format: { kind: 'decimal' } };
|
|
41
|
+
case 'checkbox':
|
|
42
|
+
case 'switch':
|
|
43
|
+
// A checkbox writes a JSON boolean. A string field carrying the hint keeps a text entry.
|
|
44
|
+
return primitive === 'boolean' ? { family: 'boolean' } : { family: 'text' };
|
|
45
|
+
case 'select':
|
|
46
|
+
return primitive === 'boolean' ? { family: 'boolean-choice' } : { family: 'choice' };
|
|
47
|
+
// A radio is a radio group and a combobox is a filtering picker. Painting either as a
|
|
48
|
+
// `<select>` would erase the affordance the author declared while claiming to honour it.
|
|
49
|
+
case 'radio':
|
|
50
|
+
return primitive === 'boolean' ? { family: 'boolean-choice' } : { family: 'radio-group' };
|
|
51
|
+
case 'combobox':
|
|
52
|
+
return primitive === 'boolean' ? { family: 'boolean-choice' } : { family: 'combobox' };
|
|
53
|
+
// A rating is a short scale of stops, which is a radio group of stars or numbers rather
|
|
54
|
+
// than a dropdown. `likert` keeps the picker: its domain is usually a long labelled scale.
|
|
55
|
+
case 'rating':
|
|
56
|
+
return { family: 'rating' };
|
|
57
|
+
case 'likert':
|
|
58
|
+
return { family: 'choice' };
|
|
59
|
+
case 'text':
|
|
60
|
+
case 'textarea':
|
|
61
|
+
case 'email':
|
|
62
|
+
case 'url':
|
|
63
|
+
case 'password':
|
|
64
|
+
return { family: 'text' };
|
|
65
|
+
case 'phone':
|
|
66
|
+
// A control hint implies no validation (`MODEL-3.1.3`). A national spelling such as
|
|
67
|
+
// `(415) 555-0123` writes the raw string; `is_phone` is the optional canonical check.
|
|
68
|
+
return { family: 'text' };
|
|
69
|
+
case 'integer':
|
|
70
|
+
return { family: 'buffered', format: { kind: 'integer' } };
|
|
71
|
+
case 'decimal':
|
|
72
|
+
return { family: 'buffered', format: { kind: 'decimal' } };
|
|
73
|
+
case 'money':
|
|
74
|
+
return { family: 'buffered', format: { kind: 'money' } };
|
|
75
|
+
case 'quantity':
|
|
76
|
+
// A quantity is an exact amount and a unit. The numeric entry is buffered against the
|
|
77
|
+
// declaration's scale and the picker supplies the value's second member, so one control
|
|
78
|
+
// writes `{ value, unit }` rather than a bare number the wire would refuse.
|
|
79
|
+
return { family: 'quantity' };
|
|
80
|
+
case 'slider':
|
|
81
|
+
return { family: 'slider' };
|
|
82
|
+
case 'date':
|
|
83
|
+
return { family: 'date' };
|
|
84
|
+
case 'datetime':
|
|
85
|
+
return { family: 'datetime' };
|
|
86
|
+
case 'time':
|
|
87
|
+
return { family: 'time' };
|
|
88
|
+
case 'duration':
|
|
89
|
+
return { family: 'duration' };
|
|
90
|
+
// An address and a coordinate are structured records, not scalars: the wire refuses a
|
|
91
|
+
// string where either shape is required, so a text box would silently lose the write. The
|
|
92
|
+
// matrix admits exactly one hint per primitive, and the primitive is restated here because
|
|
93
|
+
// a hint the compiler refuses on a string was historically painted as a static value.
|
|
94
|
+
case 'address':
|
|
95
|
+
return primitive === 'address' ? { family: 'address' } : { family: 'static' };
|
|
96
|
+
case 'map':
|
|
97
|
+
return primitive === 'coordinate' ? { family: 'coordinate' } : { family: 'static' };
|
|
98
|
+
case 'media_capture':
|
|
99
|
+
return { family: 'media' };
|
|
100
|
+
case 'signature_capture':
|
|
101
|
+
return { family: 'signature' };
|
|
102
|
+
case 'multiselect':
|
|
103
|
+
case 'rank':
|
|
104
|
+
case 'tags':
|
|
105
|
+
return { family: 'list' };
|
|
106
|
+
case 'timeslot':
|
|
107
|
+
return { family: 'choice' };
|
|
108
|
+
default:
|
|
109
|
+
return assertNeverControl(node.control);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** A union arm the compiler proves unreachable. */
|
|
113
|
+
function assertNeverControl(value) {
|
|
114
|
+
throw new Error(`unreachable control: ${JSON.stringify(value)}`);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* One write a control attempts, with the session's refusal kept out of the event handler.
|
|
118
|
+
*
|
|
119
|
+
* `field.enabled` is the binding's own answer — access permits a write and every `enabled` rule
|
|
120
|
+
* passes — so a refused interaction is not offered at all. The guard is still repeated at the
|
|
121
|
+
* write because access can be revoked between the render and the click, and the engine's
|
|
122
|
+
* structured refusal must never escape a DOM handler: a browser reports an uncaught handler error
|
|
123
|
+
* to the console and continues, leaving the control showing a value the record does not hold.
|
|
124
|
+
* A write refusal the session states as an `AxiomError` (a row identity that cannot be cleared,
|
|
125
|
+
* for one) is a refusal too: it is reported to the control rather than thrown, so the reader sees
|
|
126
|
+
* why the value returned and nobody sees a console error.
|
|
127
|
+
*/
|
|
128
|
+
function writeTo(field, value, onRefusal) {
|
|
129
|
+
if (!field.enabled)
|
|
130
|
+
return false;
|
|
131
|
+
try {
|
|
132
|
+
field.setValue(value);
|
|
133
|
+
onRefusal?.(undefined);
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
if (error instanceof WriteRefusedError) {
|
|
138
|
+
onRefusal?.(undefined);
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
if (error instanceof AxiomError) {
|
|
142
|
+
onRefusal?.(error.message);
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* A write a control makes by handing the binding straight to a hook, caught before it escapes.
|
|
150
|
+
*
|
|
151
|
+
* `useBufferedInput` calls `field.setValue` itself, so its `setText`/`onBlur` callbacks are the
|
|
152
|
+
* one write path in this adapter that does not pass through `writeTo`. Access may still be
|
|
153
|
+
* revoked between the render that offered the control and the keystroke that answers it, and the
|
|
154
|
+
* engine's `WriteRefusedError` must become a sentence beside the control rather than an uncaught
|
|
155
|
+
* handler error the browser reports and then continues past. An `AxiomError` is the session's
|
|
156
|
+
* structured refusal too; a programmer error is not caught here.
|
|
157
|
+
*/
|
|
158
|
+
function absorbWrite(run) {
|
|
159
|
+
try {
|
|
160
|
+
run();
|
|
161
|
+
return { kind: 'accepted' };
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
if (error instanceof WriteRefusedError || error instanceof AxiomError) {
|
|
165
|
+
return { kind: 'refused' };
|
|
166
|
+
}
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The failures one control paints.
|
|
172
|
+
*
|
|
173
|
+
* The binding already answers this under the session's reveal policy: a warning, a refused entry
|
|
174
|
+
* and the failures a visit or a submit has revealed are in `visibleErrors`, while an untouched
|
|
175
|
+
* requirement and a rule still missing an operand are not. The adapter adds no second policy.
|
|
176
|
+
*/
|
|
177
|
+
function visibleErrorsOf(field) {
|
|
178
|
+
return field.visibleErrors;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The artifact states a message key for a rule, deferred check or gate and none for the rest, so
|
|
182
|
+
* the four message-less kinds carry the host's own copy in the selected locale.
|
|
183
|
+
*/
|
|
184
|
+
function errorTextOf(error, field, translate, copy) {
|
|
185
|
+
const label = translate(field.descriptor.labelKey ?? field.fieldId);
|
|
186
|
+
switch (error.kind) {
|
|
187
|
+
case 'validate':
|
|
188
|
+
case 'deferred':
|
|
189
|
+
case 'gate':
|
|
190
|
+
return translate(error.messageKey, error.params);
|
|
191
|
+
case 'required':
|
|
192
|
+
// The artifact's own sentence wins when it declared one; the host's generic sentence
|
|
193
|
+
// exists for a requirement the artifact states no message for.
|
|
194
|
+
return error.messageKey === undefined
|
|
195
|
+
? copy.required(label, error.rowName)
|
|
196
|
+
: translate(error.messageKey, error.params);
|
|
197
|
+
case 'cardinality':
|
|
198
|
+
// A minimum of zero cannot fail, so the catalogue states no sentence and the control
|
|
199
|
+
// paints none rather than an empty paragraph.
|
|
200
|
+
return copy.cardinality(label, error.bound, error.count, error.limit) ?? '';
|
|
201
|
+
case 'invalid-value':
|
|
202
|
+
return copy.invalidValue(label);
|
|
203
|
+
case 'access':
|
|
204
|
+
return copy.notEditable(label, error.access);
|
|
205
|
+
case 'screen': {
|
|
206
|
+
const sentence = translate(error.messageKey);
|
|
207
|
+
const reasons = error.reasons.map((reason) => translate(reason.messageKey, reason.params));
|
|
208
|
+
return reasons.length === 0 ? sentence : `${sentence} ${reasons.join('; ')}`;
|
|
209
|
+
}
|
|
210
|
+
case 'row-key':
|
|
211
|
+
// The entry, not the record: the value on screen still belongs to the row it showed.
|
|
212
|
+
return copy.rowKey(label, error.rowName);
|
|
213
|
+
default:
|
|
214
|
+
return assertNeverError(error);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** A union arm the compiler proves unreachable. */
|
|
218
|
+
function assertNeverError(value) {
|
|
219
|
+
throw new Error(`unreachable field error: ${JSON.stringify(value)}`);
|
|
220
|
+
}
|
|
221
|
+
/** The classes one affordance carries: the base control hook plus its declared kind. */
|
|
222
|
+
function controlClassName(control) {
|
|
223
|
+
return `ax-control ax-control--${control}`;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* A field's visible name, wherever the family paints it.
|
|
227
|
+
*
|
|
228
|
+
* A single control takes it as its `<label>`; a radio group takes it as its `<legend>`. The
|
|
229
|
+
* words and the required marker are the same either way, so a group cannot drift into a caption
|
|
230
|
+
* that says less than the control above it would.
|
|
231
|
+
*/
|
|
232
|
+
function ControlLabel({ node, field, copy, }) {
|
|
233
|
+
return (_jsxs(_Fragment, { children: [node.label, field.a11y.required ? (_jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": "true", children: " *" }), _jsxs("span", { className: "ax-visually-hidden axiom-visually-hidden", children: [" ", copy.requiredMarker()] })] })) : null] }));
|
|
234
|
+
}
|
|
235
|
+
export function Control({ node, rowKey, translate, }) {
|
|
236
|
+
const field = useField(node.fieldId, rowKey);
|
|
237
|
+
const copy = useHostCopy();
|
|
238
|
+
const locale = useLocale();
|
|
239
|
+
const components = useContext(ControlComponentsContext);
|
|
240
|
+
const Custom = components?.[node.control] ??
|
|
241
|
+
(node.declaration.primitive === 'signature_ref'
|
|
242
|
+
? components?.signature
|
|
243
|
+
: node.declaration.primitive === 'media_ref'
|
|
244
|
+
? components?.media
|
|
245
|
+
: undefined);
|
|
246
|
+
const canCustom = Custom !== undefined &&
|
|
247
|
+
field.computation.kind === 'none' &&
|
|
248
|
+
field.withheldReason !== 'masked' &&
|
|
249
|
+
field.withheldReason !== 'write_only' &&
|
|
250
|
+
field.access !== 'masked';
|
|
251
|
+
// The binding owns the ids: the control, its failure and its description are three elements
|
|
252
|
+
// the session already names, so a row cannot collide with its sibling and this adapter invents
|
|
253
|
+
// no convention of its own.
|
|
254
|
+
const id = field.a11y.controlId;
|
|
255
|
+
const errorId = field.a11y.errorId;
|
|
256
|
+
const descriptionId = field.a11y.descriptionId;
|
|
257
|
+
// A value the record withholds is one the reader did not answer, so the note describes the
|
|
258
|
+
// record rather than a control the reader has changed. The binding owns the answer, so a masked,
|
|
259
|
+
// redacted or stored `write_only` value states it without the host naming the field again.
|
|
260
|
+
const showDeferred = field.withheld === true;
|
|
261
|
+
// The deferral is text the control is described by, so a reader who lands on the empty box is
|
|
262
|
+
// told the record holds a value the server chose not to send rather than that nobody answered.
|
|
263
|
+
const deferredId = `${id}-deferred`;
|
|
264
|
+
const hasVisibleErrors = visibleErrorsOf(field).length > 0;
|
|
265
|
+
// A visible failure and the hint are both reasons the control cannot be used as it stands, so
|
|
266
|
+
// the control is described by the concatenation rather than by whichever one wins: swapping
|
|
267
|
+
// the hint out on failure removes the help text exactly when the reader needs it.
|
|
268
|
+
const describedBy = [
|
|
269
|
+
hasVisibleErrors ? errorId : undefined,
|
|
270
|
+
node.description === undefined ? undefined : descriptionId,
|
|
271
|
+
showDeferred ? deferredId : undefined,
|
|
272
|
+
]
|
|
273
|
+
.filter((entry) => entry !== undefined)
|
|
274
|
+
.join(' ');
|
|
275
|
+
const plan = planOf(node);
|
|
276
|
+
// A caption that names a group is never a `<label for>`: pointing one at the first option let
|
|
277
|
+
// a click on the caption answer for the user. These families paint the caption as their own
|
|
278
|
+
// `<legend>` instead, and the group is the element a summary link reaches.
|
|
279
|
+
const checkboxGroup = plan.family === 'list' &&
|
|
280
|
+
node.control !== 'rank' &&
|
|
281
|
+
node.declaration.primitive === 'array' &&
|
|
282
|
+
(node.declaration.options !== undefined || node.declaration.optionSource !== undefined);
|
|
283
|
+
// The states belong on the field wrapper so the label, hint and failure can react to them; the
|
|
284
|
+
// control itself still carries `aria-invalid`/`aria-required`, which is what assistive
|
|
285
|
+
// technology reads. A value this device may not write is read-only in every branch below.
|
|
286
|
+
// A masked or stored `write_only` value has no editable control at all; a redacted value the
|
|
287
|
+
// access answer still permits a write keeps an ordinary control and states the handoff beside
|
|
288
|
+
// it. The wrapper's read-only marker follows the same answer.
|
|
289
|
+
const settled = field.withheldReason === 'masked' || field.withheldReason === 'write_only';
|
|
290
|
+
const readOnlyLike = field.computation.kind !== 'none' || settled || field.readOnly;
|
|
291
|
+
// A standalone boolean is one row: the mark and its label side by side. Its label is the
|
|
292
|
+
// choice's own `<label>`, so no second label above it is painted unless the entry is read-only
|
|
293
|
+
// or replaced by a host slot.
|
|
294
|
+
const labelBeside = !readOnlyLike && plan.family === 'boolean';
|
|
295
|
+
const labelInGroup = !readOnlyLike &&
|
|
296
|
+
((plan.family === 'radio-group' && node.declaration.options !== undefined) ||
|
|
297
|
+
plan.family === 'rating' ||
|
|
298
|
+
plan.family === 'address' ||
|
|
299
|
+
plan.family === 'coordinate' ||
|
|
300
|
+
checkboxGroup ||
|
|
301
|
+
choiceListSource(node, field) !== undefined ||
|
|
302
|
+
node.control === 'rank' ||
|
|
303
|
+
(plan.family === 'list' && node.declaration.items?.primitive === 'media_ref') ||
|
|
304
|
+
(plan.family === 'boolean-choice' && node.control === 'radio'));
|
|
305
|
+
const customPaint = canCustom && Custom !== undefined;
|
|
306
|
+
return (_jsxs("div", { className: "ax-field", "data-control": node.control, "data-binding": node.fieldId, ...(field.a11y.invalid ? { 'data-invalid': '' } : {}), ...(field.a11y.required ? { 'data-required': '' } : {}), ...(readOnlyLike ? { 'data-readonly': '' } : {}), ...(field.withheld === true ? { 'data-deferred': node.fieldId } : {}), children: [(labelBeside || labelInGroup) && !customPaint ? null : (_jsx("label", { className: "ax-label", htmlFor: id, id: `${id}-label`, children: _jsx(ControlLabel, { node: node, field: field, copy: copy }) })), customPaint && Custom !== undefined ? (_jsx(Custom, { controlId: id, descriptionId: descriptionId, errorId: errorId, field: field, value: field.value, setValue: field.setValue, trySetValue: field.trySetValue, error: visibleErrorsOf(field)[0], required: field.a11y.required, disabled: field.disabled, readOnly: field.readOnly, mayWrite: field.mayWrite, locale: locale, options: node.declaration.options, constraints: {
|
|
307
|
+
minimum: node.declaration.minimum,
|
|
308
|
+
maximum: node.declaration.maximum,
|
|
309
|
+
step: node.declaration.step,
|
|
310
|
+
units: node.declaration.units,
|
|
311
|
+
accept: node.declaration.accept,
|
|
312
|
+
minBytes: node.declaration.minBytes,
|
|
313
|
+
maxBytes: node.declaration.maxBytes,
|
|
314
|
+
scale: node.declaration.scale,
|
|
315
|
+
currency: node.declaration.currency,
|
|
316
|
+
} })) : (_jsx(Entry, { node: node, field: field, plan: plan, id: id, ...(rowKey === undefined ? {} : { rowKey }), ...(describedBy === '' ? {} : { describedBy }), translate: translate })), node.description === undefined ? null : (_jsx("p", { className: "ax-hint", id: descriptionId, children: node.description })), showDeferred ? (_jsx("p", { className: "ax-hint", id: deferredId, "data-deferred-note": "", children: copy.deferredValue() })) : null, _jsx(Errors, { id: errorId, field: field, translate: translate, copy: copy })] }));
|
|
317
|
+
}
|
|
318
|
+
/** The failures a control announces, in the artifact's order. */
|
|
319
|
+
function Errors({ id, field, translate, copy, }) {
|
|
320
|
+
const errors = visibleErrorsOf(field);
|
|
321
|
+
if (errors.length === 0)
|
|
322
|
+
return null;
|
|
323
|
+
return (_jsx("div", { id: id, children: errors.map((error) => (_jsx("p", { className: "ax-error", ...(error.kind === 'screen' ? { 'data-screen-id': error.screenId } : {}), children: errorTextOf(error, field, translate, copy) }, errorKey(error)))) }));
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* The direction a value's own script runs in, independent of the sentence around it.
|
|
327
|
+
*
|
|
328
|
+
* Digits, a decimal point and a sign are a left-to-right run even in an Arabic paragraph. A
|
|
329
|
+
* numeric value is therefore stated `ltr` inside its `<bdi>` isolation; a string, a date's own
|
|
330
|
+
* text or a host-formatted value keeps the container's direction.
|
|
331
|
+
*/
|
|
332
|
+
function valueDirection(primitive) {
|
|
333
|
+
switch (primitive) {
|
|
334
|
+
case 'integer':
|
|
335
|
+
case 'decimal':
|
|
336
|
+
case 'money':
|
|
337
|
+
case 'instant':
|
|
338
|
+
case 'coordinate':
|
|
339
|
+
return 'ltr';
|
|
340
|
+
default:
|
|
341
|
+
return undefined;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* A freeform list of text entries, typed as one comma-separated line.
|
|
346
|
+
*
|
|
347
|
+
* The comma rule is stated beside the entry through the control's own description relation,
|
|
348
|
+
* and each commit announces how many entries the record now holds.
|
|
349
|
+
*
|
|
350
|
+
* The text is a draft until it is committed, because a controlled value derived from the record
|
|
351
|
+
* erases the separator on the keystroke that types it: the record is `["one"]`, the rendered text
|
|
352
|
+
* is `one`, and a comma written into it is removed before the next character. Keeping the typed
|
|
353
|
+
* line until blur or Enter is what lets a person write a second entry at all.
|
|
354
|
+
*/
|
|
355
|
+
function FreeformListEntry({ field, shared, }) {
|
|
356
|
+
const copy = useHostCopy();
|
|
357
|
+
const committed = Array.isArray(field.value)
|
|
358
|
+
? field.value
|
|
359
|
+
.map((entry) => renderValueText(entry, copy.display, field.descriptor.items?.primitive))
|
|
360
|
+
.join(', ')
|
|
361
|
+
: '';
|
|
362
|
+
const [draft, setDraft] = useState(undefined);
|
|
363
|
+
// One live region for the entry count, always present so a committed count is announced:
|
|
364
|
+
// a status node that mounts together with its text is silent in some readers.
|
|
365
|
+
const [announcement, setAnnouncement] = useState('');
|
|
366
|
+
const text = draft ?? committed;
|
|
367
|
+
// The comma rule travels in the same description relation the control already carries, so
|
|
368
|
+
// a reader who lands on the entry hears how to write a second one before typing it.
|
|
369
|
+
const hintId = `${shared.id}-tags-hint`;
|
|
370
|
+
const described = [shared['aria-describedby'], hintId]
|
|
371
|
+
.filter((entry) => entry !== undefined && entry !== '')
|
|
372
|
+
.join(' ');
|
|
373
|
+
const commit = () => {
|
|
374
|
+
const entries = text
|
|
375
|
+
.split(',')
|
|
376
|
+
.map((entry) => entry.trim())
|
|
377
|
+
.filter((entry) => entry !== '');
|
|
378
|
+
writeTo(field, entries.length === 0 ? null : entries);
|
|
379
|
+
setDraft(undefined);
|
|
380
|
+
setAnnouncement(copy.freeformTagsCount(entries.length));
|
|
381
|
+
};
|
|
382
|
+
return (_jsxs(_Fragment, { children: [_jsx("input", { ...shared, ...(described === '' ? {} : { 'aria-describedby': described }), type: "text", value: text, onChange: (event) => setDraft(event.target.value), onBlur: () => {
|
|
383
|
+
commit();
|
|
384
|
+
shared.onBlur?.();
|
|
385
|
+
}, onKeyDown: (event) => {
|
|
386
|
+
if (event.key !== 'Enter')
|
|
387
|
+
return;
|
|
388
|
+
event.preventDefault();
|
|
389
|
+
commit();
|
|
390
|
+
} }), _jsx("p", { className: "ax-hint", id: hintId, children: copy.freeformTagsHint() }), _jsx("p", { className: "ax-hint", role: "status", "data-tags-count": "", children: announcement })] }));
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* A wire value read as text, for a control the host does not write from its own domain.
|
|
394
|
+
*
|
|
395
|
+
* A static value is still a control a reader can reach, so it carries the relations the
|
|
396
|
+
* interactive branches carry: the failure it is described by when one is visible, the help text
|
|
397
|
+
* otherwise, and `aria-invalid` when a rule fails. A `masked` value is the one case where the
|
|
398
|
+
* visible text is a placeholder and the withholding itself is the fact to announce, so a
|
|
399
|
+
* visually-hidden sentence states it inside the same output.
|
|
400
|
+
*/
|
|
401
|
+
function ReadOnlyValue({ field, id, control, extraDescribedBy, }) {
|
|
402
|
+
const copy = useHostCopy();
|
|
403
|
+
// The same relation `Control` computes: the hint, the visible failure and the deferral are
|
|
404
|
+
// three answers to "why is this what it is", so the control is described by all of them.
|
|
405
|
+
// See `Control`: the note describes a withheld value, not a control the reader has answered.
|
|
406
|
+
const showDeferred = field.withheld === true;
|
|
407
|
+
const deferredId = `${id}-deferred`;
|
|
408
|
+
const describedBy = [
|
|
409
|
+
visibleErrorsOf(field).length > 0 ? field.a11y.errorId : undefined,
|
|
410
|
+
field.a11y.descriptionId,
|
|
411
|
+
showDeferred ? deferredId : undefined,
|
|
412
|
+
extraDescribedBy,
|
|
413
|
+
]
|
|
414
|
+
.filter((entry) => entry !== undefined && entry !== '')
|
|
415
|
+
.join(' ');
|
|
416
|
+
const text = renderFieldText(field, copy.display);
|
|
417
|
+
// A computed value the engine cannot decide has no text. A blank cell reads as "this value
|
|
418
|
+
// does not exist"; the shared dash says the same without the empty box.
|
|
419
|
+
// A value this record holds and the device may not read was never entered here, so the
|
|
420
|
+
// static branch paints no placeholder of its own: the sentence beside it is the answer.
|
|
421
|
+
const shown = renderDisplayText(text, field.withheld);
|
|
422
|
+
return (_jsxs("output", { id: id,
|
|
423
|
+
// A read-only value is still a value a keyboard may copy or a summary link may land on.
|
|
424
|
+
// `-1` makes it focusable by script without adding a tab stop of its own.
|
|
425
|
+
tabIndex: -1, className: controlClassName(control), "data-readonly": field.fieldId, "aria-invalid": field.a11y.invalid, ...(describedBy === '' ? {} : { 'aria-describedby': describedBy }), children: [_jsxs("bdi", { ...(valueDirection(field.descriptor.primitive) === undefined ? {} : { dir: 'ltr' }), children: [shown, control === 'percentage' ? copy.percentageSign() : null] }), field.withheld === true ? (_jsx("span", { className: "ax-visually-hidden axiom-visually-hidden", "data-withheld": "", children: copy.withheldValue() })) : null] }));
|
|
426
|
+
}
|
|
427
|
+
function Entry({ node, field, plan, id, rowKey, describedBy, translate, }) {
|
|
428
|
+
const copy = useHostCopy();
|
|
429
|
+
const locale = useLocale();
|
|
430
|
+
const { nativeTimeslot } = useHostActions();
|
|
431
|
+
// A writer owns this value. Painting its control would offer a write the session must refuse.
|
|
432
|
+
if (field.computation.kind !== 'none')
|
|
433
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
434
|
+
// A value the record withholds is delivered as `undefined` on purpose: for a masked binding the
|
|
435
|
+
// host's stand-in (`field.text`) is the only text the page may show, and a stored `write_only`
|
|
436
|
+
// one has no readable text at all. Both paint as the static answer the record gives, and the
|
|
437
|
+
// note beside them says the value is settled elsewhere.
|
|
438
|
+
if (field.withheldReason === 'masked' || field.withheldReason === 'write_only')
|
|
439
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
440
|
+
const shared = {
|
|
441
|
+
className: controlClassName(node.control),
|
|
442
|
+
id,
|
|
443
|
+
'aria-required': field.a11y.required,
|
|
444
|
+
'aria-invalid': field.a11y.invalid,
|
|
445
|
+
'aria-describedby': describedBy,
|
|
446
|
+
'aria-busy': field.a11y.busy ? true : undefined,
|
|
447
|
+
disabled: field.disabled,
|
|
448
|
+
readOnly: field.readOnly,
|
|
449
|
+
onBlur: field.onBlur,
|
|
450
|
+
};
|
|
451
|
+
// One availability story reaches every affordance: when the record refuses or cannot decide an
|
|
452
|
+
// offered code, the options are painted as a list that states each answer in place. A held code
|
|
453
|
+
// the domain no longer offers keeps the sentence the select and the radio group state beside
|
|
454
|
+
// it, because the list is not the place to describe a value that is not an entry.
|
|
455
|
+
if (choiceListSource(node, field) === 'declared') {
|
|
456
|
+
if (field.readOnly)
|
|
457
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
458
|
+
const options = node.declaration.options ?? [];
|
|
459
|
+
const heldCode = unmatchedDeclaredCode(options, field.value);
|
|
460
|
+
return (_jsxs(_Fragment, { children: [_jsx(ChoiceList, { node: node, field: field, shared: shared, entries: declaredChoiceEntries(node, field, translate, copy), multiple: node.declaration.primitive === 'array', allowClear: plan.family === 'choice' ||
|
|
461
|
+
plan.family === 'combobox' ||
|
|
462
|
+
(plan.family === 'boolean-choice' && node.control !== 'radio') }), heldCode === undefined ? null : (_jsx("p", { className: "ax-hint", "data-held-value": "", children: heldCodeSentence(field, translate, copy) }))] }));
|
|
463
|
+
}
|
|
464
|
+
switch (plan.family) {
|
|
465
|
+
case 'buffered':
|
|
466
|
+
return (_jsx(BufferedEntry, { node: node, field: field, id: id, ...(rowKey === undefined ? {} : { rowKey }), ...(describedBy === undefined ? {} : { describedBy }), format: plan.format }));
|
|
467
|
+
case 'text':
|
|
468
|
+
return _jsx(TextEntry, { node: node, field: field, shared: shared });
|
|
469
|
+
case 'boolean':
|
|
470
|
+
// A checkbox has no `readonly` state in HTML: a `readonly` attribute is ignored and the
|
|
471
|
+
// box still toggles, which would hand the session a write it must refuse. A value the
|
|
472
|
+
// access answer leaves read-only is painted as a value instead.
|
|
473
|
+
if (field.readOnly)
|
|
474
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
475
|
+
return (_jsxs("label", { className: "ax-choice", htmlFor: shared.id, id: `${shared.id}-label`, children: [_jsx("input", { ...shared, type: "checkbox", ...(node.control === 'switch' ? { role: 'switch' } : {}), checked: field.value === true, onChange: (event) => writeTo(field, event.target.checked) }), _jsxs("span", { children: [node.label, field.a11y.required ? (_jsxs(_Fragment, { children: [_jsxs("span", { className: "ax-required", "aria-hidden": "true", children: [' ', "*"] }), _jsxs("span", { className: "ax-visually-hidden axiom-visually-hidden", children: [' ', copy.requiredMarker()] })] })) : null] })] }));
|
|
476
|
+
case 'boolean-choice':
|
|
477
|
+
if (field.readOnly)
|
|
478
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
479
|
+
return _jsx(BooleanChoice, { node: node, field: field, shared: shared, translate: translate });
|
|
480
|
+
case 'choice': {
|
|
481
|
+
// Nor does a `<select>`: an option is still selectable under a `readonly` attribute, so
|
|
482
|
+
// the same refusal would be thrown out of `onChange`. Paint the read-only answer instead.
|
|
483
|
+
if (field.readOnly)
|
|
484
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
485
|
+
if (node.control === 'timeslot' && nativeTimeslot !== true) {
|
|
486
|
+
return (_jsx(ScheduleField, { fieldId: node.fieldId, label: node.label, value: field.value, hasHostSource: node.declaration.optionSource !== undefined, ...(node.declaration.options === undefined
|
|
487
|
+
? {}
|
|
488
|
+
: { declaredOptions: node.declaration.options }), controlId: id, describedBy: describedBy, required: field.a11y.required, invalid: field.a11y.invalid, busy: field.a11y.busy, disabled: field.disabled, readOnly: field.readOnly, ...(rowKey === undefined ? {} : { rowKey }), onBlur: field.onBlur, onChange: (value) => writeTo(field, value) }));
|
|
489
|
+
}
|
|
490
|
+
const options = node.declaration.options;
|
|
491
|
+
if (options === undefined && node.declaration.optionSource !== undefined) {
|
|
492
|
+
return (_jsx(AsyncChoice, { node: node, field: field, shared: shared, ...(rowKey === undefined ? {} : { rowKey }), translate: translate }));
|
|
493
|
+
}
|
|
494
|
+
if (options === undefined)
|
|
495
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
496
|
+
// A measured option's record value is not its source code. Match through the runtime's
|
|
497
|
+
// mapping so a selected duration paints its own entry rather than the no-choice entry.
|
|
498
|
+
const selected = options.find((option) => sameWireValue(optionCodeValue(node.declaration, option.code), field.value));
|
|
499
|
+
// A held code no option matches keeps its own disabled entry: the control must not look
|
|
500
|
+
// empty while the record still holds a value, and the sentence beside it says why the
|
|
501
|
+
// entry cannot be chosen again.
|
|
502
|
+
const heldCode = unmatchedDeclaredCode(options, field.value);
|
|
503
|
+
const heldSentence = heldCode === undefined ? undefined : heldCodeSentence(field, translate, copy);
|
|
504
|
+
return (_jsxs(_Fragment, { children: [_jsxs("select", { ...shared, value: selected === undefined ? codeText(field.value) : String(selected.code), onChange: (event) => {
|
|
505
|
+
const value = event.target.value;
|
|
506
|
+
// The placeholder is the "no choice" entry: choosing it writes `null`, exactly as
|
|
507
|
+
// emptying a text input does. Reading it as an index would select the first option.
|
|
508
|
+
if (value === '') {
|
|
509
|
+
writeTo(field, null);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const chosen = options.find((option) => String(option.code) === value);
|
|
513
|
+
if (chosen !== undefined) {
|
|
514
|
+
writeTo(field, optionCodeValue(node.declaration, chosen.code));
|
|
515
|
+
}
|
|
516
|
+
}, children: [_jsx("option", { value: "", children: copy.selectPlaceholder() }), heldCode === undefined ? null : (_jsx("option", { value: heldCode, disabled: true, children: renderValueText(field.value, copy.display) })), options.map((option) => (_jsxs("option", { value: String(option.code), children: [translate(option.labelKey), option.deprecated === true ? copy.deprecatedSuffix() : null] }, String(option.code))))] }), heldSentence === undefined ? null : (_jsx("p", { className: "ax-hint", "data-held-value": "", children: heldSentence })), heldCode === undefined && options.length === 0 ? (_jsx("p", { className: "ax-hint", role: "status", "data-empty-options": "", children: copy.emptyOptions() })) : null] }));
|
|
517
|
+
}
|
|
518
|
+
case 'list':
|
|
519
|
+
return (_jsx(ListEntry, { node: node, field: field, shared: shared, ...(rowKey === undefined ? {} : { rowKey }), translate: translate }));
|
|
520
|
+
case 'radio-group':
|
|
521
|
+
case 'rating': {
|
|
522
|
+
if (field.readOnly)
|
|
523
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
524
|
+
if (node.declaration.options === undefined) {
|
|
525
|
+
if (node.declaration.optionSource?.kind === 'host') {
|
|
526
|
+
return (_jsx(AsyncChoice, { node: node, field: field, shared: shared, ...(rowKey === undefined ? {} : { rowKey }), translate: translate }));
|
|
527
|
+
}
|
|
528
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
529
|
+
}
|
|
530
|
+
return (_jsx(ChoiceRadioGroup, { node: node, field: field, shared: shared, translate: translate, variant: plan.family === 'rating' ? 'rating' : 'plain' }));
|
|
531
|
+
}
|
|
532
|
+
case 'combobox':
|
|
533
|
+
if (field.readOnly)
|
|
534
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
535
|
+
if (node.declaration.options === undefined) {
|
|
536
|
+
if (node.declaration.optionSource?.kind === 'host') {
|
|
537
|
+
return (_jsx(AsyncChoice, { node: node, field: field, shared: shared, ...(rowKey === undefined ? {} : { rowKey }), translate: translate }));
|
|
538
|
+
}
|
|
539
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
540
|
+
}
|
|
541
|
+
return _jsx(ComboboxEntry, { node: node, field: field, shared: shared, translate: translate });
|
|
542
|
+
case 'date':
|
|
543
|
+
return (_jsx(DatePicker, { controlId: id, descriptionId: field.a11y.descriptionId, errorId: field.a11y.errorId, label: node.label, value: typeof field.value === 'string' ? field.value : undefined, setValue: (value) => writeTo(field, value), required: field.a11y.required, invalid: field.a11y.invalid, disabled: field.disabled, readOnly: field.readOnly, locale: locale, minimum: node.declaration.minimum, maximum: node.declaration.maximum, onBlur: field.onBlur }));
|
|
544
|
+
case 'datetime':
|
|
545
|
+
return _jsx(DateTimeEntry, { node: node, field: field, id: id, describedBy: describedBy });
|
|
546
|
+
case 'time':
|
|
547
|
+
return _jsx(TimeEntry, { node: node, field: field, id: id, describedBy: describedBy });
|
|
548
|
+
case 'duration':
|
|
549
|
+
return _jsx(DurationEntry, { node: node, field: field, shared: shared });
|
|
550
|
+
case 'media':
|
|
551
|
+
return (_jsx(MediaEntry, { field: field, id: id, translate: translate, accept: node.declaration.accept, minBytes: node.declaration.minBytes, maxBytes: node.declaration.maxBytes }));
|
|
552
|
+
case 'signature':
|
|
553
|
+
return _jsx(SignatureEntry, { field: field, id: id, control: node.control, translate: translate });
|
|
554
|
+
case 'quantity':
|
|
555
|
+
return (_jsx(QuantityEntry, { node: node, field: field, id: id, ...(rowKey === undefined ? {} : { rowKey }), ...(describedBy === undefined ? {} : { describedBy }) }));
|
|
556
|
+
case 'slider':
|
|
557
|
+
return _jsx(SliderEntry, { node: node, field: field, shared: shared, translate: translate });
|
|
558
|
+
case 'address':
|
|
559
|
+
// A read-only address is a value, not six disabled boxes: the static reading states the
|
|
560
|
+
// whole address through the host's display formatter and offers no write at all.
|
|
561
|
+
if (field.readOnly)
|
|
562
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
563
|
+
return (_jsx(AddressEditor, { controlId: id, legend: _jsx(ControlLabel, { node: node, field: field, copy: copy }), label: node.label, value: field.value, setValue: (value) => writeTo(field, value), describedBy: describedBy, required: field.a11y.required, invalid: field.a11y.invalid, disabled: field.disabled, onBlur: field.onBlur }));
|
|
564
|
+
case 'coordinate':
|
|
565
|
+
if (field.readOnly)
|
|
566
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
567
|
+
return (_jsx(CoordinateEditor, { controlId: id, fieldId: node.fieldId, legend: _jsx(ControlLabel, { node: node, field: field, copy: copy }), label: node.label, value: field.value, setValue: (value) => writeTo(field, value), describedBy: describedBy, required: field.a11y.required, invalid: field.a11y.invalid, disabled: field.disabled, onBlur: field.onBlur }));
|
|
568
|
+
case 'static':
|
|
569
|
+
return _jsx(ReadOnlyValue, { field: field, id: id, control: node.control });
|
|
570
|
+
default:
|
|
571
|
+
return assertNeverPlan(plan);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* A captured signature as a person reads it.
|
|
576
|
+
*
|
|
577
|
+
* The wire id (`sig_1`) is an internal reference, never visible text. What matters is that the
|
|
578
|
+
* record is signed, when, and how many declared fields the signature covers; a value that does
|
|
579
|
+
* not carry those facts falls back to the ordinary read-only value.
|
|
580
|
+
*/
|
|
581
|
+
function SignatureValue({ field, id, control, extraDescribedBy, }) {
|
|
582
|
+
const copy = useHostCopy();
|
|
583
|
+
// A hook must run before the branches that fall back to the ordinary read-only value.
|
|
584
|
+
const deferred = field.withheld === true;
|
|
585
|
+
if (field.maskedText !== undefined || field.access === 'masked') {
|
|
586
|
+
return (_jsx(ReadOnlyValue, { field: field, id: id, control: control, ...(extraDescribedBy === undefined ? {} : { extraDescribedBy }) }));
|
|
587
|
+
}
|
|
588
|
+
const value = field.computation.kind === 'value' ? field.computation.value : field.value;
|
|
589
|
+
const record = isRecord(value) ? value : undefined;
|
|
590
|
+
const signedAt = record !== undefined && typeof record['signedAt'] === 'string' ? record['signedAt'] : undefined;
|
|
591
|
+
const targets = record !== undefined && Array.isArray(record['coveredTargets'])
|
|
592
|
+
? record['coveredTargets']
|
|
593
|
+
: undefined;
|
|
594
|
+
if (signedAt === undefined && targets === undefined) {
|
|
595
|
+
return (_jsx(ReadOnlyValue, { field: field, id: id, control: control, ...(extraDescribedBy === undefined ? {} : { extraDescribedBy }) }));
|
|
596
|
+
}
|
|
597
|
+
const covered = targets?.length ?? field.descriptor.covers?.length;
|
|
598
|
+
const describedBy = [
|
|
599
|
+
visibleErrorsOf(field).length > 0 ? field.a11y.errorId : undefined,
|
|
600
|
+
field.a11y.descriptionId,
|
|
601
|
+
deferred ? `${id}-deferred` : undefined,
|
|
602
|
+
extraDescribedBy,
|
|
603
|
+
]
|
|
604
|
+
.filter((entry) => entry !== undefined && entry !== '')
|
|
605
|
+
.join(' ');
|
|
606
|
+
return (_jsx("output", { id: id, tabIndex: -1, className: controlClassName(control), "data-readonly": field.fieldId, "aria-invalid": field.a11y.invalid, ...(describedBy === '' ? {} : { 'aria-describedby': describedBy }), children: _jsx("bdi", { children: copy.signed(signedAt, covered) }) }));
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* A captured signature: its value, and the host's sign action when this page has a signer.
|
|
610
|
+
*
|
|
611
|
+
* The binding is read like every other host-domain control: nothing here writes a signature the
|
|
612
|
+
* wire contract did not produce, and the action asks the host to sign the record as it stands.
|
|
613
|
+
* A page with no signer (an offline build or a host without a capture port) paints the value
|
|
614
|
+
* alongside a localized notice explaining that this device cannot sign, rather than offering a
|
|
615
|
+
* button that can only fail or leaving a blocked field unexplained.
|
|
616
|
+
*/
|
|
617
|
+
function SignatureEntry({ field, id, control, translate, }) {
|
|
618
|
+
const { captureSignature } = useHostActions();
|
|
619
|
+
const copy = useHostCopy();
|
|
620
|
+
const [failure, setFailure] = useState(undefined);
|
|
621
|
+
// A read-only viewer must not be offered the signer: `enabled` is the access answer plus every
|
|
622
|
+
// `enabled` rule, and a signature is only ever signed over a record the page may write.
|
|
623
|
+
if (!field.enabled || field.access !== 'read_write')
|
|
624
|
+
return _jsx(SignatureValue, { field: field, id: id, control: control });
|
|
625
|
+
const label = translate(field.descriptor.labelKey ?? field.fieldId);
|
|
626
|
+
if (captureSignature === undefined) {
|
|
627
|
+
const unavailableId = `${id}-signature-unavailable`;
|
|
628
|
+
return (_jsxs(_Fragment, { children: [_jsx(SignatureValue, { field: field, id: id, control: control, extraDescribedBy: unavailableId }), _jsx("p", { className: "ax-hint", id: unavailableId, children: copy.signatureUnavailable(label) })] }));
|
|
629
|
+
}
|
|
630
|
+
const paintFailure = (detail) => setFailure(detail);
|
|
631
|
+
const capture = () => {
|
|
632
|
+
setFailure(undefined);
|
|
633
|
+
// The action is host code on the other side of a boundary: a synchronous throw and a
|
|
634
|
+
// rejected promise are the same failure, and neither may escape an event handler unhandled.
|
|
635
|
+
try {
|
|
636
|
+
void captureSignature(field.fieldId, { label }).then(paintFailure, (error) => {
|
|
637
|
+
paintFailure(error instanceof Error ? error.message : copy.signerFailed());
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
catch (error) {
|
|
641
|
+
paintFailure(error instanceof Error ? error.message : copy.signerFailed());
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
return (_jsxs(_Fragment, { children: [_jsx(SignatureValue, { field: field, id: id, control: control }), _jsx("div", { className: "ax-actions", children: _jsx("button", { type: "button", className: "ax-button ax-button--secondary", onClick: capture, children: copy.captureSignature(label) }) }), failure === undefined ? null : (_jsxs("div", { className: "ax-error", role: "alert", children: [_jsx("p", { children: copy.captureFailed() }), _jsx("p", { className: "ax-hint", children: failure })] }))] }));
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* A boolean painted as a choice.
|
|
648
|
+
*
|
|
649
|
+
* The compiler admits `select`/`radio` on a boolean and states its two codes as the strings
|
|
650
|
+
* `"true"` and `"false"` — a code is the value the control writes, and a boolean's wire value is
|
|
651
|
+
* a JSON boolean. This converts the selected code back to one; writing the code itself would be
|
|
652
|
+
* refused by the wire and, because `writeTo` absorbs that refusal, would look like a dead control.
|
|
653
|
+
*/
|
|
654
|
+
function BooleanChoice({ node, field, shared, translate, }) {
|
|
655
|
+
const copy = useHostCopy();
|
|
656
|
+
const declared = node.declaration.options;
|
|
657
|
+
const options = declared === undefined
|
|
658
|
+
? [
|
|
659
|
+
{ code: 'true', labelKey: '' },
|
|
660
|
+
{ code: 'false', labelKey: '' },
|
|
661
|
+
]
|
|
662
|
+
: declared;
|
|
663
|
+
const value = field.value === true ? 'true' : field.value === false ? 'false' : '';
|
|
664
|
+
if (node.control === 'radio') {
|
|
665
|
+
return (_jsxs("fieldset", { className: "ax-radio-group", id: shared.id, tabIndex: -1, "aria-invalid": shared['aria-invalid'], "aria-describedby": shared['aria-describedby'], "aria-busy": shared['aria-busy'], disabled: shared.disabled, onBlur: shared.onBlur, children: [_jsx("legend", { className: "ax-label", children: _jsx(ControlLabel, { node: node, field: field, copy: copy }) }), options.map((option, index) => {
|
|
666
|
+
const code = String(option.code);
|
|
667
|
+
return (_jsxs("label", { className: "ax-choice", children: [_jsx("input", { id: `${shared.id}-${String(index)}`, className: "ax-control--radio", type: "radio", name: shared.id, value: code, checked: value === code, disabled: shared.disabled, "aria-required": shared['aria-required'], onChange: () => writeTo(field, code === 'true') }), _jsxs("span", { children: [option.labelKey === ''
|
|
668
|
+
? (copy.display(code === 'true') ?? code)
|
|
669
|
+
: translate(option.labelKey), option.deprecated === true ? copy.deprecatedSuffix() : null] })] }, code));
|
|
670
|
+
})] }));
|
|
671
|
+
}
|
|
672
|
+
return (_jsxs("select", { ...shared, value: value, onChange: (event) => {
|
|
673
|
+
const next = event.target.value;
|
|
674
|
+
if (next === '') {
|
|
675
|
+
writeTo(field, null);
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
writeTo(field, next === 'true');
|
|
679
|
+
}, children: [_jsx("option", { value: "", children: copy.selectPlaceholder() }), options.map((option) => {
|
|
680
|
+
const code = String(option.code);
|
|
681
|
+
return (_jsxs("option", { value: code, children: [option.labelKey === ''
|
|
682
|
+
? (copy.display(code === 'true') ?? code)
|
|
683
|
+
: translate(option.labelKey), option.deprecated === true ? copy.deprecatedSuffix() : null] }, code));
|
|
684
|
+
})] }));
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* A non-boolean choice as a real radio group, and a rating as its numbered stops.
|
|
688
|
+
*
|
|
689
|
+
* A `<select>` was the wrong affordance for both: it hides the domain behind a picker, opens a
|
|
690
|
+
* wheel on iOS, and makes a three-stop rating a dropdown. Native radios keep the whole domain
|
|
691
|
+
* visible, move as one tab stop, and take their group name from a `<legend>` — which, unlike the
|
|
692
|
+
* `<label>` a single control uses, cannot be clicked to answer for the user.
|
|
693
|
+
*
|
|
694
|
+
* A rating's stops keep the declared labels (usually numbers 1–5) and add a star that is
|
|
695
|
+
* decoration only, so the word the screen reader hears is the scale's own.
|
|
696
|
+
*/
|
|
697
|
+
function ChoiceRadioGroup({ node, field, shared, translate, variant, }) {
|
|
698
|
+
const copy = useHostCopy();
|
|
699
|
+
const options = node.declaration.options ?? [];
|
|
700
|
+
// A held code no option matches is stated beside the group rather than as another radio: a
|
|
701
|
+
// radio for it would be a second selectable answer, while the record's value is not one.
|
|
702
|
+
const heldCode = unmatchedDeclaredCode(options, field.value);
|
|
703
|
+
const heldSentence = heldCode === undefined ? undefined : heldCodeSentence(field, translate, copy);
|
|
704
|
+
return (_jsxs("fieldset", { className: variant === 'rating' ? 'ax-radio-group ax-rating' : 'ax-radio-group',
|
|
705
|
+
// The group is the element a summary link lands on; `-1` makes it focusable by script
|
|
706
|
+
// without adding a tab stop of its own. The radios follow it as one tab stop.
|
|
707
|
+
id: shared.id, tabIndex: -1, "aria-invalid": shared['aria-invalid'], "aria-describedby": shared['aria-describedby'], "aria-busy": shared['aria-busy'], disabled: shared.disabled, onBlur: shared.onBlur, children: [_jsx("legend", { className: "ax-label", children: _jsx(ControlLabel, { node: node, field: field, copy: copy }) }), options.map((option, index) => {
|
|
708
|
+
const code = String(option.code);
|
|
709
|
+
const value = optionCodeValue(node.declaration, option.code);
|
|
710
|
+
return (_jsxs("label", { className: "ax-choice", children: [_jsx("input", { id: `${shared.id}-${String(index)}`, className: "ax-control--radio", type: "radio", name: shared.id, value: code, checked: sameWireValue(field.value, value), disabled: shared.disabled, "aria-required": shared['aria-required'], onChange: () => writeTo(field, value) }), _jsxs("span", { children: [variant === 'rating' ? (_jsxs("span", { className: "ax-rating-star", "aria-hidden": "true", children: ["\u2605", ' '] })) : null, translate(option.labelKey), option.deprecated === true ? copy.deprecatedSuffix() : null] })] }, code));
|
|
711
|
+
}), heldSentence === undefined ? null : (_jsx("p", { className: "ax-hint", "data-held-value": "", children: heldSentence })), heldCode === undefined && options.length === 0 ? (_jsx("p", { className: "ax-hint", role: "status", "data-empty-options": "", children: copy.emptyOptions() })) : null] }));
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Why one offered code may not be chosen, in the words the page renders, `ENGINE-7.1.7`.
|
|
715
|
+
*
|
|
716
|
+
* A declaration that states a reason key has its words translated; a host page that states none,
|
|
717
|
+
* and a code whose availability is still undecided, fall back to the host's own sentence, because
|
|
718
|
+
* the artifact never declared one and the catalogue must not invent a message key.
|
|
719
|
+
*/
|
|
720
|
+
function availabilityWords(answer, fieldLabel, optionLabel, translate, copy) {
|
|
721
|
+
if (answer.messageKey !== undefined) {
|
|
722
|
+
return { state: answer.state, reason: translate(answer.messageKey, answer.params) };
|
|
723
|
+
}
|
|
724
|
+
return {
|
|
725
|
+
state: answer.state,
|
|
726
|
+
reason: answer.state === 'unavailable'
|
|
727
|
+
? copy.unavailableOption(optionLabel)
|
|
728
|
+
: copy.undeterminedOption(fieldLabel, optionLabel, answer.cause ?? 'unknown'),
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* How a control's options reach a choice list, or `undefined` when it paints its own affordance.
|
|
733
|
+
*
|
|
734
|
+
* The list is used exactly when the record's own answer states that an offered code may not be
|
|
735
|
+
* chosen, and that answer already merges a declared condition with a host page's item, so one
|
|
736
|
+
* condition decides the caption's home and the widget alike. Two shapes are left alone: a
|
|
737
|
+
* `timeslot` page's value is a slot record the runtime states no code for, and a `rank` orders
|
|
738
|
+
* its options rather than selecting among them.
|
|
739
|
+
*/
|
|
740
|
+
function choiceListSource(node, field) {
|
|
741
|
+
const { declaration } = node;
|
|
742
|
+
if (field.unavailableOptions === undefined || field.unavailableOptions.length === 0)
|
|
743
|
+
return undefined;
|
|
744
|
+
if (node.control === 'rank' || node.control === 'timeslot')
|
|
745
|
+
return undefined;
|
|
746
|
+
if (declaration.options !== undefined)
|
|
747
|
+
return 'declared';
|
|
748
|
+
return declaration.optionSource?.kind === 'host' ? 'host' : undefined;
|
|
749
|
+
}
|
|
750
|
+
/** The declared domain as choice entries, each carrying the record's availability answer. */
|
|
751
|
+
function declaredChoiceEntries(node, field, translate, copy) {
|
|
752
|
+
const answers = field.unavailableOptions ?? [];
|
|
753
|
+
return (node.declaration.options ?? []).map((option) => {
|
|
754
|
+
const label = translate(option.labelKey);
|
|
755
|
+
const answer = answers.find((entry) => sameWireValue(entry.code, option.code));
|
|
756
|
+
return {
|
|
757
|
+
// The runtime owns the code-to-wire mapping: boolean and duration choices do not hold
|
|
758
|
+
// their source code as the record's value.
|
|
759
|
+
value: optionCodeValue(node.declaration, option.code),
|
|
760
|
+
key: `declared:${String(option.code)}`,
|
|
761
|
+
code: String(option.code),
|
|
762
|
+
label,
|
|
763
|
+
...(option.deprecated === true ? { deprecated: true } : {}),
|
|
764
|
+
...(answer === undefined
|
|
765
|
+
? {}
|
|
766
|
+
: availabilityWords(answer, node.label, label, translate, copy)),
|
|
767
|
+
};
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
/** A loaded page as choice entries, matched to the record's answer for the same code. */
|
|
771
|
+
function hostChoiceEntries(items, answers, fieldLabel, translate, copy) {
|
|
772
|
+
return items.map((item, index) => {
|
|
773
|
+
const label = item.labelKey === '' ? renderValueText(item.value, copy.display) : translate(item.labelKey);
|
|
774
|
+
const answer = answers.find((entry) => sameWireValue(entry.code, item.value));
|
|
775
|
+
return {
|
|
776
|
+
value: item.value,
|
|
777
|
+
key: `host:${String(index)}:${canonicalizeRestricted(item.value)}`,
|
|
778
|
+
code: String(item.value),
|
|
779
|
+
label,
|
|
780
|
+
...(item.deprecated === true ? { deprecated: true } : {}),
|
|
781
|
+
...(answer === undefined
|
|
782
|
+
? {}
|
|
783
|
+
: availabilityWords(answer, fieldLabel, label, translate, copy)),
|
|
784
|
+
};
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* A multiple-selection write with the held values no painted entry represents.
|
|
789
|
+
*
|
|
790
|
+
* A host page is a window onto its domain, not the whole record. Values outside that window were
|
|
791
|
+
* not toggled, so they keep their existing order ahead of the selected entries' painted order.
|
|
792
|
+
*/
|
|
793
|
+
function withUnpaintedHeldValues(heldValues, paintedValues, selectedValues) {
|
|
794
|
+
const painted = new Set(paintedValues.map((value) => canonicalizeRestricted(value)));
|
|
795
|
+
return [
|
|
796
|
+
...heldValues.filter((value) => !painted.has(canonicalizeRestricted(value))),
|
|
797
|
+
...selectedValues,
|
|
798
|
+
];
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* The keyboard hand-off a host choice list owes the affordance that replaces it.
|
|
802
|
+
*
|
|
803
|
+
* A page refresh can remove the entry the reader was on, and the availability branch can give way
|
|
804
|
+
* to the declared control. Both take the focused element out of the document, and a browser drops
|
|
805
|
+
* focus to `<body>` rather than to what replaced it. The list cannot make that hand-off itself,
|
|
806
|
+
* because the list is what is being removed, so the owner whose lifetime spans the remount
|
|
807
|
+
* remembers the request and answers it after the replacement has painted.
|
|
808
|
+
*
|
|
809
|
+
* The request is spent once it lands, which is the same contract `OptionsStatus` and the rank
|
|
810
|
+
* control keep. A request that stayed armed would reclaim focus from the page body on every later
|
|
811
|
+
* render of this owner, whether a host page settled or a sibling wrote, long after the reader had
|
|
812
|
+
* left of their own accord, and a move the reader did not cause is the one WCAG 3.2.1 forbids. It
|
|
813
|
+
* is not spent before it lands either: the replacement can be painted in a later commit than the
|
|
814
|
+
* one that dropped focus, so a render with nothing to hand on to leaves the request outstanding.
|
|
815
|
+
*/
|
|
816
|
+
function useChoiceListFocusHandoff(shared) {
|
|
817
|
+
const restoreFocus = useRef(false);
|
|
818
|
+
useEffect(() => {
|
|
819
|
+
if (!restoreFocus.current)
|
|
820
|
+
return;
|
|
821
|
+
const active = document.activeElement;
|
|
822
|
+
const control = document.getElementById(shared.id);
|
|
823
|
+
// Focus inside this binding's own affordance says the entry the reader was on is still
|
|
824
|
+
// painted, so the hand-off is not due and a later render must still be able to make it.
|
|
825
|
+
if (active !== null && control !== null && control.contains(active))
|
|
826
|
+
return;
|
|
827
|
+
if (active === null || active === document.body) {
|
|
828
|
+
control?.focus();
|
|
829
|
+
if (control !== null && document.activeElement === control)
|
|
830
|
+
restoreFocus.current = false;
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
// The reader is on an element the page can name, so they moved on: there is nothing to hand
|
|
834
|
+
// the keyboard on to and a later render must not take it back from them.
|
|
835
|
+
restoreFocus.current = false;
|
|
836
|
+
});
|
|
837
|
+
// `ChoiceList` keeps the hand-off in an effect's dependencies, so the callback is one identity
|
|
838
|
+
// for the owner's lifetime rather than a new one per render.
|
|
839
|
+
const onListFocus = useCallback(() => {
|
|
840
|
+
restoreFocus.current = true;
|
|
841
|
+
}, []);
|
|
842
|
+
return {
|
|
843
|
+
shared: {
|
|
844
|
+
...shared,
|
|
845
|
+
onBlur: () => {
|
|
846
|
+
// A named exit is the reader's own move: there is nothing to hand the keyboard on to.
|
|
847
|
+
restoreFocus.current = false;
|
|
848
|
+
shared.onBlur();
|
|
849
|
+
},
|
|
850
|
+
},
|
|
851
|
+
onListFocus,
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
/**
|
|
855
|
+
* A choice field whose options stay selectable when the record may not hold them.
|
|
856
|
+
*
|
|
857
|
+
* A native `<select>` and a native radio both express "unavailable" by removing the option from
|
|
858
|
+
* the tab order or refusing the click, which is exactly what a reader needs here: the reason is
|
|
859
|
+
* read at the option, and pressing an unavailable option still hands the write to the session so
|
|
860
|
+
* a host can stage the alternative it suggests. So every entry is a focusable `option` carrying
|
|
861
|
+
* `aria-selected` and, when the record refuses its code, `aria-disabled` and the sentence that
|
|
862
|
+
* says why. Focus moves by arrow, `Home` and `End`, and one entry is the list's tab stop; `Enter`
|
|
863
|
+
* and `Space` write, as a click does.
|
|
864
|
+
*
|
|
865
|
+
* The HTML `disabled` attribute is never set: an unavailable option is announced as such and
|
|
866
|
+
* stays pressable, and a field-level refusal is announced on the list while `writeTo` keeps the
|
|
867
|
+
* write from reaching the session.
|
|
868
|
+
*/
|
|
869
|
+
function ChoiceList({ node, field, shared, entries, multiple, allowClear, onFocus, }) {
|
|
870
|
+
const copy = useHostCopy();
|
|
871
|
+
const labelId = `${shared.id}-label`;
|
|
872
|
+
// The entry the list focuses when a reader tabs to it, and the one the arrows move from. It is
|
|
873
|
+
// the record's own choice until the reader moves it, so the list opens where the answer is.
|
|
874
|
+
const [movedTo, setMovedTo] = useState(undefined);
|
|
875
|
+
const list = useRef(null);
|
|
876
|
+
const focusWithin = useRef(false);
|
|
877
|
+
useEffect(() => {
|
|
878
|
+
return () => {
|
|
879
|
+
// A write can make the last refused code available and replace this list with the declared
|
|
880
|
+
// control. If the reader was inside the list, land on that replacement rather than <body>.
|
|
881
|
+
// An async host hands this to its owner, whose lifetime covers the page refresh too.
|
|
882
|
+
if (onFocus === undefined && focusWithin.current) {
|
|
883
|
+
document.getElementById(shared.id)?.focus();
|
|
884
|
+
}
|
|
885
|
+
};
|
|
886
|
+
}, [onFocus, shared.id]);
|
|
887
|
+
// A key no declared code can spell, because a code is any string: the no-choice entry is the
|
|
888
|
+
// one entry that belongs to no domain.
|
|
889
|
+
const options = allowClear && !multiple
|
|
890
|
+
? [{ value: null, key: 'no-choice', code: '', label: copy.selectPlaceholder() }, ...entries]
|
|
891
|
+
: entries;
|
|
892
|
+
// One comparison states both what the record holds and what a press toggles: a wire value is
|
|
893
|
+
// identified by its canonical text, which is how the checkbox group beside this list decides
|
|
894
|
+
// membership too. Two comparators would let the entry the reader sees selected and the entry a
|
|
895
|
+
// press removes disagree.
|
|
896
|
+
const heldValues = multiple && Array.isArray(field.value) ? field.value : [];
|
|
897
|
+
const held = new Set(heldValues.map((value) => canonicalizeRestricted(value)));
|
|
898
|
+
const isSelected = (entry) => {
|
|
899
|
+
if (multiple)
|
|
900
|
+
return held.has(canonicalizeRestricted(entry.value));
|
|
901
|
+
if (entry.value === null)
|
|
902
|
+
return field.value === undefined || field.value === null;
|
|
903
|
+
return field.value !== undefined && sameWireValue(field.value, entry.value);
|
|
904
|
+
};
|
|
905
|
+
const chosen = options.findIndex((entry) => isSelected(entry));
|
|
906
|
+
const active = options.length === 0 ? -1 : Math.min(movedTo ?? Math.max(chosen, 0), options.length - 1);
|
|
907
|
+
const focusAt = (index) => {
|
|
908
|
+
setMovedTo(index);
|
|
909
|
+
const painted = list.current?.querySelectorAll('[role="option"]');
|
|
910
|
+
painted?.[index]?.focus();
|
|
911
|
+
};
|
|
912
|
+
const activate = (entry) => {
|
|
913
|
+
if (!multiple) {
|
|
914
|
+
writeTo(field, entry.value);
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
// A multiple selection writes the entries in the order they are painted, never in the order
|
|
918
|
+
// the reader happened to press them, so two records holding one set hold one array. A host
|
|
919
|
+
// page is only a window onto its domain: values held outside that window are not entries the
|
|
920
|
+
// reader toggled and stay in the record in their existing order.
|
|
921
|
+
const next = new Set(held);
|
|
922
|
+
const token = canonicalizeRestricted(entry.value);
|
|
923
|
+
if (next.has(token))
|
|
924
|
+
next.delete(token);
|
|
925
|
+
else
|
|
926
|
+
next.add(token);
|
|
927
|
+
writeTo(field, withUnpaintedHeldValues(heldValues, options.map((option) => option.value), options
|
|
928
|
+
.filter((option) => next.has(canonicalizeRestricted(option.value)))
|
|
929
|
+
.map((option) => option.value)));
|
|
930
|
+
};
|
|
931
|
+
const onKeyDown = (event, index) => {
|
|
932
|
+
switch (event.key) {
|
|
933
|
+
case 'ArrowDown':
|
|
934
|
+
event.preventDefault();
|
|
935
|
+
focusAt(Math.min(index + 1, options.length - 1));
|
|
936
|
+
return;
|
|
937
|
+
case 'ArrowUp':
|
|
938
|
+
event.preventDefault();
|
|
939
|
+
focusAt(Math.max(index - 1, 0));
|
|
940
|
+
return;
|
|
941
|
+
case 'Home':
|
|
942
|
+
event.preventDefault();
|
|
943
|
+
focusAt(0);
|
|
944
|
+
return;
|
|
945
|
+
case 'End':
|
|
946
|
+
event.preventDefault();
|
|
947
|
+
focusAt(options.length - 1);
|
|
948
|
+
return;
|
|
949
|
+
// Enter and Space are the two ways a keyboard presses a button, and an option is one: the
|
|
950
|
+
// write is the same one a pointer makes, so the session decides what happens to it.
|
|
951
|
+
case 'Enter':
|
|
952
|
+
case ' ': {
|
|
953
|
+
event.preventDefault();
|
|
954
|
+
const entry = options[index];
|
|
955
|
+
if (entry !== undefined)
|
|
956
|
+
activate(entry);
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
default:
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
};
|
|
963
|
+
return (_jsxs("fieldset", { className: "ax-choice-list", ...(shared.disabled ? { 'aria-disabled': true } : {}), onFocus: () => {
|
|
964
|
+
focusWithin.current = true;
|
|
965
|
+
onFocus?.();
|
|
966
|
+
}, onBlur: (event) => {
|
|
967
|
+
if (event.currentTarget.contains(event.relatedTarget))
|
|
968
|
+
return;
|
|
969
|
+
focusWithin.current = false;
|
|
970
|
+
shared.onBlur();
|
|
971
|
+
}, children: [_jsx("legend", { className: "ax-label", id: labelId, children: _jsx(ControlLabel, { node: node, field: field, copy: copy }) }), _jsx("ul", { ref: list,
|
|
972
|
+
// The list is the control a summary link and this binding's `id` name, and it is
|
|
973
|
+
// focusable by script so a link lands on it without adding a tab stop of its own.
|
|
974
|
+
id: shared.id, tabIndex: -1, role: "listbox", "aria-labelledby": labelId, "aria-describedby": shared['aria-describedby'], "aria-invalid": shared['aria-invalid'], "aria-required": shared['aria-required'], "aria-busy": shared['aria-busy'], ...(multiple ? { 'aria-multiselectable': true } : {}), children: options.map((entry, index) => {
|
|
975
|
+
const entryId = `${shared.id}-option-${String(index)}`;
|
|
976
|
+
const reasonId = `${entryId}-reason`;
|
|
977
|
+
return (_jsxs("li", { id: entryId, className: "ax-option", role: "option", "aria-labelledby": entry.reason === undefined ? `${entryId}-label` : `${entryId}-label ${reasonId}`, "aria-selected": isSelected(entry), ...(entry.state === 'unavailable' ? { 'aria-disabled': 'true' } : {}), ...(entry.state === undefined ? {} : { 'data-option-state': entry.state }), "data-option-code": entry.code, tabIndex: index === active ? 0 : -1, onClick: () => activate(entry), onKeyDown: (event) => onKeyDown(event, index), children: [_jsxs("span", { className: "ax-option-label", id: `${entryId}-label`, children: [entry.label, entry.deprecated === true ? copy.deprecatedSuffix() : null] }), ' ', entry.reason === undefined ? null : (_jsx("span", { className: "ax-option-reason", id: reasonId, "data-option-reason": "", children: entry.reason }))] }, entry.key));
|
|
978
|
+
}) })] }));
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
* A choice whose declared affordance is a filtering combobox.
|
|
982
|
+
*
|
|
983
|
+
* The input keeps the typed query, the listbox filters on the visible labels, and only an
|
|
984
|
+
* explicit choice (a click, or Enter on the active option) writes a value. Arrow keys move the
|
|
985
|
+
* active option through `aria-activedescendant`, so focus never leaves the input and a keyboard
|
|
986
|
+
* user hears each candidate as they pass it. Typing nothing must not select anything: the
|
|
987
|
+
* placeholder problem a `<select>` has cannot arise here. Prefix matches lead substring matches
|
|
988
|
+
* so typing the start of a label finds it first. The hint below the input states the contract in
|
|
989
|
+
* one sentence, because filtering plus Enter-to-commit is not what a native select does.
|
|
990
|
+
*/
|
|
991
|
+
function ComboboxEntry({ node, field, shared, translate, }) {
|
|
992
|
+
const copy = useHostCopy();
|
|
993
|
+
const options = node.declaration.options ?? [];
|
|
994
|
+
const listId = `${shared.id}-listbox`;
|
|
995
|
+
const hintId = `${shared.id}-combobox-hint`;
|
|
996
|
+
const [query, setQuery] = useState(undefined);
|
|
997
|
+
const [open, setOpen] = useState(false);
|
|
998
|
+
const [activeIndex, setActiveIndex] = useState(0);
|
|
999
|
+
const selected = options.find((option) => sameWireValue(optionCodeValue(node.declaration, option.code), field.value));
|
|
1000
|
+
// A held code no option matches leaves the entry empty: the typed text is a query, never the
|
|
1001
|
+
// record, so showing the stale label would turn it into a filter that hides the domain. The
|
|
1002
|
+
// sentence below the input says what the record holds and why it is not offered.
|
|
1003
|
+
const heldCode = unmatchedDeclaredCode(options, field.value);
|
|
1004
|
+
const heldSentence = heldCode === undefined ? undefined : heldCodeSentence(field, translate, copy);
|
|
1005
|
+
const text = query ?? (selected === undefined ? '' : translate(selected.labelKey));
|
|
1006
|
+
// The selected label is not a query: opening the list offers the whole domain, and only what
|
|
1007
|
+
// the user types narrows it. Otherwise focusing a selected combobox would hide every other
|
|
1008
|
+
// option and a reader could not change their answer.
|
|
1009
|
+
const needle = query === undefined ? '' : query.trim().toLowerCase();
|
|
1010
|
+
const lowered = (option) => translate(option.labelKey).toLowerCase();
|
|
1011
|
+
const matching = needle === '' ? options : options.filter((option) => lowered(option).includes(needle));
|
|
1012
|
+
// Prefix matches lead substring matches, keeping declaration order inside each run: typing the
|
|
1013
|
+
// start of a label must not bury it under a longer label that merely contains those letters.
|
|
1014
|
+
const filtered = needle === ''
|
|
1015
|
+
? matching
|
|
1016
|
+
: [
|
|
1017
|
+
...matching.filter((option) => lowered(option).startsWith(needle)),
|
|
1018
|
+
...matching.filter((option) => !lowered(option).startsWith(needle)),
|
|
1019
|
+
];
|
|
1020
|
+
const active = filtered[Math.min(activeIndex, filtered.length - 1)];
|
|
1021
|
+
// A list with no candidate is not a listbox: the control stays closed rather than announcing
|
|
1022
|
+
// an empty popup, and `aria-activedescendant` never names an option that is not painted.
|
|
1023
|
+
const showList = open && filtered.length > 0;
|
|
1024
|
+
const comboboxDescribed = [shared['aria-describedby'], hintId]
|
|
1025
|
+
.filter((entry) => entry !== undefined && entry !== '')
|
|
1026
|
+
.join(' ');
|
|
1027
|
+
const choose = (option) => {
|
|
1028
|
+
writeTo(field, optionCodeValue(node.declaration, option.code));
|
|
1029
|
+
setQuery(undefined);
|
|
1030
|
+
setOpen(false);
|
|
1031
|
+
setActiveIndex(0);
|
|
1032
|
+
};
|
|
1033
|
+
return (_jsxs(_Fragment, { children: [_jsx("input", { ...shared, "aria-describedby": comboboxDescribed === '' ? undefined : comboboxDescribed, type: "text", role: "combobox", autoComplete: node.hints.web.autoComplete, "aria-autocomplete": "list", "aria-expanded": showList, ...(showList
|
|
1034
|
+
? {
|
|
1035
|
+
'aria-controls': listId,
|
|
1036
|
+
...(active === undefined
|
|
1037
|
+
? {}
|
|
1038
|
+
: { 'aria-activedescendant': `${shared.id}-option-${String(activeIndex)}` }),
|
|
1039
|
+
}
|
|
1040
|
+
: {}), value: text, onFocus: () => setOpen(true), onChange: (event) => {
|
|
1041
|
+
const next = event.target.value;
|
|
1042
|
+
setQuery(next);
|
|
1043
|
+
setOpen(true);
|
|
1044
|
+
setActiveIndex(0);
|
|
1045
|
+
// Emptying the entry is a clear, exactly as it is in a text field; a prefix still
|
|
1046
|
+
// being typed writes nothing until it names an option.
|
|
1047
|
+
if (next === '')
|
|
1048
|
+
writeTo(field, null);
|
|
1049
|
+
}, onKeyDown: (event) => {
|
|
1050
|
+
if (event.key === 'ArrowDown') {
|
|
1051
|
+
event.preventDefault();
|
|
1052
|
+
if (!showList) {
|
|
1053
|
+
setOpen(true);
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
setActiveIndex((index) => Math.min(index + 1, filtered.length - 1));
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
if (event.key === 'ArrowUp') {
|
|
1060
|
+
event.preventDefault();
|
|
1061
|
+
setActiveIndex((index) => Math.max(index - 1, 0));
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
if (event.key === 'Home') {
|
|
1065
|
+
event.preventDefault();
|
|
1066
|
+
if (!showList)
|
|
1067
|
+
setOpen(true);
|
|
1068
|
+
setActiveIndex(0);
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
if (event.key === 'End') {
|
|
1072
|
+
event.preventDefault();
|
|
1073
|
+
if (!showList)
|
|
1074
|
+
setOpen(true);
|
|
1075
|
+
setActiveIndex(Math.max(filtered.length - 1, 0));
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
if (event.key === 'Enter' && showList && active !== undefined) {
|
|
1079
|
+
event.preventDefault();
|
|
1080
|
+
choose(active);
|
|
1081
|
+
return;
|
|
1082
|
+
}
|
|
1083
|
+
if (event.key === 'Escape') {
|
|
1084
|
+
setOpen(false);
|
|
1085
|
+
setQuery(undefined);
|
|
1086
|
+
}
|
|
1087
|
+
}, onBlur: () => {
|
|
1088
|
+
setOpen(false);
|
|
1089
|
+
setQuery(undefined);
|
|
1090
|
+
shared.onBlur();
|
|
1091
|
+
} }), _jsx("p", { className: "ax-hint", id: hintId, children: copy.comboboxHint() }), heldSentence === undefined ? null : (_jsx("p", { className: "ax-hint", "data-held-value": "", children: heldSentence })), filtered.length === 0 ? (_jsx("p", { className: "ax-hint", role: "status", "data-empty-options": "", children: copy.emptyOptions() })) : null, showList ? (_jsx("ul", { className: "ax-rank-list", id: listId, role: "listbox", "aria-label": node.label, children: filtered.map((option, index) => (_jsxs("li", { id: `${shared.id}-option-${String(index)}`, role: "option", "aria-selected": sameWireValue(optionCodeValue(node.declaration, option.code), field.value), ...(index === activeIndex ? { 'data-active': '' } : {}),
|
|
1092
|
+
// Selecting on mousedown keeps focus in the input: the blur that a click would
|
|
1093
|
+
// cause is what would otherwise close the list before the choice lands.
|
|
1094
|
+
onMouseDown: (event) => {
|
|
1095
|
+
event.preventDefault();
|
|
1096
|
+
choose(option);
|
|
1097
|
+
}, children: [translate(option.labelKey), option.deprecated === true ? copy.deprecatedSuffix() : null] }, String(option.code)))) })) : null] }));
|
|
1098
|
+
}
|
|
1099
|
+
/** The object URL a media reference can be previewed from, when the host answers one. */
|
|
1100
|
+
function mediaSrcOf(mediaSrc, value) {
|
|
1101
|
+
if (mediaSrc === undefined || !isRecord(value))
|
|
1102
|
+
return undefined;
|
|
1103
|
+
return mediaSrc(value);
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* A media reference's thumbnail: the host's object URL for an image, or a neutral glyph and the
|
|
1107
|
+
* media type. A missing preview is a capability answer, never a broken image frame.
|
|
1108
|
+
*/
|
|
1109
|
+
function MediaPreview({ value, src, }) {
|
|
1110
|
+
const mime = mediaMime(value);
|
|
1111
|
+
if (src !== undefined && isImageMedia(value)) {
|
|
1112
|
+
return _jsx("img", { className: "ax-media-thumb", src: src, alt: "", loading: "lazy" });
|
|
1113
|
+
}
|
|
1114
|
+
return (_jsxs("span", { className: "ax-media-thumb ax-media-thumb--glyph", "data-media-glyph": "", children: [_jsx("span", { className: "ax-media-glyph", "aria-hidden": "true", children: "\u25A7" }), mime === '' ? null : _jsx("span", { className: "ax-media-type", children: friendlyMediaType(mime) })] }));
|
|
1115
|
+
}
|
|
1116
|
+
/** The media blockers a summary is painting for one file of one field. */
|
|
1117
|
+
function mediaBlockersOf(blockers, fieldId, index) {
|
|
1118
|
+
return blockers.flatMap((blocker) => blocker.kind === 'media' && blocker.fieldId === fieldId && blocker.index === index
|
|
1119
|
+
? [blocker]
|
|
1120
|
+
: []);
|
|
1121
|
+
}
|
|
1122
|
+
/** The host's sentence for a media constraint, under the file it is about. */
|
|
1123
|
+
function MediaConstraintErrors({ blockers, fieldId, label, }) {
|
|
1124
|
+
const copy = useHostCopy();
|
|
1125
|
+
if (blockers.length === 0)
|
|
1126
|
+
return null;
|
|
1127
|
+
return (_jsx(_Fragment, { children: blockers.map((blocker) => (_jsx("p", { className: "ax-error", role: "alert", children: copy.mediaConstraint(label, blocker.member, blocker.limit, blocker.value) }, `${fieldId}:${blocker.member}:${String(blocker.value)}`))) }));
|
|
1128
|
+
}
|
|
1129
|
+
/** The host sentence one capture failure states, with both sizes in the host's own units. */
|
|
1130
|
+
function mediaFailureText(failure, label, copy, locale) {
|
|
1131
|
+
switch (failure.kind) {
|
|
1132
|
+
case 'too-large':
|
|
1133
|
+
return {
|
|
1134
|
+
sentence: copy.mediaTooLarge(humanSize(failure.bytes, locale), humanSize(failure.limit, locale)),
|
|
1135
|
+
};
|
|
1136
|
+
case 'server':
|
|
1137
|
+
return { sentence: copy.mediaServerRefused() };
|
|
1138
|
+
case 'constraint':
|
|
1139
|
+
return {
|
|
1140
|
+
sentence: copy.mediaConstraint(label, failure.member, failure.limit, failure.value),
|
|
1141
|
+
};
|
|
1142
|
+
case 'detail':
|
|
1143
|
+
return { sentence: copy.mediaFailed(), detail: failure.detail };
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
/**
|
|
1147
|
+
* Why a captured reference is outside its declaration, in the declaration's own terms.
|
|
1148
|
+
*
|
|
1149
|
+
* The adapter never uploads, but a host can return a reference it already stored. `maxBytes`
|
|
1150
|
+
* has its own catalogue sentence; a `minBytes` or `accept` violation is stated as the detail the
|
|
1151
|
+
* host sentence wraps, because those two need the file's own type and size to be actionable.
|
|
1152
|
+
*/
|
|
1153
|
+
function mediaOutsideDeclared(reference, bounds) {
|
|
1154
|
+
const bytes = mediaBytes(reference);
|
|
1155
|
+
if (bytes !== undefined && bounds.maxBytes !== undefined && bytes > bounds.maxBytes) {
|
|
1156
|
+
return { kind: 'too-large', bytes, limit: bounds.maxBytes };
|
|
1157
|
+
}
|
|
1158
|
+
if (bytes !== undefined && bounds.minBytes !== undefined && bytes < bounds.minBytes) {
|
|
1159
|
+
return { kind: 'constraint', member: 'minBytes', limit: bounds.minBytes, value: bytes };
|
|
1160
|
+
}
|
|
1161
|
+
const mime = mediaMime(reference);
|
|
1162
|
+
if (mime !== '' &&
|
|
1163
|
+
bounds.accept !== undefined &&
|
|
1164
|
+
bounds.accept.length > 0 &&
|
|
1165
|
+
!mediaAcceptMatches(bounds.accept, mime)) {
|
|
1166
|
+
return { kind: 'constraint', member: 'accept', limit: bounds.accept, value: mime };
|
|
1167
|
+
}
|
|
1168
|
+
return undefined;
|
|
1169
|
+
}
|
|
1170
|
+
/**
|
|
1171
|
+
* The failure a rejected capture action states.
|
|
1172
|
+
*
|
|
1173
|
+
* A host that already knows why (the file's type or size) throws `CaptureRefusedError`, whose
|
|
1174
|
+
* message is a clause the catalogue sentence wraps; any other rejection is the store's, and its
|
|
1175
|
+
* status code is never repeated. A synchronous throw and a rejected promise are the same case.
|
|
1176
|
+
*/
|
|
1177
|
+
function mediaCaptureFailure(error, bounds) {
|
|
1178
|
+
if (error instanceof Error && error.name === 'AxiomCaptureRefused' && error.message !== '') {
|
|
1179
|
+
return { kind: 'detail', detail: error.message };
|
|
1180
|
+
}
|
|
1181
|
+
return { kind: 'server', limit: bounds.maxBytes };
|
|
1182
|
+
}
|
|
1183
|
+
/**
|
|
1184
|
+
* One `media_ref`: the reference the host already resolved, and the host's capture action.
|
|
1185
|
+
*
|
|
1186
|
+
* The adapter never uploads. `captureMedia` is host code that opens a picker and returns the
|
|
1187
|
+
* closed `media_ref` it chose (id, digest, media type, size); this control writes that answer.
|
|
1188
|
+
* A media blocker carries no artifact message, so its host sentence is painted here, under the
|
|
1189
|
+
* file it names, when the blocker has no array index.
|
|
1190
|
+
*/
|
|
1191
|
+
function MediaEntry({ field, id, translate, accept, minBytes, maxBytes, }) {
|
|
1192
|
+
const { captureMedia, mediaSrc } = useHostActions();
|
|
1193
|
+
const copy = useHostCopy();
|
|
1194
|
+
const locale = useLocale();
|
|
1195
|
+
const summary = useFormState();
|
|
1196
|
+
const [failure, setFailure] = useState(undefined);
|
|
1197
|
+
const value = field.value;
|
|
1198
|
+
const label = translate(field.descriptor.labelKey ?? field.fieldId);
|
|
1199
|
+
const hasValue = value !== undefined && value !== null;
|
|
1200
|
+
const canCapture = captureMedia !== undefined && field.enabled && field.access === 'read_write';
|
|
1201
|
+
const src = value === undefined || value === null ? undefined : mediaSrcOf(mediaSrc, value);
|
|
1202
|
+
const blockers = mediaBlockersOf(summary.visibleBlockers, field.fieldId, undefined);
|
|
1203
|
+
const bounds = { accept, minBytes, maxBytes };
|
|
1204
|
+
const capture = () => {
|
|
1205
|
+
if (captureMedia === undefined)
|
|
1206
|
+
return;
|
|
1207
|
+
setFailure(undefined);
|
|
1208
|
+
try {
|
|
1209
|
+
void captureMedia(field.fieldId, {
|
|
1210
|
+
label,
|
|
1211
|
+
...(accept === undefined ? {} : { accept }),
|
|
1212
|
+
...(minBytes === undefined ? {} : { minBytes }),
|
|
1213
|
+
...(maxBytes === undefined ? {} : { maxBytes }),
|
|
1214
|
+
}).then((reference) => {
|
|
1215
|
+
if (reference === undefined)
|
|
1216
|
+
return;
|
|
1217
|
+
const refused = mediaOutsideDeclared(reference, bounds);
|
|
1218
|
+
if (refused !== undefined) {
|
|
1219
|
+
setFailure(refused);
|
|
1220
|
+
return;
|
|
1221
|
+
}
|
|
1222
|
+
writeTo(field, reference);
|
|
1223
|
+
}, (error) => setFailure(mediaCaptureFailure(error, bounds)));
|
|
1224
|
+
}
|
|
1225
|
+
catch (error) {
|
|
1226
|
+
setFailure(mediaCaptureFailure(error, bounds));
|
|
1227
|
+
}
|
|
1228
|
+
};
|
|
1229
|
+
const refusalId = `${id}-media-refusal`;
|
|
1230
|
+
// An id may be stated only when its element is rendered: the field error node appears only when
|
|
1231
|
+
// failures are visible, the description only when declared, the deferral note only when
|
|
1232
|
+
// withheld, and the capture refusal only after a rejected capture.
|
|
1233
|
+
const describedBy = [
|
|
1234
|
+
visibleErrorsOf(field).length > 0 ? field.a11y.errorId : undefined,
|
|
1235
|
+
field.a11y.descriptionId,
|
|
1236
|
+
field.withheld === true ? `${id}-deferred` : undefined,
|
|
1237
|
+
failure === undefined ? undefined : refusalId,
|
|
1238
|
+
]
|
|
1239
|
+
.filter((entry) => entry !== undefined && entry !== '')
|
|
1240
|
+
.join(' ');
|
|
1241
|
+
const describedAttr = describedBy === '' ? undefined : describedBy;
|
|
1242
|
+
const invalid = field.a11y.invalid || failure !== undefined;
|
|
1243
|
+
const failureText = failure === undefined ? undefined : mediaFailureText(failure, label, copy, locale);
|
|
1244
|
+
return (_jsxs(_Fragment, { children: [hasValue ? (_jsxs("output", { id: id, className: `${controlClassName('media_capture')} ax-media`, "data-readonly": field.fieldId, "aria-invalid": invalid, "aria-describedby": describedAttr, children: [_jsx(MediaPreview, { value: value, src: src }), _jsxs("span", { className: "ax-media-body", children: [_jsx("span", { className: "ax-media-line ax-row-key", title: mediaLine(value, locale), children: mediaDisplayLine(value, 28, locale) }), canCapture ? (_jsxs("span", { className: "ax-actions", "aria-invalid": invalid ? true : undefined, "aria-describedby": describedAttr, children: [_jsx("button", { type: "button", className: "ax-button ax-button--secondary", onClick: capture, children: copy.attachMedia() }), _jsx("button", { type: "button", className: "ax-button ax-button--danger", onClick: () => writeTo(field, null), children: copy.removeMedia(label) })] })) : null] })] })) : canCapture ? null : (_jsx("output", { id: id, className: controlClassName('media_capture'), "data-readonly": field.fieldId, "aria-invalid": invalid, "aria-describedby": describedAttr, children: "\u2014" })), !hasValue && canCapture ? (_jsx("div", { className: "ax-actions", "aria-invalid": invalid ? true : undefined, "aria-describedby": describedAttr, children: _jsx("button", { type: "button", className: "ax-button ax-button--secondary", "aria-describedby": describedAttr, onClick: capture, children: copy.attachMedia() }) })) : null, _jsx(MediaConstraintErrors, { blockers: blockers, fieldId: field.fieldId, label: label }), failureText === undefined ? null : (_jsxs(_Fragment, { children: [_jsx("p", { className: "ax-error", role: "alert", id: refusalId, "data-media-failure": failure?.kind, children: failureText.sentence }), failureText.detail === undefined ? null : (_jsx("p", { className: "ax-hint", children: failureText.detail }))] }))] }));
|
|
1245
|
+
}
|
|
1246
|
+
/**
|
|
1247
|
+
* A `media_ref[]` value: the captured references, and the host's capture action per file.
|
|
1248
|
+
*
|
|
1249
|
+
* `minItems` is the count contract, so adding the first reference is what clears a `belowMin`
|
|
1250
|
+
* blocker and removing the last restores it. Nothing is uploaded; the host owns storage. A media
|
|
1251
|
+
* blocker with an `index` is painted under that entry, where the file it names is visible.
|
|
1252
|
+
*/
|
|
1253
|
+
function MediaListEntry({ field, id, translate, accept, minBytes, maxBytes, }) {
|
|
1254
|
+
const { captureMedia, mediaSrc } = useHostActions();
|
|
1255
|
+
const copy = useHostCopy();
|
|
1256
|
+
const locale = useLocale();
|
|
1257
|
+
const summary = useFormState();
|
|
1258
|
+
const [failure, setFailure] = useState(undefined);
|
|
1259
|
+
const values = Array.isArray(field.value) ? field.value : [];
|
|
1260
|
+
const label = translate(field.descriptor.labelKey ?? field.fieldId);
|
|
1261
|
+
const canCapture = captureMedia !== undefined && field.enabled && field.access === 'read_write';
|
|
1262
|
+
const bounds = { accept, minBytes, maxBytes };
|
|
1263
|
+
const capture = () => {
|
|
1264
|
+
if (captureMedia === undefined)
|
|
1265
|
+
return;
|
|
1266
|
+
setFailure(undefined);
|
|
1267
|
+
try {
|
|
1268
|
+
void captureMedia(field.fieldId, {
|
|
1269
|
+
label,
|
|
1270
|
+
...(accept === undefined ? {} : { accept }),
|
|
1271
|
+
...(minBytes === undefined ? {} : { minBytes }),
|
|
1272
|
+
...(maxBytes === undefined ? {} : { maxBytes }),
|
|
1273
|
+
}).then((reference) => {
|
|
1274
|
+
if (reference === undefined)
|
|
1275
|
+
return;
|
|
1276
|
+
const refused = mediaOutsideDeclared(reference, bounds);
|
|
1277
|
+
if (refused !== undefined) {
|
|
1278
|
+
setFailure(refused);
|
|
1279
|
+
return;
|
|
1280
|
+
}
|
|
1281
|
+
writeTo(field, [...values, reference]);
|
|
1282
|
+
}, (error) => setFailure(mediaCaptureFailure(error, bounds)));
|
|
1283
|
+
}
|
|
1284
|
+
catch (error) {
|
|
1285
|
+
setFailure(mediaCaptureFailure(error, bounds));
|
|
1286
|
+
}
|
|
1287
|
+
};
|
|
1288
|
+
// An id may be stated only when its element is rendered: the error node appears when the
|
|
1289
|
+
// failure is painted, so an unconditional `errorId` is a promise to nothing — axe reports it as
|
|
1290
|
+
// incomplete and a screen reader reads no description, or whatever later takes the id.
|
|
1291
|
+
const describedBy = [field.showError ? field.a11y.errorId : undefined, field.a11y.descriptionId]
|
|
1292
|
+
.filter((entry) => entry !== undefined && entry !== '')
|
|
1293
|
+
.join(' ');
|
|
1294
|
+
const failureText = failure === undefined ? undefined : mediaFailureText(failure, label, copy, locale);
|
|
1295
|
+
return (_jsxs("fieldset", { className: "ax-checklist", id: id, tabIndex: -1, "aria-invalid": field.a11y.invalid || failure !== undefined, ...(describedBy === '' ? {} : { 'aria-describedby': describedBy }), disabled: field.disabled, onBlur: field.onBlur, children: [_jsx("legend", { className: "ax-label", children: label }), values.length === 0 ? _jsx("p", { className: "ax-hint", children: copy.rowsEmpty() }) : null, values.length === 0 ? null : (_jsx("ul", { id: `${id}-list`, className: "ax-rows ax-media-list", children: values.map((value, index) => {
|
|
1296
|
+
const name = `${label} ${String(index + 1)}`;
|
|
1297
|
+
const blockers = mediaBlockersOf(summary.visibleBlockers, field.fieldId, index);
|
|
1298
|
+
return (_jsxs("li", { className: "ax-row ax-media-item", children: [_jsx(MediaPreview, { value: value, src: mediaSrcOf(mediaSrc, value) }), _jsxs("span", { className: "ax-media-body", children: [_jsx("span", { className: "ax-media-line ax-row-key", title: mediaLine(value, locale), children: mediaDisplayLine(value, 28, locale) }), canCapture ? (_jsx("span", { className: "ax-actions", children: _jsx("button", { type: "button", className: "ax-button ax-button--danger", onClick: () => writeTo(field, values.filter((_, at) => at !== index)), children: copy.removeMedia(name) }) })) : null] }), _jsx(MediaConstraintErrors, { blockers: blockers, fieldId: field.fieldId, label: label })] }, `${String(index)}:${canonicalizeRestricted(value)}`));
|
|
1299
|
+
}) })), canCapture ? (_jsx("div", { className: "ax-actions", children: _jsx("button", { type: "button", className: "ax-button ax-button--secondary", onClick: capture, children: copy.attachMedia() }) })) : null, failureText === undefined ? null : (_jsxs(_Fragment, { children: [_jsx("p", { className: "ax-error", role: "alert", "data-media-failure": failure?.kind, children: failureText.sentence }), failureText.detail === undefined ? null : (_jsx("p", { className: "ax-hint", children: failureText.detail }))] }))] }));
|
|
1300
|
+
}
|
|
1301
|
+
/**
|
|
1302
|
+
* A choice whose domain the host loads on demand, `[MODEL-3.1.8]`.
|
|
1303
|
+
*
|
|
1304
|
+
* The items come from `useAsyncOptions`, keyed by the declared dependencies, so a source offers
|
|
1305
|
+
* the values the window the form holds asks for and one change reloads one source. The selected
|
|
1306
|
+
* item is matched by the wire contract's own equality, because a timeslot is an object rather
|
|
1307
|
+
* than a code. A `combobox` over the same domain is `SearchableChoice`: the one declaration that
|
|
1308
|
+
* states a filtering affordance gets a search.
|
|
1309
|
+
*/
|
|
1310
|
+
function AsyncChoice({ node, field, shared, rowKey, translate, }) {
|
|
1311
|
+
const pages = useAsyncOptions(node.fieldId, rowKey);
|
|
1312
|
+
const copy = useHostCopy();
|
|
1313
|
+
// The query belongs to the host-choice composite, not to one affordance it can temporarily
|
|
1314
|
+
// replace: an unreadable basis or an availability list must not revive an invisible filter.
|
|
1315
|
+
const [query, setQuery] = useState(undefined);
|
|
1316
|
+
// This owner outlives the availability branch it can replace, so it is the one that can hand
|
|
1317
|
+
// the keyboard on to the control that takes the branch's place.
|
|
1318
|
+
const handoff = useChoiceListFocusHandoff(shared);
|
|
1319
|
+
const selected = pages.items.findIndex((item) => sameWireValue(item.value, field.value));
|
|
1320
|
+
// A picker whose domain has not arrived offers nothing yet: its first page is still loading,
|
|
1321
|
+
// so the select is not usable and states that it is busy while the status line announces it.
|
|
1322
|
+
const busy = pages.readable && pages.loading;
|
|
1323
|
+
// The loaded page is the domain a reader is choosing from, so a code the record refuses is
|
|
1324
|
+
// painted in place: the answers are the record's own, and the labels are the page's. The list
|
|
1325
|
+
// replaces the search too, because a page that states availability cannot be painted by a
|
|
1326
|
+
// native affordance and the declared form of the control is already that list.
|
|
1327
|
+
if (choiceListSource(node, field) === 'host') {
|
|
1328
|
+
return (_jsxs(_Fragment, { children: [_jsx(ChoiceList, { node: node, field: field, shared: handoff.shared, entries: hostChoiceEntries(pages.items, field.unavailableOptions ?? [], node.label, translate, copy), multiple: false, allowClear: node.control !== 'radio', onFocus: handoff.onListFocus }), _jsx(OptionsStatus, { pages: pages, copy: copy, controlId: shared.id })] }));
|
|
1329
|
+
}
|
|
1330
|
+
if (node.control === 'combobox') {
|
|
1331
|
+
// A domain keyed by a value this device may not read is not a search the reader can use: no
|
|
1332
|
+
// page is requested, every candidate would be missing, and an empty entry that accepts text
|
|
1333
|
+
// would say nothing about why. The value the record holds is painted instead.
|
|
1334
|
+
if (!pages.readable) {
|
|
1335
|
+
return (_jsxs(_Fragment, { children: [_jsx(ReadOnlyValue, { field: field, id: shared.id, control: node.control }), _jsx(OptionsStatus, { pages: pages, copy: copy, controlId: shared.id })] }));
|
|
1336
|
+
}
|
|
1337
|
+
return (_jsx(SearchableChoice, { node: node, field: field, shared: shared, ...(rowKey === undefined ? {} : { rowKey }), translate: translate, query: query, setQuery: setQuery }));
|
|
1338
|
+
}
|
|
1339
|
+
if (node.control === 'radio') {
|
|
1340
|
+
// A radio group paints a static value until its host domain answers availability; keep that
|
|
1341
|
+
// fallback rather than silently replacing the affordance with a select.
|
|
1342
|
+
return (_jsxs(_Fragment, { children: [_jsx(ReadOnlyValue, { field: field, id: shared.id, control: node.control }), _jsx(OptionsStatus, { pages: pages, copy: copy, controlId: shared.id })] }));
|
|
1343
|
+
}
|
|
1344
|
+
return (_jsxs(_Fragment, { children: [_jsxs("select", { ...shared, "aria-busy": busy ? true : shared['aria-busy'], disabled: shared.disabled || busy, value: selected === -1 ? '' : String(selected), onChange: (event) => {
|
|
1345
|
+
const value = event.target.value;
|
|
1346
|
+
// `Number('')` is `0`, so the placeholder would otherwise read as "choose the first
|
|
1347
|
+
// slot". The placeholder is the no-choice entry and writes `null`.
|
|
1348
|
+
if (value === '') {
|
|
1349
|
+
writeTo(field, null);
|
|
1350
|
+
return;
|
|
1351
|
+
}
|
|
1352
|
+
const item = pages.items[Number(value)];
|
|
1353
|
+
if (item !== undefined)
|
|
1354
|
+
writeTo(field, item.value);
|
|
1355
|
+
}, children: [_jsx("option", { value: "", children: copy.selectPlaceholder() }), pages.items.map((item, index) => (_jsxs("option", { value: String(index), children: [item.labelKey === ''
|
|
1356
|
+
? renderValueText(item.value, copy.display)
|
|
1357
|
+
: translate(item.labelKey), item.deprecated === true ? copy.deprecatedSuffix() : null] }, canonicalizeRestricted(item.value))))] }), _jsx(OptionsStatus, { pages: pages, copy: copy, controlId: shared.id })] }));
|
|
1358
|
+
}
|
|
1359
|
+
/** A union arm the compiler proves unreachable. */
|
|
1360
|
+
function assertNeverPlan(value) {
|
|
1361
|
+
throw new Error(`unreachable control plan: ${JSON.stringify(value)}`);
|
|
1362
|
+
}
|
|
1363
|
+
/**
|
|
1364
|
+
* A host-loaded domain the reader can search, `[MODEL-3.1.8]`.
|
|
1365
|
+
*
|
|
1366
|
+
* The input is the query and nothing else: what the reader types narrows the host's pages, and
|
|
1367
|
+
* the record changes only when an option is chosen. Enter on text that names nothing writes
|
|
1368
|
+
* nothing, Escape restores the committed value, and emptying the entry restores the first page.
|
|
1369
|
+
* The committed label is read from the domain the pages have offered rather than from the last
|
|
1370
|
+
* page, so a value chosen out of a search keeps its words when the search moves on; it is not
|
|
1371
|
+
* painted as one of the current matches, because the page the reader is looking at may not hold
|
|
1372
|
+
* it.
|
|
1373
|
+
*
|
|
1374
|
+
* A page for the current text has not arrived yet while the pages are loading, and the empty
|
|
1375
|
+
* result states its own sentence: a search that matches nothing is a different answer from a
|
|
1376
|
+
* source that offers nothing.
|
|
1377
|
+
*/
|
|
1378
|
+
function SearchableChoice({ node, field, shared, rowKey, translate, query, setQuery, }) {
|
|
1379
|
+
const pages = useAsyncOptions(node.fieldId, rowKey);
|
|
1380
|
+
const copy = useHostCopy();
|
|
1381
|
+
const listId = `${shared.id}-listbox`;
|
|
1382
|
+
const hintId = `${shared.id}-combobox-hint`;
|
|
1383
|
+
const [open, setOpen] = useState(false);
|
|
1384
|
+
const [activeIndex, setActiveIndex] = useState(0);
|
|
1385
|
+
const list = useRef(null);
|
|
1386
|
+
// A page that marks a code unavailable paints the choice list instead of this search (see
|
|
1387
|
+
// `choiceListSource`), so this list states no availability answer and no entry is refused.
|
|
1388
|
+
const entries = hostChoiceEntries(pages.items, [], node.label, translate, copy);
|
|
1389
|
+
const held = field.value === undefined || field.value === null ? undefined : field.value;
|
|
1390
|
+
const committedItem = held === undefined ? undefined : pages.offered.find((item) => sameWireValue(item.value, held));
|
|
1391
|
+
const committed = held === undefined
|
|
1392
|
+
? ''
|
|
1393
|
+
: committedItem === undefined || committedItem.labelKey === ''
|
|
1394
|
+
? renderValueText(held, copy.display)
|
|
1395
|
+
: translate(committedItem.labelKey);
|
|
1396
|
+
const text = query ?? committed;
|
|
1397
|
+
const busy = pages.readable && pages.loading;
|
|
1398
|
+
// A list with no candidate is not a listbox: the control stays closed rather than announcing
|
|
1399
|
+
// an empty popup, and `aria-activedescendant` never names an option that is not painted. A
|
|
1400
|
+
// page still in flight holds the list open and busy, so the reader is told the answer is the
|
|
1401
|
+
// host's rather than seeing the list disappear on every keystroke.
|
|
1402
|
+
const showList = open && (busy || entries.length > 0);
|
|
1403
|
+
// The active entry is read through the list rather than stored: a page that arrives after a
|
|
1404
|
+
// dependency moved can hold fewer options than the reader had moved through, and an index past
|
|
1405
|
+
// the end would name an option that is not painted and commit one that is.
|
|
1406
|
+
const activeAt = entries.length === 0 ? -1 : Math.min(activeIndex, entries.length - 1);
|
|
1407
|
+
const active = activeAt === -1 ? undefined : entries[activeAt];
|
|
1408
|
+
const described = [shared['aria-describedby'], hintId]
|
|
1409
|
+
.filter((entry) => entry !== undefined && entry !== '')
|
|
1410
|
+
.join(' ');
|
|
1411
|
+
useEffect(() => {
|
|
1412
|
+
if (!showList)
|
|
1413
|
+
return;
|
|
1414
|
+
const painted = list.current?.children[activeAt];
|
|
1415
|
+
// `scrollIntoView` is a browser behaviour; a DOM without layout (jsdom) has none.
|
|
1416
|
+
if (painted instanceof HTMLElement)
|
|
1417
|
+
painted.scrollIntoView?.({ block: 'nearest' });
|
|
1418
|
+
}, [showList, activeAt]);
|
|
1419
|
+
const search = (next) => {
|
|
1420
|
+
setQuery(next);
|
|
1421
|
+
setOpen(true);
|
|
1422
|
+
setActiveIndex(0);
|
|
1423
|
+
pages.search(next);
|
|
1424
|
+
};
|
|
1425
|
+
const resetSearch = () => {
|
|
1426
|
+
setQuery(undefined);
|
|
1427
|
+
setOpen(false);
|
|
1428
|
+
setActiveIndex(0);
|
|
1429
|
+
// The entry shows the value the record holds, so the pages go back to the first one the
|
|
1430
|
+
// source offers rather than staying narrowed by text that is no longer on screen.
|
|
1431
|
+
pages.search('');
|
|
1432
|
+
};
|
|
1433
|
+
const choose = (entry) => {
|
|
1434
|
+
writeTo(field, entry.value);
|
|
1435
|
+
resetSearch();
|
|
1436
|
+
};
|
|
1437
|
+
/** Moves the active entry, clamped to the list the current page paints. */
|
|
1438
|
+
const move = (to) => {
|
|
1439
|
+
setActiveIndex(Math.max(0, Math.min(to, entries.length - 1)));
|
|
1440
|
+
};
|
|
1441
|
+
const onKeyDown = (event) => {
|
|
1442
|
+
// Composing readers use the same arrows, Escape and Enter to operate the IME. Those keys
|
|
1443
|
+
// belong to the text composition, not to a candidate the record could commit.
|
|
1444
|
+
if (event.nativeEvent.isComposing)
|
|
1445
|
+
return;
|
|
1446
|
+
switch (event.key) {
|
|
1447
|
+
case 'ArrowDown':
|
|
1448
|
+
event.preventDefault();
|
|
1449
|
+
if (!showList)
|
|
1450
|
+
setOpen(true);
|
|
1451
|
+
else
|
|
1452
|
+
move(activeAt + 1);
|
|
1453
|
+
return;
|
|
1454
|
+
case 'ArrowUp':
|
|
1455
|
+
event.preventDefault();
|
|
1456
|
+
move(activeAt - 1);
|
|
1457
|
+
return;
|
|
1458
|
+
case 'Home':
|
|
1459
|
+
event.preventDefault();
|
|
1460
|
+
if (!showList)
|
|
1461
|
+
setOpen(true);
|
|
1462
|
+
move(0);
|
|
1463
|
+
return;
|
|
1464
|
+
case 'End':
|
|
1465
|
+
event.preventDefault();
|
|
1466
|
+
if (!showList)
|
|
1467
|
+
setOpen(true);
|
|
1468
|
+
move(entries.length - 1);
|
|
1469
|
+
return;
|
|
1470
|
+
// Only an entry commits. Text that names no option is a search that found nothing, and
|
|
1471
|
+
// writing it would turn the record into free text the domain never offered.
|
|
1472
|
+
case 'Enter':
|
|
1473
|
+
if (!showList || active === undefined)
|
|
1474
|
+
return;
|
|
1475
|
+
event.preventDefault();
|
|
1476
|
+
choose(active);
|
|
1477
|
+
return;
|
|
1478
|
+
case 'Escape':
|
|
1479
|
+
resetSearch();
|
|
1480
|
+
return;
|
|
1481
|
+
default:
|
|
1482
|
+
return;
|
|
1483
|
+
}
|
|
1484
|
+
};
|
|
1485
|
+
const leave = (destination) => {
|
|
1486
|
+
if (destination instanceof HTMLElement) {
|
|
1487
|
+
const paging = destination.closest('[data-options-more]');
|
|
1488
|
+
// Moving between this entry and its own paging action stays inside one composite control.
|
|
1489
|
+
// Another control's action, and a return from paging to this entry, are distinguished by id.
|
|
1490
|
+
if (destination.id === shared.id || paging?.getAttribute('data-options-more') === shared.id) {
|
|
1491
|
+
return;
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
resetSearch();
|
|
1495
|
+
shared.onBlur();
|
|
1496
|
+
};
|
|
1497
|
+
return (_jsxs(_Fragment, { children: [_jsx("input", { ...shared, "aria-describedby": described === '' ? undefined : described, type: "text", role: "combobox", autoComplete: node.hints.web.autoComplete, "aria-autocomplete": "list", "aria-expanded": showList, ...(showList ? { 'aria-controls': listId } : {}), ...(showList && active !== undefined
|
|
1498
|
+
? { 'aria-activedescendant': `${shared.id}-option-${String(activeAt)}` }
|
|
1499
|
+
: {}), value: text, onFocus: () => setOpen(true), onChange: (event) => search(event.target.value), onKeyDown: onKeyDown, onBlur: (event) => leave(event.relatedTarget) }), _jsx("p", { className: "ax-hint", id: hintId, children: copy.comboboxHint() }), showList ? (_jsx("ul", { ref: list, className: "ax-rank-list", id: listId, role: "listbox", "aria-label": node.label, "aria-busy": busy ? true : undefined, children: entries.map((entry, index) => (_jsxs("li", { id: `${shared.id}-option-${String(index)}`, role: "option", "aria-selected": sameWireValue(entry.value, field.value), ...(index === activeAt ? { 'data-active': '' } : {}),
|
|
1500
|
+
// Selecting on mousedown keeps focus in the input: the blur a click would cause
|
|
1501
|
+
// is what would otherwise close the list before the choice lands.
|
|
1502
|
+
onMouseDown: (event) => {
|
|
1503
|
+
event.preventDefault();
|
|
1504
|
+
choose(entry);
|
|
1505
|
+
}, children: [entry.label, entry.deprecated === true ? copy.deprecatedSuffix() : null] }, entry.key))) })) : null, _jsx(OptionsStatus, { pages: pages, copy: copy, controlId: shared.id, emptySentence: query === undefined || query.trim() === '' ? copy.emptyOptions() : copy.noMatches(), onLeave: leave })] }));
|
|
1506
|
+
}
|
|
1507
|
+
function codeText(value) {
|
|
1508
|
+
return typeof value === 'string' || typeof value === 'number' ? String(value) : '';
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* A scalar code the record holds that no declared option matches, or `undefined`.
|
|
1512
|
+
*
|
|
1513
|
+
* Only a complete declared domain can answer this: a paginated host page may hold the value on
|
|
1514
|
+
* a later page, so async domains never report a held code as missing.
|
|
1515
|
+
*/
|
|
1516
|
+
function unmatchedDeclaredCode(options, value) {
|
|
1517
|
+
if (typeof value !== 'string' && typeof value !== 'number')
|
|
1518
|
+
return undefined;
|
|
1519
|
+
const offered = options.some((option) => sameWireValue(option.code, value));
|
|
1520
|
+
return offered ? undefined : codeText(value);
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1523
|
+
* The sentence a held code is announced with.
|
|
1524
|
+
*
|
|
1525
|
+
* The field's own invalid-value words win when the engine states them; otherwise the host's
|
|
1526
|
+
* generic sentence says the domain no longer offers the value. An unavailable option is a
|
|
1527
|
+
* blocker the summary names, while a code no option matches is invalid, so the invalid words
|
|
1528
|
+
* are the reuse here.
|
|
1529
|
+
*/
|
|
1530
|
+
function heldCodeSentence(field, translate, copy) {
|
|
1531
|
+
const label = translate(field.descriptor.labelKey ?? field.fieldId);
|
|
1532
|
+
const invalid = visibleErrorsOf(field).find((error) => error.kind === 'invalid-value');
|
|
1533
|
+
if (invalid !== undefined)
|
|
1534
|
+
return errorTextOf(invalid, field, translate, copy);
|
|
1535
|
+
return copy.invalidValue(label);
|
|
1536
|
+
}
|
|
1537
|
+
function TextEntry({ node, field, shared, }) {
|
|
1538
|
+
const copy = useHostCopy();
|
|
1539
|
+
// A write the session refuses (a row identity that may not be cleared, for one) leaves the
|
|
1540
|
+
// value as it was; without a sentence beside the control the page that snaps back reads as a
|
|
1541
|
+
// dead input. The refusal is local to this entry and cleared by the next accepted write.
|
|
1542
|
+
const [refusal, setRefusal] = useState(undefined);
|
|
1543
|
+
const refusalId = `${shared.id}-refusal`;
|
|
1544
|
+
const describedBy = [shared['aria-describedby'], refusal === undefined ? undefined : refusalId]
|
|
1545
|
+
.filter((id) => id !== undefined)
|
|
1546
|
+
.join(' ');
|
|
1547
|
+
const relations = {
|
|
1548
|
+
...shared,
|
|
1549
|
+
'aria-invalid': shared['aria-invalid'] || refusal !== undefined,
|
|
1550
|
+
...(describedBy === '' ? {} : { 'aria-describedby': describedBy }),
|
|
1551
|
+
};
|
|
1552
|
+
const write = (value) => {
|
|
1553
|
+
writeTo(field, value, (message) => {
|
|
1554
|
+
setRefusal(message === undefined ? undefined : copy.writeRefused());
|
|
1555
|
+
});
|
|
1556
|
+
};
|
|
1557
|
+
const message = refusal === undefined ? null : (_jsx("p", { className: "ax-error", role: "alert", id: refusalId, children: refusal }));
|
|
1558
|
+
const text = typeof field.value === 'string' ? field.value : '';
|
|
1559
|
+
if (node.control === 'textarea') {
|
|
1560
|
+
return (_jsxs(_Fragment, { children: [_jsx("textarea", { ...relations, placeholder: node.placeholder, value: text, onChange: (event) => write(event.target.value === '' ? null : event.target.value) }), message] }));
|
|
1561
|
+
}
|
|
1562
|
+
return (_jsxs(_Fragment, { children: [_jsx("input", { ...relations,
|
|
1563
|
+
// A string field carrying the `checkbox`/`switch` hint is painted as text (a boolean is
|
|
1564
|
+
// the only wire value a checkbox can write), so the hint's own `type="checkbox"` would
|
|
1565
|
+
// turn the entry into a control that never carries the text the user types.
|
|
1566
|
+
type: node.control === 'checkbox' || node.control === 'switch' ? 'text' : node.hints.web.type, inputMode: node.hints.web.inputMode, autoComplete: node.hints.web.autoComplete,
|
|
1567
|
+
// A password entry is never autocorrected or autocapitalised: the platform's suggestion
|
|
1568
|
+
// bubble would show fragments of a secret beside the field it belongs to.
|
|
1569
|
+
autoCorrect: node.hints.web.autoCorrect, autoCapitalize: node.hints.web.autoCapitalize, placeholder: node.placeholder, value: text, onChange: (event) => write(event.target.value === '' ? null : event.target.value) }), message] }));
|
|
1570
|
+
}
|
|
1571
|
+
/**
|
|
1572
|
+
* A multiple selection as a checkbox group.
|
|
1573
|
+
*
|
|
1574
|
+
* It is the default for every list family: a native `<select multiple>` needs modifier-click on
|
|
1575
|
+
* desktop and has no usable affordance on iOS. Each box is a 44 px target, the group carries the
|
|
1576
|
+
* field's required/invalid/described-by relations, and the write is the options in declaration
|
|
1577
|
+
* order — never the order the user happened to tick them — so two records with the same selection
|
|
1578
|
+
* hold the same array.
|
|
1579
|
+
*/
|
|
1580
|
+
function CheckboxGroup({ node, field, shared, choices, }) {
|
|
1581
|
+
const copy = useHostCopy();
|
|
1582
|
+
const heldValues = Array.isArray(field.value) ? field.value : [];
|
|
1583
|
+
const selected = new Set(heldValues.map((value) => canonicalizeRestricted(value)));
|
|
1584
|
+
const labelId = `${shared.id}-label`;
|
|
1585
|
+
const toggle = (choice, checked) => {
|
|
1586
|
+
const next = new Set(selected);
|
|
1587
|
+
const token = canonicalizeRestricted(choice.value);
|
|
1588
|
+
if (checked)
|
|
1589
|
+
next.add(token);
|
|
1590
|
+
else
|
|
1591
|
+
next.delete(token);
|
|
1592
|
+
writeTo(field, withUnpaintedHeldValues(heldValues, choices.map((choice) => choice.value), choices
|
|
1593
|
+
.filter((choice) => next.has(canonicalizeRestricted(choice.value)))
|
|
1594
|
+
.map((choice) => choice.value)));
|
|
1595
|
+
};
|
|
1596
|
+
return (_jsxs("fieldset", { className: "ax-checklist",
|
|
1597
|
+
// The group itself is the element a summary link lands on: a `<label for>` pointing at the
|
|
1598
|
+
// first box would make the caption answer the question for the user.
|
|
1599
|
+
id: shared.id, tabIndex: -1, "aria-describedby": shared['aria-describedby'], "aria-invalid": shared['aria-invalid'], "aria-busy": shared['aria-busy'], disabled: shared.disabled, onBlur: shared.onBlur, children: [_jsx("legend", { className: "ax-label", id: labelId, children: _jsx(ControlLabel, { node: node, field: field, copy: copy }) }), choices.map((choice, index) => (_jsxs("label", { className: "ax-check", children: [_jsx("input", { id: `${shared.id}-${String(index)}`, type: "checkbox", value: choice.key, checked: selected.has(canonicalizeRestricted(choice.value)), disabled: shared.disabled || choice.disabled === true, "aria-required": shared['aria-required'], "aria-invalid": shared['aria-invalid'], "aria-describedby": shared['aria-describedby'], onChange: (event) => toggle(choice, event.target.checked) }), _jsxs("span", { children: [choice.label, choice.deprecated === true ? copy.deprecatedSuffix() : null] })] }, choice.key))), choices.length === 0 ? (_jsx("p", { className: "ax-hint", role: "status", "data-empty-options": "", children: copy.emptyOptions() })) : null] }));
|
|
1600
|
+
}
|
|
1601
|
+
/**
|
|
1602
|
+
* A list control: a declared option domain is a multiple selection, and a freeform list of
|
|
1603
|
+
* strings is comma-separated text. A list of structured items has no text spelling, so it is
|
|
1604
|
+
* shown read-only rather than written as a string the item contract would refuse.
|
|
1605
|
+
*
|
|
1606
|
+
* `rank` is not a multiple selection: a native multi-select reports which options are chosen,
|
|
1607
|
+
* never the order the user chose them in. It gets an ordered list whose writes are the order on
|
|
1608
|
+
* screen (`MODEL-3.1.6`).
|
|
1609
|
+
*/
|
|
1610
|
+
function ListEntry({ node, field, shared, rowKey, translate, }) {
|
|
1611
|
+
const copy = useHostCopy();
|
|
1612
|
+
const { nativeMultiSelect } = useHostActions();
|
|
1613
|
+
const options = node.declaration.options;
|
|
1614
|
+
const source = node.declaration.optionSource;
|
|
1615
|
+
if (field.readOnly)
|
|
1616
|
+
return _jsx(ReadOnlyValue, { field: field, id: shared.id, control: node.control });
|
|
1617
|
+
if (node.control === 'rank') {
|
|
1618
|
+
if (options !== undefined)
|
|
1619
|
+
return _jsx(StaticRankEntry, { node: node, field: field, shared: shared, translate: translate });
|
|
1620
|
+
if (source?.kind === 'field') {
|
|
1621
|
+
return (_jsx(FieldRankEntry, { field: field, shared: shared, sourceFieldId: source.fieldId, label: node.label }));
|
|
1622
|
+
}
|
|
1623
|
+
if (source?.kind === 'host') {
|
|
1624
|
+
return (_jsx(HostRankEntry, { node: node, field: field, shared: shared, ...(rowKey === undefined ? {} : { rowKey }), translate: translate }));
|
|
1625
|
+
}
|
|
1626
|
+
return _jsx(StaticRankEntry, { node: node, field: field, shared: shared, translate: translate });
|
|
1627
|
+
}
|
|
1628
|
+
if (options === undefined && source?.kind === 'field') {
|
|
1629
|
+
// A `tags` domain declared as another field's recorded array. The domain is that field's
|
|
1630
|
+
// current value, read through the same binding; it is not a page list and has no loader.
|
|
1631
|
+
return (_jsx(FieldListEntry, { node: node, field: field, shared: shared, sourceFieldId: source.fieldId }));
|
|
1632
|
+
}
|
|
1633
|
+
if (options === undefined && source?.kind === 'host') {
|
|
1634
|
+
// A `tags` domain loaded from the host is the same paginated domain a `timeslot` picker uses.
|
|
1635
|
+
return (_jsx(HostListEntry, { node: node, field: field, shared: shared, ...(rowKey === undefined ? {} : { rowKey }), translate: translate }));
|
|
1636
|
+
}
|
|
1637
|
+
if (node.declaration.items?.primitive === 'media_ref') {
|
|
1638
|
+
return (_jsx(MediaListEntry, { field: field, id: shared.id, translate: translate, accept: node.declaration.accept, minBytes: node.declaration.minBytes, maxBytes: node.declaration.maxBytes }));
|
|
1639
|
+
}
|
|
1640
|
+
if (options === undefined) {
|
|
1641
|
+
if (node.declaration.items?.primitive !== 'string')
|
|
1642
|
+
return _jsx(ReadOnlyValue, { field: field, id: shared.id, control: node.control });
|
|
1643
|
+
return _jsx(FreeformListEntry, { field: field, shared: shared });
|
|
1644
|
+
}
|
|
1645
|
+
const heldValues = Array.isArray(field.value) ? field.value : [];
|
|
1646
|
+
const held = new Set(heldValues.map((entry) => canonicalizeRestricted(entry)));
|
|
1647
|
+
const chosen = new Set(options.flatMap((option) => held.has(canonicalizeRestricted(optionCodeValue(node.declaration, option.code)))
|
|
1648
|
+
? [String(option.code)]
|
|
1649
|
+
: []));
|
|
1650
|
+
const choices = options.map((option) => ({
|
|
1651
|
+
key: String(option.code),
|
|
1652
|
+
value: optionCodeValue(node.declaration, option.code),
|
|
1653
|
+
label: translate(option.labelKey),
|
|
1654
|
+
deprecated: option.deprecated === true,
|
|
1655
|
+
}));
|
|
1656
|
+
if (!nativeMultiSelect) {
|
|
1657
|
+
return _jsx(CheckboxGroup, { node: node, field: field, shared: shared, choices: choices });
|
|
1658
|
+
}
|
|
1659
|
+
return (_jsxs("fieldset", { className: "ax-checklist", id: `${shared.id}-group`, tabIndex: -1, "aria-invalid": shared['aria-invalid'], "aria-describedby": shared['aria-describedby'], "aria-busy": shared['aria-busy'], disabled: shared.disabled, onBlur: shared.onBlur, children: [_jsx("legend", { className: "ax-label", children: _jsx(ControlLabel, { node: node, field: field, copy: copy }) }), _jsx("select", { ...shared, id: shared.id, "aria-required": shared['aria-required'], multiple: true, value: [...chosen], onChange: (event) => {
|
|
1660
|
+
const selected = new Set();
|
|
1661
|
+
for (const option of event.target.options) {
|
|
1662
|
+
if (option.selected)
|
|
1663
|
+
selected.add(option.value);
|
|
1664
|
+
}
|
|
1665
|
+
writeTo(field, withUnpaintedHeldValues(heldValues, choices.map((choice) => choice.value), options
|
|
1666
|
+
.filter((option) => selected.has(String(option.code)))
|
|
1667
|
+
.map((option) => optionCodeValue(node.declaration, option.code))));
|
|
1668
|
+
}, children: options.map((option) => (_jsxs("option", { value: String(option.code), children: [translate(option.labelKey), option.deprecated === true ? copy.deprecatedSuffix() : null] }, String(option.code)))) }), options.length === 0 ? (_jsx("p", { className: "ax-hint", role: "status", "data-empty-options": "", children: copy.emptyOptions() })) : null] }));
|
|
1669
|
+
}
|
|
1670
|
+
/**
|
|
1671
|
+
* A list control whose domain is a host source.
|
|
1672
|
+
*
|
|
1673
|
+
* `tags` and `rank` may declare a host `optionSource`; it is the same paginated domain a
|
|
1674
|
+
* `timeslot` picker uses, so it resolves through `useAsyncOptions` rather than a bare text box.
|
|
1675
|
+
*/
|
|
1676
|
+
function HostListEntry({ node, field, shared, rowKey, translate, }) {
|
|
1677
|
+
const pages = useAsyncOptions(node.fieldId, rowKey);
|
|
1678
|
+
const copy = useHostCopy();
|
|
1679
|
+
const { nativeMultiSelect } = useHostActions();
|
|
1680
|
+
const handoff = useChoiceListFocusHandoff(shared);
|
|
1681
|
+
const chosen = Array.isArray(field.value) ? field.value : [];
|
|
1682
|
+
const selected = pages.items.flatMap((item, index) => chosen.some((entry) => sameWireValue(entry, item.value)) ? [String(index)] : []);
|
|
1683
|
+
const busy = pages.readable && pages.loading;
|
|
1684
|
+
// A multiple selection whose loaded page carries a code the record refuses is painted as the
|
|
1685
|
+
// same list, so the reason and the press reach the reader in place of a checkbox it could not
|
|
1686
|
+
// tick and a reason it could not read.
|
|
1687
|
+
if (choiceListSource(node, field) === 'host') {
|
|
1688
|
+
return (_jsxs(_Fragment, { children: [_jsx(ChoiceList, { node: node, field: field, shared: handoff.shared, entries: hostChoiceEntries(pages.items, field.unavailableOptions ?? [], node.label, translate, copy), multiple: true, allowClear: false, onFocus: handoff.onListFocus }), _jsx(OptionsStatus, { pages: pages, copy: copy, controlId: shared.id })] }));
|
|
1689
|
+
}
|
|
1690
|
+
// A further page does not change the affordance: the group keeps the items it has while one
|
|
1691
|
+
// more is requested, rather than handing the reader a native list they did not ask for. The
|
|
1692
|
+
// group owns the empty sentence, but the same status component stays mounted across every
|
|
1693
|
+
// affordance so the further-page action keeps its press and focus state.
|
|
1694
|
+
if (pages.readable &&
|
|
1695
|
+
pages.resolved &&
|
|
1696
|
+
!nativeMultiSelect &&
|
|
1697
|
+
pages.error === undefined &&
|
|
1698
|
+
(pages.items.length > 0 || !pages.hasMore)) {
|
|
1699
|
+
return (_jsxs(_Fragment, { children: [_jsx(CheckboxGroup, { node: node, field: field, shared: shared, choices: pages.items.map((item, index) => ({
|
|
1700
|
+
key: `${String(index)}:${canonicalizeRestricted(item.value)}`,
|
|
1701
|
+
value: item.value,
|
|
1702
|
+
label: item.labelKey === ''
|
|
1703
|
+
? renderValueText(item.value, copy.display)
|
|
1704
|
+
: translate(item.labelKey),
|
|
1705
|
+
deprecated: item.deprecated === true,
|
|
1706
|
+
})) }), _jsx(OptionsStatus, { pages: pages, copy: copy, controlId: shared.id, includeEmpty: false })] }));
|
|
1707
|
+
}
|
|
1708
|
+
return (_jsxs(_Fragment, { children: [_jsx("select", { ...shared, multiple: true, "aria-busy": busy ? true : shared['aria-busy'], disabled: shared.disabled || busy, value: selected, onChange: (event) => {
|
|
1709
|
+
const picked = [];
|
|
1710
|
+
for (const option of event.target.options) {
|
|
1711
|
+
if (!option.selected)
|
|
1712
|
+
continue;
|
|
1713
|
+
const item = pages.items[Number(option.value)];
|
|
1714
|
+
if (item !== undefined)
|
|
1715
|
+
picked.push(item.value);
|
|
1716
|
+
}
|
|
1717
|
+
writeTo(field, withUnpaintedHeldValues(chosen, pages.items.map((item) => item.value), picked));
|
|
1718
|
+
}, children: pages.items.map((item, index) => (_jsxs("option", { value: String(index), children: [item.labelKey === ''
|
|
1719
|
+
? renderValueText(item.value, copy.display)
|
|
1720
|
+
: translate(item.labelKey), item.deprecated === true ? copy.deprecatedSuffix() : null] }, `${String(index)}:${canonicalizeRestricted(item.value)}`))) }), _jsx(OptionsStatus, { pages: pages, copy: copy, controlId: shared.id })] }));
|
|
1721
|
+
}
|
|
1722
|
+
/**
|
|
1723
|
+
* The page states a host-loaded domain reports, painted under the control that offers it.
|
|
1724
|
+
*
|
|
1725
|
+
* Loading, a domain that resolved to nothing, a page the loader refused and a domain that holds
|
|
1726
|
+
* further pages are different answers, so each has its own line or action. An unresolved page and
|
|
1727
|
+
* one whose dependency is withheld state neither loading nor an empty domain, and a page that
|
|
1728
|
+
* states another page is not yet an empty domain.
|
|
1729
|
+
*
|
|
1730
|
+
* `controlId` is the id of whichever affordance this domain is painted through: a press that
|
|
1731
|
+
* finishes the domain removes the action it was pressed on, and the reader's keyboard has to land
|
|
1732
|
+
* on the control rather than on the document.
|
|
1733
|
+
*/
|
|
1734
|
+
function OptionsStatus({ pages, copy, controlId, includeEmpty = true, emptySentence, onLeave, }) {
|
|
1735
|
+
return (_jsxs(_Fragment, { children: [_jsx("p", { className: "ax-hint", role: "status", "data-options-status": "", children: pages.readable && pages.loading ? copy.loadingOptions() : '' }), includeEmpty &&
|
|
1736
|
+
pages.readable &&
|
|
1737
|
+
pages.resolved &&
|
|
1738
|
+
pages.error === undefined &&
|
|
1739
|
+
pages.items.length === 0 &&
|
|
1740
|
+
!pages.hasMore ? (_jsx("p", { className: "ax-hint", role: "status", "data-empty-options": "", children: emptySentence ?? copy.emptyOptions() })) : null, !pages.readable || pages.error === undefined ? null : (_jsxs("div", { className: "ax-error", role: "alert", "data-options": "failed", children: [_jsx("p", { children: copy.optionsFailed() }), _jsx("p", { className: "ax-hint", "data-options-detail": "", children: pages.error.message })] })), _jsx(OptionsMore, { pages: pages, copy: copy, controlId: controlId, ...(onLeave === undefined ? {} : { onLeave }) })] }));
|
|
1741
|
+
}
|
|
1742
|
+
/**
|
|
1743
|
+
* The action that asks a paginated domain for its next page.
|
|
1744
|
+
*
|
|
1745
|
+
* A page list is bounded and cancellable, so the consumer advances it: the reader asks, the
|
|
1746
|
+
* machine loads one more page and the control re-reads the merged domain — including the
|
|
1747
|
+
* availability answers the new page carries. The action stays painted while a page is in flight,
|
|
1748
|
+
* so the keyboard rests on it across the load, and a press while one is loading is the machine's
|
|
1749
|
+
* to refuse rather than a second request.
|
|
1750
|
+
*/
|
|
1751
|
+
function OptionsMore({ pages, copy, controlId, onLeave, }) {
|
|
1752
|
+
const [asked, setAsked] = useState(false);
|
|
1753
|
+
const complete = !pages.loading && !pages.hasMore;
|
|
1754
|
+
useEffect(() => {
|
|
1755
|
+
if (!asked || !complete)
|
|
1756
|
+
return;
|
|
1757
|
+
// The action that was pressed is gone, and the domain may have published an availability
|
|
1758
|
+
// answer that replaces the affordance in a later commit, so the keyboard is handed over once
|
|
1759
|
+
// both have settled. A reader who moved on during the load keeps where they went.
|
|
1760
|
+
const handoff = setTimeout(() => {
|
|
1761
|
+
setAsked(false);
|
|
1762
|
+
if (document.activeElement === null || document.activeElement === document.body) {
|
|
1763
|
+
document.getElementById(controlId)?.focus();
|
|
1764
|
+
}
|
|
1765
|
+
}, 0);
|
|
1766
|
+
return () => {
|
|
1767
|
+
clearTimeout(handoff);
|
|
1768
|
+
};
|
|
1769
|
+
}, [asked, complete, controlId]);
|
|
1770
|
+
if (!pages.readable || !pages.hasMore || pages.error !== undefined)
|
|
1771
|
+
return null;
|
|
1772
|
+
return (_jsx("div", { className: "ax-actions", "data-options-more": controlId, onBlur: (event) => onLeave?.(event.relatedTarget), children: _jsx("button", { type: "button", className: "ax-button ax-button--secondary", "aria-busy": pages.loading ? true : undefined, onClick: () => {
|
|
1773
|
+
setAsked(true);
|
|
1774
|
+
pages.loadMore();
|
|
1775
|
+
}, children: copy.loadMoreOptions() }) }));
|
|
1776
|
+
}
|
|
1777
|
+
/**
|
|
1778
|
+
* A list control whose domain is another field's recorded array.
|
|
1779
|
+
*
|
|
1780
|
+
* `optionSource: { kind: "field" }` names a sibling binding, not a page list; asking
|
|
1781
|
+
* `useAsyncOptions` for one is a programming error. The domain is that binding's current value,
|
|
1782
|
+
* read through the same session, so an edit to the source repaints this picker.
|
|
1783
|
+
*/
|
|
1784
|
+
function FieldListEntry({ node, field, shared, sourceFieldId, }) {
|
|
1785
|
+
const source = useField(sourceFieldId);
|
|
1786
|
+
const copy = useHostCopy();
|
|
1787
|
+
const { nativeMultiSelect } = useHostActions();
|
|
1788
|
+
const items = Array.isArray(source.value) ? source.value : [];
|
|
1789
|
+
const chosen = Array.isArray(field.value) ? field.value : [];
|
|
1790
|
+
const selected = items.flatMap((value, index) => chosen.some((entry) => sameWireValue(entry, value)) ? [String(index)] : []);
|
|
1791
|
+
if (!nativeMultiSelect) {
|
|
1792
|
+
return (_jsx(CheckboxGroup, { node: node, field: field, shared: shared, choices: items.map((value, index) => ({
|
|
1793
|
+
key: `${String(index)}:${canonicalizeRestricted(value)}`,
|
|
1794
|
+
value,
|
|
1795
|
+
label: renderValueText(value, copy.display),
|
|
1796
|
+
})) }));
|
|
1797
|
+
}
|
|
1798
|
+
return (_jsxs("fieldset", { className: "ax-checklist", id: `${shared.id}-group`, tabIndex: -1, "aria-invalid": shared['aria-invalid'], "aria-describedby": shared['aria-describedby'], "aria-busy": shared['aria-busy'], disabled: shared.disabled, onBlur: shared.onBlur, children: [_jsx("legend", { className: "ax-label", children: _jsx(ControlLabel, { node: node, field: field, copy: copy }) }), _jsx("select", { ...shared, id: shared.id, "aria-required": shared['aria-required'], multiple: true, value: selected, onChange: (event) => {
|
|
1799
|
+
const picked = [];
|
|
1800
|
+
for (const option of event.target.options) {
|
|
1801
|
+
if (!option.selected)
|
|
1802
|
+
continue;
|
|
1803
|
+
const value = items[Number(option.value)];
|
|
1804
|
+
if (value !== undefined)
|
|
1805
|
+
picked.push(value);
|
|
1806
|
+
}
|
|
1807
|
+
writeTo(field, withUnpaintedHeldValues(chosen, items, picked));
|
|
1808
|
+
}, children: items.map((value, index) => (_jsx("option", { value: String(index), children: renderValueText(value, copy.display) }, `${String(index)}:${canonicalizeRestricted(value)}`))) }), items.length === 0 ? (_jsx("p", { className: "ax-hint", role: "status", "data-empty-options": "", children: copy.emptyOptions() })) : null] }));
|
|
1809
|
+
}
|
|
1810
|
+
/** A ranking with a static, declared option domain. */
|
|
1811
|
+
function StaticRankEntry({ node, field, shared, translate, }) {
|
|
1812
|
+
const domain = (node.declaration.options ?? []).map((option) => ({
|
|
1813
|
+
key: String(option.code),
|
|
1814
|
+
value: option.code,
|
|
1815
|
+
label: translate(option.labelKey),
|
|
1816
|
+
}));
|
|
1817
|
+
return (_jsx(RankEditor, { field: field, shared: shared, label: node.label, domain: domain, busy: false, status: "", failure: undefined }));
|
|
1818
|
+
}
|
|
1819
|
+
/** A ranking whose domain is another field's value, or a host source: loaded pages of items. */
|
|
1820
|
+
function HostRankEntry({ node, field, shared, rowKey, translate, }) {
|
|
1821
|
+
const pages = useAsyncOptions(node.fieldId, rowKey);
|
|
1822
|
+
const copy = useHostCopy();
|
|
1823
|
+
const domain = pages.items.map((item) => ({
|
|
1824
|
+
key: canonicalizeRestricted(item.value),
|
|
1825
|
+
value: item.value,
|
|
1826
|
+
label: item.labelKey === '' ? renderValueText(item.value, copy.display) : translate(item.labelKey),
|
|
1827
|
+
}));
|
|
1828
|
+
return (_jsx(RankEditor, { field: field, shared: shared, label: node.label, domain: domain, busy: pages.loading, status: pages.loading ? copy.loadingOptions() : '', failure: pages.error === undefined ? undefined : copy.optionsFailed(), failureDetail: pages.error?.message }));
|
|
1829
|
+
}
|
|
1830
|
+
/** A ranking whose domain is another field's recorded array, read through the same session. */
|
|
1831
|
+
function FieldRankEntry({ field, shared, sourceFieldId, label, }) {
|
|
1832
|
+
const source = useField(sourceFieldId);
|
|
1833
|
+
const copy = useHostCopy();
|
|
1834
|
+
const items = Array.isArray(source.value) ? source.value : [];
|
|
1835
|
+
const domain = items.map((value, index) => ({
|
|
1836
|
+
key: `${String(index)}:${canonicalizeRestricted(value)}`,
|
|
1837
|
+
value,
|
|
1838
|
+
label: renderValueText(value, copy.display),
|
|
1839
|
+
}));
|
|
1840
|
+
return (_jsx(RankEditor, { field: field, shared: shared, label: label, domain: domain, busy: false, status: "", failure: undefined }));
|
|
1841
|
+
}
|
|
1842
|
+
/**
|
|
1843
|
+
* The orderable list, shared by a declared and a loaded domain.
|
|
1844
|
+
*
|
|
1845
|
+
* A native `<select multiple>` reports membership, not order, so it can never store a ranking.
|
|
1846
|
+
* This control renders the current value as an ordered list; moving, removing or adding writes
|
|
1847
|
+
* the array in the order on screen, which is the order the user expressed.
|
|
1848
|
+
*/
|
|
1849
|
+
function RankEditor({ field, shared, label, domain, busy, status, failure, failureDetail, }) {
|
|
1850
|
+
const copy = useHostCopy();
|
|
1851
|
+
const values = Array.isArray(field.value) ? field.value : [];
|
|
1852
|
+
const entries = values.map((value) => {
|
|
1853
|
+
const match = domain.find((item) => sameWireValue(item.value, value));
|
|
1854
|
+
return {
|
|
1855
|
+
// A ranking's values are unique, so the value alone is the identity: a move changes the
|
|
1856
|
+
// position but not the row, and a key carrying the index would remount it (and lose focus).
|
|
1857
|
+
key: canonicalizeRestricted(value),
|
|
1858
|
+
value,
|
|
1859
|
+
label: match?.label ?? renderValueText(value, copy.display),
|
|
1860
|
+
};
|
|
1861
|
+
});
|
|
1862
|
+
const addable = domain.filter((item) => !values.some((value) => sameWireValue(value, item.value)));
|
|
1863
|
+
const commit = (next) => {
|
|
1864
|
+
writeTo(field, next);
|
|
1865
|
+
};
|
|
1866
|
+
return (_jsx(RankOrder, { shared: shared, label: label, entries: entries, addable: addable, busy: busy, status: status, failure: failure, failureDetail: failureDetail, copy: copy, onMove: (index, delta) => {
|
|
1867
|
+
const next = [...values];
|
|
1868
|
+
const target = index + delta;
|
|
1869
|
+
if (target < 0 || target >= next.length)
|
|
1870
|
+
return;
|
|
1871
|
+
const moved = next.splice(index, 1)[0];
|
|
1872
|
+
if (moved === undefined)
|
|
1873
|
+
return;
|
|
1874
|
+
next.splice(target, 0, moved);
|
|
1875
|
+
commit(next);
|
|
1876
|
+
}, onRemove: (index) => {
|
|
1877
|
+
commit(values.filter((_, at) => at !== index));
|
|
1878
|
+
}, onAdd: (value) => {
|
|
1879
|
+
commit([...values, value]);
|
|
1880
|
+
} }));
|
|
1881
|
+
}
|
|
1882
|
+
/** The presentational half of a ranking: an ordered list plus one add picker. */
|
|
1883
|
+
function RankOrder({ shared, label, entries, addable, busy, status, failure, failureDetail, copy, onMove, onRemove, onAdd, }) {
|
|
1884
|
+
// Each handle registers itself under its action and its value, so a reordered list still has
|
|
1885
|
+
// one address per row and the effect below can hand the keyboard back after the repaint.
|
|
1886
|
+
const buttons = useRef(new Map());
|
|
1887
|
+
const addPicker = useRef(null);
|
|
1888
|
+
const pendingFocus = useRef(undefined);
|
|
1889
|
+
const [announcement, setAnnouncement] = useState('');
|
|
1890
|
+
useEffect(() => {
|
|
1891
|
+
const request = pendingFocus.current;
|
|
1892
|
+
if (request === undefined)
|
|
1893
|
+
return;
|
|
1894
|
+
pendingFocus.current = undefined;
|
|
1895
|
+
if (request.where === 'add' || entries.length === 0) {
|
|
1896
|
+
addPicker.current?.focus();
|
|
1897
|
+
return;
|
|
1898
|
+
}
|
|
1899
|
+
const wanted = buttons.current.get(`${request.action}:${request.key}`);
|
|
1900
|
+
// A moved row's pressed button may have become disabled at the end of its travel (the first
|
|
1901
|
+
// row has no "up"), so its other move handle is the same row's handle.
|
|
1902
|
+
const alternate = request.action === 'remove'
|
|
1903
|
+
? undefined
|
|
1904
|
+
: buttons.current.get(`${request.action === 'up' ? 'down' : 'up'}:${request.key}`);
|
|
1905
|
+
const target = [wanted, alternate].find((button) => button !== undefined && !button.disabled);
|
|
1906
|
+
if (target !== undefined) {
|
|
1907
|
+
target.focus();
|
|
1908
|
+
return;
|
|
1909
|
+
}
|
|
1910
|
+
// The row itself may be gone (a removal whose neighbour was already at its bound). Land on
|
|
1911
|
+
// the next enabled handle rather than on `<body>`.
|
|
1912
|
+
for (const button of buttons.current.values()) {
|
|
1913
|
+
if (!button.disabled) {
|
|
1914
|
+
button.focus();
|
|
1915
|
+
return;
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
addPicker.current?.focus();
|
|
1919
|
+
}, [entries]);
|
|
1920
|
+
const register = (action, key) => {
|
|
1921
|
+
return (element) => {
|
|
1922
|
+
const token = `${action}:${key}`;
|
|
1923
|
+
if (element === null)
|
|
1924
|
+
buttons.current.delete(token);
|
|
1925
|
+
else
|
|
1926
|
+
buttons.current.set(token, element);
|
|
1927
|
+
};
|
|
1928
|
+
};
|
|
1929
|
+
const move = (index, delta) => {
|
|
1930
|
+
const entry = entries[index];
|
|
1931
|
+
if (entry === undefined)
|
|
1932
|
+
return;
|
|
1933
|
+
const position = index + delta + 1;
|
|
1934
|
+
if (position < 1 || position > entries.length)
|
|
1935
|
+
return;
|
|
1936
|
+
pendingFocus.current = {
|
|
1937
|
+
where: 'button',
|
|
1938
|
+
action: delta === -1 ? 'up' : 'down',
|
|
1939
|
+
key: entry.key,
|
|
1940
|
+
};
|
|
1941
|
+
setAnnouncement(copy.rankMoved(entry.label, position, entries.length));
|
|
1942
|
+
onMove(index, delta);
|
|
1943
|
+
};
|
|
1944
|
+
const remove = (index) => {
|
|
1945
|
+
const entry = entries[index];
|
|
1946
|
+
if (entry === undefined)
|
|
1947
|
+
return;
|
|
1948
|
+
const next = entries[index + 1] ?? entries[index - 1];
|
|
1949
|
+
pendingFocus.current =
|
|
1950
|
+
next === undefined ? { where: 'add' } : { where: 'button', action: 'remove', key: next.key };
|
|
1951
|
+
setAnnouncement(copy.rankRemoved(entry.label, entries.length - 1));
|
|
1952
|
+
onRemove(index);
|
|
1953
|
+
};
|
|
1954
|
+
return (_jsxs("fieldset", { className: "ax-rank", id: shared.id, tabIndex: -1, "aria-invalid": shared['aria-invalid'], "aria-describedby": shared['aria-describedby'], "aria-busy": shared['aria-busy'], disabled: shared.disabled, onBlur: shared.onBlur, children: [_jsx("legend", { className: "ax-label", children: label }), entries.length === 0 ? (_jsx("p", { className: "ax-hint", children: copy.rankEmpty() })) : (_jsx("ol", { className: "ax-rank-list", children: entries.map((entry, index) => (_jsxs("li", { className: "ax-rank-item", children: [_jsx("span", { className: "ax-rank-label", children: entry.label }), _jsxs("div", { className: "ax-actions", children: [_jsx("button", { type: "button", ref: register('up', entry.key), "aria-label": copy.rankMoveUp(entry.label), disabled: busy || index === 0, onClick: () => move(index, -1), children: "\u2191" }), _jsx("button", { type: "button", ref: register('down', entry.key), "aria-label": copy.rankMoveDown(entry.label), disabled: busy || index === entries.length - 1, onClick: () => move(index, 1), children: "\u2193" }), _jsx("button", { type: "button", ref: register('remove', entry.key), "aria-label": copy.rankRemove(entry.label), disabled: busy, onClick: () => remove(index), children: "\u00D7" })] })] }, entry.key))) })), _jsxs("select", { ...shared, id: `${shared.id}-add`, "aria-describedby": undefined, "aria-required": shared['aria-required'], ref: addPicker, className: "ax-control ax-control--rank", "aria-label": copy.rankAddTo(label), disabled: shared.disabled || busy, value: "", onChange: (event) => {
|
|
1955
|
+
const value = event.target.value;
|
|
1956
|
+
// The placeholder is the picker's current value, not an option: choosing it is a
|
|
1957
|
+
// "no choice" gesture and appends nothing. `Number('')` would be `0`, the first item.
|
|
1958
|
+
if (value === '')
|
|
1959
|
+
return;
|
|
1960
|
+
const item = addable[Number(value)];
|
|
1961
|
+
if (item !== undefined)
|
|
1962
|
+
onAdd(item.value);
|
|
1963
|
+
}, children: [_jsx("option", { value: "", children: copy.rankAdd() }), addable.map((item, index) => (_jsx("option", { value: String(index), children: item.label }, item.key)))] }), _jsx("p", { className: "ax-hint", role: "status", "data-rank-status": "", children: status === '' ? announcement : status }), failure === undefined ? null : (_jsxs("div", { className: "ax-error", role: "alert", children: [_jsx("p", { children: failure }), failureDetail === undefined ? null : _jsx("p", { className: "ax-hint", children: failureDetail })] }))] }));
|
|
1964
|
+
}
|
|
1965
|
+
/**
|
|
1966
|
+
* A bounded number as a slider.
|
|
1967
|
+
*
|
|
1968
|
+
* The bounds and step are the compiled presentation hints, not the enforcement: the declared
|
|
1969
|
+
* rules still decide what may be submitted. The current value is stated beside the track, the
|
|
1970
|
+
* ends are labelled, and a declared option domain supplies tick labels. A range input cannot be
|
|
1971
|
+
* read-only, so a read-only binding paints its value instead.
|
|
1972
|
+
*
|
|
1973
|
+
* A range input with no value paints the platform's midpoint, which would claim an answer the
|
|
1974
|
+
* record does not hold and disagree with both the empty output beside it and the first nudge.
|
|
1975
|
+
* An unanswered field therefore pins the thumb to the declared minimum — where a nudge starts —
|
|
1976
|
+
* and states that it has no answer yet, visibly and through `aria-valuetext`.
|
|
1977
|
+
*/
|
|
1978
|
+
function SliderEntry({ node, field, shared, translate, }) {
|
|
1979
|
+
const copy = useHostCopy();
|
|
1980
|
+
if (field.readOnly)
|
|
1981
|
+
return _jsx(ReadOnlyValue, { field: field, id: shared.id, control: node.control });
|
|
1982
|
+
const minimum = node.declaration.minimum;
|
|
1983
|
+
const maximum = node.declaration.maximum;
|
|
1984
|
+
if (minimum === undefined || maximum === undefined) {
|
|
1985
|
+
// A slider without a whole range has no track; until the declaration carries one, the value
|
|
1986
|
+
// is still editable as a number rather than painted by a control that cannot move.
|
|
1987
|
+
return _jsx(BufferedEntry, { node: node, field: field, id: shared.id, format: { kind: 'decimal' } });
|
|
1988
|
+
}
|
|
1989
|
+
const step = node.declaration.step;
|
|
1990
|
+
const value = field.value;
|
|
1991
|
+
const text = typeof value === 'string' || typeof value === 'number' ? String(value) : '';
|
|
1992
|
+
const unanswered = text === '';
|
|
1993
|
+
// The thumb sits at the start of the declared range, not at the browser's midpoint. That is
|
|
1994
|
+
// also the value the first nudge steps away from, so the track and the write agree.
|
|
1995
|
+
const position = unanswered ? String(minimum) : text;
|
|
1996
|
+
const positionNumber = Number(position);
|
|
1997
|
+
const unansweredText = unanswered ? copy.notAnswered(node.label) : undefined;
|
|
1998
|
+
const options = node.declaration.options ?? [];
|
|
1999
|
+
return (_jsxs("div", { className: "ax-slider", children: [_jsxs("div", { className: "ax-slider-head", children: [_jsx("output", { htmlFor: shared.id, className: "ax-slider-value", "aria-label": node.label, children: unansweredText === undefined ? _jsx("bdi", { dir: "ltr", children: text }) : unansweredText }), _jsxs("span", { className: "ax-slider-bounds", children: [String(minimum), "\u2013", String(maximum)] })] }), _jsx("input", { ...shared, type: "range", min: String(minimum), max: String(maximum), ...(step === undefined ? {} : { step: String(step) }), value: position, ...(Number.isFinite(positionNumber) ? { 'aria-valuenow': positionNumber } : {}), ...(unansweredText === undefined ? {} : { 'aria-valuetext': unansweredText }), onChange: (event) => {
|
|
2000
|
+
const next = event.target.value;
|
|
2001
|
+
writeTo(field, node.declaration.primitive === 'integer' ? Number(next) : next);
|
|
2002
|
+
} }), options.length === 0 ? null : (_jsx("div", { className: "ax-slider-ticks", "aria-hidden": "true", children: options.map((option) => (_jsx("span", { className: "ax-slider-tick", children: translate(option.labelKey) }, String(option.code)))) }))] }));
|
|
2003
|
+
}
|
|
2004
|
+
/** Whether the declaration asks for seconds on a time or date-time control. */
|
|
2005
|
+
function wantsSeconds(node) {
|
|
2006
|
+
return node.declaration.annotations?.['seconds'] === true;
|
|
2007
|
+
}
|
|
2008
|
+
/** The local civil date and clock of a stored instant, when it is one. */
|
|
2009
|
+
function localPartsOf(iso) {
|
|
2010
|
+
if (iso === undefined)
|
|
2011
|
+
return undefined;
|
|
2012
|
+
const date = new Date(iso);
|
|
2013
|
+
if (Number.isNaN(date.getTime()))
|
|
2014
|
+
return undefined;
|
|
2015
|
+
const pad = (value) => String(value).padStart(2, '0');
|
|
2016
|
+
return {
|
|
2017
|
+
date: `${String(date.getFullYear())}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`,
|
|
2018
|
+
time: `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`,
|
|
2019
|
+
};
|
|
2020
|
+
}
|
|
2021
|
+
function parseDurationAmount(raw) {
|
|
2022
|
+
const trimmed = raw.trim();
|
|
2023
|
+
if (trimmed === '')
|
|
2024
|
+
return { kind: 'empty' };
|
|
2025
|
+
if (trimmed === '+')
|
|
2026
|
+
return { kind: 'partial' };
|
|
2027
|
+
const body = trimmed.startsWith('+') ? trimmed.slice(1) : trimmed;
|
|
2028
|
+
if (body.startsWith('-')) {
|
|
2029
|
+
return { kind: 'invalid', reason: 'buffer.integer.minimum', params: { minimum: 0 } };
|
|
2030
|
+
}
|
|
2031
|
+
if (/^[0-9]*[.,][0-9]*$/u.test(body)) {
|
|
2032
|
+
return { kind: 'invalid', reason: 'buffer.integer.no_point' };
|
|
2033
|
+
}
|
|
2034
|
+
if (!/^[0-9]+$/u.test(body)) {
|
|
2035
|
+
return { kind: 'invalid', reason: 'buffer.integer.digits' };
|
|
2036
|
+
}
|
|
2037
|
+
const amount = Number(body);
|
|
2038
|
+
if (!Number.isSafeInteger(amount)) {
|
|
2039
|
+
return { kind: 'invalid', reason: 'buffer.integer.not_safe' };
|
|
2040
|
+
}
|
|
2041
|
+
return { kind: 'value', amount };
|
|
2042
|
+
}
|
|
2043
|
+
/**
|
|
2044
|
+
* A duration as a whole number and the declared unit, `MODEL-2.1.24`.
|
|
2045
|
+
*
|
|
2046
|
+
* The unit travels in the value, so the control writes `{ amount, unit }` and never a bare
|
|
2047
|
+
* number; the affix states the localized unit and is associated with the input through
|
|
2048
|
+
* `aria-describedby` so assistive technology announces the unit beside the field label.
|
|
2049
|
+
* Invalid edits (negative or fractional entries) stay buffered in the input with an explicit
|
|
2050
|
+
* localized refusal sentence rather than silently reverting to the committed value.
|
|
2051
|
+
*/
|
|
2052
|
+
function DurationEntry({ node, field, shared, }) {
|
|
2053
|
+
const copy = useHostCopy();
|
|
2054
|
+
const record = isRecord(field.value) ? field.value : undefined;
|
|
2055
|
+
const committed = record !== undefined && typeof record['amount'] === 'number' ? String(record['amount']) : '';
|
|
2056
|
+
const unit = node.declaration.unit ?? 'minutes';
|
|
2057
|
+
const [draft, setDraft] = useState(undefined);
|
|
2058
|
+
const [writeRefusal, setWriteRefusal] = useState(undefined);
|
|
2059
|
+
const text = draft !== undefined && draft.id === shared.id ? draft.text : committed;
|
|
2060
|
+
const parsed = parseDurationAmount(text);
|
|
2061
|
+
const parseError = parsed.kind === 'invalid' ? parsed : undefined;
|
|
2062
|
+
const unitId = `${shared.id}-unit`;
|
|
2063
|
+
const refusalId = `${shared.id}-refusal`;
|
|
2064
|
+
const writeRefusalId = `${shared.id}-write-refusal`;
|
|
2065
|
+
const described = [
|
|
2066
|
+
shared['aria-describedby'],
|
|
2067
|
+
unitId,
|
|
2068
|
+
parseError === undefined ? undefined : refusalId,
|
|
2069
|
+
writeRefusal === undefined ? undefined : writeRefusalId,
|
|
2070
|
+
]
|
|
2071
|
+
.filter((entry) => entry !== undefined && entry !== '')
|
|
2072
|
+
.join(' ');
|
|
2073
|
+
const commitValue = (next, canonicalText, raw) => {
|
|
2074
|
+
const result = absorbWrite(() => field.setValue(next));
|
|
2075
|
+
if (result.kind === 'accepted') {
|
|
2076
|
+
setWriteRefusal(undefined);
|
|
2077
|
+
setDraft(raw === canonicalText ? undefined : { id: shared.id, text: raw });
|
|
2078
|
+
return;
|
|
2079
|
+
}
|
|
2080
|
+
setWriteRefusal(copy.writeRefused());
|
|
2081
|
+
setDraft({ id: shared.id, text: raw });
|
|
2082
|
+
};
|
|
2083
|
+
return (_jsxs(_Fragment, { children: [_jsxs("div", { className: "ax-duration", children: [_jsx("input", { ...shared, type: node.hints.web.type, inputMode: node.hints.web.inputMode, autoComplete: node.hints.web.autoComplete, autoCorrect: node.hints.web.autoCorrect, autoCapitalize: node.hints.web.autoCapitalize, placeholder: node.placeholder, "aria-invalid": shared['aria-invalid'] || parseError !== undefined || writeRefusal !== undefined, "aria-describedby": described === '' ? undefined : described, value: text, onChange: (event) => {
|
|
2084
|
+
const raw = event.target.value;
|
|
2085
|
+
const next = parseDurationAmount(raw);
|
|
2086
|
+
if (next.kind === 'empty') {
|
|
2087
|
+
commitValue(null, '', raw);
|
|
2088
|
+
return;
|
|
2089
|
+
}
|
|
2090
|
+
if (next.kind === 'value') {
|
|
2091
|
+
commitValue({ amount: next.amount, unit }, String(next.amount), raw);
|
|
2092
|
+
return;
|
|
2093
|
+
}
|
|
2094
|
+
setWriteRefusal(undefined);
|
|
2095
|
+
setDraft({ id: shared.id, text: raw });
|
|
2096
|
+
}, onKeyDown: (event) => {
|
|
2097
|
+
if (event.key !== 'Escape')
|
|
2098
|
+
return;
|
|
2099
|
+
setDraft(undefined);
|
|
2100
|
+
setWriteRefusal(undefined);
|
|
2101
|
+
}, onBlur: () => {
|
|
2102
|
+
if (parsed.kind === 'empty') {
|
|
2103
|
+
if (committed !== '') {
|
|
2104
|
+
commitValue(null, '', '');
|
|
2105
|
+
}
|
|
2106
|
+
else {
|
|
2107
|
+
setDraft(undefined);
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
else if (parsed.kind === 'value') {
|
|
2111
|
+
const target = { amount: parsed.amount, unit };
|
|
2112
|
+
if (!sameWireValue(target, field.value)) {
|
|
2113
|
+
commitValue(target, String(parsed.amount), String(parsed.amount));
|
|
2114
|
+
}
|
|
2115
|
+
else {
|
|
2116
|
+
setDraft(undefined);
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
shared.onBlur();
|
|
2120
|
+
} }), _jsx("span", { className: "ax-duration-unit", id: unitId, children: copy.durationUnit(unit) })] }), parseError === undefined ? null : (_jsx("p", { className: "ax-error", role: "alert", id: refusalId, "data-buffer": "refused", children: copy.buffer(parseError.reason, parseError.params) })), writeRefusal === undefined ? null : (_jsx("p", { className: "ax-error", role: "alert", id: writeRefusalId, "data-write": "refused", children: writeRefusal }))] }));
|
|
2121
|
+
}
|
|
2122
|
+
/** A time of day on its own, written as `HH:MM` or `HH:MM:SS`. */
|
|
2123
|
+
function TimeEntry({ node, field, id, describedBy, }) {
|
|
2124
|
+
const locale = useLocale();
|
|
2125
|
+
return (_jsx(TimeSegments, { controlId: id, value: typeof field.value === 'string' ? field.value : undefined, onChange: (text) => writeTo(field, text), seconds: wantsSeconds(node), locale: locale, disabled: field.disabled, readOnly: field.readOnly, label: node.label, describedBy: describedBy, invalid: field.a11y.invalid, required: field.a11y.required, onBlur: field.onBlur }));
|
|
2126
|
+
}
|
|
2127
|
+
/**
|
|
2128
|
+
* A date and a time of day on one binding.
|
|
2129
|
+
*
|
|
2130
|
+
* The value is still the wire's canonical UTC instant. The date picker edits the local civil
|
|
2131
|
+
* date, the segments edit the local clock, and the two are combined into the instant the record
|
|
2132
|
+
* holds; the browser's zone is the host commitment `datetime` already documents.
|
|
2133
|
+
*
|
|
2134
|
+
* A time alone is not an instant, so the clock segments are disabled — and say why — until a date
|
|
2135
|
+
* has been chosen. Previously an empty field still painted an editable `09:00` clock whose
|
|
2136
|
+
* keystrokes were silently dropped: `write` returned early with no date, so ArrowUp moved nothing
|
|
2137
|
+
* and wrote nothing. Refusing the interaction with a stated reason is the honest empty state, and
|
|
2138
|
+
* choosing a date enables the clock at the default reading it shows.
|
|
2139
|
+
*/
|
|
2140
|
+
function DateTimeEntry({ node, field, id, describedBy, }) {
|
|
2141
|
+
const locale = useLocale();
|
|
2142
|
+
const stored = typeof field.value === 'string' ? field.value : undefined;
|
|
2143
|
+
const local = localPartsOf(stored);
|
|
2144
|
+
const date = local?.date;
|
|
2145
|
+
const time = local?.time ?? '09:00:00';
|
|
2146
|
+
const dateFirstId = `${id}-date-first`;
|
|
2147
|
+
// The reason is the calendar's own word for the gesture, so a reader who lands on a disabled
|
|
2148
|
+
// segment hears the action that would enable it rather than a sentence invented here. A field
|
|
2149
|
+
// the session has already made read-only or disabled offers no such gesture, so it states no
|
|
2150
|
+
// instruction it cannot honour.
|
|
2151
|
+
const clockBlocked = date === undefined && !field.readOnly && !field.disabled;
|
|
2152
|
+
const dateFirst = clockBlocked ? scheduleText(locale).chooseDate : undefined;
|
|
2153
|
+
const timeDescribed = [describedBy, dateFirst === undefined ? undefined : dateFirstId]
|
|
2154
|
+
.filter((entry) => entry !== undefined && entry !== '')
|
|
2155
|
+
.join(' ');
|
|
2156
|
+
const write = (nextDate, nextTime) => {
|
|
2157
|
+
if (nextDate === undefined)
|
|
2158
|
+
return;
|
|
2159
|
+
const [year, month, day] = nextDate.split('-').map(Number);
|
|
2160
|
+
const [hours, minutes, seconds] = nextTime.split(':').map(Number);
|
|
2161
|
+
if (year === undefined ||
|
|
2162
|
+
month === undefined ||
|
|
2163
|
+
day === undefined ||
|
|
2164
|
+
hours === undefined ||
|
|
2165
|
+
minutes === undefined) {
|
|
2166
|
+
return;
|
|
2167
|
+
}
|
|
2168
|
+
const instant = new Date(year, month - 1, day, hours, minutes, seconds ?? 0);
|
|
2169
|
+
if (Number.isNaN(instant.getTime()))
|
|
2170
|
+
return;
|
|
2171
|
+
writeTo(field, instant.toISOString());
|
|
2172
|
+
};
|
|
2173
|
+
return (_jsxs("div", { className: "ax-datetime", children: [_jsx(DatePicker, { controlId: id, descriptionId: field.a11y.descriptionId, errorId: field.a11y.errorId, label: node.label, value: date, setValue: (next) => write(next, time), required: field.a11y.required, invalid: field.a11y.invalid, disabled: field.disabled, readOnly: field.readOnly, locale: locale, minimum: node.declaration.minimum, maximum: node.declaration.maximum, onBlur: field.onBlur }), _jsx(TimeSegments, { controlId: `${id}-time`, value: time, onChange: (next) => write(date, next), seconds: wantsSeconds(node), locale: locale, disabled: field.disabled || date === undefined, readOnly: field.readOnly, label: node.label, describedBy: timeDescribed === '' ? undefined : timeDescribed, invalid: field.a11y.invalid, required: field.a11y.required, onBlur: field.onBlur }), dateFirst === undefined ? null : (_jsx("p", { className: "ax-hint", id: dateFirstId, children: dateFirst }))] }));
|
|
2174
|
+
}
|
|
2175
|
+
/**
|
|
2176
|
+
* The currency mark and placement `formCopy` uses, read back from its own output.
|
|
2177
|
+
*
|
|
2178
|
+
* The mark table and `MONEY_MARK` live in the copy layer, and this adapter must not carry a
|
|
2179
|
+
* second copy of either: the summary and the entry would then be two chances to state the same
|
|
2180
|
+
* amount differently. Formatting a sentinel amount through `display` and removing the digits it
|
|
2181
|
+
* printed yields exactly the mark and gap the sentence uses, on the side it uses.
|
|
2182
|
+
*/
|
|
2183
|
+
function currencyAffix(copy, language, currency) {
|
|
2184
|
+
const sentinel = '123456789';
|
|
2185
|
+
const shown = copy.display({ minor: sentinel, currency, scale: 0 });
|
|
2186
|
+
if (shown === undefined)
|
|
2187
|
+
return undefined;
|
|
2188
|
+
const digits = groupCanonicalNumericText(sentinel, language, DEFAULT_DIGIT_POLICY);
|
|
2189
|
+
const at = shown.indexOf(digits);
|
|
2190
|
+
if (at === -1)
|
|
2191
|
+
return undefined;
|
|
2192
|
+
const before = shown.slice(0, at);
|
|
2193
|
+
const after = shown.slice(at + digits.length);
|
|
2194
|
+
if (before.trim() === '' && after.trim() === '')
|
|
2195
|
+
return undefined;
|
|
2196
|
+
return before.trim() === '' ? { side: 'after', mark: after } : { side: 'before', mark: before };
|
|
2197
|
+
}
|
|
2198
|
+
/**
|
|
2199
|
+
* Canonical numeric text in the locale's marks.
|
|
2200
|
+
*
|
|
2201
|
+
* The value is the caller's: a percentage or a quantity arrives as canonical decimal text, while
|
|
2202
|
+
* a scaled quantity may keep trailing zeros and is money text. Anything else has no display, so
|
|
2203
|
+
* the summary says nothing rather than formatting a value the record does not hold.
|
|
2204
|
+
*/
|
|
2205
|
+
function localeNumberText(text, language) {
|
|
2206
|
+
if (isDecimalString(text))
|
|
2207
|
+
return formatDecimal(text, language);
|
|
2208
|
+
if (isCanonicalMoneyText(text)) {
|
|
2209
|
+
return groupCanonicalNumericText(text, language, DEFAULT_DIGIT_POLICY);
|
|
2210
|
+
}
|
|
2211
|
+
return undefined;
|
|
2212
|
+
}
|
|
2213
|
+
/** A text entry whose raw keystrokes are not the value, `[MODEL-7.1.1]`. */
|
|
2214
|
+
/** A declared bound as an integer, when it is one; a decimal bound is ignored by the stepper. */
|
|
2215
|
+
function integerBoundOf(value) {
|
|
2216
|
+
if (typeof value === 'number')
|
|
2217
|
+
return Number.isInteger(value) ? value : undefined;
|
|
2218
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
2219
|
+
return undefined;
|
|
2220
|
+
const parsed = Number(value);
|
|
2221
|
+
return Number.isInteger(parsed) ? parsed : undefined;
|
|
2222
|
+
}
|
|
2223
|
+
/** The declared step as a positive integer; an absent or fractional step steps by one. */
|
|
2224
|
+
function integerStepOf(node) {
|
|
2225
|
+
const parsed = integerBoundOf(node.declaration.step);
|
|
2226
|
+
return parsed === undefined || parsed <= 0 ? 1 : parsed;
|
|
2227
|
+
}
|
|
2228
|
+
/** The committed value as an integer, when the wire holds one. */
|
|
2229
|
+
function integerOf(value) {
|
|
2230
|
+
if (typeof value === 'number')
|
|
2231
|
+
return Number.isInteger(value) ? value : undefined;
|
|
2232
|
+
if (typeof value === 'string' && /^-?\d+$/u.test(value.trim())) {
|
|
2233
|
+
const parsed = Number(value);
|
|
2234
|
+
return Number.isInteger(parsed) ? parsed : undefined;
|
|
2235
|
+
}
|
|
2236
|
+
return undefined;
|
|
2237
|
+
}
|
|
2238
|
+
/**
|
|
2239
|
+
* The value one step would write, or `undefined` when the step would leave the declared range.
|
|
2240
|
+
*
|
|
2241
|
+
* Refusing rather than clamping is deliberate: a reader at the maximum who presses `+` should
|
|
2242
|
+
* see the button disabled and the value unchanged, not a silent write that looks like a step.
|
|
2243
|
+
* An empty entry steps from the minimum when one is declared, so the first press lands inside
|
|
2244
|
+
* the range rather than at a value the artifact forbids.
|
|
2245
|
+
*/
|
|
2246
|
+
function steppedInteger(value, delta, minimum, maximum) {
|
|
2247
|
+
const current = integerOf(value) ?? minimum ?? 0;
|
|
2248
|
+
const next = current + delta;
|
|
2249
|
+
if (minimum !== undefined && next < minimum)
|
|
2250
|
+
return undefined;
|
|
2251
|
+
if (maximum !== undefined && next > maximum)
|
|
2252
|
+
return undefined;
|
|
2253
|
+
return next;
|
|
2254
|
+
}
|
|
2255
|
+
/** Whether the declared step can move an integer stepper: absent, or a positive whole number. */
|
|
2256
|
+
function integerStepUsable(step) {
|
|
2257
|
+
if (step === undefined)
|
|
2258
|
+
return true;
|
|
2259
|
+
const parsed = integerBoundOf(step);
|
|
2260
|
+
return parsed !== undefined && parsed > 0;
|
|
2261
|
+
}
|
|
2262
|
+
/**
|
|
2263
|
+
* The bounds sentence an integer entry states, or `undefined` when the declaration states none.
|
|
2264
|
+
*
|
|
2265
|
+
* The engine stays the only decider of a refused value: this states the declared bounds in
|
|
2266
|
+
* words and on the input's own `min` and `max`, so the stepper, the attributes and the words
|
|
2267
|
+
* agree before any value is written.
|
|
2268
|
+
*/
|
|
2269
|
+
function integerBoundsOf(minimum, maximum) {
|
|
2270
|
+
if (minimum !== undefined && maximum !== undefined) {
|
|
2271
|
+
return { reason: 'buffer.integer.range', params: { minimum, maximum } };
|
|
2272
|
+
}
|
|
2273
|
+
if (minimum !== undefined) {
|
|
2274
|
+
return { reason: 'buffer.integer.minimum', params: { minimum } };
|
|
2275
|
+
}
|
|
2276
|
+
if (maximum !== undefined) {
|
|
2277
|
+
return { reason: 'buffer.integer.maximum', params: { maximum } };
|
|
2278
|
+
}
|
|
2279
|
+
return undefined;
|
|
2280
|
+
}
|
|
2281
|
+
/**
|
|
2282
|
+
* A whole-number refusal restated for a percentage, which names the percent instead.
|
|
2283
|
+
*
|
|
2284
|
+
* Only the three reasons that talk about whole numbers are restated; a neutral decimal reason
|
|
2285
|
+
* passes through unchanged.
|
|
2286
|
+
*/
|
|
2287
|
+
function percentageReasonOf(reason) {
|
|
2288
|
+
if (reason === 'buffer.integer.digits')
|
|
2289
|
+
return 'buffer.percentage.digits';
|
|
2290
|
+
if (reason === 'buffer.integer.no_point')
|
|
2291
|
+
return 'buffer.percentage.no_point';
|
|
2292
|
+
if (reason === 'buffer.integer.not_safe')
|
|
2293
|
+
return 'buffer.percentage.not_safe';
|
|
2294
|
+
return reason;
|
|
2295
|
+
}
|
|
2296
|
+
/** An over-scale refusal restated for a quantity, whose parser key names the money family. */
|
|
2297
|
+
function quantityReasonOf(reason) {
|
|
2298
|
+
if (reason === 'buffer.money.scale_exceeded')
|
|
2299
|
+
return 'buffer.quantity.scale_exceeded';
|
|
2300
|
+
return reason;
|
|
2301
|
+
}
|
|
2302
|
+
function BufferedEntry({ node, field, id, rowKey, describedBy, format, }) {
|
|
2303
|
+
const locale = useLocale();
|
|
2304
|
+
const language = resolveLocale(locale).language;
|
|
2305
|
+
const buffer = useBufferedInput(node.fieldId, format, rowKey, locale === undefined ? undefined : { locale });
|
|
2306
|
+
const copy = useHostCopy();
|
|
2307
|
+
const currency = format.kind === 'money' ? node.declaration.currency : undefined;
|
|
2308
|
+
// The value and the sign are the host's copy. The marker and its side come from the same copy
|
|
2309
|
+
// when its display exposes placement. A custom display that does not is still paired with the
|
|
2310
|
+
// packaged marker rather than leaving the amount unidentified; the value itself stays the
|
|
2311
|
+
// host's, never the packaged one.
|
|
2312
|
+
const affix = currency === undefined
|
|
2313
|
+
? undefined
|
|
2314
|
+
: (currencyAffix(copy, language, currency) ??
|
|
2315
|
+
currencyAffix(formCopy(language), language, currency));
|
|
2316
|
+
const percentage = node.control === 'percentage';
|
|
2317
|
+
// The summary states the committed amount, never the half-typed entry: a group mark or a bare
|
|
2318
|
+
// `12.` is an entry the record does not hold yet, and formatting it would invent a value.
|
|
2319
|
+
// A money value is the wire record, so the same `display` the read-only control and the demo
|
|
2320
|
+
// use is the only formatter: one amount cannot be grouped one way here and another way there.
|
|
2321
|
+
const committedText = buffer.dirty || format.kind === 'money'
|
|
2322
|
+
? undefined
|
|
2323
|
+
: percentage && (typeof field.value === 'string' || typeof field.value === 'number')
|
|
2324
|
+
? String(field.value)
|
|
2325
|
+
: undefined;
|
|
2326
|
+
const amountText = committedText === undefined ? undefined : localeNumberText(committedText, language);
|
|
2327
|
+
// The domain a bare number does not state: the currency for money, the sign for a percentage.
|
|
2328
|
+
// The entry keeps the canonical text; these are the visible and announced words beside it.
|
|
2329
|
+
const display = buffer.dirty
|
|
2330
|
+
? undefined
|
|
2331
|
+
: format.kind === 'money'
|
|
2332
|
+
? field.value === undefined || field.value === null
|
|
2333
|
+
? undefined
|
|
2334
|
+
: copy.display(field.value)
|
|
2335
|
+
: percentage
|
|
2336
|
+
? `${amountText ?? committedText}${copy.percentageSign()}`
|
|
2337
|
+
: undefined;
|
|
2338
|
+
// The mark and its side are the copy layer's, read once through `currencyAffix`; the span that
|
|
2339
|
+
// carries it is named so a reader who lands on the entry hears which currency it is in.
|
|
2340
|
+
const prefix = affix?.side === 'before' ? affix.mark : undefined;
|
|
2341
|
+
const suffix = affix?.side === 'after' ? affix.mark : undefined;
|
|
2342
|
+
const domainId = affix === undefined ? (percentage ? `${id}-unit` : undefined) : `${id}-currency`;
|
|
2343
|
+
const displayId = `${id}-display`;
|
|
2344
|
+
// A refusal is about this control, so it is described by it: an invalid input whose description
|
|
2345
|
+
// omits the reason it is invalid leaves a reader who returns to the field without the answer.
|
|
2346
|
+
const refusalId = `${id}-refusal`;
|
|
2347
|
+
// The buffer writes through `field.setValue` itself, so a session refusal is caught here and
|
|
2348
|
+
// painted as this control's second failure rather than escaping the keystroke handler.
|
|
2349
|
+
const [writeRefusal, setWriteRefusal] = useState(undefined);
|
|
2350
|
+
const writeRefusalId = `${id}-write-refusal`;
|
|
2351
|
+
const attempt = (run) => {
|
|
2352
|
+
setWriteRefusal(absorbWrite(run).kind === 'accepted' ? undefined : copy.writeRefused());
|
|
2353
|
+
};
|
|
2354
|
+
// A whole number is a quantity a reader adjusts as often as types: the stepper is the mouse and
|
|
2355
|
+
// arrow-key path, while the input keeps the buffered parse contract untouched.
|
|
2356
|
+
const integer = format.kind === 'integer';
|
|
2357
|
+
const step = integerStepOf(node);
|
|
2358
|
+
const minimum = integerBoundOf(node.declaration.minimum);
|
|
2359
|
+
const maximum = integerBoundOf(node.declaration.maximum);
|
|
2360
|
+
// A declared step the stepper cannot honour stays stated instead of stepping by one: the
|
|
2361
|
+
// stepper is disabled, the declaration is worded, and typing still writes exact values the
|
|
2362
|
+
// engine alone decides on.
|
|
2363
|
+
const stepUsable = integerStepUsable(node.declaration.step);
|
|
2364
|
+
const stepId = `${id}-step`;
|
|
2365
|
+
const stepSentence = integer && !stepUsable && node.declaration.step !== undefined
|
|
2366
|
+
? copy.buffer('buffer.integer.step', { step: String(node.declaration.step) })
|
|
2367
|
+
: undefined;
|
|
2368
|
+
// The declared bounds the stepper respects, stated in words and on the input itself.
|
|
2369
|
+
const bounds = integer ? integerBoundsOf(minimum, maximum) : undefined;
|
|
2370
|
+
const boundsText = bounds === undefined ? undefined : copy.buffer(bounds.reason, bounds.params);
|
|
2371
|
+
const boundsId = `${id}-bounds`;
|
|
2372
|
+
// A percentage names the percent in a whole-number refusal; the params travel unchanged.
|
|
2373
|
+
const shownParseError = percentage && integer && buffer.parseError !== undefined
|
|
2374
|
+
? percentageReasonOf(buffer.parseError)
|
|
2375
|
+
: buffer.parseError;
|
|
2376
|
+
const described = [
|
|
2377
|
+
describedBy,
|
|
2378
|
+
shownParseError === undefined ? undefined : refusalId,
|
|
2379
|
+
writeRefusal === undefined ? undefined : writeRefusalId,
|
|
2380
|
+
boundsText === undefined ? undefined : boundsId,
|
|
2381
|
+
stepSentence === undefined ? undefined : stepId,
|
|
2382
|
+
domainId,
|
|
2383
|
+
display === undefined ? undefined : displayId,
|
|
2384
|
+
]
|
|
2385
|
+
.filter((entry) => entry !== undefined)
|
|
2386
|
+
.join(' ');
|
|
2387
|
+
// A dirty buffer is an entry the record does not hold yet: a half-typed `12.` or a refused
|
|
2388
|
+
// write. Disabling the stepper while dirty prevents an enabled handle from silently no-oping or
|
|
2389
|
+
// discarding the reader's draft before it commits.
|
|
2390
|
+
const stepperDisabled = field.disabled || field.readOnly || !stepUsable || buffer.dirty;
|
|
2391
|
+
const stepTo = (delta) => {
|
|
2392
|
+
if (stepperDisabled)
|
|
2393
|
+
return;
|
|
2394
|
+
const next = steppedInteger(field.value, delta, minimum, maximum);
|
|
2395
|
+
if (next !== undefined)
|
|
2396
|
+
attempt(() => buffer.setText(String(next)));
|
|
2397
|
+
};
|
|
2398
|
+
const blur = () => {
|
|
2399
|
+
const result = absorbWrite(() => buffer.onBlur());
|
|
2400
|
+
if (result.kind === 'accepted') {
|
|
2401
|
+
setWriteRefusal(undefined);
|
|
2402
|
+
return;
|
|
2403
|
+
}
|
|
2404
|
+
setWriteRefusal(copy.writeRefused());
|
|
2405
|
+
// The buffer's handler stopped before recording the visit; a refused commit is still the
|
|
2406
|
+
// reader leaving the field, so the host's reveal rule must still see it.
|
|
2407
|
+
field.onBlur();
|
|
2408
|
+
};
|
|
2409
|
+
const nextDown = integer && stepUsable ? steppedInteger(field.value, -step, minimum, maximum) : undefined;
|
|
2410
|
+
const nextUp = integer && stepUsable ? steppedInteger(field.value, step, minimum, maximum) : undefined;
|
|
2411
|
+
const input = (_jsx("input", { id: id, className: controlClassName(node.control), type: node.hints.web.type, inputMode: node.hints.web.inputMode, autoComplete: node.hints.web.autoComplete, autoCorrect: node.hints.web.autoCorrect, autoCapitalize: node.hints.web.autoCapitalize, placeholder: node.placeholder, "aria-required": field.a11y.required, "aria-invalid": field.a11y.invalid || buffer.parseKind === 'invalid' || writeRefusal !== undefined, "aria-describedby": described, "aria-busy": field.a11y.busy ? true : undefined, disabled: field.disabled, readOnly: field.readOnly, value: buffer.text, onChange: (event) => attempt(() => buffer.setText(event.target.value)), ...(!integer || minimum === undefined ? {} : { min: String(minimum) }), ...(!integer || maximum === undefined ? {} : { max: String(maximum) }),
|
|
2412
|
+
// Escape forgets the half-typed or refused entry and shows the committed value again;
|
|
2413
|
+
// focus stays in the entry and the refusal pattern is unchanged. Arrow keys step a whole
|
|
2414
|
+
// number and never scroll the page: they run only for an integer entry with a usable
|
|
2415
|
+
// step, and `preventDefault` is what keeps a focused input from moving the viewport.
|
|
2416
|
+
onKeyDown: (event) => {
|
|
2417
|
+
if (event.key === 'Escape') {
|
|
2418
|
+
buffer.reset();
|
|
2419
|
+
return;
|
|
2420
|
+
}
|
|
2421
|
+
if (!integer || !stepUsable)
|
|
2422
|
+
return;
|
|
2423
|
+
if (event.key !== 'ArrowUp' && event.key !== 'ArrowDown')
|
|
2424
|
+
return;
|
|
2425
|
+
event.preventDefault();
|
|
2426
|
+
stepTo(event.key === 'ArrowUp' ? step : -step);
|
|
2427
|
+
},
|
|
2428
|
+
// The buffer's own blur commits the entry and records the visit; `blur` keeps that contract
|
|
2429
|
+
// and only adds the refusal it would otherwise have thrown out of the handler.
|
|
2430
|
+
onBlur: blur }));
|
|
2431
|
+
return (_jsxs(_Fragment, { children: [integer ? (_jsxs("span", { className: "ax-stepper", "data-stepper": "", children: [_jsx("button", { type: "button", className: "ax-stepper__button", "aria-label": copy.integerStep(node.label, 'decrease'), "aria-controls": id, disabled: stepperDisabled || nextDown === undefined, onClick: () => stepTo(-step), children: "\u2212" }), input, _jsx("button", { type: "button", className: "ax-stepper__button", "aria-label": copy.integerStep(node.label, 'increase'), "aria-controls": id, disabled: stepperDisabled || nextUp === undefined, onClick: () => stepTo(step), children: "+" })] })) : prefix === undefined && suffix === undefined && !percentage ? (input) : (_jsxs("span", { className: percentage ? 'ax-percentage' : 'ax-money', ...(suffix === undefined
|
|
2432
|
+
? {}
|
|
2433
|
+
: { style: { gridTemplateColumns: 'minmax(0, 1fr) auto' } }), children: [prefix === undefined ? null : (_jsx("span", { className: "ax-money-prefix", id: `${id}-currency`, children: prefix })), input, suffix === undefined ? null : (_jsx("span", { className: "ax-money-prefix", id: `${id}-currency`, style: { borderInlineStart: 0, borderInlineEnd: '1px solid var(--ax-border)' }, children: suffix })), percentage ? (_jsx("span", { className: "ax-control-suffix", id: `${id}-unit`, children: copy.percentageSign() })) : null] })), display === undefined ? null : (_jsx("p", { className: "ax-hint", id: displayId, "data-value-display": "", children: display })), boundsText === undefined ? null : (_jsx("p", { className: "ax-hint", id: boundsId, children: boundsText })), stepSentence === undefined ? null : (_jsx("p", { className: "ax-hint", id: stepId, children: stepSentence })), shownParseError === undefined ? null : (_jsx("p", { className: "ax-error", role: "alert", id: refusalId, "data-buffer": "refused", children: copy.buffer(shownParseError, buffer.parseErrorParams) })), writeRefusal === undefined ? null : (_jsx("p", { className: "ax-error", role: "alert", id: writeRefusalId, "data-write": "refused", children: writeRefusal }))] }));
|
|
2434
|
+
}
|
|
2435
|
+
/** The amount member of a committed quantity, when the value carries one. */
|
|
2436
|
+
function quantityAmountOf(value) {
|
|
2437
|
+
if (!isRecord(value))
|
|
2438
|
+
return undefined;
|
|
2439
|
+
const amount = value['value'];
|
|
2440
|
+
return typeof amount === 'string' ? amount : undefined;
|
|
2441
|
+
}
|
|
2442
|
+
/** The unit member of a committed quantity, when the value carries one. */
|
|
2443
|
+
function quantityUnitOf(value) {
|
|
2444
|
+
if (!isRecord(value))
|
|
2445
|
+
return undefined;
|
|
2446
|
+
const unit = value['unit'];
|
|
2447
|
+
return typeof unit === 'string' ? unit : undefined;
|
|
2448
|
+
}
|
|
2449
|
+
/**
|
|
2450
|
+
* A measured quantity: an exact amount and the unit the declaration admits, `MODEL-2.1.21`.
|
|
2451
|
+
*
|
|
2452
|
+
* The text entry is buffered like every decimal, so `12.` is kept as an entry and only a
|
|
2453
|
+
* complete amount is written; the unit picker supplies the value's second member, so a write is
|
|
2454
|
+
* always `{ value, unit }` and never a bare number the wire would refuse. A declaration with
|
|
2455
|
+
* scale zero is an integer quantity, so it uses a whole-number keypad and a stepper, and its
|
|
2456
|
+
* parse refuses a decimal point the declaration could not hold.
|
|
2457
|
+
*/
|
|
2458
|
+
function QuantityEntry({ node, field, id, rowKey, describedBy, }) {
|
|
2459
|
+
const locale = useLocale();
|
|
2460
|
+
const language = resolveLocale(locale).language;
|
|
2461
|
+
const copy = useHostCopy();
|
|
2462
|
+
const units = node.declaration.units ?? [];
|
|
2463
|
+
const committedUnit = quantityUnitOf(field.value);
|
|
2464
|
+
const [chosenUnit, setChosenUnit] = useState(undefined);
|
|
2465
|
+
const unit = chosenUnit !== undefined && units.includes(chosenUnit)
|
|
2466
|
+
? chosenUnit
|
|
2467
|
+
: committedUnit !== undefined && units.includes(committedUnit)
|
|
2468
|
+
? committedUnit
|
|
2469
|
+
: (units[0] ?? '');
|
|
2470
|
+
const integer = (node.declaration.scale ?? 0) === 0;
|
|
2471
|
+
const buffer = useBufferedInput(node.fieldId, { kind: 'quantity', unit }, rowKey, locale === undefined ? undefined : { locale });
|
|
2472
|
+
const refusalId = `${id}-refusal`;
|
|
2473
|
+
// Same contract as `BufferedEntry`: the buffer writes through the binding itself, so a session
|
|
2474
|
+
// refusal is caught and painted here instead of escaping the amount entry or its blur.
|
|
2475
|
+
const [writeRefusal, setWriteRefusal] = useState(undefined);
|
|
2476
|
+
const writeRefusalId = `${id}-write-refusal`;
|
|
2477
|
+
const attempt = (run) => {
|
|
2478
|
+
setWriteRefusal(absorbWrite(run).kind === 'accepted' ? undefined : copy.writeRefused());
|
|
2479
|
+
};
|
|
2480
|
+
const committedAmount = buffer.dirty ? undefined : quantityAmountOf(field.value);
|
|
2481
|
+
const amountText = committedAmount === undefined ? undefined : localeNumberText(committedAmount, language);
|
|
2482
|
+
const display = amountText === undefined ? undefined : `${amountText} ${unit}`;
|
|
2483
|
+
const displayId = `${id}-display`;
|
|
2484
|
+
// An over-scale quantity refuses under its own key; the money key would misname the family.
|
|
2485
|
+
const shownParseError = buffer.parseError === undefined ? undefined : quantityReasonOf(buffer.parseError);
|
|
2486
|
+
// The summary carries the unit, so a reader who cannot see the picker still hears the measure,
|
|
2487
|
+
// and the failure and the help text are both still named.
|
|
2488
|
+
const described = [
|
|
2489
|
+
describedBy,
|
|
2490
|
+
shownParseError === undefined ? undefined : refusalId,
|
|
2491
|
+
writeRefusal === undefined ? undefined : writeRefusalId,
|
|
2492
|
+
display === undefined ? undefined : displayId,
|
|
2493
|
+
]
|
|
2494
|
+
.filter((entry) => entry !== undefined)
|
|
2495
|
+
.join(' ');
|
|
2496
|
+
const changeUnit = (next) => {
|
|
2497
|
+
setChosenUnit(next);
|
|
2498
|
+
const amount = quantityAmountOf(field.value);
|
|
2499
|
+
// With no committed amount the picker only chooses what the next keystroke will write.
|
|
2500
|
+
if (amount === undefined)
|
|
2501
|
+
return;
|
|
2502
|
+
writeTo(field, { value: amount, unit: next });
|
|
2503
|
+
};
|
|
2504
|
+
const step = (delta) => {
|
|
2505
|
+
const amount = quantityAmountOf(field.value);
|
|
2506
|
+
if (amount === undefined)
|
|
2507
|
+
return;
|
|
2508
|
+
const current = Number(amount);
|
|
2509
|
+
const next = current + delta;
|
|
2510
|
+
if (!Number.isSafeInteger(current) || !Number.isSafeInteger(next))
|
|
2511
|
+
return;
|
|
2512
|
+
writeTo(field, { value: String(next), unit });
|
|
2513
|
+
};
|
|
2514
|
+
return (_jsxs(_Fragment, { children: [_jsxs("div", { className: "ax-quantity", children: [integer ? (_jsx("button", { type: "button", className: "ax-quantity-step", "aria-label": copy.quantityStep(node.label, 'decrease'), disabled: field.disabled || !field.enabled, onClick: () => step(-1), children: "\u2212" })) : null, _jsx("input", { id: id, className: controlClassName(node.control), type: node.hints.web.type, inputMode: integer ? 'numeric' : node.hints.web.inputMode, autoComplete: node.hints.web.autoComplete, placeholder: node.placeholder, "aria-label": copy.quantityAmount(node.label), "aria-required": field.a11y.required, "aria-invalid": field.a11y.invalid || buffer.parseKind === 'invalid' || writeRefusal !== undefined, "aria-describedby": described, "aria-busy": field.a11y.busy ? true : undefined, disabled: field.disabled, readOnly: field.readOnly, value: buffer.text, onChange: (event) => attempt(() => buffer.setText(event.target.value)), onBlur: () => {
|
|
2515
|
+
const result = absorbWrite(() => buffer.onBlur());
|
|
2516
|
+
if (result.kind === 'accepted') {
|
|
2517
|
+
setWriteRefusal(undefined);
|
|
2518
|
+
return;
|
|
2519
|
+
}
|
|
2520
|
+
setWriteRefusal(copy.writeRefused());
|
|
2521
|
+
field.onBlur();
|
|
2522
|
+
} }), integer ? (_jsx("button", { type: "button", className: "ax-quantity-step", "aria-label": copy.quantityStep(node.label, 'increase'), disabled: field.disabled || !field.enabled, onClick: () => step(1), children: "+" })) : null, _jsx("select", { className: "ax-control ax-control--quantity-unit", "aria-label": copy.quantityUnit(node.label), "aria-describedby": described, disabled: field.disabled || !field.enabled, value: unit, onChange: (event) => changeUnit(event.target.value), children: units.map((entry) => (_jsx("option", { value: entry, children: entry }, entry))) })] }), display === undefined ? null : (_jsx("p", { className: "ax-hint", id: displayId, "data-value-display": "", children: display })), shownParseError === undefined ? null : (_jsx("p", { className: "ax-error", role: "alert", id: refusalId, "data-buffer": "refused", children: copy.buffer(shownParseError, buffer.parseErrorParams) })), writeRefusal === undefined ? null : (_jsx("p", { className: "ax-error", role: "alert", id: writeRefusalId, "data-write": "refused", children: writeRefusal }))] }));
|
|
2523
|
+
}
|
|
2524
|
+
//# sourceMappingURL=controls.js.map
|