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

Commit 227bbeb

Browse files
committed
feat(next): more reactivity improvements
1 parent 4f6bce7 commit 227bbeb

File tree

3 files changed

+80
-42
lines changed

3 files changed

+80
-42
lines changed

dev/vue/App.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
:form="form"
99
@submited="handleSubmit"
1010
@changed="valueChanged"
11+
@error="handleError"
1112
/>
1213
<button
1314
class="btn bg-teal-500 text-white hover:bg-teal-700 mt-4"
@@ -26,7 +27,7 @@
2627
</template>
2728

2829
<script lang="ts">
29-
import { defineComponent, reactive, ref } from 'vue';
30+
import { defineComponent, onMounted, reactive, ref } from 'vue';
3031
import {
3132
TextInput,
3233
SelectInput,
@@ -90,18 +91,23 @@ export default defineComponent({
9091
function valueChanged(values) {
9192
Object.assign(formValues, values);
9293
}
93-
/* onMounted(() =>
94+
function handleError(errors) {
95+
// eslint-disable-next-line no-undef
96+
alert(errors);
97+
}
98+
onMounted(() =>
9499
setTimeout(() => {
95100
form.fields[0].label = 'RockNRoll';
96101
form.fields[0].value = 'James Bond';
97102
}, 2000),
98-
); */
103+
);
99104
return {
100105
title,
101106
form,
102107
handleSubmit,
103108
valueChanged,
104109
formValues,
110+
handleError,
105111
};
106112
},
107113
});

src/components/dynamic-form/DynamicForm.vue

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@
1919
import {
2020
defineComponent,
2121
PropType,
22-
toRef,
2322
reactive,
24-
watchEffect,
2523
ref,
2624
Ref,
2725
computed,
2826
onMounted,
27+
watchEffect,
28+
watch,
2929
} from 'vue';
3030
import { DynamicForm } from './form';
3131
import DynamicInput from '@/components/dynamic-input/DynamicInput.vue';
3232
import { InputBase, FormControl } from '@/core/models';
3333
3434
const props = {
35-
form: Object as PropType<DynamicForm>,
35+
form: {
36+
type: Object as PropType<DynamicForm>,
37+
required: true,
38+
},
3639
};
3740
3841
const components = {
@@ -44,10 +47,32 @@ export default defineComponent({
4447
props,
4548
components,
4649
setup(props, { emit }) {
47-
const controls: Ref<FormControl<any>[] | undefined> = ref([]);
50+
const controls: Ref<FormControl<any>[]> = ref([]);
4851
const formValues = reactive({});
4952
const submited = ref(false);
5053
54+
const isValid = computed(() => {
55+
const control = controls?.value?.find(control => !control.valid);
56+
return control ? control.valid : true;
57+
});
58+
59+
const errors = computed(() => {
60+
return controls.value
61+
? controls.value.reduce((prev, curr) => {
62+
const errors = Object.keys(curr.errors || {}) || [];
63+
if (errors.length > 0) {
64+
const error = {};
65+
error[curr.name] = curr.errors;
66+
return {
67+
...prev,
68+
...error,
69+
};
70+
}
71+
return prev;
72+
}, {})
73+
: {};
74+
});
75+
5176
function valueChange(changedValue: any) {
5277
Object.assign(formValues, changedValue);
5378
emit('changed', formValues);
@@ -60,6 +85,7 @@ export default defineComponent({
6085
? new FormControl({ ...field, value: null })
6186
: new FormControl({ ...field }),
6287
) || [];
88+
console.log('mapControls', controls.value);
6389
}
6490
6591
function resetForm() {
@@ -68,8 +94,12 @@ export default defineComponent({
6894
6995
function handleSubmit() {
7096
submited.value = true;
71-
emit('submited', formValues);
72-
resetForm();
97+
if (isValid.value) {
98+
emit('submited', formValues);
99+
resetForm();
100+
} else {
101+
emit('error', formValues);
102+
}
73103
}
74104
75105
function initValues() {
@@ -95,17 +125,18 @@ export default defineComponent({
95125
initValues();
96126
});
97127
98-
/* watchEffect(() => {
128+
watch(props, (prev, current) => {
99129
mapControls();
100-
initValues();
101-
}); */
130+
});
102131
103132
return {
104133
controls,
105134
form: props.form,
106135
valueChange,
107136
formValues,
108137
handleSubmit,
138+
isValid,
139+
errors,
109140
};
110141
},
111142
});

src/components/dynamic-input/DynamicInput.vue

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</div>
3737
</template>
3838
<script lang="ts">
39-
import { defineComponent, PropType, computed, ref } from 'vue';
39+
import { defineComponent, PropType, computed, ref, reactive } from 'vue';
4040
import TextInput from '@/components/text-input/TextInput.vue';
4141
import SelectInput from '@/components/select-input/SelectInput.vue';
4242
@@ -49,13 +49,42 @@ const components = {
4949
};
5050
5151
const props = {
52-
control: Object as PropType<FormControl<any>>,
52+
control: {
53+
type: Object as PropType<FormControl<any>>,
54+
required: true,
55+
},
5356
};
5457
export default defineComponent({
5558
name: 'asDynamicInput',
5659
components,
5760
props,
5861
setup(props, { emit }) {
62+
const showErrors = computed(() => {
63+
return props?.control?.errors && keys(props?.control?.errors).length > 0;
64+
/* props.control.errors &&
65+
Object.keys(props.control.errors).length > 0 &&
66+
(this.submited || this.autoValidate) */
67+
});
68+
69+
const getClasses = computed(() => {
70+
return [
71+
'dynamic-input',
72+
'form-group',
73+
{
74+
'form-group--error': showErrors.value,
75+
},
76+
`${props?.control?.customClass || ''}`,
77+
];
78+
});
79+
80+
const errorMessages = computed(() => {
81+
const errors = values(props?.control?.errors || {});
82+
if (errors.length > 0) {
83+
return errors.map(value => value.text);
84+
}
85+
return [];
86+
});
87+
5988
function validate() {
6089
if (
6190
props.control &&
@@ -109,39 +138,11 @@ export default defineComponent({
109138
},
110139
};
111140
}); */
112-
113-
const showErrors = computed(() => {
114-
return props?.control?.errors && keys(props?.control?.errors).length > 0;
115-
/* props.control.errors &&
116-
Object.keys(props.control.errors).length > 0 &&
117-
(this.submited || this.autoValidate) */
118-
});
119-
120-
const getClasses = computed(() => {
121-
return [
122-
'dynamic-input',
123-
'form-group',
124-
{
125-
'form-group--error': showErrors.value,
126-
},
127-
`${props?.control?.customClass || ''}`,
128-
];
129-
});
130-
131-
const errorMessages = computed(() => {
132-
const errors = values(props?.control?.errors || {});
133-
if (errors.length > 0) {
134-
return errors.map(value => value.text);
135-
}
136-
return [];
137-
});
138-
139141
return {
140142
valueChange,
141143
errorMessages,
142144
getClasses,
143145
showErrors,
144-
control: ref(props.control),
145146
};
146147
147148
/* return () => {

0 commit comments

Comments
 (0)