@terpjs/react-core 0.25.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -7
- package/package.json +6 -5
- package/src/Field.test.tsx +95 -1
- package/src/Field.tsx +70 -1
- package/src/LoginView.test.tsx +33 -0
- package/src/LoginView.tsx +36 -23
- package/src/Meter.test.tsx +304 -0
- package/src/Meter.tsx +159 -0
- package/src/QuietActions.test.tsx +118 -0
- package/src/QuietActions.tsx +75 -0
- package/src/UserMenu.test.tsx +23 -0
- package/src/UserMenu.tsx +9 -1
- package/src/admin/AuditLogAdmin.tsx +1 -1
- package/src/admin/admin.test.tsx +57 -5
- package/src/admin/module.tsx +9 -6
- package/src/bootstrap.test.tsx +1 -1
- package/src/bootstrap.tsx +2 -1
- package/src/dataview/DataView.test.tsx +70 -0
- package/src/dataview/README.md +5 -3
- package/src/dataview/index.ts +1 -1
- package/src/dataview/internal.tsx +76 -19
- package/src/dataview/types.ts +9 -35
- package/src/errorMessages.test.tsx +21 -0
- package/src/errorMessages.tsx +33 -19
- package/src/index.ts +7 -3
- package/src/locale.test.tsx +35 -0
- package/src/locale.tsx +62 -4
- package/src/markers.test.ts +33 -11
- package/src/ssr.test.tsx +18 -0
- package/src/styles.test.ts +341 -33
- package/src/styles.ts +546 -71
- package/src/testing.test.tsx +162 -0
- package/src/testing.ts +84 -0
- package/src/tokens.guard.test.ts +11 -6
- package/src/ui/controlMessages.tsx +27 -14
- package/src/uiText.literals.test.ts +19 -6
- package/src/uiText.test.tsx +54 -2
- package/src/uiText.tsx +166 -6
- package/vitest.setup.ts +7 -20
package/README.md
CHANGED
|
@@ -5,8 +5,9 @@ Stack A (React) of the Terp frontend contract: the provider/hooks that wire a tr
|
|
|
5
5
|
**token-styled component surface** every app module composes its screens from.
|
|
6
6
|
|
|
7
7
|
This README is the catalog of that surface. Everything listed here is exported from
|
|
8
|
-
the package root (`import { … } from "@terpjs/react-core"`)
|
|
9
|
-
|
|
8
|
+
the package root (`import { … } from "@terpjs/react-core"`), except the test-setup helper
|
|
9
|
+
under [Testing components](#testing-components), which has a subpath of its own; each export
|
|
10
|
+
also carries JSDoc, so your editor shows the same guidance inline. **Never deep-import** from
|
|
10
11
|
`src/` or `dist/` — the boundary lint (`@terpjs/eslint-boundaries`) refuses it.
|
|
11
12
|
|
|
12
13
|
## Conventions (the lint enforces these)
|
|
@@ -55,7 +56,7 @@ JSDoc, so your editor shows the same guidance inline. **Never deep-import** from
|
|
|
55
56
|
| `useSso`, `parseSsoCallback`, `fetchSsoAuthorizationUrl`, `completeSsoCallback` | The SSO login seam (ADR 0058): `useSso().begin(provider)` opens an OIDC flow; `TerpProvider` completes the `/auth/callback/{provider}` redirect landing into a normal session on boot. `renderTerpApp({ ssoProviders })` wires the buttons in one line. |
|
|
56
57
|
| `RequireAuth` | Renders children only with a session; pairs with the router so the app mounts only when signed in. |
|
|
57
58
|
| `ThemeProvider`, `ThemeToggle`, `useTheme` | Theming over the shipped palettes — `light`, `dark`, `midnight`, `twilight`, `contrast` — plus `system` to follow the OS preference. Applies `data-theme` on `<html>` (the token stylesheet carries every palette) and persists the choice. `defaultTheme` is how an app ships on a named theme — declare it in `layout-contract.json` so a tool can read and rewrite it, or pass the bootstrap option; both is refused. `renderTerpApp` mounts it for every app; the shell header uses an icon-only, token-themed `variant="inline"` menu. |
|
|
58
|
-
| `LocaleProvider`, `defineAppLocales`, `LanguageSwitcher`, `useLocale`, `LOCALE_EN`, `LOCALE_NL` | The language seam over `UiTextProvider`: `defineAppLocales(i18n, frameworkCatalogs)` validates and merges checked-in app messages with framework chrome, the active locale persists, and the shell offers a picker. Pass `sourceLocale` with `locales`; missing/empty target messages, undocumented source copies, invalid locale selection and a non-English locale without a complete framework-string catalog throw rather than silently falling back. |
|
|
59
|
+
| `LocaleProvider`, `defineAppLocales`, `LanguageSwitcher`, `useLocale`, `LOCALE_EN`, `LOCALE_NL` | The language seam over `UiTextProvider`: `defineAppLocales(i18n, frameworkCatalogs)` validates and merges checked-in app messages with framework chrome, the active locale persists, and the shell offers a picker. Pass `sourceLocale` with `locales`; missing/empty target messages, undocumented source copies, invalid locale selection and a non-English locale without a complete framework-string catalog throw rather than silently falling back. `<html lang>` follows the active locale from mount and across every switch. |
|
|
59
60
|
| `UserMenu`, `userInitials` | The signed-in user's menu, pinned by `buildAppRouter` to the bottom of the sidebar: an initials avatar trigger opening the identity block, **Settings** (the built-in profile page) and sign-out. Collapses to the avatar in the icon rail. |
|
|
60
61
|
| `ProfileView` | The built-in profile / settings page (`/profile`): the server-validated identity, theme + language preferences, and sign-out. |
|
|
61
62
|
|
|
@@ -201,7 +202,7 @@ marker, counted by the escape-hatch budget.
|
|
|
201
202
|
| `LoadingState`, `InlineSpinner` | Full loading block (announces itself) / compact inline glyph. |
|
|
202
203
|
| `EmptyState` | The standard "nothing here yet" block, with an `action` slot for the next step. |
|
|
203
204
|
| `ErrorState`, `describeError` | Human-readable failure block for a caught error. |
|
|
204
|
-
| `ErrorMessagesProvider`, `useErrorMessage
|
|
205
|
+
| `ErrorMessagesProvider`, `useErrorMessage` | Map stable backend error codes to copy; falls back to the envelope `detail`. The platform's own codes are worded by the active locale (`errorCode*` in `TerpStrings`), and an app's map wins over them. |
|
|
205
206
|
| `ToastProvider`, `useToast` | Transient success/error feedback (no toast library). |
|
|
206
207
|
| `ConfirmDialog` | Accessible confirmation modal (native `<dialog>`); use before any destructive action. A modal is for a confirmation or an explicit post-action moment — an edit form or a detail view belongs in a routed page, or in an expanded row beside the thing it edits (ADR 0096 §4). |
|
|
207
208
|
|
|
@@ -220,11 +221,14 @@ marker, counted by the escape-hatch budget.
|
|
|
220
221
|
| `Tabs` | In-page (non-routed) tab set with `tablist` / `tab` / `tabpanel` roles, arrow-key navigation, and controlled or uncontrolled value. |
|
|
221
222
|
| `Avatar` | The initials tile: `from` (an email or a name) or explicit `initials`, and a closed `size` of `sm` (2rem, an account menu) or `md` (3.5rem, a profile header). `aria-hidden`, because the name it abbreviates is always rendered beside it. No `src`: `/me` carries no avatar URL, so an image slot would be a prop with nothing behind it. |
|
|
222
223
|
| `Badge` | Small status pill: `<Badge tone="success">Synced</Badge>` (or `label="Synced"`); `tone`: neutral / info / success / warning / danger. |
|
|
224
|
+
| `Meter` | One bounded value — a quota used, a score against its range — as a bar with the value printed beside it (ADR 0158). The bar **is** the native `<meter>` (`value`, `min` default 0, `max` default 1, and the optional `low` / `high` / `optimum` bands), styled from the sheet, so the browser draws the proportion and nothing renders an inline width. `label` is required and is the meter's **accessible name, not printed**: every place a quantity sits already has a visible caption — a `DetailList` term, a `Card` title, a `HubCard` title — so a meter goes inside one, and the standard layout contract refuses one loose in a body. `format` takes the `Intl.NumberFormatOptions` the `format` helpers take and defaults to a percentage, which is always the **share of the range** (`value={74} max={100}` prints `74%`, not Intl's `7,400%`); any other style prints the value itself, unclamped, so an overrun reads `120%` against a full bar. The printed copy is `aria-hidden` and handed to the element as `aria-valuetext`, so a screen reader hears it once — and so does a `HubCard` link whose stat is a meter. Bands are opt-in: with none the bar is one accent colour, and with any the fill takes success / warning / danger by region — reinforcing the printed value, never replacing words where the judgement matters. |
|
|
223
225
|
| `Tooltip` | Accessible focus/hover tooltip that describes its trigger with `aria-describedby`. |
|
|
224
226
|
| `Popover`, `Menu`, `MenuItem` | Shared anchored overlay and dropdown-menu primitives: body-portaled, viewport-aware panels that escape scroll/table clipping, with outside-click/Escape close, focus return, selected-item semantics, and roving keyboard navigation. |
|
|
225
227
|
| `Alert` | Inline banner for persistent feedback (`tone`: neutral / info / success / warning / danger); warnings and danger announce as `alert`, others as `status`. |
|
|
226
228
|
| `Markdown` | Safe, dependency-free markdown renderer for headings, paragraphs, bold, italic, inline code, code blocks, lists, and safe links; raw HTML is rendered as text and never passed through. |
|
|
227
229
|
| `Field` | Label + control + hint/error wrapper for one form field. The error is announced (`role="alert"`) as well as described, because `aria-describedby` alone is silent for a rejection that arrives after focus has left the control. |
|
|
230
|
+
| `FieldRow` | Several `Field`s side by side, with their labels, their controls and their messages each on a **shared line**. A field is as tall as its label, its control *and* whatever it has to say, so neither flex alignment is right: `align="end"` lines up the messages, so a field that grows a hint lifts its own control above its neighbours' and a bare action button sinks to the depth of the longest error on the row; `align="start"` lines up the labels, which fixes the fields and leaves the button riding above the controls. A flex row can align one **edge** of a box and the thing that has to line up here is a band in the middle of it, so the row owns three rows and each field becomes a `subgrid` of them — the instrument `DetailListGroup` already uses, for the same reason. Anything that is not a field lands on the control line, which is a placement rather than a wrapper, so a remove button keeps its own element and its own name. `gap` moves the **column** gap only: the row gap is each field's internal label-to-control measure, shared by every field on the row. Below the viewport cutover it is one column at full width — three controls and an action do not fit a phone, and that is also where the alignment problem stops existing. |
|
|
231
|
+
| `QuietActions` | A value with an action attached, where the action is **quiet until someone reaches for it** — a copyable identifier, a row's remove, a list item's open. Five copyable digests on one card meant five copy buttons competing with the five values they applied to. The space is **reserved, not collapsed**: the action fades rather than appears, so nothing moves when a pointer arrives. Three things make it safe rather than merely quiet — the resting state sits behind `@media (hover: hover)`, so a touch screen with no hover to reveal anything gets a visible control instead of an invisible tappable one; it reveals on `:focus-within` as well as hover, because opacity does not remove an element from the tab order and a keyboard must never land on something it cannot see; and opacity is not `visibility`, so the action is announced, named and operable throughout. The actions are a slot, the same one `Card` publishes, rather than a convention about which children count. |
|
|
228
232
|
|
|
229
233
|
## Layout
|
|
230
234
|
|
|
@@ -270,7 +274,7 @@ single screen by claiming its path from an app module.
|
|
|
270
274
|
|
|
271
275
|
| Export | Use |
|
|
272
276
|
|---|---|
|
|
273
|
-
| `UiTextProvider`, `Trans`, `useUiText`, `useStrings`, `resolveUiText`, `DEFAULT_STRINGS` | The `UiText` seam: descriptors for props, `Trans` for body copy, and framework strings through one resolver. `LocaleProvider` is the batteries-included catalog layer and refuses missing target-locale entries. |
|
|
277
|
+
| `UiTextProvider`, `Trans`, `useUiText`, `useStrings`, `resolveUiText`, `DEFAULT_STRINGS` | The `UiText` seam: descriptors for props, `Trans` for body copy, and framework strings through one resolver. `LocaleProvider` is the batteries-included catalog layer and refuses missing target-locale entries. A packaged module's nav label is a `FrameworkText` (`{ framework: "admin" }`), which `useUiText` reads from the active `TerpStrings` rather than the app's messages. |
|
|
274
278
|
|
|
275
279
|
## Testing components
|
|
276
280
|
|
|
@@ -281,5 +285,25 @@ file plus an explicit `afterEach(cleanup)` (the default environment is node). Ru
|
|
|
281
285
|
npm run -w @terpjs/react-core typecheck && npm run -w @terpjs/react-core test
|
|
282
286
|
```
|
|
283
287
|
|
|
284
|
-
|
|
285
|
-
|
|
288
|
+
jsdom has no `HTMLDialogElement.showModal/close`; `vitest.setup.ts` calls
|
|
289
|
+
`installDialogPolyfill()` from `@terpjs/react-core/testing` to supply them, so components may
|
|
290
|
+
use the native `<dialog>` freely.
|
|
291
|
+
|
|
292
|
+
### Testing an app that uses these components
|
|
293
|
+
|
|
294
|
+
A generated app's `frontend/vitest.setup.ts` installs the same polyfill, so an app's unit test
|
|
295
|
+
can open a `ConfirmDialog`, click Confirm or Cancel, or press Escape
|
|
296
|
+
(`fireEvent.keyDown(dialog, { key: "Escape" })`) and get what a browser would do — including
|
|
297
|
+
a pending dialog refusing to close. An app whose setup file predates it, or has diverged from
|
|
298
|
+
the template's, adds the call itself:
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
import { installDialogPolyfill } from "@terpjs/react-core/testing";
|
|
302
|
+
|
|
303
|
+
installDialogPolyfill();
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
`@terpjs/react-core/testing` is for test setup only and is not re-exported from the package
|
|
307
|
+
root, so none of it reaches a production bundle (ADR 0155). What the polyfill does not
|
|
308
|
+
reproduce — focus moving into the dialog and back to its opener, the inert page behind it — is
|
|
309
|
+
the Playwright suite's to test, in a real browser.
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terpjs/react-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.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": {
|
|
7
7
|
".": "./src/index.ts",
|
|
8
|
-
"./layout.manifest.json": "./src/layout.manifest.json"
|
|
8
|
+
"./layout.manifest.json": "./src/layout.manifest.json",
|
|
9
|
+
"./testing": "./src/testing.ts"
|
|
9
10
|
},
|
|
10
11
|
"types": "./src/index.ts",
|
|
11
12
|
"scripts": {
|
|
@@ -13,8 +14,8 @@
|
|
|
13
14
|
"test": "vitest run"
|
|
14
15
|
},
|
|
15
16
|
"dependencies": {
|
|
16
|
-
"@tanstack/react-router": "^1.170.
|
|
17
|
-
"@terpjs/contract": "^0.
|
|
17
|
+
"@tanstack/react-router": "^1.170.38",
|
|
18
|
+
"@terpjs/contract": "^0.28.0"
|
|
18
19
|
},
|
|
19
20
|
"peerDependencies": {
|
|
20
21
|
"react": "^19.0.0",
|
|
@@ -26,7 +27,7 @@
|
|
|
26
27
|
"@types/react": "^19.3.0",
|
|
27
28
|
"@types/react-dom": "^19.3.0",
|
|
28
29
|
"@vitejs/plugin-react": "^6.1.1",
|
|
29
|
-
"jsdom": "^30.0
|
|
30
|
+
"jsdom": "^30.1.0",
|
|
30
31
|
"react": "^19.3.0",
|
|
31
32
|
"react-dom": "^19.3.0",
|
|
32
33
|
"typescript": "^5.9.3",
|
package/src/Field.test.tsx
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
import { cleanup, render, screen } from "@testing-library/react";
|
|
3
3
|
import { afterEach, describe, expect, it } from "vitest";
|
|
4
4
|
|
|
5
|
-
import { Field } from "./Field";
|
|
5
|
+
import { Field, FieldRow } from "./Field";
|
|
6
|
+
import { TERP_STYLES_CSS } from "./styles";
|
|
6
7
|
import { UiTextProvider } from "./uiText";
|
|
8
|
+
import { Button } from "./ui/Button";
|
|
7
9
|
import { Input } from "./ui/Input";
|
|
8
10
|
import { Select } from "./ui/Select";
|
|
9
11
|
import { Textarea } from "./ui/Textarea";
|
|
@@ -157,4 +159,96 @@ describe("Select / Textarea primitives", () => {
|
|
|
157
159
|
expect(screen.getByLabelText("Status")).toBeInTheDocument();
|
|
158
160
|
expect(screen.getByLabelText("Notes")).toBeInTheDocument();
|
|
159
161
|
});
|
|
162
|
+
|
|
163
|
+
it("emits no messages envelope for a field that has nothing to say", () => {
|
|
164
|
+
// The guard that makes the envelope safe to add at all. Emitted unconditionally it
|
|
165
|
+
// would put a grid row and a gap inside every field in every app carrying neither a
|
|
166
|
+
// hint nor an error -- which is most of them -- and the change would arrive as a few
|
|
167
|
+
// pixels of drift on screens nobody touched. Asserted as absence, because a present
|
|
168
|
+
// but empty box is exactly the failure and it renders as nothing visible.
|
|
169
|
+
render(
|
|
170
|
+
<Field label="Plain">
|
|
171
|
+
<Input />
|
|
172
|
+
</Field>,
|
|
173
|
+
);
|
|
174
|
+
expect(
|
|
175
|
+
document.querySelector('[data-terp="field-messages"]'),
|
|
176
|
+
"a field with no messages renders the DOM it rendered before",
|
|
177
|
+
).toBeNull();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("puts a hint and an error in one envelope, still described and still an alert", () => {
|
|
181
|
+
// Both in one box, which is what lets a field occupy a single messages LINE inside a
|
|
182
|
+
// FieldRow -- two loose spans would take two of the row's three lines and push every
|
|
183
|
+
// other field's control down with them.
|
|
184
|
+
//
|
|
185
|
+
// The wiring has to survive the move, and the two halves survive for different
|
|
186
|
+
// reasons: the control must be described by BOTH ids, and the error must keep
|
|
187
|
+
// role="alert", because aria-describedby is read when focus arrives and says nothing
|
|
188
|
+
// about an error that appears on submit. Dropping either would be invisible here
|
|
189
|
+
// without asserting it.
|
|
190
|
+
render(
|
|
191
|
+
<Field label="Both" hint="A hint" error="Required.">
|
|
192
|
+
<Input />
|
|
193
|
+
</Field>,
|
|
194
|
+
);
|
|
195
|
+
const envelope = document.querySelector('[data-terp="field-messages"]');
|
|
196
|
+
expect(envelope, "one envelope holds both").not.toBeNull();
|
|
197
|
+
const hint = envelope!.querySelector('[data-terp="field-hint"]')!;
|
|
198
|
+
const error = envelope!.querySelector('[data-terp="field-error"]')!;
|
|
199
|
+
expect(hint.textContent).toBe("A hint");
|
|
200
|
+
expect(error.textContent).toBe("Required.");
|
|
201
|
+
expect(error.getAttribute("role")).toBe("alert");
|
|
202
|
+
|
|
203
|
+
const control = screen.getByLabelText("Both");
|
|
204
|
+
const described = (control.getAttribute("aria-describedby") ?? "").split(" ");
|
|
205
|
+
expect(described, "the hint is still pointed at").toContain(hint.id);
|
|
206
|
+
expect(described, "and so is the error").toContain(error.id);
|
|
207
|
+
expect(control.getAttribute("aria-invalid")).toBe("true");
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("gives a FieldRow its fields and leaves everything else as itself", () => {
|
|
211
|
+
// The row is a PLACEMENT, not a wrapper: a child that is not a field keeps its own
|
|
212
|
+
// element and its own accessible name, and the sheet moves it to the control line by
|
|
213
|
+
// selector rather than by nesting it in something. Asserted on the DOM because that is
|
|
214
|
+
// this lane's half -- the tracks are pinned in styles.test.ts and the measured result
|
|
215
|
+
// in the workbench's computed lane.
|
|
216
|
+
render(
|
|
217
|
+
<FieldRow>
|
|
218
|
+
<Field label="A">
|
|
219
|
+
<Input />
|
|
220
|
+
</Field>
|
|
221
|
+
<Field label="B" hint="Why">
|
|
222
|
+
<Input />
|
|
223
|
+
</Field>
|
|
224
|
+
<Button>Remove</Button>
|
|
225
|
+
</FieldRow>,
|
|
226
|
+
);
|
|
227
|
+
const row = document.querySelector('[data-terp="field-row"]')!;
|
|
228
|
+
expect(row.querySelectorAll('[data-terp="field"]')).toHaveLength(2);
|
|
229
|
+
expect(
|
|
230
|
+
row.querySelector("button"),
|
|
231
|
+
"a non-field child is rendered as itself, not wrapped",
|
|
232
|
+
).toBe(screen.getByRole("button", { name: "Remove" }));
|
|
233
|
+
// The default gap stamps nothing, so the base rule stands rather than being restated
|
|
234
|
+
// in the DOM -- the idiom Stack, Grid and DetailList all use.
|
|
235
|
+
expect(row.getAttribute("data-gap")).toBeNull();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("stamps a FieldRow gap the sheet actually has a rule for", () => {
|
|
239
|
+
// A gap whose roll-call entry is missing stamps an attribute that styles nothing, which
|
|
240
|
+
// is a prop that silently does not work. The token set is closed, so this reads the
|
|
241
|
+
// sheet rather than trusting the type to have been kept in step with it.
|
|
242
|
+
render(
|
|
243
|
+
<FieldRow gap={4}>
|
|
244
|
+
<Field label="C">
|
|
245
|
+
<Input />
|
|
246
|
+
</Field>
|
|
247
|
+
</FieldRow>,
|
|
248
|
+
);
|
|
249
|
+
expect(document.querySelector('[data-terp="field-row"]')!.getAttribute("data-gap")).toBe(
|
|
250
|
+
"4",
|
|
251
|
+
);
|
|
252
|
+
expect(TERP_STYLES_CSS).toContain('[data-terp="field-row"][data-gap="4"]');
|
|
253
|
+
});
|
|
160
254
|
});
|
package/src/Field.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { cloneElement, isValidElement, useId } from "react";
|
|
2
|
-
import type { ReactNode } from "react";
|
|
2
|
+
import type { HTMLAttributes, ReactNode } from "react";
|
|
3
3
|
|
|
4
|
+
import type { SpaceToken } from "./layout";
|
|
4
5
|
import { injectTerpStyles } from "./styles";
|
|
5
6
|
import { mergeDescribedBy, useControlMessages } from "./ui/controlMessages";
|
|
6
7
|
import { useUiText } from "./uiText";
|
|
@@ -90,3 +91,71 @@ export function Field({ label, children, error, hint }: FieldProps) {
|
|
|
90
91
|
</div>
|
|
91
92
|
);
|
|
92
93
|
}
|
|
94
|
+
|
|
95
|
+
export interface FieldRowProps extends Omit<HTMLAttributes<HTMLDivElement>, "style"> {
|
|
96
|
+
/**
|
|
97
|
+
* Distance between the columns, as a step on the token spacing scale (default `2`).
|
|
98
|
+
*
|
|
99
|
+
* The COLUMN gap only. The row gap is the field's own label-to-control distance and is
|
|
100
|
+
* not a caller's to move: it is shared by every field in the row, so changing it from
|
|
101
|
+
* the outside would be changing the internal measure of each one.
|
|
102
|
+
*/
|
|
103
|
+
gap?: SpaceToken;
|
|
104
|
+
/** The fields, and the actions that belong beside them. */
|
|
105
|
+
children: ReactNode;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Several {@link Field}s side by side, with their labels, their controls and their
|
|
110
|
+
* messages each on a shared line.
|
|
111
|
+
*
|
|
112
|
+
* It exists because a row of fields had no correct alignment, and both wrong ones were
|
|
113
|
+
* reachable by an ordinary `Stack`. A field is as tall as its label, its control AND
|
|
114
|
+
* whatever it has to say, so in a flex row:
|
|
115
|
+
*
|
|
116
|
+
* - `align="end"` lines up the BOTTOMS, which is the messages. A field that grows a hint
|
|
117
|
+
* lifts its own control above its neighbours', and a bare action button beside it sinks
|
|
118
|
+
* to the depth of the longest error on the row. Measured at 44px on a three-field row
|
|
119
|
+
* the moment one hint appeared.
|
|
120
|
+
* - `align="start"` lines up the TOPS, which fixes the fields and breaks the button: with
|
|
121
|
+
* no label of its own it rides up level with the labels instead of the controls.
|
|
122
|
+
*
|
|
123
|
+
* Neither is a bug in `Stack`. A flex row can align one edge of a box, and the thing that
|
|
124
|
+
* has to line up here is a band in the MIDDLE of it, which needs the boxes to share tracks
|
|
125
|
+
* rather than edges. So the row owns three rows and each field becomes a subgrid of them --
|
|
126
|
+
* the same instrument {@link DetailListGroup} uses to share one label column across several
|
|
127
|
+
* lists, for the same reason: the alternative is each box measuring itself.
|
|
128
|
+
*
|
|
129
|
+
* Anything that is not a field lands on the control line. That covers the case the row was
|
|
130
|
+
* written for -- a remove button, an add button, a unit select beside an amount -- and it
|
|
131
|
+
* is a placement rather than a wrapper, so the child stays whatever it was.
|
|
132
|
+
*
|
|
133
|
+
* ```tsx
|
|
134
|
+
* <FieldRow>
|
|
135
|
+
* <Field label={field}><Combobox … /></Field>
|
|
136
|
+
* <Field label={handling}><Select … /></Field>
|
|
137
|
+
* <Field label={reason} hint={whyItIsRequired}><Select … /></Field>
|
|
138
|
+
* <Button variant="ghost" icon={<Icon name="trash" />} aria-label={remove} />
|
|
139
|
+
* </FieldRow>
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* **Below the framework's viewport cutover it becomes one column**, each field full width.
|
|
143
|
+
* Three controls and an action do not fit a phone at any gap, and a row that kept its
|
|
144
|
+
* tracks there would either scroll sideways or squeeze every control to unusable. One
|
|
145
|
+
* column is also where the alignment problem stops existing, so nothing is lost by
|
|
146
|
+
* dropping the subgrid with it.
|
|
147
|
+
*/
|
|
148
|
+
export function FieldRow({ gap, children, ...rest }: FieldRowProps) {
|
|
149
|
+
return (
|
|
150
|
+
<div
|
|
151
|
+
{...rest}
|
|
152
|
+
data-terp="field-row"
|
|
153
|
+
// The gap roll-call's idiom: an unset gap stamps nothing and leaves the base rule
|
|
154
|
+
// standing, rather than restating the default in the DOM.
|
|
155
|
+
data-gap={gap === undefined ? undefined : String(gap)}
|
|
156
|
+
>
|
|
157
|
+
{children}
|
|
158
|
+
</div>
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
package/src/LoginView.test.tsx
CHANGED
|
@@ -87,3 +87,36 @@ describe("LoginView accessible names", () => {
|
|
|
87
87
|
expect(password).toHaveAttribute("autocomplete", "current-password");
|
|
88
88
|
});
|
|
89
89
|
});
|
|
90
|
+
|
|
91
|
+
describe("LoginView named parts", () => {
|
|
92
|
+
it("names each credential and the submit button for a caller that cannot read the labels", async () => {
|
|
93
|
+
// `@terpjs/conformance` signs in to every Terp app through this screen, and it used to find
|
|
94
|
+
// the controls by their English names — so an app that boots in any other locale failed its
|
|
95
|
+
// own conformance suite before it had written a line. It finds them by marker path now,
|
|
96
|
+
// which makes each path a cross-package contract: it has to land on exactly the control the
|
|
97
|
+
// helper means to fill. The dev fill button is rendered so a submit part wrapped around the
|
|
98
|
+
// wrong button, or around both, fails rather than passing by being the only candidate.
|
|
99
|
+
stubFetch();
|
|
100
|
+
const { container } = render(
|
|
101
|
+
<TerpProvider baseUrl="https://api.test">
|
|
102
|
+
<LoginView devCredentials={{ email: "admin@example.test", password: "pw" }} />
|
|
103
|
+
</TerpProvider>,
|
|
104
|
+
);
|
|
105
|
+
await screen.findByRole("heading", { name: "Sign in" });
|
|
106
|
+
|
|
107
|
+
const only = (selector: string) => {
|
|
108
|
+
const found = container.querySelectorAll(selector);
|
|
109
|
+
expect(found, selector).toHaveLength(1);
|
|
110
|
+
return found[0];
|
|
111
|
+
};
|
|
112
|
+
expect(only('[data-terp="login-email"] [data-terp="input"]')).toBe(
|
|
113
|
+
screen.getByLabelText("Email"),
|
|
114
|
+
);
|
|
115
|
+
expect(only('[data-terp="login-password"] [data-terp="input"]')).toBe(
|
|
116
|
+
screen.getByLabelText("Password"),
|
|
117
|
+
);
|
|
118
|
+
expect(only('[data-terp="login-submit"] [data-terp="button"]')).toBe(
|
|
119
|
+
screen.getByRole("button", { name: "Sign in" }),
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
});
|
package/src/LoginView.tsx
CHANGED
|
@@ -48,6 +48,13 @@ export interface DevCredentials {
|
|
|
48
48
|
* and the four call sites say what they mean. And the submit button hand-rolled a busy state
|
|
49
49
|
* out of `disabled` plus a swapped label; `loading` is that state, and it adds the spinner and
|
|
50
50
|
* the `aria-busy` the hand-rolled version never had.
|
|
51
|
+
*
|
|
52
|
+
* The two credentials and the submit button each sit in a named part — `login-email`,
|
|
53
|
+
* `login-password`, `login-submit` — for anything that has to find them without reading them.
|
|
54
|
+
* Their labels are in whatever language the app ships, so an accessible name cannot be the
|
|
55
|
+
* handle for a locale-agnostic caller, and `@terpjs/conformance` drives this screen in every
|
|
56
|
+
* locale. The parts are `display: contents`: they generate no box, so each field and the button
|
|
57
|
+
* stay grid items of the form and its gap falls between them exactly as before.
|
|
51
58
|
*/
|
|
52
59
|
export function LoginView({ ssoProviders = [], devCredentials }: LoginViewProps = {}) {
|
|
53
60
|
const auth = useAuth();
|
|
@@ -103,29 +110,35 @@ export function LoginView({ ssoProviders = [], devCredentials }: LoginViewProps
|
|
|
103
110
|
`Field` wraps the control in a `<label>`, which is why it needs no id wiring.
|
|
104
111
|
The comment below is left because it dates the omission — an autocomplete
|
|
105
112
|
token was considered for these fields and a label was not. */}
|
|
106
|
-
<
|
|
107
|
-
<
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
113
|
+
<div data-terp="login-email">
|
|
114
|
+
<Field label={strings.email}>
|
|
115
|
+
<Input
|
|
116
|
+
type="email"
|
|
117
|
+
// Neither field declared an autocomplete token, so no password manager offered to
|
|
118
|
+
// fill or save this form — the one place in the framework where that matters most.
|
|
119
|
+
autoComplete="username"
|
|
120
|
+
value={email}
|
|
121
|
+
onChange={(event) => setEmail(event.target.value)}
|
|
122
|
+
required
|
|
123
|
+
/>
|
|
124
|
+
</Field>
|
|
125
|
+
</div>
|
|
126
|
+
<div data-terp="login-password">
|
|
127
|
+
<Field label={strings.password}>
|
|
128
|
+
<Input
|
|
129
|
+
type="password"
|
|
130
|
+
autoComplete="current-password"
|
|
131
|
+
value={password}
|
|
132
|
+
onChange={(event) => setPassword(event.target.value)}
|
|
133
|
+
required
|
|
134
|
+
/>
|
|
135
|
+
</Field>
|
|
136
|
+
</div>
|
|
137
|
+
<div data-terp="login-submit">
|
|
138
|
+
<Button type="submit" fullWidth loading={busy}>
|
|
139
|
+
{busy ? strings.signingIn : strings.signIn}
|
|
140
|
+
</Button>
|
|
141
|
+
</div>
|
|
129
142
|
{devCredentials ? (
|
|
130
143
|
<Button
|
|
131
144
|
type="button"
|