Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit a1af31a

Browse files
committed
feature(next): form submission
1 parent 43c29d7 commit a1af31a

File tree

7 files changed

+153
-46
lines changed

7 files changed

+153
-46
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = {
2020
'error',
2121
{
2222
allowDestructuring: true, // Allow `const { props, state } = this`; false by default
23-
allowedNames: ['self'], // Allow `const self = this`; `[]` by default
23+
allowedNames: ['self', 'vdf'], // Allow `const self = this`; `[]` by default
2424
},
2525
],
2626
},

dev/vue/App.vue

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
<h1 class="title m-4">{{ title }}</h1>
55
<div class="flex justify-between">
66
<div class="card p-6">
7-
<dynamic-form :form="form" />
7+
<dynamic-form :form="form" @submited="handleSubmit" />
8+
<button
9+
class="btn bg-teal-500 text-white hover:bg-teal-700 mt-4"
10+
submit="true"
11+
:form="form?.id"
12+
>
13+
Submit Form
14+
</button>
815
</div>
916
<div class="card p-6">
1017
<pre>{{ form }}</pre>
@@ -28,7 +35,7 @@ export default defineComponent({
2835
setup() {
2936
const title = ref('Vue Dynamic Forms');
3037
const form = reactive<DynamicForm>({
31-
id: 'form',
38+
id: 'example-form',
3239
fields: [
3340
new TextInput({
3441
label: 'Name',
@@ -57,6 +64,9 @@ export default defineComponent({
5764
}),
5865
],
5966
});
67+
function handleSubmit(values) {
68+
console.log('Values Submitted', values);
69+
}
6070
onMounted(() =>
6171
setTimeout(() => {
6272
form.fields[0].label = 'RockNRoll';
@@ -66,6 +76,7 @@ export default defineComponent({
6676
return {
6777
title,
6878
form,
79+
handleSubmit,
6980
};
7081
},
7182
});

dev/vue/styles/base.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@
1919
pre {
2020
@apply bg-gray-900 text-white text-xs p-4 rounded-md;
2121
}
22+
23+
.btn {
24+
@apply font-bold py-2 px-4 rounded;
25+
}

package-lock.json

Lines changed: 112 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"main": "dist/as-dynamic-forms.common.js",
2828
"dependencies": {
29-
"vue": "^3.0.0-0"
29+
"vue": "^3.0.0-rc.7"
3030
},
3131
"devDependencies": {
3232
"@typescript-eslint/eslint-plugin": "^2.33.0",

src/components/dynamic-form/DynamicForm.vue

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<template>
2-
<form class="dynamic-form" novalidate :id="form.id">
2+
<form
3+
class="dynamic-form"
4+
novalidate
5+
:id="form.id"
6+
:name="form.id"
7+
@submit.prevent="handleSubmit"
8+
>
39
<dynamic-input
410
v-for="control in controls"
511
:key="control?.name"
@@ -40,6 +46,7 @@ export default defineComponent({
4046
setup(props, { emit }) {
4147
const controls: Ref<FormControl<any>[] | undefined> = ref([]);
4248
const formValues = reactive({});
49+
const submited = ref(false);
4350
watchEffect(() => {
4451
controls.value =
4552
props.form?.fields?.map(
@@ -64,21 +71,19 @@ export default defineComponent({
6471
function valueChanged(changedValue: any) {
6572
Object.assign(formValues, changedValue);
6673
}
67-
/* const formValues = computed(() => {
68-
return controls.value
69-
? controls.value.reduce((prev, curr) => {
70-
const obj = {};
71-
obj[curr.name] =
72-
curr.type === 'number' ? parseFloat(curr.value) : curr.value;
73-
return {
74-
...prev,
75-
...obj,
76-
};
77-
}, {})
78-
: {};
79-
}); */
8074
81-
return { controls, form: props.form, valueChanged, formValues };
75+
function handleSubmit() {
76+
submited.value = true;
77+
emit('submited', formValues);
78+
}
79+
80+
return {
81+
controls,
82+
form: props.form,
83+
valueChanged,
84+
formValues,
85+
handleSubmit,
86+
};
8287
},
8388
});
8489
</script>

src/dynamicForms.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export function createDynamicForms(
1717
const vdf: DynamicFormsPlugin = {
1818
options,
1919
install(app: App) {
20-
const vdf = this;
20+
const self = this;
2121
app.component('dynamic-form', DynamicForm);
22-
app.provide(dynamicFormsSymbol, vdf);
22+
app.provide(dynamicFormsSymbol, self);
2323
},
2424
};
2525

0 commit comments

Comments
 (0)