@terpjs/react-core 0.30.0 → 0.32.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/README.md +35 -14
- package/package.json +5 -5
- package/src/DashboardPage.tsx +34 -0
- package/src/Field.test.tsx +26 -1
- package/src/Field.tsx +15 -5
- package/src/HubPage.tsx +7 -2
- package/src/ModuleNav.test.tsx +29 -1
- package/src/ModuleNav.tsx +5 -1
- package/src/Page.test.tsx +192 -0
- package/src/Page.tsx +141 -55
- package/src/PageActions.test.tsx +64 -3
- package/src/PageActions.tsx +40 -15
- package/src/PageSequence.tsx +114 -0
- package/src/Stat.test.tsx +376 -0
- package/src/Stat.tsx +316 -0
- package/src/Timeline.test.tsx +74 -0
- package/src/Timeline.tsx +71 -0
- package/src/admin/AdminHub.tsx +233 -78
- package/src/admin/AuditLogAdmin.tsx +3 -1
- package/src/admin/GroupCreate.tsx +3 -1
- package/src/admin/GroupDetail.tsx +72 -67
- package/src/admin/GroupsAdmin.tsx +3 -1
- package/src/admin/ModuleAccessPanel.tsx +13 -12
- package/src/admin/RecordHistory.tsx +88 -0
- package/src/admin/UserCreate.tsx +3 -1
- package/src/admin/UserDetail.tsx +8 -1
- package/src/admin/UsersAdmin.tsx +3 -0
- package/src/admin/admin.test.tsx +389 -19
- package/src/bootstrap.test.tsx +31 -7
- package/src/bootstrap.tsx +30 -4
- package/src/charts/BarChart.tsx +74 -0
- package/src/charts/ProportionBar.tsx +118 -0
- package/src/charts/StatusHistory.tsx +75 -0
- package/src/charts/TrendChart.tsx +232 -0
- package/src/charts/charts.test.tsx +409 -0
- package/src/charts/scale.ts +103 -0
- package/src/currentInView.ts +28 -0
- package/src/dataview/DataView.test.tsx +303 -1
- package/src/dataview/DataView.tsx +53 -3
- package/src/dataview/DataViewCardList.tsx +21 -13
- package/src/dataview/DataViewTable.tsx +5 -6
- package/src/dataview/DataViewToolbar.tsx +4 -1
- package/src/dataview/README.md +18 -0
- package/src/dataview/index.ts +1 -0
- package/src/dataview/internal.tsx +112 -5
- package/src/dataview/types.ts +39 -10
- package/src/errorMessages.test.tsx +17 -0
- package/src/errorMessages.tsx +18 -0
- package/src/format.ts +15 -0
- package/src/index.ts +24 -1
- package/src/layout.manifest.json +2 -9
- package/src/layout.manifest.test.ts +4 -2
- package/src/layout.test.tsx +74 -6
- package/src/layout.tsx +85 -25
- package/src/layoutContract.test.tsx +239 -1
- package/src/layoutContract.ts +131 -5
- package/src/layoutDeclaration.test.ts +31 -10
- package/src/layoutDeclaration.ts +9 -4
- package/src/locale.tsx +28 -3
- package/src/markers.test.ts +70 -2
- package/src/router.test.tsx +15 -3
- package/src/router.tsx +11 -2
- package/src/ssr.test.tsx +18 -0
- package/src/styles.test.ts +288 -190
- package/src/styles.ts +1379 -361
- package/src/theme.test.tsx +44 -18
- package/src/theme.themes.test.ts +22 -2
- package/src/theme.tsx +22 -14
- package/src/themes.ts +39 -4
- package/src/tokens.guard.test.ts +13 -15
- package/src/typography.tsx +5 -1
- package/src/ui/Alert.test.tsx +24 -1
- package/src/ui/Alert.tsx +10 -1
- package/src/ui/Card.tsx +5 -4
- package/src/ui/Tabs.test.tsx +44 -0
- package/src/ui/Tabs.tsx +5 -2
- package/src/ui/Tooltip.test.tsx +171 -2
- package/src/ui/Tooltip.tsx +144 -8
- package/src/ui/controlMessages.tsx +20 -7
- package/src/uiText.test.tsx +16 -0
- package/src/uiText.tsx +105 -7
- package/src/viewerTimeZone.ts +15 -0
package/src/Stat.tsx
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { useContext, useEffect } from "react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
import { formatList, useFormatNumber } from "./format";
|
|
5
|
+
import { HeadlineContext } from "./layoutContract";
|
|
6
|
+
import { useLocale } from "./locale";
|
|
7
|
+
import { Meter } from "./Meter";
|
|
8
|
+
import type { MeterProps } from "./Meter";
|
|
9
|
+
import { injectTerpStyles } from "./styles";
|
|
10
|
+
import { fillPlaceholders, resolveUiTextNode, useStrings, useUiText } from "./uiText";
|
|
11
|
+
import type { UiText, UiTextNode } from "./uiText";
|
|
12
|
+
|
|
13
|
+
injectTerpStyles();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Whether a change is good news, bad news, or neither.
|
|
17
|
+
*
|
|
18
|
+
* The caller's fact, never inferred (ADR 0169 §5): more rows synced is good and more deliveries
|
|
19
|
+
* failed is bad, and only the module that owns the number knows which — the reason ADR 0097 §5
|
|
20
|
+
* gave for refusing a navigation badge a tone of its own.
|
|
21
|
+
*/
|
|
22
|
+
export type StatSentiment = "positive" | "negative" | "neutral";
|
|
23
|
+
|
|
24
|
+
/** A figure's change against an earlier value of itself. */
|
|
25
|
+
export interface StatDelta {
|
|
26
|
+
/** The change. Its sign is the arrow's direction, and zero reads as unchanged. */
|
|
27
|
+
value: number;
|
|
28
|
+
/** Whether the change is good news, bad news or neither — see {@link StatSentiment}. */
|
|
29
|
+
sentiment: StatSentiment;
|
|
30
|
+
/**
|
|
31
|
+
* How the change prints, as the `Intl.NumberFormatOptions` the `format` helpers take
|
|
32
|
+
* (default: a plain number). It always carries its sign, so `{ style: "percent" }` with
|
|
33
|
+
* `0.12` prints `+12%`; a `signDisplay` of the caller's own is overridden to keep it.
|
|
34
|
+
*
|
|
35
|
+
* The printed change is what the arrow and the tone follow: a change that rounds to zero in
|
|
36
|
+
* this format prints `0`, draws the bar for no change and reads as neutral, whatever its
|
|
37
|
+
* declared sentiment, because there is no change left to judge.
|
|
38
|
+
*/
|
|
39
|
+
format?: Intl.NumberFormatOptions;
|
|
40
|
+
/**
|
|
41
|
+
* What the change is measured against — "vs last week", "since the previous run". `label`
|
|
42
|
+
* rather than a name of its own, so the i18n lint reads it as copy wherever it is written.
|
|
43
|
+
*/
|
|
44
|
+
label?: UiText;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** One point of a figure's recent history: when, and how much. */
|
|
48
|
+
export interface StatPoint {
|
|
49
|
+
/** When — a period's name or a formatted date. Read out in the trend's text alternative. */
|
|
50
|
+
label: UiText;
|
|
51
|
+
/** How much, in the figure's own format. */
|
|
52
|
+
value: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The range a figure is read against: `Meter`'s range and bands, with the top required. */
|
|
56
|
+
export type StatTarget = Omit<MeterProps, "value" | "label" | "max"> & {
|
|
57
|
+
/** The top of the range — the budget, the quota, the goal. */
|
|
58
|
+
max: number;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export interface StatProps {
|
|
62
|
+
/** What the figure is ("Open orders"). Printed above the value, and the target meter's name. */
|
|
63
|
+
label: UiText;
|
|
64
|
+
/**
|
|
65
|
+
* The figure. A number prints through the `format` helpers in the app's locale; a string
|
|
66
|
+
* prints as written, for a figure that is not a quantity ("3 d 4 h", "A+"); `null` or
|
|
67
|
+
* `undefined` prints the framework's dash, for a figure not known yet.
|
|
68
|
+
*/
|
|
69
|
+
value: number | string | null | undefined;
|
|
70
|
+
/** How a numeric value prints, as `Intl.NumberFormatOptions` (default: a plain number). */
|
|
71
|
+
format?: Intl.NumberFormatOptions;
|
|
72
|
+
/** The change against an earlier value, as a pill whose tone is the declared sentiment. */
|
|
73
|
+
delta?: StatDelta;
|
|
74
|
+
/**
|
|
75
|
+
* The figure's recent history, oldest first, drawn as a sparkline under the value — ADR
|
|
76
|
+
* 0158's sparkline, the first chart kind. Two points or more draw a line; every point is
|
|
77
|
+
* also read out as text, which is the sparkline's data alternative.
|
|
78
|
+
*/
|
|
79
|
+
trend?: readonly StatPoint[];
|
|
80
|
+
/**
|
|
81
|
+
* The range the figure is read against, drawn as a `Meter` under the value: a spend against
|
|
82
|
+
* its budget, a count against the minimum it must reach. Declare `low`, `high` and
|
|
83
|
+
* `optimum` for the bands, as on `Meter`. Ignored unless the value is a number.
|
|
84
|
+
*/
|
|
85
|
+
target?: StatTarget;
|
|
86
|
+
/** One short line under the figure — "of 1,200 budgeted", "last run 4 minutes ago". */
|
|
87
|
+
caption?: UiTextNode;
|
|
88
|
+
/**
|
|
89
|
+
* The page's headline figure: filled with the brand colour, the loudest thing on the page.
|
|
90
|
+
*
|
|
91
|
+
* At most one figure on a page is the headline — scarcity is what lets the accent mean
|
|
92
|
+
* something (ADR 0169 §4) — and under a layout contract both halves refuse a second one.
|
|
93
|
+
*/
|
|
94
|
+
headline?: boolean;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const DASH = "—";
|
|
98
|
+
|
|
99
|
+
/** The delta's sign rule, applied last over the caller's format so the sign always shows. */
|
|
100
|
+
const SIGNED: Intl.NumberFormatOptions = { signDisplay: "exceptZero" };
|
|
101
|
+
|
|
102
|
+
// The arrow, as geometry: a triangle up, one down, and a bar for no change. Paths rather than
|
|
103
|
+
// glyphs, so the mark is the same in every font and its colour is the pill's.
|
|
104
|
+
const ARROWS = {
|
|
105
|
+
up: "M6 2 L11 10 L1 10 Z",
|
|
106
|
+
down: "M6 10 L1 2 L11 2 Z",
|
|
107
|
+
flat: "M1 5 H11 V7 H1 Z",
|
|
108
|
+
} as const;
|
|
109
|
+
|
|
110
|
+
// The sparkline's canvas. The SVG is stretched to its box (preserveAspectRatio="none"), so these
|
|
111
|
+
// are proportions rather than pixels; the stroke keeps its width through the stretch because
|
|
112
|
+
// the sheet gives it vector-effect: non-scaling-stroke.
|
|
113
|
+
const TREND_WIDTH = 100;
|
|
114
|
+
const TREND_HEIGHT = 24;
|
|
115
|
+
const TREND_INSET = 2;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Which way a change points, read from the change as it prints rather than as it was passed:
|
|
119
|
+
* `0.004` as a percentage prints `0%`, and an up arrow beside `0%` would contradict itself.
|
|
120
|
+
* The sign is a part of its own in `formatToParts`, so this holds in every locale's spelling.
|
|
121
|
+
*/
|
|
122
|
+
function directionOf(
|
|
123
|
+
value: number,
|
|
124
|
+
format: Intl.NumberFormatOptions,
|
|
125
|
+
locale: string | undefined,
|
|
126
|
+
): "up" | "down" | "flat" {
|
|
127
|
+
const parts = new Intl.NumberFormat(locale, format).formatToParts(value);
|
|
128
|
+
return parts.some((part) => part.type === "plusSign")
|
|
129
|
+
? "up"
|
|
130
|
+
: parts.some((part) => part.type === "minusSign")
|
|
131
|
+
? "down"
|
|
132
|
+
: "flat";
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The sparkline's points, scaled to its canvas: x by position, y by value, top is highest. */
|
|
136
|
+
function trendPoints(values: readonly number[]): string {
|
|
137
|
+
const low = Math.min(...values);
|
|
138
|
+
const high = Math.max(...values);
|
|
139
|
+
const span = high - low;
|
|
140
|
+
const step = TREND_WIDTH / (values.length - 1);
|
|
141
|
+
return values
|
|
142
|
+
.map((value, index) => {
|
|
143
|
+
// A flat series is a line through the middle, not a line along the floor.
|
|
144
|
+
const share = span === 0 ? 0.5 : (value - low) / span;
|
|
145
|
+
const y = TREND_HEIGHT - TREND_INSET - share * (TREND_HEIGHT - 2 * TREND_INSET);
|
|
146
|
+
return `${round(index * step)},${round(y)}`;
|
|
147
|
+
})
|
|
148
|
+
.join(" ");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const round = (value: number) => Math.round(value * 100) / 100;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* One figure, with what it needs to be read: its label, its value in the app's locale, how it
|
|
155
|
+
* changed, its recent history, and the range it sits in.
|
|
156
|
+
*
|
|
157
|
+
* The shape of data ADR 0169 calls "one figure, with context". A `Card` with a title and a line
|
|
158
|
+
* of text was the way to show one, and it put the label above the number in a larger type than
|
|
159
|
+
* the number — the figure was the quietest thing in its own box. Here the value is the largest
|
|
160
|
+
* type on the tile, and the label names it.
|
|
161
|
+
*
|
|
162
|
+
* **Where it sits.** On its own, a figure is a tile on `--color-bg-surface`, the rung where data
|
|
163
|
+
* is read (ADR 0169 §3). Several figures about one subject are a {@link StatGroup}, which draws
|
|
164
|
+
* them as one ruled row without a tile each. In a `HubCard`'s `stat` row the card is the frame,
|
|
165
|
+
* so the figure has none of its own. The `headline` figure is filled with the brand colour.
|
|
166
|
+
*
|
|
167
|
+
* **A tone is also a word.** The delta's pill is coloured by the declared sentiment and carries
|
|
168
|
+
* its sign, so every reader sees the direction; the sentiment itself is a word in the
|
|
169
|
+
* accessibility tree ("favourable", "unfavourable"), because a colour alone says it to nobody
|
|
170
|
+
* who cannot see the colour. The sparkline is drawn for the eye and read out as text — each
|
|
171
|
+
* point's label and value — which is ADR 0158's data alternative in the one form valid inside a
|
|
172
|
+
* `HubCard`'s link: a figure is phrasing content throughout, so a table cannot be.
|
|
173
|
+
*
|
|
174
|
+
* It renders no inline styles: the sparkline is SVG geometry in attributes, and every colour
|
|
175
|
+
* comes from the sheet, keyed on the markers and on `data-sentiment` (ADR 0094, ADR 0169 §7).
|
|
176
|
+
*
|
|
177
|
+
* ```tsx
|
|
178
|
+
* <Stat
|
|
179
|
+
* label={{ id: "orders.open", message: "Open orders" }}
|
|
180
|
+
* value={1284}
|
|
181
|
+
* delta={{ value: 0.12, format: { style: "percent" }, sentiment: "positive", label: { id: "orders.vsLastWeek", message: "vs last week" } }}
|
|
182
|
+
* trend={weeks.map((week) => ({ label: week.name, value: week.open }))}
|
|
183
|
+
* />
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
export function Stat({
|
|
187
|
+
label,
|
|
188
|
+
value,
|
|
189
|
+
format,
|
|
190
|
+
delta,
|
|
191
|
+
trend,
|
|
192
|
+
target,
|
|
193
|
+
caption,
|
|
194
|
+
headline = false,
|
|
195
|
+
}: StatProps) {
|
|
196
|
+
const resolve = useUiText();
|
|
197
|
+
const strings = useStrings();
|
|
198
|
+
const formatNumber = useFormatNumber();
|
|
199
|
+
const locale = useLocale()?.locale;
|
|
200
|
+
// A headline registers with its page, which counts them (ADR 0169 §4): a figure that
|
|
201
|
+
// renders late -- after its own data arrives -- is counted when it arrives, where a DOM count
|
|
202
|
+
// taken when the page rendered would have missed it.
|
|
203
|
+
const headlines = useContext(HeadlineContext);
|
|
204
|
+
useEffect(
|
|
205
|
+
() => (headline && headlines !== null ? headlines.register() : undefined),
|
|
206
|
+
[headline, headlines],
|
|
207
|
+
);
|
|
208
|
+
const printed =
|
|
209
|
+
typeof value === "number"
|
|
210
|
+
? formatNumber(value, format)
|
|
211
|
+
: typeof value === "string" && value !== ""
|
|
212
|
+
? value
|
|
213
|
+
: DASH;
|
|
214
|
+
const deltaFormat =
|
|
215
|
+
delta === undefined ? SIGNED : { ...(delta.format ?? {}), ...SIGNED };
|
|
216
|
+
const direction =
|
|
217
|
+
delta === undefined || !Number.isFinite(delta.value)
|
|
218
|
+
? undefined
|
|
219
|
+
: directionOf(delta.value, deltaFormat, locale);
|
|
220
|
+
// No change left to judge is neutral, whatever was declared for the change.
|
|
221
|
+
const sentiment = direction === "flat" ? "neutral" : delta?.sentiment;
|
|
222
|
+
const sentimentWord =
|
|
223
|
+
sentiment === "positive"
|
|
224
|
+
? strings.statFavourable
|
|
225
|
+
: sentiment === "negative"
|
|
226
|
+
? strings.statUnfavourable
|
|
227
|
+
: undefined;
|
|
228
|
+
const points = trend ?? [];
|
|
229
|
+
// The text alternative names every point, label and value, in the order drawn and joined the
|
|
230
|
+
// way the app's locale joins a list ("a, b and c"), so it reads as a sentence in every
|
|
231
|
+
// language. A value that is not a number prints the dash here and draws nothing below.
|
|
232
|
+
const trendText =
|
|
233
|
+
points.length === 0
|
|
234
|
+
? undefined
|
|
235
|
+
: fillPlaceholders(strings.statTrend, {
|
|
236
|
+
points: formatList(
|
|
237
|
+
points.map((point) => `${resolve(point.label)}: ${formatNumber(point.value, format)}`),
|
|
238
|
+
locale,
|
|
239
|
+
),
|
|
240
|
+
});
|
|
241
|
+
const drawable = points.map((point) => point.value).filter((value) => Number.isFinite(value));
|
|
242
|
+
const line = drawable.length >= 2 ? trendPoints(drawable) : undefined;
|
|
243
|
+
const showsTarget = target !== undefined && typeof value === "number" && Number.isFinite(value);
|
|
244
|
+
return (
|
|
245
|
+
<span data-terp="stat" data-headline={headline ? "true" : undefined}>
|
|
246
|
+
<span data-terp="stat-label">{resolve(label)}</span>
|
|
247
|
+
<span data-terp="stat-value" data-empty={printed === DASH ? "true" : undefined}>
|
|
248
|
+
{printed}
|
|
249
|
+
</span>
|
|
250
|
+
{delta !== undefined && direction !== undefined && (
|
|
251
|
+
<span data-terp="stat-delta">
|
|
252
|
+
<span data-terp="stat-change" data-sentiment={sentiment}>
|
|
253
|
+
<svg data-terp="stat-arrow" viewBox="0 0 12 12" aria-hidden="true" focusable="false">
|
|
254
|
+
<path d={ARROWS[direction]} />
|
|
255
|
+
</svg>
|
|
256
|
+
{formatNumber(delta.value, deltaFormat)}
|
|
257
|
+
{sentimentWord !== undefined && (
|
|
258
|
+
<span data-terp="stat-sentiment">{` ${sentimentWord}`}</span>
|
|
259
|
+
)}
|
|
260
|
+
</span>
|
|
261
|
+
{delta.label !== undefined && (
|
|
262
|
+
<span data-terp="stat-delta-label">{resolve(delta.label)}</span>
|
|
263
|
+
)}
|
|
264
|
+
</span>
|
|
265
|
+
)}
|
|
266
|
+
{/* The series' text, alone when there is no line to draw: as the only child of a block
|
|
267
|
+
of its own it left an empty block the height of the block's margin. */}
|
|
268
|
+
{trendText !== undefined && line === undefined && (
|
|
269
|
+
<span data-terp="stat-trend-data">{trendText}</span>
|
|
270
|
+
)}
|
|
271
|
+
{trendText !== undefined && line !== undefined && (
|
|
272
|
+
<span data-terp="stat-trend">
|
|
273
|
+
<svg
|
|
274
|
+
data-terp="stat-trend-chart"
|
|
275
|
+
viewBox={`0 0 ${TREND_WIDTH} ${TREND_HEIGHT}`}
|
|
276
|
+
preserveAspectRatio="none"
|
|
277
|
+
aria-hidden="true"
|
|
278
|
+
focusable="false"
|
|
279
|
+
>
|
|
280
|
+
<polygon
|
|
281
|
+
data-terp="stat-trend-area"
|
|
282
|
+
points={`0,${TREND_HEIGHT} ${line} ${TREND_WIDTH},${TREND_HEIGHT}`}
|
|
283
|
+
/>
|
|
284
|
+
<polyline data-terp="stat-trend-line" points={line} />
|
|
285
|
+
</svg>
|
|
286
|
+
<span data-terp="stat-trend-data">{trendText}</span>
|
|
287
|
+
</span>
|
|
288
|
+
)}
|
|
289
|
+
{showsTarget && <Meter label={label} value={value} {...target} />}
|
|
290
|
+
{caption !== undefined && caption !== null && caption !== false && caption !== "" && (
|
|
291
|
+
<span data-terp="stat-caption">{resolveUiTextNode(caption, resolve)}</span>
|
|
292
|
+
)}
|
|
293
|
+
</span>
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export interface StatGroupProps {
|
|
298
|
+
/** The figures: `Stat`s, one of which may be the page's headline. */
|
|
299
|
+
children: ReactNode;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Several figures about one subject, as one ruled row: the shape ADR 0169 calls "a group of
|
|
304
|
+
* figures", and the usual content of a page's `summary` band.
|
|
305
|
+
*
|
|
306
|
+
* Unframed, always — the figures sit on whatever the group sits on, divided by hairline rules
|
|
307
|
+
* rather than boxed one by one, so a row of four figures reads as one statement about the
|
|
308
|
+
* subject rather than four tiles. It is the same row in the summary band and in a body; nothing
|
|
309
|
+
* about it changes with where it is placed.
|
|
310
|
+
*
|
|
311
|
+
* The row wraps by itself, as many figures to a line as fit at 8rem (two on a phone), and a
|
|
312
|
+
* figure that starts a line carries no rule before it.
|
|
313
|
+
*/
|
|
314
|
+
export function StatGroup({ children }: StatGroupProps) {
|
|
315
|
+
return <span data-terp="stat-group">{children}</span>;
|
|
316
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, render, screen, within } from "@testing-library/react";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
+
|
|
6
|
+
import { LOCALE_EN, LOCALE_NL, LocaleProvider } from "./locale";
|
|
7
|
+
import { Timeline } from "./Timeline";
|
|
8
|
+
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
cleanup();
|
|
11
|
+
window.localStorage.clear();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
function renderIn(locale: "nl" | "en", node: ReactNode) {
|
|
15
|
+
return render(
|
|
16
|
+
<LocaleProvider locales={{ nl: LOCALE_NL, en: LOCALE_EN }} defaultLocale={locale}>
|
|
17
|
+
{node}
|
|
18
|
+
</LocaleProvider>,
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const EVENTS = [
|
|
23
|
+
{ label: "Changed", when: "2026-08-21T09:30:00Z", detail: "Actor: 9f2c1b7e" },
|
|
24
|
+
{ label: "Created", when: "2026-08-20T08:00:00Z", tone: "success" as const },
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
describe("Timeline", () => {
|
|
28
|
+
it("is an ordered list named for what its events are of, one item per event", () => {
|
|
29
|
+
// An ordered list, so a screen reader says how many events there are and where it is.
|
|
30
|
+
renderIn("en", <Timeline label="History" events={EVENTS} />);
|
|
31
|
+
const list = screen.getByRole("list", { name: "History" });
|
|
32
|
+
expect(list.tagName).toBe("OL");
|
|
33
|
+
// Mutation: drop the stated role, and VoiceOver reads a list without markers as no list.
|
|
34
|
+
expect(list).toHaveAttribute("role", "list");
|
|
35
|
+
expect(within(list).getAllByRole("listitem")).toHaveLength(2);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("prints each event's words and its moment in the app's locale, with the instant in a time element", () => {
|
|
39
|
+
// Mutation: drop the dateTime, and the machine-readable instant is gone.
|
|
40
|
+
renderIn("nl", <Timeline label="Geschiedenis" events={EVENTS} />);
|
|
41
|
+
const times = [...document.querySelectorAll('[data-terp="timeline-when"]')];
|
|
42
|
+
expect(times[0]!.tagName).toBe("TIME");
|
|
43
|
+
expect(times[0]).toHaveAttribute("dateTime", "2026-08-21T09:30:00.000Z");
|
|
44
|
+
expect(times[0]!.textContent).toMatch(/21 aug\.? 2026/);
|
|
45
|
+
expect(document.querySelector('[data-terp="timeline-label"]')!.textContent).toBe("Changed");
|
|
46
|
+
expect(document.querySelector('[data-terp="timeline-detail"]')!.textContent).toBe("Actor: 9f2c1b7e");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("marks an event's tone on the event and hides the marker, whose word is the label", () => {
|
|
50
|
+
renderIn("en", <Timeline label="History" events={EVENTS} />);
|
|
51
|
+
const events = [...document.querySelectorAll('[data-terp="timeline-event"]')];
|
|
52
|
+
expect(events[0]).not.toHaveAttribute("data-tone");
|
|
53
|
+
expect(events[1]).toHaveAttribute("data-tone", "success");
|
|
54
|
+
expect(events[1]!.querySelector('[data-terp="timeline-marker"]')).toHaveAttribute("aria-hidden", "true");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("renders no detail line for a detail that renders nothing, and no instant for a value that is not a date", () => {
|
|
58
|
+
// Mutation: test `detail !== undefined` alone, and an empty line is added.
|
|
59
|
+
renderIn("en", (
|
|
60
|
+
<Timeline
|
|
61
|
+
label="History"
|
|
62
|
+
events={[
|
|
63
|
+
{ label: "A", when: "not a date", detail: "" },
|
|
64
|
+
{ label: "B", when: null, detail: false },
|
|
65
|
+
]}
|
|
66
|
+
/>
|
|
67
|
+
));
|
|
68
|
+
expect(document.querySelector('[data-terp="timeline-detail"]')).toBeNull();
|
|
69
|
+
for (const time of document.querySelectorAll('[data-terp="timeline-when"]')) {
|
|
70
|
+
expect(time).not.toHaveAttribute("dateTime");
|
|
71
|
+
expect(time.textContent).toBe("—");
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
package/src/Timeline.tsx
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { FormattableDate } from "./format";
|
|
2
|
+
import { useFormatDateTime } from "./format";
|
|
3
|
+
import { injectTerpStyles } from "./styles";
|
|
4
|
+
import type { BadgeTone } from "./ui/Badge";
|
|
5
|
+
import { resolveUiTextNode, useUiText } from "./uiText";
|
|
6
|
+
import type { UiText, UiTextNode } from "./uiText";
|
|
7
|
+
|
|
8
|
+
injectTerpStyles();
|
|
9
|
+
|
|
10
|
+
/** One thing that happened, and when. */
|
|
11
|
+
export interface TimelineEvent {
|
|
12
|
+
/** What happened, in words ("Updated", "Approved"). */
|
|
13
|
+
label: UiText;
|
|
14
|
+
/** When: a date-time the `format` helpers take, printed in the app's locale with its time. */
|
|
15
|
+
when: FormattableDate;
|
|
16
|
+
/** One line more — who did it, or what it changed. */
|
|
17
|
+
detail?: UiTextNode;
|
|
18
|
+
/**
|
|
19
|
+
* The event's tone where it marks a change of state worth seeing at a glance — a deletion
|
|
20
|
+
* as danger. The label is its word, so the marker's colour never carries meaning alone.
|
|
21
|
+
*/
|
|
22
|
+
tone?: BadgeTone;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface TimelineProps {
|
|
26
|
+
/** What the events are of ("History"): the list's accessible name. */
|
|
27
|
+
label: UiText;
|
|
28
|
+
/** The events, in the order to read them — a record's history newest first. */
|
|
29
|
+
events: readonly TimelineEvent[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** An ISO string for `<time dateTime>`, or nothing where the value is not a date. */
|
|
33
|
+
function isoOf(value: FormattableDate): string | undefined {
|
|
34
|
+
const date = value instanceof Date ? value : value === null || value === undefined ? null : new Date(value);
|
|
35
|
+
return date === null || Number.isNaN(date.getTime()) ? undefined : date.toISOString();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Events in order (ADR 0169 §5): what happened to a record, and when, on a line that joins them
|
|
40
|
+
* — a record's audit trail on its detail page, a request's steps.
|
|
41
|
+
*
|
|
42
|
+
* An ordered list, so a screen reader announces how many events there are and where it is
|
|
43
|
+
* among them -- with the list role stated, because WebKit drops a list's semantics once the
|
|
44
|
+
* sheet takes its markers away -- and each `when` is a `<time>` carrying its machine-readable
|
|
45
|
+
* instant. The marker is
|
|
46
|
+
* decoration: the label says what its tone shows. Unframed, so it reads in a card or on the page
|
|
47
|
+
* alike.
|
|
48
|
+
*/
|
|
49
|
+
export function Timeline({ label, events }: TimelineProps) {
|
|
50
|
+
const resolve = useUiText();
|
|
51
|
+
const formatDateTime = useFormatDateTime();
|
|
52
|
+
return (
|
|
53
|
+
<ol data-terp="timeline" role="list" aria-label={resolve(label)}>
|
|
54
|
+
{events.map((event, index) => (
|
|
55
|
+
<li key={index} data-terp="timeline-event" data-tone={event.tone}>
|
|
56
|
+
<span data-terp="timeline-marker" aria-hidden="true" />
|
|
57
|
+
<span data-terp="timeline-label">{resolve(event.label)}</span>
|
|
58
|
+
<time data-terp="timeline-when" dateTime={isoOf(event.when)}>
|
|
59
|
+
{formatDateTime(event.when)}
|
|
60
|
+
</time>
|
|
61
|
+
{event.detail !== undefined &&
|
|
62
|
+
event.detail !== null &&
|
|
63
|
+
event.detail !== false &&
|
|
64
|
+
event.detail !== "" && (
|
|
65
|
+
<span data-terp="timeline-detail">{resolveUiTextNode(event.detail, resolve)}</span>
|
|
66
|
+
)}
|
|
67
|
+
</li>
|
|
68
|
+
))}
|
|
69
|
+
</ol>
|
|
70
|
+
);
|
|
71
|
+
}
|