@mapomodule/form 2.0.0-beta.9 → 2.0.0-experimental.2

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 (145) hide show
  1. package/README.md +38 -119
  2. package/dist/index.d.mts +2 -1
  3. package/dist/index.d.ts +3 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.mjs +1 -0
  6. package/dist/module.d.mts +1 -0
  7. package/dist/module.d.ts.map +1 -1
  8. package/dist/module.json +1 -1
  9. package/dist/module.mjs +9 -0
  10. package/dist/runtime/components/MapoFocusPortal.vue +9 -2
  11. package/dist/runtime/components/MapoFocusPortal.vue.d.ts.map +1 -1
  12. package/dist/runtime/components/MapoForm.vue +28 -6
  13. package/dist/runtime/components/MapoForm.vue.d.ts +13 -1
  14. package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
  15. package/dist/runtime/components/MapoFormAnnotation.vue +79 -0
  16. package/dist/runtime/components/MapoFormAnnotation.vue.d.ts +27 -0
  17. package/dist/runtime/components/MapoFormAnnotation.vue.d.ts.map +1 -0
  18. package/dist/runtime/components/MapoFormAnnotationBadges.vue +28 -0
  19. package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts +13 -0
  20. package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts.map +1 -0
  21. package/dist/runtime/components/MapoFormField.vue +47 -11
  22. package/dist/runtime/components/MapoFormField.vue.d.ts +15 -9
  23. package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
  24. package/dist/runtime/components/MapoFormGroup.vue +18 -2
  25. package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
  26. package/dist/runtime/components/MapoFormTabs.vue +50 -8
  27. package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
  28. package/dist/runtime/components/fields/MapoMediaField.vue +5 -3
  29. package/dist/runtime/components/fields/MapoMediaField.vue.d.ts.map +1 -1
  30. package/dist/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  31. package/dist/runtime/components/fields/MapoMediaM2mField.vue.d.ts.map +1 -1
  32. package/dist/runtime/components/fields/MapoRepeater.vue +73 -14
  33. package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
  34. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +167 -0
  35. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts +20 -0
  36. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -0
  37. package/dist/runtime/components/fields/MapoRepeaterItem.vue +46 -10
  38. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts +6 -2
  39. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
  40. package/dist/runtime/components/fields/MapoWygEditor.vue +19 -13
  41. package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
  42. package/dist/runtime/composables/useFieldAnnotation.d.ts +18 -0
  43. package/dist/runtime/composables/useFieldAnnotation.d.ts.map +1 -0
  44. package/dist/runtime/composables/useFieldAnnotation.js +16 -0
  45. package/dist/runtime/composables/useFocusMode.d.ts +3 -1
  46. package/dist/runtime/composables/useFocusMode.d.ts.map +1 -1
  47. package/dist/runtime/composables/useFormFromSchema.d.ts +23 -1
  48. package/dist/runtime/composables/useFormFromSchema.d.ts.map +1 -1
  49. package/dist/runtime/composables/useFormFromSchema.js +171 -32
  50. package/dist/runtime/composables/useMapoForm.d.ts +21 -3
  51. package/dist/runtime/composables/useMapoForm.d.ts.map +1 -1
  52. package/dist/runtime/composables/useMapoForm.js +49 -15
  53. package/dist/runtime/types/annotation.d.ts +43 -0
  54. package/dist/runtime/types/annotation.d.ts.map +1 -0
  55. package/dist/runtime/types/annotation.js +0 -0
  56. package/dist/runtime/types/descriptor.d.ts +81 -4
  57. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  58. package/dist/runtime/types/index.d.ts +2 -1
  59. package/dist/runtime/types/index.d.ts.map +1 -1
  60. package/dist/runtime/utils/annotations.d.ts +40 -0
  61. package/dist/runtime/utils/annotations.d.ts.map +1 -0
  62. package/dist/runtime/utils/annotations.js +78 -0
  63. package/dist/runtime/utils/draft.d.ts +21 -0
  64. package/dist/runtime/utils/draft.d.ts.map +1 -0
  65. package/dist/runtime/utils/draft.js +108 -0
  66. package/dist/runtime/utils/fieldComponent.d.ts +9 -0
  67. package/dist/runtime/utils/fieldComponent.d.ts.map +1 -0
  68. package/dist/runtime/utils/fieldComponent.js +12 -0
  69. package/dist/runtime/utils/fieldFocus.d.ts +24 -0
  70. package/dist/runtime/utils/fieldFocus.d.ts.map +1 -0
  71. package/dist/runtime/utils/fieldFocus.js +41 -0
  72. package/dist/runtime/utils/fieldsToJsonSchema.d.ts +38 -0
  73. package/dist/runtime/utils/fieldsToJsonSchema.d.ts.map +1 -0
  74. package/dist/runtime/utils/fieldsToJsonSchema.js +248 -0
  75. package/dist/runtime/utils/repeaterTemplates.d.ts +26 -0
  76. package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
  77. package/dist/runtime/utils/repeaterTemplates.js +51 -0
  78. package/dist/runtime/utils/reveal.d.ts +34 -0
  79. package/dist/runtime/utils/reveal.d.ts.map +1 -0
  80. package/dist/runtime/utils/reveal.js +29 -0
  81. package/dist/runtime/utils/tabVisibility.d.ts +18 -0
  82. package/dist/runtime/utils/tabVisibility.d.ts.map +1 -0
  83. package/dist/runtime/utils/tabVisibility.js +18 -0
  84. package/package.json +5 -5
  85. package/src/__tests__/MapoFocusPortal.component.test.ts +97 -0
  86. package/src/__tests__/MapoForm.component.test.ts +79 -0
  87. package/src/__tests__/MapoFormAnnotation.component.test.ts +244 -0
  88. package/src/__tests__/MapoFormField.component.test.ts +508 -0
  89. package/src/__tests__/MapoFormGroup.component.test.ts +141 -0
  90. package/src/__tests__/MapoFormTabs.component.test.ts +254 -0
  91. package/src/__tests__/MapoRepeaterItem.component.test.ts +286 -0
  92. package/src/__tests__/MapoWygEditor.component.test.ts +95 -0
  93. package/src/__tests__/annotations.test.ts +227 -0
  94. package/src/__tests__/draft.test.ts +221 -0
  95. package/src/__tests__/fieldComponent.test.ts +31 -0
  96. package/src/__tests__/fieldExtensions.test.ts +63 -0
  97. package/src/__tests__/fieldFocus.test.ts +178 -0
  98. package/src/__tests__/fieldsToJsonSchema.test.ts +630 -0
  99. package/src/__tests__/headlessRenderer.ts +85 -0
  100. package/src/__tests__/repeaterTemplates.test.ts +154 -0
  101. package/src/__tests__/reveal.test.ts +89 -0
  102. package/src/__tests__/schemaSources.test.ts +132 -0
  103. package/src/__tests__/tabVisibility.test.ts +102 -0
  104. package/src/__tests__/useFormFromSchema.test.ts +387 -0
  105. package/src/__tests__/useMapoForm.test.ts +213 -2
  106. package/src/index.ts +9 -0
  107. package/src/module.ts +9 -0
  108. package/src/runtime/components/MapoFocusPortal.vue +15 -2
  109. package/src/runtime/components/MapoForm.vue +47 -4
  110. package/src/runtime/components/MapoFormAnnotation.vue +113 -0
  111. package/src/runtime/components/MapoFormAnnotationBadges.vue +41 -0
  112. package/src/runtime/components/MapoFormField.vue +72 -11
  113. package/src/runtime/components/MapoFormGroup.vue +20 -2
  114. package/src/runtime/components/MapoFormTabs.vue +64 -8
  115. package/src/runtime/components/fields/MapoMediaField.vue +5 -3
  116. package/src/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  117. package/src/runtime/components/fields/MapoRepeater.vue +82 -14
  118. package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +188 -0
  119. package/src/runtime/components/fields/MapoRepeaterItem.vue +56 -8
  120. package/src/runtime/components/fields/MapoWygEditor.vue +22 -14
  121. package/src/runtime/composables/useFieldAnnotation.ts +38 -0
  122. package/src/runtime/composables/useFocusMode.ts +7 -1
  123. package/src/runtime/composables/useFormFromSchema.ts +303 -41
  124. package/src/runtime/composables/useMapoForm.ts +103 -23
  125. package/src/runtime/types/annotation.ts +53 -0
  126. package/src/runtime/types/descriptor.ts +94 -4
  127. package/src/runtime/types/index.ts +10 -0
  128. package/src/runtime/utils/annotations.ts +169 -0
  129. package/src/runtime/utils/draft.ts +158 -0
  130. package/src/runtime/utils/fieldComponent.ts +34 -0
  131. package/src/runtime/utils/fieldFocus.ts +73 -0
  132. package/src/runtime/utils/fieldsToJsonSchema.ts +405 -0
  133. package/src/runtime/utils/repeaterTemplates.ts +103 -0
  134. package/src/runtime/utils/reveal.ts +68 -0
  135. package/src/runtime/utils/tabVisibility.ts +44 -0
  136. package/dist/__tests__/__mocks__/nuxt-imports.d.ts +0 -4
  137. package/dist/__tests__/__mocks__/nuxt-imports.d.ts.map +0 -1
  138. package/dist/__tests__/camomillaRoundTrip.test.d.ts +0 -2
  139. package/dist/__tests__/camomillaRoundTrip.test.d.ts.map +0 -1
  140. package/dist/__tests__/registry.test.d.ts +0 -2
  141. package/dist/__tests__/registry.test.d.ts.map +0 -1
  142. package/dist/__tests__/useFormFromSchema.test.d.ts +0 -2
  143. package/dist/__tests__/useFormFromSchema.test.d.ts.map +0 -1
  144. package/dist/__tests__/useMapoForm.test.d.ts +0 -2
  145. package/dist/__tests__/useMapoForm.test.d.ts.map +0 -1
