@opsinjs/react 0.0.0-stage → 0.1.0
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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +114 -2
- package/dist/components/accordion.d.ts +82 -0
- package/dist/components/accordion.d.ts.map +1 -0
- package/dist/components/accordion.js +154 -0
- package/dist/components/accordion.js.map +1 -0
- package/dist/components/alert-banner.d.ts +291 -0
- package/dist/components/alert-banner.d.ts.map +1 -0
- package/dist/components/alert-banner.js +529 -0
- package/dist/components/alert-banner.js.map +1 -0
- package/dist/components/avatar.d.ts +45 -0
- package/dist/components/avatar.d.ts.map +1 -0
- package/dist/components/avatar.js +159 -0
- package/dist/components/avatar.js.map +1 -0
- package/dist/components/badge.d.ts +78 -0
- package/dist/components/badge.d.ts.map +1 -0
- package/dist/components/badge.js +32 -0
- package/dist/components/badge.js.map +1 -0
- package/dist/components/body-map.d.ts +51 -0
- package/dist/components/body-map.d.ts.map +1 -0
- package/dist/components/body-map.js +190 -0
- package/dist/components/body-map.js.map +1 -0
- package/dist/components/button.d.ts +260 -0
- package/dist/components/button.d.ts.map +1 -0
- package/dist/components/button.js +609 -0
- package/dist/components/button.js.map +1 -0
- package/dist/components/callout.d.ts +99 -0
- package/dist/components/callout.d.ts.map +1 -0
- package/dist/components/callout.js +285 -0
- package/dist/components/callout.js.map +1 -0
- package/dist/components/card.d.ts +156 -0
- package/dist/components/card.d.ts.map +1 -0
- package/dist/components/card.js +396 -0
- package/dist/components/card.js.map +1 -0
- package/dist/components/care-card.d.ts +182 -0
- package/dist/components/care-card.d.ts.map +1 -0
- package/dist/components/care-card.js +544 -0
- package/dist/components/care-card.js.map +1 -0
- package/dist/components/checkbox.d.ts +58 -0
- package/dist/components/checkbox.d.ts.map +1 -0
- package/dist/components/checkbox.js +167 -0
- package/dist/components/checkbox.js.map +1 -0
- package/dist/components/combobox.d.ts +71 -0
- package/dist/components/combobox.d.ts.map +1 -0
- package/dist/components/combobox.js +204 -0
- package/dist/components/combobox.js.map +1 -0
- package/dist/components/consent-sheet.d.ts +186 -0
- package/dist/components/consent-sheet.d.ts.map +1 -0
- package/dist/components/consent-sheet.js +452 -0
- package/dist/components/consent-sheet.js.map +1 -0
- package/dist/components/dialog.d.ts +108 -0
- package/dist/components/dialog.d.ts.map +1 -0
- package/dist/components/dialog.js +573 -0
- package/dist/components/dialog.js.map +1 -0
- package/dist/components/disclaimer-note.d.ts +144 -0
- package/dist/components/disclaimer-note.d.ts.map +1 -0
- package/dist/components/disclaimer-note.js +486 -0
- package/dist/components/disclaimer-note.js.map +1 -0
- package/dist/components/divider.d.ts +76 -0
- package/dist/components/divider.d.ts.map +1 -0
- package/dist/components/divider.js +101 -0
- package/dist/components/divider.js.map +1 -0
- package/dist/components/dose-tracker.d.ts +137 -0
- package/dist/components/dose-tracker.d.ts.map +1 -0
- package/dist/components/dose-tracker.js +154 -0
- package/dist/components/dose-tracker.js.map +1 -0
- package/dist/components/empty-state.d.ts +171 -0
- package/dist/components/empty-state.d.ts.map +1 -0
- package/dist/components/empty-state.js +387 -0
- package/dist/components/empty-state.js.map +1 -0
- package/dist/components/field.d.ts +195 -0
- package/dist/components/field.d.ts.map +1 -0
- package/dist/components/field.js +282 -0
- package/dist/components/field.js.map +1 -0
- package/dist/components/goal-ring.d.ts +114 -0
- package/dist/components/goal-ring.d.ts.map +1 -0
- package/dist/components/goal-ring.js +254 -0
- package/dist/components/goal-ring.js.map +1 -0
- package/dist/components/icon-button.d.ts +124 -0
- package/dist/components/icon-button.d.ts.map +1 -0
- package/dist/components/icon-button.js +113 -0
- package/dist/components/icon-button.js.map +1 -0
- package/dist/components/link.d.ts +122 -0
- package/dist/components/link.d.ts.map +1 -0
- package/dist/components/link.js +164 -0
- package/dist/components/link.js.map +1 -0
- package/dist/components/log-sheet.d.ts +259 -0
- package/dist/components/log-sheet.d.ts.map +1 -0
- package/dist/components/log-sheet.js +437 -0
- package/dist/components/log-sheet.js.map +1 -0
- package/dist/components/menu.d.ts +69 -0
- package/dist/components/menu.d.ts.map +1 -0
- package/dist/components/menu.js +190 -0
- package/dist/components/menu.js.map +1 -0
- package/dist/components/metric-tile.d.ts +301 -0
- package/dist/components/metric-tile.d.ts.map +1 -0
- package/dist/components/metric-tile.js +533 -0
- package/dist/components/metric-tile.js.map +1 -0
- package/dist/components/number-field.d.ts +62 -0
- package/dist/components/number-field.d.ts.map +1 -0
- package/dist/components/number-field.js +134 -0
- package/dist/components/number-field.js.map +1 -0
- package/dist/components/popover.d.ts +70 -0
- package/dist/components/popover.d.ts.map +1 -0
- package/dist/components/popover.js +174 -0
- package/dist/components/popover.js.map +1 -0
- package/dist/components/progress.d.ts +83 -0
- package/dist/components/progress.d.ts.map +1 -0
- package/dist/components/progress.js +140 -0
- package/dist/components/progress.js.map +1 -0
- package/dist/components/questionnaire.d.ts +123 -0
- package/dist/components/questionnaire.d.ts.map +1 -0
- package/dist/components/questionnaire.js +148 -0
- package/dist/components/questionnaire.js.map +1 -0
- package/dist/components/radio-group.d.ts +66 -0
- package/dist/components/radio-group.d.ts.map +1 -0
- package/dist/components/radio-group.js +186 -0
- package/dist/components/radio-group.js.map +1 -0
- package/dist/components/range-bar.d.ts +172 -0
- package/dist/components/range-bar.d.ts.map +1 -0
- package/dist/components/range-bar.js +578 -0
- package/dist/components/range-bar.js.map +1 -0
- package/dist/components/range-legend.d.ts +97 -0
- package/dist/components/range-legend.d.ts.map +1 -0
- package/dist/components/range-legend.js +166 -0
- package/dist/components/range-legend.js.map +1 -0
- package/dist/components/reading-input.d.ts +225 -0
- package/dist/components/reading-input.d.ts.map +1 -0
- package/dist/components/reading-input.js +651 -0
- package/dist/components/reading-input.js.map +1 -0
- package/dist/components/relative-time.d.ts +177 -0
- package/dist/components/relative-time.d.ts.map +1 -0
- package/dist/components/relative-time.js +603 -0
- package/dist/components/relative-time.js.map +1 -0
- package/dist/components/result-card.d.ts +319 -0
- package/dist/components/result-card.d.ts.map +1 -0
- package/dist/components/result-card.js +614 -0
- package/dist/components/result-card.js.map +1 -0
- package/dist/components/scale-input.d.ts +58 -0
- package/dist/components/scale-input.d.ts.map +1 -0
- package/dist/components/scale-input.js +170 -0
- package/dist/components/scale-input.js.map +1 -0
- package/dist/components/score-dial.d.ts +205 -0
- package/dist/components/score-dial.d.ts.map +1 -0
- package/dist/components/score-dial.js +728 -0
- package/dist/components/score-dial.js.map +1 -0
- package/dist/components/scroll-area.d.ts +38 -0
- package/dist/components/scroll-area.d.ts.map +1 -0
- package/dist/components/scroll-area.js +138 -0
- package/dist/components/scroll-area.js.map +1 -0
- package/dist/components/segmented-control.d.ts +69 -0
- package/dist/components/segmented-control.d.ts.map +1 -0
- package/dist/components/segmented-control.js +259 -0
- package/dist/components/segmented-control.js.map +1 -0
- package/dist/components/select.d.ts +72 -0
- package/dist/components/select.d.ts.map +1 -0
- package/dist/components/select.js +205 -0
- package/dist/components/select.js.map +1 -0
- package/dist/components/sheet.d.ts +192 -0
- package/dist/components/sheet.d.ts.map +1 -0
- package/dist/components/sheet.js +952 -0
- package/dist/components/sheet.js.map +1 -0
- package/dist/components/skeleton.d.ts +104 -0
- package/dist/components/skeleton.d.ts.map +1 -0
- package/dist/components/skeleton.js +266 -0
- package/dist/components/skeleton.js.map +1 -0
- package/dist/components/slider.d.ts +55 -0
- package/dist/components/slider.d.ts.map +1 -0
- package/dist/components/slider.js +170 -0
- package/dist/components/slider.js.map +1 -0
- package/dist/components/source-citation.d.ts +127 -0
- package/dist/components/source-citation.d.ts.map +1 -0
- package/dist/components/source-citation.js +391 -0
- package/dist/components/source-citation.js.map +1 -0
- package/dist/components/spinner.d.ts +86 -0
- package/dist/components/spinner.d.ts.map +1 -0
- package/dist/components/spinner.js +97 -0
- package/dist/components/spinner.js.map +1 -0
- package/dist/components/status-pill.d.ts +90 -0
- package/dist/components/status-pill.d.ts.map +1 -0
- package/dist/components/status-pill.js +259 -0
- package/dist/components/status-pill.js.map +1 -0
- package/dist/components/stepper.d.ts +99 -0
- package/dist/components/stepper.d.ts.map +1 -0
- package/dist/components/stepper.js +152 -0
- package/dist/components/stepper.js.map +1 -0
- package/dist/components/surface.d.ts +92 -0
- package/dist/components/surface.d.ts.map +1 -0
- package/dist/components/surface.js +261 -0
- package/dist/components/surface.js.map +1 -0
- package/dist/components/switch.d.ts +48 -0
- package/dist/components/switch.d.ts.map +1 -0
- package/dist/components/switch.js +152 -0
- package/dist/components/switch.js.map +1 -0
- package/dist/components/symptom-picker.d.ts +69 -0
- package/dist/components/symptom-picker.d.ts.map +1 -0
- package/dist/components/symptom-picker.js +170 -0
- package/dist/components/symptom-picker.js.map +1 -0
- package/dist/components/tab-bar.d.ts +110 -0
- package/dist/components/tab-bar.d.ts.map +1 -0
- package/dist/components/tab-bar.js +118 -0
- package/dist/components/tab-bar.js.map +1 -0
- package/dist/components/table.d.ts +117 -0
- package/dist/components/table.d.ts.map +1 -0
- package/dist/components/table.js +65 -0
- package/dist/components/table.js.map +1 -0
- package/dist/components/tabs.d.ts +106 -0
- package/dist/components/tabs.d.ts.map +1 -0
- package/dist/components/tabs.js +136 -0
- package/dist/components/tabs.js.map +1 -0
- package/dist/components/term.d.ts +243 -0
- package/dist/components/term.d.ts.map +1 -0
- package/dist/components/term.js +562 -0
- package/dist/components/term.js.map +1 -0
- package/dist/components/textarea.d.ts +157 -0
- package/dist/components/textarea.d.ts.map +1 -0
- package/dist/components/textarea.js +66 -0
- package/dist/components/textarea.js.map +1 -0
- package/dist/components/timeline-entry.d.ts +99 -0
- package/dist/components/timeline-entry.d.ts.map +1 -0
- package/dist/components/timeline-entry.js +47 -0
- package/dist/components/timeline-entry.js.map +1 -0
- package/dist/components/toast.d.ts +80 -0
- package/dist/components/toast.d.ts.map +1 -0
- package/dist/components/toast.js +266 -0
- package/dist/components/toast.js.map +1 -0
- package/dist/components/tooltip.d.ts +48 -0
- package/dist/components/tooltip.d.ts.map +1 -0
- package/dist/components/tooltip.js +159 -0
- package/dist/components/tooltip.js.map +1 -0
- package/dist/components/trend-sparkline.d.ts +188 -0
- package/dist/components/trend-sparkline.d.ts.map +1 -0
- package/dist/components/trend-sparkline.js +796 -0
- package/dist/components/trend-sparkline.js.map +1 -0
- package/dist/components/value.d.ts +150 -0
- package/dist/components/value.d.ts.map +1 -0
- package/dist/components/value.js +402 -0
- package/dist/components/value.js.map +1 -0
- package/dist/components/visually-hidden.d.ts +61 -0
- package/dist/components/visually-hidden.d.ts.map +1 -0
- package/dist/components/visually-hidden.js +172 -0
- package/dist/components/visually-hidden.js.map +1 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +72 -0
- package/dist/index.js.map +1 -0
- package/dist/opsinjs.d.ts +443 -0
- package/dist/opsinjs.d.ts.map +1 -0
- package/dist/opsinjs.js +906 -0
- package/dist/opsinjs.js.map +1 -0
- package/dist/status.d.ts +111 -0
- package/dist/status.d.ts.map +1 -0
- package/dist/status.js +125 -0
- package/dist/status.js.map +1 -0
- package/dist/styles.css +2 -0
- package/dist/utils.d.ts +20 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +82 -0
- package/dist/utils.js.map +1 -0
- package/package.json +86 -3
- package/styles/base.css +132 -0
- package/styles/roles.css +412 -0
- package/styles/standalone.css +20 -0
- package/styles/tailwind.css +18 -0
- package/styles/theme.css +309 -0
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
/**
|
|
4
|
+
* ReadingInput is a Field for typing a measurement, with the unit beside the
|
|
5
|
+
* number rather than hidden in the label.
|
|
6
|
+
*
|
|
7
|
+
* IT COMPOSES `Field` AND REIMPLEMENTS NOTHING. The label relationship, the
|
|
8
|
+
* description list, the invalid state and the 44px floor are Field's, which is
|
|
9
|
+
* Base UI's, and none of them is rewritten here. What this file adds is the
|
|
10
|
+
* three things a health measurement needs that a text input does not: the unit
|
|
11
|
+
* is on screen while the reader types, a compound reading is one named group of
|
|
12
|
+
* separately-typable parts, and every change reports which unit the number is in.
|
|
13
|
+
*
|
|
14
|
+
* THERE IS NO `plausible` PROP, AND ITS ABSENCE IS THE SPECIFICATION.
|
|
15
|
+
* `reading-input.mdx` proposed `{ min, max, unit }` and said the product owns
|
|
16
|
+
* the bounds. This file goes one step further and gives the product the
|
|
17
|
+
* COMPARISON as well, for four reasons that are each on their own sufficient:
|
|
18
|
+
*
|
|
19
|
+
* 1. A bound this component holds is a bound this component has to compare
|
|
20
|
+
* against, and that comparison is four clinical decisions nobody has made:
|
|
21
|
+
* whether the ends are inclusive, what happens to each part of a compound
|
|
22
|
+
* reading, which unit the bounds are stated in once the reader has switched
|
|
23
|
+
* units, and what counts as "outside" for a value the reader is halfway
|
|
24
|
+
* through typing.
|
|
25
|
+
* 2. §7 of the substrate contract forbids shipping such a bound "for any
|
|
26
|
+
* metric, in any population, ever… This applies to example data too", and
|
|
27
|
+
* `check-a11y.mts` enforces it with no exemption of any kind for a name
|
|
28
|
+
* matching `plausib`. So the prop could never appear in this file's demo,
|
|
29
|
+
* in any example, or in any preview. It would be a feature nobody could
|
|
30
|
+
* ever see working.
|
|
31
|
+
* 3. The specification's own hint copy had to be corrected once for exactly
|
|
32
|
+
* this reason: it recommended a hint of "for example 128", which is a
|
|
33
|
+
* plausible systolic reading rendered as guidance by a system with no
|
|
34
|
+
* clinical owner.
|
|
35
|
+
* 4. What the reader actually needs is a SENTENCE, not a bound, and only
|
|
36
|
+
* the product can write it. An example is "that is higher than most
|
|
37
|
+
* readings, did you mean 128?". `warning` takes that sentence. The
|
|
38
|
+
* product compares; this file renders, associates it with the control,
|
|
39
|
+
* and refuses to make it an error.
|
|
40
|
+
*
|
|
41
|
+
* A value nobody has told this component is impossible is a value it accepts and
|
|
42
|
+
* passes on, unchanged. There is no bound anywhere in this file, and nothing is
|
|
43
|
+
* ever rejected, clamped, cleared or reordered on the way through.
|
|
44
|
+
*
|
|
45
|
+
* UNITS: WHAT IT CONVERTS, AND THE ONE IT REFUSES BY NAME.
|
|
46
|
+
* The switch offers whatever `units` lists, and on a switch it asks
|
|
47
|
+
* `convertUnit()`. That function is generated from `tokens/units.json` and
|
|
48
|
+
* carries only conversions that are true by DEFINITION. kg↔lb↔st is the 1959
|
|
49
|
+
* international pound; °C↔°F is the definition of the Fahrenheit scale.
|
|
50
|
+
* mmol/L↔mg/dL is not there and will not be: the factor is the molar mass of
|
|
51
|
+
* the substance being measured, and this component does not know what was
|
|
52
|
+
* measured. ADR 0016.
|
|
53
|
+
*
|
|
54
|
+
* So a switch this system cannot make is a switch that CLEARS the entry and says
|
|
55
|
+
* so on screen, rather than relabelling the digits. Relabelling 5.2 from mmol/L
|
|
56
|
+
* to mg/dL is an eighteen-fold error that looks entirely reasonable.
|
|
57
|
+
* A product that needs that switch supplies the arithmetic itself: it is
|
|
58
|
+
* controlled, it receives `cause: "unit"` with the unit the reader asked for, and
|
|
59
|
+
* it re-renders with its own converted value. Or there is no switch.
|
|
60
|
+
*
|
|
61
|
+
* IT HOLDS NO VALUE, AND IT IS CONTROLLED. `value`/`segments` and `unit` are the
|
|
62
|
+
* caller's; every keystroke and every switch goes out through `onChange` and
|
|
63
|
+
* comes back as props. The one piece of state here is the TEXT in each box,
|
|
64
|
+
* which is not the same thing as the value: a reader typing "5." has typed
|
|
65
|
+
* something that parses to 5, and re-rendering "5" underneath their cursor takes
|
|
66
|
+
* the full stop away again. The buffer is re-synchronised from props the moment
|
|
67
|
+
* the numbers it last reported stop matching the numbers it is given.
|
|
68
|
+
*
|
|
69
|
+
* WHY THE VALUE IS IN THE DISPLAYED UNIT AND NOT IN A CANONICAL ONE. The
|
|
70
|
+
* specification had `value` canonical with the display converted, which means
|
|
71
|
+
* every keystroke round-trips display → canonical → display. `unit-systems` rule
|
|
72
|
+
* 6 says a round trip is lossy and to round after conversion, so that design
|
|
73
|
+
* visibly mangles digits as they are typed. Here nothing round-trips: what the
|
|
74
|
+
* caller passes is what is shown, in the unit the caller names. Storing it
|
|
75
|
+
* canonically is the product's job, which is where the knowledge of what
|
|
76
|
+
* canonical means for this measurement already lives.
|
|
77
|
+
*
|
|
78
|
+
* NO COLOUR FROM EITHER AXIS, WHILE ANYBODY IS TYPING. A field that turns amber
|
|
79
|
+
* because a number is outside a range is delivering a verdict during data entry,
|
|
80
|
+
* before anyone has checked whether the number is even right. There is no
|
|
81
|
+
* `data-status` in this file, no status class, no category class, and the
|
|
82
|
+
* warning is a paragraph in the ordinary foreground colour.
|
|
83
|
+
*
|
|
84
|
+
* IT IS A CLIENT COMPONENT, and it has to be. It holds the text buffer, it
|
|
85
|
+
* handles change events, it mints ids with `useId`, and `Field.Control` is a
|
|
86
|
+
* Base UI part that carries its own `'use client'` anyway.
|
|
87
|
+
*/
|
|
88
|
+
import { useId, useState } from "react";
|
|
89
|
+
import { ChevronDown } from "lucide-react";
|
|
90
|
+
import { convertUnit, findUnit, isDevelopment, REFUSED_CONVERSIONS, } from "../opsinjs.js";
|
|
91
|
+
import { cn } from "../utils.js";
|
|
92
|
+
import { Field } from "./field.js";
|
|
93
|
+
/**
|
|
94
|
+
* Once per cause, not once per render. This is the same policy `field.tsx`
|
|
95
|
+
* implements and for the same reason, doubled here because this is a controlled
|
|
96
|
+
* input inside somebody's form. A complaint printed on every keystroke, twice
|
|
97
|
+
* under Strict Mode, buries whatever else the console had to say.
|
|
98
|
+
*
|
|
99
|
+
* None of these gets an OPSIN code. `tokens/errors.json` has no entry for any of
|
|
100
|
+
* them and a component may not mint one: the table is generated from that file
|
|
101
|
+
* and the codes are a versioned contract.
|
|
102
|
+
*/
|
|
103
|
+
const warnedCauses = new Set();
|
|
104
|
+
function warnDevelopmentOnce(cause, message) {
|
|
105
|
+
if (!isDevelopment() || warnedCauses.has(cause)) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
warnedCauses.add(cause);
|
|
109
|
+
console.warn(message);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* What this component will accept into a box, before it will call it a number.
|
|
113
|
+
*
|
|
114
|
+
* Deliberately permissive on both sides of the point and on the leading minus:
|
|
115
|
+
* a reader typing "-" or "12." or "." has typed something incomplete rather than
|
|
116
|
+
* something wrong, and a field that erases an incomplete entry erases the
|
|
117
|
+
* keystroke that was going to complete it. Anything that does not match is still
|
|
118
|
+
* KEPT in the box and reported as no reading. See `parseReading`.
|
|
119
|
+
*/
|
|
120
|
+
const NUMERIC = /^-?(?:\d+(?:\.\d*)?|\.\d*)?$/;
|
|
121
|
+
/**
|
|
122
|
+
* The text in a box, as a number, or `null` when there is no number in it.
|
|
123
|
+
*
|
|
124
|
+
* A COMMA IS A DECIMAL POINT. `patterns/forms/units-and-numeric-entry` requires
|
|
125
|
+
* both forms to be accepted, because a reader with a European keyboard layout
|
|
126
|
+
* types a comma and a field that silently drops it turns 7,5 into 75. Exactly
|
|
127
|
+
* one separator is swapped, so "1,234" is not quietly read as one thousand two
|
|
128
|
+
* hundred and thirty-four. A thousands separator is ambiguous between the two
|
|
129
|
+
* conventions and guessing which was meant is how a reading moves by a factor of
|
|
130
|
+
* a thousand.
|
|
131
|
+
*
|
|
132
|
+
* `null` here means "there is no number in this box". It does NOT mean zero, and
|
|
133
|
+
* it does not mean the reader has not started: those three are different states
|
|
134
|
+
* and the last is not expressible in a number at all. `ReadingInputChange.text`
|
|
135
|
+
* carries the raw box contents for the caller that needs to tell them apart, and
|
|
136
|
+
* Base UI stamps `data-touched`, `data-dirty` and `data-filled` on the control
|
|
137
|
+
* for the caller that would rather read the DOM.
|
|
138
|
+
*/
|
|
139
|
+
function parseReading(text) {
|
|
140
|
+
const trimmed = text.trim().replace(",", ".");
|
|
141
|
+
if (trimmed === "" || !NUMERIC.test(trimmed))
|
|
142
|
+
return null;
|
|
143
|
+
const parsed = Number(trimmed);
|
|
144
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
145
|
+
}
|
|
146
|
+
/** A number as the digits that go in a box. `null` is an empty box, never "0". */
|
|
147
|
+
function toText(value) {
|
|
148
|
+
return value === null ? "" : String(value);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* A CONVERTED number as the digits that go in a box, padded to the precision the
|
|
152
|
+
* field promised.
|
|
153
|
+
*
|
|
154
|
+
* `roundTo` returns a Number and `String()` drops a trailing zero, so a field
|
|
155
|
+
* carrying `precision={1}` and the hint "To one decimal place" converted 12.5 kg
|
|
156
|
+
* to stone and printed "2". That is one significant figure fewer than the
|
|
157
|
+
* sentence above the box had just guaranteed. A reader cannot tell a rounded
|
|
158
|
+
* 1.97 from an exact 2, which is the whole point of stating a precision.
|
|
159
|
+
*
|
|
160
|
+
* It is used ONLY on the conversion path, and that restriction is the design.
|
|
161
|
+
* `toText` also renders the caller's own digits and re-seeds the buffer from
|
|
162
|
+
* props, and padding there would rewrite a number under somebody's cursor as
|
|
163
|
+
* they type. "5." would become "5.0" mid-keystroke and the reader could never
|
|
164
|
+
* reach "5.2". `reported` stays Numbers either way, so `onChange` and
|
|
165
|
+
* `data-opsinjs-value` are untouched.
|
|
166
|
+
*
|
|
167
|
+
* With no `places` nothing is padded, because there is nothing to pad to and a
|
|
168
|
+
* made-up decimal place is the false precision `roundTo` already refuses. The
|
|
169
|
+
* range check is not decoration: `toFixed` throws a RangeError outside 0 to 100
|
|
170
|
+
* where `String()` never threw, and this component does not validate `precision`
|
|
171
|
+
* the way `Value` does. Turning a formatting defect into a crash would be the
|
|
172
|
+
* larger mistake.
|
|
173
|
+
*/
|
|
174
|
+
function toConvertedText(value, places) {
|
|
175
|
+
if (value === null)
|
|
176
|
+
return "";
|
|
177
|
+
if (places === undefined || !Number.isInteger(places) || places < 0 || places > 100) {
|
|
178
|
+
return toText(value);
|
|
179
|
+
}
|
|
180
|
+
return value.toFixed(places);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* A converted number at the caller's precision.
|
|
184
|
+
*
|
|
185
|
+
* `unit-systems` rule 6: round AFTER conversion, at the destination's precision,
|
|
186
|
+
* and accept that the round trip is lossy. With no precision nothing is rounded,
|
|
187
|
+
* which is the honest arithmetic and is also unreadable, since 5 kg is
|
|
188
|
+
* 11.023113109243878 lb. So the caller is told once, in development, that the
|
|
189
|
+
* component has no basis for choosing decimal places and they have not supplied
|
|
190
|
+
* one. There is no per-unit default here and there is none in `Value` either:
|
|
191
|
+
* precision belongs to the measurement, not to the unit, and two measurements
|
|
192
|
+
* reported in the same unit do not share one.
|
|
193
|
+
*/
|
|
194
|
+
function roundTo(value, places) {
|
|
195
|
+
if (places === undefined)
|
|
196
|
+
return value;
|
|
197
|
+
const factor = 10 ** places;
|
|
198
|
+
return Math.round(value * factor) / factor;
|
|
199
|
+
}
|
|
200
|
+
/** Why a pair is not arithmetic, for a developer. Never shown to a reader. */
|
|
201
|
+
function refusalReason(from, to) {
|
|
202
|
+
const row = REFUSED_CONVERSIONS.find((entry) => (entry.between[0] === from && entry.between[1] === to) ||
|
|
203
|
+
(entry.between[0] === to && entry.between[1] === from));
|
|
204
|
+
return row?.reason;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The one sentence a unit switch draws, built once. Both the paragraph under the
|
|
208
|
+
* boxes and `ReadingInputChange.effectText` come from here, so the copy the
|
|
209
|
+
* reader sees and the copy a product routes through its announcer cannot drift.
|
|
210
|
+
*/
|
|
211
|
+
function effectSentence(effect) {
|
|
212
|
+
if (effect.kind === "converted") {
|
|
213
|
+
return `Converted from ${effect.was} ${effect.from}.`;
|
|
214
|
+
}
|
|
215
|
+
if (effect.kind === "restored") {
|
|
216
|
+
return `Restored your entry in ${effect.to}.`;
|
|
217
|
+
}
|
|
218
|
+
return `Cleared. Enter the reading again in ${effect.to}.`;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Written out rather than assembled: Tailwind reads class names as literal
|
|
222
|
+
* strings, and anything built at runtime generates no CSS at all.
|
|
223
|
+
*
|
|
224
|
+
* `flex-wrap` on the control row is the whole of the 200%-text requirement for
|
|
225
|
+
* this component. At the reader's largest text size the unit has to wrap
|
|
226
|
+
* BENEATH the number rather than overlapping it or pushing the page sideways,
|
|
227
|
+
* and a row that cannot wrap does one of those two things. `items-end` aligns
|
|
228
|
+
* the row's items to their bottom edge, which is what a compound reading needs
|
|
229
|
+
* because its per-box labels sit above boxes of different heights. The unit
|
|
230
|
+
* block now sets its own 44px floor and centres its symbol within it, so the
|
|
231
|
+
* symbol lines up with the digits rather than hanging off the box's bottom edge.
|
|
232
|
+
*/
|
|
233
|
+
const CONTROL_ROW = "flex flex-wrap items-end gap-opsin-2";
|
|
234
|
+
/**
|
|
235
|
+
* The unit switch, when there is one.
|
|
236
|
+
*
|
|
237
|
+
* A native `<select>`, and the reasons are all the same reason. It is one tab
|
|
238
|
+
* stop, it is keyboard-operable everywhere without a roving-tabindex
|
|
239
|
+
* implementation to get wrong, it announces its own value, and on a phone it
|
|
240
|
+
* opens the platform's own picker. That picker is a bigger target than anything
|
|
241
|
+
* drawn in CSS. `appearance-none` removes only the platform's arrow, which is
|
|
242
|
+
* redrawn beside it so the control still reads as a control.
|
|
243
|
+
*
|
|
244
|
+
* The 44px floor is set in both axes and with a rem fallback, exactly as
|
|
245
|
+
* `field.tsx` sets it and for the same reason: `@opsinjs/tokens/tokens.css`
|
|
246
|
+
* reaches a page only when the app imports it, and without the fallback the
|
|
247
|
+
* declaration is invalid in a consumer app that has not wired the token sheet
|
|
248
|
+
* and the control is simply short.
|
|
249
|
+
*/
|
|
250
|
+
const UNIT_SELECT = [
|
|
251
|
+
"min-h-[var(--opsin-target-minimum,2.75rem)]",
|
|
252
|
+
"min-w-[var(--opsin-target-minimum,2.75rem)]",
|
|
253
|
+
"appearance-none rounded-opsin-sm border border-input bg-background",
|
|
254
|
+
"py-opsin-2 pl-opsin-3 pr-opsin-8 text-opsin-body",
|
|
255
|
+
"disabled:opacity-70",
|
|
256
|
+
].join(" ");
|
|
257
|
+
export function ReadingInput({ label, unit, value = null, segments, onChange, units, hint, warning, precision, name, autoComplete = "off", inputMode = "decimal", enterKeyHint, optionality = "none", disabled = false, className, }) {
|
|
258
|
+
const unitId = useId();
|
|
259
|
+
const hintId = useId();
|
|
260
|
+
const warningId = useId();
|
|
261
|
+
const effectId = useId();
|
|
262
|
+
if (label.trim() === "") {
|
|
263
|
+
warnDevelopmentOnce("empty-label", "[opsinjs] <ReadingInput> was given an empty `label`. The control now has " +
|
|
264
|
+
"no accessible name, which is the one thing composing <Field> was " +
|
|
265
|
+
"supposed to guarantee. Nothing is substituted on purpose: a made-up " +
|
|
266
|
+
'name like "Measurement" would silence the audit that would otherwise ' +
|
|
267
|
+
"catch this. See /docs/components/reading-input.");
|
|
268
|
+
}
|
|
269
|
+
if (unit.trim() === "") {
|
|
270
|
+
warnDevelopmentOnce("empty-unit:" + label, '[opsinjs] <ReadingInput label="' +
|
|
271
|
+
label +
|
|
272
|
+
'"> was given an empty `unit`. A ' +
|
|
273
|
+
"number a person is typing about themselves is ambiguous between unit " +
|
|
274
|
+
"systems without one, and nothing on the surface says which was meant. " +
|
|
275
|
+
"Pass the unit the reading is measured in.");
|
|
276
|
+
}
|
|
277
|
+
if (segments !== undefined && segments.length === 0) {
|
|
278
|
+
warnDevelopmentOnce("empty-segments:" + label, '[opsinjs] <ReadingInput label="' +
|
|
279
|
+
label +
|
|
280
|
+
'"> was given an empty `segments` ' +
|
|
281
|
+
"array. A compound reading with no parts has no boxes to type in, so " +
|
|
282
|
+
"the field fell back to a single one. Pass one entry per part, or omit " +
|
|
283
|
+
"the prop.");
|
|
284
|
+
}
|
|
285
|
+
if (segments !== undefined && segments.length > 0 && value !== null) {
|
|
286
|
+
warnDevelopmentOnce("value-and-segments:" + label, '[opsinjs] <ReadingInput label="' +
|
|
287
|
+
label +
|
|
288
|
+
'"> was given both `value` and ' +
|
|
289
|
+
"`segments`. A compound reading has no single number, so `value` was " +
|
|
290
|
+
"ignored. Pass the parts in `segments` and read them back from " +
|
|
291
|
+
"`onChange`'s `segments`.");
|
|
292
|
+
}
|
|
293
|
+
/* ONE INTERNAL SHAPE, TWO PUBLIC ONES. Everything below works on an array of
|
|
294
|
+
parts, so the simple and the compound reading share every code path that
|
|
295
|
+
could otherwise drift. That is the parsing, the conversion, the change
|
|
296
|
+
payload and the described-by wiring. The simple reading is a one-part array
|
|
297
|
+
whose part is named by the field's own label, which is also what `onChange`
|
|
298
|
+
reports, so a caller can hand `segments` straight to a ResultCard either
|
|
299
|
+
way. */
|
|
300
|
+
const compound = segments !== undefined && segments.length > 0;
|
|
301
|
+
const parts = compound
|
|
302
|
+
? segments
|
|
303
|
+
: [{ label, value }];
|
|
304
|
+
/* A NUMBER THAT DID NOT SURVIVE ITS JOURNEY IS NOT A READING, AND THE CRASH IS
|
|
305
|
+
WHY THIS IS HERE RATHER THAN IN THE PROP TYPES. `value` and
|
|
306
|
+
`segments[].value` are typed `number | null`, and NaN is a `number`:
|
|
307
|
+
`Number(row.weight)` on an em dash, an empty string or `undefined` produces
|
|
308
|
+
one, TypeScript accepts it, and nothing else in this file narrows it. Two
|
|
309
|
+
things then go wrong at once. `toText(NaN)` puts the literal string "NaN"
|
|
310
|
+
in the box. `NaN === NaN` is false, so the buffer below never reconciles
|
|
311
|
+
with the props, the render-phase `setEntry` fires on every pass, and React
|
|
312
|
+
aborts the whole tree with "Too many re-renders", taking the form and
|
|
313
|
+
everything around it off the screen. A degradation is arguable; a crash is
|
|
314
|
+
not.
|
|
315
|
+
|
|
316
|
+
So it is narrowed once, here, at the boundary where the two public shapes
|
|
317
|
+
become one internal one, and `null` is what it becomes: the empty box every
|
|
318
|
+
path below already knows how to render, with `toText` returning "" for it
|
|
319
|
+
unchanged. Every other numeric component in this registry narrows a
|
|
320
|
+
non-finite number to an explicit third state rather than drawing it. This
|
|
321
|
+
is that rule, arriving late. The caller is told in development, because a
|
|
322
|
+
silently emptied box is a reading a product thinks it passed. */
|
|
323
|
+
const incoming = parts.map((part) => part.value !== null && Number.isFinite(part.value) ? part.value : null);
|
|
324
|
+
if (parts.some((part) => part.value !== null && !Number.isFinite(part.value))) {
|
|
325
|
+
warnDevelopmentOnce("non-finite-value:" + label, '[opsinjs] <ReadingInput label="' +
|
|
326
|
+
label +
|
|
327
|
+
'"> was given a value that is not a ' +
|
|
328
|
+
"finite number. NaN or an infinity is what `Number(x)` returns " +
|
|
329
|
+
"for an em dash, an empty string or `undefined`. That is not a reading, " +
|
|
330
|
+
'so the box was left empty rather than filled with the word "NaN". ' +
|
|
331
|
+
"Pass `null` for a measurement you do not have; the field already says " +
|
|
332
|
+
"so in the reader's own words.");
|
|
333
|
+
}
|
|
334
|
+
/* THE TEXT BUFFER, AND WHY IT IS NOT THE VALUE.
|
|
335
|
+
"5." parses to 5. If the box were rendered from the parsed number the full
|
|
336
|
+
stop would vanish under the cursor the instant it was typed, and the reader
|
|
337
|
+
could never reach "5.2". So the boxes render text, the caller gets numbers,
|
|
338
|
+
and `reported` remembers the numbers this component last sent out.
|
|
339
|
+
|
|
340
|
+
The buffer is re-synchronised from props whenever `reported` and the props
|
|
341
|
+
disagree. That happens when the caller changes the reading itself, when it
|
|
342
|
+
REFUSES a change and passes the old value back, and on the first render.
|
|
343
|
+
Both are the same event from here: what the caller says is now on screen has
|
|
344
|
+
stopped matching what this component last said was on screen. Derived during
|
|
345
|
+
render rather than in an effect, so the boxes never paint one frame of a
|
|
346
|
+
value the caller has already replaced. */
|
|
347
|
+
const [entry, setEntry] = useState(() => ({
|
|
348
|
+
text: incoming.map(toText),
|
|
349
|
+
reported: incoming,
|
|
350
|
+
effect: null,
|
|
351
|
+
}));
|
|
352
|
+
/* `Object.is` rather than `===`, and it is not a stylistic swap. The values
|
|
353
|
+
reaching here are already narrowed, so the two agree on every one of them
|
|
354
|
+
today; the point is that the comparison stays correct if a non-finite value
|
|
355
|
+
ever reaches this line again, because that is the comparison whose failure
|
|
356
|
+
is an infinite render loop rather than a wrong string. */
|
|
357
|
+
const inSync = entry.reported.length === incoming.length &&
|
|
358
|
+
entry.reported.every((reported, index) => Object.is(reported, incoming[index]));
|
|
359
|
+
if (!inSync) {
|
|
360
|
+
setEntry({
|
|
361
|
+
text: incoming.map(toText),
|
|
362
|
+
reported: incoming,
|
|
363
|
+
effect: null,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
/* This render's values, not the state's: `setEntry` above schedules a second
|
|
367
|
+
render pass, and painting the stale buffer in this one would show the
|
|
368
|
+
previous reading for a frame. */
|
|
369
|
+
const text = inSync ? entry.text : incoming.map(toText);
|
|
370
|
+
const effect = inSync ? entry.effect : null;
|
|
371
|
+
const spoken = findUnit(unit);
|
|
372
|
+
const switchable = units !== undefined && units.length > 1;
|
|
373
|
+
if (compound && optionality !== "none") {
|
|
374
|
+
warnDevelopmentOnce("optionality-on-compound:" + label, '[opsinjs] <ReadingInput label="' +
|
|
375
|
+
label +
|
|
376
|
+
'"> is a compound reading and was ' +
|
|
377
|
+
"given `optionality`. The marker is a word inside a Field's label, and a " +
|
|
378
|
+
"compound reading's name is a `<legend>` rather than a label, so it was " +
|
|
379
|
+
"not applied to anything. Say it in the `label` itself until this " +
|
|
380
|
+
"component has somewhere to put it.");
|
|
381
|
+
}
|
|
382
|
+
if (units !== undefined && units.length > 0 && !units.includes(unit)) {
|
|
383
|
+
warnDevelopmentOnce("unit-not-in-units:" + unit, '[opsinjs] <ReadingInput label="' +
|
|
384
|
+
label +
|
|
385
|
+
'"> has `unit="' +
|
|
386
|
+
unit +
|
|
387
|
+
'"`, which is not one of the `units` it offers. The switch cannot show ' +
|
|
388
|
+
"the unit the number is actually in, so a reader is looking at one unit " +
|
|
389
|
+
"and being told another. Include the current unit in the list.");
|
|
390
|
+
}
|
|
391
|
+
function emit(nextText, nextUnit, cause, unitEffect, effectText) {
|
|
392
|
+
const values = nextText.map(parseReading);
|
|
393
|
+
const nextSegments = parts.map((part, index) => ({
|
|
394
|
+
label: part.label,
|
|
395
|
+
value: values[index] ?? null,
|
|
396
|
+
}));
|
|
397
|
+
onChange({
|
|
398
|
+
segments: nextSegments,
|
|
399
|
+
value: compound ? null : (nextSegments[0]?.value ?? null),
|
|
400
|
+
unit: nextUnit,
|
|
401
|
+
text: nextText,
|
|
402
|
+
cause,
|
|
403
|
+
unitEffect,
|
|
404
|
+
effectText,
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
function handleText(index, next) {
|
|
408
|
+
const nextText = text.map((current, position) => (position === index ? next : current));
|
|
409
|
+
/* The effect line is cleared the moment the reader touches a box. It
|
|
410
|
+
describes what happened to an entry that no longer exists. */
|
|
411
|
+
setEntry({ text: nextText, reported: nextText.map(parseReading), effect: null });
|
|
412
|
+
emit(nextText, unit, "value", null, null);
|
|
413
|
+
}
|
|
414
|
+
function handleUnit(next) {
|
|
415
|
+
if (next === unit)
|
|
416
|
+
return;
|
|
417
|
+
const current = text.map(parseReading);
|
|
418
|
+
const anyTyped = current.some((reading) => reading !== null);
|
|
419
|
+
/* The reader-facing echo of what they had typed, built from the PARSED
|
|
420
|
+
numbers rather than the raw buffer. `toText` returns "" for null, so a box
|
|
421
|
+
holding nothing and a box holding something that is not a number both drop
|
|
422
|
+
out rather than leaving an empty slot, and the anyTyped guard above
|
|
423
|
+
guarantees at least one survivor. The parts join with the word "and": a
|
|
424
|
+
solidus in a sentence reads as arithmetic, and the solidus is the display
|
|
425
|
+
convention for a compound reading that belongs to ResultCard, not to a
|
|
426
|
+
sentence this component writes. */
|
|
427
|
+
const wasPhrase = current
|
|
428
|
+
.map(toText)
|
|
429
|
+
.filter((digits) => digits !== "")
|
|
430
|
+
.join(" and ");
|
|
431
|
+
/* The boxes are empty, but they may be empty because a refusal cleared them,
|
|
432
|
+
and the buffer the reader typed survives in the cleared effect's wasText. */
|
|
433
|
+
if (!anyTyped) {
|
|
434
|
+
/* Back to the unit they typed in: put the digits back and say so. A switch
|
|
435
|
+
that changes what is in the boxes has to say on screen what it did, the
|
|
436
|
+
same rule that governs a conversion and a clearance, so the entry does
|
|
437
|
+
not reappear silently. The loss was a choice this branch used to make;
|
|
438
|
+
the component held the buffer all along. */
|
|
439
|
+
if (effect?.kind === "cleared" && next === effect.from) {
|
|
440
|
+
const restored = effect.wasText;
|
|
441
|
+
const restoredEffect = {
|
|
442
|
+
kind: "restored",
|
|
443
|
+
from: effect.to,
|
|
444
|
+
to: next,
|
|
445
|
+
was: effect.was,
|
|
446
|
+
wasText: restored,
|
|
447
|
+
};
|
|
448
|
+
setEntry({
|
|
449
|
+
text: restored,
|
|
450
|
+
reported: restored.map(parseReading),
|
|
451
|
+
effect: restoredEffect,
|
|
452
|
+
});
|
|
453
|
+
emit(restored, next, "unit", "restored", effectSentence(restoredEffect));
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
/* Still empty from a refusal, but the reader has moved to a third unit
|
|
457
|
+
rather than back. Keep the cleared effect and retarget it, so the
|
|
458
|
+
instruction beneath names the unit now selected and wasText survives for
|
|
459
|
+
a later switch back to the one they typed in. */
|
|
460
|
+
if (effect?.kind === "cleared") {
|
|
461
|
+
const retargeted = { ...effect, to: next };
|
|
462
|
+
setEntry({ text, reported: current, effect: retargeted });
|
|
463
|
+
emit(text, next, "unit", "unchanged", effectSentence(retargeted));
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
/* Nothing was typed and nothing was cleared, so nothing can have happened
|
|
467
|
+
to it. The switch is reported and no sentence is drawn. A line saying an
|
|
468
|
+
empty field stayed empty is noise in the description of every control on
|
|
469
|
+
the row. */
|
|
470
|
+
setEntry({ text, reported: current, effect: null });
|
|
471
|
+
emit(text, next, "unit", "unchanged", null);
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
const converted = current.map((reading) => reading === null ? null : convertUnit(reading, unit, next));
|
|
475
|
+
const unconvertible = converted.some((result, index) => current[index] !== null && result === undefined);
|
|
476
|
+
/* THE REFUSAL, AND IT IS THE POINT OF THE WHOLE UNIT SURFACE.
|
|
477
|
+
`convertUnit` returns undefined when this system does not own the factor,
|
|
478
|
+
and it does not own mmol/L↔mg/dL, HbA1c mmol/mol↔%, or kcal↔kJ. Keeping
|
|
479
|
+
the digits and changing the label under them would be an eighteen-fold
|
|
480
|
+
error that looks entirely reasonable on screen, so the entry goes and the
|
|
481
|
+
reader is told. A product that owns the arithmetic re-renders with its own
|
|
482
|
+
number on `unitEffect: "cleared"` and the reader keeps their entry. */
|
|
483
|
+
if (unconvertible) {
|
|
484
|
+
const reason = refusalReason(unit, next);
|
|
485
|
+
warnDevelopmentOnce("no-conversion:" + unit + ">" + next, '[opsinjs] <ReadingInput> cannot convert from "' +
|
|
486
|
+
unit +
|
|
487
|
+
'" to "' +
|
|
488
|
+
next +
|
|
489
|
+
'", so the entry was cleared rather than relabelled. ' +
|
|
490
|
+
(reason ??
|
|
491
|
+
"tokens/units.json carries only conversions that are true by " +
|
|
492
|
+
"definition, and there is no authored factor for this pair.") +
|
|
493
|
+
" Supply the converted value yourself when `onChange` reports " +
|
|
494
|
+
'`unitEffect: "cleared"`, or do not offer this pair in `units`.');
|
|
495
|
+
const cleared = text.map(() => "");
|
|
496
|
+
const clearedEffect = {
|
|
497
|
+
kind: "cleared",
|
|
498
|
+
from: unit,
|
|
499
|
+
to: next,
|
|
500
|
+
was: wasPhrase,
|
|
501
|
+
wasText: text,
|
|
502
|
+
};
|
|
503
|
+
setEntry({
|
|
504
|
+
text: cleared,
|
|
505
|
+
reported: cleared.map(() => null),
|
|
506
|
+
effect: clearedEffect,
|
|
507
|
+
});
|
|
508
|
+
emit(cleared, next, "unit", "cleared", effectSentence(clearedEffect));
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
if (precision === undefined) {
|
|
512
|
+
warnDevelopmentOnce("conversion-without-precision:" + label, '[opsinjs] <ReadingInput label="' +
|
|
513
|
+
label +
|
|
514
|
+
'"> converted a reading and has ' +
|
|
515
|
+
"no `precision`, so the result was not rounded and the reader is " +
|
|
516
|
+
"looking at every decimal place the arithmetic produced. There is no " +
|
|
517
|
+
"per-unit default to fall back on: decimal places belong to the " +
|
|
518
|
+
"measurement rather than to the unit. Pass the precision the " +
|
|
519
|
+
"measurement is reported to.");
|
|
520
|
+
}
|
|
521
|
+
const rounded = converted.map((result) => result === undefined || result === null ? null : roundTo(result, precision));
|
|
522
|
+
const nextText = rounded.map((result) => toConvertedText(result, precision));
|
|
523
|
+
const convertedEffect = {
|
|
524
|
+
kind: "converted",
|
|
525
|
+
from: unit,
|
|
526
|
+
to: next,
|
|
527
|
+
was: wasPhrase,
|
|
528
|
+
wasText: text,
|
|
529
|
+
};
|
|
530
|
+
setEntry({
|
|
531
|
+
text: nextText,
|
|
532
|
+
reported: rounded,
|
|
533
|
+
effect: convertedEffect,
|
|
534
|
+
});
|
|
535
|
+
emit(nextText, next, "unit", "converted", effectSentence(convertedEffect));
|
|
536
|
+
}
|
|
537
|
+
/* THE DESCRIPTION LIST, ASSEMBLED HERE AND MERGED BY BASE UI.
|
|
538
|
+
`Field.Control` runs whatever `aria-describedby` it is given through the
|
|
539
|
+
labelable context, which appends the ids Field registered for the hint and
|
|
540
|
+
the error rather than replacing them. So these four ids are added to that
|
|
541
|
+
list, never substituted for it. This is verified against
|
|
542
|
+
`LabelableProvider`, which splits the incoming attribute and concatenates.
|
|
543
|
+
|
|
544
|
+
The unit is in the DESCRIPTION rather than in the name, so the field
|
|
545
|
+
announces as "Example measurement, edit, 12, in kilograms" and the unit is
|
|
546
|
+
still available to somebody who arrived by touch exploration. In a compound
|
|
547
|
+
reading the hint is here too, because the group's guidance belongs to every
|
|
548
|
+
box in it rather than to the first. */
|
|
549
|
+
const descriptionIds = [
|
|
550
|
+
unit.trim() === "" ? undefined : unitId,
|
|
551
|
+
compound && hint !== undefined && hint.trim() !== "" ? hintId : undefined,
|
|
552
|
+
warning !== undefined && warning.trim() !== "" ? warningId : undefined,
|
|
553
|
+
effect === null ? undefined : effectId,
|
|
554
|
+
].filter((id) => id !== undefined);
|
|
555
|
+
const describedBy = descriptionIds.join(" ");
|
|
556
|
+
/* The select names the effect line FIRST, so the sentence is the first thing
|
|
557
|
+
heard the next time the select is announced after a switch. This does not
|
|
558
|
+
announce anything on change: focus stays on the select, and a select's
|
|
559
|
+
description is not re-read when its value changes. Telling the reader that
|
|
560
|
+
the number changed is the product's job through `onChange`'s `effectText`,
|
|
561
|
+
routed to a polite announcer. This only improves what a reader hears when
|
|
562
|
+
they next move within or re-focus the select. */
|
|
563
|
+
const unitDescribedBy = (effect === null
|
|
564
|
+
? descriptionIds
|
|
565
|
+
: [effectId, ...descriptionIds.filter((id) => id !== effectId)]).join(" ");
|
|
566
|
+
const unitBlock = (_jsxs("div", { className: "flex min-h-[var(--opsin-target-minimum,2.75rem)] shrink-0 items-center", "data-slot": "reading-input-unit", children: [switchable ? (_jsxs("span", { className: "relative inline-flex items-center", children: [_jsx("select", { "aria-label": `Unit for ${label}`, "aria-describedby": unitDescribedBy === "" ? undefined : unitDescribedBy, className: UNIT_SELECT, disabled: disabled, onChange: (event) => handleUnit(event.currentTarget.value), value: unit, children: (units ?? []).map((option) => {
|
|
567
|
+
const spokenOption = findUnit(option);
|
|
568
|
+
return (_jsx("option", { "aria-label": spokenOption === undefined ? undefined : spokenOption.plural, value: option, children: option }, option));
|
|
569
|
+
}) }), _jsx(ChevronDown, { "aria-hidden": "true", className: "pointer-events-none absolute right-opsin-3 size-[1em]" })] })) : (
|
|
570
|
+
/* Hidden from speech only when there is a spoken form to hear instead.
|
|
571
|
+
With no entry in the unit table the symbol stays in the accessibility
|
|
572
|
+
tree: awkward to listen to, and true. */
|
|
573
|
+
_jsx("span", { "aria-hidden": spoken !== undefined, className: "text-opsin-body", children: unit })), unit.trim() === "" ? null : (_jsx("span", { className: "sr-only", id: unitId, children: spoken === undefined ? unit : `in ${spoken.plural}` }))] }));
|
|
574
|
+
const boxes = parts.map((part, index) => {
|
|
575
|
+
const control = (_jsx(Field.Control, { "aria-describedby": describedBy === "" ? undefined : describedBy, autoComplete: autoComplete, disabled: disabled, enterKeyHint: enterKeyHint, inputMode: inputMode, name: name === undefined ? undefined : compound ? `${name}-${index}` : name, onChange: (event) => handleText(index, event.currentTarget.value),
|
|
576
|
+
/* `type="text"` and not `type="number"`, deliberately. A number input
|
|
577
|
+
silently discards what it cannot parse. So a reader who types a comma
|
|
578
|
+
on a European keyboard watches their entry disappear with no message.
|
|
579
|
+
It also exposes spin buttons the specification refuses as a sole
|
|
580
|
+
means of entry, and it scrolls the value on a trackpad. `inputMode`
|
|
581
|
+
brings up the same keypad without any of that. */
|
|
582
|
+
type: "text", value: text[index] ?? "" }));
|
|
583
|
+
return (_jsx("div", { className: "flex min-w-0 flex-1 flex-col", "data-opsinjs-value": toText(parseReading(text[index] ?? "")), "data-slot": "reading-input-segment", children: compound ? (
|
|
584
|
+
/* Each part is its own Field, so each box has its own visible,
|
|
585
|
+
persistent, programmatically associated label. The group's name is
|
|
586
|
+
the legend above them; the part's name is here. That is what makes
|
|
587
|
+
a compound reading announce as its group and then its part, rather
|
|
588
|
+
than as two unrelated numbers. */
|
|
589
|
+
_jsx(Field, { label: part.label, children: control })) : (control) }, part.label + String(index)));
|
|
590
|
+
});
|
|
591
|
+
const warningBlock = warning === undefined || warning.trim() === "" ? null : (
|
|
592
|
+
/* NOT `Field`'s `error` PROP, and that is the whole design of this part.
|
|
593
|
+
`error` marks the control invalid, and an invalid control is a control a
|
|
594
|
+
form refuses to submit and a screen reader announces as wrong. This
|
|
595
|
+
sentence is advisory: the product thinks the number might be a typing
|
|
596
|
+
mistake, and it might equally be a true reading that matters precisely
|
|
597
|
+
because it is unusual. So it is a description, in the ordinary
|
|
598
|
+
foreground colour, with no glyph, no status colour and no aria-invalid
|
|
599
|
+
anywhere near it. Nothing here moves focus and nothing here blocks.
|
|
600
|
+
|
|
601
|
+
It is set at `text-opsin-headline`, the emphasis weight, because it is
|
|
602
|
+
the one sentence the product most wants read and weight is the only axis
|
|
603
|
+
left once colour and a glyph are ruled out. That is body size at weight
|
|
604
|
+
600, so the size, leading, tracking and colour are unchanged and only
|
|
605
|
+
the weight moves. The hint above the box stays at body weight, so the
|
|
606
|
+
two remain distinguishable. `text-opsin-body` is replaced rather than
|
|
607
|
+
joined: both resolve to a font-size utility carrying its own weight
|
|
608
|
+
sub-key, and two on one element is a specificity race. */
|
|
609
|
+
_jsx("p", { className: "m-0 text-opsin-headline text-foreground", "data-slot": "reading-input-warning", id: warningId, children: warning }));
|
|
610
|
+
const effectBlock = effect === null ? null : (
|
|
611
|
+
/* WHAT HAPPENED TO THE TYPED VALUE, ON SCREEN. The specification is
|
|
612
|
+
explicit that a unit switch must never silently convert and never
|
|
613
|
+
silently keep the digits, and that whichever it does it has to say so.
|
|
614
|
+
These three sentences are the only reader-facing copy this component
|
|
615
|
+
owns, and there is no prop to translate them. That is a real gap, listed
|
|
616
|
+
on the page rather than hidden here.
|
|
617
|
+
|
|
618
|
+
It is NOT a live region. The substrate contract forbids a component
|
|
619
|
+
mounting one on the caller's behalf, so this joins the description of
|
|
620
|
+
every control on the row and is read when focus reaches one, rather than
|
|
621
|
+
interrupting. What that costs is on the page too.
|
|
622
|
+
|
|
623
|
+
The colour splits by what the sentence asks of the reader. 'converted'
|
|
624
|
+
and 'restored' are reassurance about a change already made safely, so
|
|
625
|
+
they stay muted body. 'cleared' takes the full foreground at the
|
|
626
|
+
emphasis weight, because the boxes are empty and this sentence is the
|
|
627
|
+
only thing telling the reader why and what to do; information a reader
|
|
628
|
+
acts on does not go in the colour reserved for things that can be
|
|
629
|
+
skipped, which is field.tsx's own rule for the same colour. The weight
|
|
630
|
+
matches the advisory above, so the two sentences a reader must act on
|
|
631
|
+
look like each other. The two size utilities are mutually exclusive
|
|
632
|
+
because each carries its own weight sub-key. */
|
|
633
|
+
_jsx("p", { className: cn("m-0", effect.kind === "cleared"
|
|
634
|
+
? "text-opsin-headline text-foreground"
|
|
635
|
+
: "text-opsin-body text-muted-foreground"), "data-slot": "reading-input-effect", id: effectId, children: effectSentence(effect) }));
|
|
636
|
+
if (!compound) {
|
|
637
|
+
return (_jsx("div", { className: cn("w-full", className), "data-slot": "reading-input", children: _jsxs(Field, { hint: hint, label: label, optionality: optionality, children: [_jsxs("div", { className: CONTROL_ROW, "data-slot": "reading-input-control", children: [boxes, unitBlock] }), warningBlock, effectBlock] }) }));
|
|
638
|
+
}
|
|
639
|
+
return (
|
|
640
|
+
/* A real `<fieldset>` and a real `<legend>`, because a compound reading is
|
|
641
|
+
one measurement made of separately-typable parts and that is the only
|
|
642
|
+
native construct that says so. The browser resets are explicit: a fieldset
|
|
643
|
+
ships with a border, padding and a `min-inline-size: min-content` that
|
|
644
|
+
stops it shrinking inside a flex column. The legend sits one type step
|
|
645
|
+
above its parts, at `title3` over the parts' `headline`, because three
|
|
646
|
+
identical bold lines force the reader to work out which one names the
|
|
647
|
+
pair and which two are its halves. This is the only place in the
|
|
648
|
+
component where a type step carries structure. */
|
|
649
|
+
_jsxs("fieldset", { className: cn("m-0 w-full min-w-0 border-0 p-0", className), "data-slot": "reading-input", disabled: disabled, children: [_jsx("legend", { className: "mb-opsin-2 p-0 text-opsin-title3 text-foreground", "data-slot": "reading-input-legend", children: label }), hint === undefined || hint.trim() === "" ? null : (_jsx("p", { className: "m-0 mb-opsin-2 text-opsin-body text-muted-foreground", "data-slot": "reading-input-hint", id: hintId, children: hint })), _jsxs("div", { className: CONTROL_ROW, "data-slot": "reading-input-control", children: [boxes, unitBlock] }), warningBlock, effectBlock] }));
|
|
650
|
+
}
|
|
651
|
+
//# sourceMappingURL=reading-input.js.map
|