@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.
- package/README.md +38 -119
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/module.d.mts +1 -0
- package/dist/module.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +9 -0
- package/dist/runtime/components/MapoFocusPortal.vue +9 -2
- package/dist/runtime/components/MapoFocusPortal.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoForm.vue +28 -6
- package/dist/runtime/components/MapoForm.vue.d.ts +13 -1
- package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormAnnotation.vue +79 -0
- package/dist/runtime/components/MapoFormAnnotation.vue.d.ts +27 -0
- package/dist/runtime/components/MapoFormAnnotation.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoFormAnnotationBadges.vue +28 -0
- package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts +13 -0
- package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoFormField.vue +47 -11
- package/dist/runtime/components/MapoFormField.vue.d.ts +15 -9
- package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormGroup.vue +18 -2
- package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormTabs.vue +50 -8
- package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoMediaField.vue +5 -3
- package/dist/runtime/components/fields/MapoMediaField.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoMediaM2mField.vue +3 -2
- package/dist/runtime/components/fields/MapoMediaM2mField.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeater.vue +73 -14
- package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +167 -0
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts +20 -0
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -0
- package/dist/runtime/components/fields/MapoRepeaterItem.vue +46 -10
- package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts +6 -2
- package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoWygEditor.vue +19 -13
- package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
- package/dist/runtime/composables/useFieldAnnotation.d.ts +18 -0
- package/dist/runtime/composables/useFieldAnnotation.d.ts.map +1 -0
- package/dist/runtime/composables/useFieldAnnotation.js +16 -0
- package/dist/runtime/composables/useFocusMode.d.ts +3 -1
- package/dist/runtime/composables/useFocusMode.d.ts.map +1 -1
- package/dist/runtime/composables/useFormFromSchema.d.ts +23 -1
- package/dist/runtime/composables/useFormFromSchema.d.ts.map +1 -1
- package/dist/runtime/composables/useFormFromSchema.js +171 -32
- package/dist/runtime/composables/useMapoForm.d.ts +21 -3
- package/dist/runtime/composables/useMapoForm.d.ts.map +1 -1
- package/dist/runtime/composables/useMapoForm.js +49 -15
- package/dist/runtime/types/annotation.d.ts +43 -0
- package/dist/runtime/types/annotation.d.ts.map +1 -0
- package/dist/runtime/types/annotation.js +0 -0
- package/dist/runtime/types/descriptor.d.ts +81 -4
- package/dist/runtime/types/descriptor.d.ts.map +1 -1
- package/dist/runtime/types/index.d.ts +2 -1
- package/dist/runtime/types/index.d.ts.map +1 -1
- package/dist/runtime/utils/annotations.d.ts +40 -0
- package/dist/runtime/utils/annotations.d.ts.map +1 -0
- package/dist/runtime/utils/annotations.js +78 -0
- package/dist/runtime/utils/draft.d.ts +21 -0
- package/dist/runtime/utils/draft.d.ts.map +1 -0
- package/dist/runtime/utils/draft.js +108 -0
- package/dist/runtime/utils/fieldComponent.d.ts +9 -0
- package/dist/runtime/utils/fieldComponent.d.ts.map +1 -0
- package/dist/runtime/utils/fieldComponent.js +12 -0
- package/dist/runtime/utils/fieldFocus.d.ts +24 -0
- package/dist/runtime/utils/fieldFocus.d.ts.map +1 -0
- package/dist/runtime/utils/fieldFocus.js +41 -0
- package/dist/runtime/utils/fieldsToJsonSchema.d.ts +38 -0
- package/dist/runtime/utils/fieldsToJsonSchema.d.ts.map +1 -0
- package/dist/runtime/utils/fieldsToJsonSchema.js +248 -0
- package/dist/runtime/utils/repeaterTemplates.d.ts +26 -0
- package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
- package/dist/runtime/utils/repeaterTemplates.js +51 -0
- package/dist/runtime/utils/reveal.d.ts +34 -0
- package/dist/runtime/utils/reveal.d.ts.map +1 -0
- package/dist/runtime/utils/reveal.js +29 -0
- package/dist/runtime/utils/tabVisibility.d.ts +18 -0
- package/dist/runtime/utils/tabVisibility.d.ts.map +1 -0
- package/dist/runtime/utils/tabVisibility.js +18 -0
- package/package.json +5 -5
- package/src/__tests__/MapoFocusPortal.component.test.ts +97 -0
- package/src/__tests__/MapoForm.component.test.ts +79 -0
- package/src/__tests__/MapoFormAnnotation.component.test.ts +244 -0
- package/src/__tests__/MapoFormField.component.test.ts +508 -0
- package/src/__tests__/MapoFormGroup.component.test.ts +141 -0
- package/src/__tests__/MapoFormTabs.component.test.ts +254 -0
- package/src/__tests__/MapoRepeaterItem.component.test.ts +286 -0
- package/src/__tests__/MapoWygEditor.component.test.ts +95 -0
- package/src/__tests__/annotations.test.ts +227 -0
- package/src/__tests__/draft.test.ts +221 -0
- package/src/__tests__/fieldComponent.test.ts +31 -0
- package/src/__tests__/fieldExtensions.test.ts +63 -0
- package/src/__tests__/fieldFocus.test.ts +178 -0
- package/src/__tests__/fieldsToJsonSchema.test.ts +630 -0
- package/src/__tests__/headlessRenderer.ts +85 -0
- package/src/__tests__/repeaterTemplates.test.ts +154 -0
- package/src/__tests__/reveal.test.ts +89 -0
- package/src/__tests__/schemaSources.test.ts +132 -0
- package/src/__tests__/tabVisibility.test.ts +102 -0
- package/src/__tests__/useFormFromSchema.test.ts +387 -0
- package/src/__tests__/useMapoForm.test.ts +213 -2
- package/src/index.ts +9 -0
- package/src/module.ts +9 -0
- package/src/runtime/components/MapoFocusPortal.vue +15 -2
- package/src/runtime/components/MapoForm.vue +47 -4
- package/src/runtime/components/MapoFormAnnotation.vue +113 -0
- package/src/runtime/components/MapoFormAnnotationBadges.vue +41 -0
- package/src/runtime/components/MapoFormField.vue +72 -11
- package/src/runtime/components/MapoFormGroup.vue +20 -2
- package/src/runtime/components/MapoFormTabs.vue +64 -8
- package/src/runtime/components/fields/MapoMediaField.vue +5 -3
- package/src/runtime/components/fields/MapoMediaM2mField.vue +3 -2
- package/src/runtime/components/fields/MapoRepeater.vue +82 -14
- package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +188 -0
- package/src/runtime/components/fields/MapoRepeaterItem.vue +56 -8
- package/src/runtime/components/fields/MapoWygEditor.vue +22 -14
- package/src/runtime/composables/useFieldAnnotation.ts +38 -0
- package/src/runtime/composables/useFocusMode.ts +7 -1
- package/src/runtime/composables/useFormFromSchema.ts +303 -41
- package/src/runtime/composables/useMapoForm.ts +103 -23
- package/src/runtime/types/annotation.ts +53 -0
- package/src/runtime/types/descriptor.ts +94 -4
- package/src/runtime/types/index.ts +10 -0
- package/src/runtime/utils/annotations.ts +169 -0
- package/src/runtime/utils/draft.ts +158 -0
- package/src/runtime/utils/fieldComponent.ts +34 -0
- package/src/runtime/utils/fieldFocus.ts +73 -0
- package/src/runtime/utils/fieldsToJsonSchema.ts +405 -0
- package/src/runtime/utils/repeaterTemplates.ts +103 -0
- package/src/runtime/utils/reveal.ts +68 -0
- package/src/runtime/utils/tabVisibility.ts +44 -0
- package/dist/__tests__/__mocks__/nuxt-imports.d.ts +0 -4
- package/dist/__tests__/__mocks__/nuxt-imports.d.ts.map +0 -1
- package/dist/__tests__/camomillaRoundTrip.test.d.ts +0 -2
- package/dist/__tests__/camomillaRoundTrip.test.d.ts.map +0 -1
- package/dist/__tests__/registry.test.d.ts +0 -2
- package/dist/__tests__/registry.test.d.ts.map +0 -1
- package/dist/__tests__/useFormFromSchema.test.d.ts +0 -2
- package/dist/__tests__/useFormFromSchema.test.d.ts.map +0 -1
- package/dist/__tests__/useMapoForm.test.d.ts +0 -2
- package/dist/__tests__/useMapoForm.test.d.ts.map +0 -1
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { createRenderer } from "vue";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A node of the headless renderer. Component tests run in Node without a DOM;
|
|
5
|
+
* nodes that are plain objects are enough to mount a component with real
|
|
6
|
+
* reactivity, watchers and events, and to read back what it rendered.
|
|
7
|
+
*/
|
|
8
|
+
export interface HeadlessNode {
|
|
9
|
+
/** Tag of an element; absent on text and comment nodes. */
|
|
10
|
+
tag?: string;
|
|
11
|
+
/** Content of a text node. */
|
|
12
|
+
text?: string;
|
|
13
|
+
/** Attributes and DOM props, as the last render set them. */
|
|
14
|
+
props: Record<string, unknown>;
|
|
15
|
+
parent: HeadlessNode | null;
|
|
16
|
+
children: HeadlessNode[];
|
|
17
|
+
// `v-show` reads and writes `style.display` on the element it is set on.
|
|
18
|
+
style: Record<string, string>;
|
|
19
|
+
// `<Transition>` adds and removes its classes on the element, and reads
|
|
20
|
+
// `ownerDocument.body.offsetHeight` to force a reflow before leaving.
|
|
21
|
+
classList: {
|
|
22
|
+
add(...names: string[]): void;
|
|
23
|
+
remove(...names: string[]): void;
|
|
24
|
+
};
|
|
25
|
+
ownerDocument: { body: { offsetHeight: number } };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const headlessDocument = { body: { offsetHeight: 0 } };
|
|
29
|
+
|
|
30
|
+
export const headlessNode = (tag?: string): HeadlessNode => ({
|
|
31
|
+
tag,
|
|
32
|
+
props: {},
|
|
33
|
+
parent: null,
|
|
34
|
+
children: [],
|
|
35
|
+
style: {},
|
|
36
|
+
classList: { add: () => {}, remove: () => {} },
|
|
37
|
+
ownerDocument: headlessDocument,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** Where `<Teleport to="body">` renders. */
|
|
41
|
+
export const headlessBody = headlessNode("body");
|
|
42
|
+
|
|
43
|
+
export const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
44
|
+
createElement: (tag) => headlessNode(tag),
|
|
45
|
+
createText: (text) => ({ ...headlessNode(), text }),
|
|
46
|
+
createComment: () => headlessNode(),
|
|
47
|
+
setText: (node, text) => {
|
|
48
|
+
node.text = text;
|
|
49
|
+
},
|
|
50
|
+
setElementText: (node, text) => {
|
|
51
|
+
node.children = text ? [{ ...headlessNode(), text, parent: node }] : [];
|
|
52
|
+
},
|
|
53
|
+
patchProp: (el, key, _previous, next) => {
|
|
54
|
+
el.props[key] = next;
|
|
55
|
+
},
|
|
56
|
+
insert(child, parent, anchor) {
|
|
57
|
+
child.parent = parent;
|
|
58
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
59
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
60
|
+
},
|
|
61
|
+
remove(child) {
|
|
62
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
63
|
+
child.parent = null;
|
|
64
|
+
},
|
|
65
|
+
parentNode: (node) => node.parent,
|
|
66
|
+
querySelector: (selector) => (selector === "body" ? headlessBody : null),
|
|
67
|
+
nextSibling: (node) =>
|
|
68
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/** The text a node renders, its descendants' included, without extra spaces. */
|
|
72
|
+
export function textOf(node: HeadlessNode): string {
|
|
73
|
+
const own = node.text ?? "";
|
|
74
|
+
const nested = node.children.map(textOf).join(" ");
|
|
75
|
+
return `${own} ${nested}`.replace(/\s+/g, " ").trim();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The elements under a node, in document order, that match the test. */
|
|
79
|
+
export function findAll(
|
|
80
|
+
node: HeadlessNode,
|
|
81
|
+
test: (candidate: HeadlessNode) => boolean,
|
|
82
|
+
): HeadlessNode[] {
|
|
83
|
+
const own = test(node) ? [node] : [];
|
|
84
|
+
return [...own, ...node.children.flatMap((child) => findAll(child, test))];
|
|
85
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
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("fits preview images with contain by default", () => {
|
|
47
|
+
const [bare, rich] = normalizeTemplates({
|
|
48
|
+
cta: [title],
|
|
49
|
+
hero: { image: "/hero.png", fields: [title] },
|
|
50
|
+
});
|
|
51
|
+
expect(bare?.imageFit).toBe("contain");
|
|
52
|
+
expect(rich?.imageFit).toBe("contain");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("applies the repeater's image fit and lets a template override it", () => {
|
|
56
|
+
const list = normalizeTemplates(
|
|
57
|
+
{
|
|
58
|
+
hero: { image: "/hero.png", fields: [title] },
|
|
59
|
+
logo: { image: "/logo.svg", imageFit: "scale-down", fields: [title] },
|
|
60
|
+
},
|
|
61
|
+
"cover",
|
|
62
|
+
);
|
|
63
|
+
expect(list.map((tpl) => tpl.imageFit)).toEqual(["cover", "scale-down"]);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("tolerates a template object without fields", () => {
|
|
67
|
+
expect(
|
|
68
|
+
normalizeTemplates({ empty: { label: "Empty" } as never })[0]?.fields,
|
|
69
|
+
).toEqual([]);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("shouldUseModalPicker", () => {
|
|
74
|
+
it("never opens a modal without templates", () => {
|
|
75
|
+
expect(shouldUseModalPicker("modal", 0)).toBe(false);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("switches to the modal above 3 templates in auto mode", () => {
|
|
79
|
+
expect(shouldUseModalPicker(undefined, 3)).toBe(false);
|
|
80
|
+
expect(shouldUseModalPicker("auto", 4)).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("honours an explicit mode", () => {
|
|
84
|
+
expect(shouldUseModalPicker("select", 10)).toBe(false);
|
|
85
|
+
expect(shouldUseModalPicker("modal", 1)).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("filterTemplates", () => {
|
|
90
|
+
const list = normalizeTemplates({
|
|
91
|
+
hero: { label: "Hero", category: "Header", fields: [] },
|
|
92
|
+
text: {
|
|
93
|
+
label: "Rich text",
|
|
94
|
+
description: "Paragraphs",
|
|
95
|
+
category: "Content",
|
|
96
|
+
fields: [],
|
|
97
|
+
},
|
|
98
|
+
cta: [title],
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("returns everything without filters", () => {
|
|
102
|
+
expect(filterTemplates(list, "", "")).toHaveLength(3);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("matches label and description, case-insensitively", () => {
|
|
106
|
+
expect(filterTemplates(list, "HERO", "").map((t) => t.key)).toEqual([
|
|
107
|
+
"hero",
|
|
108
|
+
]);
|
|
109
|
+
expect(filterTemplates(list, "paragr", "").map((t) => t.key)).toEqual([
|
|
110
|
+
"text",
|
|
111
|
+
]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("filters by category", () => {
|
|
115
|
+
expect(filterTemplates(list, "", "Content").map((t) => t.key)).toEqual([
|
|
116
|
+
"text",
|
|
117
|
+
]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("combines query and category", () => {
|
|
121
|
+
expect(filterTemplates(list, "hero", "Content")).toEqual([]);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("wireframeShapes", () => {
|
|
126
|
+
it("maps field types to shapes and caps at 5", () => {
|
|
127
|
+
const fields = [
|
|
128
|
+
{ key: "a", type: "text" },
|
|
129
|
+
{ key: "b", type: "textarea" },
|
|
130
|
+
{ key: "c", type: "media" },
|
|
131
|
+
{ key: "d", type: "boolean" },
|
|
132
|
+
{ key: "e", type: "editor" },
|
|
133
|
+
{ key: "f", type: "text" },
|
|
134
|
+
] as AnyFieldDescriptor[];
|
|
135
|
+
expect(wireframeShapes(fields)).toEqual([
|
|
136
|
+
"line",
|
|
137
|
+
"lines",
|
|
138
|
+
"box",
|
|
139
|
+
"check",
|
|
140
|
+
"lines",
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe("reachesMax", () => {
|
|
146
|
+
it("is false without a limit", () => {
|
|
147
|
+
expect(reachesMax(100, undefined)).toBe(false);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("is true when the next item hits the limit", () => {
|
|
151
|
+
expect(reachesMax(2, 3)).toBe(true);
|
|
152
|
+
expect(reachesMax(1, 3)).toBe(false);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { fieldAtPath, tabHoldsField } from "../runtime/utils/reveal";
|
|
4
|
+
import type { AnyFieldDescriptor } from "../runtime/types/index.js";
|
|
5
|
+
import type { TabEntry } from "../runtime/types/layout.js";
|
|
6
|
+
|
|
7
|
+
const fields: AnyFieldDescriptor[] = [
|
|
8
|
+
{ key: "title", type: "text", translatable: true },
|
|
9
|
+
{ key: "seo", type: "text" },
|
|
10
|
+
{ key: "seo.title", type: "text" },
|
|
11
|
+
{ key: "blocks", type: "repeater" },
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
describe("fieldAtPath", () => {
|
|
15
|
+
it("finds the field a path names", () => {
|
|
16
|
+
expect(fieldAtPath(fields, "seo", "")).toEqual({
|
|
17
|
+
field: fields[1],
|
|
18
|
+
rest: "",
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("finds the field whose value holds the path, with the rest of it", () => {
|
|
23
|
+
expect(fieldAtPath(fields, "blocks.2.author", "")).toEqual({
|
|
24
|
+
field: fields[3],
|
|
25
|
+
rest: "2.author",
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("prefers the longest key, so a dotted key wins over its start", () => {
|
|
30
|
+
expect(fieldAtPath(fields, "seo.title", "")?.field).toBe(fields[2]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("takes a translatable field's path in the language and its bare key", () => {
|
|
34
|
+
const inItalian = fieldAtPath(fields, "translations.it.title", "it");
|
|
35
|
+
const bare = fieldAtPath(fields, "title", "it");
|
|
36
|
+
|
|
37
|
+
expect(inItalian).toEqual({ field: fields[0], rest: "" });
|
|
38
|
+
expect(bare).toEqual({ field: fields[0], rest: "" });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("finds nothing for another language's path, or a key that only starts the same", () => {
|
|
42
|
+
expect(fieldAtPath(fields, "translations.en.title", "it")).toBeUndefined();
|
|
43
|
+
expect(fieldAtPath(fields, "titles", "")).toBeUndefined();
|
|
44
|
+
expect(fieldAtPath(fields, "blocksX.0", "")).toBeUndefined();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("tabHoldsField", () => {
|
|
49
|
+
const field = (key: string): AnyFieldDescriptor => ({ key, type: "text" });
|
|
50
|
+
const tab = (
|
|
51
|
+
name: string,
|
|
52
|
+
groups: TabEntry["groups"],
|
|
53
|
+
children: TabEntry[] = [],
|
|
54
|
+
): TabEntry => ({
|
|
55
|
+
name,
|
|
56
|
+
groups,
|
|
57
|
+
children: new Map(children.map((child) => [child.name, child])),
|
|
58
|
+
});
|
|
59
|
+
const content = tab(
|
|
60
|
+
"content",
|
|
61
|
+
new Map([
|
|
62
|
+
[
|
|
63
|
+
"main",
|
|
64
|
+
{
|
|
65
|
+
fields: [field("title")],
|
|
66
|
+
subtabs: new Map([["more", [field("subtitle")]]]),
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
]),
|
|
70
|
+
[
|
|
71
|
+
tab(
|
|
72
|
+
"advanced",
|
|
73
|
+
new Map([
|
|
74
|
+
["__flat__", { fields: [field("canonical")], subtabs: new Map() }],
|
|
75
|
+
]),
|
|
76
|
+
),
|
|
77
|
+
],
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
it("finds a field in a group, in a group's sub-tab and in a child tab", () => {
|
|
81
|
+
expect(tabHoldsField(content, "title")).toBe(true);
|
|
82
|
+
expect(tabHoldsField(content, "subtitle")).toBe(true);
|
|
83
|
+
expect(tabHoldsField(content, "canonical")).toBe(true);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("does not find a field of another tab", () => {
|
|
87
|
+
expect(tabHoldsField(content, "seo_title")).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
drfMetadataToJsonSchema,
|
|
4
|
+
openApiToJsonSchema,
|
|
5
|
+
} from "@mapomodule/utils";
|
|
6
|
+
|
|
7
|
+
import { useFormFromSchema } from "../runtime/composables/useFormFromSchema.js";
|
|
8
|
+
import type { AnyFieldDescriptor } from "../runtime/types/index.js";
|
|
9
|
+
|
|
10
|
+
// The normalizers of @mapomodule/utils write the dialect useFormFromSchema
|
|
11
|
+
// reads: these tests read their output as a form would.
|
|
12
|
+
|
|
13
|
+
/** `[key, type]` of each descriptor, and the attrs of one. */
|
|
14
|
+
const types = (fields: AnyFieldDescriptor[]) =>
|
|
15
|
+
fields.map((field) => [field.key, field.type]);
|
|
16
|
+
const attrsOf = (fields: AnyFieldDescriptor[], key: string) =>
|
|
17
|
+
(fields.find((field) => field.key === key) as { attrs?: object })?.attrs;
|
|
18
|
+
|
|
19
|
+
describe("DRF metadata → descriptors", () => {
|
|
20
|
+
const fields = useFormFromSchema(
|
|
21
|
+
drfMetadataToJsonSchema({
|
|
22
|
+
actions: {
|
|
23
|
+
POST: {
|
|
24
|
+
id: { type: "integer", read_only: true, label: "ID" },
|
|
25
|
+
title: { type: "string", required: true, label: "Title" },
|
|
26
|
+
status: {
|
|
27
|
+
type: "choice",
|
|
28
|
+
label: "Status",
|
|
29
|
+
choices: [{ value: "draft", display_name: "Draft" }],
|
|
30
|
+
},
|
|
31
|
+
contact: { type: "email", label: "Contact" },
|
|
32
|
+
poster: { type: "image upload", label: "Poster" },
|
|
33
|
+
seats: { type: "integer", min_value: 1, max_value: 9 },
|
|
34
|
+
starts_at: { type: "datetime", label: "Starts at" },
|
|
35
|
+
venue: { type: "field", required: true, label: "Venue" },
|
|
36
|
+
keywords: { type: "list", child: { type: "string" } },
|
|
37
|
+
address: {
|
|
38
|
+
type: "nested object",
|
|
39
|
+
children: { city: { type: "string" } },
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
})!,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
it("gives each DRF type its field", () => {
|
|
47
|
+
expect(types(fields)).toEqual([
|
|
48
|
+
["id", "number"],
|
|
49
|
+
["title", "text"],
|
|
50
|
+
["status", "select"],
|
|
51
|
+
["contact", "email"],
|
|
52
|
+
["poster", "file"],
|
|
53
|
+
["seats", "number"],
|
|
54
|
+
["starts_at", "datetime"],
|
|
55
|
+
["venue", "field:venue"],
|
|
56
|
+
["keywords", "select"],
|
|
57
|
+
["address", "object:address"],
|
|
58
|
+
]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("carries labels, flags, choices and limits", () => {
|
|
62
|
+
expect(fields[0]).toMatchObject({ label: "ID", readonly: true });
|
|
63
|
+
expect(fields[1]).toMatchObject({ label: "Title", required: true });
|
|
64
|
+
expect(attrsOf(fields, "status")).toEqual({
|
|
65
|
+
options: [{ text: "Draft", value: "draft" }],
|
|
66
|
+
});
|
|
67
|
+
expect(attrsOf(fields, "poster")).toEqual({ accept: "image/*" });
|
|
68
|
+
expect(attrsOf(fields, "seats")).toEqual({ min: 1, max: 9 });
|
|
69
|
+
expect(attrsOf(fields, "keywords")).toEqual({ multiple: true });
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("OpenAPI → descriptors", () => {
|
|
74
|
+
const document = {
|
|
75
|
+
paths: {
|
|
76
|
+
"/api/venues/": {
|
|
77
|
+
get: {
|
|
78
|
+
responses: {
|
|
79
|
+
"200": {
|
|
80
|
+
content: {
|
|
81
|
+
"application/json": {
|
|
82
|
+
schema: {
|
|
83
|
+
type: "array",
|
|
84
|
+
items: { $ref: "#/components/schemas/Venue" },
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
components: {
|
|
94
|
+
schemas: {
|
|
95
|
+
Venue: { type: "object", properties: { name: { type: "string" } } },
|
|
96
|
+
Event: {
|
|
97
|
+
type: "object",
|
|
98
|
+
properties: {
|
|
99
|
+
venue: { allOf: [{ $ref: "#/components/schemas/Venue" }] },
|
|
100
|
+
venues: {
|
|
101
|
+
type: "array",
|
|
102
|
+
items: { $ref: "#/components/schemas/Venue" },
|
|
103
|
+
},
|
|
104
|
+
starts_at: {
|
|
105
|
+
type: "string",
|
|
106
|
+
format: "date-time",
|
|
107
|
+
nullable: true,
|
|
108
|
+
},
|
|
109
|
+
price: { type: "string", format: "decimal" },
|
|
110
|
+
metadata: {},
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
const fields = useFormFromSchema(openApiToJsonSchema(document, "Event")!);
|
|
117
|
+
|
|
118
|
+
it("reads relations with their endpoint, and the rest as DRF would", () => {
|
|
119
|
+
expect(types(fields)).toEqual([
|
|
120
|
+
["venue", "fks"],
|
|
121
|
+
["venues", "m2m"],
|
|
122
|
+
["starts_at", "datetime"],
|
|
123
|
+
["price", "number"],
|
|
124
|
+
["metadata", "field:metadata"],
|
|
125
|
+
]);
|
|
126
|
+
expect(attrsOf(fields, "venues")).toMatchObject({
|
|
127
|
+
endpoint: "/api/venues/",
|
|
128
|
+
multiple: true,
|
|
129
|
+
});
|
|
130
|
+
expect(fields[2]!.label).toBe("Starts at");
|
|
131
|
+
});
|
|
132
|
+
});
|
|
@@ -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
|
+
});
|