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

Commit a601625

Browse files
authored
Merge pull request #116 from alvarosaburido/feature/library-options-for-next
Feature/library options for next
2 parents 0b8b4e4 + ca16e61 commit a601625

File tree

16 files changed

+2042
-2305
lines changed

16 files changed

+2042
-2305
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: ci
33
on:
44
pull_request:
55
branches:
6-
- master
6+
- 0.x
77
- next
88
- dev
99

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Release
22
on:
33
push:
44
branches:
5-
- master
5+
- 0.x
66
- next
77
jobs:
88
release:

dev/typescript/App.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ import {
5656
CheckboxInput,
5757
RadioInput,
5858
InputBase,
59+
email,
60+
pattern,
5961
} from '../../src';
60-
import { email, pattern } from '@/core/utils';
6162
6263
export default defineComponent({
6364
name: 'app',

dev/typescript/main.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ import { createApp } from 'vue';
33
import App from './App.vue';
44
import './styles/main.scss';
55

6-
import { createDynamicForms } from '../../src';
6+
// import { createDynamicForms } from '../../src';
7+
import { createDynamicForms } from '../../dist/as-dynamic-forms.esm';
78

89
const VueDynamicForms = createDynamicForms({
9-
theme: 'material',
10+
autoValidate: true,
11+
form: {
12+
customClass: 'plugin-options-class-added',
13+
method: 'POST',
14+
netlify: false,
15+
netlifyHoneypot: null,
16+
},
1017
});
1118

1219
export const app = createApp(App);

dev/vue/App.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ import {
3434
EmailInput,
3535
FormValidation,
3636
PasswordInput,
37+
email,
38+
pattern,
3739
TextAreaInput,
38-
} from '../../dist/as-dynamic-forms.esm';
39-
import { email, pattern } from '@/core/utils';
40+
} from '../../src';
4041
4142
export default defineComponent({
4243
name: 'app',

dev/vue/styles/_vendors.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$input-border-radius: 50px; */
99
// @import '../../src/styles/themes/default.scss';
1010

11-
@import '@/styles/themes/default.scss';
11+
/* @import '@/styles/themes/default.scss'; */
1212

1313
// Third-party
1414

package-lock.json

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

package.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"release": {
1818
"branches": [
19-
"master",
19+
"0.x",
2020
"next"
2121
]
2222
},
@@ -41,13 +41,13 @@
4141
"vue": "^3.0.0"
4242
},
4343
"devDependencies": {
44-
"@microsoft/api-extractor": "^7.9.19",
44+
"@microsoft/api-extractor": "^7.9.22",
4545
"@rollup/plugin-buble": "^0.21.3",
46-
"@rollup/plugin-commonjs": "^15.0.0",
46+
"@rollup/plugin-commonjs": "^15.1.0",
4747
"@rollup/plugin-node-resolve": "^9.0.0",
4848
"@rollup/plugin-replace": "^2.3.3",
49-
"@typescript-eslint/eslint-plugin": "^4.1.1",
50-
"@typescript-eslint/parser": "^4.1.1",
49+
"@typescript-eslint/eslint-plugin": "^4.2.0",
50+
"@typescript-eslint/parser": "^4.2.0",
5151
"@vue/cli-plugin-babel": "4.5.6",
5252
"@vue/cli-plugin-eslint": "4.5.6",
5353
"@vue/cli-plugin-typescript": "~4.5.6",
@@ -61,14 +61,14 @@
6161
"babel-eslint": "10.1.0",
6262
"babel-jest": "26.3.0",
6363
"chalk": "^4.1.0",
64-
"eslint": "7.9.0",
64+
"eslint": "7.10.0",
6565
"eslint-plugin-prettier": "^3.1.4",
6666
"eslint-plugin-vue": "^7.0.0-0",
6767
"node-sass": "4.14.1",
6868
"prettier": "2.1.2",
69-
"rollup": "^2.27.1",
69+
"rollup": "^2.28.2",
7070
"rollup-plugin-terser": "^7.0.2",
71-
"rollup-plugin-typescript2": "^0.27.2",
71+
"rollup-plugin-typescript2": "^0.27.3",
7272
"rollup-plugin-vue": "^6.0.0-beta.10",
7373
"sass-loader": "10.0.2",
7474
"semantic-release": "17.1.2",
@@ -77,6 +77,9 @@
7777
"typescript": "~4.0.3",
7878
"vue": "^3.0.0",
7979
"vue-select": "3.10.8",
80-
"vuepress": "1.5.4"
80+
"vuepress": "1.6.0"
81+
},
82+
"dependencies": {
83+
"rollup-plugin-scss": "^2.6.0"
8184
}
8285
}

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':

0 commit comments

Comments
 (0)