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

Commit cc8e39b

Browse files
committed
feat(forms): intercept change event and pass control name up
1 parent f894ca5 commit cc8e39b

File tree

4 files changed

+52
-34
lines changed

4 files changed

+52
-34
lines changed

src/components/dynamic-form/DynamicForm.vue

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,10 @@ export default defineComponent({
8282
const { options } = inject(dynamicFormsSymbol);
8383
8484
const controls: Ref<FormControl<InputType>[]> = ref([]);
85-
const formValues = reactive({});
8685
const submited = ref(false);
8786
8887
onMounted(() => {
8988
mapControls();
90-
initValues();
9189
});
9290
// TODO: enable again when plugin theme option is available
9391
@@ -120,6 +118,22 @@ export default defineComponent({
120118
return !hasInvalidControls;
121119
});
122120
121+
const formValues = computed(() => {
122+
return removeEmpty(
123+
controls.value.reduce((prev, curr) => {
124+
const obj = {};
125+
obj[curr.name] =
126+
curr.type === FieldTypes.NUMBER
127+
? parseFloat(`${curr.value}`)
128+
: curr.value;
129+
return {
130+
...prev,
131+
...obj,
132+
};
133+
}, {}),
134+
);
135+
});
136+
123137
const errors = computed(() => {
124138
return controls.value
125139
? controls.value.reduce((prev, curr) => {
@@ -160,9 +174,18 @@ export default defineComponent({
160174
}
161175
});
162176
163-
function valueChange(changedValue: Record<string, unknown>) {
164-
Object.assign(formValues, changedValue);
165-
ctx.emit('change', removeEmpty(formValues));
177+
function valueChange(event: Record<string, unknown>) {
178+
if (event) {
179+
const newControl = controls.value.find(
180+
control => control.name === event.name,
181+
);
182+
if (newControl) {
183+
newControl.value = event.value as string;
184+
newControl.dirty = event.value !== null;
185+
}
186+
console.log('DynamicForms:controls', controls.value);
187+
ctx.emit('change', formValues.value);
188+
}
166189
}
167190
168191
function mapControls(empty?: boolean) {
@@ -208,26 +231,6 @@ export default defineComponent({
208231
}
209232
}
210233
211-
function initValues() {
212-
Object.assign(
213-
formValues,
214-
controls.value
215-
? controls.value.reduce((prev, curr) => {
216-
const obj = {};
217-
obj[curr.name] =
218-
curr.type === FieldTypes.NUMBER
219-
? parseFloat(`${curr.value}`)
220-
: curr.value;
221-
return {
222-
...prev,
223-
...obj,
224-
};
225-
}, {})
226-
: {},
227-
);
228-
ctx.emit('changed', formValues);
229-
}
230-
231234
watch(props.form.fields, () => {
232235
mapControls();
233236
});

src/components/dynamic-input/DynamicInput.vue

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export default defineComponent({
131131
return [];
132132
});
133133
134-
function validate() {
134+
/* function validate() {
135135
if (
136136
props.control &&
137137
props.control.validations &&
@@ -161,12 +161,16 @@ export default defineComponent({
161161
props.control.errors = validation;
162162
props.control.valid = Object.keys(validation).length === 0;
163163
}
164-
}
164+
} */
165165
166166
function valueChange($event) {
167-
let value;
167+
console.log('DynamicInput:change', $event);
168+
emit('change', $event);
169+
/* let value;
168170
const newValue = {};
169171
172+
console.log('DynamicInput:change', $event);
173+
170174
if (isEvent($event)) {
171175
$event.stopPropagation();
172176
value =
@@ -181,7 +185,7 @@ export default defineComponent({
181185
newValue[props.control.name] = value;
182186
validate();
183187
emit('change', newValue);
184-
}
188+
} */
185189
}
186190
187191
return () => {

src/components/text-input/TextInput.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { defineComponent, h, PropType } from 'vue';
2+
import { defineComponent, h, PropType, ref } from 'vue';
33
import {
44
ColorInput,
55
EmailInput,
@@ -21,14 +21,13 @@ export default defineComponent({
2121
props,
2222
setup(props, { emit }) {
2323
const { onChange, onFocus, onBlur } = useInputEvents(props, emit);
24-
2524
return () =>
2625
h('input', {
2726
id: props.control.name,
2827
name: props.control.name || '',
2928
type: props.control.type,
3029
class: ['form-control'],
31-
value: props?.control?.value,
30+
value: props.control.value,
3231
disabled: props.control.disabled,
3332
placeholder: props.control.placeholder,
3433
required: props.control.required,

src/composables/input-events.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@ import { watch } from 'vue';
55
export function useInputEvents(props: any, emit: any) {
66
function onChange($event): void {
77
if (props.control) {
8-
props.control.dirty = true;
8+
$event.stopImmediatePropagation();
9+
10+
emit('change', {
11+
name: props.control.name,
12+
value: props.control.type === 'checkbox'
13+
? $event.target.checked
14+
: $event.target.value,
15+
});
916
}
1017
}
1118
function onCheck($event): void {
1219
if (props.control) {
13-
props.control.dirty = true;
20+
$event.stopImmediatePropagation();
21+
22+
emit('change', {
23+
name: props.control.name,
24+
value: $event.target.checked,
25+
});
1426
}
1527
}
1628
function onFocus(): void {

0 commit comments

Comments
 (0)