@middag-io/react 0.51.1 → 0.52.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.
Files changed (118) hide show
  1. package/dist-lib/{DateField-lPZsiWfC.js → DateField-CduB6lUv.js} +21 -23
  2. package/dist-lib/{DateRangeField-B7zWB48D.js → DateRangeField-DlA_9i4w.js} +17 -13
  3. package/dist-lib/{FormField-BGqAQLtX.js → FormField-CDdY7-md.js} +2090 -2054
  4. package/dist-lib/{FormPanelBlock-CGgA83Mu.js → FormPanelBlock-zo8tCgtS.js} +1 -2
  5. package/dist-lib/base/blocks/CardGridBlock.d.ts.map +1 -1
  6. package/dist-lib/base/blocks/DenseTableBlock.d.ts.map +1 -1
  7. package/dist-lib/base/blocks/EmptyStateBlock.d.ts.map +1 -1
  8. package/dist-lib/base/blocks/FormPanelBlock.d.ts.map +1 -1
  9. package/dist-lib/base/blocks/MetricCardBlock.d.ts.map +1 -1
  10. package/dist-lib/base/form/FormField.d.ts +8 -1
  11. package/dist-lib/base/form/FormField.d.ts.map +1 -1
  12. package/dist-lib/base/form/fields/DateField.d.ts +1 -1
  13. package/dist-lib/base/form/fields/DateField.d.ts.map +1 -1
  14. package/dist-lib/base/form/fields/DateRangeField.d.ts +3 -1
  15. package/dist-lib/base/form/fields/DateRangeField.d.ts.map +1 -1
  16. package/dist-lib/base/form/fields/MultiSelectField.d.ts +3 -1
  17. package/dist-lib/base/form/fields/MultiSelectField.d.ts.map +1 -1
  18. package/dist-lib/base/form/fields/RadioField.d.ts +3 -1
  19. package/dist-lib/base/form/fields/RadioField.d.ts.map +1 -1
  20. package/dist-lib/base/form/fields/RatingField.d.ts +3 -1
  21. package/dist-lib/base/form/fields/RatingField.d.ts.map +1 -1
  22. package/dist-lib/base/form/fields/SliderField.d.ts +3 -1
  23. package/dist-lib/base/form/fields/SliderField.d.ts.map +1 -1
  24. package/dist-lib/base/form/fields/TagsField.d.ts.map +1 -1
  25. package/dist-lib/base/partials/DataTable/cell-renderers.d.ts.map +1 -1
  26. package/dist-lib/base/partials/DataTable/index.d.ts.map +1 -1
  27. package/dist-lib/base/partials/DataTable/types.d.ts +2 -2
  28. package/dist-lib/base/partials/DataTable/types.d.ts.map +1 -1
  29. package/dist-lib/base/partials/EmptyPlaceholder/index.d.ts.map +1 -1
  30. package/dist-lib/base/shell/BasicShell.d.ts +12 -1
  31. package/dist-lib/base/shell/BasicShell.d.ts.map +1 -1
  32. package/dist-lib/base/shell/BasicShell.js +126 -111
  33. package/dist-lib/base/shell/partials/InlineEditablePanel.js +193 -176
  34. package/dist-lib/base/shell/partials/InspectorContext.js +1 -1
  35. package/dist-lib/base/theme/tailwind-theme.css +13 -5
  36. package/dist-lib/base/theme/theme-bridge.css +8 -0
  37. package/dist-lib/base/theme/themes/classic.css +15 -11
  38. package/dist-lib/base/theme/tokens.css +36 -10
  39. package/dist-lib/base/utils/php-wire.d.ts +33 -0
  40. package/dist-lib/base/utils/php-wire.d.ts.map +1 -0
  41. package/dist-lib/base/utils/php-wire.js +19 -0
  42. package/dist-lib/chunks/{subpath-DjkzcIPp.js → subpath-Bb5vw3A_.js} +21 -23
  43. package/dist-lib/chunks/{subpath-fEhy6n_U.js → subpath-CZB5_RY7.js} +2 -0
  44. package/dist-lib/chunks/subpath-DVHs9lfh.js +29 -0
  45. package/dist-lib/contracts/block-data/data-display.d.ts +12 -287
  46. package/dist-lib/contracts/block-data/data-display.d.ts.map +1 -1
  47. package/dist-lib/contracts/block-data/interactive.d.ts +14 -22
  48. package/dist-lib/contracts/block-data/interactive.d.ts.map +1 -1
  49. package/dist-lib/contracts/block-data/shared.d.ts +2 -17
  50. package/dist-lib/contracts/block-data/shared.d.ts.map +1 -1
  51. package/dist-lib/contracts/generated/index.d.ts +2 -2
  52. package/dist-lib/contracts/generated/schemas.d.ts +1584 -192
  53. package/dist-lib/contracts/generated/schemas.d.ts.map +1 -1
  54. package/dist-lib/contracts/generated/types.d.ts +288 -8
  55. package/dist-lib/contracts/generated/types.d.ts.map +1 -1
  56. package/dist-lib/contracts/page-contract-schema.d.ts +15 -0
  57. package/dist-lib/contracts/page-contract-schema.d.ts.map +1 -1
  58. package/dist-lib/i18n/I18nProvider.js +1 -1
  59. package/dist-lib/i18n/instance.js +1 -1
  60. package/dist-lib/i18n/locales/en/ui.json +1 -0
  61. package/dist-lib/i18n/locales/pt-BR/ui.json +1 -0
  62. package/dist-lib/i18n/register-strings.js +1 -1
  63. package/dist-lib/index.d.ts +2 -2
  64. package/dist-lib/index.d.ts.map +1 -1
  65. package/dist-lib/lib/hooks/use-prefers-reduced-motion.d.ts +6 -0
  66. package/dist-lib/lib/hooks/use-prefers-reduced-motion.d.ts.map +1 -0
  67. package/dist-lib/lib/hooks/use-prefers-reduced-motion.js +21 -0
  68. package/dist-lib/middag-react.css +1 -1
  69. package/dist-lib/middag-react.js +10407 -10012
  70. package/dist-lib/primitives/reui/badge.d.ts +5 -0
  71. package/dist-lib/primitives/reui/badge.d.ts.map +1 -1
  72. package/dist-lib/primitives/reui/badge.js +14 -14
  73. package/dist-lib/primitives/reui/sidebar.d.ts.map +1 -1
  74. package/dist-lib/primitives/reui/sidebar.js +46 -34
  75. package/docs/public/schemas/fragment.json +1258 -19
  76. package/docs/public/schemas/page-contract.json +1258 -19
  77. package/docs/public/schemas/shared-props.json +1258 -19
  78. package/package.json +6 -1
  79. package/src/base/blocks/CardGridBlock.tsx +10 -2
  80. package/src/base/blocks/DenseTableBlock.tsx +18 -11
  81. package/src/base/blocks/DetailPanelBlock.tsx +1 -1
  82. package/src/base/blocks/EmptyStateBlock.tsx +5 -1
  83. package/src/base/blocks/FormPanelBlock.tsx +0 -1
  84. package/src/base/blocks/MetricCardBlock.tsx +9 -2
  85. package/src/base/form/FormField.tsx +40 -7
  86. package/src/base/form/fields/DateField.tsx +3 -3
  87. package/src/base/form/fields/DateRangeField.tsx +19 -14
  88. package/src/base/form/fields/MultiSelectField.tsx +9 -8
  89. package/src/base/form/fields/RadioField.tsx +4 -0
  90. package/src/base/form/fields/RatingField.tsx +4 -0
  91. package/src/base/form/fields/SliderField.tsx +4 -1
  92. package/src/base/form/fields/TagsField.tsx +3 -0
  93. package/src/base/partials/DataTable/cell-renderers.tsx +23 -12
  94. package/src/base/partials/DataTable/index.tsx +5 -2
  95. package/src/base/partials/DataTable/types.ts +2 -5
  96. package/src/base/partials/DetailSection/index.tsx +2 -2
  97. package/src/base/partials/EmptyPlaceholder/index.tsx +7 -3
  98. package/src/base/partials/MarkdownContent/index.tsx +1 -1
  99. package/src/base/shell/BasicShell.tsx +47 -8
  100. package/src/base/shell/partials/InspectorContext.tsx +1 -1
  101. package/src/base/theme/tailwind-theme.css +13 -5
  102. package/src/base/theme/theme-bridge.css +8 -0
  103. package/src/base/theme/themes/classic.css +15 -11
  104. package/src/base/theme/tokens.css +36 -10
  105. package/src/base/utils/php-wire.ts +54 -0
  106. package/src/contracts/block-data/data-display.ts +46 -337
  107. package/src/contracts/block-data/interactive.ts +18 -23
  108. package/src/contracts/block-data/shared.ts +2 -14
  109. package/src/contracts/generated/index.ts +2 -2
  110. package/src/contracts/generated/schemas.ts +312 -9
  111. package/src/contracts/generated/types.ts +314 -8
  112. package/src/i18n/locales/en/ui.json +1 -0
  113. package/src/i18n/locales/pt-BR/ui.json +1 -0
  114. package/src/lib/hooks/use-prefers-reduced-motion.ts +32 -0
  115. package/src/primitives/reui/badge.tsx +19 -14
  116. package/src/primitives/reui/sidebar.tsx +30 -3
  117. package/src/theme.css +11 -5
  118. package/dist-lib/chunks/subpath-DoiyWIYV.js +0 -25
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middag-io/react",
3
- "version": "0.51.1",
3
+ "version": "0.52.0",
4
4
  "type": "module",
