@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.
- package/dist/__tests__/MapoFormGroup.component.test.d.ts +2 -0
- package/dist/__tests__/MapoFormGroup.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/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/MapoRepeater.vue +3 -1
- package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +12 -1
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.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/types/descriptor.d.ts +27 -3
- 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/repeaterTemplates.d.ts +5 -3
- package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -1
- package/dist/runtime/utils/repeaterTemplates.js +2 -1
- 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__/fieldVisibility.component.test.ts +546 -0
- package/src/__tests__/repeaterTemplates.test.ts +20 -0
- package/src/__tests__/tabVisibility.test.ts +27 -0
- package/src/index.ts +1 -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/MapoRepeater.vue +3 -1
- package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +19 -2
- package/src/runtime/components/fields/MapoRepeaterItem.vue +32 -2
- package/src/runtime/types/descriptor.ts +33 -3
- package/src/runtime/types/index.ts +1 -0
- package/src/runtime/utils/repeaterTemplates.ts +6 -1
- package/src/runtime/utils/tabVisibility.ts +42 -4
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import type { AnyFieldDescriptor, NormalizedRepeaterTemplate, RepeaterDescriptor } from "../types/descriptor.js";
|
|
1
|
+
import type { AnyFieldDescriptor, NormalizedRepeaterTemplate, RepeaterDescriptor, RepeaterImageFit } from "../types/descriptor.js";
|
|
2
2
|
/** Above this many templates, `picker: "auto"` switches from the dropdown to the modal. */
|
|
3
3
|
export declare const MODAL_PICKER_THRESHOLD = 3;
|
|
4
4
|
/**
|
|
5
5
|
* Turns `attrs.templates` into a list with defaults applied. A bare field array
|
|
6
|
-
* becomes a template whose label is its key.
|
|
6
|
+
* becomes a template whose label is its key. `imageFit` falls back to the
|
|
7
|
+
* repeater's `templateImageFit`, then to `contain`, so a preview is never cropped
|
|
8
|
+
* unless someone asks for it.
|
|
7
9
|
*/
|
|
8
|
-
export declare function normalizeTemplates(templates: NonNullable<RepeaterDescriptor["attrs"]>["templates"]): NormalizedRepeaterTemplate[];
|
|
10
|
+
export declare function normalizeTemplates(templates: NonNullable<RepeaterDescriptor["attrs"]>["templates"], defaultImageFit?: RepeaterImageFit): NormalizedRepeaterTemplate[];
|
|
9
11
|
/** Whether the block picker modal replaces the dropdown for this many templates. */
|
|
10
12
|
export declare function shouldUseModalPicker(picker: "auto" | "select" | "modal" | undefined, templateCount: number): boolean;
|
|
11
13
|
/** Filters templates by category and by a case-insensitive text query. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repeaterTemplates.d.ts","sourceRoot":"","sources":["../../../src/runtime/utils/repeaterTemplates.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,0BAA0B,EAC1B,kBAAkB,
|
|
1
|
+
{"version":3,"file":"repeaterTemplates.d.ts","sourceRoot":"","sources":["../../../src/runtime/utils/repeaterTemplates.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,0BAA0B,EAC1B,kBAAkB,EAClB,gBAAgB,EAEjB,MAAM,wBAAwB,CAAC;AAEhC,2FAA2F;AAC3F,eAAO,MAAM,sBAAsB,IAAI,CAAC;AAExC;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,WAAW,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,EAChE,eAAe,GAAE,gBAA4B,GAC5C,0BAA0B,EAAE,CAe9B;AAED,oFAAoF;AACpF,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,MAAM,GAAG,QAAQ,GAAG,OAAO,GAAG,SAAS,EAC/C,aAAa,EAAE,MAAM,GACpB,OAAO,CAOT;AAED,0EAA0E;AAC1E,wBAAgB,eAAe,CAC7B,SAAS,EAAE,0BAA0B,EAAE,EACvC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,GACf,0BAA0B,EAAE,CAS9B;AAED,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,GAAG,OAAO,CAAC;AAEhE,gFAAgF;AAChF,wBAAgB,eAAe,CAC7B,MAAM,EAAE,kBAAkB,EAAE,GAC3B,cAAc,EAAE,CAQlB;AAED,yDAAyD;AACzD,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAEtE"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export const MODAL_PICKER_THRESHOLD = 3;
|
|
2
|
-
export function normalizeTemplates(templates) {
|
|
2
|
+
export function normalizeTemplates(templates, defaultImageFit = "contain") {
|
|
3
3
|
return Object.entries(templates ?? {}).map(([key, tpl]) => {
|
|
4
4
|
const meta = Array.isArray(tpl) ? {} : tpl;
|
|
5
5
|
return {
|
|
@@ -8,6 +8,7 @@ export function normalizeTemplates(templates) {
|
|
|
8
8
|
description: meta.description,
|
|
9
9
|
icon: meta.icon,
|
|
10
10
|
image: meta.image,
|
|
11
|
+
imageFit: meta.imageFit ?? defaultImageFit,
|
|
11
12
|
wireframe: meta.wireframe,
|
|
12
13
|
category: meta.category,
|
|
13
14
|
fields: Array.isArray(tpl) ? tpl : tpl.fields ?? []
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { AnyFieldDescriptor } from "../types/index.js";
|
|
2
2
|
import type { TabEntry } from "../types/layout.js";
|
|
3
|
+
/**
|
|
4
|
+
* Whether the field's `visible` predicate removes it for this model. A removed
|
|
5
|
+
* field is not rendered, and neither is its grid cell.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isFieldRemoved(field: AnyFieldDescriptor, model: Record<string, unknown>): boolean;
|
|
3
8
|
/**
|
|
4
9
|
* Whether a field shows for this model: neither its `visible` predicate, which
|
|
5
10
|
* removes it, nor its `show` predicate, which hides it, keeps it out.
|
|
@@ -12,7 +17,8 @@ export declare function isFieldShown(field: AnyFieldDescriptor, model: Record<st
|
|
|
12
17
|
export declare function tabHasShownFields(tab: TabEntry, model: Record<string, unknown>): boolean;
|
|
13
18
|
/**
|
|
14
19
|
* The tab to open among the ones shown: the tab the user picked, while it is
|
|
15
|
-
* shown; otherwise the first one. Empty when no tab is shown.
|
|
20
|
+
* shown; otherwise the first one. Empty when no tab is shown. A group's
|
|
21
|
+
* sub-tabs are picked the same way.
|
|
16
22
|
*/
|
|
17
|
-
export declare function pickActiveTab(shownTabs: TabEntry[], picked: string | null): string;
|
|
23
|
+
export declare function pickActiveTab(shownTabs: Pick<TabEntry, "name">[], picked: string | null): string;
|
|
18
24
|
//# sourceMappingURL=tabVisibility.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tabVisibility.d.ts","sourceRoot":"","sources":["../../../src/runtime/utils/tabVisibility.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"tabVisibility.d.ts","sourceRoot":"","sources":["../../../src/runtime/utils/tabVisibility.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AA4BnD;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,kBAAkB,EACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAET;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,kBAAkB,EACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAIT;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,QAAQ,EACb,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAQT;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE,EACnC,MAAM,EAAE,MAAM,GAAG,IAAI,GACpB,MAAM,CAGR"}
|
|
@@ -1,6 +1,24 @@
|
|
|
1
|
+
function passesPredicate(field, name, model) {
|
|
2
|
+
const predicate = field[name];
|
|
3
|
+
if (!predicate) return true;
|
|
4
|
+
try {
|
|
5
|
+
return Boolean(predicate({ model }));
|
|
6
|
+
} catch (error) {
|
|
7
|
+
if (import.meta.dev) {
|
|
8
|
+
console.warn(
|
|
9
|
+
`[MapoForm] The \`${name}\` predicate of field "${String(field.key)}" threw, so the field is hidden.`,
|
|
10
|
+
error
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function isFieldRemoved(field, model) {
|
|
17
|
+
return !passesPredicate(field, "visible", model);
|
|
18
|
+
}
|
|
1
19
|
export function isFieldShown(field, model) {
|
|
2
|
-
const removed = field
|
|
3
|
-
const hidden = field
|
|
20
|
+
const removed = isFieldRemoved(field, model);
|
|
21
|
+
const hidden = !passesPredicate(field, "show", model);
|
|
4
22
|
return !removed && !hidden;
|
|
5
23
|
}
|
|
6
24
|
export function tabHasShownFields(tab, model) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapomodule/form",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.15",
|
|
4
4
|
"description": "Mapo declarative typed form engine and field components",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@internationalized/date": "^3.7.0",
|
|
50
|
-
"@mapomodule/i18n": "2.0.0-beta.
|
|
50
|
+
"@mapomodule/i18n": "2.0.0-beta.6",
|
|
51
51
|
"@mapomodule/store": "2.0.0-beta.6",
|
|
52
52
|
"@mapomodule/utils": "2.0.0-beta.7",
|
|
53
53
|
"@tiptap/extension-image": "^2.11.5",
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createRenderer, h } from "vue";
|
|
3
|
+
import type { App, Component } from "vue";
|
|
4
|
+
|
|
5
|
+
import MapoFormGroup from "../runtime/components/MapoFormGroup.vue";
|
|
6
|
+
|
|
7
|
+
const runtimeConfig = vi.hoisted(() => ({
|
|
8
|
+
public: { mapoForm: { groups: { expanded: true } } },
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
// Keys of the fields the group renders: its body renders them only while the
|
|
12
|
+
// group is expanded.
|
|
13
|
+
const renderedFields = vi.hoisted(() => [] as string[]);
|
|
14
|
+
|
|
15
|
+
vi.mock("#app", () => ({ useRuntimeConfig: () => runtimeConfig }));
|
|
16
|
+
vi.mock("../runtime/composables/useMapoForm.js", () => ({
|
|
17
|
+
injectMapoForm: () => undefined,
|
|
18
|
+
}));
|
|
19
|
+
vi.mock("../runtime/components/MapoFormField.vue", () => ({
|
|
20
|
+
default: {
|
|
21
|
+
name: "MapoFormField",
|
|
22
|
+
props: { descriptor: { type: Object, required: true } },
|
|
23
|
+
setup(props: { descriptor: { key: string } }) {
|
|
24
|
+
renderedFields.push(props.descriptor.key);
|
|
25
|
+
return () => null;
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
// Tests run in Node without a DOM. A renderer whose nodes are plain objects is
|
|
31
|
+
// enough to mount the component; `style` takes what `v-show` writes, and
|
|
32
|
+
// `querySelector` gives the field-expand `Teleport` a target.
|
|
33
|
+
interface HeadlessNode {
|
|
34
|
+
parent: HeadlessNode | null;
|
|
35
|
+
children: HeadlessNode[];
|
|
36
|
+
style: Record<string, string>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const headlessNode = (): HeadlessNode => ({
|
|
40
|
+
parent: null,
|
|
41
|
+
children: [],
|
|
42
|
+
style: {},
|
|
43
|
+
});
|
|
44
|
+
const body = headlessNode();
|
|
45
|
+
|
|
46
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
47
|
+
createElement: headlessNode,
|
|
48
|
+
createText: headlessNode,
|
|
49
|
+
createComment: headlessNode,
|
|
50
|
+
setText: () => {},
|
|
51
|
+
setElementText: () => {},
|
|
52
|
+
patchProp: () => {},
|
|
53
|
+
insert(child, parent, anchor) {
|
|
54
|
+
child.parent = parent;
|
|
55
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
56
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
57
|
+
},
|
|
58
|
+
remove(child) {
|
|
59
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
60
|
+
child.parent = null;
|
|
61
|
+
},
|
|
62
|
+
parentNode: (node) => node.parent,
|
|
63
|
+
nextSibling: (node) =>
|
|
64
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
65
|
+
querySelector: () => body,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const UIcon: Component = { name: "UIcon", render: () => null };
|
|
69
|
+
|
|
70
|
+
let app: App<HeadlessNode> | undefined;
|
|
71
|
+
|
|
72
|
+
/** Mounts a group with one field; `initialExpanded` is passed only when given. */
|
|
73
|
+
function mountGroup(props: { initialExpanded?: boolean } = {}) {
|
|
74
|
+
const root: Component = {
|
|
75
|
+
render: () =>
|
|
76
|
+
h(MapoFormGroup as Component, {
|
|
77
|
+
name: "seo",
|
|
78
|
+
fields: [{ key: "meta_title", type: "text" }],
|
|
79
|
+
...props,
|
|
80
|
+
}),
|
|
81
|
+
};
|
|
82
|
+
app = createApp(root);
|
|
83
|
+
app.component("UIcon", UIcon);
|
|
84
|
+
app.mount(headlessNode());
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
describe("MapoFormGroup initial state", () => {
|
|
88
|
+
beforeEach(() => {
|
|
89
|
+
renderedFields.length = 0;
|
|
90
|
+
// The field-expand listeners touch `document`, which Node does not have.
|
|
91
|
+
vi.stubGlobal("document", {
|
|
92
|
+
addEventListener: vi.fn(),
|
|
93
|
+
removeEventListener: vi.fn(),
|
|
94
|
+
body: { style: {} },
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
afterEach(() => {
|
|
99
|
+
app?.unmount();
|
|
100
|
+
app = undefined;
|
|
101
|
+
vi.unstubAllGlobals();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("starts expanded when the prop is absent and groups.expanded is true", () => {
|
|
105
|
+
runtimeConfig.public.mapoForm.groups.expanded = true;
|
|
106
|
+
mountGroup();
|
|
107
|
+
|
|
108
|
+
expect(renderedFields).toEqual(["meta_title"]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("starts collapsed when the prop is absent and groups.expanded is false", () => {
|
|
112
|
+
runtimeConfig.public.mapoForm.groups.expanded = false;
|
|
113
|
+
mountGroup();
|
|
114
|
+
|
|
115
|
+
expect(renderedFields).toEqual([]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it.each([
|
|
119
|
+
{ initialExpanded: false, configExpanded: true, fields: [] },
|
|
120
|
+
{ initialExpanded: true, configExpanded: false, fields: ["meta_title"] },
|
|
121
|
+
])(
|
|
122
|
+
"an explicit initialExpanded $initialExpanded wins over groups.expanded $configExpanded",
|
|
123
|
+
({ initialExpanded, configExpanded, fields }) => {
|
|
124
|
+
runtimeConfig.public.mapoForm.groups.expanded = configExpanded;
|
|
125
|
+
mountGroup({ initialExpanded });
|
|
126
|
+
|
|
127
|
+
expect(renderedFields).toEqual(fields);
|
|
128
|
+
},
|
|
129
|
+
);
|
|
130
|
+
});
|