@terpjs/react-core 0.18.0 → 0.20.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 +11 -11
- package/src/Authorized.tsx +20 -5
- package/src/Field.tsx +11 -22
- package/src/TerpProvider.tsx +13 -2
- package/src/admin/AccessAdmin.tsx +118 -0
- package/src/admin/AdminHub.tsx +10 -0
- package/src/admin/GroupDetail.tsx +7 -0
- package/src/admin/ModuleAccessPanel.tsx +349 -0
- package/src/admin/UserCreate.tsx +16 -6
- package/src/admin/UserDetail.tsx +23 -16
- package/src/admin/UsersAdmin.tsx +9 -3
- package/src/admin/access.test.tsx +655 -0
- package/src/admin/accessModel.test.ts +210 -0
- package/src/admin/accessModel.ts +165 -0
- package/src/admin/accessTiles.tsx +65 -0
- package/src/admin/admin.test.tsx +95 -3
- package/src/admin/module.tsx +3 -0
- package/src/admin/roles.ts +29 -15
- package/src/admin/useAccessLadder.ts +102 -0
- package/src/async-budget.test.ts +24 -0
- package/src/bootstrap.tsx +10 -0
- package/src/capabilities.test.ts +20 -0
- package/src/capabilities.ts +13 -3
- package/src/index.ts +2 -0
- package/src/locale.tsx +29 -0
- package/src/markers.test.ts +7 -0
- package/src/styles.test.ts +1 -0
- package/src/styles.ts +64 -0
- package/src/ui/Checkbox.tsx +56 -22
- package/src/ui/Radio.tsx +25 -1
- package/src/ui/Switch.tsx +57 -23
- package/src/ui/TileGroup.test.tsx +185 -0
- package/src/ui/TileGroup.tsx +220 -0
- package/src/ui/controlMessages.test.tsx +136 -0
- package/src/ui/controlMessages.tsx +81 -0
- package/src/uiText.tsx +70 -0
- package/vite.config.ts +17 -0
- package/vitest.setup.ts +50 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { useTerpClient } from "../TerpProvider";
|
|
4
|
+
import { unwrap } from "../unwrap";
|
|
5
|
+
import type { TerpStrings } from "../uiText";
|
|
6
|
+
|
|
7
|
+
import { buildModuleRows } from "./accessModel";
|
|
8
|
+
import type { ModuleRow } from "./accessModel";
|
|
9
|
+
|
|
10
|
+
/** One rung of the app's declared ladder, ready to render. */
|
|
11
|
+
export interface AdminRoleOption {
|
|
12
|
+
rank: number;
|
|
13
|
+
/** The name the app declared — `viewer`, `approver`, whatever it chose. */
|
|
14
|
+
name: string;
|
|
15
|
+
/** What to show a person: a framework translation where one exists, else the name. */
|
|
16
|
+
label: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The three rungs `terp-core` ships, by rank. Used only to *translate* a declared rung whose
|
|
21
|
+
* name matches one of them — never to decide which rungs exist. That distinction is the whole
|
|
22
|
+
* point of this file: `roles.ts` used to return these three ranks as literals, so an app with a
|
|
23
|
+
* four-rung ladder got a three-rung admin UI while ADR 0022 promised the role model was the
|
|
24
|
+
* app's. Now the ladder comes from `GET /api/v1/access/model` and this map only supplies copy.
|
|
25
|
+
*/
|
|
26
|
+
function packagedLabel(strings: TerpStrings, name: string): string | null {
|
|
27
|
+
if (name === "viewer") return strings.roleViewer;
|
|
28
|
+
if (name === "editor") return strings.roleEditor;
|
|
29
|
+
if (name === "admin") return strings.roleAdmin;
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AccessLadder {
|
|
34
|
+
/** The declared rungs, rank-ascending. Empty until the first load resolves. */
|
|
35
|
+
rungs: AdminRoleOption[];
|
|
36
|
+
/**
|
|
37
|
+
* Every module the app mounted, in ladder order, with what each rung adds.
|
|
38
|
+
*
|
|
39
|
+
* Carried by the same hook because it comes out of the same payload: the endpoint reports
|
|
40
|
+
* the ladder and the modules together, and a second hook for the second half would fetch
|
|
41
|
+
* `/model` twice to answer one question. The assignment panel filters this to the modules
|
|
42
|
+
* that opted in; the access screen keeps the refusals too, so it can say why.
|
|
43
|
+
*/
|
|
44
|
+
modules: ModuleRow[];
|
|
45
|
+
/** True while the model is in flight. */
|
|
46
|
+
loading: boolean;
|
|
47
|
+
/** The last failure message, or `null`. */
|
|
48
|
+
error: string | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The app's declared role ladder, read from the access model.
|
|
53
|
+
*
|
|
54
|
+
* Admin-only, like the endpoint — every screen that needs a ladder is already behind the
|
|
55
|
+
* `role: "admin"` route guard, so there is no case where this is fetched by a caller the
|
|
56
|
+
* server would refuse.
|
|
57
|
+
*
|
|
58
|
+
* A rung the framework has no translation for renders under its declared name rather than as
|
|
59
|
+
* `rank 25`, which is what the old rank-only fallback produced. The name is the app author's
|
|
60
|
+
* own word for it, so it is a better answer than a number even before anyone translates it.
|
|
61
|
+
*/
|
|
62
|
+
export function useAccessLadder(strings: TerpStrings): AccessLadder {
|
|
63
|
+
const client = useTerpClient();
|
|
64
|
+
const [rungs, setRungs] = useState<AdminRoleOption[]>([]);
|
|
65
|
+
const [modules, setModules] = useState<ModuleRow[]>([]);
|
|
66
|
+
const [loading, setLoading] = useState(true);
|
|
67
|
+
const [error, setError] = useState<string | null>(null);
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
let live = true;
|
|
71
|
+
setLoading(true);
|
|
72
|
+
// `unwrap` is how every hook in here reads a typed response: it throws the `ApiError`
|
|
73
|
+
// whose stable `code` the error-message machinery maps to copy, rather than leaving each
|
|
74
|
+
// caller to branch on `data` versus `error`.
|
|
75
|
+
void (async () => {
|
|
76
|
+
try {
|
|
77
|
+
const model = unwrap(await client.GET("/api/v1/access/model", {}));
|
|
78
|
+
if (!live) return;
|
|
79
|
+
setRungs(
|
|
80
|
+
[...model.roles]
|
|
81
|
+
.sort((a, b) => a.rank - b.rank)
|
|
82
|
+
.map((role) => ({
|
|
83
|
+
rank: role.rank,
|
|
84
|
+
name: role.name,
|
|
85
|
+
label: packagedLabel(strings, role.name) ?? role.name,
|
|
86
|
+
})),
|
|
87
|
+
);
|
|
88
|
+
setModules(buildModuleRows(model));
|
|
89
|
+
setError(null);
|
|
90
|
+
} catch (cause: unknown) {
|
|
91
|
+
if (live) setError(cause instanceof Error ? cause.message : String(cause));
|
|
92
|
+
} finally {
|
|
93
|
+
if (live) setLoading(false);
|
|
94
|
+
}
|
|
95
|
+
})();
|
|
96
|
+
return () => {
|
|
97
|
+
live = false;
|
|
98
|
+
};
|
|
99
|
+
}, [client, strings]);
|
|
100
|
+
|
|
101
|
+
return { rungs, modules, loading, error };
|
|
102
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getConfig } from "@testing-library/dom";
|
|
2
|
+
import { expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
// Testing Library's 1000ms default is not enough for this suite, and the way it fails is
|
|
5
|
+
// the problem: intermittently, on whichever test the machine happened to be busy during.
|
|
6
|
+
// Five failures across one day, four different tests, each passing alone and on a re-run —
|
|
7
|
+
// and one of them red in CI on a branch whose own change was fine.
|
|
8
|
+
//
|
|
9
|
+
// What this file holds is that the `configure` call in vitest.setup.ts actually TOOK
|
|
10
|
+
// EFFECT. Reading the number back out of the setup file would pass with the call deleted
|
|
11
|
+
// and the constant left behind, which is the one failure mode a config guard must not
|
|
12
|
+
// have. The bounds it has to sit between — above the default, below a toast's
|
|
13
|
+
// auto-dismiss, below `testTimeout` — span three files, so they are held from the Python
|
|
14
|
+
// side in `test_frontend_async_budget.py`, the way this repository holds its other
|
|
15
|
+
// cross-file config invariants. This package compiles with `types: []`, and reading files
|
|
16
|
+
// here would need node types it deliberately does not have.
|
|
17
|
+
|
|
18
|
+
const _WELL_ABOVE_THE_DEFAULT_MS = 2_000;
|
|
19
|
+
|
|
20
|
+
it("configures an async budget the loaded machine can actually meet", () => {
|
|
21
|
+
// Not a render: `findBy*` in these tests spans a resolved fetch, the state it sets and
|
|
22
|
+
// the re-render that puts the text on screen. One second covers that only when idle.
|
|
23
|
+
expect(getConfig().asyncUtilTimeout).toBeGreaterThanOrEqual(_WELL_ABOVE_THE_DEFAULT_MS);
|
|
24
|
+
});
|
package/src/bootstrap.tsx
CHANGED
|
@@ -42,6 +42,14 @@ export interface AdminAreaSections {
|
|
|
42
42
|
groups?: boolean;
|
|
43
43
|
/** The audit-log overview (terp-cap-audit). */
|
|
44
44
|
audit?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* The access screen: which roles exist and what each may do per module (terp-cap-access).
|
|
47
|
+
*
|
|
48
|
+
* Gated like the others because it is not decoration — the screen reads
|
|
49
|
+
* `GET /api/v1/access/model`, which only exists where the access capability is mounted, so an
|
|
50
|
+
* app without it would ship a nav entry leading to a 404.
|
|
51
|
+
*/
|
|
52
|
+
access?: boolean;
|
|
45
53
|
}
|
|
46
54
|
|
|
47
55
|
/** Route-path prefix per admin section, used to filter the packaged manifest. */
|
|
@@ -49,6 +57,7 @@ const ADMIN_SECTION_PREFIXES: Record<keyof AdminAreaSections, string> = {
|
|
|
49
57
|
users: "/admin/users",
|
|
50
58
|
groups: "/admin/groups",
|
|
51
59
|
audit: "/admin/audit",
|
|
60
|
+
access: "/admin/access",
|
|
52
61
|
};
|
|
53
62
|
|
|
54
63
|
function resolveAdminSections(
|
|
@@ -59,6 +68,7 @@ function resolveAdminSections(
|
|
|
59
68
|
users: sections.users !== false,
|
|
60
69
|
groups: sections.groups !== false,
|
|
61
70
|
audit: sections.audit !== false,
|
|
71
|
+
access: sections.access !== false,
|
|
62
72
|
};
|
|
63
73
|
}
|
|
64
74
|
|
package/src/capabilities.test.ts
CHANGED
|
@@ -22,3 +22,23 @@ describe("canPerform", () => {
|
|
|
22
22
|
expect(DEFAULT_RANK_THRESHOLDS).toEqual({ read: 10, write: 20, admin: 30 });
|
|
23
23
|
});
|
|
24
24
|
});
|
|
25
|
+
|
|
26
|
+
describe("canPerform with a per-module rung", () => {
|
|
27
|
+
it("takes the higher of the global rank and the module rung", () => {
|
|
28
|
+
// What the server's guard computes (ADR 0121). Without the rung the UI refused what the
|
|
29
|
+
// guard would have allowed, so a caller who could reach a module *only* through a rung had
|
|
30
|
+
// every control there hidden — a control living on one side of the wire.
|
|
31
|
+
expect(canPerform(10, "write")).toBe(false);
|
|
32
|
+
expect(canPerform(10, "write", DEFAULT_RANK_THRESHOLDS, 20)).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("never lowers a global rank", () => {
|
|
36
|
+
// The rung is additive at the guard, so a lower one cannot demote anyone here either. This
|
|
37
|
+
// is the case an implementation that *replaced* the rank would break.
|
|
38
|
+
expect(canPerform(30, "write", DEFAULT_RANK_THRESHOLDS, 10)).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("ignores a rung for a threshold it still does not reach", () => {
|
|
42
|
+
expect(canPerform(10, "admin", DEFAULT_RANK_THRESHOLDS, 20)).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
});
|
package/src/capabilities.ts
CHANGED
|
@@ -14,13 +14,23 @@ export const DEFAULT_RANK_THRESHOLDS: RankThresholds = {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* Whether a caller
|
|
18
|
-
*
|
|
17
|
+
* Whether a caller may perform `action`, at `roleRank` globally and optionally at
|
|
18
|
+
* `moduleRank` inside the module in question.
|
|
19
|
+
*
|
|
20
|
+
* The effective rank is the **higher** of the two, which is what the server's guard computes
|
|
21
|
+
* (ADR 0121): a per-module rung raises authority inside one module and never lowers it. Passing
|
|
22
|
+
* the rung is what stops the UI disagreeing with the guard — without it a caller who may reach
|
|
23
|
+
* a module *only* through a rung had the button hidden, so the interface refused what the server
|
|
24
|
+
* would have allowed, and the control existed on one side of the wire only.
|
|
25
|
+
*
|
|
26
|
+
* This is the UI gate only; the backend independently enforces authorization on every request.
|
|
19
27
|
*/
|
|
20
28
|
export function canPerform(
|
|
21
29
|
roleRank: number,
|
|
22
30
|
action: Action,
|
|
23
31
|
thresholds: RankThresholds = DEFAULT_RANK_THRESHOLDS,
|
|
32
|
+
moduleRank?: number,
|
|
24
33
|
): boolean {
|
|
25
|
-
|
|
34
|
+
const effective = moduleRank === undefined ? roleRank : Math.max(roleRank, moduleRank);
|
|
35
|
+
return effective >= thresholds[action];
|
|
26
36
|
}
|
package/src/index.ts
CHANGED
|
@@ -138,6 +138,8 @@ export { Checkbox } from "./ui/Checkbox";
|
|
|
138
138
|
export type { CheckboxProps } from "./ui/Checkbox";
|
|
139
139
|
export { Radio, RadioGroup } from "./ui/Radio";
|
|
140
140
|
export type { RadioGroupProps, RadioOption, RadioProps } from "./ui/Radio";
|
|
141
|
+
export { TileGroup } from "./ui/TileGroup";
|
|
142
|
+
export type { Tile, TileGroupProps } from "./ui/TileGroup";
|
|
141
143
|
export { Switch } from "./ui/Switch";
|
|
142
144
|
export type { SwitchProps } from "./ui/Switch";
|
|
143
145
|
export { Tabs } from "./ui/Tabs";
|
package/src/locale.tsx
CHANGED
|
@@ -223,6 +223,35 @@ export const LOCALE_NL: LocaleCatalog = {
|
|
|
223
223
|
adminGroupsDescription: "Bundel permissies; lidmaatschap past ze toe",
|
|
224
224
|
adminAudit: "Auditlog",
|
|
225
225
|
adminAuditDescription: "Elke wijziging: wat, wie, wanneer",
|
|
226
|
+
adminAccess: "Toegang",
|
|
227
|
+
adminAccessDescription: "Welke rollen er zijn, en wat elke rol per module mag",
|
|
228
|
+
accessNoRung: "Geen toegang",
|
|
229
|
+
accessAdds: "Extra ten opzichte van de tier eronder",
|
|
230
|
+
accessKindRead: "Bekijken",
|
|
231
|
+
accessKindWrite: "Wijzigen",
|
|
232
|
+
accessKindDelete: "Verwijderen",
|
|
233
|
+
accessAddsNothing: "Niets extra ten opzichte van de tier eronder",
|
|
234
|
+
accessNeverAssignable: "Nooit per module toe te kennen",
|
|
235
|
+
accessNotAssignable: "Niet per module toe te kennen",
|
|
236
|
+
accessUnexplainedRoutes: "{count} actie(s) in deze module hebben nog geen omschrijving",
|
|
237
|
+
accessDeclaredOnly:
|
|
238
|
+
"Dit is wat de applicatie vastlegt. Het laat niet zien wie welke rol heeft — open een persoon om dat te zien.",
|
|
239
|
+
moduleAccessTitle: "Toegang per module",
|
|
240
|
+
moduleAccessDescription:
|
|
241
|
+
"Een rol hier geldt in die ene module en verhoogt de toegang, nooit omlaag. Modules die dit niet aanbieden staan er niet bij.",
|
|
242
|
+
moduleAccessNoneAssignable:
|
|
243
|
+
"Geen enkele module in deze applicatie accepteert een eigen rol, dus hier is niets in te stellen.",
|
|
244
|
+
moduleAccessVia: "Hier ook {role}, via {name}",
|
|
245
|
+
moduleAccessStale: "Vastgelegd, maar zonder effect: {reason}",
|
|
246
|
+
moduleAccessFloor: "Overal al {role}, dus alles tot hier verandert niets",
|
|
247
|
+
moduleAccessConfirmTitle: "{role} geven in {module}?",
|
|
248
|
+
moduleAccessConfirm:
|
|
249
|
+
"Dit is de meest verstrekkende rol die de module heeft. Alles van de tier eronder, plus wat op de tegel staat.",
|
|
250
|
+
moduleAccessRevokeTitle: "De rol in {module} weghalen?",
|
|
251
|
+
moduleAccessRevoke:
|
|
252
|
+
"De toegang in deze module valt terug op wat de rol van deze persoon overal geeft.",
|
|
253
|
+
moduleAccessOrphaned:
|
|
254
|
+
"Staat nog op {role} in {module}, dat geen eigen rol meer accepteert. Het heeft geen effect; het weghalen is de enige wijziging die nog kan.",
|
|
226
255
|
statusColumn: "Status",
|
|
227
256
|
createdColumn: "Aangemaakt",
|
|
228
257
|
statusActive: "Actief",
|
package/src/markers.test.ts
CHANGED
|
@@ -93,6 +93,7 @@ const MARKERS = [
|
|
|
93
93
|
"combobox-option",
|
|
94
94
|
"combobox-token",
|
|
95
95
|
"combobox-token-remove",
|
|
96
|
+
"control-field",
|
|
96
97
|
"control-label",
|
|
97
98
|
"dataview",
|
|
98
99
|
"dataview-actions-cell",
|
|
@@ -234,6 +235,12 @@ const MARKERS = [
|
|
|
234
235
|
"text",
|
|
235
236
|
"theme-toggle",
|
|
236
237
|
"theme-toggle-label",
|
|
238
|
+
"tile",
|
|
239
|
+
"tile-body",
|
|
240
|
+
"tile-delta",
|
|
241
|
+
"tile-group",
|
|
242
|
+
"tile-label",
|
|
243
|
+
"tile-note",
|
|
237
244
|
"toast",
|
|
238
245
|
"toast-body",
|
|
239
246
|
"toast-icon",
|
package/src/styles.test.ts
CHANGED
package/src/styles.ts
CHANGED
|
@@ -957,6 +957,60 @@ textarea[data-terp="input"] {
|
|
|
957
957
|
cursor: pointer;
|
|
958
958
|
transition: background-color var(--motion-duration-fast) var(--motion-easing-standard);
|
|
959
959
|
}
|
|
960
|
+
[data-terp="tile-group"] {
|
|
961
|
+
display: grid;
|
|
962
|
+
grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr));
|
|
963
|
+
gap: var(--space-2);
|
|
964
|
+
}
|
|
965
|
+
[data-terp="tile"] {
|
|
966
|
+
display: flex;
|
|
967
|
+
flex-direction: column;
|
|
968
|
+
gap: var(--space-1);
|
|
969
|
+
padding: var(--space-3);
|
|
970
|
+
border: 1px solid var(--color-neutral-300);
|
|
971
|
+
border-radius: var(--radius-md);
|
|
972
|
+
background: var(--color-neutral-0);
|
|
973
|
+
cursor: pointer;
|
|
974
|
+
transition: border-color var(--motion-duration-fast) var(--motion-easing-standard);
|
|
975
|
+
}
|
|
976
|
+
[data-terp="tile"]:hover:not([aria-disabled="true"]) {
|
|
977
|
+
border-color: var(--color-neutral-600);
|
|
978
|
+
}
|
|
979
|
+
[data-terp="tile"][data-selected] {
|
|
980
|
+
border-color: var(--color-fg-accent);
|
|
981
|
+
background: var(--color-neutral-100);
|
|
982
|
+
}
|
|
983
|
+
/* The floor: a rung already reached some other way (an inherited rung, say). Marked so the
|
|
984
|
+
reader can see that the tier in force is not the one they picked, without it looking
|
|
985
|
+
selected — those are different facts and a single visual state would conflate them. */
|
|
986
|
+
[data-terp="tile"][data-floor] {
|
|
987
|
+
border-style: dashed;
|
|
988
|
+
border-color: var(--color-fg-accent);
|
|
989
|
+
}
|
|
990
|
+
/* Deliberately not the visual gravity a plan picker gives its top tier. This control exists to
|
|
991
|
+
bias downward, so the most privileged tile reads as a decision rather than a recommendation:
|
|
992
|
+
the danger colour is on the border and the label, and there is no fill to make it inviting. */
|
|
993
|
+
[data-terp="tile"][data-tone="danger"] {
|
|
994
|
+
border-color: var(--color-status-danger);
|
|
995
|
+
}
|
|
996
|
+
[data-terp="tile"][data-tone="danger"] [data-terp="tile-label"] {
|
|
997
|
+
color: var(--color-status-danger);
|
|
998
|
+
}
|
|
999
|
+
[data-terp="tile"][aria-disabled="true"] {
|
|
1000
|
+
cursor: not-allowed;
|
|
1001
|
+
color: var(--color-fg-subtle);
|
|
1002
|
+
background: var(--color-neutral-100);
|
|
1003
|
+
}
|
|
1004
|
+
[data-terp="tile-label"] {
|
|
1005
|
+
font-weight: var(--font-weight-semibold);
|
|
1006
|
+
font-size: var(--font-size-sm);
|
|
1007
|
+
}
|
|
1008
|
+
[data-terp="tile-body"] {
|
|
1009
|
+
font-size: var(--font-size-xs);
|
|
1010
|
+
color: var(--color-fg-subtle);
|
|
1011
|
+
display: grid;
|
|
1012
|
+
gap: var(--space-1);
|
|
1013
|
+
}
|
|
960
1014
|
[data-terp="radio-group"] {
|
|
961
1015
|
display: grid;
|
|
962
1016
|
gap: var(--space-2);
|
|
@@ -3660,6 +3714,16 @@ button[data-terp="input"][data-placeholder="true"] {
|
|
|
3660
3714
|
font-size: var(--font-size-sm);
|
|
3661
3715
|
color: var(--color-neutral-700);
|
|
3662
3716
|
}
|
|
3717
|
+
/* The envelope for the three self-labelling controls (Switch, Checkbox, RadioGroup),
|
|
3718
|
+
which cannot use [data-terp="field"]: that one is a <label> wrapping its control, and
|
|
3719
|
+
these render their own. The justify-items:start is the load-bearing declaration — the
|
|
3720
|
+
control's own <label> is inline-flex, and a stretched grid item would grow its click
|
|
3721
|
+
target to the full column, so clicking empty space far from the text would toggle it. */
|
|
3722
|
+
[data-terp="control-field"] {
|
|
3723
|
+
display: grid;
|
|
3724
|
+
gap: var(--space-1);
|
|
3725
|
+
justify-items: start;
|
|
3726
|
+
}
|
|
3663
3727
|
[data-terp="field-hint"] {
|
|
3664
3728
|
color: var(--color-fg-subtle);
|
|
3665
3729
|
font-size: var(--font-size-xs);
|
package/src/ui/Checkbox.tsx
CHANGED
|
@@ -3,40 +3,74 @@ import type { ChangeEvent, InputHTMLAttributes } from "react";
|
|
|
3
3
|
import { injectTerpStyles } from "../styles";
|
|
4
4
|
import { useUiText } from "../uiText";
|
|
5
5
|
import type { UiText } from "../uiText";
|
|
6
|
+
import { mergeDescribedBy, useControlMessages } from "./controlMessages";
|
|
6
7
|
|
|
7
8
|
injectTerpStyles();
|
|
8
9
|
|
|
9
10
|
export interface CheckboxProps
|
|
10
11
|
extends Omit<InputHTMLAttributes<HTMLInputElement>, "type" | "checked" | "defaultChecked" | "onChange"> {
|
|
11
12
|
label: UiText;
|
|
13
|
+
/** Optional helper text under the control (pointed at by `aria-describedby`). */
|
|
14
|
+
hint?: UiText;
|
|
15
|
+
/** A control-level error, shown under the control and announced when it appears. */
|
|
16
|
+
error?: string | null;
|
|
12
17
|
checked?: boolean;
|
|
13
18
|
defaultChecked?: boolean;
|
|
14
19
|
onChange?: (checked: boolean) => void;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
|
-
/**
|
|
18
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Token-styled labelled checkbox — use instead of a raw `<input type="checkbox">`.
|
|
24
|
+
*
|
|
25
|
+
* It carries its own `hint` / `error` rather than going inside a `Field`, because it cannot
|
|
26
|
+
* go inside one: the checkbox renders its own `<label>` around the input, and a `<label>`
|
|
27
|
+
* within `Field`'s `<label>` is invalid HTML — the browser associates the control with the
|
|
28
|
+
* outer one and the field's label text is lost. The wiring is shared with `Field` through
|
|
29
|
+
* `useControlMessages`, so the ids, `aria-describedby`, `aria-invalid` and the error's
|
|
30
|
+
* `role="alert"` behave identically here.
|
|
31
|
+
*/
|
|
32
|
+
export function Checkbox({
|
|
33
|
+
label,
|
|
34
|
+
hint,
|
|
35
|
+
error,
|
|
36
|
+
checked,
|
|
37
|
+
defaultChecked,
|
|
38
|
+
onChange,
|
|
39
|
+
style,
|
|
40
|
+
...rest
|
|
41
|
+
}: CheckboxProps) {
|
|
19
42
|
const resolve = useUiText();
|
|
43
|
+
const { hasError, describedBy, messages } = useControlMessages(hint, error);
|
|
20
44
|
return (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
:
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
45
|
+
// The wrapper is unconditional, and that is not cosmetic. Rendering it only when there is
|
|
46
|
+
// a message would change the element type at this position the moment an error arrives,
|
|
47
|
+
// so React would unmount the label and mount a fresh input — an uncontrolled checkbox
|
|
48
|
+
// would silently lose its state exactly when the form told the user something was wrong.
|
|
49
|
+
<div data-terp="control-field" style={style}>
|
|
50
|
+
<label data-terp="control-label">
|
|
51
|
+
<input
|
|
52
|
+
{...rest}
|
|
53
|
+
type="checkbox"
|
|
54
|
+
data-terp="checkbox"
|
|
55
|
+
checked={checked}
|
|
56
|
+
defaultChecked={defaultChecked}
|
|
57
|
+
// A caller who pointed the control at their own description keeps it: the field's
|
|
58
|
+
// ids are added to theirs, never substituted for them.
|
|
59
|
+
aria-describedby={mergeDescribedBy(rest["aria-describedby"], describedBy)}
|
|
60
|
+
aria-invalid={rest["aria-invalid"] ?? (hasError ? true : undefined)}
|
|
61
|
+
// Attached only when there is something to call, exactly as Select does and for the
|
|
62
|
+
// reason stated there: an unconditional handler silences React's own "you provided a
|
|
63
|
+
// `checked` prop to a form field without an `onChange` handler" guard, so a caller who
|
|
64
|
+
// pinned `checked` and forgot the handler gets a control that looks operable, never
|
|
65
|
+
// changes, and says nothing about it. The spread form is what keeps the prop absent
|
|
66
|
+
// rather than present-and-undefined, which React treats as the same mistake.
|
|
67
|
+
{...(onChange !== undefined
|
|
68
|
+
? { onChange: (event: ChangeEvent<HTMLInputElement>) => onChange(event.currentTarget.checked) }
|
|
69
|
+
: {})}
|
|
70
|
+
/>
|
|
71
|
+
<span>{resolve(label)}</span>
|
|
72
|
+
</label>
|
|
73
|
+
{messages}
|
|
74
|
+
</div>
|
|
40
75
|
);
|
|
41
76
|
}
|
|
42
|
-
|
package/src/ui/Radio.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import { useId, useState } from "react";
|
|
|
2
2
|
import type { ChangeEvent, InputHTMLAttributes } from "react";
|
|
3
3
|
|
|
4
4
|
import { injectTerpStyles } from "../styles";
|
|
5
|
+
import { useControlMessages } from "./controlMessages";
|
|
5
6
|
import { useUiText } from "../uiText";
|
|
6
7
|
import type { UiText } from "../uiText";
|
|
7
8
|
|
|
@@ -51,6 +52,18 @@ export interface RadioOption {
|
|
|
51
52
|
|
|
52
53
|
export interface RadioGroupProps {
|
|
53
54
|
label: UiText;
|
|
55
|
+
/** Optional helper text under the options (pointed at by the group's `aria-describedby`). */
|
|
56
|
+
hint?: UiText;
|
|
57
|
+
/**
|
|
58
|
+
* A group-level error, shown under the options and announced when it appears.
|
|
59
|
+
*
|
|
60
|
+
* This is the one of the three self-labelling controls with a genuine error need. A
|
|
61
|
+
* boolean cannot hold a value its type refuses, so a switch or a checkbox has little to
|
|
62
|
+
* be wrong about — but a required radio group **can** be left unset, and until this prop
|
|
63
|
+
* existed that rejection had nowhere to go except a form-level summary that never names
|
|
64
|
+
* the control it is about.
|
|
65
|
+
*/
|
|
66
|
+
error?: string | null;
|
|
54
67
|
name?: string;
|
|
55
68
|
options?: readonly RadioOption[];
|
|
56
69
|
value?: string;
|
|
@@ -62,6 +75,8 @@ export interface RadioGroupProps {
|
|
|
62
75
|
/** Accessible token-styled radio group; pass `options` for the standard generated radios. */
|
|
63
76
|
export function RadioGroup({
|
|
64
77
|
label,
|
|
78
|
+
hint,
|
|
79
|
+
error,
|
|
65
80
|
name,
|
|
66
81
|
options,
|
|
67
82
|
value,
|
|
@@ -71,6 +86,7 @@ export function RadioGroup({
|
|
|
71
86
|
}: RadioGroupProps) {
|
|
72
87
|
const generatedName = useId();
|
|
73
88
|
const resolve = useUiText();
|
|
89
|
+
const { hasError, describedBy, messages } = useControlMessages(hint, error);
|
|
74
90
|
const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue ?? "");
|
|
75
91
|
const selected = value ?? uncontrolledValue;
|
|
76
92
|
const groupName = name ?? generatedName;
|
|
@@ -83,7 +99,14 @@ export function RadioGroup({
|
|
|
83
99
|
}
|
|
84
100
|
|
|
85
101
|
return (
|
|
86
|
-
|
|
102
|
+
// The description and the invalid state belong to the GROUP, not to any one radio: the
|
|
103
|
+
// thing that is unanswered is the question, and a screen-reader user moving through the
|
|
104
|
+
// options should hear it once at the group rather than repeated on each option.
|
|
105
|
+
<fieldset
|
|
106
|
+
data-terp="radio-group"
|
|
107
|
+
aria-describedby={describedBy}
|
|
108
|
+
aria-invalid={hasError ? true : undefined}
|
|
109
|
+
>
|
|
87
110
|
<legend data-terp="radio-group-legend">{resolve(label)}</legend>
|
|
88
111
|
<div data-terp="radio-group-options">
|
|
89
112
|
{options?.map((option) => (
|
|
@@ -102,6 +125,7 @@ export function RadioGroup({
|
|
|
102
125
|
/>
|
|
103
126
|
))}
|
|
104
127
|
</div>
|
|
128
|
+
{messages}
|
|
105
129
|
</fieldset>
|
|
106
130
|
);
|
|
107
131
|
}
|
package/src/ui/Switch.tsx
CHANGED
|
@@ -3,41 +3,75 @@ import type { ChangeEvent, InputHTMLAttributes } from "react";
|
|
|
3
3
|
import { injectTerpStyles } from "../styles";
|
|
4
4
|
import { useUiText } from "../uiText";
|
|
5
5
|
import type { UiText } from "../uiText";
|
|
6
|
+
import { mergeDescribedBy, useControlMessages } from "./controlMessages";
|
|
6
7
|
|
|
7
8
|
injectTerpStyles();
|
|
8
9
|
|
|
9
10
|
export interface SwitchProps
|
|
10
11
|
extends Omit<InputHTMLAttributes<HTMLInputElement>, "type" | "checked" | "defaultChecked" | "onChange" | "role"> {
|
|
11
12
|
label: UiText;
|
|
13
|
+
/** Optional helper text under the control (pointed at by `aria-describedby`). */
|
|
14
|
+
hint?: UiText;
|
|
15
|
+
/** A control-level error, shown under the control and announced when it appears. */
|
|
16
|
+
error?: string | null;
|
|
12
17
|
checked?: boolean;
|
|
13
18
|
defaultChecked?: boolean;
|
|
14
19
|
onChange?: (checked: boolean) => void;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
|
-
/**
|
|
18
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Token-styled labelled switch for boolean settings.
|
|
24
|
+
*
|
|
25
|
+
* It carries its own `hint` / `error` rather than going inside a `Field`, because it cannot
|
|
26
|
+
* go inside one: the switch renders its own `<label>` around the input, and a `<label>`
|
|
27
|
+
* within `Field`'s `<label>` is invalid HTML — the browser associates the control with the
|
|
28
|
+
* outer one and the field's label text is lost. The wiring is shared with `Field` through
|
|
29
|
+
* `useControlMessages`, so the ids, `aria-describedby`, `aria-invalid` and the error's
|
|
30
|
+
* `role="alert"` behave identically here.
|
|
31
|
+
*/
|
|
32
|
+
export function Switch({
|
|
33
|
+
label,
|
|
34
|
+
hint,
|
|
35
|
+
error,
|
|
36
|
+
checked,
|
|
37
|
+
defaultChecked,
|
|
38
|
+
onChange,
|
|
39
|
+
style,
|
|
40
|
+
...rest
|
|
41
|
+
}: SwitchProps) {
|
|
19
42
|
const resolve = useUiText();
|
|
43
|
+
const { hasError, describedBy, messages } = useControlMessages(hint, error);
|
|
20
44
|
return (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
:
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
// The wrapper is unconditional, and that is not cosmetic. Rendering it only when there is
|
|
46
|
+
// a message would change the element type at this position the moment an error arrives,
|
|
47
|
+
// so React would unmount the label and mount a fresh input — an uncontrolled switch would
|
|
48
|
+
// silently lose its state exactly when the form told the user something was wrong.
|
|
49
|
+
<div data-terp="control-field" style={style}>
|
|
50
|
+
<label data-terp="control-label">
|
|
51
|
+
<input
|
|
52
|
+
{...rest}
|
|
53
|
+
type="checkbox"
|
|
54
|
+
role="switch"
|
|
55
|
+
data-terp="switch"
|
|
56
|
+
checked={checked}
|
|
57
|
+
defaultChecked={defaultChecked}
|
|
58
|
+
// A caller who pointed the control at their own description keeps it: the field's
|
|
59
|
+
// ids are added to theirs, never substituted for them.
|
|
60
|
+
aria-describedby={mergeDescribedBy(rest["aria-describedby"], describedBy)}
|
|
61
|
+
aria-invalid={rest["aria-invalid"] ?? (hasError ? true : undefined)}
|
|
62
|
+
// Attached only when there is something to call, exactly as Select does and for the
|
|
63
|
+
// reason stated there: an unconditional handler silences React's own "you provided a
|
|
64
|
+
// `checked` prop to a form field without an `onChange` handler" guard, so a caller who
|
|
65
|
+
// pinned `checked` and forgot the handler gets a control that looks operable, never
|
|
66
|
+
// changes, and says nothing about it. The spread form is what keeps the prop absent
|
|
67
|
+
// rather than present-and-undefined, which React treats as the same mistake.
|
|
68
|
+
{...(onChange !== undefined
|
|
69
|
+
? { onChange: (event: ChangeEvent<HTMLInputElement>) => onChange(event.currentTarget.checked) }
|
|
70
|
+
: {})}
|
|
71
|
+
/>
|
|
72
|
+
<span>{resolve(label)}</span>
|
|
73
|
+
</label>
|
|
74
|
+
{messages}
|
|
75
|
+
</div>
|
|
41
76
|
);
|
|
42
77
|
}
|
|
43
|
-
|