@terpjs/react-core 0.27.0 → 0.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -7
- package/package.json +4 -3
- package/src/Field.test.tsx +95 -1
- package/src/Field.tsx +70 -1
- package/src/LoginView.test.tsx +33 -0
- package/src/LoginView.tsx +36 -23
- package/src/Meter.test.tsx +304 -0
- package/src/Meter.tsx +159 -0
- package/src/QuietActions.test.tsx +118 -0
- package/src/QuietActions.tsx +75 -0
- package/src/UserMenu.test.tsx +23 -0
- package/src/UserMenu.tsx +9 -1
- package/src/admin/AccessAdmin.tsx +6 -2
- package/src/admin/AuditLogAdmin.tsx +1 -1
- package/src/admin/access.test.tsx +2 -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 +99 -0
- package/src/dataview/DataViewPagination.tsx +2 -2
- package/src/dataview/DataViewToolbar.tsx +2 -2
- package/src/dataview/README.md +5 -3
- package/src/dataview/index.ts +1 -1
- package/src/dataview/internal.tsx +102 -21
- package/src/dataview/types.ts +11 -37
- package/src/errorMessages.test.tsx +21 -0
- package/src/errorMessages.tsx +33 -19
- package/src/index.ts +9 -3
- package/src/locale.test.tsx +164 -4
- package/src/locale.tsx +105 -10
- package/src/markers.test.ts +33 -11
- package/src/ssr.test.tsx +18 -0
- package/src/styles.test.ts +265 -14
- package/src/styles.ts +424 -49
- package/src/testing.test.tsx +162 -0
- package/src/testing.ts +84 -0
- package/src/tokens.guard.test.ts +11 -6
- package/src/ui/controlMessages.tsx +27 -14
- package/src/uiText.literals.test.ts +19 -6
- package/src/uiText.test.tsx +143 -2
- package/src/uiText.tsx +287 -15
- package/vitest.setup.ts +7 -20
package/src/uiText.tsx
CHANGED
|
@@ -1,9 +1,79 @@
|
|
|
1
1
|
import { createContext, useCallback, useContext, useMemo } from "react";
|
|
2
2
|
import type { ReactNode } from "react";
|
|
3
|
-
import type { UiText } from "@terpjs/contract";
|
|
3
|
+
import type { FrameworkText, UiText } from "@terpjs/contract";
|
|
4
4
|
|
|
5
5
|
export type { UiText } from "@terpjs/contract";
|
|
6
6
|
|
|
7
|
+
// The keys a manifest's `FrameworkText` may name are this table's, so a misspelt one is a
|
|
8
|
+
// typecheck error where the manifest is written rather than an empty label on screen. Only its
|
|
9
|
+
// plain-string keys: a nav label has no count to choose a plural form by.
|
|
10
|
+
declare module "@terpjs/contract" {
|
|
11
|
+
interface TerpFrameworkStrings extends Pick<TerpStrings, FrameworkTextKey> {}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A framework string whose wording depends on a count: one form per CLDR plural category.
|
|
16
|
+
*
|
|
17
|
+
* English and Dutch each have two categories, `one` and `other`; Polish has four. The form is
|
|
18
|
+
* chosen by the count under the active locale's `Intl.PluralRules`, so "1 result" and
|
|
19
|
+
* "2 results" are two sentences rather than one sentence with "(s)" in it, and `other` answers
|
|
20
|
+
* for a category the catalog has no form for. Each form keeps the same `{placeholder}`s,
|
|
21
|
+
* because a category is a grammatical class and not a number: in Russian, 21 takes the `one`
|
|
22
|
+
* form.
|
|
23
|
+
*/
|
|
24
|
+
export type PluralText = Readonly<Partial<Record<Intl.LDMLPluralRule, string>>> & {
|
|
25
|
+
readonly other: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Every CLDR plural category; a {@link PluralText} names its forms from these. */
|
|
29
|
+
const PLURAL_CATEGORIES: readonly string[] = ["zero", "one", "two", "few", "many", "other"];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Refuse a count-bearing framework string that is not a {@link PluralText}.
|
|
33
|
+
*
|
|
34
|
+
* Structure only, and deliberately: which categories a language uses comes from the runtime's
|
|
35
|
+
* ICU data, which differs between browsers and versions — French is `one`/`other` on some and
|
|
36
|
+
* `one`/`many`/`other` on newer ones — so a check against it would pass a catalog in one browser
|
|
37
|
+
* and blank the app in the next. The shipped catalogs are held to their languages' categories
|
|
38
|
+
* by react-core's own tests instead, where the ICU is fixed. *where* names the table in the
|
|
39
|
+
* message: a locale, or a provider given its strings directly.
|
|
40
|
+
*/
|
|
41
|
+
export function assertPluralShape(where: string, key: string, value: unknown): void {
|
|
42
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`${where} framework string "${key}" counts something, so it takes one form per plural ` +
|
|
45
|
+
'category — at least { other: "…" }, usually { one: "…", other: "…" } — not a single string.',
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const forms = value as Record<string, unknown>;
|
|
49
|
+
const stray = Object.keys(forms).find((category) => !PLURAL_CATEGORIES.includes(category));
|
|
50
|
+
if (stray !== undefined) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`${where} framework string "${key}" has a "${stray}" form, which is not a plural ` +
|
|
53
|
+
`category (${PLURAL_CATEGORIES.join(", ")}).`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
for (const category of ["other", ...Object.keys(forms)]) {
|
|
57
|
+
const form = forms[category];
|
|
58
|
+
if (typeof form !== "string" || form.trim() === "") {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`${where} framework string "${key}" has no "${category}" form` +
|
|
61
|
+
(category === "other" ? ", which every language uses." : "."),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Whether *text* is a {@link PluralText} rather than a {@link UiText}. */
|
|
68
|
+
export function isPluralText(text: UiText | PluralText): text is PluralText {
|
|
69
|
+
return typeof text === "object" && "other" in text;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The `TerpStrings` keys that hold one plain string, which a `FrameworkText` may name. */
|
|
73
|
+
type FrameworkTextKey = {
|
|
74
|
+
[K in keyof TerpStrings]: TerpStrings[K] extends string ? K : never;
|
|
75
|
+
}[keyof TerpStrings];
|
|
76
|
+
|
|
7
77
|
/**
|
|
8
78
|
* A piece of user-facing text: either a plain string (used as-is) or a message
|
|
9
79
|
* descriptor — a stable `id` for a translation catalog plus the source-language
|
|
@@ -181,8 +251,8 @@ export interface TerpStrings {
|
|
|
181
251
|
accessNeverAssignable: string;
|
|
182
252
|
/** Access pane: shown on a module that has not opted into per-module roles. */
|
|
183
253
|
accessNotAssignable: string;
|
|
184
|
-
/** Access pane: warns that some of the module's routes declared no operation
|
|
185
|
-
accessUnexplainedRoutes:
|
|
254
|
+
/** Access pane: warns that some of the module's routes declared no operation; `{count}`. */
|
|
255
|
+
accessUnexplainedRoutes: PluralText;
|
|
186
256
|
/** Access pane: the note that this screen shows the declared model, not who holds what. */
|
|
187
257
|
accessDeclaredOnly: string;
|
|
188
258
|
/** Assignment panel: the section heading on a person's or group's detail screen. */
|
|
@@ -281,6 +351,8 @@ export interface TerpStrings {
|
|
|
281
351
|
whenColumn: string;
|
|
282
352
|
/** Audit admin: expanded row's payload heading. */
|
|
283
353
|
details: string;
|
|
354
|
+
/** Audit admin: expanded row's label for the request that caused the event. */
|
|
355
|
+
requestLabel: string;
|
|
284
356
|
/** Generic success toast after a saved mutation. */
|
|
285
357
|
saved: string;
|
|
286
358
|
/** Generic failure toast when a request did not go through. */
|
|
@@ -303,6 +375,91 @@ export interface TerpStrings {
|
|
|
303
375
|
selectDate: string;
|
|
304
376
|
/** DateRangePicker: trigger text before a range is chosen. */
|
|
305
377
|
selectDateRange: string;
|
|
378
|
+
// DataView. Framework copy like everything above, so a locale catalog translates it and the
|
|
379
|
+
// completeness check requires it; `DataView`'s per-instance `strings` prop still wins. Kept
|
|
380
|
+
// apart from the similar keys above (`loading`, `errorTitle`, `moreActions`,
|
|
381
|
+
// `clearSelection`) on purpose: the English differs, and a translation may too.
|
|
382
|
+
/** DataView: the search box's placeholder and accessible name. */
|
|
383
|
+
dataViewSearchPlaceholder: string;
|
|
384
|
+
/** DataView: clears the search box. */
|
|
385
|
+
dataViewClearSearch: string;
|
|
386
|
+
/** DataView: the toolbar action that resets the caller's filters. */
|
|
387
|
+
dataViewClearFilters: string;
|
|
388
|
+
/** DataView: the column-settings menu trigger. */
|
|
389
|
+
dataViewViewOptions: string;
|
|
390
|
+
/** DataView: heading of the column list inside the column-settings menu. */
|
|
391
|
+
dataViewColumns: string;
|
|
392
|
+
/** DataView: moves a column earlier; prefixed to the column's name. */
|
|
393
|
+
dataViewMoveUp: string;
|
|
394
|
+
/** DataView: moves a column later; prefixed to the column's name. */
|
|
395
|
+
dataViewMoveDown: string;
|
|
396
|
+
/** DataView: switches to the table layout. */
|
|
397
|
+
dataViewTableView: string;
|
|
398
|
+
/** DataView: switches to the card layout. */
|
|
399
|
+
dataViewCardView: string;
|
|
400
|
+
/** DataView: the page-size selector. */
|
|
401
|
+
dataViewPageSize: string;
|
|
402
|
+
/** DataView: the footer's result range; `{from}`, `{to}` and `{total}`, plural by `{total}`. */
|
|
403
|
+
dataViewResultsRange: PluralText;
|
|
404
|
+
/** DataView: the footer's page position; `{page}` and `{pages}` are replaced. */
|
|
405
|
+
dataViewPageOf: string;
|
|
406
|
+
/** DataView: pagination, to the first page. */
|
|
407
|
+
dataViewFirstPage: string;
|
|
408
|
+
/** DataView: pagination, one page back. */
|
|
409
|
+
dataViewPreviousPage: string;
|
|
410
|
+
/** DataView: pagination, one page forward. */
|
|
411
|
+
dataViewNextPage: string;
|
|
412
|
+
/** DataView: pagination, to the last page. */
|
|
413
|
+
dataViewLastPage: string;
|
|
414
|
+
/** DataView: the header checkbox that selects every row on the page. */
|
|
415
|
+
dataViewSelectAllPage: string;
|
|
416
|
+
/** DataView: a row's selection checkbox. */
|
|
417
|
+
dataViewSelectRow: string;
|
|
418
|
+
/** DataView: the selection count; `{count}` is replaced. */
|
|
419
|
+
dataViewSelected: string;
|
|
420
|
+
/** DataView: widens the selection to every result; `{total}`, plural by it. */
|
|
421
|
+
dataViewSelectAllResults: PluralText;
|
|
422
|
+
/** DataView: clears the row selection. */
|
|
423
|
+
dataViewClearSelection: string;
|
|
424
|
+
/** DataView: the row-action and batch-action overflow trigger. */
|
|
425
|
+
dataViewMoreActions: string;
|
|
426
|
+
/** DataView: the actions column header. */
|
|
427
|
+
dataViewActions: string;
|
|
428
|
+
/** DataView: a row's open button; `{label}` is replaced by the record's name. */
|
|
429
|
+
dataViewOpenRow: string;
|
|
430
|
+
/** DataView: opens a row's detail panel. */
|
|
431
|
+
dataViewExpandRow: string;
|
|
432
|
+
/** DataView: closes a row's detail panel. */
|
|
433
|
+
dataViewCollapseRow: string;
|
|
434
|
+
/** DataView: the empty state when the caller passes no `emptyMessage`. */
|
|
435
|
+
dataViewEmpty: string;
|
|
436
|
+
/** DataView: the first load's placeholder. */
|
|
437
|
+
dataViewLoading: string;
|
|
438
|
+
/** DataView: shown while a refetch replaces rows already on screen. */
|
|
439
|
+
dataViewRefreshing: string;
|
|
440
|
+
/** DataView: the error state's title. */
|
|
441
|
+
dataViewErrorTitle: string;
|
|
442
|
+
/** DataView: a column's resize handle; prefixed to the column's name. */
|
|
443
|
+
dataViewResizeColumn: string;
|
|
444
|
+
// The wording for the platform's own `AppError` codes, which `useErrorMessage` shows in place
|
|
445
|
+
// of the backend's `detail`. An app's `errorMessages` map still wins for any code it names,
|
|
446
|
+
// including these; a code neither side words falls back to the `detail`.
|
|
447
|
+
/** Error code `bad_request`. */
|
|
448
|
+
errorCodeBadRequest: string;
|
|
449
|
+
/** Error code `validation_failed`. */
|
|
450
|
+
errorCodeValidationFailed: string;
|
|
451
|
+
/** Error code `invalid_token`. */
|
|
452
|
+
errorCodeInvalidToken: string;
|
|
453
|
+
/** Error code `authentication_required`. */
|
|
454
|
+
errorCodeAuthenticationRequired: string;
|
|
455
|
+
/** Error code `permission_denied`. */
|
|
456
|
+
errorCodePermissionDenied: string;
|
|
457
|
+
/** Error code `not_found`. */
|
|
458
|
+
errorCodeNotFound: string;
|
|
459
|
+
/** Error code `conflict`. */
|
|
460
|
+
errorCodeConflict: string;
|
|
461
|
+
/** Error code `stale_data`. */
|
|
462
|
+
errorCodeStaleData: string;
|
|
306
463
|
}
|
|
307
464
|
|
|
308
465
|
export const DEFAULT_STRINGS: TerpStrings = {
|
|
@@ -382,7 +539,10 @@ export const DEFAULT_STRINGS: TerpStrings = {
|
|
|
382
539
|
accessAddsNothing: "Nothing beyond the tier below",
|
|
383
540
|
accessNeverAssignable: "Never assignable per module",
|
|
384
541
|
accessNotAssignable: "Not assignable per module",
|
|
385
|
-
accessUnexplainedRoutes:
|
|
542
|
+
accessUnexplainedRoutes: {
|
|
543
|
+
one: "{count} action in this module has no description yet",
|
|
544
|
+
other: "{count} actions in this module have no description yet",
|
|
545
|
+
},
|
|
386
546
|
accessDeclaredOnly:
|
|
387
547
|
"This is what the application declares. It does not show who holds which role — open a person to see that.",
|
|
388
548
|
moduleAccessTitle: "Access per module",
|
|
@@ -439,18 +599,67 @@ export const DEFAULT_STRINGS: TerpStrings = {
|
|
|
439
599
|
targetColumn: "Target",
|
|
440
600
|
whenColumn: "When",
|
|
441
601
|
details: "Details",
|
|
602
|
+
requestLabel: "Request",
|
|
442
603
|
saved: "Saved",
|
|
443
604
|
requestFailed: "The request failed. Try again.",
|
|
605
|
+
dataViewSearchPlaceholder: "Search…",
|
|
606
|
+
dataViewClearSearch: "Clear search",
|
|
607
|
+
dataViewClearFilters: "Clear filters",
|
|
608
|
+
dataViewViewOptions: "View options",
|
|
609
|
+
dataViewColumns: "Columns",
|
|
610
|
+
dataViewMoveUp: "Move up",
|
|
611
|
+
dataViewMoveDown: "Move down",
|
|
612
|
+
dataViewTableView: "Table view",
|
|
613
|
+
dataViewCardView: "Card view",
|
|
614
|
+
dataViewPageSize: "Rows per page",
|
|
615
|
+
dataViewResultsRange: {
|
|
616
|
+
one: "{from}–{to} of {total} result",
|
|
617
|
+
other: "{from}–{to} of {total} results",
|
|
618
|
+
},
|
|
619
|
+
dataViewPageOf: "Page {page} of {pages}",
|
|
620
|
+
dataViewFirstPage: "First page",
|
|
621
|
+
dataViewPreviousPage: "Previous page",
|
|
622
|
+
dataViewNextPage: "Next page",
|
|
623
|
+
dataViewLastPage: "Last page",
|
|
624
|
+
dataViewSelectAllPage: "Select all rows on this page",
|
|
625
|
+
dataViewSelectRow: "Select row",
|
|
626
|
+
dataViewSelected: "{count} selected",
|
|
627
|
+
dataViewSelectAllResults: {
|
|
628
|
+
one: "Select the {total} result",
|
|
629
|
+
other: "Select all {total} results",
|
|
630
|
+
},
|
|
631
|
+
dataViewClearSelection: "Clear selection",
|
|
632
|
+
dataViewMoreActions: "More actions",
|
|
633
|
+
dataViewActions: "Actions",
|
|
634
|
+
dataViewOpenRow: "Open details: {label}",
|
|
635
|
+
dataViewExpandRow: "Expand row",
|
|
636
|
+
dataViewCollapseRow: "Collapse row",
|
|
637
|
+
dataViewEmpty: "Nothing to show.",
|
|
638
|
+
dataViewLoading: "Loading…",
|
|
639
|
+
dataViewRefreshing: "Refreshing…",
|
|
640
|
+
dataViewErrorTitle: "Could not load data.",
|
|
641
|
+
dataViewResizeColumn: "Resize column",
|
|
642
|
+
errorCodeBadRequest: "The request could not be processed.",
|
|
643
|
+
errorCodeValidationFailed: "Some fields are invalid. Check the form and try again.",
|
|
644
|
+
errorCodeInvalidToken: "Your session is invalid. Sign in again.",
|
|
645
|
+
errorCodeAuthenticationRequired: "Sign in to continue.",
|
|
646
|
+
errorCodePermissionDenied: "You do not have permission to do this.",
|
|
647
|
+
errorCodeNotFound: "This item could not be found.",
|
|
648
|
+
errorCodeConflict: "This conflicts with the current state. Refresh and try again.",
|
|
649
|
+
errorCodeStaleData: "This item was changed by someone else. Refresh and try again.",
|
|
444
650
|
};
|
|
445
651
|
|
|
446
652
|
interface UiTextContextValue {
|
|
447
653
|
strings: TerpStrings;
|
|
448
654
|
resolveText: ResolveUiText;
|
|
655
|
+
/** The locale whose plural rules choose a {@link PluralText}'s form. */
|
|
656
|
+
locale: string;
|
|
449
657
|
}
|
|
450
658
|
|
|
451
659
|
const UiTextContext = createContext<UiTextContextValue>({
|
|
452
660
|
strings: DEFAULT_STRINGS,
|
|
453
661
|
resolveText: resolveUiText,
|
|
662
|
+
locale: "en",
|
|
454
663
|
});
|
|
455
664
|
|
|
456
665
|
export interface UiTextProviderProps {
|
|
@@ -459,9 +668,17 @@ export interface UiTextProviderProps {
|
|
|
459
668
|
/**
|
|
460
669
|
* Custom {@link UiText} resolver — the hook for a real i18n runtime: pass a
|
|
461
670
|
* function that looks descriptors up in the active locale's catalog
|
|
462
|
-
* (falling back to `message`). Defaults to {@link resolveUiText}.
|
|
671
|
+
* (falling back to `message`). Defaults to {@link resolveUiText}. It is handed the app's
|
|
672
|
+
* strings and descriptors only: a manifest's `FrameworkText` is answered from `strings`
|
|
673
|
+
* before it is reached (see {@link useUiText}).
|
|
463
674
|
*/
|
|
464
675
|
resolveText?: ResolveUiText;
|
|
676
|
+
/**
|
|
677
|
+
* The locale code the `strings` are in, whose plural rules choose a {@link PluralText}'s
|
|
678
|
+
* form. `LocaleProvider` passes its active locale; without one the parent's applies, and
|
|
679
|
+
* at the root that is English.
|
|
680
|
+
*/
|
|
681
|
+
locale?: string;
|
|
465
682
|
children: ReactNode;
|
|
466
683
|
}
|
|
467
684
|
|
|
@@ -471,15 +688,22 @@ export interface UiTextProviderProps {
|
|
|
471
688
|
* use the bundled English defaults. An app localises by wrapping its tree
|
|
472
689
|
* once — no per-component wiring, no i18n dependency inside react-core.
|
|
473
690
|
*/
|
|
474
|
-
export function UiTextProvider({ strings, resolveText, children }: UiTextProviderProps) {
|
|
691
|
+
export function UiTextProvider({ strings, resolveText, locale, children }: UiTextProviderProps) {
|
|
475
692
|
const parent = useContext(UiTextContext);
|
|
476
|
-
const value = useMemo<UiTextContextValue>(
|
|
477
|
-
|
|
693
|
+
const value = useMemo<UiTextContextValue>(() => {
|
|
694
|
+
// A table given here directly has not been through LocaleProvider's check, and a
|
|
695
|
+
// count-bearing key written the old way, as one string, would render nothing usable.
|
|
696
|
+
for (const [key, supplied] of Object.entries(strings ?? {})) {
|
|
697
|
+
if (typeof DEFAULT_STRINGS[key as keyof TerpStrings] !== "string") {
|
|
698
|
+
assertPluralShape("UiTextProvider", key, supplied);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
return {
|
|
478
702
|
strings: { ...parent.strings, ...strings },
|
|
479
703
|
resolveText: resolveText ?? parent.resolveText,
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
);
|
|
704
|
+
locale: locale ?? parent.locale,
|
|
705
|
+
};
|
|
706
|
+
}, [parent, strings, resolveText, locale]);
|
|
483
707
|
return <UiTextContext.Provider value={value}>{children}</UiTextContext.Provider>;
|
|
484
708
|
}
|
|
485
709
|
|
|
@@ -488,10 +712,58 @@ export function useStrings(): TerpStrings {
|
|
|
488
712
|
return useContext(UiTextContext).strings;
|
|
489
713
|
}
|
|
490
714
|
|
|
491
|
-
/**
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
715
|
+
/**
|
|
716
|
+
* The form of a {@link PluralText} the active locale's grammar gives *count*.
|
|
717
|
+
*
|
|
718
|
+
* `other` answers for a category the text has no form for — a catalog written against another
|
|
719
|
+
* engine's plural data, or one that leaves a rare category out — so a count always renders a
|
|
720
|
+
* sentence. For an app that renders a count-bearing framework string itself, read from
|
|
721
|
+
* {@link useStrings}.
|
|
722
|
+
*/
|
|
723
|
+
export function usePlural(): (text: PluralText, count: number) => string {
|
|
724
|
+
const { locale } = useContext(UiTextContext);
|
|
725
|
+
return useMemo(() => {
|
|
726
|
+
const rules = new Intl.PluralRules(locale);
|
|
727
|
+
return (text: PluralText, count: number) => text[rules.select(count)] ?? text.other;
|
|
728
|
+
}, [locale]);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* The active {@link UiText} resolver — call it on any `UiText` prop before rendering, and on a
|
|
733
|
+
* manifest's navigation label, which may also be a {@link FrameworkText}.
|
|
734
|
+
*
|
|
735
|
+
* A `FrameworkText` is answered here, from the same table {@link useStrings} returns, and never
|
|
736
|
+
* reaches the resolver a provider was given. That resolver is the app's: `LocaleProvider`'s
|
|
737
|
+
* treats a descriptor's `message` as the app's source-locale text and looks every other locale
|
|
738
|
+
* up in the app's messages, and framework copy is neither. A key the table does not have
|
|
739
|
+
* throws rather than rendering an empty label; the type already refuses one, so this is what
|
|
740
|
+
* holds a manifest the typecheck never saw.
|
|
741
|
+
*/
|
|
742
|
+
export function useUiText(): (text: UiText | FrameworkText) => string {
|
|
743
|
+
const { strings, resolveText } = useContext(UiTextContext);
|
|
744
|
+
return useCallback(
|
|
745
|
+
(text: UiText | FrameworkText) => {
|
|
746
|
+
if (typeof text === "string" || !("framework" in text)) {
|
|
747
|
+
return resolveText(text);
|
|
748
|
+
}
|
|
749
|
+
if (!Object.hasOwn(strings, text.framework)) {
|
|
750
|
+
throw new Error(
|
|
751
|
+
`FrameworkText names "${text.framework}", which is not a framework string. ` +
|
|
752
|
+
"Name a TerpStrings key (for example \"admin\"); an app's own copy is a " +
|
|
753
|
+
"{ id, message } descriptor in frontend/i18n.json.",
|
|
754
|
+
);
|
|
755
|
+
}
|
|
756
|
+
const value: unknown = strings[text.framework];
|
|
757
|
+
if (typeof value !== "string") {
|
|
758
|
+
throw new Error(
|
|
759
|
+
`FrameworkText names "${text.framework}", which counts something and has one form ` +
|
|
760
|
+
"per plural category. A navigation label is one string; name a plain one.",
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
return value;
|
|
764
|
+
},
|
|
765
|
+
[strings, resolveText],
|
|
766
|
+
);
|
|
495
767
|
}
|
|
496
768
|
|
|
497
769
|
/** Props for {@link Trans}: one stable catalog id and its source-language fallback. */
|
package/vitest.setup.ts
CHANGED
|
@@ -5,6 +5,8 @@ import "@testing-library/jest-dom/vitest";
|
|
|
5
5
|
|
|
6
6
|
import { configure } from "@testing-library/dom";
|
|
7
7
|
|
|
8
|
+
import { installDialogPolyfill } from "@terpjs/react-core/testing";
|
|
9
|
+
|
|
8
10
|
// `findBy*` and `waitFor` default to a 1000ms budget, and the component tests spend it on
|
|
9
11
|
// a path that is not a render: a mocked fetch resolving, then the state it sets, then the
|
|
10
12
|
// re-render that finally puts the text on screen. One second does not reliably cover that,
|
|
@@ -93,23 +95,8 @@ if (typeof window !== "undefined") {
|
|
|
93
95
|
window.scrollTo = (() => {}) as typeof window.scrollTo;
|
|
94
96
|
}
|
|
95
97
|
|
|
96
|
-
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
showModal?: () => void;
|
|
102
|
-
close?: () => void;
|
|
103
|
-
};
|
|
104
|
-
if (typeof proto.showModal !== "function") {
|
|
105
|
-
proto.showModal = function showModal(this: HTMLDialogElement) {
|
|
106
|
-
this.setAttribute("open", "");
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
if (typeof proto.close !== "function") {
|
|
110
|
-
proto.close = function close(this: HTMLDialogElement) {
|
|
111
|
-
this.removeAttribute("open");
|
|
112
|
-
this.dispatchEvent(new Event("close"));
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
}
|
|
98
|
+
// jsdom has no `showModal()` / `close()`, and ConfirmDialog calls both. The polyfill is the
|
|
99
|
+
// one every generated app's setup file installs, imported through the same published subpath
|
|
100
|
+
// rather than by relative path, so this suite also proves the export an app resolves exists.
|
|
101
|
+
// A no-op in the node-environment files, which have no HTMLDialogElement.
|
|
102
|
+
installDialogPolyfill();
|