@mapomodule/form 2.0.0-beta.11 → 2.0.0-beta.13

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 (44) hide show
  1. package/dist/__tests__/repeaterTemplates.test.d.ts +2 -0
  2. package/dist/__tests__/repeaterTemplates.test.d.ts.map +1 -0
  3. package/dist/index.d.mts +1 -1
  4. package/dist/index.d.ts +1 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/module.json +1 -1
  7. package/dist/runtime/components/MapoForm.vue +2 -0
  8. package/dist/runtime/components/MapoForm.vue.d.ts +5 -0
  9. package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
  10. package/dist/runtime/components/MapoFormGroup.vue +2 -2
  11. package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
  12. package/dist/runtime/components/MapoFormTabs.vue +2 -2
  13. package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
  14. package/dist/runtime/components/fields/MapoRepeater.vue +66 -13
  15. package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
  16. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +156 -0
  17. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts +20 -0
  18. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -0
  19. package/dist/runtime/components/fields/MapoRepeaterItem.vue +13 -1
  20. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts +6 -2
  21. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
  22. package/dist/runtime/composables/useMapoForm.d.ts.map +1 -1
  23. package/dist/runtime/composables/useMapoForm.js +4 -1
  24. package/dist/runtime/types/descriptor.d.ts +37 -2
  25. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  26. package/dist/runtime/types/index.d.ts +1 -1
  27. package/dist/runtime/types/index.d.ts.map +1 -1
  28. package/dist/runtime/utils/repeaterTemplates.d.ts +18 -0
  29. package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
  30. package/dist/runtime/utils/repeaterTemplates.js +43 -0
  31. package/package.json +3 -3
  32. package/src/__tests__/repeaterTemplates.test.ts +134 -0
  33. package/src/__tests__/useMapoForm.test.ts +62 -2
  34. package/src/index.ts +1 -0
  35. package/src/runtime/components/MapoForm.vue +7 -0
  36. package/src/runtime/components/MapoFormGroup.vue +2 -2
  37. package/src/runtime/components/MapoFormTabs.vue +2 -2
  38. package/src/runtime/components/fields/MapoRepeater.vue +75 -13
  39. package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +171 -0
  40. package/src/runtime/components/fields/MapoRepeaterItem.vue +14 -0
  41. package/src/runtime/composables/useMapoForm.ts +6 -1
  42. package/src/runtime/types/descriptor.ts +42 -2
  43. package/src/runtime/types/index.ts +1 -0
  44. package/src/runtime/utils/repeaterTemplates.ts +80 -0
