44 novalidate
55 :id =" form.id"
66 :name =" form.id"
7+ v-bind =" formattedOptions"
78 @submit.prevent =" handleSubmit"
89 >
910 <dynamic-input
1011 v-for =" control in controls"
1112 :key =" control.name"
1213 :control =" control"
14+ :submited =" submited"
1315 @changed =" valueChange"
1416 >
1517 <template v-slot :customField =" props " >
@@ -42,11 +44,14 @@ import {
4244 computed ,
4345 onMounted ,
4446 watch ,
47+ inject ,
4548} from ' vue' ;
4649import { DynamicForm } from ' ./form' ;
4750import DynamicInput from ' ../dynamic-input/DynamicInput.vue' ;
4851
4952import { InputBase , FormControl } from ' ../../core/models' ;
53+ import { dynamicFormsSymbol } from ' ../../useApi' ;
54+ import { warn } from ' ../../core/utils/warning' ;
5055
5156const props = {
5257 form: {
@@ -59,11 +64,15 @@ const components = {
5964 DynamicInput ,
6065};
6166
67+ const AVAILABLE_THEMES = [' default' , ' material' ];
68+
6269export default defineComponent ({
6370 name: ' asDynamicForm' ,
6471 props ,
6572 components ,
66- setup(props , { emit , slots }) {
73+ setup(props , ctx ) {
74+ const { options } = inject (dynamicFormsSymbol );
75+
6776 const controls: Ref <FormControl <any >[]> = ref ([]);
6877 const formValues = reactive ({});
6978 const submited = ref (false );
@@ -72,6 +81,31 @@ export default defineComponent({
7281 mapControls ();
7382 initValues ();
7483 });
84+ // TODO: enable again when plugin theme option is available
85+
86+ /* const validTheme = computed(
87+ () => options.theme && AVAILABLE_THEMES.includes(options.theme),
88+ );
89+
90+ if (!validTheme.value) {
91+ warn(
92+ `There isn't a theme: ${
93+ options.theme
94+ } just yet, please choose one of the available themes: ${AVAILABLE_THEMES.join(
95+ ', ',
96+ )}`,
97+ );
98+ } */
99+
100+ const deNormalizedScopedSlots = computed (() => Object .keys (ctx .slots ));
101+
102+ const normalizedControls = computed (() => {
103+ let normalizedControls = {};
104+ controls .value .forEach (element => {
105+ normalizedControls [element .name ] = element ;
106+ });
107+ return normalizedControls ;
108+ });
75109
76110 const isValid = computed (() => {
77111 const control = controls ?.value ?.find (control => ! control .valid );
@@ -95,9 +129,22 @@ export default defineComponent({
95129 : {};
96130 });
97131
132+ const formattedOptions = computed (() => {
133+ const { customClass, method, netlify, netlifyHoneypot } = options .form ;
134+ return {
135+ class: [
136+ customClass ,
137+ /* validTheme.value ? `theme-${options.theme}` : null, */
138+ ].join (' ' ),
139+ method ,
140+ ' data-netlify' : netlify ,
141+ ' data-netlify-honeypot' : netlifyHoneypot ,
142+ };
143+ });
144+
98145 function valueChange(changedValue : any ) {
99146 Object .assign (formValues , changedValue );
100- emit (' changed' , formValues );
147+ ctx . emit (' changed' , formValues );
101148 }
102149
103150 function mapControls(empty ? ) {
@@ -116,10 +163,10 @@ export default defineComponent({
116163 function handleSubmit() {
117164 submited .value = true ;
118165 if (isValid .value ) {
119- emit (' submited' , formValues );
166+ ctx . emit (' submited' , formValues );
120167 resetForm ();
121168 } else {
122- emit (' error' , formValues );
169+ ctx . emit (' error' , formValues );
123170 }
124171 }
125172
@@ -138,19 +185,9 @@ export default defineComponent({
138185 }, {})
139186 : {},
140187 );
141- emit (' changed' , formValues );
188+ ctx . emit (' changed' , formValues );
142189 }
143190
144- const deNormalizedScopedSlots = computed (() => Object .keys (slots ));
145-
146- const normalizedControls = computed (() => {
147- let normalizedControls = {};
148- controls .value .forEach (element => {
149- normalizedControls [element .name ] = element ;
150- });
151- return normalizedControls ;
152- });
153-
154191 watch (props , () => {
155192 mapControls ();
156193 });
@@ -166,9 +203,8 @@ export default defineComponent({
166203 deNormalizedScopedSlots ,
167204 normalizedControls ,
168205 submited ,
206+ formattedOptions ,
169207 };
170208 },
171209});
172210 </script >
173-
174- <style ></style >
0 commit comments