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

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 (48) hide show
  1. package/dist/__tests__/MapoFormGroup.component.test.d.ts +2 -0
  2. package/dist/__tests__/MapoFormGroup.component.test.d.ts.map +1 -0
  3. package/dist/__tests__/fieldVisibility.component.test.d.ts +2 -0
  4. package/dist/__tests__/fieldVisibility.component.test.d.ts.map +1 -0
  5. package/dist/index.d.mts +1 -1
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/module.json +1 -1
  9. package/dist/runtime/components/MapoFormField.vue +5 -10
  10. package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
  11. package/dist/runtime/components/MapoFormFlatSection.vue +21 -3
  12. package/dist/runtime/components/MapoFormFlatSection.vue.d.ts +1 -0
  13. package/dist/runtime/components/MapoFormFlatSection.vue.d.ts.map +1 -1
  14. package/dist/runtime/components/MapoFormGroup.vue +69 -34
  15. package/dist/runtime/components/MapoFormGroup.vue.d.ts +19 -4
  16. package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
  17. package/dist/runtime/components/fields/MapoRepeater.vue +3 -1
  18. package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
  19. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +12 -1
  20. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -1
  21. package/dist/runtime/components/fields/MapoRepeaterItem.vue +18 -2
  22. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
  23. package/dist/runtime/types/descriptor.d.ts +27 -3
  24. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  25. package/dist/runtime/types/index.d.ts +1 -1
  26. package/dist/runtime/types/index.d.ts.map +1 -1
  27. package/dist/runtime/utils/repeaterTemplates.d.ts +5 -3
  28. package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -1
  29. package/dist/runtime/utils/repeaterTemplates.js +2 -1
  30. package/dist/runtime/utils/tabVisibility.d.ts +8 -2
  31. package/dist/runtime/utils/tabVisibility.d.ts.map +1 -1
  32. package/dist/runtime/utils/tabVisibility.js +20 -2
  33. package/package.json +2 -2
  34. package/src/__tests__/MapoFormGroup.component.test.ts +130 -0
  35. package/src/__tests__/fieldVisibility.component.test.ts +546 -0
  36. package/src/__tests__/repeaterTemplates.test.ts +20 -0
  37. package/src/__tests__/tabVisibility.test.ts +27 -0
  38. package/src/index.ts +1 -0
  39. package/src/runtime/components/MapoFormField.vue +7 -13
  40. package/src/runtime/components/MapoFormFlatSection.vue +38 -5
  41. package/src/runtime/components/MapoFormGroup.vue +116 -43
  42. package/src/runtime/components/fields/MapoRepeater.vue +3 -1
  43. package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +19 -2
  44. package/src/runtime/components/fields/MapoRepeaterItem.vue +32 -2
  45. package/src/runtime/types/descriptor.ts +33 -3
  46. package/src/runtime/types/index.ts +1 -0
  47. package/src/runtime/utils/repeaterTemplates.ts +6 -1
  48. package/src/runtime/utils/tabVisibility.ts +42 -4
@@ -2,16 +2,43 @@
2
2
  import { ref, computed, provide, onMounted, onUnmounted } from "vue";
3
3
  import type { AnyFieldDescriptor } from "../types/index.js";
4
4
  import { injectMapoForm } from "../composables/useMapoForm.js";
5
+ import { isFieldRemoved, isFieldShown } from "../utils/tabVisibility.js";
5
6
  import MapoFormField from "./MapoFormField.vue";
6
7
 
7
- const props = defineProps<{ fields: AnyFieldDescriptor[] }>();
8
+ const props = defineProps<{
9
+ /** Fields to lay out in the 12-column grid. */
10
+ fields: AnyFieldDescriptor[];
11
+ }>();
8
12
 
9
13
  defineSlots<Record<string, unknown>>();
10
14
 
11
- const form = injectMapoForm();
15
+ const form = injectMapoForm<Record<string, unknown>>();
12
16
  const model = computed(() => form?.model.value);