@@ -0,0 +1,171 @@
1
+ <script setup lang="ts">
2
+ import { ref, computed, watch } from "vue";
3
+ import { useI18n } from "vue-i18n";
4
+ import type { NormalizedRepeaterTemplate } from "../../types/descriptor.js";
5
+ import {
6
+ filterTemplates,
7
+ wireframeShapes,
8
+ } from "../../utils/repeaterTemplates.js";
9
+
10
+ const props = defineProps<{
11
+ templates: NormalizedRepeaterTemplate[];
12
+ /** Disables the cards (e.g. `maxItems` reached). */
13
+ disabled?: boolean;
14
+ }>();
15
+
16
+ const open = defineModel<boolean>("open", { default: false });
17
+
18
+ const emit = defineEmits<{
19
+ /** `keepOpen` is true when Shift was held, so the caller can add several blocks. */
20
+ select: [key: string, keepOpen: boolean];
21
+ }>();
22
+
23
+ const { t } = useI18n();
24
+
25
+ const query = ref("");
26
+ const category = ref("");
27
+
28
+ watch(open, (isOpen) => {
29
+ if (isOpen) {
30
+ query.value = "";
31
+ category.value = "";
32
+ }
33
+ });
34
+
35
+ const categories = computed(
36
+ () =>
37
+ [
38
+ ...new Set(props.templates.map((tpl) => tpl.category).filter(Boolean)),
39
+ ] as string[],
40
+ );
41
+
42
+ const filtered = computed(() =>
43
+ filterTemplates(props.templates, query.value, category.value),
44
+ );
45
+
46
+ const shapesFor = wireframeShapes;
47
+ function pick(key: string, event: MouseEvent) {
48
+ if (props.disabled) return;
49
+ emit("select", key, event.shiftKey);
50
+ }
51
+ </script>
52
+
53
+ <template>
54
+ <UModal v-model:open="open" :title="t('mapo.repeater.chooseBlock')">
55
+ <template #body>
56
+ <div class="space-y-4">
57
+ <UInput
58
+ v-model="query"
59
+ icon="i-lucide-search"
60
+ :placeholder="t('mapo.repeater.searchBlocks')"
61
+ class="w-full"
62
+ autofocus
63
+ />
64
+
65
+ <div v-if="categories.length" class="flex flex-wrap gap-1.5">
66
+ <UButton
67
+ size="xs"
68
+ :variant="category === '' ? 'solid' : 'outline'"
69
+ color="neutral"
70
+ :label="t('mapo.repeater.allCategories')"
71
+ @click="category = ''"
72
+ />
73
+ <UButton
74
+ v-for="cat in categories"
75
+ :key="cat"
76
+ size="xs"
77
+ :variant="category === cat ? 'solid' : 'outline'"
78
+ color="neutral"
79
+ :label="cat"
80
+ @click="category = cat"
81
+ />
82
+ </div>
83
+
84
+ <div
85
+ v-if="filtered.length"
86
+ class="grid grid-cols-1 gap-3 sm:grid-cols-2"
87
+ >
88
+ <button
89
+ v-for="tpl in filtered"
90
+ :key="tpl.key"
91
+ type="button"
92
+ class="group flex flex-col overflow-hidden rounded-lg border border-default text-left transition hover:border-primary hover:shadow focus-visible:outline-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-50"
93
+ :disabled="disabled"
94
+ :data-template="tpl.key"
95
+ @click="pick(tpl.key, $event)"
96
+ >
97
+ <!-- Preview: image > wireframe > icon -->
98
+ <div
99
+ class="flex h-28 w-full items-center justify-center bg-elevated"
100
+ >
101
+ <img
102
+ v-if="tpl.image"
103
+ :src="tpl.image"
104
+ alt=""
105
+ class="h-full w-full object-cover"
106
+ />
107
+ <div
108
+ v-else-if="tpl.wireframe"
109
+ class="flex w-3/4 flex-col gap-1.5"
110
+ aria-hidden="true"
111
+ >
112
+ <template v-for="(shape, i) in shapesFor(tpl.fields)" :key="i">
113
+ <div
114
+ v-if="shape === 'line'"
115
+ class="h-2 w-full rounded bg-accented"
116
+ />
117
+ <div v-else-if="shape === 'lines'" class="space-y-1">
118
+ <div class="h-1.5 w-full rounded bg-accented" />
119
+ <div class="h-1.5 w-5/6 rounded bg-accented" />
120
+ </div>
121
+ <div
122
+ v-else-if="shape === 'box'"
123
+ class="h-8 w-full rounded bg-accented"
124
+ />
125
+ <div v-else class="h-2.5 w-2.5 rounded-sm bg-accented" />
126
+ </template>
127
+ </div>
128
+ <UIcon
129
+ v-else
130
+ :name="tpl.icon ?? 'i-lucide-layout-template'"
131
+ class="h-10 w-10 text-dimmed group-hover:text-primary"
132
+ />
133
+ </div>
134
+
135
+ <div class="space-y-0.5 p-3">
136
+ <div class="flex items-center gap-1.5">
137
+ <UIcon
138
+ v-if="tpl.icon && (tpl.image || tpl.wireframe)"
139
+ :name="tpl.icon"
140
+ class="h-4 w-4 shrink-0 text-muted"
141
+ />
142
+ <span class="truncate text-sm font-medium text-highlighted">
143
+ {{ tpl.label }}
144
+ </span>
145
+ <UBadge
146
+ v-if="tpl.category"
147
+ size="xs"
148
+ variant="subtle"
149
+ color="neutral"
150
+ class="ml-auto"
151
+ >
152
+ {{ tpl.category }}
153
+ </UBadge>
154
+ </div>
155
+ <p v-if="tpl.description" class="line-clamp-2 text-xs text-muted">
156
+ {{ tpl.description }}
157
+ </p>
158
+ </div>
159
+ </button>
160
+ </div>
161
+ <p v-else class="py-6 text-center text-sm text-dimmed">
162
+ {{ t("mapo.repeater.noBlocksFound") }}
163
+ </p>
164
+
165
+ <p class="text-xs text-dimmed">
166
+ {{ t("mapo.repeater.keepOpenHint") }}
167
+ </p>
168
+ </div>
169
+ </template>
170
+ </UModal>
171
+ </template>
@@ -35,6 +35,10 @@ const props = defineProps<{
35
35
  selected?: boolean;
36
36
  /** true = bulk selection mode is active in the parent repeater. */
37
37
  selectionMode?: boolean;
38
+ /** Display name of the item's template (repeaters with `attrs.templates`). */
39
+ templateLabel?: string;
40
+ /** Icon of the item's template. */
41
+ templateIcon?: string;
38
42
  }>();
