@mapomodule/form 2.0.0-beta.8 → 2.0.0-experimental.1
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 +173 -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__/article.schema.json +193 -0
- package/src/__tests__/camomillaRoundTrip.test.ts +73 -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 +306 -42
- 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__/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
|
@@ -10,6 +10,8 @@ type DescriptorWithAttrs = {
|
|
|
10
10
|
};
|
|
11
11
|
fields?: unknown[];
|
|
12
12
|
visible?: (ctx: { model: Record<string, unknown> }) => boolean;
|
|
13
|
+
readonly?: boolean;
|
|
14
|
+
translatable?: boolean;
|
|
13
15
|
};
|
|
14
16
|
|
|
15
17
|
// Pydantic v2 / DRF Spectacular style schema.
|
|
@@ -413,3 +415,388 @@ describe("useFormFromSchema — number attrs", () => {
|
|
|
413
415
|
expect(typed?.attrs?.max).toBe(5);
|
|
414
416
|
});
|
|
415
417
|
});
|
|
418
|
+
|
|
419
|
+
describe("useFormFromSchema — schema flags", () => {
|
|
420
|
+
// Camomilla's OPTIONS schema marks per-language properties. Dropping the flag
|
|
421
|
+
// renders them as plain fields under language tabs that no longer bind them.
|
|
422
|
+
const flagged: JSONSchema = {
|
|
423
|
+
type: "object",
|
|
424
|
+
properties: {
|
|
425
|
+
title: { type: "string", title: "Titolo", translatable: true },
|
|
426
|
+
identifier: { type: "string", title: "Identifier" },
|
|
427
|
+
created_at: { type: "string", format: "date-time", readOnly: true },
|
|
428
|
+
},
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
it("carries translatable and readOnly onto the descriptor", () => {
|
|
432
|
+
const fields = useFormFromSchema(flagged);
|
|
433
|
+
const by = (k: string) =>
|
|
434
|
+
fields.find((f) => f.key === k) as DescriptorWithAttrs | undefined;
|
|
435
|
+
expect(by("title")?.translatable).toBe(true);
|
|
436
|
+
expect(by("identifier")?.translatable).toBeUndefined();
|
|
437
|
+
expect(by("created_at")?.readonly).toBe(true);
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
/** The descriptor of a one-property schema. */
|
|
442
|
+
function fieldFor(property: JSONSchema, defs?: Record<string, JSONSchema>) {
|
|
443
|
+
const [field] = useFormFromSchema({
|
|
444
|
+
type: "object",
|
|
445
|
+
properties: { value: property },
|
|
446
|
+
...(defs ? { $defs: defs } : {}),
|
|
447
|
+
});
|
|
448
|
+
return field as DescriptorWithAttrs & {
|
|
449
|
+
type: string;
|
|
450
|
+
noDraft?: boolean;
|
|
451
|
+
attrs?: Record<string, unknown>;
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
describe("useFormFromSchema — standard keywords", () => {
|
|
456
|
+
it.each([
|
|
457
|
+
[{ type: "string", format: "email" }, "email"],
|
|
458
|
+
[{ type: "string", format: "uri" }, "url"],
|
|
459
|
+
[{ type: "string", format: "binary" }, "file"],
|
|
460
|
+
[{ type: "string", contentMediaType: "text/html" }, "editor"],
|
|
461
|
+
])("maps %j to %s", (property, type) => {
|
|
462
|
+
expect(fieldFor(property).type).toBe(type);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
it("keeps a writeOnly value, such as a password, out of drafts", () => {
|
|
466
|
+
expect(fieldFor({ type: "string", writeOnly: true }).noDraft).toBe(true);
|
|
467
|
+
expect(fieldFor({ type: "string" }).noDraft).toBeUndefined();
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it("reads choices with labels from oneOf or anyOf of const and title", () => {
|
|
471
|
+
const labeled = fieldFor({
|
|
472
|
+
type: "string",
|
|
473
|
+
oneOf: [
|
|
474
|
+
{ const: "draft", title: "Draft" },
|
|
475
|
+
{ const: "published", title: "Published" },
|
|
476
|
+
],
|
|
477
|
+
});
|
|
478
|
+
expect(labeled.type).toBe("select");
|
|
479
|
+
expect(labeled.attrs?.options).toEqual([
|
|
480
|
+
{ text: "Draft", value: "draft" },
|
|
481
|
+
{ text: "Published", value: "published" },
|
|
482
|
+
]);
|
|
483
|
+
|
|
484
|
+
const nullable = fieldFor({
|
|
485
|
+
anyOf: [{ const: 1, title: "Low" }, { const: 2 }, { type: "null" }],
|
|
486
|
+
});
|
|
487
|
+
expect(nullable.attrs?.options).toEqual([
|
|
488
|
+
{ text: "Low", value: 1 },
|
|
489
|
+
{ text: "2", value: 2 },
|
|
490
|
+
]);
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
it("reads labeled choices behind a $ref", () => {
|
|
494
|
+
const field = fieldFor(
|
|
495
|
+
{ $ref: "#/$defs/Status" },
|
|
496
|
+
{
|
|
497
|
+
Status: { oneOf: [{ const: "draft", title: "Draft" }] },
|
|
498
|
+
},
|
|
499
|
+
);
|
|
500
|
+
expect(field.attrs?.options).toEqual([{ text: "Draft", value: "draft" }]);
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
it("maps an array of choices to a multiple select with options", () => {
|
|
504
|
+
const field = fieldFor({
|
|
505
|
+
type: "array",
|
|
506
|
+
items: { type: "string", enum: ["art", "music"] },
|
|
507
|
+
});
|
|
508
|
+
expect(field.type).toBe("select");
|
|
509
|
+
expect(field.attrs).toEqual({
|
|
510
|
+
options: [
|
|
511
|
+
{ text: "art", value: "art" },
|
|
512
|
+
{ text: "music", value: "music" },
|
|
513
|
+
],
|
|
514
|
+
multiple: true,
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
it("maps an array of plain values to a multiple select", () => {
|
|
519
|
+
const field = fieldFor({ type: "array", items: { type: "string" } });
|
|
520
|
+
expect(field.type).toBe("select");
|
|
521
|
+
expect(field.attrs).toEqual({ multiple: true });
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
it("maps a list of models behind a $ref to a repeater", () => {
|
|
525
|
+
const field = fieldFor(
|
|
526
|
+
{ type: "array", items: { $ref: "#/$defs/Block" } },
|
|
527
|
+
{ Block: { type: "object", properties: { title: { type: "string" } } } },
|
|
528
|
+
);
|
|
529
|
+
expect(field.type).toBe("repeater");
|
|
530
|
+
expect((field.fields as Array<{ key: string }>).map((f) => f.key)).toEqual([
|
|
531
|
+
"title",
|
|
532
|
+
]);
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
it("reads minItems and maxItems as the repeater limits", () => {
|
|
536
|
+
const field = fieldFor({
|
|
537
|
+
type: "array",
|
|
538
|
+
items: { type: "object", properties: {} },
|
|
539
|
+
minItems: 1,
|
|
540
|
+
maxItems: 5,
|
|
541
|
+
});
|
|
542
|
+
expect(field.attrs).toEqual({ minItems: 1, maxItems: 5 });
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
it("maps items told apart by _template to repeater templates", () => {
|
|
546
|
+
const field = fieldFor({
|
|
547
|
+
type: "array",
|
|
548
|
+
items: {
|
|
549
|
+
discriminator: { propertyName: "_template" },
|
|
550
|
+
oneOf: [
|
|
551
|
+
{
|
|
552
|
+
type: "object",
|
|
553
|
+
title: "Hero",
|
|
554
|
+
properties: {
|
|
555
|
+
_template: { const: "hero" },
|
|
556
|
+
title: { type: "string" },
|
|
557
|
+
},
|
|
558
|
+
required: ["_template", "title"],
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
type: "object",
|
|
562
|
+
properties: {
|
|
563
|
+
_template: { const: "quote" },
|
|
564
|
+
text: { type: "string" },
|
|
565
|
+
},
|
|
566
|
+
},
|
|
567
|
+
// No const: no item can pick it.
|
|
568
|
+
{ type: "object", properties: { other: { type: "string" } } },
|
|
569
|
+
],
|
|
570
|
+
},
|
|
571
|
+
});
|
|
572
|
+
expect(field.type).toBe("repeater");
|
|
573
|
+
expect(field.fields).toBeUndefined();
|
|
574
|
+
const templates = field.attrs?.templates as Record<string, unknown>;
|
|
575
|
+
expect(Object.keys(templates)).toEqual(["hero", "quote"]);
|
|
576
|
+
expect(templates.hero).toMatchObject({
|
|
577
|
+
label: "Hero",
|
|
578
|
+
fields: [{ key: "title", type: "text", required: true }],
|
|
579
|
+
});
|
|
580
|
+
expect(templates.quote).toMatchObject([{ key: "text", type: "text" }]);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it("leaves items told apart by another property to a select, as before", () => {
|
|
584
|
+
const field = fieldFor({
|
|
585
|
+
type: "array",
|
|
586
|
+
items: {
|
|
587
|
+
discriminator: { propertyName: "kind" },
|
|
588
|
+
oneOf: [{ type: "object", properties: { kind: { const: "a" } } }],
|
|
589
|
+
},
|
|
590
|
+
});
|
|
591
|
+
expect(field.type).toBe("select");
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
it("recognises an SEO object stored with permalink", () => {
|
|
595
|
+
const field = fieldFor({
|
|
596
|
+
type: "object",
|
|
597
|
+
properties: {
|
|
598
|
+
title: { type: "string" },
|
|
599
|
+
description: { type: "string" },
|
|
600
|
+
permalink: { type: "string" },
|
|
601
|
+
},
|
|
602
|
+
});
|
|
603
|
+
expect(field.type).toBe("seo");
|
|
604
|
+
});
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
describe("useFormFromSchema — Mapo keywords", () => {
|
|
608
|
+
it("takes the type from x-mapo-field before any other keyword", () => {
|
|
609
|
+
expect(fieldFor({ type: "boolean", "x-mapo-field": "switch" }).type).toBe(
|
|
610
|
+
"switch",
|
|
611
|
+
);
|
|
612
|
+
expect(fieldFor({ enum: ["a"], "x-mapo-field": "radio" }).type).toBe(
|
|
613
|
+
"radio",
|
|
614
|
+
);
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
it("reads the keywords of the marked type", () => {
|
|
618
|
+
const field = fieldFor({
|
|
619
|
+
type: "number",
|
|
620
|
+
minimum: 1,
|
|
621
|
+
maximum: 5,
|
|
622
|
+
"x-mapo-field": "slider",
|
|
623
|
+
});
|
|
624
|
+
expect(field.attrs).toEqual({ min: 1, max: 5 });
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
it("merges x-mapo-attrs over the attrs read from the keywords", () => {
|
|
628
|
+
const field = fieldFor({
|
|
629
|
+
type: "relation",
|
|
630
|
+
endpoint: "/api/users/",
|
|
631
|
+
"x-mapo-attrs": { returnObject: false, itemText: "username" },
|
|
632
|
+
});
|
|
633
|
+
expect(field.attrs).toEqual({
|
|
634
|
+
endpoint: "/api/users/",
|
|
635
|
+
returnObject: false,
|
|
636
|
+
itemText: "username",
|
|
637
|
+
});
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
it("keeps a single relation with multiple values when marked fks", () => {
|
|
641
|
+
const field = fieldFor({
|
|
642
|
+
type: "relation",
|
|
643
|
+
endpoint: "/api/users/",
|
|
644
|
+
multiple: true,
|
|
645
|
+
"x-mapo-field": "fks",
|
|
646
|
+
});
|
|
647
|
+
expect(field.type).toBe("fks");
|
|
648
|
+
expect(field.attrs?.multiple).toBe(true);
|
|
649
|
+
});
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
describe("useFormFromSchema — schemas that point back at themselves", () => {
|
|
653
|
+
type Tree = { key: string; type: string; fields?: Tree[] };
|
|
654
|
+
const shape = (field: Tree): unknown =>
|
|
655
|
+
field.fields
|
|
656
|
+
? { [field.key]: field.fields.map(shape) }
|
|
657
|
+
: `${field.key}:${field.type}`;
|
|
658
|
+
|
|
659
|
+
it("expands a model that contains itself once", () => {
|
|
660
|
+
const fields = useFormFromSchema({
|
|
661
|
+
type: "object",
|
|
662
|
+
properties: { menu: { type: "array", items: { $ref: "#/$defs/Item" } } },
|
|
663
|
+
$defs: {
|
|
664
|
+
Item: {
|
|
665
|
+
type: "object",
|
|
666
|
+
properties: {
|
|
667
|
+
label: { type: "string" },
|
|
668
|
+
children: { type: "array", items: { $ref: "#/$defs/Item" } },
|
|
669
|
+
},
|
|
670
|
+
},
|
|
671
|
+
},
|
|
672
|
+
});
|
|
673
|
+
expect(fields.map((f) => shape(f as Tree))).toEqual([
|
|
674
|
+
{ menu: ["label:text", { children: [] }] },
|
|
675
|
+
]);
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
it("stops two models that list each other", () => {
|
|
679
|
+
const fields = useFormFromSchema({
|
|
680
|
+
type: "object",
|
|
681
|
+
properties: { authors: { type: "array", items: { $ref: "#/$defs/A" } } },
|
|
682
|
+
$defs: {
|
|
683
|
+
A: {
|
|
684
|
+
type: "object",
|
|
685
|
+
properties: {
|
|
686
|
+
books: { type: "array", items: { $ref: "#/$defs/B" } },
|
|
687
|
+
},
|
|
688
|
+
},
|
|
689
|
+
B: {
|
|
690
|
+
type: "object",
|
|
691
|
+
properties: {
|
|
692
|
+
authors: { type: "array", items: { $ref: "#/$defs/A" } },
|
|
693
|
+
},
|
|
694
|
+
},
|
|
695
|
+
},
|
|
696
|
+
});
|
|
697
|
+
expect(fields.map((f) => shape(f as Tree))).toEqual([
|
|
698
|
+
{ authors: [{ books: [{ authors: [] }] }] },
|
|
699
|
+
]);
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
it("stops a nullable list of itself, as Pydantic writes Optional[list[Self]]", () => {
|
|
703
|
+
const nullableList = {
|
|
704
|
+
anyOf: [
|
|
705
|
+
{ type: "array", items: { $ref: "#/$defs/Node" } },
|
|
706
|
+
{ type: "null" },
|
|
707
|
+
],
|
|
708
|
+
};
|
|
709
|
+
const fields = useFormFromSchema({
|
|
710
|
+
type: "object",
|
|
711
|
+
properties: { nodes: nullableList },
|
|
712
|
+
$defs: {
|
|
713
|
+
Node: {
|
|
714
|
+
type: "object",
|
|
715
|
+
properties: {
|
|
716
|
+
name: { type: "string" },
|
|
717
|
+
children: nullableList,
|
|
718
|
+
},
|
|
719
|
+
},
|
|
720
|
+
},
|
|
721
|
+
});
|
|
722
|
+
expect(fields.map((f) => shape(f as Tree))).toEqual([
|
|
723
|
+
{ nodes: ["name:text", { children: [] }] },
|
|
724
|
+
]);
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
it("stops templates whose blocks hold the same templates", () => {
|
|
728
|
+
const fields = useFormFromSchema({
|
|
729
|
+
type: "object",
|
|
730
|
+
properties: {
|
|
731
|
+
blocks: { type: "array", items: { $ref: "#/$defs/Block" } },
|
|
732
|
+
},
|
|
733
|
+
$defs: {
|
|
734
|
+
Block: {
|
|
735
|
+
discriminator: { propertyName: "_template" },
|
|
736
|
+
oneOf: [{ $ref: "#/$defs/Section" }],
|
|
737
|
+
},
|
|
738
|
+
Section: {
|
|
739
|
+
type: "object",
|
|
740
|
+
properties: {
|
|
741
|
+
_template: { const: "section" },
|
|
742
|
+
blocks: { type: "array", items: { $ref: "#/$defs/Block" } },
|
|
743
|
+
},
|
|
744
|
+
},
|
|
745
|
+
},
|
|
746
|
+
});
|
|
747
|
+
const templates = (
|
|
748
|
+
fields[0] as DescriptorWithAttrs & {
|
|
749
|
+
attrs: { templates: Record<string, Tree[]> };
|
|
750
|
+
}
|
|
751
|
+
).attrs.templates;
|
|
752
|
+
expect(templates.section).toEqual([
|
|
753
|
+
expect.objectContaining({ key: "blocks", type: "repeater", fields: [] }),
|
|
754
|
+
]);
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
it("reads boolean schemas instead of throwing", () => {
|
|
758
|
+
const [tags, anything] = useFormFromSchema({
|
|
759
|
+
type: "object",
|
|
760
|
+
properties: {
|
|
761
|
+
tags: { type: "array", items: true as unknown as JSONSchema },
|
|
762
|
+
anything: true as unknown as JSONSchema,
|
|
763
|
+
},
|
|
764
|
+
});
|
|
765
|
+
expect(tags?.type).toBe("select");
|
|
766
|
+
expect(anything?.type).toBe("text");
|
|
767
|
+
});
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
describe("useFormFromSchema — attrs a schema cannot set", () => {
|
|
771
|
+
it("drops event handlers and DOM properties from x-mapo-attrs", () => {
|
|
772
|
+
const field = fieldFor({
|
|
773
|
+
type: "string",
|
|
774
|
+
"x-mapo-attrs": {
|
|
775
|
+
placeholder: "Title",
|
|
776
|
+
onfocus: "alert(1)",
|
|
777
|
+
onClick: "alert(1)",
|
|
778
|
+
innerHTML: "<img src=x onerror=alert(1)>",
|
|
779
|
+
srcdoc: "<script></script>",
|
|
780
|
+
},
|
|
781
|
+
});
|
|
782
|
+
expect(field.attrs).toEqual({ placeholder: "Title" });
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
it("restores the other properties of each choice, without the unsafe ones", () => {
|
|
786
|
+
const field = fieldFor({
|
|
787
|
+
type: "string",
|
|
788
|
+
oneOf: [
|
|
789
|
+
{
|
|
790
|
+
const: "draft",
|
|
791
|
+
title: "Draft",
|
|
792
|
+
"x-mapo-attrs": { icon: "i-lucide-pencil", onSelect: "alert(1)" },
|
|
793
|
+
},
|
|
794
|
+
{ const: "published", title: "Published" },
|
|
795
|
+
],
|
|
796
|
+
});
|
|
797
|
+
expect(field.attrs?.options).toEqual([
|
|
798
|
+
{ icon: "i-lucide-pencil", text: "Draft", value: "draft" },
|
|
799
|
+
{ text: "Published", value: "published" },
|
|
800
|
+
]);
|
|
801
|
+
});
|
|
802
|
+
});
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
-
import { ref } from "vue";
|
|
2
|
+
import { nextTick, ref } from "vue";
|
|
3
3
|
import { useMapoForm } from "../runtime/composables/useMapoForm.js";
|
|
4
4
|
import { defaultRegistry } from "../runtime/registry/defaults.js";
|
|
5
5
|
import type { FieldDescriptor } from "../runtime/types/descriptor.js";
|
|
6
6
|
|
|
7
|
+
const snackShow = vi.hoisted(() => vi.fn());
|
|
7
8
|
vi.mock("@mapomodule/store/runtime/stores/auth", () => ({
|
|
8
9
|
useAuthStore: () => ({ info: { id: 1, username: "testuser" } }),
|
|
9
10
|
}));
|
|
10
11
|
vi.mock("@mapomodule/store/runtime/stores/snack", () => ({
|
|
11
|
-
useSnackStore: () => ({ show:
|
|
12
|
+
useSnackStore: () => ({ show: snackShow }),
|
|
12
13
|
}));
|
|
13
14
|
// Pulls in `nuxt/app`, which cannot resolve outside a Nuxt build. Assertions
|
|
14
15
|
// below match on message keys, so echoing the key back is the useful stub.
|
|
@@ -18,6 +19,7 @@ vi.mock("@mapomodule/i18n/runtime/composables/useMapoT", () => ({
|
|
|
18
19
|
|
|
19
20
|
// provide/inject called outside setup() emits a warning; silence it in tests.
|
|
20
21
|
beforeEach(() => {
|
|
22
|
+
snackShow.mockClear();
|
|
21
23
|
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
22
24
|
});
|
|
23
25
|
|
|
@@ -109,6 +111,64 @@ describe("getFieldValue", () => {
|
|
|
109
111
|
const { form } = makeForm({ title: "Existing" });
|
|
110
112
|
expect(form.getFieldValue({ key: "title", type: "text" })).toBe("Existing");
|
|
111
113
|
});
|
|
114
|
+
|
|
115
|
+
describe("server error notifications", () => {
|
|
116
|
+
it("uses the field label and falls back to the field key", async () => {
|
|
117
|
+
const model = ref<Article>({
|
|
118
|
+
title: "",
|
|
119
|
+
slug: "",
|
|
120
|
+
is_draft: false,
|
|
121
|
+
count: 0,
|
|
122
|
+
translations: {},
|
|
123
|
+
});
|
|
124
|
+
const fields = ref<FieldDescriptor<Article>[]>([
|
|
125
|
+
{ key: "title", type: "text", label: "Article title" },
|
|
126
|
+
]);
|
|
127
|
+
const errors = ref<Record<string, string[]>>({});
|
|
128
|
+
useMapoForm({
|
|
129
|
+
model,
|
|
130
|
+
fields,
|
|
131
|
+
errors,
|
|
132
|
+
registry: defaultRegistry,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
errors.value = {
|
|
136
|
+
title: ["This field is required."],
|
|
137
|
+
unknown: ["Invalid value."],
|
|
138
|
+
};
|
|
139
|
+
await nextTick();
|
|
140
|
+
|
|
141
|
+
expect(snackShow).toHaveBeenCalledWith(
|
|
142
|
+
"Article title: This field is required.\nunknown: Invalid value.",
|
|
143
|
+
"error",
|
|
144
|
+
);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("suppresses duplicate server error notifications when disabled", async () => {
|
|
148
|
+
const model = ref<Article>({
|
|
149
|
+
title: "",
|
|
150
|
+
slug: "",
|
|
151
|
+
is_draft: false,
|
|
152
|
+
count: 0,
|
|
153
|
+
translations: {},
|
|
154
|
+
});
|
|
155
|
+
const errors = ref<Record<string, string[]>>({});
|
|
156
|
+
useMapoForm({
|
|
157
|
+
model,
|
|
158
|
+
fields: ref<FieldDescriptor<Article>[]>([
|
|
159
|
+
{ key: "title", type: "text", label: "Article title" },
|
|
160
|
+
]),
|
|
161
|
+
errors,
|
|
162
|
+
notifyErrors: false,
|
|
163
|
+
registry: defaultRegistry,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
errors.value = { title: ["This field is required."] };
|
|
167
|
+
await nextTick();
|
|
168
|
+
|
|
169
|
+
expect(snackShow).not.toHaveBeenCalled();
|
|
170
|
+
});
|
|
171
|
+
});
|
|
112
172
|
});
|
|
113
173
|
|
|
114
174
|
// ─── getPatch ────────────────────────────────────────────────────────────────
|
|
@@ -322,3 +382,154 @@ describe("submit", () => {
|
|
|
322
382
|
expect(calls[1]).toHaveProperty("slug", "new-slug"); // full model
|
|
323
383
|
});
|
|
324
384
|
});
|
|
385
|
+
|
|
386
|
+
// ─── draft ────────────────────────────────────────────────────────────────────
|
|
387
|
+
|
|
388
|
+
describe("draft", () => {
|
|
389
|
+
it("keeps a noDraft field out of the draft in every language and under a dotted key", async () => {
|
|
390
|
+
vi.useFakeTimers();
|
|
391
|
+
const stored = new Map<string, string>();
|
|
392
|
+
vi.stubGlobal("localStorage", {
|
|
393
|
+
getItem: (key: string) => stored.get(key) ?? null,
|
|
394
|
+
setItem: (key: string, value: string) => void stored.set(key, value),
|
|
395
|
+
removeItem: (key: string) => void stored.delete(key),
|
|
396
|
+
});
|
|
397
|
+
try {
|
|
398
|
+
const model = ref<Record<string, unknown>>({ translations: {} });
|
|
399
|
+
const fields = ref<FieldDescriptor[]>([
|
|
400
|
+
{ key: "title", type: "text" as const, translatable: true },
|
|
401
|
+
{
|
|
402
|
+
key: "token",
|
|
403
|
+
type: "text" as const,
|
|
404
|
+
translatable: true,
|
|
405
|
+
noDraft: true,
|
|
406
|
+
},
|
|
407
|
+
{ key: "api.key", type: "text" as const, noDraft: true },
|
|
408
|
+
]);
|
|
409
|
+
const form = useMapoForm({
|
|
410
|
+
model,
|
|
411
|
+
fields,
|
|
412
|
+
registry: defaultRegistry,
|
|
413
|
+
currentLang: ref("en"),
|
|
414
|
+
languages: ["en", "it"],
|
|
415
|
+
draftKey: "article:new",
|
|
416
|
+
});
|
|
417
|
+
form.setFieldValue("title", "Hello");
|
|
418
|
+
form.setFieldValue("token", "t-en");
|
|
419
|
+
form.setFieldValue("api.key", "k-123");
|
|
420
|
+
await nextTick();
|
|
421
|
+
vi.advanceTimersByTime(2000);
|
|
422
|
+
const saved = JSON.parse(stored.get("mapo:draft:1:article:new") ?? "{}");
|
|
423
|
+
expect(saved.model).toEqual({
|
|
424
|
+
translations: { en: { title: "Hello" } },
|
|
425
|
+
api: {},
|
|
426
|
+
});
|
|
427
|
+
} finally {
|
|
428
|
+
vi.unstubAllGlobals();
|
|
429
|
+
vi.useRealTimers();
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
// ─── revealField ─────────────────────────────────────────────────────────────
|
|
435
|
+
|
|
436
|
+
describe("revealField", () => {
|
|
437
|
+
interface Page {
|
|
438
|
+
title: string;
|
|
439
|
+
subtitle: string;
|
|
440
|
+
blocks: { title: string }[];
|
|
441
|
+
translations: Record<string, Partial<Page>>;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function makePage(lang = "") {
|
|
445
|
+
const model = ref<Page>({
|
|
446
|
+
title: "",
|
|
447
|
+
subtitle: "",
|
|
448
|
+
blocks: [{ title: "" }],
|
|
449
|
+
translations: {},
|
|
450
|
+
});
|
|
451
|
+
const fields = ref<FieldDescriptor<Page>[]>([
|
|
452
|
+
{ key: "title", type: "text" as const, translatable: true },
|
|
453
|
+
{
|
|
454
|
+
key: "subtitle",
|
|
455
|
+
type: "text" as const,
|
|
456
|
+
show: ({ model: page }) => page.title !== "",
|
|
457
|
+
},
|
|
458
|
+
{ key: "blocks", type: "repeater" as const },
|
|
459
|
+
]);
|
|
460
|
+
const form = useMapoForm({
|
|
461
|
+
model,
|
|
462
|
+
fields,
|
|
463
|
+
registry: defaultRegistry,
|
|
464
|
+
currentLang: ref(lang),
|
|
465
|
+
});
|
|
466
|
+
return { model, form };
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
it("asks the form's parts for a field, by its key and the rest of the path", () => {
|
|
470
|
+
const { form } = makePage();
|
|
471
|
+
|
|
472
|
+
void form.revealField("blocks.0.title");
|
|
473
|
+
|
|
474
|
+
expect(form.ctx.reveal?.value).toMatchObject({
|
|
475
|
+
path: "blocks.0.title",
|
|
476
|
+
key: "blocks",
|
|
477
|
+
rest: "0.title",
|
|
478
|
+
});
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it("names a translatable field by the path of its value or its bare key", () => {
|
|
482
|
+
const { form } = makePage("it");
|
|
483
|
+
|
|
484
|
+
void form.revealField("translations.it.title");
|
|
485
|
+
expect(form.ctx.reveal?.value).toMatchObject({ key: "title", rest: "" });
|
|
486
|
+
|
|
487
|
+
void form.revealField("title");
|
|
488
|
+
expect(form.ctx.reveal?.value).toMatchObject({ key: "title", rest: "" });
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it("answers true once a field took the focus, and ends the request", async () => {
|
|
492
|
+
const { form } = makePage();
|
|
493
|
+
|
|
494
|
+
const revealed = form.revealField("title");
|
|
495
|
+
form.ctx.reveal?.value?.settle(true);
|
|
496
|
+
|
|
497
|
+
await expect(revealed).resolves.toBe(true);
|
|
498
|
+
expect(form.ctx.reveal?.value).toBeUndefined();
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
it("answers false at once for a field it does not have or does not show", async () => {
|
|
502
|
+
const { form } = makePage();
|
|
503
|
+
|
|
504
|
+
await expect(form.revealField("summary")).resolves.toBe(false);
|
|
505
|
+
await expect(form.revealField("subtitle")).resolves.toBe(false);
|
|
506
|
+
expect(form.ctx.reveal?.value).toBeUndefined();
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it("ends a request under way with false when another one starts", async () => {
|
|
510
|
+
const { form } = makePage();
|
|
511
|
+
|
|
512
|
+
const first = form.revealField("title");
|
|
513
|
+
const second = form.revealField("blocks.0.title");
|
|
514
|
+
|
|
515
|
+
await expect(first).resolves.toBe(false);
|
|
516
|
+
expect(form.ctx.reveal?.value).toMatchObject({ key: "blocks" });
|
|
517
|
+
form.ctx.reveal?.value?.settle(true);
|
|
518
|
+
await expect(second).resolves.toBe(true);
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it("gives up with false when no field focuses its control in time", async () => {
|
|
522
|
+
vi.useFakeTimers();
|
|
523
|
+
try {
|
|
524
|
+
const { form } = makePage();
|
|
525
|
+
|
|
526
|
+
const revealed = form.revealField("title");
|
|
527
|
+
await vi.advanceTimersByTimeAsync(3000);
|
|
528
|
+
|
|
529
|
+
await expect(revealed).resolves.toBe(false);
|
|
530
|
+
expect(form.ctx.reveal?.value).toBeUndefined();
|
|
531
|
+
} finally {
|
|
532
|
+
vi.useRealTimers();
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -23,12 +23,15 @@ export {
|
|
|
23
23
|
} from "./runtime/composables/useProgressiveDisclosure.js";
|
|
24
24
|
export { flattenFieldGroups } from "./runtime/utils/flattenFieldGroups.js";
|
|
25
25
|
export type { FieldGroupDescriptor } from "./runtime/utils/flattenFieldGroups.js";
|
|
26
|
+
export { fieldsToJsonSchema } from "./runtime/utils/fieldsToJsonSchema.js";
|
|
27
|
+
export type { FieldsToJsonSchemaOptions } from "./runtime/utils/fieldsToJsonSchema.js";
|
|
26
28
|
export { KnownFieldType } from "./runtime/types/index.js";
|
|
27
29
|
export type {
|
|
28
30
|
FieldType,
|
|
29
31
|
FieldAccessor,
|
|
30
32
|
FieldDescriptor,
|
|
31
33
|
AnyFieldDescriptor,
|
|
34
|
+
FieldExtensions,
|
|
32
35
|
TextDescriptor,
|
|
33
36
|
NumberDescriptor,
|
|
34
37
|
BooleanDescriptor,
|
|
@@ -41,6 +44,8 @@ export type {
|
|
|
41
44
|
SeoDescriptor,
|
|
42
45
|
MapDescriptor,
|
|
43
46
|
RepeaterDescriptor,
|
|
47
|
+
RepeaterTemplate,
|
|
48
|
+
RepeaterImageFit,
|
|
44
49
|
MediaDescriptor,
|
|
45
50
|
MediaM2mDescriptor,
|
|
46
51
|
EnhancedMediaDescriptor,
|
|
@@ -51,4 +56,8 @@ export type {
|
|
|
51
56
|
FieldComponentEntry,
|
|
52
57
|
FieldRegistry,
|
|
53
58
|
PartialFieldRegistry,
|
|
59
|
+
FieldAnnotation,
|
|
60
|
+
FieldAnnotationAction,
|
|
61
|
+
FieldAnnotationColor,
|
|
62
|
+
FieldAnnotations,
|
|
54
63
|
} from "./runtime/types/index.js";
|
package/src/module.ts
CHANGED
|
@@ -120,6 +120,7 @@ export {};
|
|
|
120
120
|
"MapoFormField",
|
|
121
121
|
"MapoFormGroup",
|
|
122
122
|
"MapoFormTabs",
|
|
123
|
+
"MapoFormAnnotation",
|
|
123
124
|
"MapoUnknownField",
|
|
124
125
|
"MapoFocusPortal",
|
|
125
126
|
];
|
|
@@ -163,10 +164,18 @@ export {};
|
|
|
163
164
|
name: "useFormFromSchema",
|
|
164
165
|
from: resolver.resolve("./runtime/composables/useFormFromSchema"),
|
|
165
166
|
},
|
|
167
|
+
{
|
|
168
|
+
name: "fieldsToJsonSchema",
|
|
169
|
+
from: resolver.resolve("./runtime/utils/fieldsToJsonSchema"),
|
|
170
|
+
},
|
|
166
171
|
{
|
|
167
172
|
name: "useFormDraft",
|
|
168
173
|
from: resolver.resolve("./runtime/composables/useFormDraft"),
|
|
169
174
|
},
|
|
175
|
+
{
|
|
176
|
+
name: "useFieldAnnotation",
|
|
177
|
+
from: resolver.resolve("./runtime/composables/useFieldAnnotation"),
|
|
178
|
+
},
|
|
170
179
|
{
|
|
171
180
|
name: "useFocusMode",
|
|
172
181
|
from: resolver.resolve("./runtime/composables/useFocusMode"),
|