5
5
  "description": "MIDDAG shared React UI library for Moodle, WordPress and other Inertia hosts",
6
6
  "license": "Apache-2.0",
@@ -162,9 +162,11 @@
162
162
  "@types/react-dom": "^19.2.3",
163
163
  "@vitejs/plugin-react": "^6.0.1",
164
164
  "@vitest/coverage-v8": "^4.1.12",
165
+ "axe-core": "^4.14.1",
165
166
  "country-flag-icons": "^1.6.17",
166
167
  "eslint": "^10.2.0",
167
168
  "eslint-config-prettier": "^10.1.8",
169
+ "eslint-plugin-jsx-a11y": "^6.10.2",
168
170
  "eslint-plugin-react-hooks": "^7.1.0",
169
171
  "eslint-plugin-react-refresh": "^0.5.2",
170
172
  "i18next-parser": "^9.4.0",
@@ -188,6 +190,9 @@
188
190
  "happy-dom": "^21.0.4",
189
191
  "quick-temp": {
190
192
  "rimraf": "^6.1.4"
193
+ },
194
+ "eslint-plugin-jsx-a11y": {
195
+ "eslint": "$eslint"
191
196
  }
192
197
  },
193
198
  "homepage": "https://ui-docs.middag.io",
@@ -22,6 +22,7 @@ import { useInspector } from "@/base/shell/partials/InspectorContext";
22
22
  import { isDangerIntent } from "@/base/utils/action-intent";
