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

Commit cddc0d6

Browse files
authored
Merge pull request #207 from asigloo/bugfix/event-obj-is-being-passed-as-a-value
Bugfix/event obj is being passed as a value
2 parents bdce974 + 229d196 commit cddc0d6

File tree

10 files changed

+58
-19
lines changed

10 files changed

+58
-19
lines changed

dev/typescript/App.vue

+10-3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import {
8181
pattern,
8282
ValidatorTrigger,
8383
ValidationTriggerTypes,
84+
TextAreaField,
8485
} from '../../src';
8586
/* } from '../../dist/as-dynamic-forms.esm'; */
8687
export default defineComponent({
@@ -89,10 +90,10 @@ export default defineComponent({
8990
const title = ref('Vue Dynamic Forms');
9091
const formValues = reactive({});
9192
let consoleOptions = ref();
92-
const emailValidator: FormValidator = {
93+
const emailValidator: FormValidator = Validator({
9394
validator: email,
9495
text: 'Email format is incorrect',
95-
};
96+
});
9697
9798
const emailUniquenessValidator: FormValidator = {
9899
validator: value =>
@@ -118,6 +119,7 @@ export default defineComponent({
118119
'name',
119120
'email',
120121
'password',
122+
'bio',
121123
'console',
122124
'games',
123125
'stock',
@@ -151,6 +153,11 @@ export default defineComponent({
151153
autocomplete: 'current-password',
152154
validations: [passwordValidator],
153155
}),
156+
bio: TextAreaField({
157+
label: 'Bio',
158+
cols: 30,
159+
rows: 4,
160+
}),
154161
stock: NumberField({
155162
label: 'Stock',
156163
customClass: 'w-1/2 pr-4',
@@ -246,7 +253,7 @@ export default defineComponent({
246253
247254
function valueChanged(values) {
248255
Object.assign(formValues, values);
249-
/* console.log('Values', values); */
256+
console.log('Values', values);
250257
}
251258
252259
function handleError(errors) {

src/components/dynamic-form/DynamicForm.vue

+8-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ import {
6363
InputEvent,
6464
} from '@/core/models';
6565
import { dynamicFormsSymbol } from '@/useApi';
66-
import { deepClone, hasValue, removeEmpty } from '@/core/utils/helpers';
66+
import {
67+
deepClone,
68+
hasValue,
69+
isEvent,
70+
removeEmpty,
71+
} from '@/core/utils/helpers';
6772
import { FieldControl } from '@/core/factories';
6873
6974
const props = {
@@ -192,8 +197,8 @@ export default defineComponent({
192197
return updatedCtrl;
193198
}
194199
195-
function valueChange(event: Record<string, unknown>) {
196-
if (event && hasValue(event.value)) {
200+
function valueChange(event: any) {
201+
if (hasValue(event.value)) {
197202
const updatedCtrl = findControlByName(event.name);
198203
if (updatedCtrl) {
199204
updatedCtrl.value = event.value as string;

src/components/dynamic-input/DynamicInput.vue

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
InputEvent,
2929
} from '@/core/models';
3030
31-
import { isArray, isObject } from '@/core/utils/helpers';
31+
import { isArray, isEvent, isObject } from '@/core/utils/helpers';
3232
import { useInputEvents } from '@/composables/input-events';
3333
3434
const components = {
@@ -63,7 +63,7 @@ export default defineComponent({
6363
components,
6464
props,
6565
setup(props, { emit, slots }) {
66-
const { onFocus, onBlur } = useInputEvents(props?.control, emit);
66+
const { onFocus, onInput, onChange, onBlur } = useInputEvents(props, emit);
6767
6868
let component;
6969
@@ -106,8 +106,8 @@ export default defineComponent({
106106
return [classes, props?.control?.customClass];
107107
});
108108
109-
function valueChange($event) {
110-
emit('change', $event);
109+
function valueChange(event) {
110+
emit('change', event);
111111
}
112112
113113
return () => {
@@ -180,7 +180,7 @@ export default defineComponent({
180180
},
181181
slots.customField({
182182
control: props.control,
183-
onChange: valueChange,
183+
onChange: onInput,
184184
onFocus,
185185
onBlur,
186186
}),

src/components/number-input/NumberInput.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default defineComponent({
1616
name: 'asNumberInput',
1717
props,
1818
setup(props, { emit }) {
19-
const { onInput, onFocus, onBlur } = useInputEvents(props, emit);
19+
const { onInput, onChange, onFocus, onBlur } = useInputEvents(props, emit);
2020
const {
2121
isRequired,
2222
errorMessages,
@@ -44,6 +44,7 @@ export default defineComponent({
4444
onFocus,
4545
onBlur,
4646
onInput,
47+
onChange,
4748
}),
4849
isPendingValidation.value
4950
? null

src/components/radio-input/RadioInput.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default defineComponent({
1616
name: 'asRadioInput',
1717
props,
1818
setup(props, { emit }) {
19-
const { onInput, onFocus, onBlur } = useInputEvents(props, emit);
19+
const { onChange, onInput, onFocus, onBlur } = useInputEvents(props, emit);
2020
2121
const { errorMessages, isPendingValidation } = useInputValidation(
2222
props,
@@ -35,6 +35,7 @@ export default defineComponent({
3535
onFocus,
3636
onBlur,
3737
onInput,
38+
onChange,
3839
}),
3940
h(
4041
'label',

src/components/select-input/SelectInput.vue

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export default defineComponent({
1818
props,
1919
setup(props, { emit }) {
2020
return () => {
21-
const { onInput, onFocus, onBlur } = useInputEvents(props, emit);
21+
const { onInput, onChange, onFocus, onBlur } = useInputEvents(
22+
props,
23+
emit,
24+
);
2225
const {
2326
isRequired,
2427
errorMessages,
@@ -61,6 +64,7 @@ export default defineComponent({
6164
onFocus,
6265
onBlur,
6366
onInput,
67+
onChange,
6468
},
6569
options,
6670
),

src/components/text-area-input/TextAreaInput.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default defineComponent({
1515
name: 'asTextAreaInput',
1616
props,
1717
setup(props, { emit }) {
18-
const { onInput, onFocus, onBlur } = useInputEvents(props, emit);
18+
const { onInput, onChange, onFocus, onBlur } = useInputEvents(props, emit);
1919
const {
2020
isRequired,
2121
errorMessages,
@@ -41,6 +41,7 @@ export default defineComponent({
4141
onFocus,
4242
onBlur,
4343
onInput,
44+
onChange,
4445
}),
4546
isPendingValidation.value
4647
? null

src/components/text-input/TextInput.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default defineComponent({
2525
name: 'asTextInput',
2626
props,
2727
setup(props, { emit }) {
28-
const { onInput, onFocus, onBlur, getClasses } = useInputEvents(
28+
const { onInput, onChange, onFocus, onBlur, getClasses } = useInputEvents(
2929
props,
3030
emit,
3131
);
@@ -52,6 +52,7 @@ export default defineComponent({
5252
onFocus,
5353
onBlur,
5454
onInput,
55+
onChange,
5556
}),
5657
isPendingValidation.value
5758
? null

src/composables/input-events.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ValidationTriggerTypes } from '@/core/models';
66

77
interface InputEventsComposition {
88
onInput: ($event: Event) => void;
9+
onChange: ($event: Event) => void;
910
onCheck: ($event: Event) => void;
1011
onFocus: () => void;
1112
onBlur: () => void;
@@ -18,9 +19,8 @@ export function useInputEvents(props: any, emit: any): InputEventsComposition {
1819
function onInput($event: Event): void {
1920
const element = $event.target as HTMLInputElement;
2021

22+
$event.stopImmediatePropagation();
2123
if (props.control && hasValue(element.value)) {
22-
$event.stopImmediatePropagation();
23-
2424
if (
2525
(!props.control.valid &&
2626
props.control.validationTrigger.type ===
@@ -37,6 +37,10 @@ export function useInputEvents(props: any, emit: any): InputEventsComposition {
3737
});
3838
}
3939
}
40+
function onChange($event: Event): void {
41+
$event.stopImmediatePropagation();
42+
$event.preventDefault();
43+
}
4044
function onCheck($event: Event): void {
4145
const element = $event.target as HTMLInputElement;
4246
if (props.control) {
@@ -68,7 +72,7 @@ export function useInputEvents(props: any, emit: any): InputEventsComposition {
6872
watch(
6973
() => props?.control?.value,
7074
(curr, prev) => {
71-
if (prev === undefined && hasValue(curr)) {
75+
if (prev !== undefined && hasValue(curr) && curr !== prev) {
7276
emit('change', {
7377
name: props.control.name,
7478
value: props.control.value,
@@ -80,6 +84,7 @@ export function useInputEvents(props: any, emit: any): InputEventsComposition {
8084
return {
8185
onFocus,
8286
onInput,
87+
onChange,
8388
onBlur,
8489
onCheck,
8590
getClasses,

src/core/factories.ts

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
FormValidator,
1515
ValidationTriggerTypes,
1616
ValidationTrigger,
17+
TextAreaInput,
1718
} from './models';
1819

1920
const EMPTY_CONTROL = {
@@ -62,6 +63,19 @@ export const TextField = ({
6263
type: FieldTypes.TEXT,
6364
});
6465

66+
export const TextAreaField = ({
67+
value,
68+
cols,
69+
rows,
70+
...rest
71+
}: Partial<TextAreaInput>): TextAreaInput => ({
72+
...FieldBase(rest),
73+
value,
74+
cols,
75+
rows,
76+
type: FieldTypes.TEXTAREA,
77+
});
78+
6579
export const EmailField = ({
6680
value,
6781
...rest

0 commit comments

Comments
 (0)