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

Commit 9f40b70

Browse files
authored
Merge pull request #170 from alvarosaburido/feature/improve-event-handling
feat(inputs): improve event handling
2 parents c36c23e + 64abf4e commit 9f40b70

File tree

6 files changed

+40
-24
lines changed

6 files changed

+40
-24
lines changed

dev/typescript/App.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<dynamic-form
88
:form="form"
99
@submited="handleSubmit"
10-
@changed="valueChanged"
10+
@change="valueChanged"
1111
@error="handleError"
1212
>
1313
<template
@@ -195,7 +195,7 @@ export default defineComponent({
195195
196196
function valueChanged(values) {
197197
Object.assign(formValues, values);
198-
console.log('Values', values);
198+
/* console.log('Values', values); */
199199
}
200200
201201
function handleError(errors) {

src/components/checkbox-input/CheckboxInput.vue

+15-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ export default defineComponent({
1111
name: 'asCheckboxInput',
1212
props,
1313
setup(props, { emit }) {
14-
const { onCheck, onFocus, onBlur } = useInputEvents(props, emit);
15-
16-
return () => [
14+
const { onChange, onFocus, onBlur } = useInputEvents(props, emit);
15+
const renderCheckbox = [
1716
h('input', {
1817
name: props?.control?.name || '',
1918
type: props?.control?.type,
@@ -24,7 +23,7 @@ export default defineComponent({
2423
checked: props?.control?.value,
2524
onFocus,
2625
onBlur,
27-
onChange: onCheck,
26+
onChange,
2827
}),
2928
h(
3029
'label',
@@ -35,6 +34,18 @@ export default defineComponent({
3534
props?.control?.label,
3635
),
3736
];
37+
38+
return () => [
39+
h(
40+
'div',
41+
{
42+
class: 'checkbox-group',
43+
tabIndex: -1,
44+
role: 'group',
45+
},
46+
renderCheckbox,
47+
),
48+
];
3849
},
3950
});
4051
</script>

src/components/dynamic-form/DynamicForm.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
:key="control.name"
1313
:control="control"
1414
:submited="submited"
15-
@changed="valueChange"
15+
@change="valueChange"
1616
>
1717
<template v-slot:customField="props">
1818
<div
@@ -156,7 +156,7 @@ export default defineComponent({
156156
157157
function valueChange(changedValue: Record<string, unknown>) {
158158
Object.assign(formValues, changedValue);
159-
ctx.emit('changed', removeEmpty(formValues));
159+
ctx.emit('change', removeEmpty(formValues));
160160
}
161161
162162
function mapControls(empty?: boolean) {

src/components/dynamic-input/DynamicInput.vue

+17-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
InputType,
2525
} from '@/core/models';
2626
27-
import { isEmpty, entries, values, keys } from '@/core/utils/helpers';
27+
import { isEmpty, entries, values, keys, isEvent } from '@/core/utils/helpers';
2828
import { useInputEvents } from '@/composables/input-events';
2929
import { dynamicFormsSymbol } from '@/useApi';
3030
@@ -50,7 +50,7 @@ const props = {
5050
5151
export type ControlAttribute<T extends InputType> = {
5252
control: FormControl<T>;
53-
onChanged: (value: unknown) => void;
53+
onChange: (value: unknown) => void;
5454
};
5555
5656
export default defineComponent({
@@ -66,7 +66,7 @@ export default defineComponent({
6666
const attributes = computed(() => {
6767
return {
6868
control: props?.control,
69-
onChanged: valueChange,
69+
onChange: valueChange,
7070
};
7171
});
7272
@@ -142,12 +142,24 @@ export default defineComponent({
142142
}
143143
}
144144
145-
function valueChange(value) {
145+
function valueChange($event) {
146+
let value;
146147
const newValue = {};
148+
149+
if (isEvent($event)) {
150+
$event.stopPropagation();
151+
value =
152+
props.control.type === 'checkbox'
153+
? $event.target.checked
154+
: $event.target.value;
155+
} else {
156+
value = $event;
157+
}
158+
147159
if (props.control) {
148160
newValue[props.control.name] = value;
149161
validate();
150-
emit('changed', newValue);
162+
emit('change', newValue);
151163
}
152164
}
153165

src/composables/input-events.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@
33
import { watch } from 'vue';
44

55
export function useInputEvents(props: any, emit: any) {
6-
function onChange($event: any): void {
7-
const value =
8-
props.control.type === 'number'
9-
? parseFloat(`${$event.target.value}`)
10-
: $event.target.value;
6+
function onChange($event): void {
117
if (props.control) {
12-
props.control.value = value;
138
props.control.dirty = true;
149
}
15-
emit('changed', value);
1610
}
17-
function onCheck($event: any): void {
11+
function onCheck($event): void {
1812
if (props.control) {
19-
props.control.value = $event.target.checked;
2013
props.control.dirty = true;
2114
}
22-
emit('changed', $event.target.checked);
2315
}
2416
function onFocus(): void {
2517
emit('focus');
@@ -34,7 +26,7 @@ export function useInputEvents(props: any, emit: any) {
3426
watch(props, (form: any) => {
3527
if (form?.control && !form?.control?.dirty) {
3628
form.control.dirty = true;
37-
emit('changed', form.control.value);
29+
emit('change', form.control.value);
3830
}
3931
});
4032

src/core/utils/helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const slugify = (text: string): string =>
1414

1515
export const isArray = (a: any) => !!a && a.constructor === Array;
1616
export const isObject = (a: any) => !!a && a.constructor === Object;
17+
export const isEvent = (e: any) => !!e && e.constructor === Event;
1718

1819
export const isEmpty = (entry: any) => {
1920
if (isArray(entry)) {

0 commit comments

Comments
 (0)