13
17
  const currentLang = computed(() => form?.currentLang.value ?? "");
14
18
 
19
+ // ─── Visibility ──────────────────────────────────────────────────────────────
20
+
21
+ // Each field sits in a grid cell, and the cell follows the field: `visible`
22
+ // removes it with the field, so it leaves no gap in its row; `show` hides it
23
+ // and keeps the field mounted, with its state. This holds for a cell filled by
24
+ // the `field.<key>` slot too.
25
+ const predicateModel = computed<Record<string, unknown>>(
26
+ () => model.value ?? {},
27
+ );
28
+
29
+ const keptFields = computed(() =>
30
+ props.fields.filter((field) => !isFieldRemoved(field, predicateModel.value)),
31
+ );
32
+
33
+ function isShown(field: AnyFieldDescriptor): boolean {
34
+ return isFieldShown(field, predicateModel.value);
35
+ }
36
+
37
+ // With no field to show, the grid goes as well: empty, it would still add its
38
+ // spacing to the form.
39
+ const hasKeptFields = computed(() => keptFields.value.length > 0);
40
+ const hasShownFields = computed(() => keptFields.value.some(isShown));
41
+
15
42
  // ─── Field expand ────────────────────────────────────────────────────────────
16
43
 
17
44
  const expandedFieldKey = ref<string | null>(null);