39
43
 
40
44
  const emit = defineEmits<{
@@ -291,6 +295,16 @@ function enterFocusMode() {
291
295
  selectionMode ? toggleSelectFromEvent($event) : (expanded = !expanded)
292
296
  "
293
297
  >
298
+ <UBadge
299
+ v-if="templateLabel"
300
+ color="neutral"
301
+ variant="subtle"
302
+ size="xs"
303
+ :icon="templateIcon"
304
+ class="shrink-0"
305
+ >
306
+ {{ templateLabel }}
307
+ </UBadge>
294
308
  <span class="truncate">{{ label }}</span>
295
309
  <UBadge v-if="hasErrors" color="error" variant="soft" size="xs">
296
310
  errors
@@ -140,7 +140,12 @@ export function useMapoForm<T extends object>(options: UseMapoFormOptions<T>) {
140
140
  const entries = Object.entries(val);
141
141
  if (!entries.length) return;
142
142
  const lines = entries
143
- .map(([field, msgs]) => `${field}: ${(msgs as string[]).join(", ")}`)
143
+ .map(([field, msgs]) => {
144
+ const label =
145
+ toValue(fields).find((descriptor) => descriptor.key === field)
146
+ ?.label || field;
147
+ return `${label}: ${(msgs as string[]).join(", ")}`;
148
+ })
144
149
  .join("\n");
145
150
  snack.show(lines, "error");
146
151
  },
@@ -303,6 +303,36 @@ export interface MapDescriptor<
303
303
  };
304
304
  }
305
305
 
306
+ /**
307
+ * A repeater block template with presentation metadata.
308
+ * Use it instead of a bare field array to describe the block in the picker.
309
+ */
310
+ export interface RepeaterTemplate {
311
+ /** Field descriptors of the block. */
312
+ fields: AnyFieldDescriptor[];
313
+ /** Human-readable name. Defaults to the template key. */
314
+ label?: string;
315
+ /** Short explanation of what the block is for. */
316
+ description?: string;
317
+ /** Iconify name (e.g. `i-lucide-image`). Used as the preview fallback and in the item header. */
318
+ icon?: string;
319
+ /** Preview image URL, shown in the picker card. Takes precedence over `wireframe` and `icon`. */
320
+ image?: string;
321
+ /** Draw a schematic preview generated from the template fields (used when there is no `image`). */
322
+ wireframe?: boolean;
323
+ /** Group name: the picker shows one filter chip per category. */
324
+ category?: string;
325
+ }
326
+
327
+ /** A {@link RepeaterTemplate} normalized for display, with its key and defaults applied. @internal */
328
+ export interface NormalizedRepeaterTemplate extends Omit<
329
+ RepeaterTemplate,
330
+ "label"
331
+ > {
332
+ key: string;
333
+ label: string;
334
+ }
335
+
306
336
  /** Descriptor for a drag-and-drop repeater with nested field descriptors. */
307
337
  export interface RepeaterDescriptor<
308
338
  T = Record<string, unknown>,
@@ -314,8 +344,18 @@ export interface RepeaterDescriptor<
314
344
  */
315
345
  fields?: AnyFieldDescriptor[];
