@terpjs/react-core 0.27.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 +4 -3
- 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 +265 -14
- package/src/styles.ts +424 -49
- 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/UserMenu.test.tsx
CHANGED
|
@@ -160,6 +160,29 @@ describe("UserMenu", () => {
|
|
|
160
160
|
expect(trigger?.tagName).toBe("BUTTON");
|
|
161
161
|
});
|
|
162
162
|
|
|
163
|
+
it("names the sign-out item for a caller that cannot read it", async () => {
|
|
164
|
+
// The second half of the same contract. `logout()` used to click the menuitem named
|
|
165
|
+
// "Sign out", which is a sentence in one language: an app that boots in any other locale
|
|
166
|
+
// failed its own conformance suite before it had any code of its own. The helper finds the
|
|
167
|
+
// item by marker path now, so what is pinned here is that the path lands on sign-out and on
|
|
168
|
+
// nothing else — with Settings present, so a part wrapped around the wrong item, or around
|
|
169
|
+
// both, fails rather than passing by being the only candidate.
|
|
170
|
+
stubAuthFetch();
|
|
171
|
+
render(
|
|
172
|
+
<TerpProvider baseUrl="https://api.test">
|
|
173
|
+
<LogInOnMount />
|
|
174
|
+
<UserMenu onSettings={() => undefined} />
|
|
175
|
+
</TerpProvider>,
|
|
176
|
+
);
|
|
177
|
+
fireEvent.click(await screen.findByRole("button", { name: /jane\.doe@example\.com/ }));
|
|
178
|
+
// The panel is portalled to document.body, so the path is resolved from the document.
|
|
179
|
+
const named = document.querySelectorAll(
|
|
180
|
+
'[data-terp="user-menu-sign-out"] [data-terp="menu-item"]',
|
|
181
|
+
);
|
|
182
|
+
expect(named).toHaveLength(1);
|
|
183
|
+
expect(named[0]).toBe(screen.getByRole("menuitem", { name: "Sign out" }));
|
|
184
|
+
});
|
|
185
|
+
|
|
163
186
|
it("signs out via the menu (revokes the token server-side)", async () => {
|
|
164
187
|
const fetchMock = stubAuthFetch();
|
|
165
188
|
render(
|
package/src/UserMenu.tsx
CHANGED
|
@@ -94,7 +94,15 @@ export function UserMenu({ collapsed = false, onSettings, defaultOpen }: UserMen
|
|
|
94
94
|
}}
|
|
95
95
|
/>
|
|
96
96
|
)}
|
|
97
|
-
|
|
97
|
+
{/* A named part, for the one caller that has to find sign-out without reading it:
|
|
98
|
+
`@terpjs/conformance`'s logout helper, which runs in whatever language the app
|
|
99
|
+
ships, so the item's accessible name cannot be its handle. `menu-item` stays on
|
|
100
|
+
the button because that is its styling, and one element carries one marker.
|
|
101
|
+
The part is display: contents, so the menu's grid still spaces the item and its
|
|
102
|
+
roving focus still finds it among the menuitems. */}
|
|
103
|
+
<div data-terp="user-menu-sign-out">
|
|
104
|
+
<MenuItem label={strings.signOut} icon={<Icon name="logout" />} onSelect={() => void auth.logout()} />
|
|
105
|
+
</div>
|
|
98
106
|
</>
|
|
99
107
|
)}
|
|
100
108
|
</Menu>
|
|
@@ -102,7 +102,7 @@ export function AuditLogAdmin() {
|
|
|
102
102
|
items={[
|
|
103
103
|
{ label: strings.targetColumn, value: `${event.target_type} ${event.target_id}` },
|
|
104
104
|
{ label: strings.actorColumn, value: event.actor_id ?? "—" },
|
|
105
|
-
{ label:
|
|
105
|
+
{ label: strings.requestLabel, value: event.request_id ?? "—" },
|
|
106
106
|
]}
|
|
107
107
|
/>
|
|
108
108
|
{event.payload !== null && (
|
package/src/admin/admin.test.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
import { RouterProvider, createMemoryHistory } from "@tanstack/react-router";
|
|
3
|
-
import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
3
|
+
import { act, cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
|
4
4
|
import { useEffect } from "react";
|
|
5
5
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
6
6
|
import type { ModuleManifest } from "@terpjs/contract";
|
|
@@ -9,6 +9,8 @@ import type { ComponentType } from "react";
|
|
|
9
9
|
import { withAdminArea } from "../bootstrap";
|
|
10
10
|
import type { AdminAreaSections } from "../bootstrap";
|
|
11
11
|
import { formatDateTime } from "../format";
|
|
12
|
+
import { LOCALE_EN, LOCALE_NL, LocaleProvider } from "../locale";
|
|
13
|
+
import type { LocaleProviderProps } from "../locale";
|
|
12
14
|
import { buildAppRouter } from "../router";
|
|
13
15
|
import { Page } from "../Page";
|
|
14
16
|
import { TerpProvider, useAuth } from "../TerpProvider";
|
|
@@ -47,7 +49,7 @@ describe("withAdminArea", () => {
|
|
|
47
49
|
"/admin/groups/new",
|
|
48
50
|
"/admin/groups/$groupId",
|
|
49
51
|
]));
|
|
50
|
-
expect(admin?.nav?.[0]?.label).
|
|
52
|
+
expect(admin?.nav?.[0]?.label).toEqual({ framework: "admin" });
|
|
51
53
|
expect(views.TerpAdminHub).toBeDefined();
|
|
52
54
|
});
|
|
53
55
|
|
|
@@ -115,7 +117,7 @@ describe("withAdminArea", () => {
|
|
|
115
117
|
groups: false,
|
|
116
118
|
});
|
|
117
119
|
const admin = manifests.find((manifest) => manifest.name === "terp-admin");
|
|
118
|
-
expect(admin?.nav?.[0]?.label).
|
|
120
|
+
expect(admin?.nav?.[0]?.label).toEqual({ framework: "admin" });
|
|
119
121
|
expect(admin?.routes.map((route) => route.path)).toEqual([
|
|
120
122
|
"/admin",
|
|
121
123
|
"/admin/audit",
|
|
@@ -395,10 +397,14 @@ function stubAdminFetch() {
|
|
|
395
397
|
return fetchMock;
|
|
396
398
|
}
|
|
397
399
|
|
|
400
|
+
/** An English-source app opened in Dutch. */
|
|
401
|
+
const DUTCH = { locales: { en: LOCALE_EN, nl: LOCALE_NL }, defaultLocale: "nl" };
|
|
402
|
+
|
|
398
403
|
function renderAdminApp(
|
|
399
404
|
initialPath: string,
|
|
400
405
|
roleRank = 30,
|
|
401
406
|
adminArea: boolean | AdminAreaSections = true,
|
|
407
|
+
locale?: Pick<LocaleProviderProps, "locales" | "defaultLocale">,
|
|
402
408
|
) {
|
|
403
409
|
const manifests: ModuleManifest[] = [
|
|
404
410
|
{ name: "notes", routes: [{ path: "/", view: "NotesList" }], nav: [] },
|
|
@@ -427,13 +433,22 @@ function renderAdminApp(
|
|
|
427
433
|
});
|
|
428
434
|
});
|
|
429
435
|
}
|
|
430
|
-
|
|
436
|
+
const app = (
|
|
431
437
|
<TerpProvider baseUrl="https://api.test">
|
|
432
438
|
<ToastProvider>
|
|
433
439
|
<LogInOnMount />
|
|
434
440
|
<RouterProvider router={router} />
|
|
435
441
|
</ToastProvider>
|
|
436
|
-
</TerpProvider
|
|
442
|
+
</TerpProvider>
|
|
443
|
+
);
|
|
444
|
+
render(
|
|
445
|
+
locale === undefined ? (
|
|
446
|
+
app
|
|
447
|
+
) : (
|
|
448
|
+
<LocaleProvider locales={locale.locales} defaultLocale={locale.defaultLocale}>
|
|
449
|
+
{app}
|
|
450
|
+
</LocaleProvider>
|
|
451
|
+
),
|
|
437
452
|
);
|
|
438
453
|
return { fetchMock, router };
|
|
439
454
|
}
|
|
@@ -863,6 +878,43 @@ describe("the packaged admin area", () => {
|
|
|
863
878
|
}
|
|
864
879
|
});
|
|
865
880
|
|
|
881
|
+
it("speaks the app's language on the audit screen, its table and expanded row included", async () => {
|
|
882
|
+
// The framework's own screen, under the framework's own Dutch catalog. Its title was Dutch
|
|
883
|
+
// and everything its DataView drew was not — the result count, the row controls — because
|
|
884
|
+
// DataView's strings were a table the locale could not reach; and the expanded row carried
|
|
885
|
+
// one label written straight into the screen. Asserted here rather than on a bare DataView
|
|
886
|
+
// because this is where an app's user actually met the two languages side by side.
|
|
887
|
+
renderAdminApp("/admin/audit", 30, true, DUTCH);
|
|
888
|
+
await screen.findByRole("heading", { level: 1, name: "Auditlog" });
|
|
889
|
+
expect(await screen.findByText("1–1 van 1 resultaten")).toBeInTheDocument();
|
|
890
|
+
|
|
891
|
+
fireEvent.click(await screen.findByRole("button", { name: "Rij uitklappen" }));
|
|
892
|
+
expect(await screen.findByText("Verzoek")).toBeInTheDocument();
|
|
893
|
+
expect(screen.queryByText("Request")).toBeNull();
|
|
894
|
+
expect(screen.queryByText(/results/)).toBeNull();
|
|
895
|
+
});
|
|
896
|
+
|
|
897
|
+
it.each([
|
|
898
|
+
["nl", "nl", "Hoofdnavigatie", "Beheer"],
|
|
899
|
+
["nl", "en", "Primary", "Admin"],
|
|
900
|
+
["en", "nl", "Hoofdnavigatie", "Beheer"],
|
|
901
|
+
])(
|
|
902
|
+
"labels the sidebar entry in the active locale (source %s, opened in %s)",
|
|
903
|
+
async (source, active, navigationName, label) => {
|
|
904
|
+
// The first row is the case a `{ id, message }` descriptor could not serve. In an app whose
|
|
905
|
+
// source locale is Dutch, a descriptor opened in Dutch renders its `message` without
|
|
906
|
+
// consulting any catalog — and a framework-authored message is English. The entry names
|
|
907
|
+
// the framework string instead, so it is the active table's word whichever locale is the
|
|
908
|
+
// source: Dutch where the app opens in Dutch, English where it opens in English.
|
|
909
|
+
const locales =
|
|
910
|
+
source === "nl" ? { nl: LOCALE_NL, en: LOCALE_EN } : { en: LOCALE_EN, nl: LOCALE_NL };
|
|
911
|
+
renderAdminApp("/admin", 30, true, { locales, defaultLocale: active });
|
|
912
|
+
const sidebar = await screen.findByRole("navigation", { name: navigationName });
|
|
913
|
+
expect(await within(sidebar).findByRole("link", { name: label })).toBeInTheDocument();
|
|
914
|
+
expect(within(sidebar).getAllByRole("link")).toHaveLength(1);
|
|
915
|
+
},
|
|
916
|
+
);
|
|
917
|
+
|
|
866
918
|
it("keeps the audit payload's scroll container reachable by keyboard", async () => {
|
|
867
919
|
// The gate for the SC 2.1.1 fix, and it has to be here rather than in the workbench.
|
|
868
920
|
// `code-block` declares `overflow-x: auto`, so a wide payload is a scroll container, and a
|
package/src/admin/module.tsx
CHANGED
|
@@ -13,11 +13,14 @@ import { UserDetail } from "./UserDetail";
|
|
|
13
13
|
import { UsersAdmin } from "./UsersAdmin";
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* The packaged administration area every Terp app ships: one admin-gated
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
16
|
+
* The packaged administration area every Terp app ships: one admin-gated sidebar entry
|
|
17
|
+
* opening a hub (`/admin`) whose cards lead to the users, groups and audit-log overviews —
|
|
18
|
+
* the UI over the base-profile capabilities the backend mounts in every app (users,
|
|
19
|
+
* groups + access, audit; ADR 0074). The whole area is `role: "admin"` end to end; the
|
|
20
|
+
* backend re-checks every call regardless.
|
|
21
|
+
*
|
|
22
|
+
* The entry's label names the `admin` framework string rather than spelling it out, so it
|
|
23
|
+
* reads "Beheer" under `LOCALE_NL` whatever the app's own source locale is (ADR 0153).
|
|
21
24
|
*
|
|
22
25
|
* `renderTerpApp` injects it by default (`adminArea: false` opts out; an app
|
|
23
26
|
* manifest claiming one of its paths overrides that screen). An L2 composition
|
|
@@ -38,7 +41,7 @@ export const adminModule: TerpModule = {
|
|
|
38
41
|
{ path: "/admin/audit", view: "TerpAdminAudit", role: "admin" },
|
|
39
42
|
{ path: "/admin/access", view: "TerpAdminAccess", role: "admin" },
|
|
40
43
|
],
|
|
41
|
-
nav: [{ label: "
|
|
44
|
+
nav: [{ label: { framework: "admin" }, to: "/admin", icon: "shield", role: "admin" }],
|
|
42
45
|
}),
|
|
43
46
|
views: {
|
|
44
47
|
TerpAdminHub: AdminHub,
|
package/src/bootstrap.test.tsx
CHANGED
|
@@ -122,7 +122,7 @@ describe("renderTerpApp", () => {
|
|
|
122
122
|
screen.getByText("That value table is published and cannot be edited."),
|
|
123
123
|
).toBeInTheDocument(),
|
|
124
124
|
);
|
|
125
|
-
// And an override of a built-in code wins over
|
|
125
|
+
// And an override of a built-in code wins over the built-in wording, which is the
|
|
126
126
|
// half that would still look fine if the map were merged the wrong way round.
|
|
127
127
|
expect(screen.getByText("Niets gevonden.")).toBeInTheDocument();
|
|
128
128
|
expect(screen.queryByText("This item could not be found.")).not.toBeInTheDocument();
|
package/src/bootstrap.tsx
CHANGED
|
@@ -132,7 +132,8 @@ export interface RenderTerpAppOptions {
|
|
|
132
132
|
headerActions?: ReactNode;
|
|
133
133
|
/**
|
|
134
134
|
* Per-code overrides and additions for the error copy an app shows
|
|
135
|
-
* ({@link ErrorMessagesProvider}),
|
|
135
|
+
* ({@link ErrorMessagesProvider}), which win over the platform's own wording for its codes
|
|
136
|
+
* — the `errorCode*` keys of the active locale's `TerpStrings`.
|
|
136
137
|
*
|
|
137
138
|
* The provider, the hook and the default map were all exported and there was no way to
|
|
138
139
|
* register a map from the one-call bootstrap, which owns everything between the root and
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
|
3
3
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
4
|
|
|
5
|
+
import { LOCALE_EN, LOCALE_NL, LanguageSwitcher, LocaleProvider } from "../locale";
|
|
5
6
|
import { DataView } from "./DataView";
|
|
7
|
+
import { DataViewPagination } from "./DataViewPagination";
|
|
6
8
|
import { InMemoryDataViewRepository } from "./repositories/InMemoryDataViewRepository";
|
|
7
9
|
import { InMemoryViewStateRepository } from "./repositories/viewState";
|
|
8
10
|
import type { DataViewColumn, DataViewQuery, DataViewRepository } from "./types";
|
|
@@ -584,3 +586,71 @@ describe("DataView search scope", () => {
|
|
|
584
586
|
}
|
|
585
587
|
});
|
|
586
588
|
});
|
|
589
|
+
|
|
590
|
+
describe("DataView localisation", () => {
|
|
591
|
+
// DataView's strings are framework strings: the `dataView*` keys of TerpStrings. They used to
|
|
592
|
+
// be a separate English table that only the per-instance `strings` prop could change, so a
|
|
593
|
+
// DataView under a Dutch LocaleProvider still said "Search…" and "1–4 of 4 results" — the
|
|
594
|
+
// framework's own admin screens included — while every locale gate reported the catalog
|
|
595
|
+
// complete.
|
|
596
|
+
afterEach(() => {
|
|
597
|
+
// The switcher persists its choice; the next test must not open in it.
|
|
598
|
+
window.localStorage.clear();
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
it("speaks the app's locale without a strings prop, and follows a switch", async () => {
|
|
602
|
+
render(
|
|
603
|
+
<LocaleProvider locales={{ en: LOCALE_EN, nl: LOCALE_NL }}>
|
|
604
|
+
<LanguageSwitcher />
|
|
605
|
+
<DataView repository={inMemoryRepo()} columns={COLUMNS} />
|
|
606
|
+
</LocaleProvider>,
|
|
607
|
+
);
|
|
608
|
+
expect(await screen.findByText("1–4 of 4 results")).toBeInTheDocument();
|
|
609
|
+
|
|
610
|
+
fireEvent.click(screen.getByRole("button", { name: "Language" }));
|
|
611
|
+
fireEvent.click(screen.getByRole("menuitemradio", { name: "Nederlands" }));
|
|
612
|
+
|
|
613
|
+
expect(await screen.findByText("1–4 van 4 resultaten")).toBeInTheDocument();
|
|
614
|
+
expect(screen.getByRole("searchbox", { name: "Zoeken…" })).toHaveAttribute(
|
|
615
|
+
"placeholder",
|
|
616
|
+
"Zoeken…",
|
|
617
|
+
);
|
|
618
|
+
expect(screen.getByRole("button", { name: "Rijen per pagina" })).toBeInTheDocument();
|
|
619
|
+
expect(screen.getByRole("button", { name: "Weergaveopties" })).toBeInTheDocument();
|
|
620
|
+
expect(screen.queryByText(/results/)).not.toBeInTheDocument();
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
it("lets a per-instance string win over the locale", async () => {
|
|
624
|
+
render(
|
|
625
|
+
<LocaleProvider locales={{ en: LOCALE_EN, nl: LOCALE_NL }} defaultLocale="nl">
|
|
626
|
+
<DataView
|
|
627
|
+
repository={inMemoryRepo()}
|
|
628
|
+
columns={COLUMNS}
|
|
629
|
+
strings={{ searchPlaceholder: "Zoek een ticket…" }}
|
|
630
|
+
/>
|
|
631
|
+
</LocaleProvider>,
|
|
632
|
+
);
|
|
633
|
+
// The rest of the view still follows the locale; only the overridden key does not.
|
|
634
|
+
expect(await screen.findByText("1–4 van 4 resultaten")).toBeInTheDocument();
|
|
635
|
+
expect(screen.getByRole("searchbox", { name: "Zoek een ticket…" })).toBeInTheDocument();
|
|
636
|
+
expect(screen.queryByRole("searchbox", { name: "Zoeken…" })).not.toBeInTheDocument();
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
it("localises a sub-component rendered outside any DataView", () => {
|
|
640
|
+
// The parts are exported for hand-built compositions, and outside a DataView there is no
|
|
641
|
+
// provider above them. The context's default used to be a finished English set with a
|
|
642
|
+
// resolver that ignored the locale, so this path stayed English whatever the provider did.
|
|
643
|
+
render(
|
|
644
|
+
<LocaleProvider locales={{ en: LOCALE_EN, nl: LOCALE_NL }} defaultLocale="nl">
|
|
645
|
+
<DataViewPagination
|
|
646
|
+
pagination={{ pageIndex: 1, pageSize: 10 }}
|
|
647
|
+
totalCount={45}
|
|
648
|
+
onPaginationChange={() => {}}
|
|
649
|
+
/>
|
|
650
|
+
</LocaleProvider>,
|
|
651
|
+
);
|
|
652
|
+
expect(screen.getByText("11–20 van 45 resultaten")).toBeInTheDocument();
|
|
653
|
+
expect(screen.getByText("Pagina 2 van 5")).toBeInTheDocument();
|
|
654
|
+
expect(screen.getByRole("button", { name: "Volgende pagina" })).toBeInTheDocument();
|
|
655
|
+
});
|
|
656
|
+
});
|
package/src/dataview/README.md
CHANGED
|
@@ -152,9 +152,11 @@ versioned envelope; corrupt data falls back to defaults) and
|
|
|
152
152
|
`density="comfortable"` cannot make one view comfortable inside a compact subtree.
|
|
153
153
|
- **Variants**: `variant="embedded"` renders a plain compact view (no view toggle, no
|
|
154
154
|
page-size selector, no pagination footer, all rows) for panels/detail sections.
|
|
155
|
-
- **i18n**: no hard-coded user-facing strings
|
|
156
|
-
|
|
157
|
-
|
|
155
|
+
- **i18n**: no hard-coded user-facing strings. The defaults are framework strings — the
|
|
156
|
+
`dataView*` keys of `TerpStrings` — so a DataView follows the app's `LocaleProvider` like
|
|
157
|
+
the rest of the chrome, and `LOCALE_NL` translates them. The `strings` prop overrides a key
|
|
158
|
+
for one instance and wins over the locale; every value is a `UiText` resolved through the
|
|
159
|
+
active resolver.
|
|
158
160
|
|
|
159
161
|
## Files
|
|
160
162
|
|
package/src/dataview/index.ts
CHANGED
|
@@ -32,7 +32,7 @@ export type {
|
|
|
32
32
|
DataViewSearchScope,
|
|
33
33
|
DataViewStrings,
|
|
34
34
|
} from "./types";
|
|
35
|
-
export {
|
|
35
|
+
export { emptyDataViewState } from "./types";
|
|
36
36
|
|
|
37
37
|
export { InMemoryDataViewRepository } from "./repositories/InMemoryDataViewRepository";
|
|
38
38
|
export type { InMemoryDataViewRepositoryOptions } from "./repositories/InMemoryDataViewRepository";
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { createContext, useCallback, useContext } from "react";
|
|
1
|
+
import { createContext, useCallback, useContext, useMemo } from "react";
|
|
2
2
|
import type { ReactNode } from "react";
|
|
3
3
|
|
|
4
4
|
import { useFormatDate } from "../format";
|
|
5
5
|
import { injectTerpStyles } from "../styles";
|
|
6
6
|
import { Menu, MenuItem } from "../ui/Menu";
|
|
7
|
-
import { useUiText } from "../uiText";
|
|
8
|
-
import type { ResolveUiText, UiText } from "../uiText";
|
|
9
|
-
import {
|
|
7
|
+
import { useStrings, useUiText } from "../uiText";
|
|
8
|
+
import type { ResolveUiText, TerpStrings, UiText } from "../uiText";
|
|
9
|
+
import { formatDataViewString } from "./types";
|
|
10
10
|
import type { DataViewStrings } from "./types";
|
|
11
11
|
|
|
12
12
|
injectTerpStyles();
|
|
@@ -19,15 +19,77 @@ export interface DataViewTextApi {
|
|
|
19
19
|
format: (text: UiText, values: Record<string, string | number>) => string;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
/**
|
|
23
|
+
* The DataView's strings, read out of the framework table.
|
|
24
|
+
*
|
|
25
|
+
* They used to be a second table of English defaults that only the per-instance `strings` prop
|
|
26
|
+
* could change. A locale catalog is typed against `TerpStrings`, so it had nowhere to put a
|
|
27
|
+
* DataView translation, and the completeness check that refuses a half-translated shell walked
|
|
28
|
+
* a table these strings were not in: under a Dutch locale every DataView rendered its toolbar
|
|
29
|
+
* and footer in English, the framework's own admin screens included, with every gate green.
|
|
30
|
+
*
|
|
31
|
+
* Written out rather than derived from the key names, so a missing or misspelt key is a type
|
|
32
|
+
* error here and not an `undefined` on screen.
|
|
33
|
+
*/
|
|
34
|
+
function dataViewStrings(strings: TerpStrings): DataViewStrings {
|
|
35
|
+
return {
|
|
36
|
+
searchPlaceholder: strings.dataViewSearchPlaceholder,
|
|
37
|
+
clearSearch: strings.dataViewClearSearch,
|
|
38
|
+
clearFilters: strings.dataViewClearFilters,
|
|
39
|
+
viewOptions: strings.dataViewViewOptions,
|
|
40
|
+
columns: strings.dataViewColumns,
|
|
41
|
+
moveUp: strings.dataViewMoveUp,
|
|
42
|
+
moveDown: strings.dataViewMoveDown,
|
|
43
|
+
tableView: strings.dataViewTableView,
|
|
44
|
+
cardView: strings.dataViewCardView,
|
|
45
|
+
pageSize: strings.dataViewPageSize,
|
|
46
|
+
resultsRange: strings.dataViewResultsRange,
|
|
47
|
+
pageOf: strings.dataViewPageOf,
|
|
48
|
+
firstPage: strings.dataViewFirstPage,
|
|
49
|
+
previousPage: strings.dataViewPreviousPage,
|
|
50
|
+
nextPage: strings.dataViewNextPage,
|
|
51
|
+
lastPage: strings.dataViewLastPage,
|
|
52
|
+
selectAllPage: strings.dataViewSelectAllPage,
|
|
53
|
+
selectRow: strings.dataViewSelectRow,
|
|
54
|
+
selected: strings.dataViewSelected,
|
|
55
|
+
selectAllResults: strings.dataViewSelectAllResults,
|
|
56
|
+
clearSelection: strings.dataViewClearSelection,
|
|
57
|
+
moreActions: strings.dataViewMoreActions,
|
|
58
|
+
actions: strings.dataViewActions,
|
|
59
|
+
openRow: strings.dataViewOpenRow,
|
|
60
|
+
expandRow: strings.dataViewExpandRow,
|
|
61
|
+
collapseRow: strings.dataViewCollapseRow,
|
|
62
|
+
empty: strings.dataViewEmpty,
|
|
63
|
+
loading: strings.dataViewLoading,
|
|
64
|
+
refreshing: strings.dataViewRefreshing,
|
|
65
|
+
errorTitle: strings.dataViewErrorTitle,
|
|
66
|
+
resizeColumn: strings.dataViewResizeColumn,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A DataView's per-instance overrides, and nothing else.
|
|
72
|
+
*
|
|
73
|
+
* The context carries only what the instance said, never a finished set of strings, so there is
|
|
74
|
+
* no default value to go stale: a sub-component rendered on its own — a `DataViewPagination`
|
|
75
|
+
* outside any `DataView` — reads the active locale exactly as one inside does. The context's
|
|
76
|
+
* default used to BE a finished set, English strings and a resolver that ignored the locale, so
|
|
77
|
+
* a fix to the provider alone would have left every standalone part exactly as it was.
|
|
78
|
+
*/
|
|
79
|
+
const DataViewOverridesContext = createContext<Partial<DataViewStrings> | undefined>(undefined);
|
|
28
80
|
|
|
29
81
|
export function useDataViewText(): DataViewTextApi {
|
|
30
|
-
|
|
82
|
+
const overrides = useContext(DataViewOverridesContext);
|
|
83
|
+
const framework = useStrings();
|
|
84
|
+
const resolve = useUiText();
|
|
85
|
+
return useMemo<DataViewTextApi>(
|
|
86
|
+
() => ({
|
|
87
|
+
strings: { ...dataViewStrings(framework), ...overrides },
|
|
88
|
+
resolve,
|
|
89
|
+
format: (text, values) => formatDataViewString(resolve(text), values),
|
|
90
|
+
}),
|
|
91
|
+
[framework, overrides, resolve],
|
|
92
|
+
);
|
|
31
93
|
}
|
|
32
94
|
|
|
33
95
|
/**
|
|
@@ -66,14 +128,9 @@ export function DataViewTextProvider({
|
|
|
66
128
|
overrides?: Partial<DataViewStrings>;
|
|
67
129
|
children: ReactNode;
|
|
68
130
|
}) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
strings,
|
|
73
|
-
resolve,
|
|
74
|
-
format: (text, values) => formatDataViewString(resolve(text), values),
|
|
75
|
-
};
|
|
76
|
-
return <DataViewTextContext.Provider value={api}>{children}</DataViewTextContext.Provider>;
|
|
131
|
+
return (
|
|
132
|
+
<DataViewOverridesContext.Provider value={overrides}>{children}</DataViewOverridesContext.Provider>
|
|
133
|
+
);
|
|
77
134
|
}
|
|
78
135
|
|
|
79
136
|
/**
|
package/src/dataview/types.ts
CHANGED
|
@@ -199,7 +199,15 @@ export interface DataViewSearchScope {
|
|
|
199
199
|
broadenedLabel: UiText;
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
/**
|
|
202
|
+
/**
|
|
203
|
+
* Every user-facing string the DataView renders, overridable per instance through its
|
|
204
|
+
* `strings` prop.
|
|
205
|
+
*
|
|
206
|
+
* The defaults are not here. Each key is read from the active locale's `TerpStrings`, under
|
|
207
|
+
* the same name with a `dataView` prefix (`searchPlaceholder` is `dataViewSearchPlaceholder`),
|
|
208
|
+
* so a DataView speaks whatever language the app's `LocaleProvider` does and a catalog that
|
|
209
|
+
* leaves one of them out is refused. A per-instance override wins over the locale.
|
|
210
|
+
*/
|
|
203
211
|
export interface DataViewStrings {
|
|
204
212
|
searchPlaceholder: UiText;
|
|
205
213
|
clearSearch: UiText;
|
|
@@ -234,40 +242,6 @@ export interface DataViewStrings {
|
|
|
234
242
|
resizeColumn: UiText;
|
|
235
243
|
}
|
|
236
244
|
|
|
237
|
-
export const DEFAULT_DATA_VIEW_STRINGS: DataViewStrings = {
|
|
238
|
-
searchPlaceholder: "Search…",
|
|
239
|
-
clearSearch: "Clear search",
|
|
240
|
-
clearFilters: "Clear filters",
|
|
241
|
-
viewOptions: "View options",
|
|
242
|
-
columns: "Columns",
|
|
243
|
-
moveUp: "Move up",
|
|
244
|
-
moveDown: "Move down",
|
|
245
|
-
tableView: "Table view",
|
|
246
|
-
cardView: "Card view",
|
|
247
|
-
pageSize: "Rows per page",
|
|
248
|
-
resultsRange: "{from}–{to} of {total} results",
|
|
249
|
-
pageOf: "Page {page} of {pages}",
|
|
250
|
-
firstPage: "First page",
|
|
251
|
-
previousPage: "Previous page",
|
|
252
|
-
nextPage: "Next page",
|
|
253
|
-
lastPage: "Last page",
|
|
254
|
-
selectAllPage: "Select all rows on this page",
|
|
255
|
-
selectRow: "Select row",
|
|
256
|
-
selected: "{count} selected",
|
|
257
|
-
selectAllResults: "Select all {total} results",
|
|
258
|
-
clearSelection: "Clear selection",
|
|
259
|
-
moreActions: "More actions",
|
|
260
|
-
actions: "Actions",
|
|
261
|
-
openRow: "Open details: {label}",
|
|
262
|
-
expandRow: "Expand row",
|
|
263
|
-
collapseRow: "Collapse row",
|
|
264
|
-
empty: "Nothing to show.",
|
|
265
|
-
loading: "Loading…",
|
|
266
|
-
refreshing: "Refreshing…",
|
|
267
|
-
errorTitle: "Could not load data.",
|
|
268
|
-
resizeColumn: "Resize column",
|
|
269
|
-
};
|
|
270
|
-
|
|
271
245
|
/** Tiny `{placeholder}` formatter for the countable strings above. */
|
|
272
246
|
export function formatDataViewString(
|
|
273
247
|
template: string,
|
|
@@ -4,6 +4,7 @@ import { afterEach, describe, expect, it } from "vitest";
|
|
|
4
4
|
|
|
5
5
|
import { ErrorState } from "./ErrorState";
|
|
6
6
|
import { ErrorMessagesProvider, useErrorMessage } from "./errorMessages";
|
|
7
|
+
import { LOCALE_EN, LOCALE_NL, LocaleProvider } from "./locale";
|
|
7
8
|
import { UiTextProvider } from "./uiText";
|
|
8
9
|
import { ApiError } from "./unwrap";
|
|
9
10
|
|
|
@@ -57,6 +58,26 @@ describe("useErrorMessage", () => {
|
|
|
57
58
|
});
|
|
58
59
|
});
|
|
59
60
|
|
|
61
|
+
describe("useErrorMessage under a locale", () => {
|
|
62
|
+
it("words the platform's codes in the active locale, and an app's map still wins", () => {
|
|
63
|
+
// The built-in wording used to be an English map that was the context's default, and a
|
|
64
|
+
// plain string resolves as-is, so these two read in English under the Dutch catalog while
|
|
65
|
+
// that catalog was reported complete.
|
|
66
|
+
render(
|
|
67
|
+
<LocaleProvider locales={{ en: LOCALE_EN, nl: LOCALE_NL }} defaultLocale="nl">
|
|
68
|
+
<ErrorMessagesProvider messages={{ not_found: "Geen factuur met dit nummer." }}>
|
|
69
|
+
<Probe error={new ApiError("x", { code: "permission_denied", status: 403 })} />
|
|
70
|
+
<Probe error={new ApiError("x", { code: "not_found", status: 404 })} />
|
|
71
|
+
</ErrorMessagesProvider>
|
|
72
|
+
</LocaleProvider>,
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const statuses = screen.getAllByRole("status");
|
|
76
|
+
expect(statuses[0]).toHaveTextContent("Je hebt geen toestemming om dit te doen.");
|
|
77
|
+
expect(statuses[1]).toHaveTextContent("Geen factuur met dit nummer.");
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
60
81
|
describe("ErrorState with coded errors", () => {
|
|
61
82
|
it("prefers the mapped message over the backend detail for known codes", () => {
|
|
62
83
|
render(
|
package/src/errorMessages.tsx
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createContext, useCallback, useContext, useMemo } from "react";
|
|
2
2
|
import type { ReactNode } from "react";
|
|
3
3
|
|
|
4
|
-
import { useUiText } from "./uiText";
|
|
5
|
-
import type { UiText } from "./uiText";
|
|
4
|
+
import { useStrings, useUiText } from "./uiText";
|
|
5
|
+
import type { TerpStrings, UiText } from "./uiText";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Client-owned messages for the platform's stable error codes.
|
|
@@ -19,22 +19,29 @@ import type { UiText } from "./uiText";
|
|
|
19
19
|
export type ErrorMessages = Record<string, UiText>;
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* The wording for the core `AppError` taxonomy, read from the active locale.
|
|
23
|
+
*
|
|
24
|
+
* These were an exported map of English strings, the context's default value, so a plain
|
|
25
|
+
* string resolved as-is and a `permission_denied` read "You do not have permission to do this."
|
|
26
|
+
* under every locale — while the Dutch catalog was reported complete, because the check walks
|
|
27
|
+
* `TerpStrings` and these were not in it. They are `errorCode*` keys of that table now, so a
|
|
28
|
+
* locale catalog translates them and a non-English one that leaves one out is refused.
|
|
25
29
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
function builtInErrorMessages(strings: TerpStrings): ErrorMessages {
|
|
31
|
+
return {
|
|
32
|
+
bad_request: strings.errorCodeBadRequest,
|
|
33
|
+
validation_failed: strings.errorCodeValidationFailed,
|
|
34
|
+
invalid_token: strings.errorCodeInvalidToken,
|
|
35
|
+
authentication_required: strings.errorCodeAuthenticationRequired,
|
|
36
|
+
permission_denied: strings.errorCodePermissionDenied,
|
|
37
|
+
not_found: strings.errorCodeNotFound,
|
|
38
|
+
conflict: strings.errorCodeConflict,
|
|
39
|
+
stale_data: strings.errorCodeStaleData,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
36
42
|
|
|
37
|
-
|
|
43
|
+
/** The app's own map only; the built-in wording is read from the locale at every use. */
|
|
44
|
+
const ErrorMessagesContext = createContext<ErrorMessages>({});
|
|
38
45
|
|
|
39
46
|
export interface ErrorMessagesProviderProps {
|
|
40
47
|
/** Per-code overrides and additions, merged over any outer provider's map. */
|
|
@@ -44,8 +51,9 @@ export interface ErrorMessagesProviderProps {
|
|
|
44
51
|
|
|
45
52
|
/**
|
|
46
53
|
* Override or extend the code→message map for a subtree. Wrap the app once to
|
|
47
|
-
*
|
|
48
|
-
*
|
|
54
|
+
* register module-specific codes, or to reword a built-in one — its translated
|
|
55
|
+
* wording already comes from the active locale; nests, so a module can add its
|
|
56
|
+
* own codes without touching the app shell.
|
|
49
57
|
*/
|
|
50
58
|
export function ErrorMessagesProvider({ messages, children }: ErrorMessagesProviderProps) {
|
|
51
59
|
const parent = useContext(ErrorMessagesContext);
|
|
@@ -60,8 +68,14 @@ export function ErrorMessagesProvider({ messages, children }: ErrorMessagesProvi
|
|
|
60
68
|
* property, as carried by `ApiError` thrown from `unwrap`.
|
|
61
69
|
*/
|
|
62
70
|
export function useErrorMessage(): (error: unknown) => string | null {
|
|
63
|
-
const
|
|
71
|
+
const appMessages = useContext(ErrorMessagesContext);
|
|
72
|
+
const strings = useStrings();
|
|
64
73
|
const resolve = useUiText();
|
|
74
|
+
// The app's map over the built-in wording, so an app still overrides a platform code.
|
|
75
|
+
const messages = useMemo(
|
|
76
|
+
() => ({ ...builtInErrorMessages(strings), ...appMessages }),
|
|
77
|
+
[strings, appMessages],
|
|
78
|
+
);
|
|
65
79
|
return useCallback(
|
|
66
80
|
(error: unknown) => {
|
|
67
81
|
if (error === null || typeof error !== "object") {
|