@@ -9,16 +9,28 @@
9
9
  * // In layouts/default.vue
10
10
  * <MapoFocusPortal />
11
11
  */
12
- import { computed, ref, onMounted, onUnmounted } from "vue";
12
+ import { computed, ref, onMounted, onUnmounted, unref } from "vue";
13
13
  import { watch } from "vue";
14
14
  import { useI18n } from "vue-i18n";
15
15
  import { useFocusMode } from "../composables/useFocusMode.js";
16
+ import type { FieldAnnotations } from "../types/index.js";
16
17
  import MapoForm from "./MapoForm.vue";
17
18
 
18
19
  const { t } = useI18n();
19
20
  const { focusTarget, exit } = useFocusMode();
20
21
 
21
22
  const isOpen = computed(() => focusTarget.value !== null);
23
+
24
+ // `useFocusMode()` hands the target out through `readonly()`, which unwraps the
25
+ // refs it holds: `errors` and `annotations` arrive as plain values, so reading
26
+ // `.value` on them gives `undefined`. `unref` accepts both shapes.
27
+ const focusErrors = computed(
28
+ () =>
29
+ unref(focusTarget.value?.errors) as Record<string, string[]> | undefined,
30
+ );
31
+ const focusAnnotations = computed(
32
+ () => unref(focusTarget.value?.annotations) as FieldAnnotations | undefined,
33
+ );
22
34
  const canUseDocument = typeof document !== "undefined";