316
346
  attrs?: {
317
- /** Multiple templates for heterogeneous items (`oneOf` discriminated union). */
318
- templates?: Record<string, AnyFieldDescriptor[]>;
347
+ /**
348
+ * Multiple templates for heterogeneous items (`oneOf` discriminated union).
349
+ * A value is either a bare field array or a {@link RepeaterTemplate} with label,
350
+ * description, icon, preview image and category.
351
+ */
352
+ templates?: Record<string, AnyFieldDescriptor[] | RepeaterTemplate>;
353
+ /**
354
+ * How the user chooses a template when adding an item: `select` (dropdown),
355
+ * `modal` (card gallery with search) or `auto`. Default: `auto`, which uses the
356
+ * modal when there are more than 3 templates.
357
+ */
358
+ picker?: "auto" | "select" | "modal";
319
359
  /** Label shown when an item is collapsed. Receives the item and its index. */
320
360
  previewLabel?: (item: unknown, index: number) => string;
321
361
  confirmDelete?: boolean;
@@ -21,6 +21,7 @@ export type {
21
21
  SeoDescriptor,
22
22
  MapDescriptor,
23
23
  RepeaterDescriptor,
24
+ RepeaterTemplate,
24
25
  MediaDescriptor,
25
26
  MediaM2mDescriptor,
26
27
  EnhancedMediaDescriptor,
@@ -0,0 +1,80 @@
1
+ import type {
2
+ AnyFieldDescriptor,
3
+ NormalizedRepeaterTemplate,
4
+ RepeaterDescriptor,
5
+ RepeaterTemplate,
6
+ } from "../types/descriptor.js";
7
+
8
+ /** Above this many templates, `picker: "auto"` switches from the dropdown to the modal. */
9
+ export const MODAL_PICKER_THRESHOLD = 3;
10
+
11
+ /**
12
+ * Turns `attrs.templates` into a list with defaults applied. A bare field array
13
+ * becomes a template whose label is its key.
14
+ */
15
+ export function normalizeTemplates(
16
+ templates: NonNullable<RepeaterDescriptor["attrs"]>["templates"],
17
+ ): NormalizedRepeaterTemplate[] {
18
+ return Object.entries(templates ?? {}).map(([key, tpl]) => {
19
+ const meta: Partial<RepeaterTemplate> = Array.isArray(tpl) ? {} : tpl;
20
+ return {
21
+ key,
22
+ label: meta.label ?? key,
23
+ description: meta.description,
24
+ icon: meta.icon,
25
+ image: meta.image,
26
+ wireframe: meta.wireframe,
27
+ category: meta.category,
28
+ fields: Array.isArray(tpl) ? tpl : (tpl.fields ?? []),
29
+ };
30
+ });
31
+ }
32
+
33
+ /** Whether the block picker modal replaces the dropdown for this many templates. */
34
+ export function shouldUseModalPicker(
35
+ picker: "auto" | "select" | "modal" | undefined,
36
+ templateCount: number,
37
+ ): boolean {
38
+ if (templateCount === 0) return false;
39
+ const mode = picker ?? "auto";
40
+ return (
41
+ mode === "modal" ||
42
+ (mode === "auto" && templateCount > MODAL_PICKER_THRESHOLD)
43
+ );
44
+ }
45
+
46
+ /** Filters templates by category and by a case-insensitive text query. */
47
+ export function filterTemplates(
48
+ templates: NormalizedRepeaterTemplate[],
49
+ query: string,
50
+ category: string,
51
+ ): NormalizedRepeaterTemplate[] {
52
+ const q = query.trim().toLowerCase();
53
+ return templates.filter((tpl) => {
54
+ if (category && tpl.category !== category) return false;
55
+ if (!q) return true;
56
+ return [tpl.label, tpl.description, tpl.category].some((v) =>
57
+ v?.toLowerCase().includes(q),
58
+ );
59
+ });
60
+ }
61
+
62
+ export type WireframeShape = "line" | "lines" | "box" | "check";
63
+
64
+ /** One schematic shape per field type, capped at 5 so it fits a picker card. */
65
+ export function wireframeShapes(
66
+ fields: AnyFieldDescriptor[],
67
+ ): WireframeShape[] {
68
+ return fields.slice(0, 5).map((f) => {
69
+ const type = String(f.type);
70
+ if (type === "textarea" || type === "editor") return "lines";
71
+ if (type.includes("media") || type === "file") return "box";
72
+ if (type === "boolean") return "check";
73
+ return "line";
74
+ });
75
+ }
76
+
77
+ /** True when adding one more item reaches `maxItems`. */
78
+ export function reachesMax(current: number, maxItems?: number): boolean {
79
+ return !!maxItems && current + 1 >= maxItems;
80
+ }