@terpjs/react-core 0.35.0 → 0.36.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/package.json +10 -10
- package/src/TerpProvider.tsx +25 -3
- package/src/TerpProvider.unmount.test.tsx +100 -0
- package/src/dataview/DataView.test.tsx +22 -6
- package/src/dataview/DataViewToolbar.tsx +5 -1
- package/src/errorMessages.test.tsx +15 -0
- package/src/errorMessages.tsx +2 -0
- package/src/locale.tsx +2 -0
- package/src/ui/Combobox.tsx +2 -3
- package/src/ui/DatePicker.test.tsx +64 -8
- package/src/ui/DatePicker.tsx +51 -4
- package/src/uiText.test.tsx +25 -2
- package/src/uiText.tsx +6 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terpjs/react-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Terp React stack core — typed @terpjs/contract client provider, auth session, capability gates, TanStack Router adapter, app shell, page archetypes, DataView and token-styled UI primitives. First frontend stack; see README.md for the component catalog.",
|
|
6
6
|
"exports": {
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"test": "vitest run"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@tanstack/react-router": "^1.170.
|
|
18
|
-
"@terpjs/contract": "^0.
|
|
17
|
+
"@tanstack/react-router": "^1.170.46",
|
|
18
|
+
"@terpjs/contract": "^0.36.0"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
21
|
"react": "^19.0.0",
|
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
"@testing-library/react": "^16.3.3",
|
|
27
27
|
"@types/react": "^19.3.0",
|
|
28
28
|
"@types/react-dom": "^19.3.0",
|
|
29
|
-
"@vitejs/plugin-react": "^6.1.
|
|
30
|
-
"jsdom": "^30.1.
|
|
31
|
-
"react": "^19.3.
|
|
32
|
-
"react-dom": "^19.3.
|
|
33
|
-
"typescript": "^
|
|
34
|
-
"vite": "^8.3.
|
|
35
|
-
"vitest": "^5.0.
|
|
29
|
+
"@vitejs/plugin-react": "^6.1.2",
|
|
30
|
+
"jsdom": "^30.1.3",
|
|
31
|
+
"react": "^19.3.1",
|
|
32
|
+
"react-dom": "^19.3.1",
|
|
33
|
+
"typescript": "^6.0.3",
|
|
34
|
+
"vite": "^8.3.5",
|
|
35
|
+
"vitest": "^5.0.5"
|
|
36
36
|
},
|
|
37
37
|
"license": "Apache-2.0",
|
|
38
38
|
"repository": {
|
package/src/TerpProvider.tsx
CHANGED
|
@@ -142,9 +142,23 @@ export function TerpProvider({
|
|
|
142
142
|
};
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
+
// True from mount to unmount. A boot check can still be out when the tree unmounts, and
|
|
146
|
+
// its answer must then restore nothing: a state update on an unmounted provider does
|
|
147
|
+
// nothing useful, and React still reads `window.event` to schedule it, which throws once
|
|
148
|
+
// the environment that defined `window` is torn down. A ref rather than the boot effect's
|
|
149
|
+
// `cancelled` flag, because `refresh` is also the app's own (`auth.refresh()`), and
|
|
150
|
+
// because a StrictMode remount must not drop an SSO completion its first effect started.
|
|
151
|
+
const mountedRef = useRef(false);
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
mountedRef.current = true;
|
|
154
|
+
return () => {
|
|
155
|
+
mountedRef.current = false;
|
|
156
|
+
};
|
|
157
|
+
}, []);
|
|
158
|
+
|
|
145
159
|
const clearSession = useCallback(() => {
|
|
146
160
|
tokenRef.current = null;
|
|
147
|
-
setUser(null);
|
|
161
|
+
if (mountedRef.current) setUser(null);
|
|
148
162
|
}, []);
|
|
149
163
|
|
|
150
164
|
const refreshClient = useMemo(
|
|
@@ -221,11 +235,15 @@ export function TerpProvider({
|
|
|
221
235
|
const refresh = useCallback(async (): Promise<CurrentUser | null> => {
|
|
222
236
|
const startingToken = tokenRef.current;
|
|
223
237
|
const token = await refreshAccessToken();
|
|
238
|
+
// Unmounted while the request was out: there is no session left to restore, so ask
|
|
239
|
+
// for no user and set nothing.
|
|
240
|
+
if (!mountedRef.current) return null;
|
|
224
241
|
if (!token) {
|
|
225
242
|
if (tokenRef.current === startingToken) clearSession();
|
|
226
243
|
return null;
|
|
227
244
|
}
|
|
228
245
|
const me = await loadCurrentUser(client).catch(() => null);
|
|
246
|
+
if (!mountedRef.current) return null;
|
|
229
247
|
if (!me) {
|
|
230
248
|
if (tokenRef.current === token) clearSession();
|
|
231
249
|
return null;
|
|
@@ -240,12 +258,13 @@ export function TerpProvider({
|
|
|
240
258
|
async (pending: SsoCallbackParams): Promise<void> => {
|
|
241
259
|
try {
|
|
242
260
|
const token = await completeSsoCallback(client, pending.provider, pending);
|
|
261
|
+
if (!mountedRef.current) return;
|
|
243
262
|
tokenRef.current = token;
|
|
244
263
|
const me = await loadCurrentUser(client);
|
|
245
|
-
setUser(me);
|
|
264
|
+
if (mountedRef.current) setUser(me);
|
|
246
265
|
} catch (error) {
|
|
247
266
|
tokenRef.current = null;
|
|
248
|
-
setSsoError(error);
|
|
267
|
+
if (mountedRef.current) setSsoError(error);
|
|
249
268
|
}
|
|
250
269
|
},
|
|
251
270
|
[client],
|
|
@@ -271,6 +290,9 @@ export function TerpProvider({
|
|
|
271
290
|
return;
|
|
272
291
|
}
|
|
273
292
|
await refresh();
|
|
293
|
+
// Unmounted, or replaced by a newer boot, while the check was out: what it found is
|
|
294
|
+
// no longer this boot's to report.
|
|
295
|
+
if (cancelled) return;
|
|
274
296
|
if (noAnswerRef.current) {
|
|
275
297
|
// The console half of the same message. The screen tells whoever is looking at
|
|
276
298
|
// the app; this tells whoever has devtools open, which for this failure is the
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, render } from "@testing-library/react";
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { TerpProvider } from "./TerpProvider";
|
|
6
|
+
|
|
7
|
+
function json(body: unknown, status = 200): Response {
|
|
8
|
+
return new Response(JSON.stringify(body), {
|
|
9
|
+
status,
|
|
10
|
+
headers: { "content-type": "application/json" },
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
cleanup();
|
|
16
|
+
vi.restoreAllMocks();
|
|
17
|
+
vi.unstubAllGlobals();
|
|
18
|
+
window.history.replaceState({}, "", "/");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
interface HeldRequest {
|
|
22
|
+
url: string;
|
|
23
|
+
answer: (response: Response) => void;
|
|
24
|
+
fail: (reason: unknown) => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A fetch that holds every request until the test answers it. */
|
|
28
|
+
function heldFetch() {
|
|
29
|
+
const held: HeldRequest[] = [];
|
|
30
|
+
const fetchMock = vi.fn<typeof fetch>(
|
|
31
|
+
(input) =>
|
|
32
|
+
new Promise<Response>((answer, fail) => {
|
|
33
|
+
held.push({ url: (input as Request).url, answer, fail });
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
37
|
+
return { fetchMock, held };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Mounts a provider, waits for its first boot request, and unmounts it with that request
|
|
42
|
+
* still out. The returned spy counts reads of `window.event` from then on: React reads it to
|
|
43
|
+
* schedule every state update made outside an event, and it is the read that throws when the
|
|
44
|
+
* answer arrives after a test environment has removed `window`.
|
|
45
|
+
*/
|
|
46
|
+
async function unmountDuringBoot(held: HeldRequest[]) {
|
|
47
|
+
const { unmount } = render(
|
|
48
|
+
<TerpProvider baseUrl="https://api.test">
|
|
49
|
+
<span>app</span>
|
|
50
|
+
</TerpProvider>,
|
|
51
|
+
);
|
|
52
|
+
await vi.waitFor(() => expect(held).toHaveLength(1));
|
|
53
|
+
unmount();
|
|
54
|
+
return vi.spyOn(window, "event", "get");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Lets everything an answer sets off run to its end. */
|
|
58
|
+
function settle(): Promise<void> {
|
|
59
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
describe("a provider unmounted while its boot check is out", () => {
|
|
63
|
+
it("does nothing with a check that was never answered", async () => {
|
|
64
|
+
const { held } = heldFetch();
|
|
65
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
66
|
+
const event = await unmountDuringBoot(held);
|
|
67
|
+
|
|
68
|
+
held[0].fail(new TypeError("Failed to fetch"));
|
|
69
|
+
await settle();
|
|
70
|
+
|
|
71
|
+
expect(event).not.toHaveBeenCalled();
|
|
72
|
+
// The "did not answer" warning is about an app that is still on the screen.
|
|
73
|
+
expect(warn).not.toHaveBeenCalled();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("asks for no user when the check restores a session", async () => {
|
|
77
|
+
const { fetchMock, held } = heldFetch();
|
|
78
|
+
const event = await unmountDuringBoot(held);
|
|
79
|
+
|
|
80
|
+
held[0].answer(json({ access_token: "restored", token_type: "bearer" }));
|
|
81
|
+
await settle();
|
|
82
|
+
|
|
83
|
+
expect(event).not.toHaveBeenCalled();
|
|
84
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
85
|
+
expect(held[0].url).toMatch(/\/api\/v1\/auth\/refresh$/);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("finishes no SSO landing", async () => {
|
|
89
|
+
window.history.replaceState({}, "", "/auth/callback/dex?code=c1&state=s1");
|
|
90
|
+
const { fetchMock, held } = heldFetch();
|
|
91
|
+
const event = await unmountDuringBoot(held);
|
|
92
|
+
|
|
93
|
+
held[0].answer(json({ access_token: "sso-token", token_type: "bearer" }));
|
|
94
|
+
await settle();
|
|
95
|
+
|
|
96
|
+
expect(event).not.toHaveBeenCalled();
|
|
97
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
98
|
+
expect(held[0].url).toMatch(/\/api\/v1\/oidc\/dex\/callback$/);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -513,7 +513,7 @@ describe("DataView embedded variant", () => {
|
|
|
513
513
|
expect(screen.getByRole("searchbox")).toBeInTheDocument();
|
|
514
514
|
expect(screen.queryByText(/of 4 results/)).not.toBeInTheDocument();
|
|
515
515
|
expect(screen.queryByRole("button", { name: "Card view" })).not.toBeInTheDocument();
|
|
516
|
-
expect(screen.queryByRole("button", { name:
|
|
516
|
+
expect(screen.queryByRole("button", { name: /^Rows per page\b/ })).not.toBeInTheDocument();
|
|
517
517
|
// All rows rendered — the parent owns paging.
|
|
518
518
|
expect(screen.getAllByRole("row")).toHaveLength(5);
|
|
519
519
|
});
|
|
@@ -622,7 +622,7 @@ describe("DataView localisation", () => {
|
|
|
622
622
|
"placeholder",
|
|
623
623
|
"Zoeken…",
|
|
624
624
|
);
|
|
625
|
-
expect(screen.getByRole("button", { name: "Rijen per pagina" })).toBeInTheDocument();
|
|
625
|
+
expect(screen.getByRole("button", { name: "Rijen per pagina 10" })).toBeInTheDocument();
|
|
626
626
|
expect(screen.getByRole("button", { name: "Weergaveopties" })).toBeInTheDocument();
|
|
627
627
|
expect(screen.queryByText(/results/)).not.toBeInTheDocument();
|
|
628
628
|
});
|
|
@@ -951,7 +951,7 @@ describe("DataView page size", () => {
|
|
|
951
951
|
await screen.findByText("Broken printer");
|
|
952
952
|
// Four rows against a smallest page of ten: the control changed nothing. Mutation:
|
|
953
953
|
// offering it at any size.
|
|
954
|
-
expect(screen.queryByRole("button", { name:
|
|
954
|
+
expect(screen.queryByRole("button", { name: /^Rows per page\b/ })).not.toBeInTheDocument();
|
|
955
955
|
// The controls that work at any size stay.
|
|
956
956
|
expect(screen.getByRole("button", { name: "View options" })).toBeInTheDocument();
|
|
957
957
|
});
|
|
@@ -961,7 +961,7 @@ describe("DataView page size", () => {
|
|
|
961
961
|
await screen.findByText("Broken printer");
|
|
962
962
|
// Mutation: comparing with the largest option. (The page in force is 3 here too, so the
|
|
963
963
|
// test below is the one that tells it apart from the smallest option.)
|
|
964
|
-
expect(screen.getByRole("button", { name: "Rows per page" })).toBeInTheDocument();
|
|
964
|
+
expect(screen.getByRole("button", { name: "Rows per page 3" })).toBeInTheDocument();
|
|
965
965
|
});
|
|
966
966
|
|
|
967
967
|
it("offers it while the page in force is smaller than every option, and the view pages", async () => {
|
|
@@ -970,7 +970,7 @@ describe("DataView page size", () => {
|
|
|
970
970
|
// view paging with no way to show the rows at once.
|
|
971
971
|
render(<DataView repository={inMemoryRepo()} columns={COLUMNS} initialPageSize={3} />);
|
|
972
972
|
await screen.findByText("Broken printer");
|
|
973
|
-
expect(screen.getByRole("button", { name: "Rows per page" })).toBeInTheDocument();
|
|
973
|
+
expect(screen.getByRole("button", { name: "Rows per page 3" })).toBeInTheDocument();
|
|
974
974
|
});
|
|
975
975
|
|
|
976
976
|
it("keeps it while the total is not known yet", () => {
|
|
@@ -982,6 +982,22 @@ describe("DataView page size", () => {
|
|
|
982
982
|
render(<DataView repository={repository} columns={COLUMNS} />);
|
|
983
983
|
// Mutation: reading an unknown total as none, which takes the control away during every
|
|
984
984
|
// first load and puts it back when a large collection arrives.
|
|
985
|
-
expect(screen.getByRole("button", { name: "Rows per page" })).toBeInTheDocument();
|
|
985
|
+
expect(screen.getByRole("button", { name: "Rows per page 10" })).toBeInTheDocument();
|
|
986
|
+
});
|
|
987
|
+
|
|
988
|
+
it("names the trigger by its label and then the count it shows (WCAG 2.5.3)", async () => {
|
|
989
|
+
// `aria-label` replaces a button's content in its name, so the label alone hid the count
|
|
990
|
+
// the button shows: a screen reader never heard it, and a voice-control user saying "25"
|
|
991
|
+
// named nothing. axe 4.14 reports it as label-content-name-mismatch. Mutation: the label
|
|
992
|
+
// alone, or the count alone, as the name.
|
|
993
|
+
render(<DataView repository={inMemoryRepo()} columns={COLUMNS} pageSizeOptions={[2, 25]} initialPageSize={25} />);
|
|
994
|
+
await screen.findByText("Broken printer");
|
|
995
|
+
const trigger = screen.getByRole("button", { name: "Rows per page 25" });
|
|
996
|
+
expect(trigger).toHaveTextContent(/^25$/);
|
|
997
|
+
expect(trigger).toHaveAccessibleName(expect.stringContaining(trigger.textContent!));
|
|
998
|
+
|
|
999
|
+
fireEvent.click(trigger);
|
|
1000
|
+
fireEvent.click(screen.getByRole("menuitemradio", { name: "2" }));
|
|
1001
|
+
expect(await screen.findByRole("button", { name: "Rows per page 2" })).toHaveTextContent(/^2$/);
|
|
986
1002
|
});
|
|
987
1003
|
});
|
|
@@ -189,7 +189,11 @@ export function DataViewToolbar<T>(props: DataViewToolbarProps<T>) {
|
|
|
189
189
|
<span data-terp="dataview-toolbar-spacer" />
|
|
190
190
|
{props.onPageSizeChange !== undefined && props.pageSize !== undefined && (
|
|
191
191
|
<DataViewMenu
|
|
192
|
-
|
|
192
|
+
// The label, then the count the trigger shows. `aria-label` replaces the content in
|
|
193
|
+
// a button's name, so "Rows per page" alone hid the "25" on the button from a screen
|
|
194
|
+
// reader and gave a voice-control user nothing that matched it (WCAG 2.5.3, Label in
|
|
195
|
+
// Name). Joined by a space, as an `aria-labelledby` over the two would be.
|
|
196
|
+
triggerLabel={`${resolve(strings.pageSize)} ${props.pageSize}`}
|
|
193
197
|
// A fragment, not a wrapper span: [data-terp="menu-trigger"] already declares
|
|
194
198
|
// display: inline-flex, align-items: center, justify-content: center and
|
|
195
199
|
// gap: var(--space-1) — the deleted span's three declarations verbatim, with the
|
|
@@ -76,6 +76,21 @@ describe("useErrorMessage", () => {
|
|
|
76
76
|
});
|
|
77
77
|
|
|
78
78
|
describe("useErrorMessage under a locale", () => {
|
|
79
|
+
it.each([
|
|
80
|
+
["en", "idempotency_not_supported", 400, "This action does not support automatic retries. Contact support to continue."],
|
|
81
|
+
["nl", "idempotency_not_supported", 400, "Deze actie ondersteunt geen automatische herhalingen. Neem contact op met ondersteuning om verder te gaan."],
|
|
82
|
+
["en", "idempotency_replay_not_authorized", 409, "The earlier result cannot be shown. Check the current status before submitting this action again."],
|
|
83
|
+
["nl", "idempotency_replay_not_authorized", 409, "Het eerdere resultaat kan niet worden getoond. Controleer de huidige status voordat je deze actie opnieuw verstuurt."],
|
|
84
|
+
])("renders %s wording for %s without falling back to backend detail", (locale, code, status, words) => {
|
|
85
|
+
render(
|
|
86
|
+
<LocaleProvider locales={{ en: LOCALE_EN, nl: LOCALE_NL }} defaultLocale={locale}>
|
|
87
|
+
<ErrorState error={new ApiError("Unlocalized backend detail.", { code, status })} />
|
|
88
|
+
</LocaleProvider>,
|
|
89
|
+
);
|
|
90
|
+
expect(screen.getByRole("alert")).toHaveTextContent(words);
|
|
91
|
+
expect(screen.queryByText("Unlocalized backend detail.")).not.toBeInTheDocument();
|
|
92
|
+
});
|
|
93
|
+
|
|
79
94
|
it("words the platform's codes in the active locale, and an app's map still wins", () => {
|
|
80
95
|
// The built-in wording used to be an English map that was the context's default, and a
|
|
81
96
|
// plain string resolves as-is, so these two read in English under the Dutch catalog while
|
package/src/errorMessages.tsx
CHANGED
|
@@ -49,6 +49,8 @@ function builtInErrorMessages(strings: TerpStrings): ErrorMessages {
|
|
|
49
49
|
idempotency_body_too_large: strings.errorCodeRequestTooLarge,
|
|
50
50
|
idempotency_unavailable: strings.errorCodeInternalError,
|
|
51
51
|
idempotency_in_flight: strings.errorCodeStillProcessing,
|
|
52
|
+
idempotency_not_supported: strings.errorCodeIdempotencyNotSupported,
|
|
53
|
+
idempotency_replay_not_authorized: strings.errorCodeIdempotencyReplayNotAuthorized,
|
|
52
54
|
// The core's other refusals: work another process holds is still being done, work whose
|
|
53
55
|
// lease ran out is a conflict to refresh past, and sealed configuration that cannot be read
|
|
54
56
|
// is the server's failure. A weak password keeps its detail: it names the policy's numbers.
|
package/src/locale.tsx
CHANGED
|
@@ -400,6 +400,8 @@ export const LOCALE_NL: LocaleCatalog = {
|
|
|
400
400
|
errorCodeRequestTooLarge: "Dit is te groot om te versturen. Maak het kleiner en probeer het opnieuw.",
|
|
401
401
|
errorCodeInternalError: "Er ging iets mis op de server. Probeer het zo opnieuw.",
|
|
402
402
|
errorCodeStillProcessing: "Dit wordt nog verwerkt. Wacht even en kijk dan opnieuw.",
|
|
403
|
+
errorCodeIdempotencyNotSupported: "Deze actie ondersteunt geen automatische herhalingen. Neem contact op met ondersteuning om verder te gaan.",
|
|
404
|
+
errorCodeIdempotencyReplayNotAuthorized: "Het eerdere resultaat kan niet worden getoond. Controleer de huidige status voordat je deze actie opnieuw verstuurt.",
|
|
403
405
|
},
|
|
404
406
|
};
|
|
405
407
|
|
package/src/ui/Combobox.tsx
CHANGED
|
@@ -401,9 +401,8 @@ function ComboboxShell({
|
|
|
401
401
|
onFocus?.(event);
|
|
402
402
|
if (!disabled) {
|
|
403
403
|
setOpen(true);
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
);
|
|
404
|
+
const firstSelected = multiple ? null : (selectedValues[0] ?? null);
|
|
405
|
+
setActiveValue(firstSelected ?? enabledOptions[0]?.value ?? null);
|
|
407
406
|
}
|
|
408
407
|
}}
|
|
409
408
|
onBlur={onBlur}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
3
3
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
4
|
|
|
5
|
+
import { Field } from "../Field";
|
|
5
6
|
import { LOCALE_EN, LOCALE_NL, LocaleProvider } from "../locale";
|
|
6
7
|
import { DatePicker, DateRangePicker } from "./DatePicker";
|
|
7
8
|
|
|
@@ -19,7 +20,7 @@ describe("DatePicker", () => {
|
|
|
19
20
|
<DatePicker aria-label="Due date" defaultValue={new Date(2026, 6, 7)} onChange={onChange} />
|
|
20
21
|
</LocaleProvider>,
|
|
21
22
|
);
|
|
22
|
-
fireEvent.click(screen.getByRole("button", { name:
|
|
23
|
+
fireEvent.click(screen.getByRole("button", { name: /^Due date / }));
|
|
23
24
|
expect(screen.getByRole("grid", { name: /July 2026/ })).toBeInTheDocument();
|
|
24
25
|
fireEvent.keyDown(screen.getByRole("grid"), { key: "ArrowRight" });
|
|
25
26
|
fireEvent.keyDown(screen.getByRole("grid"), { key: "Enter" });
|
|
@@ -36,7 +37,7 @@ describe("DatePicker", () => {
|
|
|
36
37
|
<DatePicker aria-label="Due date" defaultValue={new Date(2026, 6, 7)} />
|
|
37
38
|
</LocaleProvider>,
|
|
38
39
|
);
|
|
39
|
-
fireEvent.click(screen.getByRole("button", { name:
|
|
40
|
+
fireEvent.click(screen.getByRole("button", { name: /^Due date / }));
|
|
40
41
|
const grid = screen.getByRole("grid");
|
|
41
42
|
const rows = screen.getAllByRole("row");
|
|
42
43
|
expect(rows).toHaveLength(6);
|
|
@@ -57,7 +58,7 @@ describe("DatePicker", () => {
|
|
|
57
58
|
<DatePicker aria-label="Due date" defaultValue={new Date(2026, 6, 7)} />
|
|
58
59
|
</LocaleProvider>,
|
|
59
60
|
);
|
|
60
|
-
fireEvent.click(screen.getByRole("button", { name:
|
|
61
|
+
fireEvent.click(screen.getByRole("button", { name: /^Due date / }));
|
|
61
62
|
const grid = screen.getByRole("grid");
|
|
62
63
|
const cursor = () => grid.querySelector<HTMLElement>('[tabindex="0"]');
|
|
63
64
|
|
|
@@ -82,7 +83,7 @@ describe("DatePicker", () => {
|
|
|
82
83
|
<DatePicker aria-label="Due date" defaultValue={new Date(2026, 6, 7)} />
|
|
83
84
|
</LocaleProvider>,
|
|
84
85
|
);
|
|
85
|
-
fireEvent.click(screen.getByRole("button", { name:
|
|
86
|
+
fireEvent.click(screen.getByRole("button", { name: /^Due date / }));
|
|
86
87
|
await waitFor(() =>
|
|
87
88
|
expect(document.activeElement).toBe(
|
|
88
89
|
screen.getByRole("grid").querySelector('[tabindex="0"]'),
|
|
@@ -106,7 +107,7 @@ describe("DatePicker", () => {
|
|
|
106
107
|
</LocaleProvider>,
|
|
107
108
|
);
|
|
108
109
|
expect(screen.getByRole("grid", { name: /July 2026/ })).toBeInTheDocument();
|
|
109
|
-
expect(screen.getByRole("button", { name:
|
|
110
|
+
expect(screen.getByRole("button", { name: /^Due date / })).toHaveAttribute(
|
|
110
111
|
"aria-expanded",
|
|
111
112
|
"true",
|
|
112
113
|
);
|
|
@@ -131,19 +132,74 @@ describe("DatePicker", () => {
|
|
|
131
132
|
).toBeInTheDocument();
|
|
132
133
|
});
|
|
133
134
|
|
|
135
|
+
// WCAG 2.5.3, Label in Name: `aria-label` replaces a button's content in its name, so the
|
|
136
|
+
// label alone named a trigger that shows a date after the label and nothing of the date. A
|
|
137
|
+
// screen reader never heard the value, and a voice-control user saying the date they could
|
|
138
|
+
// see named nothing; axe 4.14 reports it as label-content-name-mismatch.
|
|
139
|
+
it("names the trigger by its label and then the date it shows", () => {
|
|
140
|
+
render(
|
|
141
|
+
<LocaleProvider locales={{ en: LOCALE_EN }}>
|
|
142
|
+
<DatePicker aria-label="Due date" value={new Date(2026, 0, 15)} />
|
|
143
|
+
</LocaleProvider>,
|
|
144
|
+
);
|
|
145
|
+
const trigger = screen.getByRole("button", { name: "Due date Jan 15, 2026" });
|
|
146
|
+
// The name contains exactly the text the trigger shows (the glyph beside it is hidden).
|
|
147
|
+
expect(trigger.querySelector("span:not([aria-hidden])")?.textContent).toBe("Jan 15, 2026");
|
|
148
|
+
// Mutation: the label alone, as before.
|
|
149
|
+
expect(screen.queryByRole("button", { name: "Due date" })).not.toBeInTheDocument();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("names an empty trigger by its label and then the placeholder it shows", () => {
|
|
153
|
+
render(<DatePicker aria-label="Due date" value={null} />);
|
|
154
|
+
expect(screen.getByRole("button", { name: "Due date Select date" })).toBeInTheDocument();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("is named by a Field's label text and then its date, and described by the hint", () => {
|
|
158
|
+
// In a `Field` the label arrives as `aria-labelledby` (pointed at the label's text span),
|
|
159
|
+
// which the picker used to drop, so the trigger took the whole <label>'s text as its name,
|
|
160
|
+
// hint and error included. It now points at the label text and at its own shown text.
|
|
161
|
+
render(
|
|
162
|
+
<LocaleProvider locales={{ en: LOCALE_EN }}>
|
|
163
|
+
<Field label="Due date" hint="A weekday">
|
|
164
|
+
<DatePicker value={new Date(2026, 0, 15)} />
|
|
165
|
+
</Field>
|
|
166
|
+
</LocaleProvider>,
|
|
167
|
+
);
|
|
168
|
+
const trigger = screen.getByRole("button", { name: "Due date Jan 15, 2026" });
|
|
169
|
+
const [label, value] = trigger.getAttribute("aria-labelledby")!.split(" ");
|
|
170
|
+
expect(document.getElementById(label!)?.textContent).toBe("Due date");
|
|
171
|
+
expect(document.getElementById(value!)?.textContent).toBe("Jan 15, 2026");
|
|
172
|
+
expect(trigger).toHaveAccessibleDescription("A weekday");
|
|
173
|
+
expect(trigger).not.toHaveAttribute("aria-label");
|
|
174
|
+
});
|
|
175
|
+
|
|
134
176
|
it("uses the active locale for month and weekday names", () => {
|
|
135
177
|
render(
|
|
136
178
|
<LocaleProvider locales={{ nl: LOCALE_NL }}>
|
|
137
179
|
<DatePicker aria-label="Datum" defaultValue={new Date(2026, 6, 7)} />
|
|
138
180
|
</LocaleProvider>,
|
|
139
181
|
);
|
|
140
|
-
fireEvent.click(screen.getByRole("button", { name:
|
|
182
|
+
fireEvent.click(screen.getByRole("button", { name: /^Datum / }));
|
|
141
183
|
expect(screen.getByRole("grid", { name: /juli 2026/i })).toBeInTheDocument();
|
|
142
184
|
expect(screen.getByText(/zo/i)).toBeInTheDocument();
|
|
143
185
|
});
|
|
144
186
|
});
|
|
145
187
|
|
|
146
188
|
describe("DateRangePicker", () => {
|
|
189
|
+
it("names the trigger by its label and then the range it shows", () => {
|
|
190
|
+
render(
|
|
191
|
+
<LocaleProvider locales={{ en: LOCALE_EN }}>
|
|
192
|
+
<DateRangePicker
|
|
193
|
+
aria-label="Reporting period"
|
|
194
|
+
value={{ start: new Date(2026, 0, 15), end: new Date(2026, 0, 22) }}
|
|
195
|
+
/>
|
|
196
|
+
</LocaleProvider>,
|
|
197
|
+
);
|
|
198
|
+
expect(
|
|
199
|
+
screen.getByRole("button", { name: "Reporting period Jan 15, 2026 – Jan 22, 2026" }),
|
|
200
|
+
).toBeInTheDocument();
|
|
201
|
+
});
|
|
202
|
+
|
|
147
203
|
it("opens the calendar on mount when asked", () => {
|
|
148
204
|
render(
|
|
149
205
|
<DateRangePicker
|
|
@@ -170,7 +226,7 @@ describe("DateRangePicker", () => {
|
|
|
170
226
|
it("selects a start/end range and closes after the end", () => {
|
|
171
227
|
const onChange = vi.fn();
|
|
172
228
|
render(<DateRangePicker aria-label="Window" defaultValue={{ start: new Date(2026, 6, 10), end: null }} onChange={onChange} />);
|
|
173
|
-
fireEvent.click(screen.getByRole("button", { name:
|
|
229
|
+
fireEvent.click(screen.getByRole("button", { name: /^Window / }));
|
|
174
230
|
fireEvent.click(dayCells("12")[0]!);
|
|
175
231
|
expect(onChange).toHaveBeenCalledWith({ start: new Date(2026, 6, 10), end: new Date(2026, 6, 12) });
|
|
176
232
|
expect(screen.queryByRole("grid")).not.toBeInTheDocument();
|
|
@@ -187,7 +243,7 @@ describe("DateRangePicker", () => {
|
|
|
187
243
|
onChange={onChange}
|
|
188
244
|
/>,
|
|
189
245
|
);
|
|
190
|
-
fireEvent.click(screen.getByRole("button", { name:
|
|
246
|
+
fireEvent.click(screen.getByRole("button", { name: /^Window / }));
|
|
191
247
|
expect(dayCells("4")[0]).toBeDisabled();
|
|
192
248
|
fireEvent.click(dayCells("8")[0]!);
|
|
193
249
|
expect(onChange).toHaveBeenCalledWith({ start: new Date(2026, 6, 8), end: null });
|
package/src/ui/DatePicker.tsx
CHANGED
|
@@ -23,7 +23,15 @@ export interface DatePickerProps {
|
|
|
23
23
|
max?: Date;
|
|
24
24
|
disabled?: boolean;
|
|
25
25
|
placeholder?: UiText;
|
|
26
|
+
/** The field's label. The trigger is named by it and then by the value it shows. */
|
|
26
27
|
"aria-label"?: string;
|
|
28
|
+
/**
|
|
29
|
+
* The id of the element holding the field's label, as `Field` passes it. The trigger is named
|
|
30
|
+
* by that element and then by the value it shows.
|
|
31
|
+
*/
|
|
32
|
+
"aria-labelledby"?: string;
|
|
33
|
+
/** What describes the field, as `Field` passes it for its hint and its error. */
|
|
34
|
+
"aria-describedby"?: string;
|
|
27
35
|
"aria-invalid"?: boolean | "true" | "false";
|
|
28
36
|
/** Open the calendar on mount (uncontrolled), the same shape `Popover` and `Menu` take. */
|
|
29
37
|
defaultOpen?: boolean;
|
|
@@ -37,7 +45,15 @@ export interface DateRangePickerProps {
|
|
|
37
45
|
max?: Date;
|
|
38
46
|
disabled?: boolean;
|
|
39
47
|
placeholder?: UiText;
|
|
48
|
+
/** The field's label. The trigger is named by it and then by the value it shows. */
|
|
40
49
|
"aria-label"?: string;
|
|
50
|
+
/**
|
|
51
|
+
* The id of the element holding the field's label, as `Field` passes it. The trigger is named
|
|
52
|
+
* by that element and then by the value it shows.
|
|
53
|
+
*/
|
|
54
|
+
"aria-labelledby"?: string;
|
|
55
|
+
/** What describes the field, as `Field` passes it for its hint and its error. */
|
|
56
|
+
"aria-describedby"?: string;
|
|
41
57
|
"aria-invalid"?: boolean | "true" | "false";
|
|
42
58
|
/** Open the calendar on mount (uncontrolled), the same shape `Popover` and `Menu` take. */
|
|
43
59
|
defaultOpen?: boolean;
|
|
@@ -53,6 +69,8 @@ export function DatePicker({
|
|
|
53
69
|
disabled = false,
|
|
54
70
|
placeholder,
|
|
55
71
|
"aria-label": ariaLabel,
|
|
72
|
+
"aria-labelledby": ariaLabelledBy,
|
|
73
|
+
"aria-describedby": ariaDescribedBy,
|
|
56
74
|
"aria-invalid": ariaInvalid,
|
|
57
75
|
defaultOpen = false,
|
|
58
76
|
}: DatePickerProps) {
|
|
@@ -75,6 +93,7 @@ export function DatePicker({
|
|
|
75
93
|
// nothing, which is the whole point of having one.
|
|
76
94
|
const formatted =
|
|
77
95
|
selected === null ? resolve(placeholder ?? strings.selectDate) : formatDate(selected, locale);
|
|
96
|
+
const valueId = useId();
|
|
78
97
|
|
|
79
98
|
function commit(next: Date) {
|
|
80
99
|
if (value === undefined) {
|
|
@@ -94,12 +113,13 @@ export function DatePicker({
|
|
|
94
113
|
type="button"
|
|
95
114
|
data-terp="input"
|
|
96
115
|
aria-haspopup="dialog"
|
|
97
|
-
|
|
116
|
+
{...triggerName(ariaLabel, ariaLabelledBy, formatted, valueId)}
|
|
117
|
+
aria-describedby={ariaDescribedBy}
|
|
98
118
|
aria-invalid={ariaInvalid}
|
|
99
119
|
disabled={disabled}
|
|
100
120
|
data-placeholder={selected === null ? "true" : undefined}
|
|
101
121
|
>
|
|
102
|
-
<span>{formatted}</span>
|
|
122
|
+
<span id={valueId}>{formatted}</span>
|
|
103
123
|
<span aria-hidden="true">📅</span>
|
|
104
124
|
</button>
|
|
105
125
|
}
|
|
@@ -130,6 +150,8 @@ export function DateRangePicker({
|
|
|
130
150
|
disabled = false,
|
|
131
151
|
placeholder,
|
|
132
152
|
"aria-label": ariaLabel,
|
|
153
|
+
"aria-labelledby": ariaLabelledBy,
|
|
154
|
+
"aria-describedby": ariaDescribedBy,
|
|
133
155
|
"aria-invalid": ariaInvalid,
|
|
134
156
|
defaultOpen = false,
|
|
135
157
|
}: DateRangePickerProps) {
|
|
@@ -150,6 +172,7 @@ export function DateRangePicker({
|
|
|
150
172
|
: selected.end === null
|
|
151
173
|
? `${formatDate(selected.start, locale)} –`
|
|
152
174
|
: `${formatDate(selected.start, locale)} – ${formatDate(selected.end, locale)}`;
|
|
175
|
+
const valueId = useId();
|
|
153
176
|
|
|
154
177
|
function commit(next: DateRangeValue) {
|
|
155
178
|
if (value === undefined) {
|
|
@@ -171,12 +194,13 @@ export function DateRangePicker({
|
|
|
171
194
|
type="button"
|
|
172
195
|
data-terp="input"
|
|
173
196
|
aria-haspopup="dialog"
|
|
174
|
-
|
|
197
|
+
{...triggerName(ariaLabel, ariaLabelledBy, formatted, valueId)}
|
|
198
|
+
aria-describedby={ariaDescribedBy}
|
|
175
199
|
aria-invalid={ariaInvalid}
|
|
176
200
|
disabled={disabled}
|
|
177
201
|
data-placeholder={selected.start === null ? "true" : undefined}
|
|
178
202
|
>
|
|
179
|
-
<span>{formatted}</span>
|
|
203
|
+
<span id={valueId}>{formatted}</span>
|
|
180
204
|
<span aria-hidden="true">📅</span>
|
|
181
205
|
</button>
|
|
182
206
|
}
|
|
@@ -197,6 +221,29 @@ export function DateRangePicker({
|
|
|
197
221
|
);
|
|
198
222
|
}
|
|
199
223
|
|
|
224
|
+
/**
|
|
225
|
+
* The trigger's accessible name: the field's label, then the value the trigger shows.
|
|
226
|
+
*
|
|
227
|
+
* The label alone used to be the whole name, and `aria-label` REPLACES a button's content in
|
|
228
|
+
* its name, so a trigger reading "Jan 15, 2026" was named "Due date": a screen reader never
|
|
229
|
+
* heard the value, and a voice-control user saying what they could see named nothing (WCAG
|
|
230
|
+
* 2.5.3, Label in Name). Both ways a label arrives give the same name, the label and the shown
|
|
231
|
+
* text joined by a space: `aria-labelledby` (from `Field`) points at the label and at the
|
|
232
|
+
* shown text, which the browser joins that way, and `aria-label` is composed to match. No
|
|
233
|
+
* label, no attribute: the name is the shown text, which already contains itself.
|
|
234
|
+
*/
|
|
235
|
+
function triggerName(
|
|
236
|
+
label: string | undefined,
|
|
237
|
+
labelledBy: string | undefined,
|
|
238
|
+
shown: string,
|
|
239
|
+
valueId: string,
|
|
240
|
+
): { "aria-label"?: string; "aria-labelledby"?: string } {
|
|
241
|
+
if (labelledBy !== undefined) {
|
|
242
|
+
return { "aria-labelledby": `${labelledBy} ${valueId}` };
|
|
243
|
+
}
|
|
244
|
+
return label === undefined ? {} : { "aria-label": `${label} ${shown}` };
|
|
245
|
+
}
|
|
246
|
+
|
|
200
247
|
interface CalendarProps {
|
|
201
248
|
mode: "single" | "range";
|
|
202
249
|
locale: string | undefined;
|
package/src/uiText.test.tsx
CHANGED
|
@@ -17,7 +17,17 @@ import {
|
|
|
17
17
|
} from "./uiText";
|
|
18
18
|
import type { PluralText } from "./uiText";
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
function json(body: unknown): Response {
|
|
21
|
+
return new Response(JSON.stringify(body), {
|
|
22
|
+
status: 200,
|
|
23
|
+
headers: { "content-type": "application/json" },
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
cleanup();
|
|
29
|
+
vi.unstubAllGlobals();
|
|
30
|
+
});
|
|
21
31
|
|
|
22
32
|
describe("usePlural", () => {
|
|
23
33
|
// Polish, because its categories differ from English for the same counts: 3 is "few" and 5
|
|
@@ -157,7 +167,19 @@ describe("UiTextProvider", () => {
|
|
|
157
167
|
expect(screen.getByText("Laden...")).toBeInTheDocument();
|
|
158
168
|
});
|
|
159
169
|
|
|
160
|
-
it("localises ResourceList's empty message and create button", () => {
|
|
170
|
+
it("localises ResourceList's empty message and create button", async () => {
|
|
171
|
+
// The create button is write-gated, so the boot check restores an editor's session.
|
|
172
|
+
// Waiting for the button is waiting for that boot to finish: no request is still out
|
|
173
|
+
// when the test ends and the environment is torn down.
|
|
174
|
+
vi.stubGlobal(
|
|
175
|
+
"fetch",
|
|
176
|
+
vi.fn<typeof fetch>(async (input) => {
|
|
177
|
+
if ((input as Request).url.endsWith("/api/v1/auth/refresh")) {
|
|
178
|
+
return json({ access_token: "t", token_type: "bearer" });
|
|
179
|
+
}
|
|
180
|
+
return json({ id: "1", email: "editor@example.com", role_rank: 20, role_name: "editor" });
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
161
183
|
render(
|
|
162
184
|
<TerpProvider baseUrl="http://api.test">
|
|
163
185
|
<UiTextProvider strings={{ emptyList: "Nog niets.", add: "Toevoegen" }}>
|
|
@@ -180,6 +202,7 @@ describe("UiTextProvider", () => {
|
|
|
180
202
|
</TerpProvider>,
|
|
181
203
|
);
|
|
182
204
|
expect(screen.getByText("Nog niets.")).toBeInTheDocument();
|
|
205
|
+
expect(await screen.findByRole("button", { name: "Toevoegen" })).toBeInTheDocument();
|
|
183
206
|
});
|
|
184
207
|
});
|
|
185
208
|
|
package/src/uiText.tsx
CHANGED
|
@@ -533,6 +533,10 @@ export interface TerpStrings {
|
|
|
533
533
|
errorCodeInternalError: string;
|
|
534
534
|
/** Error code `idempotency_in_flight`: the same request is still being carried out. */
|
|
535
535
|
errorCodeStillProcessing: string;
|
|
536
|
+
/** Error code `idempotency_not_supported`: this action refuses retry keys. */
|
|
537
|
+
errorCodeIdempotencyNotSupported: string;
|
|
538
|
+
/** A completed request's result is withheld; never suggest blindly resubmitting it. */
|
|
539
|
+
errorCodeIdempotencyReplayNotAuthorized: string;
|
|
536
540
|
}
|
|
537
541
|
|
|
538
542
|
export const DEFAULT_STRINGS: TerpStrings = {
|
|
@@ -745,6 +749,8 @@ export const DEFAULT_STRINGS: TerpStrings = {
|
|
|
745
749
|
errorCodeRequestTooLarge: "This is too large to send. Make it smaller and try again.",
|
|
746
750
|
errorCodeInternalError: "Something went wrong on the server. Try again in a moment.",
|
|
747
751
|
errorCodeStillProcessing: "This is still being processed. Wait a moment, then check again.",
|
|
752
|
+
errorCodeIdempotencyNotSupported: "This action does not support automatic retries. Contact support to continue.",
|
|
753
|
+
errorCodeIdempotencyReplayNotAuthorized: "The earlier result cannot be shown. Check the current status before submitting this action again.",
|
|
748
754
|
};
|
|
749
755
|
|
|
750
756
|
interface UiTextContextValue {
|