@mapomodule/form 2.0.0-beta.10 → 2.0.0-beta.12

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 (56) hide show
  1. package/README.md +37 -119
  2. package/dist/__tests__/repeaterTemplates.test.d.ts +2 -0
  3. package/dist/__tests__/repeaterTemplates.test.d.ts.map +1 -0
  4. package/dist/__tests__/tabVisibility.test.d.ts +2 -0
  5. package/dist/__tests__/tabVisibility.test.d.ts.map +1 -0
  6. package/dist/index.d.mts +1 -1
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/module.json +1 -1
  10. package/dist/runtime/components/MapoForm.vue +12 -3
  11. package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
  12. package/dist/runtime/components/MapoFormGroup.vue +2 -2
  13. package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
  14. package/dist/runtime/components/MapoFormTabs.vue +19 -7
  15. package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
  16. package/dist/runtime/components/fields/MapoMediaField.vue +5 -3
  17. package/dist/runtime/components/fields/MapoMediaField.vue.d.ts.map +1 -1
  18. package/dist/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  19. package/dist/runtime/components/fields/MapoMediaM2mField.vue.d.ts.map +1 -1
  20. package/dist/runtime/components/fields/MapoRepeater.vue +66 -13
  21. package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
  22. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +156 -0
  23. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts +20 -0
  24. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -0
  25. package/dist/runtime/components/fields/MapoRepeaterItem.vue +13 -1
  26. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts +6 -2
  27. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
  28. package/dist/runtime/components/fields/MapoWygEditor.vue +18 -12
  29. package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
  30. package/dist/runtime/types/descriptor.d.ts +37 -2
  31. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  32. package/dist/runtime/types/index.d.ts +1 -1
  33. package/dist/runtime/types/index.d.ts.map +1 -1
  34. package/dist/runtime/utils/repeaterTemplates.d.ts +18 -0
  35. package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
  36. package/dist/runtime/utils/repeaterTemplates.js +43 -0
  37. package/dist/runtime/utils/tabVisibility.d.ts +18 -0
  38. package/dist/runtime/utils/tabVisibility.d.ts.map +1 -0
  39. package/dist/runtime/utils/tabVisibility.js +18 -0
  40. package/package.json +4 -4
  41. package/src/__tests__/repeaterTemplates.test.ts +134 -0
  42. package/src/__tests__/tabVisibility.test.ts +102 -0
  43. package/src/index.ts +1 -0
  44. package/src/runtime/components/MapoForm.vue +17 -4
  45. package/src/runtime/components/MapoFormGroup.vue +2 -2
  46. package/src/runtime/components/MapoFormTabs.vue +27 -7
  47. package/src/runtime/components/fields/MapoMediaField.vue +5 -3
  48. package/src/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  49. package/src/runtime/components/fields/MapoRepeater.vue +75 -13
  50. package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +171 -0
  51. package/src/runtime/components/fields/MapoRepeaterItem.vue +14 -0
  52. package/src/runtime/components/fields/MapoWygEditor.vue +18 -12
  53. package/src/runtime/types/descriptor.ts +42 -2
  54. package/src/runtime/types/index.ts +1 -0
  55. package/src/runtime/utils/repeaterTemplates.ts +80 -0
  56. package/src/runtime/utils/tabVisibility.ts +44 -0