@@ -139,10 +166,16 @@ function colClass(cols: AnyFieldDescriptor["cols"]): string {
139
166
  </Transition>
140
167
  </Teleport>
141
168
 
142
- <!-- Flat grid -->
143
- <div class="grid grid-cols-12 gap-5">
169
+ <!-- Flat grid: not rendered while `visible` removes every field, hidden while
170
+ none of the others shows. -->
171
+ <div
172
+ v-if="hasKeptFields"
173
+ v-show="hasShownFields"
174
+ class="grid grid-cols-12 gap-5"
175
+ >
144
176
  <div
145
- v-for="field in fields"
177
+ v-for="field in keptFields"
178
+ v-show="isShown(field)"
146
179
  :key="field.key as string"
147
180
  :class="colClass(field.cols)"
148
181
  >
@@ -1,43 +1,48 @@
1
1
  <script setup lang="ts">
2
- import {
3
- ref,
4
- computed,
5
- provide,
6
- watchEffect,
7
- onMounted,
8
- onUnmounted,
9
- } from "vue";
2
+ import { ref, computed, provide, onMounted, onUnmounted } from "vue";
10
3
  import { useRuntimeConfig } from "#app";
11
4
  import type {
12
5
  AnyFieldDescriptor,
13
6
  MapoFormRuntimeConfig,
14
7
  } from "../types/index.js";
15
8
  import { injectMapoForm } from "../composables/useMapoForm.js";
9
+ import {
10
+ isFieldRemoved,
11
+ isFieldShown,
12
+ pickActiveTab,
13
+ } from "../utils/tabVisibility.js";
16
14
  import MapoFormField from "./MapoFormField.vue";
17
15
 
18
- const props = defineProps<{
19
- name: string;
20
- label?: string;
21
- fields: AnyFieldDescriptor[];
22
- subtabs?: Map<string, AnyFieldDescriptor[]>;
23
- initialExpanded?: boolean;
24
- }>();
16
+ // Vue reads an absent Boolean prop as `false` unless the prop declares a
17
+ // default. The `undefined` default keeps "not passed" apart from `false`, so the
18
+ // module option `groups.expanded` decides when no caller sets `initialExpanded`.
19
+ const props = withDefaults(
20
+ defineProps<{
21
+ /** Group identifier; the heading shows it when `label` is not set. */
22
+ name: string;
23
+ /** Heading of the group card. */
24
+ label?: string;
25
+ /** Fields of the group outside any sub-tab, in a 12-column grid. */
26
+ fields: AnyFieldDescriptor[];
27
+ /**
28
+ * Sub-tab name → its fields, shown as a tab bar inside the card. A sub-tab
29
+ * with no field to show leaves the bar.
30
+ */
31
+ subtabs?: Map<string, AnyFieldDescriptor[]>;
32
+ /**
33
+ * Whether the group starts expanded. When omitted, the module option
34
+ * `groups.expanded` decides (`true` unless configured).
35
+ */
36
+ initialExpanded?: boolean;
37
+ }>(),
38
+ { label: undefined, subtabs: undefined, initialExpanded: undefined },
39
+ );
25
40
 
26
41
  const globalExpanded: boolean =
27
42
  (useRuntimeConfig().public.mapoForm as MapoFormRuntimeConfig | undefined)
28
43
  ?.groups?.expanded ?? true;
29
44
  const expanded = ref(props.initialExpanded ?? globalExpanded);
30
45
 
31
- // ─── Sub-tabs inside the group ────────────────────────────────────────────────
32
-
33
- const activeSubtab = ref<string>("");
34
- // Initialise to first subtab when subtabs are provided.
35
- watchEffect(() => {
36
- if (props.subtabs && props.subtabs.size > 0 && !activeSubtab.value) {
37
- activeSubtab.value = [...props.subtabs.keys()][0] ?? "";
38
- }
39
- });
40
-
41
46
  // ─── Field expand ────────────────────────────────────────────────────────────
42
47
 
43
48
  const expandedFieldKey = ref<string | null>(null);
@@ -64,10 +69,65 @@ onUnmounted(() => {
64
69
 
65
70
  provide("mapoGroupExpand", { expandField, collapseField, expandedFieldKey });
66
71
 
67
- const form = injectMapoForm();
72
+ const form = injectMapoForm<Record<string, unknown>>();
68
73
  const model = computed(() => form?.model.value);
69
74
  const currentLang = computed(() => form?.currentLang.value ?? "");
70
75
 
76
+ // ─── Visibility ──────────────────────────────────────────────────────────────
77
+
78
+ // Each field sits in a grid cell, and the cell follows the field: `visible`
79
+ // removes it with the field, so it leaves no gap in its row; `show` hides it
80
+ // and keeps the field mounted, with its state. This holds for a cell filled by
81
+ // the `field.<key>` slot too.
82
+ const predicateModel = computed<Record<string, unknown>>(
83
+ () => model.value ?? {},
84
+ );
85
+
86
+ function keptOf(fields: AnyFieldDescriptor[]): AnyFieldDescriptor[] {
87
+ return fields.filter((field) => !isFieldRemoved(field, predicateModel.value));
88
+ }
89
+
90
+ function isShown(field: AnyFieldDescriptor): boolean {
91
+ return isFieldShown(field, predicateModel.value);
92
+ }
93
+
94
+ const keptFields = computed(() => keptOf(props.fields));
95
+
96
+ // A sub-tab whose fields `visible` removes has no panel.
97
+ const keptSubtabs = computed(() =>
98
+ [...(props.subtabs?.entries() ?? [])]
99
+ .map(([name, fields]) => ({ name, fields: keptOf(fields) }))
100
+ .filter((subtab) => subtab.fields.length > 0),
101
+ );
102
+
103
+ // Like the form's tabs, a sub-tab with no field to show leaves the sub-tab bar.
104
+ const shownSubtabs = computed(() =>
105
+ keptSubtabs.value.filter((subtab) => subtab.fields.some(isShown)),
106
+ );
107
+
108
+ const hasKeptFlatFields = computed(() => keptFields.value.length > 0);
109
+ const hasKeptSubtabs = computed(() => keptSubtabs.value.length > 0);
110
+ const hasShownFlatFields = computed(() => keptFields.value.some(isShown));
111
+ const hasShownSubtabs = computed(() => shownSubtabs.value.length > 0);
112
+
113
+ // The card follows its fields the way a cell does: not rendered while `visible`
114
+ // removes them all, hidden while none of the others shows.
115
+ const hasKeptFields = computed(
116
+ () => hasKeptFlatFields.value || hasKeptSubtabs.value,
117
+ );
118
+ const hasShownFields = computed(
119
+ () => hasShownFlatFields.value || hasShownSubtabs.value,
120
+ );
121
+
122
+ // ─── Sub-tabs inside the group ────────────────────────────────────────────────
123
+
124
+ // The sub-tab the user clicked; until then, or while it is hidden, the first
125
+ // shown sub-tab is open.
126
+ const pickedSubtab = ref<string | null>(null);
127
+ const activeSubtab = computed(() =>
128
+ pickActiveTab(shownSubtabs.value, pickedSubtab.value),
129
+ );
130
+
71
131
  // ─── Col-span lookup ─────────────────────────────────────────────────────────
72
132
 
73
133
  const COL_SPAN: Record<number, string> = {
@@ -167,8 +227,11 @@ function colClass(cols: AnyFieldDescriptor["cols"]): string {
167
227
  </Transition>
168
228
  </Teleport>
169
229
 
170
- <!-- Regular group (kept in the DOM so the provide stays active) -->
230
+ <!-- Regular group: not rendered while `visible` removes all its fields,
231
+ hidden while none of the others shows. -->
171
232
  <div
233
+ v-if="hasKeptFields"
234
+ v-show="hasShownFields"
172
235
  class="mapo-form-group relative overflow-hidden rounded-xl border border-gray-200 bg-white shadow-sm"
173
236
  >
174
237
  <!-- Collapsible heading -->
@@ -188,14 +251,17 @@ function colClass(cols: AnyFieldDescriptor["cols"]): string {
188
251
 
189
252
  <!-- Body -->
190
253
  <div v-if="expanded">
191
- <!-- Flat fields (no subtab) -->
254
+ <!-- Flat fields (no subtab); the sub-tab area below brings its own top
255
+ padding. -->
192
256
  <div
193
- v-if="fields.length"
194
- class="grid grid-cols-12 gap-5 px-5 pb-5 pt-4"
195
- :class="{ 'pb-0': subtabs && subtabs.size > 0 }"
257
+ v-if="hasKeptFlatFields"
258
+ v-show="hasShownFlatFields"
259
+ class="grid grid-cols-12 gap-5 px-5 pt-4"
260
+ :class="hasShownSubtabs ? 'pb-0' : 'pb-5'"
196
261
  >
197
262
  <div
198
- v-for="field in fields"
263
+ v-for="field in keptFields"
264
+ v-show="isShown(field)"
199
265
  :key="field.key as string"
200
266
  :class="colClass(field.cols)"
201
267
  >
@@ -215,34 +281,41 @@ function colClass(cols: AnyFieldDescriptor["cols"]): string {
215
281
  </div>
216
282
 
217
283
  <!-- Sub-tab bar + content (when subtabs are present) -->
218
- <div v-if="subtabs && subtabs.size > 0" class="px-5 pb-5 pt-4">
284
+ <div
285
+ v-if="hasKeptSubtabs"
286
+ v-show="hasShownSubtabs"
287
+ class="px-5 pb-5 pt-4"
288
+ >
219
289
  <!-- Sub-tab bar -->
220
- <div class="mb-4 flex flex-wrap gap-1 border-b border-gray-200">
290
+ <div
291
+ class="mapo-form-subtab-bar mb-4 flex flex-wrap gap-1 border-b border-gray-200"
292
+ >
221
293
  <button
222
- v-for="[subtabName] of subtabs"
223
- :key="subtabName"
294
+ v-for="subtab in shownSubtabs"
295
+ :key="subtab.name"
224
296
  type="button"
225
297
  class="relative max-w-full px-3 py-1.5 text-sm font-medium transition-colors focus:outline-none"
226
298
  :class="[
227
- activeSubtab === subtabName
299
+ activeSubtab === subtab.name
228
300
  ? 'text-primary-600 border-b-2 border-primary-600 -mb-px'
229
301
  : 'text-gray-500 hover:text-gray-700',
230
302
  ]"
231
- @click="activeSubtab = subtabName"
303
+ @click="pickedSubtab = subtab.name"
232
304
  >
233
- {{ subtabName }}
305
+ {{ subtab.name }}
234
306
  </button>
235
307
  </div>
236
308
 
237
309
  <!-- Sub-tab fields -->
238
310
  <div
239
- v-for="[subtabName, subtabFields] of subtabs"
240
- v-show="activeSubtab === subtabName"
241
- :key="subtabName"
311
+ v-for="subtab in keptSubtabs"
312
+ v-show="activeSubtab === subtab.name"
313
+ :key="subtab.name"
242
314
  class="grid grid-cols-12 gap-5"
243
315
  >
244
316
  <div
245
- v-for="field in subtabFields"
317
+ v-for="field in subtab.fields"
318
+ v-show="isShown(field)"
246
319
  :key="field.key as string"
247
320
  :class="colClass(field.cols)"
248
321
  >
@@ -75,7 +75,9 @@ function undo() {
75
75
  const templates = computed(() => attrs.value.templates);
76
76
 
77
77
  // Bare field arrays and RepeaterTemplate objects are both accepted.
78
- const templateList = computed(() => normalizeTemplates(templates.value));
78
+ const templateList = computed(() =>
79
+ normalizeTemplates(templates.value, attrs.value.templateImageFit),
80
+ );
79
81
  const templateByKey = computed(
80
82
  () => new Map(templateList.value.map((tpl) => [tpl.key, tpl])),
81
83
  );
@@ -1,7 +1,10 @@
1
1
  <script setup lang="ts">
2
2
  import { ref, computed, watch } from "vue";
3
3
  import { useI18n } from "vue-i18n";
4
- import type { NormalizedRepeaterTemplate } from "../../types/descriptor.js";
4
+ import type {
5
+ NormalizedRepeaterTemplate,
6
+ RepeaterImageFit,
7
+ } from "../../types/descriptor.js";
5
8
  import {
6
9
  filterTemplates,
7
10
  wireframeShapes,
@@ -43,6 +46,19 @@ const filtered = computed(() =>
43
46
  filterTemplates(props.templates, query.value, category.value),
44
47
  );
45
48
 
49
+ // Whole class names, so Tailwind finds them when it scans this file. An unknown
50
+ // value from untyped app code falls back to `contain`.
51
+ const IMAGE_FIT_CLASS: Record<RepeaterImageFit, string> = {
52
+ contain: "object-contain",
53
+ cover: "object-cover",
54
+ fill: "object-fill",
55
+ none: "object-none",
56
+ "scale-down": "object-scale-down",
57
+ };
58
+ function imageFitClass(fit: RepeaterImageFit): string {
59
+ return IMAGE_FIT_CLASS[fit] ?? IMAGE_FIT_CLASS.contain;
60
+ }
61
+
46
62
  const shapesFor = wireframeShapes;
47
63
  function pick(key: string, event: MouseEvent) {
48
64
  if (props.disabled) return;
@@ -102,7 +118,8 @@ function pick(key: string, event: MouseEvent) {
102
118
  v-if="tpl.image"
103
119
  :src="tpl.image"
104
120
  alt=""
105
- class="h-full w-full object-cover"
121
+ class="h-full w-full"
122
+ :class="imageFitClass(tpl.imageFit)"
106
123
  />
107
124
  <div
108
125
  v-else-if="tpl.wireframe"
@@ -8,6 +8,7 @@ import type {
8
8
  } from "../../types/index.js";
9
9
  import { useMapoForm, injectMapoForm } from "../../composables/useMapoForm.js";
10
10
  import { useFocusMode } from "../../composables/useFocusMode.js";
11
+ import { isFieldRemoved, isFieldShown } from "../../utils/tabVisibility.js";
11
12
  import MapoFormField from "../MapoFormField.vue";
12
13
 
13
14
  const { t } = useI18n();
@@ -129,6 +130,25 @@ watchEffect(() => {
129
130
  if (parentCtx) form.ctx.submitted.value = parentCtx.submitted.value;
130
131
  });
131
132
 
133
+ // ─── Visibility ──────────────────────────────────────────────────────────────
134
+
135
+ // Each field of the item sits in a grid cell, and the cell follows the field as
136
+ // it does in a form section: `visible` removes it with the field, so it leaves
137
+ // no gap in its row; `show` hides it and keeps the field mounted, with its
138
+ // state. The predicates read the item's model, like the fields inside.
139
+ const keptFields = computed(() =>
140
+ props.fields.filter((field) => !isFieldRemoved(field, localModel.value)),
141
+ );
142
+
143
+ function isShown(field: AnyFieldDescriptor): boolean {
144
+ return isFieldShown(field, localModel.value);
145
+ }
146
+
147
+ // With no field to show, the item body goes as well: empty, its padding would
148
+ // still open a blank strip under the header.
149
+ const hasKeptFields = computed(() => keptFields.value.length > 0);
150
+ const hasShownFields = computed(() => keptFields.value.some(isShown));
151
+
132
152
  // ─── Preview label ────────────────────────────────────────────────────────────
133
153
 
134
154
  const label = computed(
@@ -148,6 +168,11 @@ const isCompressed = computed(() => {
148
168
  return hasMiniCard && (props.totalItems ?? 0) >= threshold;
149
169
  });
150
170
 
171
+ // The item body renders while the item is open, not shrunk to its mini-card,
172
+ // and `visible` keeps one of its fields.
173
+ const isOpen = computed(() => expanded.value && !isCompressed.value);
174
+ const rendersBody = computed(() => isOpen.value && hasKeptFields.value);
175
+
151
176
  const miniCard = computed(() =>
152
177
  miniCardAttrs.value?.miniCard?.(localModel.value, props.index),
153
178
  );
@@ -403,9 +428,14 @@ function enterFocusMode() {
403
428
 
404
429
  <!-- ── Item body (fields) ──────────────────────────────────────────────── -->
405
430
  <!-- COL_SPAN: Tailwind v4 requires static classes — do not use dynamic template literals. -->
406
- <div v-if="expanded && !isCompressed" class="grid grid-cols-12 gap-5 p-4">
431
+ <div
432
+ v-if="rendersBody"
433
+ v-show="hasShownFields"
434
+ class="grid grid-cols-12 gap-5 p-4"
435
+ >
407
436
  <div
408
- v-for="field in fields"
437
+ v-for="field in keptFields"
438
+ v-show="isShown(field)"
409
439
  :key="field.key as string"
410
440
  :class="{
411
441
  'col-span-1': field.cols === 1,
@@ -82,9 +82,17 @@ interface FieldBase<T> {
82
82
  required?: boolean;
83
83
  /** Renders the field in read-only mode (no user input allowed). */
84
84
  readonly?: boolean;
85
- /** `v-if` predicate — removes the field from the DOM entirely when `false`. */
85
+ /**
86
+ * `v-if` predicate — when `false`, removes the field from the DOM. Its grid
87
+ * cell in a form section, group, sub-tab or repeater item goes with it, so it
88
+ * leaves no gap. Inside a repeater item, `model` is the item.
89
+ */
86
90
  visible?: (ctx: { model: T }) => boolean;
87
- /** `v-show` predicate — hides the field while keeping it in the DOM. */
91
+ /**
92
+ * `v-show` predicate — when `false`, hides the field and its grid cell in a
93
+ * form section, group, sub-tab or repeater item; the field stays mounted and
94
+ * keeps its state. Inside a repeater item, `model` is the item.
95
+ */
88
96
  show?: (ctx: { model: T }) => boolean;
89
97
  /** Callback fired after the field value changes. */
90
98
  onChange?: (ctx: { model: T; val: unknown }) => void;
@@ -303,6 +311,17 @@ export interface MapDescriptor<
303
311
  };
304
312
  }
305
313
 
314
+ /**
315
+ * How a template's preview `image` fits its picker card: the CSS `object-fit` values.
316
+ * `contain` shows the whole image, `cover` fills the card and crops the edges.
317
+ */
318
+ export type RepeaterImageFit =
319
+ | "contain"
320
+ | "cover"
321
+ | "fill"
322
+ | "none"
323
+ | "scale-down";
324
+
306
325
  /**
307
326
  * A repeater block template with presentation metadata.
308
327
  * Use it instead of a bare field array to describe the block in the picker.
@@ -318,6 +337,11 @@ export interface RepeaterTemplate {
318
337
  icon?: string;
319
338
  /** Preview image URL, shown in the picker card. Takes precedence over `wireframe` and `icon`. */
320
339
  image?: string;
340
+ /**
341
+ * How `image` fits the picker card (CSS `object-fit`). Default: the repeater's
342
+ * `attrs.templateImageFit`, else `contain`, which shows the whole image.
343
+ */
344
+ imageFit?: RepeaterImageFit;
321
345
  /** Draw a schematic preview generated from the template fields (used when there is no `image`). */
322
346
  wireframe?: boolean;
323
347
  /** Group name: the picker shows one filter chip per category. */
@@ -327,10 +351,11 @@ export interface RepeaterTemplate {
327
351
  /** A {@link RepeaterTemplate} normalized for display, with its key and defaults applied. @internal */
328
352
  export interface NormalizedRepeaterTemplate extends Omit<
329
353
  RepeaterTemplate,
330
- "label"
354
+ "label" | "imageFit"
331
355
  > {
332
356
  key: string;
333
357
  label: string;
358
+ imageFit: RepeaterImageFit;
334
359
  }
335
360
 
336
361
  /** Descriptor for a drag-and-drop repeater with nested field descriptors. */
@@ -356,6 +381,11 @@ export interface RepeaterDescriptor<
356
381
  * modal when there are more than 3 templates.
357
382
  */
358
383
  picker?: "auto" | "select" | "modal";
384
+ /**
385
+ * How the templates' preview images fit the picker cards (CSS `object-fit`).
386
+ * A template's own `imageFit` wins. Default: `contain`.
387
+ */
388
+ templateImageFit?: RepeaterImageFit;
359
389
  /** Label shown when an item is collapsed. Receives the item and its index. */
360
390
  previewLabel?: (item: unknown, index: number) => string;
361
391
  confirmDelete?: boolean;
@@ -22,6 +22,7 @@ export type {
22
22
  MapDescriptor,
23
23
  RepeaterDescriptor,
24
24
  RepeaterTemplate,
25
+ RepeaterImageFit,
25
26
  MediaDescriptor,
26
27
  MediaM2mDescriptor,
27
28
  EnhancedMediaDescriptor,
@@ -2,6 +2,7 @@ import type {
2
2
  AnyFieldDescriptor,
3
3
  NormalizedRepeaterTemplate,
4
4
  RepeaterDescriptor,
5
+ RepeaterImageFit,
5
6
  RepeaterTemplate,
6
7
  } from "../types/descriptor.js";
7
8
 
@@ -10,10 +11,13 @@ export const MODAL_PICKER_THRESHOLD = 3;
10
11
 
11
12
  /**
12
13
  * Turns `attrs.templates` into a list with defaults applied. A bare field array
13
- * becomes a template whose label is its key.
14
+ * becomes a template whose label is its key. `imageFit` falls back to the
15
+ * repeater's `templateImageFit`, then to `contain`, so a preview is never cropped
16
+ * unless someone asks for it.
14
17
  */
15
18
  export function normalizeTemplates(
16
19
  templates: NonNullable<RepeaterDescriptor["attrs"]>["templates"],
20
+ defaultImageFit: RepeaterImageFit = "contain",
17
21
  ): NormalizedRepeaterTemplate[] {
18
22
  return Object.entries(templates ?? {}).map(([key, tpl]) => {
19
23
  const meta: Partial<RepeaterTemplate> = Array.isArray(tpl) ? {} : tpl;
@@ -23,6 +27,7 @@ export function normalizeTemplates(
23
27
  description: meta.description,
24
28
  icon: meta.icon,
25
29
  image: meta.image,
30
+ imageFit: meta.imageFit ?? defaultImageFit,
26
31
  wireframe: meta.wireframe,
27
32
  category: meta.category,
28
33
  fields: Array.isArray(tpl) ? tpl : (tpl.fields ?? []),
@@ -1,6 +1,43 @@
1
1
  import type { AnyFieldDescriptor } from "../types/index.js";
2
2
  import type { TabEntry } from "../types/layout.js";
3
3
 
4
+ /**
5
+ * Runs the field's `visible` or `show` predicate; a field without it passes.
6
+ * A predicate that throws, say one reading `model.seo.enabled` while `seo` is
7
+ * null, counts as `false`: it hides its own field, and the error does not take
8
+ * the field's section, group card or tab down with it. Development builds warn.
9
+ */
10
+ function passesPredicate(
11
+ field: AnyFieldDescriptor,
12
+ name: "visible" | "show",
13
+ model: Record<string, unknown>,
14
+ ): boolean {
15
+ const predicate = field[name];
16
+ if (!predicate) return true;
17
+ try {
18
+ return Boolean(predicate({ model }));
19
+ } catch (error) {
20
+ if (import.meta.dev) {
21
+ console.warn(
22
+ `[MapoForm] The \`${name}\` predicate of field "${String(field.key)}" threw, so the field is hidden.`,
23
+ error,
24
+ );
25
+ }
26
+ return false;
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Whether the field's `visible` predicate removes it for this model. A removed
32
+ * field is not rendered, and neither is its grid cell.
33
+ */
34
+ export function isFieldRemoved(
35
+ field: AnyFieldDescriptor,
36
+ model: Record<string, unknown>,
37
+ ): boolean {
38
+ return !passesPredicate(field, "visible", model);
39
+ }
40
+
4
41
  /**
5
42
  * Whether a field shows for this model: neither its `visible` predicate, which
6
43
  * removes it, nor its `show` predicate, which hides it, keeps it out.
@@ -9,8 +46,8 @@ export function isFieldShown(
9
46
  field: AnyFieldDescriptor,
10
47
  model: Record<string, unknown>,
11
48
  ): boolean {
12
- const removed = field.visible ? !field.visible({ model }) : false;
13
- const hidden = field.show ? !field.show({ model }) : false;
49
+ const removed = isFieldRemoved(field, model);
50
+ const hidden = !passesPredicate(field, "show", model);
14
51
  return !removed && !hidden;
15
52
  }
16
53
 
@@ -33,10 +70,11 @@ export function tabHasShownFields(
33
70
 
34
71
  /**
35
72
  * The tab to open among the ones shown: the tab the user picked, while it is
36
- * shown; otherwise the first one. Empty when no tab is shown.
73
+ * shown; otherwise the first one. Empty when no tab is shown. A group's
74
+ * sub-tabs are picked the same way.
37
75
  */
38
76
  export function pickActiveTab(
39
- shownTabs: TabEntry[],
77
+ shownTabs: Pick<TabEntry, "name">[],
40
78
  picked: string | null,
41
79
  ): string {
42
80
  const pickedTab = shownTabs.find((tab) => tab.name === picked);