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

Commit fe146ef

Browse files
committed
feat(options): added plugin options
1 parent 0b8b4e4 commit fe146ef

File tree

4 files changed

+84
-32
lines changed

4 files changed

+84
-32
lines changed

src/components/dynamic-form/DynamicForm.vue

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
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';
4649
import { DynamicForm } from './form';
4750
import DynamicInput from '../dynamic-input/DynamicInput.vue';
4851
4952
import { InputBase, FormControl } from '../../core/models';
53+
import { dynamicFormsSymbol } from '../../useApi';
54+
import { warn } from '../../core/utils/warning';
5055
5156
const props = {
5257
form: {
@@ -59,11 +64,15 @@ const components = {
5964
DynamicInput,
6065
};
6166
67+
const AVAILABLE_THEMES = ['default', 'material'];
68+
6269
export 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>

src/components/dynamic-input/DynamicInput.vue

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
/* eslint-disable @typescript-eslint/no-use-before-define */
33
4-
import { defineComponent, PropType, computed, h } from 'vue';
4+
import { defineComponent, PropType, computed, h, inject } from 'vue';
55
import TextInput from '../text-input/TextInput.vue';
66
import SelectInput from '../select-input/SelectInput.vue';
77
import TextAreaInput from '../text-area-input/TextAreaInput.vue';
@@ -11,6 +11,7 @@ import RadioInput from '../radio-input/RadioInput.vue';
1111
import { FormControl } from '../../core/models';
1212
import { isEmpty, entries, values, keys } from '../../core/utils/helpers';
1313
import { useInputEvents } from '../../composables/input-events';
14+
import { dynamicFormsSymbol } from '../../useApi';
1415
1516
const components = {
1617
TextInput,
@@ -25,16 +26,31 @@ const props = {
2526
type: Object as PropType<FormControl<any>>,
2627
required: true,
2728
},
29+
submited: {
30+
type: Boolean,
31+
required: true,
32+
},
2833
};
2934
export default defineComponent({
3035
name: 'asDynamicInput',
3136
components,
3237
props,
3338
setup(props, { emit, slots }) {
3439
const { onFocus, onBlur } = useInputEvents(props?.control, emit);
40+
const { options } = inject(dynamicFormsSymbol);
3541
3642
let component;
3743
44+
const attributes = computed(() => {
45+
return {
46+
control: props.control,
47+
onChanged: valueChange,
48+
};
49+
});
50+
51+
const hasLabel = computed(() => props?.control?.type !== 'checkbox');
52+
const isFieldSet = computed(() => props?.control?.type === 'radio');
53+
3854
const getClasses = computed(() => {
3955
return [
4056
'dynamic-input',
@@ -49,16 +65,24 @@ export default defineComponent({
4965
];
5066
});
5167
68+
const autoValidate = computed(
69+
() => props?.control?.touched && options.autoValidate,
70+
);
71+
5272
const showErrors = computed(() => {
53-
return props?.control?.errors && keys(props?.control?.errors).length > 0;
73+
return (
74+
props?.control?.errors &&
75+
keys(props?.control?.errors).length > 0 &&
76+
(props.submited || autoValidate.value)
77+
);
5478
/* props.control.errors &&
5579
Object.keys(props.control.errors).length > 0 &&
5680
(this.submited || this.autoValidate) */
5781
});
5882
5983
const errorMessages = computed(() => {
6084
const errors = values(props?.control?.errors || {});
61-
if (errors.length > 0) {
85+
if (errors.length > 0 && (props.submited || autoValidate.value)) {
6286
return errors.map(value => value.text);
6387
}
6488
return [];
@@ -93,7 +117,6 @@ export default defineComponent({
93117
}, {});
94118
props.control.errors = validation;
95119
props.control.valid = Object.keys(validation).length === 0;
96-
console.log(props.control);
97120
}
98121
}
99122
@@ -106,16 +129,6 @@ export default defineComponent({
106129
}
107130
}
108131
109-
const attributes = computed(() => {
110-
return {
111-
control: props.control,
112-
onChanged: valueChange,
113-
};
114-
});
115-
116-
const hasLabel = computed(() => props?.control?.type !== 'checkbox');
117-
const isFieldSet = computed(() => props?.control?.type === 'radio');
118-
119132
return () => {
120133
switch (props?.control?.type) {
121134
case 'text':

src/dynamicForms.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { App } from 'vue';
22
import { dynamicFormsSymbol } from './useApi';
33
import DynamicForm from './components/dynamic-form/DynamicForm.vue';
4+
import { FormOptions } from './core/models';
45

56
export interface DynamicFormsOptions {
6-
theme?: string;
7+
autoValidate?: boolean;
8+
form?: FormOptions;
79
}
810

911
export interface DynamicFormsPlugin {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './useApi';
22
export * from './dynamicForms';
33
export * from './core/models';
4+
export * from './core/utils';

0 commit comments

Comments
 (0)