@mapomodule/form 2.0.0-beta.14 → 2.0.0-beta.16
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.
- package/dist/__tests__/MapoFormGroup.component.test.d.ts +2 -0
- package/dist/__tests__/MapoFormGroup.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoWygEditor.component.test.d.ts +2 -0
- package/dist/__tests__/MapoWygEditor.component.test.d.ts.map +1 -0
- package/dist/__tests__/fieldVisibility.component.test.d.ts +2 -0
- package/dist/__tests__/fieldVisibility.component.test.d.ts.map +1 -0
- package/dist/__tests__/formatHtml.test.d.ts +2 -0
- package/dist/__tests__/formatHtml.test.d.ts.map +1 -0
- package/dist/__tests__/sanitizeHtml.test.d.ts +2 -0
- package/dist/__tests__/sanitizeHtml.test.d.ts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/runtime/components/MapoFormField.vue +5 -10
- package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormFlatSection.vue +21 -3
- package/dist/runtime/components/MapoFormFlatSection.vue.d.ts +1 -0
- package/dist/runtime/components/MapoFormFlatSection.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormGroup.vue +69 -34
- package/dist/runtime/components/MapoFormGroup.vue.d.ts +19 -4
- package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeaterItem.vue +18 -2
- package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoWygEditor.vue +157 -50
- package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
- package/dist/runtime/types/descriptor.d.ts +65 -2
- package/dist/runtime/types/descriptor.d.ts.map +1 -1
- package/dist/runtime/types/index.d.ts +1 -1
- package/dist/runtime/types/index.d.ts.map +1 -1
- package/dist/runtime/utils/formatHtml.d.ts +16 -0
- package/dist/runtime/utils/formatHtml.d.ts.map +1 -0
- package/dist/runtime/utils/formatHtml.js +137 -0
- package/dist/runtime/utils/sanitizeHtml.d.ts +16 -0
- package/dist/runtime/utils/sanitizeHtml.d.ts.map +1 -0
- package/dist/runtime/utils/sanitizeHtml.js +37 -0
- package/dist/runtime/utils/tabVisibility.d.ts +8 -2
- package/dist/runtime/utils/tabVisibility.d.ts.map +1 -1
- package/dist/runtime/utils/tabVisibility.js +20 -2
- package/package.json +2 -2
- package/src/__tests__/MapoFormGroup.component.test.ts +130 -0
- package/src/__tests__/MapoWygEditor.component.test.ts +525 -0
- package/src/__tests__/fieldVisibility.component.test.ts +546 -0
- package/src/__tests__/formatHtml.test.ts +145 -0
- package/src/__tests__/sanitizeHtml.test.ts +40 -0
- package/src/__tests__/tabVisibility.test.ts +27 -0
- package/src/index.ts +2 -0
- package/src/runtime/components/MapoFormField.vue +7 -13
- package/src/runtime/components/MapoFormFlatSection.vue +38 -5
- package/src/runtime/components/MapoFormGroup.vue +116 -43
- package/src/runtime/components/fields/MapoRepeaterItem.vue +32 -2
- package/src/runtime/components/fields/MapoWygEditor.vue +199 -69
- package/src/runtime/types/descriptor.ts +73 -3
- package/src/runtime/types/index.ts +2 -0
- package/src/runtime/utils/formatHtml.ts +198 -0
- package/src/runtime/utils/sanitizeHtml.ts +60 -0
- package/src/runtime/utils/tabVisibility.ts +42 -4
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { isSafeUrl, sanitizeHtml } from "../runtime/utils/sanitizeHtml.js";
|
|
4
|
+
|
|
5
|
+
// The URL check behind the editor's sanitizer and its Link extension. The
|
|
6
|
+
// sanitizer itself walks a DOM, which these tests do not have.
|
|
7
|
+
|
|
8
|
+
describe("isSafeUrl", () => {
|
|
9
|
+
it("accepts relative URLs and the allowed protocols", () => {
|
|
10
|
+
for (const url of [
|
|
11
|
+
"/page",
|
|
12
|
+
"page#anchor",
|
|
13
|
+
"?q=1",
|
|
14
|
+
"https://example.com",
|
|
15
|
+
"http://example.com",
|
|
16
|
+
"mailto:a@example.com",
|
|
17
|
+
"tel:+39000",
|
|
18
|
+
]) {
|
|
19
|
+
expect(isSafeUrl(url), url).toBe(true);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("rejects script and data protocols, whatever their case", () => {
|
|
24
|
+
for (const url of [
|
|
25
|
+
"javascript:alert(1)",
|
|
26
|
+
"JavaScript:alert(1)",
|
|
27
|
+
"vbscript:msgbox(1)",
|
|
28
|
+
"data:text/html,<script>alert(1)</script>",
|
|
29
|
+
]) {
|
|
30
|
+
expect(isSafeUrl(url), url).toBe(false);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("sanitizeHtml", () => {
|
|
36
|
+
it("returns the HTML as it is without a DOM (server side)", () => {
|
|
37
|
+
expect(sanitizeHtml("<p>a</p>")).toBe("<p>a</p>");
|
|
38
|
+
expect(sanitizeHtml("")).toBe("");
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
|
|
|
2
2
|
import type { AnyFieldDescriptor } from "../runtime/types/index.js";
|
|
3
3
|
import type { TabEntry } from "../runtime/types/layout.js";
|
|
4
4
|
import {
|
|
5
|
+
isFieldRemoved,
|
|
5
6
|
isFieldShown,
|
|
6
7
|
pickActiveTab,
|
|
7
8
|
tabHasShownFields,
|
|
@@ -27,6 +28,19 @@ const tab = (
|
|
|
27
28
|
const hasTemplate = ({ model }: { model: Record<string, unknown> }) =>
|
|
28
29
|
Boolean(model.template);
|
|
29
30
|
|
|
31
|
+
describe("isFieldRemoved", () => {
|
|
32
|
+
it("removes a field only through `visible`", () => {
|
|
33
|
+
expect(isFieldRemoved(field("title"), {})).toBe(false);
|
|
34
|
+
expect(
|
|
35
|
+
isFieldRemoved(field("body", { visible: hasTemplate }), { template: "" }),
|
|
36
|
+
).toBe(true);
|
|
37
|
+
// `show` hides the field but keeps it in the form.
|
|
38
|
+
expect(
|
|
39
|
+
isFieldRemoved(field("body", { show: hasTemplate }), { template: "" }),
|
|
40
|
+
).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
30
44
|
describe("isFieldShown", () => {
|
|
31
45
|
it("shows a field without predicates", () => {
|
|
32
46
|
expect(isFieldShown(field("title"), {})).toBe(true);
|
|
@@ -42,6 +56,19 @@ describe("isFieldShown", () => {
|
|
|
42
56
|
const body = field("body", { show: hasTemplate });
|
|
43
57
|
expect(isFieldShown(body, { template: "" })).toBe(false);
|
|
44
58
|
});
|
|
59
|
+
|
|
60
|
+
it("hides a field whose predicate throws, without throwing itself", () => {
|
|
61
|
+
// Reads a nested value the model does not have yet, as on a new record.
|
|
62
|
+
const seoEnabled = ({ model }: { model: Record<string, unknown> }) =>
|
|
63
|
+
(model.seo as { enabled: boolean }).enabled;
|
|
64
|
+
const removed = field("seo_title", { visible: seoEnabled });
|
|
65
|
+
const hidden = field("seo_title", { show: seoEnabled });
|
|
66
|
+
|
|
67
|
+
expect(isFieldRemoved(removed, { seo: null })).toBe(true);
|
|
68
|
+
expect(isFieldShown(removed, { seo: null })).toBe(false);
|
|
69
|
+
expect(isFieldRemoved(hidden, { seo: null })).toBe(false);
|
|
70
|
+
expect(isFieldShown(hidden, { seo: null })).toBe(false);
|
|
71
|
+
});
|
|
45
72
|
});
|
|
46
73
|
|
|
47
74
|
describe("tabHasShownFields", () => {
|
package/src/index.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
resolveFieldAccessor,
|
|
9
9
|
} from "../types/index.js";
|
|
10
10
|
import { injectMapoForm } from "../composables/useMapoForm.js";
|
|
11
|
+
import { isFieldRemoved, isFieldShown } from "../utils/tabVisibility.js";
|
|
11
12
|
import { getNestedValue, debounce } from "@mapomodule/utils";
|
|
12
13
|
import MapoUnknownField from "./MapoUnknownField.vue";
|
|
13
14
|
|
|
@@ -24,20 +25,13 @@ const model = form.model;
|
|
|
24
25
|
|
|
25
26
|
// ─── Visibility ──────────────────────────────────────────────────────────────
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return false;
|
|
33
|
-
return true;
|
|
34
|
-
});
|
|
28
|
+
// The same checks as the grid cell around the field, so a predicate that
|
|
29
|
+
// throws hides the field instead of breaking the render.
|
|
30
|
+
const isVisible = computed(
|
|
31
|
+
() => !isFieldRemoved(props.descriptor, model.value),
|
|
32
|
+
);
|
|
35
33
|
|
|
36
|
-
const showField = computed(() =>
|
|
37
|
-
if (props.descriptor.show && !props.descriptor.show({ model: model.value }))
|
|
38
|
-
return false;
|
|
39
|
-
return true;
|
|
40
|
-
});
|
|
34
|
+
const showField = computed(() => isFieldShown(props.descriptor, model.value));
|
|
41
35
|
|
|
42
36
|
// ─── Value ───────────────────────────────────────────────────────────────────
|
|
43
37
|
|
|
@@ -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<{
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
|
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="
|
|
194
|
-
|
|
195
|
-
|
|
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
|
|
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
|
|
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
|
|
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="
|
|
223
|
-
:key="
|
|
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 ===
|
|
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="
|
|
303
|
+
@click="pickedSubtab = subtab.name"
|
|
232
304
|
>
|
|
233
|
-
{{
|
|
305
|
+
{{ subtab.name }}
|
|
234
306
|
</button>
|
|
235
307
|
</div>
|
|
236
308
|
|
|
237
309
|
<!-- Sub-tab fields -->
|
|
238
310
|
<div
|
|
239
|
-
v-for="
|
|
240
|
-
v-show="activeSubtab ===
|
|
241
|
-
:key="
|
|
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
|
|
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
|
>
|
|
@@ -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
|
|
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
|
|
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,
|