@terpjs/react-core 0.25.0 → 0.28.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 +31 -7
- package/package.json +6 -5
- package/src/Field.test.tsx +95 -1
- package/src/Field.tsx +70 -1
- package/src/LoginView.test.tsx +33 -0
- package/src/LoginView.tsx +36 -23
- package/src/Meter.test.tsx +304 -0
- package/src/Meter.tsx +159 -0
- package/src/QuietActions.test.tsx +118 -0
- package/src/QuietActions.tsx +75 -0
- package/src/UserMenu.test.tsx +23 -0
- package/src/UserMenu.tsx +9 -1
- package/src/admin/AuditLogAdmin.tsx +1 -1
- package/src/admin/admin.test.tsx +57 -5
- package/src/admin/module.tsx +9 -6
- package/src/bootstrap.test.tsx +1 -1
- package/src/bootstrap.tsx +2 -1
- package/src/dataview/DataView.test.tsx +70 -0
- package/src/dataview/README.md +5 -3
- package/src/dataview/index.ts +1 -1
- package/src/dataview/internal.tsx +76 -19
- package/src/dataview/types.ts +9 -35
- package/src/errorMessages.test.tsx +21 -0
- package/src/errorMessages.tsx +33 -19
- package/src/index.ts +7 -3
- package/src/locale.test.tsx +35 -0
- package/src/locale.tsx +62 -4
- package/src/markers.test.ts +33 -11
- package/src/ssr.test.tsx +18 -0
- package/src/styles.test.ts +341 -33
- package/src/styles.ts +546 -71
- package/src/testing.test.tsx +162 -0
- package/src/testing.ts +84 -0
- package/src/tokens.guard.test.ts +11 -6
- package/src/ui/controlMessages.tsx +27 -14
- package/src/uiText.literals.test.ts +19 -6
- package/src/uiText.test.tsx +54 -2
- package/src/uiText.tsx +166 -6
- package/vitest.setup.ts +7 -20
package/src/index.ts
CHANGED
|
@@ -108,7 +108,7 @@ export { EmptyState } from "./EmptyState";
|
|
|
108
108
|
export type { EmptyStateProps } from "./EmptyState";
|
|
109
109
|
export { ErrorState, describeError } from "./ErrorState";
|
|
110
110
|
export type { ErrorStateProps } from "./ErrorState";
|
|
111
|
-
export { ErrorMessagesProvider, useErrorMessage
|
|
111
|
+
export { ErrorMessagesProvider, useErrorMessage } from "./errorMessages";
|
|
112
112
|
export type { ErrorMessages, ErrorMessagesProviderProps } from "./errorMessages";
|
|
113
113
|
export { ConfirmDialog } from "./ConfirmDialog";
|
|
114
114
|
export { Disclosure } from "./Disclosure";
|
|
@@ -175,8 +175,12 @@ export type {
|
|
|
175
175
|
CodeProps,
|
|
176
176
|
LinkProps,
|
|
177
177
|
} from "./typography";
|
|
178
|
-
export { Field } from "./Field";
|
|
179
|
-
export
|
|
178
|
+
export { Field, FieldRow } from "./Field";
|
|
179
|
+
export { QuietActions } from "./QuietActions";
|
|
180
|
+
export { Meter } from "./Meter";
|
|
181
|
+
export type { FieldProps, FieldRowProps } from "./Field";
|
|
182
|
+
export type { QuietActionsProps } from "./QuietActions";
|
|
183
|
+
export type { MeterProps } from "./Meter";
|
|
180
184
|
export { Stack, Grid, Divider, DetailList, DetailListGroup } from "./layout";
|
|
181
185
|
export type {
|
|
182
186
|
StackProps,
|
package/src/locale.test.tsx
CHANGED
|
@@ -15,6 +15,7 @@ import { DEFAULT_STRINGS, Trans, useStrings } from "./uiText";
|
|
|
15
15
|
afterEach(() => {
|
|
16
16
|
cleanup();
|
|
17
17
|
window.localStorage.clear();
|
|
18
|
+
document.documentElement.removeAttribute("lang");
|
|
18
19
|
});
|
|
19
20
|
|
|
20
21
|
function SignOutLabel() {
|
|
@@ -265,6 +266,40 @@ describe("LocaleProvider + LanguageSwitcher", () => {
|
|
|
265
266
|
).toEqual({ greeting: "Hallo" });
|
|
266
267
|
});
|
|
267
268
|
|
|
269
|
+
it("keeps <html lang> on the active locale, from mount and across a switch", () => {
|
|
270
|
+
// A value neither locale has, so the assertion can only pass if the provider wrote it:
|
|
271
|
+
// starting from "en" would let an effect that never ran pass the English half.
|
|
272
|
+
document.documentElement.lang = "x-stale";
|
|
273
|
+
render(
|
|
274
|
+
<LocaleProvider locales={{ en: LOCALE_EN, nl: NL }} defaultLocale="nl">
|
|
275
|
+
<LanguageSwitcher />
|
|
276
|
+
</LocaleProvider>,
|
|
277
|
+
);
|
|
278
|
+
expect(document.documentElement.lang).toBe("nl");
|
|
279
|
+
|
|
280
|
+
fireEvent.click(screen.getByRole("button", { name: "Taal" }));
|
|
281
|
+
fireEvent.click(screen.getByRole("menuitemradio", { name: "English" }));
|
|
282
|
+
expect(document.documentElement.lang).toBe("en");
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("refuses a catalog that translates the shell but not DataView", () => {
|
|
286
|
+
// The upgrade path, pinned: a catalog complete against the key set before DataView's
|
|
287
|
+
// strings joined it. Refused, and by name, because the alternative is the defect itself —
|
|
288
|
+
// every table in the app back in English under a locale that claims to be complete.
|
|
289
|
+
const shellOnly = Object.fromEntries(
|
|
290
|
+
Object.keys(DEFAULT_STRINGS)
|
|
291
|
+
.filter((key) => !key.startsWith("dataView"))
|
|
292
|
+
.map((key) => [key, `de:${key}`]),
|
|
293
|
+
);
|
|
294
|
+
expect(() =>
|
|
295
|
+
render(
|
|
296
|
+
<LocaleProvider locales={{ en: LOCALE_EN, de: { strings: shellOnly } }}>
|
|
297
|
+
<span />
|
|
298
|
+
</LocaleProvider>,
|
|
299
|
+
),
|
|
300
|
+
).toThrow(/missing .* framework string translation.*dataViewSearchPlaceholder/);
|
|
301
|
+
});
|
|
302
|
+
|
|
268
303
|
it("always renders the descriptor fallback in the source locale", () => {
|
|
269
304
|
render(
|
|
270
305
|
<LocaleProvider
|
package/src/locale.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext, useCallback, useContext, useMemo, useState } from "react";
|
|
1
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
|
2
2
|
import type { ReactNode } from "react";
|
|
3
3
|
import type { UiText } from "@terpjs/contract";
|
|
4
4
|
|
|
@@ -154,9 +154,12 @@ export function defineAppLocales(
|
|
|
154
154
|
export const LOCALE_EN: LocaleCatalog = { label: "English" };
|
|
155
155
|
|
|
156
156
|
/**
|
|
157
|
-
* The built-in Dutch catalog: a
|
|
158
|
-
*
|
|
159
|
-
*
|
|
157
|
+
* The built-in Dutch catalog: a translation of every `TerpStrings` key, so
|
|
158
|
+
* `locales: { en: LOCALE_EN, nl: LOCALE_NL }` translates every string react-core
|
|
159
|
+
* reads from that table — the shell, the packaged admin area and its sidebar
|
|
160
|
+
* entry, every `DataView`'s toolbar and footer, and the wording for the platform's
|
|
161
|
+
* own error codes — with no catalog work in the app (a completeness test pins it to
|
|
162
|
+
* the `TerpStrings` key set).
|
|
160
163
|
*/
|
|
161
164
|
export const LOCALE_NL: LocaleCatalog = {
|
|
162
165
|
label: "Nederlands",
|
|
@@ -294,8 +297,51 @@ export const LOCALE_NL: LocaleCatalog = {
|
|
|
294
297
|
targetColumn: "Doel",
|
|
295
298
|
whenColumn: "Wanneer",
|
|
296
299
|
details: "Details",
|
|
300
|
+
requestLabel: "Verzoek",
|
|
297
301
|
saved: "Opgeslagen",
|
|
298
302
|
requestFailed: "Het verzoek is mislukt. Probeer opnieuw.",
|
|
303
|
+
dataViewSearchPlaceholder: "Zoeken…",
|
|
304
|
+
dataViewClearSearch: "Zoekopdracht wissen",
|
|
305
|
+
dataViewClearFilters: "Filters wissen",
|
|
306
|
+
dataViewViewOptions: "Weergaveopties",
|
|
307
|
+
dataViewColumns: "Kolommen",
|
|
308
|
+
dataViewMoveUp: "Omhoog verplaatsen",
|
|
309
|
+
dataViewMoveDown: "Omlaag verplaatsen",
|
|
310
|
+
dataViewTableView: "Tabelweergave",
|
|
311
|
+
dataViewCardView: "Kaartweergave",
|
|
312
|
+
dataViewPageSize: "Rijen per pagina",
|
|
313
|
+
dataViewResultsRange: "{from}–{to} van {total} resultaten",
|
|
314
|
+
dataViewPageOf: "Pagina {page} van {pages}",
|
|
315
|
+
dataViewFirstPage: "Eerste pagina",
|
|
316
|
+
dataViewPreviousPage: "Vorige pagina",
|
|
317
|
+
dataViewNextPage: "Volgende pagina",
|
|
318
|
+
dataViewLastPage: "Laatste pagina",
|
|
319
|
+
dataViewSelectAllPage: "Alle rijen op deze pagina selecteren",
|
|
320
|
+
dataViewSelectRow: "Rij selecteren",
|
|
321
|
+
dataViewSelected: "{count} geselecteerd",
|
|
322
|
+
dataViewSelectAllResults: "Alle {total} resultaten selecteren",
|
|
323
|
+
dataViewClearSelection: "Selectie wissen",
|
|
324
|
+
dataViewMoreActions: "Meer acties",
|
|
325
|
+
dataViewActions: "Acties",
|
|
326
|
+
dataViewOpenRow: "Details openen: {label}",
|
|
327
|
+
dataViewExpandRow: "Rij uitklappen",
|
|
328
|
+
dataViewCollapseRow: "Rij inklappen",
|
|
329
|
+
dataViewEmpty: "Niets om te tonen.",
|
|
330
|
+
dataViewLoading: "Laden…",
|
|
331
|
+
dataViewRefreshing: "Bijwerken…",
|
|
332
|
+
dataViewErrorTitle: "De gegevens konden niet worden geladen.",
|
|
333
|
+
dataViewResizeColumn: "Kolombreedte aanpassen",
|
|
334
|
+
errorCodeBadRequest: "Het verzoek kon niet worden verwerkt.",
|
|
335
|
+
errorCodeValidationFailed:
|
|
336
|
+
"Sommige velden zijn ongeldig. Controleer het formulier en probeer het opnieuw.",
|
|
337
|
+
errorCodeInvalidToken: "Je sessie is ongeldig. Log opnieuw in.",
|
|
338
|
+
errorCodeAuthenticationRequired: "Log in om verder te gaan.",
|
|
339
|
+
errorCodePermissionDenied: "Je hebt geen toestemming om dit te doen.",
|
|
340
|
+
errorCodeNotFound: "Dit item kon niet worden gevonden.",
|
|
341
|
+
errorCodeConflict:
|
|
342
|
+
"Dit botst met de huidige stand van zaken. Vernieuw de pagina en probeer het opnieuw.",
|
|
343
|
+
errorCodeStaleData:
|
|
344
|
+
"Iemand anders heeft dit item intussen gewijzigd. Vernieuw de pagina en probeer het opnieuw.",
|
|
299
345
|
},
|
|
300
346
|
};
|
|
301
347
|
|
|
@@ -326,6 +372,7 @@ export interface LocaleProviderProps {
|
|
|
326
372
|
* the choice in `localStorage`, and feeds the active catalog's string overrides to the
|
|
327
373
|
* `UiText` context — so every react-core component (and every `UiText` prop) follows the
|
|
328
374
|
* switch with no per-component wiring. Adding a language to an app is one catalog entry.
|
|
375
|
+
* It also keeps `<html lang>` on the active locale, from mount and across every switch.
|
|
329
376
|
*/
|
|
330
377
|
export function LocaleProvider({
|
|
331
378
|
locales,
|
|
@@ -351,6 +398,17 @@ export function LocaleProvider({
|
|
|
351
398
|
});
|
|
352
399
|
const activeLocale = codes.includes(locale) ? locale : fallback;
|
|
353
400
|
|
|
401
|
+
// The document says which language it is in, and it has to be the language on screen.
|
|
402
|
+
// `index.html` can only declare the default; a reader who picked another one, or a stored
|
|
403
|
+
// choice from an earlier visit, would otherwise have a Dutch interface announced to a screen
|
|
404
|
+
// reader as English and read aloud with English pronunciation throughout (WCAG 3.1.1). An
|
|
405
|
+
// effect rather than a write during render, because an effect never runs on the server:
|
|
406
|
+
// there is no `document` there, and `ssr.test.tsx` renders this provider without one to hold
|
|
407
|
+
// that.
|
|
408
|
+
useEffect(() => {
|
|
409
|
+
document.documentElement.lang = activeLocale;
|
|
410
|
+
}, [activeLocale]);
|
|
411
|
+
|
|
354
412
|
const setLocale = useCallback(
|
|
355
413
|
(next: string) => {
|
|
356
414
|
if (!codes.includes(next)) {
|
package/src/markers.test.ts
CHANGED
|
@@ -161,6 +161,8 @@ const MARKERS = [
|
|
|
161
161
|
"field-hint",
|
|
162
162
|
"field-label",
|
|
163
163
|
"field-label-text",
|
|
164
|
+
"field-messages",
|
|
165
|
+
"field-row",
|
|
164
166
|
"grid",
|
|
165
167
|
"heading",
|
|
166
168
|
"hubcard",
|
|
@@ -183,11 +185,14 @@ const MARKERS = [
|
|
|
183
185
|
"loading-state-spinner",
|
|
184
186
|
"login-brand",
|
|
185
187
|
"login-card",
|
|
188
|
+
"login-email",
|
|
186
189
|
"login-error",
|
|
187
190
|
"login-form",
|
|
191
|
+
"login-password",
|
|
188
192
|
"login-separator",
|
|
189
193
|
"login-separator-rule",
|
|
190
194
|
"login-sso",
|
|
195
|
+
"login-submit",
|
|
191
196
|
"login-title",
|
|
192
197
|
"login-view",
|
|
193
198
|
"markdown",
|
|
@@ -196,6 +201,9 @@ const MARKERS = [
|
|
|
196
201
|
"menu-item-check",
|
|
197
202
|
"menu-item-icon",
|
|
198
203
|
"menu-trigger",
|
|
204
|
+
"meter",
|
|
205
|
+
"meter-bar",
|
|
206
|
+
"meter-value",
|
|
199
207
|
"module-nav",
|
|
200
208
|
"module-nav-link",
|
|
201
209
|
"module-nav-list",
|
|
@@ -214,6 +222,8 @@ const MARKERS = [
|
|
|
214
222
|
"profile-card",
|
|
215
223
|
"profile-email",
|
|
216
224
|
"profile-role",
|
|
225
|
+
"quiet-actions",
|
|
226
|
+
"quiet-actions-slot",
|
|
217
227
|
"radio",
|
|
218
228
|
"radio-group",
|
|
219
229
|
"radio-group-legend",
|
|
@@ -254,6 +264,7 @@ const MARKERS = [
|
|
|
254
264
|
"user-menu-header",
|
|
255
265
|
"user-menu-identity",
|
|
256
266
|
"user-menu-role",
|
|
267
|
+
"user-menu-sign-out",
|
|
257
268
|
];
|
|
258
269
|
|
|
259
270
|
/**
|
|
@@ -572,20 +583,31 @@ describe("data-terp markers", () => {
|
|
|
572
583
|
|
|
573
584
|
it("injects the sheet from every module that owns a rule, or is reachable from one that does", () => {
|
|
574
585
|
// Twelve marker-rendering modules never call injectTerpStyles and do not need to: the
|
|
575
|
-
// package publishes ONE entry point and declares no `sideEffects`, so
|
|
576
|
-
// from it loads every module and two dozen of them inject. That
|
|
577
|
-
// property, and nothing asserted it — a `sideEffects: false`
|
|
578
|
-
// tree-shaking, would remove it silently and the first
|
|
579
|
-
// collapsing into one grid item. So the property
|
|
580
|
-
//
|
|
581
|
-
//
|
|
582
|
-
//
|
|
583
|
-
//
|
|
584
|
-
//
|
|
586
|
+
// package publishes ONE entry point into its modules and declares no `sideEffects`, so
|
|
587
|
+
// importing anything from it loads every module and two dozen of them inject. That
|
|
588
|
+
// guarantee is a packaging property, and nothing asserted it — a `sideEffects: false`
|
|
589
|
+
// added for bundle size, plus tree-shaking, would remove it silently and the first
|
|
590
|
+
// symptom would be Markdown's blocks collapsing into one grid item. So the property
|
|
591
|
+
// itself is what this pins.
|
|
592
|
+
// One way into this package's MODULES, which is the property. Two kinds of subpath do not
|
|
593
|
+
// weaken it. Data: `./layout.manifest.json` is JSON a tool reads out of node_modules, no
|
|
594
|
+
// module imports it, and importing JSON loads no JavaScript at all. And a code subpath that
|
|
595
|
+
// imports nothing from this package — `./testing`, the test-setup polyfill (ADR 0155) —
|
|
596
|
+
// loads no component and no rule, so it cannot render anything without the sheet. Asserted
|
|
597
|
+
// by what each further code entry imports rather than by key count, so that publishing a
|
|
598
|
+
// second way into the components is what fails, and a self-contained module is not.
|
|
585
599
|
const codeEntries = Object.entries(manifest.exports).filter(([, target]) =>
|
|
586
600
|
/\.(ts|tsx|js|mjs|cjs)$/.test(target),
|
|
587
601
|
);
|
|
588
|
-
expect(codeEntries)
|
|
602
|
+
expect(Object.fromEntries(codeEntries)["."]).toBe("./src/index.ts");
|
|
603
|
+
for (const [subpath, target] of codeEntries.filter(([key]) => key !== ".")) {
|
|
604
|
+
const text = sources[target.replace(/^\.\/src\//, "./")];
|
|
605
|
+
expect(text, `${subpath} points at ${target}, not a module in src/`).toBeDefined();
|
|
606
|
+
expect(
|
|
607
|
+
stripComments(text ?? ""),
|
|
608
|
+
`${subpath} imports from this package, which makes it a second way into its modules`,
|
|
609
|
+
).not.toMatch(/(?:from|import)\s*\(?\s*["']\.{1,2}\//);
|
|
610
|
+
}
|
|
589
611
|
expect(
|
|
590
612
|
"sideEffects" in manifest,
|
|
591
613
|
"declaring sideEffects would let a bundler drop the modules that inject the stylesheet",
|
package/src/ssr.test.tsx
CHANGED
|
@@ -4,6 +4,7 @@ import { describe, expect, it } from "vitest";
|
|
|
4
4
|
import { AppShell } from "./AppShell";
|
|
5
5
|
import { DataView } from "./dataview/DataView";
|
|
6
6
|
import type { DataViewRepository } from "./dataview/types";
|
|
7
|
+
import { LOCALE_EN, LOCALE_NL, LocaleProvider } from "./locale";
|
|
7
8
|
|
|
8
9
|
interface Row {
|
|
9
10
|
id: string;
|
|
@@ -40,4 +41,21 @@ describe("server rendering", () => {
|
|
|
40
41
|
expect(shell).toContain("Home");
|
|
41
42
|
expect(view).toContain('data-terp="dataview"');
|
|
42
43
|
});
|
|
44
|
+
|
|
45
|
+
it("renders a localised DataView without writing to a document that is not there", () => {
|
|
46
|
+
// LocaleProvider keeps <html lang> on the active locale, and there is no `document` on the
|
|
47
|
+
// server. The write lives in an effect, which server rendering never runs; moved into the
|
|
48
|
+
// render body it throws here. And the DataView's own copy is already Dutch in the markup,
|
|
49
|
+
// because its strings come from the same table the locale translates.
|
|
50
|
+
const view = renderToString(
|
|
51
|
+
<LocaleProvider locales={{ en: LOCALE_EN, nl: LOCALE_NL }} defaultLocale="nl">
|
|
52
|
+
<DataView
|
|
53
|
+
repository={repository}
|
|
54
|
+
columns={[{ id: "name", header: "Name", accessor: (row) => row.name }]}
|
|
55
|
+
/>
|
|
56
|
+
</LocaleProvider>,
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
expect(view).toContain('aria-label="Laden…"');
|
|
60
|
+
});
|
|
43
61
|
});
|