@mapomodule/form 2.0.0-beta.12 → 2.0.0-beta.13
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/module.json +1 -1
- package/dist/runtime/components/MapoForm.vue +2 -0
- package/dist/runtime/components/MapoForm.vue.d.ts +5 -0
- package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
- package/dist/runtime/composables/useMapoForm.d.ts.map +1 -1
- package/dist/runtime/composables/useMapoForm.js +4 -1
- package/package.json +2 -2
- package/src/__tests__/useMapoForm.test.ts +62 -2
- package/src/runtime/components/MapoForm.vue +7 -0
- package/src/runtime/composables/useMapoForm.ts +6 -1
package/dist/module.json
CHANGED
|
@@ -15,6 +15,7 @@ const props = defineProps({
|
|
|
15
15
|
currentLang: { type: String, required: false, default: "" },
|
|
16
16
|
readonly: { type: Boolean, required: false, default: false },
|
|
17
17
|
immediate: { type: Boolean, required: false, default: false },
|
|
18
|
+
notifyErrors: { type: Boolean, required: false, default: true },
|
|
18
19
|
registry: { type: Object, required: false, default: void 0 }
|
|
19
20
|
});
|
|
20
21
|
const injectedRegistry = computed(() => {
|
|
@@ -43,6 +44,7 @@ const form = useMapoForm({
|
|
|
43
44
|
languages: props.languages,
|
|
44
45
|
currentLang: currentLangRef,
|
|
45
46
|
immediate: props.immediate,
|
|
47
|
+
notifyErrors: props.notifyErrors,
|
|
46
48
|
debounce: globalDebounce,
|
|
47
49
|
registry: injectedRegistry.value
|
|
48
50
|
});
|
|
@@ -9,6 +9,11 @@ declare const __VLS_export: <T extends object>(__VLS_props: NonNullable<Awaited<
|
|
|
9
9
|
currentLang?: string;
|
|
10
10
|
readonly?: boolean;
|
|
11
11
|
immediate?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Show a toast for server-side field errors. Set to `false` for secondary
|
|
14
|
+
* forms that share errors with another form. Defaults to `true`.
|
|
15
|
+
*/
|
|
16
|
+
notifyErrors?: boolean;
|
|
12
17
|
/**
|
|
13
18
|
* Field component registry. When omitted, it is auto-injected from
|
|
14
19
|
* `$mapoFormRegistry` (provided by the `@mapomodule/form` plugin).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MapoForm.vue.d.ts","sourceRoot":"","sources":["../../../src/runtime/components/MapoForm.vue"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"MapoForm.vue.d.ts","sourceRoot":"","sources":["../../../src/runtime/components/MapoForm.vue"],"names":[],"mappings":"AA+QA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAG/B,OAAO,KAAK,EACV,kBAAkB,EAClB,aAAa,EAEd,MAAM,mBAAmB,CAAC;AAQ3B,QAAA,MAAM,YAAY,GAAK,CAAC,SAAS,MAAM,EACtC,aAAa,WAAW,CAAC,OAAO,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,EAC9D,YAAY,mBAAmB,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,EAC3G,gBAAgB,WAAW,CAAC,OAAO,CAAC,OAAO,WAAW,CAAC,CAAC,CAAC,QAAQ,CAAC,EAClE;WAuVO,OAAO,KAAK,EAAE,WAAW,GAAG,mBAAmB,CAAC;oBArVxC,CAAC;gBACL,kBAAkB,CAAC,CAAC,CAAC,EAAE;iBACtB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;oBACrB,MAAM,EAAE;sBACN,MAAM;mBACT,OAAO;oBACN,OAAO;QACnB;;;WAGG;uBACY,OAAO;QACtB;;;;WAIG;mBACQ,aAAa;;;KAoU+D,CAAC,GAAG,CAAC,OAAO,UAAU,SAAS;QAAE,oBAAoB,EAAE,MAAM,CAAC,CAAA;KAAE,GAAG,CAAC,GAAG,EAAE,CAAC;YAC5J,CAAC,OAAO,EAAE,OAAO,KAAK,EAAE,gBAAgB;;;;;;;;;;MAAs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AO,CAAC;QACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KAChC;;oBA2BoB,OAAO,CAAC,CAAC,CAAC;sBAKpB,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC;;;iBAzU1D,IAAI;iBACJ,MAAM,IAAI;iBACV,MAAM,IAAI;;iBAFV,IAAI;iBACJ,MAAM,IAAI;iBACV,MAAM,IAAI;;;;EAobpB"}
|
|
@@ -63,7 +63,10 @@ export function useMapoForm(options) {
|
|
|
63
63
|
(val) => {
|
|
64
64
|
const entries = Object.entries(val);
|
|
65
65
|
if (!entries.length) return;
|
|
66
|
-
const lines = entries.map(([field, msgs]) =>
|
|
66
|
+
const lines = entries.map(([field, msgs]) => {
|
|
67
|
+
const label = toValue(fields).find((descriptor) => descriptor.key === field)?.label || field;
|
|
68
|
+
return `${label}: ${msgs.join(", ")}`;
|
|
69
|
+
}).join("\n");
|
|
67
70
|
snack.show(lines, "error");
|
|
68
71
|
},
|
|
69
72
|
{ deep: true }
|
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.13",
|
|
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.5",
|
|
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",
|
|
@@ -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 ────────────────────────────────────────────────────────────────
|
|
@@ -24,6 +24,11 @@ const props = withDefaults(
|
|
|
24
24
|
currentLang?: string;
|
|
25
25
|
readonly?: boolean;
|
|
26
26
|
immediate?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Show a toast for server-side field errors. Set to `false` for secondary
|
|
29
|
+
* forms that share errors with another form. Defaults to `true`.
|
|
30
|
+
*/
|
|
31
|
+
notifyErrors?: boolean;
|
|
27
32
|
/**
|
|
28
33
|
* Field component registry. When omitted, it is auto-injected from
|
|
29
34
|
* `$mapoFormRegistry` (provided by the `@mapomodule/form` plugin).
|
|
@@ -37,6 +42,7 @@ const props = withDefaults(
|
|
|
37
42
|
currentLang: "",
|
|
38
43
|
readonly: false,
|
|
39
44
|
immediate: false,
|
|
45
|
+
notifyErrors: true,
|
|
40
46
|
registry: undefined,
|
|
41
47
|
},
|
|
42
48
|
);
|
|
@@ -85,6 +91,7 @@ const form = useMapoForm({
|
|
|
85
91
|
languages: props.languages,
|
|
86
92
|
currentLang: currentLangRef,
|
|
87
93
|
immediate: props.immediate,
|
|
94
|
+
notifyErrors: props.notifyErrors,
|
|
88
95
|
debounce: globalDebounce,
|
|
89
96
|
registry: injectedRegistry.value,
|
|
90
97
|
});
|
|
@@ -140,7 +140,12 @@ export function useMapoForm<T extends object>(options: UseMapoFormOptions<T>) {
|
|
|
140
140
|
const entries = Object.entries(val);
|
|
141
141
|
if (!entries.length) return;
|
|
142
142
|
const lines = entries
|
|
143
|
-
.map(([field, msgs]) =>
|
|
143
|
+
.map(([field, msgs]) => {
|
|
144
|
+
const label =
|
|
145
|
+
toValue(fields).find((descriptor) => descriptor.key === field)
|
|
146
|
+
?.label || field;
|
|
147
|
+
return `${label}: ${(msgs as string[]).join(", ")}`;
|
|
148
|
+
})
|
|
144
149
|
.join("\n");
|
|
145
150
|
snack.show(lines, "error");
|
|
146
151
|
},
|