Skip to content

Commit a874342

Browse files
adapt the slot field
1 parent dc9b672 commit a874342

File tree

6 files changed

+113
-23
lines changed

6 files changed

+113
-23
lines changed

src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
</template>
66

77
<script setup lang="ts">
8-
import CP1 from "./views/ModalCreate.vue";
8+
import CP1 from "./views/FormFieldsTest.vue";
99
</script>

src/components/FormFields.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ const nestedGetError = (field: string) => {
6363
:placeholder="field.placeholder"
6464
@update:model-value="onUpdate([field.name], $event)"
6565
/>
66-
<template v-else-if="field.type === 'custom'">
67-
<slot name="custom" :data="internalData" />
66+
<template v-else-if="field.type === 'slot'">
67+
<slot :name="field.name" :data="internalData" />
6868
</template>
6969
<FormSelect
7070
v-else-if="field.type === 'select'"

src/tests/FormFields.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import FormFields from "../components/FormFields.vue";
2+
import { FormField } from "../types/form";
3+
import { mount } from "@vue/test-utils";
4+
5+
const fields: FormField[] = [
6+
{
7+
name: "test01",
8+
label: "Test 01",
9+
type: "text",
10+
required: true,
11+
},
12+
{
13+
name: "test02",
14+
label: "Test 02",
15+
type: "text",
16+
required: true,
17+
},
18+
{
19+
name: "slot03",
20+
type: "slot",
21+
},
22+
];
23+
24+
describe("form fields", () => {
25+
it("has slot fields", async () => {
26+
const text = "My Custom Slot Content";
27+
28+
const formfields = mount(FormFields, {
29+
props: {
30+
fields: fields,
31+
onUpdate: () => {
32+
/* do nothing */
33+
},
34+
data: {},
35+
getError: () => undefined,
36+
},
37+
slots: {
38+
slot03: text,
39+
},
40+
});
41+
42+
expect(formfields.text()).toContain(text);
43+
});
44+
});

src/types/form.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@ export type FormOption = {
1313
value: string;
1414
};
1515

16-
export interface IRawFormField {
16+
type BaseField = {
1717
label: string;
1818
name: string;
19+
autocomplete?: string;
20+
disabled?: boolean;
21+
required?: boolean;
22+
placeholder?: string;
23+
helptext?: string;
24+
};
25+
26+
type InputField = BaseField & {
1927
type:
20-
| "multiple" // form multiple
21-
| "list" // form list
22-
| "select" // form select
2328
| "textarea" // form textarea
2429
| "toggle" // form checkbox
2530
| "file" // form file
2631
| "files" // form files
27-
| "combobox" // form combobox
28-
| "searchselect" // form searchselect
29-
| "datalist" // form datalist
3032
| "custom"
3133
// form input
32-
| "checkbox"
3334
| "button"
3435
| "color"
3536
| "date"
@@ -50,15 +51,22 @@ export interface IRawFormField {
5051
| "time"
5152
| "url"
5253
| "week";
53-
autocomplete?: string;
54-
disabled?: boolean;
55-
required?: boolean;
56-
placeholder?: string;
57-
options?: FormOptionInput[];
58-
room?: string;
59-
helptext?: string;
60-
}
54+
};
55+
56+
type OptionsField = BaseField & {
57+
type:
58+
| "multiple" // form multiple
59+
| "list" // form list
60+
| "select" // form select
61+
| "combobox" // form combobox
62+
| "searchselect" // form searchselect
63+
| "datalist" // form datalist
64+
| "checkbox";
65+
options: FormOptionInput[];
66+
};
67+
68+
type NestedField = { type: "fields"; name: string; fields: FormField[] };
69+
70+
type SlotField = { type: "slot"; name: string };
6171

62-
export type FormField =
63-
| IRawFormField
64-
| { type: "fields"; name: string; fields: FormField[] };
72+
export type FormField = InputField | OptionsField | NestedField | SlotField;

src/views/FormFieldsTest.vue

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<script setup lang="ts">
2+
import FormFields from "../components/FormFields.vue";
3+
import { FormField } from "../types/form";
4+
5+
const fields: FormField[] = [
6+
{
7+
name: "test01",
8+
label: "Test 01",
9+
type: "text",
10+
required: true,
11+
},
12+
{
13+
name: "test02",
14+
label: "Test 02",
15+
type: "text",
16+
required: true,
17+
},
18+
{
19+
name: "slot03",
20+
type: "slot",
21+
},
22+
];
23+
</script>
24+
25+
<template>
26+
<div class="max-w-md p-10">
27+
<FormFields
28+
:fields="fields"
29+
:data="{}"
30+
:on-update="() => {}"
31+
:get-error="() => undefined"
32+
>
33+
<template #slot03>
34+
<div class="bg-red-500">Slot 03</div>
35+
</template>
36+
</FormFields>
37+
</div>
38+
</template>

src/views/ModalCreate.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ref } from "vue";
55
import ModalCreate from "../components/ModalCreate.vue";
66
77
const data = ref({});
8-
const request = (d) => Promise.reject({ title: "Command Not Found" });
8+
const request = () => Promise.reject({ title: "Command Not Found" });
99
const modalOpen = ref(false);
1010
1111
const fields: FormField[] = [

0 commit comments

Comments
 (0)