23
35
 
24
36
  // Close on ESC.
@@ -102,10 +114,11 @@ function onUpdate(val: Record<string, unknown>) {
102
114
  <MapoForm
103
115
  :model-value="localModel"
104
116
  :fields="focusTarget.fields as any"
105
- :errors="focusTarget.errors.value as any"
117
+ :errors="focusErrors"
106
118
  :languages="focusTarget.languages as any"
107
119
  :current-lang="focusTarget.currentLang"
108
120
  :registry="focusTarget.registry as any"
121
+ :annotations="focusAnnotations"
109
122
  @update:model-value="onUpdate"
110
123
  />
111
124
  </div>
@@ -2,8 +2,10 @@
2
2
  import { computed, useSlots, watchEffect, inject, onUnmounted } from "vue";
3
3
  import type { Ref } from "vue";
4
4
  import { useNuxtApp, useRuntimeConfig } from "#app";
5
+ import { useI18n } from "vue-i18n";
5
6
  import type {
6
7
  AnyFieldDescriptor,
8
+ FieldAnnotations,
7
9
  FieldRegistry,
8
10
  MapoFormRuntimeConfig,
9
11
  } from "../types/index.js";
@@ -23,12 +25,23 @@ const props = withDefaults(
23
25
  currentLang?: string;
24
26
  readonly?: boolean;
25
27
  immediate?: boolean;
28
+ /**
29
+ * Show a toast for server-side field errors. Set to `false` for secondary
30
+ * forms that share errors with another form. Defaults to `true`.
31
+ */
32
+ notifyErrors?: boolean;
26
33
  /**
27
34
  * Field component registry. When omitted, it is auto-injected from
28
35
  * `$mapoFormRegistry` (provided by the `@mapomodule/form` plugin).
29
36
  * Pass it explicitly only for headless forms or one-off registry swaps.
30
37
  */
31
38
  registry?: FieldRegistry;
39
+ /**
40
+ * Marks shown next to the field labels and counted on the tabs, keyed by
41
+ * the path of the value (`title`, `translations.it.title`, `blocks.0.title`).
42
+ * Changing the map updates the marks without remounting the fields.
43
+ */
44
+ annotations?: FieldAnnotations;
32
45
  }>(),
33
46
  {
34
47
  errors: () => ({}),
@@ -36,7 +49,9 @@ const props = withDefaults(
36
49
  currentLang: "",
37
50
  readonly: false,
38
51
  immediate: false,
52
+ notifyErrors: true,
39
53
  registry: undefined,
54
+ annotations: undefined,
40
55
  },
41
56
  );
42
57
 
@@ -84,8 +99,10 @@ const form = useMapoForm({
84
99
  languages: props.languages,
85
100
  currentLang: currentLangRef,
86
101
  immediate: props.immediate,
102
+ notifyErrors: props.notifyErrors,
87
103
  debounce: globalDebounce,
88
104
  registry: injectedRegistry.value,
105
+ annotations: computed(() => props.annotations),
89
106
  });
90
107
 
91
108
  // Inherit the parent form's submitted state (provided automatically by useMapoForm)
@@ -103,6 +120,7 @@ defineExpose({
103
120
  getPatch: form.getPatch,
104
121
  isDirty: form.isDirty,
105
122
  isLoading: form.isLoading,
123
+ revealField: form.revealField,
106
124
  });
107
125
 
108
126
  type ValidateClientFn = () => {
@@ -119,6 +137,19 @@ if (registerValidator) {
119
137
  onUnmounted(unregister);
120
138
  }
121
139
 
140
+ // A detail page around the form brings a field into view through every form
141
+ // it holds, the body's and the sidebar's, as it validates them.
142
+ type RevealFieldFn = (path: string) => Promise<boolean>;
143
+ type RegisterRevealFn = (fn: RevealFieldFn) => () => void;
144
+ const registerReveal = inject<RegisterRevealFn | null>(
145
+ "mapoDetailRegisterReveal",
146
+ null,
147
+ );
148
+ if (registerReveal) {
149
+ const unregister = registerReveal(form.revealField);
150
+ onUnmounted(unregister);
151
+ }
152
+
122
153
  // Forward only field-specific slots to inner layers, avoiding host-level slots
123
154
  // such as header/footer/actions where they have no meaning.
124
155
  const slots = useSlots();
@@ -135,6 +166,12 @@ provideCurrentLang(currentLangRef);
135
166
 
136
167
  // ─── Group fields by tab/group ───────────────────────────────────────────────
137
168
 
169
+ const { t } = useI18n();
170
+
171
+ // Fields without a `tab` go here. It becomes a tab of its own only when other
172
+ // fields have one, and then it shows as "General" (`mapo.form.defaultTab`).
173
+ const DEFAULT_TAB = "__default__";
174
+
138
175
  function getOrCreateTab(map: Map<string, TabEntry>, name: string): TabEntry {
139
176
  if (!map.has(name)) {
140
177
  map.set(name, { name, groups: new Map(), children: new Map() });
@@ -151,7 +188,7 @@ const grouped = computed<Map<string, TabEntry>>(() => {
151
188
  // Normalise: 'a/b', ['a','b'], or 'a' all become a string[]
152
189
  const rawTab = field.tab;
153
190
  const tabPath: string[] = !rawTab
154
- ? ["__default__"]
191
+ ? [DEFAULT_TAB]
155
192
  : Array.isArray(rawTab)
156
193
  ? rawTab
157
194
  : rawTab.split("/");
@@ -179,17 +216,23 @@ const grouped = computed<Map<string, TabEntry>>(() => {
179
216
  }
180
217
  }
181
218
 
219
+ const defaultTab = tabs.get(DEFAULT_TAB);
220
+ if (defaultTab) defaultTab.label = t("mapo.form.defaultTab");
221
+
182
222
  return tabs;
183
223
  });
184
224
 
185
225
  const hasTabs = computed(() => {
186
226
  const keys = [...grouped.value.keys()];
187
- return keys.length > 1 || (keys.length === 1 && keys[0] !== "__default__");
227
+ const hasFields = keys.length > 0;
228
+ if (!hasFields) return false;
229
+ const hasSeveralTabs = keys.length > 1;
230
+ const hasNamedTab = keys[0] !== DEFAULT_TAB;
231
+ return hasSeveralTabs || hasNamedTab;
188
232
  });
189
233
 
190
234
  const defaultGroups = computed(
191
- () =>
192
- grouped.value.get("__default__")?.groups ?? new Map<string, GroupEntry>(),
235
+ () => grouped.value.get(DEFAULT_TAB)?.groups ?? new Map<string, GroupEntry>(),
193
236
  );
194
237
 
195
238
  const tabList = computed<TabEntry[]>(() => [...grouped.value.values()]);
@@ -0,0 +1,113 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * MapoFormAnnotation — the mark of an annotated field: a small button with the
4
+ * annotation's icon and color, named by its label, that opens a popover with
5
+ * the title, the description (or the `body` slot) and the actions.
6
+ *
7
+ * `MapoFormField` renders it next to the label; a custom label slot can render
8
+ * it too, from the `annotation` it receives.
9
+ *
10
+ * @example
11
+ * <MapoFormAnnotation :annotation="{ label: 'Imported', icon: 'i-lucide-download' }" />
12
+ */
13
+ import { computed, nextTick } from "vue";
14
+ import type { FieldAnnotation, FieldAnnotationAction } from "../types/index.js";
15
+ import {
16
+ DEFAULT_ANNOTATION_COLOR,
17
+ DEFAULT_ANNOTATION_ICON,
18
+ } from "../utils/annotations.js";
19
+ import { refocusField } from "../utils/fieldFocus.js";
20
+
21
+ const props = defineProps<{
22
+ /** The mark to show: icon and color on the button, texts and actions in the popover. */
23
+ annotation: FieldAnnotation;
24
+ /**
25
+ * The id of the field's control. After an action, a focus left on nothing
26
+ * goes back to the field: an action that takes the mark away, or changes
27
+ * the buttons, would drop it on the page. `MapoFormField` passes it.
28
+ */
29
+ field?: string;
30
+ }>();
31
+
32
+ defineSlots<{
33
+ /** Replaces the description, between the popover title and its actions. */
34
+ body?: (props: { annotation: FieldAnnotation }) => unknown;
35
+ }>();
36
+
37
+ const icon = computed(() => props.annotation.icon ?? DEFAULT_ANNOTATION_ICON);
38
+ const color = computed(
39
+ () => props.annotation.color ?? DEFAULT_ANNOTATION_COLOR,
40
+ );
41
+
42
+ // The popover is portalled to <body> with no z-index of its own, so the field
43
+ // expand panel (z-50) and the focus portal (z-[9999]) would paint over it. The
44
+ // popper wrapper copies the z-index of its content; the dialog takes its name
45
+ // from the label.
46
+ const popoverUi = { content: "z-[10000]" };
47
+ /**
48
+ * Runs an action, then gives the focus back to the field when the action
49
+ * left it on nothing: once Vue has updated, the button pressed may be gone.
50
+ */
51
+ async function run(action: FieldAnnotationAction): Promise<void> {
52
+ const id = props.field;
53
+ action.onClick();
54
+ if (id === undefined) return;
55
+ await nextTick();
56
+ refocusField(id);
57
+ }
58
+
59
+ const popoverContent = computed(() => ({
60
+ side: "bottom" as const,
61
+ align: "start" as const,
62
+ "aria-label": props.annotation.label,
63
+ }));
64
+ </script>
65
+
66
+ <template>
67
+ <UPopover :content="popoverContent" :ui="popoverUi">
68
+ <!-- The label text is the accessible name; the dot only repeats it visually. -->
69
+ <UButton
70
+ :icon="icon"
71
+ :color="color"
72
+ :aria-label="annotation.label"
73
+ variant="ghost"
74
+ size="xs"
75
+ square
76
+ class="mapo-form-annotation relative p-0.5"
77
+ >
78
+ <span
79
+ v-if="annotation.dot"
80
+ class="absolute right-0 top-0 size-1.5 rounded-full bg-current"
81
+ aria-hidden="true"
82
+ />
83
+ </UButton>
84
+
85
+ <template #content>
86
+ <div class="max-w-xs space-y-2 p-3 text-sm">
87
+ <p class="font-medium text-highlighted">
88
+ {{ annotation.label }}
89
+ </p>
90
+ <slot name="body" :annotation="annotation">
91
+ <p v-if="annotation.description" class="text-muted">
92
+ {{ annotation.description }}
93
+ </p>
94
+ </slot>
95
+ <div v-if="annotation.actions?.length" class="flex flex-wrap gap-2">
96
+ <!-- Keyed by label: when an action changes the buttons, the one
97
+ under the focus is never reused for another action. The index
98
+ keeps two actions with one label apart. -->
99
+ <UButton
100
+ v-for="(action, index) in annotation.actions"
101
+ :key="`${action.label}-${index}`"
102
+ :label="action.label"
103
+ :icon="action.icon"
104
+ :color="action.color ?? 'neutral'"
105
+ variant="soft"
106
+ size="xs"
107
+ @click="run(action)"
108
+ />
109
+ </div>
110
+ </div>
111
+ </template>
112
+ </UPopover>
113
+ </template>
@@ -0,0 +1,41 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * The annotation badges of a tab or of a repeater item: one per icon and color,
4
+ * with the count of the marks it stands for. Internal: `MapoFormTabs` and
5
+ * `MapoRepeaterItem` render it, and it is not registered for apps.
6
+ */
7
+ import { useI18n } from "vue-i18n";
8
+ import type { AnnotationGroup } from "../utils/annotations.js";
9
+
10
+ withDefaults(
11
+ defineProps<{
12
+ /** The groups to show, as `annotationGroups()` returns them. */
13
+ groups: AnnotationGroup[];
14
+ /** Badge size: `sm` on tabs, `xs` on repeater items. */
15
+ size?: "xs" | "sm";
16
+ }>(),
17
+ { size: "sm" },
18
+ );
19
+
20
+ const { t } = useI18n();
21
+ </script>
22
+
23
+ <template>
24
+ <UBadge
25
+ v-for="group in groups"
26
+ :key="`${group.icon}|${group.color}`"
27
+ :icon="group.icon"
28
+ :color="group.color"
29
+ :label="group.count"
30
+ :size="size"
31
+ variant="subtle"
32
+ role="img"
33
+ :aria-label="
34
+ t(
35
+ 'mapo.form.annotationCount',
36
+ { n: group.count, labels: group.labels.join(', ') },
37
+ group.count,
38
+ )
39
+ "
40
+ />
41
+ </template>
@@ -1,5 +1,5 @@
1
1
  <script setup lang="ts">
2
- import { computed, defineAsyncComponent, inject, ref, watch } from "vue";
2
+ import { computed, inject, nextTick, ref, watch } from "vue";
3
3
  import type { Ref } from "vue";
4
4
  import type { AnyFieldDescriptor } from "../types/index.js";
5
5
  import {
@@ -8,8 +8,14 @@ import {
8
8
  resolveFieldAccessor,
9
9
  } from "../types/index.js";
10
10
  import { injectMapoForm } from "../composables/useMapoForm.js";
11
+ import { toFieldComponent } from "../utils/fieldComponent.js";
12
+ import { valuePath } from "../utils/annotations.js";
13
+ import { focusFieldControl } from "../utils/fieldFocus.js";
14
+ import type { FieldRevealRequest } from "../utils/reveal.js";
15
+ import { useFieldAnnotation } from "../composables/useFieldAnnotation.js";
11
16
  import { getNestedValue, debounce } from "@mapomodule/utils";
12
17
  import MapoUnknownField from "./MapoUnknownField.vue";
18
+ import MapoFormAnnotation from "./MapoFormAnnotation.vue";
13
19
 
14
20
  // `any` accepts descriptors typed against any model (e.g. AnyFieldDescriptor<Article>)
15
21
  // in headless usage; the renderer itself only reads model-agnostic metadata.
@@ -41,13 +47,8 @@ const showField = computed(() => {
41
47
 
42
48
  // ─── Value ───────────────────────────────────────────────────────────────────
43
49
 
44
- const path = computed(() => {
45
- const lang = currentLang.value;
46
- if (props.descriptor.translatable && lang) {
47
- return `translations.${lang}.${props.descriptor.key as string}`;
48
- }
49
- return props.descriptor.key as string;
50
- });
50
+ // The same rule the tab badges count with, so marks and counts agree.
51
+ const path = computed(() => valuePath(props.descriptor, currentLang.value));
51
52
 
52
53
  const accessor = computed(() =>
53
54
  resolveFieldAccessor(props.descriptor, registry),
@@ -144,12 +145,12 @@ const asyncTrailingIconClass = computed(
144
145
 
145
146
  // ─── Component resolution ────────────────────────────────────────────────────
146
147
 
148
+ // The same loader always gives the same component, so a new descriptor object of
149
+ // the same type updates the mounted field instead of replacing it.
147
150
  const fieldComponent = computed(() => {
148
151
  const entry = resolveFieldComponent(props.descriptor, registry);
149
152
  if (!entry) return MapoUnknownField;
150
- if (typeof entry === "function")
151
- return defineAsyncComponent(entry as () => Promise<{ default: unknown }>);
152
- return entry;
153
+ return toFieldComponent(entry);
153
154
  });
154
155
 
155
156
  const fieldAttrs = computed(() =>
@@ -207,6 +208,13 @@ const label = computed(
207
208
  );
208
209
  const fieldId = computed(() => `field-${props.descriptor.key as string}`);
209
210
 
211
+ // ─── Annotation ──────────────────────────────────────────────────────────────
212
+
213
+ // Read from the form context, never from the descriptor: changing the map
214
+ // updates the mark and leaves the field component mounted. A custom field
215
+ // reads the same annotation with the same composable.
216
+ const annotation = useFieldAnnotation(() => props.descriptor);
217
+
210
218
  // ─── Group expand ────────────────────────────────────────────────────────────
211
219
 
212
220
  const groupExpand = inject<{
@@ -226,12 +234,48 @@ function toggleExpand() {
226
234
  if (isExpanded.value) groupExpand.collapseField();
227
235
  else groupExpand.expandField(props.descriptor.key as string);
228
236
  }
237
+
238
+ // ─── Bringing the field into view ────────────────────────────────────────────
239
+
240
+ /** The field's element: its control is looked up inside it. */
241
+ const fieldElement = ref<unknown>(null);
242
+
243
+ /** Milliseconds between two looks for the control of a field being revealed. */
244
+ const REVEAL_RETRY_MS = 50;
245
+
246
+ // `revealField()` of the form names this field: its tab, group and item open
247
+ // on the same request, and the control may render a moment later, as a field
248
+ // type loaded lazily does. The request ends when the control has the focus,
249
+ // or when the form gives up on it.
250
+ watch(
251
+ () => form.reveal?.value,
252
+ (request) => {
253
+ const isThisField =
254
+ request !== undefined &&
255
+ request.rest === "" &&
256
+ request.key === (props.descriptor.key as string);
257
+ if (isThisField) void focusWhenRendered(request);
258
+ },
259
+ { immediate: true },
260
+ );
261
+
262
+ async function focusWhenRendered(request: FieldRevealRequest): Promise<void> {
263
+ await nextTick();
264
+ while (form.reveal?.value === request) {
265
+ if (focusFieldControl(fieldElement.value, fieldId.value)) {
266
+ request.settle(true);
267
+ return;
268
+ }
269
+ await new Promise((resolve) => setTimeout(resolve, REVEAL_RETRY_MS));
270
+ }
271
+ }
229
272
  </script>
230
273
 
231
274
  <template>
232
275
  <div
233
276
  v-if="isVisible"
234
277
  v-show="showField"
278
+ ref="fieldElement"
235
279
  class="mapo-form-field group flex flex-col gap-1"
236
280
  :class="[
237
281
  descriptor.class,
@@ -244,6 +288,7 @@ function toggleExpand() {
244
288
  :name="`field.${descriptor.key as string}.label`"
245
289
  :descriptor="descriptor"
246
290
  :label="label"
291
+ :annotation="annotation"
247
292
  >
248
293
  <label
249
294
  :for="fieldId"
@@ -253,6 +298,22 @@ function toggleExpand() {
253
298
  <span v-if="descriptor.required" class="text-red-500" aria-hidden="true"
254
299
  >*</span
255
300
  >
301
+ <MapoFormAnnotation
302
+ v-if="annotation"
303
+ :annotation="annotation"
304
+ :field="fieldId"
305
+ >
306
+ <template
307
+ v-if="$slots[`field.${descriptor.key as string}.annotation`]"
308
+ #body
309
+ >
310
+ <slot
311
+ :name="`field.${descriptor.key as string}.annotation`"
312
+ :descriptor="descriptor"
313
+ :annotation="annotation"
314
+ />
315
+ </template>
316
+ </MapoFormAnnotation>
256
317
  <UIcon
257
318
  v-if="asyncTrailingIcon"
258
319
  :name="asyncTrailingIcon"
@@ -3,6 +3,7 @@ import {
3
3
  ref,
4
4
  computed,
5
5
  provide,
6
+ watch,
6
7
  watchEffect,
7
8
  onMounted,
8
9
  onUnmounted,
@@ -68,6 +69,23 @@ const form = injectMapoForm();
68
69
  const model = computed(() => form?.model.value);
69
70
  const currentLang = computed(() => form?.currentLang.value ?? "");
70
71
 
72
+ // `revealField()` opens the group that holds the field, and its sub-tab.
73
+ watch(
74
+ () => form?.reveal?.value,
75
+ (request) => {
76
+ if (!request) return;
77
+ const isOwnField = props.fields.some((field) => field.key === request.key);
78
+ const subtabs = [...(props.subtabs?.entries() ?? [])];
79
+ const subtab = subtabs.find(([, fields]) =>
80
+ fields.some((field) => field.key === request.key),
81
+ );
82
+ if (!isOwnField && !subtab) return;
83
+ expanded.value = true;
84
+ if (subtab) activeSubtab.value = subtab[0];
85
+ },
86
+ { immediate: true },
87
+ );
88
+
71
89
  // ─── Col-span lookup ─────────────────────────────────────────────────────────
72
90
 
73
91
  const COL_SPAN: Record<number, string> = {
@@ -217,12 +235,12 @@ function colClass(cols: AnyFieldDescriptor["cols"]): string {
217
235
  <!-- Sub-tab bar + content (when subtabs are present) -->
218
236
  <div v-if="subtabs && subtabs.size > 0" class="px-5 pb-5 pt-4">
219
237
  <!-- Sub-tab bar -->
220
- <div class="flex gap-1 border-b border-gray-200 mb-4">
238
+ <div class="mb-4 flex flex-wrap gap-1 border-b border-gray-200">
221
239
  <button
222
240
  v-for="[subtabName] of subtabs"
223
241
  :key="subtabName"
224
242
  type="button"
225
- class="relative px-3 py-1.5 text-sm font-medium transition-colors focus:outline-none"
243
+ class="relative max-w-full px-3 py-1.5 text-sm font-medium transition-colors focus:outline-none"
226
244
  :class="[
227
245
  activeSubtab === subtabName
228
246
  ? 'text-primary-600 border-b-2 border-primary-600 -mb-px'
@@ -1,8 +1,12 @@
1
1
  <script setup lang="ts">
2
- import { ref, computed } from "vue";
2
+ import { ref, computed, watch } from "vue";
3
3
  import { useI18n } from "vue-i18n";
4
4
  import type { TabEntry } from "../types/layout.js";
5
5
  import { injectMapoForm } from "../composables/useMapoForm.js";
6
+ import { tabHoldsField } from "../utils/reveal.js";
7
+ import { pickActiveTab, tabHasShownFields } from "../utils/tabVisibility.js";
8
+ import { tabAnnotationGroups } from "../utils/annotations.js";
9
+ import MapoFormAnnotationBadges from "./MapoFormAnnotationBadges.vue";
6
10
  import MapoFormGroup from "./MapoFormGroup.vue";
7
11
  import MapoFormFlatSection from "./MapoFormFlatSection.vue";
8
12
 
@@ -10,9 +14,42 @@ const { t } = useI18n();
10
14
 
11
15
  const props = defineProps<{ tabs: TabEntry[] }>();
12
16
 
13
- defineSlots<Record<string, unknown>>();
17
+ const slots = defineSlots<Record<string, unknown>>();
14
18
 
15
- const activeTab = ref(props.tabs[0]?.name ?? "");
19
+ const form = injectMapoForm<Record<string, unknown>>()!;
20
+
21
+ // A tab whose fields are all hidden by `visible` or `show` would open on an
22
+ // empty panel, so it leaves the tab bar until one of them shows. Content passed
23
+ // through the tab's own slots counts as something to show.
24
+ function tabHasContent(tab: TabEntry): boolean {
25
+ const hasPanelSlot = Boolean(slots[`tab.${tab.name}`]);
26
+ const hasBeforeSlot = Boolean(slots[`tab.${tab.name}.before`]);
27
+ const hasAfterSlot = Boolean(slots[`tab.${tab.name}.after`]);
28
+ if (hasPanelSlot || hasBeforeSlot || hasAfterSlot) return true;
29
+ return tabHasShownFields(tab, form.model.value);
30
+ }
31
+
32
+ const shownTabs = computed(() => props.tabs.filter(tabHasContent));
33
+
34
+ // The tab the user clicked; until then, or while it is hidden, the first shown
35
+ // tab is open.
36
+ const pickedTab = ref<string | null>(null);
37
+ const activeTab = computed(() =>
38
+ pickActiveTab(shownTabs.value, pickedTab.value),
39
+ );
40
+
41
+ // `revealField()` opens the tab that holds the field, as a click would.
42
+ watch(
43
+ () => form.reveal?.value,
44
+ (request) => {
45
+ if (!request) return;
46
+ const holder = shownTabs.value.find((tab) =>
47
+ tabHoldsField(tab, request.key),
48
+ );
49
+ if (holder) pickedTab.value = holder.name;
50
+ },
51
+ { immediate: true },
52
+ );
16
53
 
17
54
  // Recursively check if a tab (or any of its descendants) contains errors.
18
55
  function tabHasErrors(tab: TabEntry, errorKeys: Set<string>): boolean {
@@ -25,8 +62,24 @@ function tabHasErrors(tab: TabEntry, errorKeys: Set<string>): boolean {
25
62
  );
26
63
  }
27
64
 
65
+ // One badge per icon and color of the annotations in each tab, so a tab tells
66
+ // what its fields hold before it is opened.
67
+ const annotationGroups = computed(
68
+ () =>
69
+ new Map(
70
+ props.tabs.map((tab) => [
71
+ tab.name,
72
+ tabAnnotationGroups(
73
+ tab,
74
+ form.annotations?.value ?? {},
75
+ form.model.value,
76
+ form.currentLang.value,
77
+ ),
78
+ ]),
79
+ ),
80
+ );
81
+
28
82
  // Highlight tabs containing errors.
29
- const form = injectMapoForm()!;
30
83
  const tabsWithErrors = computed(() => {
31
84
  const errorKeys = new Set(Object.keys(form.errors.value));
32
85
  return new Set(
@@ -38,22 +91,25 @@ const tabsWithErrors = computed(() => {
38
91
  <template>
39
92
  <div class="mapo-form-tabs">
40
93
  <!-- Tab bar -->
41
- <div class="flex gap-1 border-b border-gray-200 pb-px">
94
+ <div class="flex flex-wrap gap-1 border-b border-gray-200 pb-px">
42
95
  <button
43
- v-for="tab in tabs"
96
+ v-for="tab in shownTabs"
44
97
  :key="tab.name"
45
98
  type="button"
46
- class="relative flex items-center gap-1.5 rounded-t px-4 py-2 text-sm font-medium transition-colors"
99
+ class="relative flex max-w-full items-center gap-1.5 rounded-t px-4 py-2 text-sm font-medium transition-colors"
47
100
  :class="[
48
101
  activeTab === tab.name
49
102
  ? 'border-b-2 border-primary-500 text-primary-600'
50
103
  : 'text-gray-500 hover:bg-gray-50 hover:text-gray-700',
51
104
  ]"
52
- @click="activeTab = tab.name"
105
+ @click="pickedTab = tab.name"
53
106
  >
54
107
  <slot :name="`tab.${tab.name}.label`" :tab="tab">
55
108
  {{ tab.label ?? tab.name }}
56
109
  </slot>
110
+ <MapoFormAnnotationBadges
111
+ :groups="annotationGroups.get(tab.name) ?? []"
112
+ />
57
113
  <span
58
114
  v-if="tabsWithErrors.has(tab.name)"
59
115
  class="inline-block h-1.5 w-1.5 rounded-full bg-red-500"
@@ -48,10 +48,12 @@ function clear() {
48
48
  <MapoMediaUnavailable v-if="!available" />
49
49
 
50
50
  <template v-else>
51
- <!-- Selected media preview -->
51
+ <!-- Selected media preview. It and the trigger below paint their own
52
+ surface, like an input: `soft` buttons and transparent cards take
53
+ the page background and vanish on a tinted page. -->
52
54
  <div
53
55
  v-if="current"
54
- class="flex items-center gap-3 rounded-lg border border-default p-2"
56
+ class="flex items-center gap-3 rounded-lg border border-default bg-default p-2"
55
57
  >
56
58
  <MapoMediaPreview :media="current" size="sm" />
57
59
  <div class="flex-1 min-w-0">
@@ -77,7 +79,7 @@ function clear() {
77
79
  v-if="!readonly && !disabled"
78
80
  :icon="current ? 'i-lucide-refresh-cw' : 'i-lucide-image-plus'"
79
81
  size="sm"
80
- variant="soft"
82
+ variant="outline"
81
83
  color="neutral"
82
84
  :block="!current"
83
85
  @click="dialogOpen = true"
@@ -95,12 +95,13 @@ function onDragEnd() {
95
95
  </div>
96
96
  </div>
97
97
 
98
- <!-- Add button -->
98
+ <!-- Add button: outlined like MapoMediaField's trigger, so it keeps its
99
+ own surface on a tinted page. -->
99
100
  <UButton
100
101
  v-if="!readonly && !disabled"
101
102
  icon="i-lucide-plus"
102
103
  size="sm"
103
- variant="soft"
104
+ variant="outline"
104
105
  color="neutral"
105
106
  @click="dialogOpen = true"
106
107
  >