@@ -0,0 +1,134 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import {
3
+ filterTemplates,
4
+ normalizeTemplates,
5
+ reachesMax,
6
+ shouldUseModalPicker,
7
+ wireframeShapes,
8
+ } from "../runtime/utils/repeaterTemplates.js";
9
+ import type { AnyFieldDescriptor } from "../runtime/types/descriptor.js";
10
+
11
+ const title: AnyFieldDescriptor = { key: "title", type: "text" };
12
+
13
+ describe("normalizeTemplates", () => {
14
+ it("returns an empty list without templates", () => {
15
+ expect(normalizeTemplates(undefined)).toEqual([]);
16
+ });
17
+
18
+ it("uses the key as label for a bare field array", () => {
19
+ const [tpl] = normalizeTemplates({ cta: [title] });
20
+ expect(tpl).toMatchObject({ key: "cta", label: "cta", fields: [title] });
21
+ expect(tpl?.description).toBeUndefined();
22
+ });
23
+
24
+ it("keeps the metadata of a RepeaterTemplate", () => {
25
+ const [tpl] = normalizeTemplates({
26
+ hero: {
27
+ label: "Hero",
28
+ description: "Big headline",
29
+ icon: "i-lucide-image",
30
+ category: "Header",
31
+ wireframe: true,
32
+ fields: [title],
33
+ },
34
+ });
35
+ expect(tpl).toMatchObject({
36
+ key: "hero",
37
+ label: "Hero",
38
+ description: "Big headline",
39
+ icon: "i-lucide-image",
40
+ category: "Header",
41
+ wireframe: true,
42
+ fields: [title],
43
+ });
44
+ });
45
+
46
+ it("tolerates a template object without fields", () => {
47
+ expect(
48
+ normalizeTemplates({ empty: { label: "Empty" } as never })[0]?.fields,
49
+ ).toEqual([]);
50
+ });
51
+ });
52
+
53
+ describe("shouldUseModalPicker", () => {
54
+ it("never opens a modal without templates", () => {
55
+ expect(shouldUseModalPicker("modal", 0)).toBe(false);
56
+ });
57
+
58
+ it("switches to the modal above 3 templates in auto mode", () => {
59
+ expect(shouldUseModalPicker(undefined, 3)).toBe(false);
60
+ expect(shouldUseModalPicker("auto", 4)).toBe(true);
61
+ });
62
+
63
+ it("honours an explicit mode", () => {
64
+ expect(shouldUseModalPicker("select", 10)).toBe(false);
65
+ expect(shouldUseModalPicker("modal", 1)).toBe(true);
66
+ });
67
+ });
68
+
69
+ describe("filterTemplates", () => {
70
+ const list = normalizeTemplates({
71
+ hero: { label: "Hero", category: "Header", fields: [] },
72
+ text: {
73
+ label: "Rich text",
74
+ description: "Paragraphs",
75
+ category: "Content",
76
+ fields: [],
77
+ },
78
+ cta: [title],
79
+ });
80
+
81
+ it("returns everything without filters", () => {
82
+ expect(filterTemplates(list, "", "")).toHaveLength(3);
83
+ });
84
+
85
+ it("matches label and description, case-insensitively", () => {
86
+ expect(filterTemplates(list, "HERO", "").map((t) => t.key)).toEqual([
87
+ "hero",
88
+ ]);
89
+ expect(filterTemplates(list, "paragr", "").map((t) => t.key)).toEqual([
90
+ "text",
91
+ ]);
92
+ });
93
+
94
+ it("filters by category", () => {
95
+ expect(filterTemplates(list, "", "Content").map((t) => t.key)).toEqual([
96
+ "text",
97
+ ]);
98
+ });
99
+
100
+ it("combines query and category", () => {
101
+ expect(filterTemplates(list, "hero", "Content")).toEqual([]);
102
+ });
103
+ });
104
+
105
+ describe("wireframeShapes", () => {
106
+ it("maps field types to shapes and caps at 5", () => {
107
+ const fields = [
108
+ { key: "a", type: "text" },
109
+ { key: "b", type: "textarea" },
110
+ { key: "c", type: "media" },
111
+ { key: "d", type: "boolean" },
112
+ { key: "e", type: "editor" },
113
+ { key: "f", type: "text" },
114
+ ] as AnyFieldDescriptor[];
115
+ expect(wireframeShapes(fields)).toEqual([
116
+ "line",
117
+ "lines",
118
+ "box",
119
+ "check",
120
+ "lines",
121
+ ]);
122
+ });
123
+ });
124
+
125
+ describe("reachesMax", () => {
126
+ it("is false without a limit", () => {
127
+ expect(reachesMax(100, undefined)).toBe(false);
128
+ });
129
+
130
+ it("is true when the next item hits the limit", () => {
131
+ expect(reachesMax(2, 3)).toBe(true);
132
+ expect(reachesMax(1, 3)).toBe(false);
133
+ });
134
+ });
@@ -0,0 +1,102 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { AnyFieldDescriptor } from "../runtime/types/index.js";
3
+ import type { TabEntry } from "../runtime/types/layout.js";
4
+ import {
5
+ isFieldShown,
6
+ pickActiveTab,
7
+ tabHasShownFields,
8
+ } from "../runtime/utils/tabVisibility.js";
9
+
10
+ type Predicates = Pick<AnyFieldDescriptor, "visible" | "show">;
11
+
12
+ const field = (key: string, predicates: Predicates = {}) =>
13
+ ({ key, type: "text", ...predicates }) as AnyFieldDescriptor;
14
+
15
+ /** A tab holding its fields in one flat group, as MapoForm builds it. */
16
+ const tab = (
17
+ name: string,
18
+ fields: AnyFieldDescriptor[],
19
+ children: TabEntry[] = [],
20
+ ): TabEntry => ({
21
+ name,
22
+ groups: new Map([["__flat__", { fields, subtabs: new Map() }]]),
23
+ children: new Map(children.map((child) => [child.name, child])),
24
+ });
25
+
26
+ // Like a page whose content fields depend on the template picked in the sidebar.
27
+ const hasTemplate = ({ model }: { model: Record<string, unknown> }) =>
28
+ Boolean(model.template);
29
+
30
+ describe("isFieldShown", () => {
31
+ it("shows a field without predicates", () => {
32
+ expect(isFieldShown(field("title"), {})).toBe(true);
33
+ });
34
+
35
+ it("hides a field that `visible` removes", () => {
36
+ const body = field("body", { visible: hasTemplate });
37
+ expect(isFieldShown(body, { template: "" })).toBe(false);
38
+ expect(isFieldShown(body, { template: "Home" })).toBe(true);
39
+ });
40
+
41
+ it("hides a field that `show` hides", () => {
42
+ const body = field("body", { show: hasTemplate });
43
+ expect(isFieldShown(body, { template: "" })).toBe(false);
44
+ });
45
+ });
46
+
47
+ describe("tabHasShownFields", () => {
48
+ it("is false while every field is hidden and true once one shows", () => {
49
+ const general = tab("__default__", [
50
+ field("body", { visible: hasTemplate }),
51
+ field("intro", { visible: hasTemplate }),
52
+ ]);
53
+ expect(tabHasShownFields(general, { template: "" })).toBe(false);
54
+ expect(tabHasShownFields(general, { template: "Home" })).toBe(true);
55
+ });
56
+
57
+ it("counts the fields of a sub-tab", () => {
58
+ const seo: TabEntry = {
59
+ name: "SEO",
60
+ groups: new Map([
61
+ [
62
+ "meta",
63
+ { fields: [], subtabs: new Map([["basic", [field("meta_title")]]]) },
64
+ ],
65
+ ]),
66
+ children: new Map(),
67
+ };
68
+ expect(tabHasShownFields(seo, {})).toBe(true);
69
+ });
70
+
71
+ it("counts the fields of a child tab", () => {
72
+ const settings = tab(
73
+ "Settings",
74
+ [field("legacy", { visible: () => false })],
75
+ [tab("SEO", [field("meta_title")])],
76
+ );
77
+ expect(tabHasShownFields(settings, {})).toBe(true);
78
+ });
79
+ });
80
+
81
+ describe("pickActiveTab", () => {
82
+ const general = tab("__default__", []);
83
+ const seo = tab("SEO", []);
84
+
85
+ it("opens the first shown tab until the user picks one", () => {
86
+ expect(pickActiveTab([general, seo], null)).toBe("__default__");
87
+ // The first tab is hidden: the next one opens, not an empty panel.
88
+ expect(pickActiveTab([seo], null)).toBe("SEO");
89
+ });
90
+
91
+ it("keeps the picked tab while it is shown", () => {
92
+ expect(pickActiveTab([general, seo], "SEO")).toBe("SEO");
93
+ });
94
+
95
+ it("falls back to the first shown tab while the picked one is hidden", () => {
96
+ expect(pickActiveTab([seo], "__default__")).toBe("SEO");
97
+ });
98
+
99
+ it("is empty when no tab is shown", () => {
100
+ expect(pickActiveTab([], null)).toBe("");
101
+ });
102
+ });
package/src/index.ts CHANGED
@@ -41,6 +41,7 @@ export type {
41
41
  SeoDescriptor,
42
42
  MapDescriptor,
43
43
  RepeaterDescriptor,
44
+ RepeaterTemplate,
44
45
  MediaDescriptor,
45
46
  MediaM2mDescriptor,
46
47
  EnhancedMediaDescriptor,
@@ -2,6 +2,7 @@
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,
7
8
  FieldRegistry,
@@ -135,6 +136,12 @@ provideCurrentLang(currentLangRef);
135
136
 
136
137
  // ─── Group fields by tab/group ───────────────────────────────────────────────
137
138
 
139
+ const { t } = useI18n();
140
+
141
+ // Fields without a `tab` go here. It becomes a tab of its own only when other
142
+ // fields have one, and then it shows as "General" (`mapo.form.defaultTab`).
143
+ const DEFAULT_TAB = "__default__";
144
+
138
145
  function getOrCreateTab(map: Map<string, TabEntry>, name: string): TabEntry {
139
146
  if (!map.has(name)) {
140
147
  map.set(name, { name, groups: new Map(), children: new Map() });
@@ -151,7 +158,7 @@ const grouped = computed<Map<string, TabEntry>>(() => {
151
158
  // Normalise: 'a/b', ['a','b'], or 'a' all become a string[]
152
159
  const rawTab = field.tab;
153
160
  const tabPath: string[] = !rawTab
154
- ? ["__default__"]
161
+ ? [DEFAULT_TAB]
155
162
  : Array.isArray(rawTab)
156
163
  ? rawTab
157
164
  : rawTab.split("/");
@@ -179,17 +186,23 @@ const grouped = computed<Map<string, TabEntry>>(() => {
179
186
  }
180
187
  }
181
188
 
189
+ const defaultTab = tabs.get(DEFAULT_TAB);
190
+ if (defaultTab) defaultTab.label = t("mapo.form.defaultTab");
191
+
182
192
  return tabs;
183
193
  });
184
194
 
185
195
  const hasTabs = computed(() => {
186
196
  const keys = [...grouped.value.keys()];
187
- return keys.length > 1 || (keys.length === 1 && keys[0] !== "__default__");
197
+ const hasFields = keys.length > 0;
198
+ if (!hasFields) return false;
199
+ const hasSeveralTabs = keys.length > 1;
200
+ const hasNamedTab = keys[0] !== DEFAULT_TAB;
201
+ return hasSeveralTabs || hasNamedTab;
188
202
  });
189
203
 
190
204
  const defaultGroups = computed(
191
- () =>
192
- grouped.value.get("__default__")?.groups ?? new Map<string, GroupEntry>(),
205
+ () => grouped.value.get(DEFAULT_TAB)?.groups ?? new Map<string, GroupEntry>(),
193
206
  );
194
207
 
195
208
  const tabList = computed<TabEntry[]>(() => [...grouped.value.values()]);
@@ -217,12 +217,12 @@ function colClass(cols: AnyFieldDescriptor["cols"]): string {
217
217
  <!-- Sub-tab bar + content (when subtabs are present) -->
218
218
  <div v-if="subtabs && subtabs.size > 0" class="px-5 pb-5 pt-4">
219
219
  <!-- Sub-tab bar -->
220
- <div class="flex gap-1 border-b border-gray-200 mb-4">
220
+ <div class="mb-4 flex flex-wrap gap-1 border-b border-gray-200">
221
221
  <button
222
222
  v-for="[subtabName] of subtabs"
223
223
  :key="subtabName"
224
224
  type="button"
225
- class="relative px-3 py-1.5 text-sm font-medium transition-colors focus:outline-none"
225
+ class="relative max-w-full px-3 py-1.5 text-sm font-medium transition-colors focus:outline-none"
226
226
  :class="[
227
227
  activeSubtab === subtabName
228
228
  ? 'text-primary-600 border-b-2 border-primary-600 -mb-px'
@@ -3,6 +3,7 @@ import { ref, computed } 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 { pickActiveTab, tabHasShownFields } from "../utils/tabVisibility.js";
6
7
  import MapoFormGroup from "./MapoFormGroup.vue";
7
8
  import MapoFormFlatSection from "./MapoFormFlatSection.vue";
8
9
 
@@ -10,9 +11,29 @@ const { t } = useI18n();
10
11
 
11
12
  const props = defineProps<{ tabs: TabEntry[] }>();
12
13
 
13
- defineSlots<Record<string, unknown>>();
14
+ const slots = defineSlots<Record<string, unknown>>();
14
15
 
15
- const activeTab = ref(props.tabs[0]?.name ?? "");
16
+ const form = injectMapoForm<Record<string, unknown>>()!;
17
+
18
+ // A tab whose fields are all hidden by `visible` or `show` would open on an
19
+ // empty panel, so it leaves the tab bar until one of them shows. Content passed
20
+ // through the tab's own slots counts as something to show.
21
+ function tabHasContent(tab: TabEntry): boolean {
22
+ const hasPanelSlot = Boolean(slots[`tab.${tab.name}`]);
23
+ const hasBeforeSlot = Boolean(slots[`tab.${tab.name}.before`]);
24
+ const hasAfterSlot = Boolean(slots[`tab.${tab.name}.after`]);
25
+ if (hasPanelSlot || hasBeforeSlot || hasAfterSlot) return true;
26
+ return tabHasShownFields(tab, form.model.value);
27
+ }
28
+
29
+ const shownTabs = computed(() => props.tabs.filter(tabHasContent));
30
+
31
+ // The tab the user clicked; until then, or while it is hidden, the first shown
32
+ // tab is open.
33
+ const pickedTab = ref<string | null>(null);
34
+ const activeTab = computed(() =>
35
+ pickActiveTab(shownTabs.value, pickedTab.value),
36
+ );
16
37
 
17
38
  // Recursively check if a tab (or any of its descendants) contains errors.
18
39
  function tabHasErrors(tab: TabEntry, errorKeys: Set<string>): boolean {
@@ -26,7 +47,6 @@ function tabHasErrors(tab: TabEntry, errorKeys: Set<string>): boolean {
26
47
  }
27
48
 
28
49
  // Highlight tabs containing errors.
29
- const form = injectMapoForm()!;
30
50
  const tabsWithErrors = computed(() => {
31
51
  const errorKeys = new Set(Object.keys(form.errors.value));
32
52
  return new Set(
@@ -38,18 +58,18 @@ const tabsWithErrors = computed(() => {
38
58
  <template>
39
59
  <div class="mapo-form-tabs">
40
60
  <!-- Tab bar -->
41
- <div class="flex gap-1 border-b border-gray-200 pb-px">
61
+ <div class="flex flex-wrap gap-1 border-b border-gray-200 pb-px">
42
62
  <button
43
- v-for="tab in tabs"
63
+ v-for="tab in shownTabs"
44
64
  :key="tab.name"
45
65
  type="button"
46
- class="relative flex items-center gap-1.5 rounded-t px-4 py-2 text-sm font-medium transition-colors"
66
+ class="relative flex max-w-full items-center gap-1.5 rounded-t px-4 py-2 text-sm font-medium transition-colors"
47
67
  :class="[
48
68
  activeTab === tab.name
49
69
  ? 'border-b-2 border-primary-500 text-primary-600'
50
70
  : 'text-gray-500 hover:bg-gray-50 hover:text-gray-700',
51
71
  ]"
52
- @click="activeTab = tab.name"
72
+ @click="pickedTab = tab.name"
53
73
  >
54
74
  <slot :name="`tab.${tab.name}.label`" :tab="tab">
55
75
  {{ tab.label ?? tab.name }}
@@ -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
  >
@@ -4,8 +4,14 @@ import { useI18n } from "vue-i18n";
4
4
  import { VueDraggable } from "vue-draggable-plus";
5
5
  import { useConfirmStore } from "@mapomodule/store/runtime/stores/confirm";
6
6
  import type { RepeaterDescriptor } from "../../types/index.js";
7
+ import {
8
+ normalizeTemplates,
9
+ reachesMax,
10
+ shouldUseModalPicker,
11
+ } from "../../utils/repeaterTemplates.js";
7
12
  import { injectMapoForm } from "../../composables/useMapoForm.js";
8
13
  import MapoRepeaterItem from "./MapoRepeaterItem.vue";
14
+ import MapoRepeaterBlockPicker from "./MapoRepeaterBlockPicker.vue";
9
15
 
10
16
  const props = defineProps<{
11
17
  modelValue: unknown;
@@ -67,12 +73,40 @@ function undo() {
67
73
  // ─── Template selection ──────────────────────────────────────────────────────
68
74
 
69
75
  const templates = computed(() => attrs.value.templates);
70
- const templateKeys = computed(() =>
71
- templates.value ? Object.keys(templates.value) : [],
76
+
77
+ // Bare field arrays and RepeaterTemplate objects are both accepted.
78
+ const templateList = computed(() => normalizeTemplates(templates.value));
79
+ const templateByKey = computed(
80
+ () => new Map(templateList.value.map((tpl) => [tpl.key, tpl])),
72
81
  );
82
+ const templateKeys = computed(() => templateList.value.map((tpl) => tpl.key));
73
83
  const hasTemplates = computed(() => templateKeys.value.length > 0);
74
84
  const selectedTemplate = ref<string>("");
75
85
 
86
+ const useModalPicker = computed(() =>
87
+ shouldUseModalPicker(attrs.value.picker, templateKeys.value.length),
88
+ );
89
+ const pickerOpen = ref(false);
90
+
91
+ const selectItems = computed(() =>
92
+ templateList.value.map((tpl) => ({
93
+ label: tpl.label,
94
+ value: tpl.key,
95
+ description: tpl.description,
96
+ icon: tpl.icon,
97
+ })),
98
+ );
99
+
100
+ function templateLabelFor(item: Record<string, unknown>): string | undefined {
101
+ const key = item._template as string | undefined;
102
+ if (!key) return undefined;
103
+ return templateByKey.value.get(key)?.label ?? key;
104
+ }
105
+
106
+ function templateIconFor(item: Record<string, unknown>): string | undefined {
107
+ return templateByKey.value.get(item._template as string)?.icon;
108
+ }
109
+
76
110
  // Async-safe: when templates load after mount, auto-pick the first one.
77
111
  watchEffect(() => {
78
112
  if (!selectedTemplate.value && templateKeys.value[0]) {
@@ -81,9 +115,9 @@ watchEffect(() => {
81
115
  });
82
116
 
83
117
  function fieldsForItem(item: Record<string, unknown>) {
84
- if (!templates.value) return props.descriptor.fields ?? [];
118
+ if (!hasTemplates.value) return props.descriptor.fields ?? [];
85
119
  const key = (item._template as string) ?? selectedTemplate.value;
86
- return templates.value[key] ?? props.descriptor.fields ?? [];
120
+ return templateByKey.value.get(key)?.fields ?? props.descriptor.fields ?? [];
87
121
  }
88
122
 
89
123
  // ─── Item CRUD ───────────────────────────────────────────────────────────────
@@ -122,15 +156,28 @@ async function askConfirm(message: string, title?: string): Promise<boolean> {
122
156
  }
123
157
  }
124
158
 
125
- function addItem() {
159
+ function addItem(templateKey?: string) {
126
160
  if (!canAdd.value) return;
127
161
  pushUndo();
128
162
  const newItem: Record<string, unknown> = hasTemplates.value
129
- ? { _template: selectedTemplate.value }
163
+ ? { _template: templateKey ?? selectedTemplate.value }
130
164
  : {};
131
165
  emitItems([...items.value, newItem]);
132
166
  }
133
167
 
168
+ function onAddClick() {
169
+ if (useModalPicker.value) pickerOpen.value = true;
170
+ else addItem();
171
+ }
172
+
173
+ function onPickTemplate(key: string, keepOpen: boolean) {
174
+ // `items` still holds the pre-add array here, so count the new item explicitly.
175
+ const full = reachesMax(items.value.length, attrs.value.maxItems);
176
+ selectedTemplate.value = key;
177
+ addItem(key);
178
+ if (!keepOpen || full) pickerOpen.value = false;
179
+ }
180
+
134
181
  async function deleteItem(index: number) {
135
182
  if (!canDelete.value) return;
136
183
  if (attrs.value.confirmDelete !== false) {
@@ -372,27 +419,38 @@ const errorPrefix = (index: number) =>
372
419
  @click="toggleSelectionMode"
373
420
  />
374
421
 
375
- <!-- Template selector -->
422
+ <!-- Template selector (dropdown mode; the modal picker opens from "Add") -->
376
423
  <USelectMenu
377
- v-if="hasTemplates"
424
+ v-if="hasTemplates && !useModalPicker"
378
425
  v-model="selectedTemplate"
379
- :items="templateKeys"
426
+ :items="selectItems"
427
+ value-key="value"
428
+ :search-input="templateKeys.length > 6"
429
+ :aria-label="t('mapo.repeater.blockType')"
380
430
  size="xs"
381
- class="w-32"
431
+ class="w-48"
382
432
  />
383
433
 
384
434
  <UButton
385
435
  size="sm"
386
436
  color="primary"
387
- icon="i-lucide-plus"
437
+ :icon="useModalPicker ? 'i-lucide-layout-grid' : 'i-lucide-plus'"
388
438
  :disabled="!canAdd"
389
- @click="addItem"
439
+ @click="onAddClick"
390
440
  >
391
- {{ t("mapo.add") }}
441
+ {{ useModalPicker ? t("mapo.repeater.addBlock") : t("mapo.add") }}
392
442
  </UButton>
393
443
  </div>
394
444
  </div>
395
445
 
446
+ <MapoRepeaterBlockPicker
447
+ v-if="useModalPicker"
448
+ v-model:open="pickerOpen"
449
+ :templates="templateList"
450
+ :disabled="!canAdd"
451
+ @select="onPickTemplate"
452
+ />
453
+
396
454
  <!-- ── Bulk actions bar (visible when there is a selection) ───────────── -->
397
455
  <Transition name="slide-down">
398
456
  <div
@@ -469,6 +527,8 @@ const errorPrefix = (index: number) =>
469
527
  <MapoRepeaterItem
470
528
  v-for="(item, index) in items"
471
529
  :key="`${uidFor(item)}-${collapseVersion}`"
530
+ :template-label="templateLabelFor(item)"
531
+ :template-icon="templateIconFor(item)"
472
532
  :uid="uidFor(item)"
473
533
  :item="item"
474
534
  :fields="fieldsForItem(item)"
@@ -501,6 +561,8 @@ const errorPrefix = (index: number) =>
501
561
  <MapoRepeaterItem
502
562
  v-for="(item, index) in items"
503
563
  :key="`${uidFor(item)}-${collapseVersion}`"
564
+ :template-label="templateLabelFor(item)"
565
+ :template-icon="templateIconFor(item)"
504
566
  :uid="uidFor(item)"
505
567
  :item="item"
506
568
  :fields="fieldsForItem(item)"