23
23
  import { getIcon } from "@/base/utils/icons";
24
24
  import { interpolate } from "@/base/utils/interpolate";
25
+ import { normalizeRows } from "@/base/utils/php-wire";
25
26
  import type {
26
27
  ActionConfirmation,
27
28
  CardGridBlockData,
@@ -37,6 +38,8 @@ import { Button } from "@/primitives/reui/button";
37
38
  export function CardGridBlock({ block }: BlockProps<CardGridBlockData>): ReactElement {
38
39
  const { t } = useTranslation();
39
40
  const { data } = block;
41
+ // PHP sends an empty row as `[]`: normalise the rows once, here.
42
+ const rows = useMemo(() => normalizeRows(data.rows), [data.rows]);
40
43
  const { select, selectedId, enabled } = useInspector();
41
44
  const variant = data.variant ?? "default";
42
45
  const reloadKey = block.key;
@@ -100,7 +103,7 @@ export function CardGridBlock({ block }: BlockProps<CardGridBlockData>): ReactEl
100
103
  [reloadKey],
101
104
  );
102
105
 
103
- if (data.rows.length === 0 && data.emptyState) {
106
+ if (rows.length === 0 && data.emptyState) {
104
107
  return (
105
108
  <div className="flex flex-col items-center justify-center rounded-lg border border-dashed p-12 text-center">
106
109
  {data.emptyState.title && (
@@ -123,7 +126,7 @@ export function CardGridBlock({ block }: BlockProps<CardGridBlockData>): ReactEl
123
126
 
124
127
  return (
125
128
  <div className="grid grid-cols-[repeat(auto-fill,minmax(300px,1fr))] gap-4">
126
- {data.rows.map((row, index) => {
129
+ {rows.map((row, index) => {
127
130
  // Preserve the row id verbatim — it may be a string/UUID, not just a
128
131
  // number — so selection works for non-numeric ids. Rows without an id
129
132
  // fall back to the index for a stable, unique React key and are not
@@ -156,9 +159,14 @@ export function CardGridBlock({ block }: BlockProps<CardGridBlockData>): ReactEl
156
159
  )}
157
160
  >
158
161
  {hasActions && (
162
+ // Keeps clicks and Enter/Space on the action buttons from also
163
+ // selecting the card (the card's keydown handler would otherwise
164
+ // preventDefault the button's own activation).
159
165
  <div
166
+ role="presentation"
160
167
  className="absolute top-3 right-3 flex items-center gap-1"
161
168
  onClick={(e) => e.stopPropagation()}
169
+ onKeyDown={(e) => e.stopPropagation()}
162
170
  >
163
171
  {editHref && (
164
172
  <Button
@@ -5,7 +5,7 @@
5
5
  * Server interactions use Inertia partial reload.
6
6
  */
7
7
 
8
- import { useCallback, useEffect, useRef, useState, type ReactElement } from "react";
8
+ import { useCallback, useEffect, useMemo, useRef, useState, type ReactElement } from "react";
9
9
  import { router } from "@inertiajs/core";
10
10
 
11
11
  import { usePolling } from "@/base/hooks/usePolling";
@@ -17,6 +17,7 @@ import { useInspector } from "@/base/shell/partials/InspectorContext";
17
17
  import { isDangerIntent } from "@/base/utils/action-intent";
18
18
  import { evaluateCondition } from "@/base/utils/conditions";
19
19
  import { interpolate } from "@/base/utils/interpolate";
20
+ import { knownValue, mapOrUndefined, normalizeFilters, normalizeRows } from "@/base/utils/php-wire";
20
21
  import type { ActionConfirmation, DenseTableBlockData } from "@/contracts/block-data";
21
22
  import type { BlockProps } from "@/engine/registries";
22
23
  import { renderLabel } from "@/i18n/render-label";
@@ -45,9 +46,15 @@ function pageQueryWithoutTableParams(search: string): string {
45
46
  return query ? `?${query}` : "";
46
47
  }
47
48
 
49
+ /** Row action variants the table draws; any other value falls back to the icon. */
50
+ const ROW_ACTION_VARIANTS = ["icon", "button"] as const;
51
+
48
52
  export function DenseTableBlock({ block }: BlockProps<DenseTableBlockData>): ReactElement {
49
53
  const { t } = useTranslation();
50
54
  const { data, key, meta } = block;
55
+ // PHP sends an empty map as `[]`: normalise rows and filters once, here.
56
+ const rows = useMemo(() => normalizeRows(data.rows), [data.rows]);
57
+ const filters = useMemo(() => normalizeFilters(data.filters), [data.filters]);
51
58
  const isLoading = meta?.loading === true;
52
59
  const clientSide = (meta as Record<string, unknown> | undefined)?.clientSide === true;
53
60
  const rowKeyField = (meta?.rowKey as string) ?? "id";
@@ -115,13 +122,13 @@ export function DenseTableBlock({ block }: BlockProps<DenseTableBlockData>): Rea
115
122
  // button appear to do nothing.
116
123
  const hasAutoSelectedRef = useRef(false);
117
124
  useEffect(() => {
118
- if (!hasAutoSelectedRef.current && inspectorEnabled && data.rows.length > 0) {
125
+ if (!hasAutoSelectedRef.current && inspectorEnabled && rows.length > 0) {
119
126
  hasAutoSelectedRef.current = true;
120
- const firstRow = data.rows[0];
127
+ const firstRow = rows[0];
121
128
  const id = firstRow.id ?? firstRow[rowKeyField];
122
129
  if (id != null) select(id as string | number);
123
130
  }
124
- }, [inspectorEnabled, data.rows, rowKeyField, select]);
131
+ }, [inspectorEnabled, rows, rowKeyField, select]);
125
132
 
126
133
  const handleParamChange = useCallback(
127
134
  (params: DataTableParamChange) => {
@@ -148,7 +155,7 @@ export function DenseTableBlock({ block }: BlockProps<DenseTableBlockData>): Rea
148
155
  // active filter sends `filters: {}`, which used to produce no key at all.
149
156
  // Seed every currently applied key as empty, then let the ones that
150
157
  // survived overwrite their own entry.
151
- Object.keys(data.filters?.applied ?? {}).forEach((k) => {
158
+ Object.keys(filters?.applied ?? {}).forEach((k) => {
152
159
  queryParams[`filter[${k}]`] = "";
153
160
  });
154
161
  Object.entries(params.filters).forEach(([k, v]) => {
@@ -170,7 +177,7 @@ export function DenseTableBlock({ block }: BlockProps<DenseTableBlockData>): Rea
170
177
  only: [reloadKey],
171
178
  });
172
179
  },
173
- [reloadKey, data.filters?.applied],
180
+ [reloadKey, filters?.applied],
174
181
  );
175
182
 
176
183
  // The toolbar's star. The action comes from the contract already carrying the
@@ -244,7 +251,7 @@ export function DenseTableBlock({ block }: BlockProps<DenseTableBlockData>): Rea
244
251
  sortable: col.sortable,
245
252
  minWidth: col.minWidth,
246
253
  maxWidth: col.maxWidth,
247
- statusMap: col.statusMap,
254
+ statusMap: mapOrUndefined(col.statusMap),
248
255
  href: col.href,
249
256
  entityType: col.entity?.type,
250
257
  entityIdField: col.entity?.id,
@@ -266,7 +273,7 @@ export function DenseTableBlock({ block }: BlockProps<DenseTableBlockData>): Rea
266
273
  label: renderLabel(action.label, t),
267
274
  icon: action.icon,
268
275
  intent: isDangerIntent(action.intent) ? ("danger" as const) : ("default" as const),
269
- variant: action.variant,
276
+ variant: knownValue(action.variant, ROW_ACTION_VARIANTS),
270
277
  onAction: (row: Record<string, unknown>) => {
271
278
  const resolvedHref = interpolate(target.url, row);
272
279
 
@@ -371,7 +378,7 @@ export function DenseTableBlock({ block }: BlockProps<DenseTableBlockData>): Rea
371
378
  <>
372
379
  <DataTable
373
380
  columns={columns}
374
- rows={data.rows}
381
+ rows={rows}
375
382
  rowKey={rowKeyField}
376
383
  pagination={
377
384
  data.pagination
@@ -382,8 +389,8 @@ export function DenseTableBlock({ block }: BlockProps<DenseTableBlockData>): Rea
382
389
  : undefined
383
390
  }
384
391
  sort={sort}
385
- filters={data.filters?.available}
386
- appliedFilters={data.filters?.applied}
392
+ filters={filters?.available}
393
+ appliedFilters={filters?.applied}
387
394
  rowActions={rowActions}
388
395
  actionsAlwaysVisible={meta?.actionsAlwaysVisible === true}
389
396
  bulkActions={data.bulkActions}
@@ -52,7 +52,7 @@ export function DetailPanelBlock({ block }: BlockProps<DetailPanelBlockData>): R
52
52
  <div className="space-y-8">
53
53
  {copyableText && <CopyReportButton text={copyableText} />}
54
54
  {data.sections.map((section) => (
55
- <section key={section.id} role="region" aria-label={renderLabel(section.title, t)}>
55
+ <section key={section.id} aria-label={renderLabel(section.title, t)}>
56
56
  <div className="mb-4 flex items-center justify-between">
57
57
  <h3 id={`${section.id}-heading`} className="text-foreground text-lg font-semibold">
58
58
  {renderLabel(section.title, t)}
@@ -8,11 +8,15 @@ import type { ReactElement } from "react";
8
8
  import { router } from "@inertiajs/core";
9
9
 
10
10
  import { EmptyPlaceholder } from "@/base/partials/EmptyPlaceholder";
11
+ import { knownValue } from "@/base/utils/php-wire";
11
12
  import type { EmptyStateBlockData } from "@/contracts/block-data";
12
13
  import type { BlockProps } from "@/engine/registries";
13
14
  import { renderLabel } from "@/i18n/render-label";
14
15
  import { useTranslation } from "@/i18n/useTranslation";
15
16
 
17
+ /** Empty-state variants the placeholder draws; any other value gets the default look. */
18
+ const EMPTY_STATE_VARIANTS = ["first-use", "no-results", "error", "permission"] as const;
19
+
16
20
  export function EmptyStateBlock({ block }: BlockProps<EmptyStateBlockData>): ReactElement {
17
21
  const { t } = useTranslation();
18
22
  const { data, title } = block;
@@ -20,7 +24,7 @@ export function EmptyStateBlock({ block }: BlockProps<EmptyStateBlockData>): Rea
20
24
  return (
21
25
  <div role="status">
22
26
  <EmptyPlaceholder
23
- variant={data.variant}
27
+ variant={knownValue(data.variant, EMPTY_STATE_VARIANTS)}
24
28
  icon={data.icon}
25
29
  lottieUrl={data.lottieUrl}
26
30
  title={renderLabel(title ?? data.variant ?? t("middag.ui.empty_state.default_title"), t)}
@@ -403,7 +403,6 @@ export function FormPanelBlock({ block }: BlockProps<FormPanelBlockData>): React
403
403
  onSubmit={form.handleSubmit(onSubmit)}
404
404
  className="mx-auto max-w-[720px] space-y-6"
405
405
  noValidate
406
- role="form"
407
406
  aria-label={renderLabel(block.title, t) || t("middag.ui.form.default_title")}
408
407
  >
409
408
  {/* Form-level / non-field server errors (keys with no matching field) */}
@@ -8,6 +8,7 @@ import { HugeiconsIcon } from "@hugeicons/react";
8
8
  import { Link } from "@inertiajs/react";
9
9
 
10
10
  import { getIcon } from "@/base/utils/icons";
11
+ import { knownValue } from "@/base/utils/php-wire";
11
12
  import type { MetricCardBlockData } from "@/contracts/block-data";
12
13
  import type { BlockProps } from "@/engine/registries";
13
14
  import { useTranslation } from "@/i18n/useTranslation";
@@ -40,8 +41,11 @@ function formatValue(value: string | number, locale = "pt-BR", compact = false):
40
41
  return new Intl.NumberFormat(locale).format(value);
41
42
  }
42
43
 
44
+ /** Delta directions the card colours; any other value is inferred from the delta sign. */
45
+ const DELTA_DIRECTIONS = ["positive", "negative", "neutral"] as const;
46
+
43
47
  const DELTA_STYLES = {
44
- positive: "text-success",
48
+ positive: "text-success-text",
45
49
  negative: "text-destructive",
46
50
  neutral: "text-muted-foreground",
47
51
  };
@@ -50,7 +54,10 @@ export function MetricCardBlock({ block }: BlockProps<MetricCardBlockData>): Rea
50
54
  const { t } = useTranslation();
51
55
  const { data, meta } = block;
52
56
  const isLoading = meta?.loading === true;
53
- const direction = inferDeltaDirection(data.delta, data.deltaDirection);
57
+ const direction = inferDeltaDirection(
58
+ data.delta,
59
+ knownValue(data.deltaDirection, DELTA_DIRECTIONS),
60
+ );
54
61
 
55
62
  if (isLoading) {
56
63
  return (
@@ -118,15 +118,26 @@ export interface FormFieldProps {
118
118
  onChange: (value: unknown) => void;
119
119
  error?: string;
120
120
  sourceValues?: Record<string, unknown>;
121
+ /**
122
+ * Level of the heading a `header` field renders (default 4). Set it to fit
123
+ * the page outline the form sits in, so headings do not skip levels.
124
+ */
125
+ headingLevel?: HeadingLevel;
121
126
  }
122
127
 
128
+ /** Heading levels a `header` field can render (level 1 is the page title). */
129
+ export type HeadingLevel = 2 | 3 | 4 | 5 | 6;
130
+
131
+ const DEFAULT_HEADING_LEVEL: HeadingLevel = 4;
132
+
123
133
  /**
124
134
  * Renders a header separator (non-interactive, no label wrapper).
125
135
  */
126
- function HeaderField({ label }: { label: string }): ReactElement {
136
+ function HeaderField({ label, level }: { label: string; level: HeadingLevel }): ReactElement {
137
+ const Heading = `h${level}` as const;
127
138
  return (
128
139
  <div className="pt-4 pb-1">
129
- <h4 className="text-foreground text-sm font-semibold">{label}</h4>
140
+ <Heading className="text-foreground text-sm font-semibold">{label}</Heading>
130
141
  </div>
131
142
  );
132
143
  }
@@ -144,18 +155,19 @@ export function FormField({
144
155
  onChange,
145
156
  error,
146
157
  sourceValues = {},
158
+ headingLevel = DEFAULT_HEADING_LEVEL,
147
159
  }: FormFieldProps): ReactElement {
148
160
  const { t } = useTranslation();
149
161
  const generatedId = useId();
150
162
  const fieldId = `field-${field.key}-${generatedId}`;
151
- const helpTextId = `${fieldId}-help`;
152
163
  const errorId = `${fieldId}-error`;
164
+ const labelId = `${fieldId}-label`;
153
165
 
154
166
  const { component, props } = field;
155
167
 
156
168
  // Header -- renders just a heading, no label/help/error wrapper.
157
169
  if (component === "header") {
158
- return <HeaderField label={props.label} />;
170
+ return <HeaderField label={props.label} level={headingLevel} />;
159
171
  }
160
172
 
161
173
  // Hidden -- renders a hidden input, no visible wrapper.
@@ -163,6 +175,11 @@ export function FormField({
163
175
  return <HiddenField id={fieldId} value={value} />;
164
176
  }
165
177
 
178
+ // Only ids of elements this component renders are handed to the input's
179
+ // aria-describedby: the help text exists only when there is help text, and
180
+ // the fields reference errorId only while `error` is set (rendered below).
181
+ const helpTextId = props.helpText ? `${fieldId}-help` : undefined;
182
+
166
183
  // Resolve conditional state against the current sibling field values.
167
184
  // `sourceValues` carries the full form value map, so disabled_when /
168
185
  // required_when can be evaluated here.
@@ -181,6 +198,7 @@ export function FormField({
181
198
  error,
182
199
  helpTextId,
183
200
  errorId,
201
+ labelId,
184
202
  t,
185
203
  sourceValues,
186
204
  effectiveDisabled,
@@ -191,9 +209,18 @@ export function FormField({
191
209
  <div className="space-y-1.5" data-testid={fieldTestId(field.key)}>
192
210
  {/* Label */}
193
211
  {!isInlineLabel && (
194
- <Label htmlFor={fieldId}>
212
+ <Label id={labelId} htmlFor={fieldId}>
195
213
  {props.label}
196
- {effectiveRequired && <span className="text-destructive ml-0.5">*</span>}
214
+ {effectiveRequired && (
215
+ <>
216
+ {/* The asterisk is visual only; screen readers hear the hidden
217
+ text instead, whatever the field's role supports. */}
218
+ <span className="text-destructive ml-0.5" aria-hidden="true">
219
+ *
220
+ </span>
221
+ <span className="sr-only">{` ${t("middag.ui.form.required_hint")}`}</span>
222
+ </>
223
+ )}
197
224
  </Label>
198
225
  )}
199
226
 
@@ -227,8 +254,9 @@ function renderField(
227
254
  onChange: (value: unknown) => void,
228
255
  props: AnyFieldProps,
229
256
  error: string | undefined,
230
- helpTextId: string,
257
+ helpTextId: string | undefined,
231
258
  errorId: string,
259
+ labelId: string,
232
260
  _t: TFunction,
233
261
  sourceValues: Record<string, unknown>,
234
262
  disabled: boolean,
@@ -340,6 +368,7 @@ function renderField(
340
368
  return (
341
369
  <MultiSelectField
342
370
  {...common}
371
+ labelId={labelId}
343
372
  value={value as string[]}
344
373
  onChange={onChange as (v: string[]) => void}
345
374
  options={props.options ?? []}
@@ -352,6 +381,7 @@ function renderField(
352
381
  return (
353
382
  <RadioField
354
383
  {...common}
384
+ labelId={labelId}
355
385
  value={value as string}
356
386
  onChange={onChange as (v: string) => void}
357
387
  options={props.options ?? []}
@@ -386,6 +416,7 @@ function renderField(
386
416
  return (
387
417
  <SliderField
388
418
  {...common}
419
+ labelId={labelId}
389
420
  value={value as number | number[] | null}
390
421
  onChange={onChange as (v: number | number[]) => void}
391
422
  min={props.min}
@@ -561,6 +592,7 @@ function renderField(
561
592
  <Suspense fallback={<InputSkeleton />}>
562
593
  <LazyDateRangeField
563
594
  {...common}
595
+ labelId={labelId}
564
596
  value={value}
565
597
  onChange={onChange}
566
598
  operator={props.operator}
@@ -572,6 +604,7 @@ function renderField(
572
604
  return (
573
605
  <RatingField
574
606
  {...common}
607
+ labelId={labelId}
575
608
  value={value as number | null}
576
609
  onChange={onChange as (v: number) => void}
577
610
  maxRating={props.maxRating}
@@ -48,7 +48,9 @@ export function DateField({
48
48
  mode = "date",
49
49
  placeholder,
50
50
  disabled,
51
- required,
51
+ // `required` is not read: the trigger is a button, which supports neither
52
+ // aria-required nor aria-invalid. The label marks the field as required and
53
+ // the error reaches assistive tech through aria-describedby.
52
54
  error,
53
55
  helpTextId,
54
56
  errorId,
@@ -118,8 +120,6 @@ export function DateField({
118
120
  type="button"
119
121
  id={id}
120
122
  disabled={disabled}
121
- aria-invalid={error ? true : undefined}
122
- aria-required={required || undefined}
123
123
  aria-describedby={describedBy}
124
124
  className="border-input bg-background ring-ring focus:ring-ring flex h-9 w-full items-center rounded-md border px-3 py-1 text-sm shadow-xs transition-colors focus:ring-1 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50"
125
125
  >
@@ -24,6 +24,8 @@ export interface DateRangeFieldProps {
24
24
  error?: string;
25
25
  helpTextId?: string;
26
26
  errorId?: string;
27
+ /** Id of the visible label (FormField renders it); names the group for assistive tech. */
28
+ labelId?: string;
27
29
  }
28
30
 
29
31
  export function DateRangeField({
@@ -35,6 +37,7 @@ export function DateRangeField({
35
37
  error,
36
38
  helpTextId,
37
39
  errorId,
40
+ labelId,
38
41
  }: DateRangeFieldProps): ReactElement {
39
42
  const describedBy =
40
43
  [error ? errorId : undefined, helpTextId].filter(Boolean).join(" ") || undefined;
@@ -47,20 +50,22 @@ export function DateRangeField({
47
50
  );
48
51
 
49
52
  return (
50
- <div
51
- id={id}
52
- aria-required={required || undefined}
53
- aria-invalid={error ? true : undefined}
54
- aria-describedby={describedBy}
55
- >
56
- <DateSelector
57
- value={value as DateSelectorValue | undefined}
58
- onChange={handleChange}
59
- allowRange
60
- defaultFilterType={(operator as DateSelectorFilterType) ?? "is"}
61
- presetMode={operator ? (operator as DateSelectorFilterType) : undefined}
62
- showInput
63
- />
53
+ <div role="group" aria-labelledby={labelId}>
54
+ <div
55
+ id={id}
56
+ aria-required={required || undefined}
57
+ aria-invalid={error ? true : undefined}
58
+ aria-describedby={describedBy}
59
+ >
60
+ <DateSelector
61
+ value={value as DateSelectorValue | undefined}
62
+ onChange={handleChange}
63
+ allowRange
64
+ defaultFilterType={(operator as DateSelectorFilterType) ?? "is"}
65
+ presetMode={operator ? (operator as DateSelectorFilterType) : undefined}
66
+ showInput
67
+ />
68
+ </div>
64
69
  </div>
65
70
  );
66
71
  }
@@ -32,6 +32,8 @@ export interface MultiSelectFieldProps {
32
32
  error?: string;
33
33
  helpTextId?: string;
34
34
  errorId?: string;
35
+ /** Id of the visible label (FormField renders it); names the group for assistive tech. */
36
+ labelId?: string;
35
37
  /**
36
38
  * Rendering mode. "auto" (default) uses a checkbox list for small option sets
37
39
  * and the searchable combobox for large ones; "combobox" / "checkbox" force it.
@@ -52,6 +54,7 @@ export function MultiSelectField({
52
54
  error,
53
55
  helpTextId,
54
56
  errorId,
57
+ labelId,
55
58
  variant = "auto",
56
59
  }: MultiSelectFieldProps): ReactElement {
57
60
  const { t } = useTranslation();
@@ -71,8 +74,7 @@ export function MultiSelectField({
71
74
  selected={selected}
72
75
  onChange={onChange}
73
76
  disabled={disabled}
74
- required={required}
75
- error={error}
77
+ labelId={labelId}
76
78
  describedBy={describedBy}
77
79
  t={t}
78
80
  />
@@ -104,8 +106,7 @@ function CheckboxList({
104
106
  selected,
105
107
  onChange,
106
108
  disabled,
107
- required,
108
- error,
109
+ labelId,
109
110
  describedBy,
110
111
  t,
111
112
  }: {
@@ -114,11 +115,12 @@ function CheckboxList({
114
115
  selected: string[];
115
116
  onChange: (value: string[]) => void;
116
117
  disabled?: boolean;
117
- required?: boolean;
118
- error?: string;
118
+ labelId?: string;
119
119
  describedBy?: string;
120
120
  t: TFunction;
121
121
  }): ReactElement {
122
+ // A group role supports neither aria-required nor aria-invalid; the error
123
+ // reaches assistive tech through aria-describedby.
122
124
  const toggle = (optValue: string) => {
123
125
  if (selected.includes(optValue)) {
124
126
  onChange(selected.filter((v) => v !== optValue));
@@ -131,8 +133,7 @@ function CheckboxList({
131
133
  <div
132
134
  id={id}
133
135
  role="group"
134
- aria-invalid={error ? true : undefined}
135
- aria-required={required || undefined}
136
+ aria-labelledby={labelId}
136
137
  aria-describedby={describedBy}
137
138
  className="border-input max-h-48 space-y-2 overflow-y-auto rounded-md border p-3"
138
139
  >
@@ -24,6 +24,8 @@ export interface RadioFieldProps {
24
24
  error?: string;
25
25
  helpTextId?: string;
26
26
  errorId?: string;
27
+ /** Id of the visible label (FormField renders it); names the group for assistive tech. */
28
+ labelId?: string;
27
29
  }
28
30
 
29
31
  export function RadioField({
@@ -36,6 +38,7 @@ export function RadioField({
36
38
  error,
37
39
  helpTextId,
38
40
  errorId,
41
+ labelId,
39
42
  }: RadioFieldProps): ReactElement {
40
43
  const describedBy =
41
44
  [error ? errorId : undefined, helpTextId].filter(Boolean).join(" ") || undefined;
@@ -48,6 +51,7 @@ export function RadioField({
48
51
  disabled={disabled}
49
52
  aria-invalid={error ? true : undefined}
50
53
  aria-required={required || undefined}
54
+ aria-labelledby={labelId}
51
55
  aria-describedby={describedBy}
52
56
  >
53
57
  {options.map((opt) => {
@@ -16,6 +16,8 @@ export interface RatingFieldProps {
16
16
  error?: string;
17
17
  helpTextId?: string;
18
18
  errorId?: string;
19
+ /** Id of the visible label (FormField renders it); names the group for assistive tech. */
20
+ labelId?: string;
19
21
  }
20
22
 
21
23
  export function RatingField({
@@ -28,6 +30,7 @@ export function RatingField({
28
30
  error,
29
31
  helpTextId,
30
32
  errorId,
33
+ labelId,
31
34
  }: RatingFieldProps): ReactElement {
32
35
  const describedBy =
33
36
  [error ? errorId : undefined, helpTextId].filter(Boolean).join(" ") || undefined;
@@ -36,6 +39,7 @@ export function RatingField({
36
39
  <div
37
40
  id={id}
38
41
  role="radiogroup"
42
+ aria-labelledby={labelId}
39
43
  aria-required={required || undefined}
40
44
  aria-invalid={error ? true : undefined}
41
45
  aria-describedby={describedBy}
@@ -23,6 +23,8 @@ export interface SliderFieldProps {
23
23
  error?: string;
24
24
  helpTextId?: string;
25
25
  errorId?: string;
26
+ /** Id of the visible label (FormField renders it); names the group for assistive tech. */
27
+ labelId?: string;
26
28
  }
27
29
 
28
30
  export function SliderField({
@@ -38,6 +40,7 @@ export function SliderField({
38
40
  error,
39
41
  helpTextId,
40
42
  errorId,
43
+ labelId,
41
44
  }: SliderFieldProps): ReactElement {
42
45
  const describedBy =
43
46
  [error ? errorId : undefined, helpTextId].filter(Boolean).join(" ") || undefined;
@@ -53,7 +56,7 @@ export function SliderField({
53
56
  const display = multiple ? arrayValue.join(" – ") : String(arrayValue[0] ?? min);
54
57
 
55
58
  return (
56
- <div className="flex items-center gap-4">
59
+ <div role="group" aria-labelledby={labelId} className="flex items-center gap-4">
57
60
  <Slider
58
61
  id={id}
59
62
  value={arrayValue}
@@ -85,6 +85,9 @@ export function TagsField({
85
85
  className={`focus-within:ring-ring flex min-h-9 w-full flex-wrap items-center gap-1.5 rounded-md border px-2 py-1.5 text-sm transition-colors focus-within:ring-1 ${
86
86
  error ? "border-destructive" : "border-input"
87
87
  } ${disabled ? "opacity-50" : ""}`}
88
+ // Pointer convenience only: clicking the box focuses the input, which
89
+ // keyboard users reach directly.
90
+ role="presentation"
88
91
  onClick={() => inputRef.current?.focus()}
89
92
  >
90
93
  {tags.map((tag, idx) => (