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

Commit

Permalink
Merge pull request #209 from asigloo/bugfix/fix-vue-warns
Browse files Browse the repository at this point in the history
fix: removed all warnings
  • Loading branch information
alvarosabu authored Dec 19, 2020
2 parents b1eeb5f + 5661b79 commit 1274df7
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 16 deletions.
2 changes: 1 addition & 1 deletion dev/vue/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="card p-6 mr-4">
<dynamic-form
:form="form"
@submited="handleSubmit"
@submitted="handleSubmit"
@changed="valueChanged"
@error="handleError"
>
Expand Down
1 change: 1 addition & 0 deletions src/components/checkbox-input/CheckboxInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const props = {
export default defineComponent({
name: 'asCheckboxInput',
inheritAttrs: false,
props,
setup(props, { emit }) {
const { onCheck, onFocus, onBlur } = useInputEvents(props, emit);
Expand Down
21 changes: 12 additions & 9 deletions src/components/dynamic-form/DynamicForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
v-for="control in controls"
:key="control.name"
:control="control"
:submited="submited"
:forceValidation="forceValidation"
@change="valueChange"
@blur="onBlur"
Expand Down Expand Up @@ -61,15 +60,12 @@ import {
InputType,
ValidationEvent,
InputEvent,
FormChanges,
} from '@/core/models';
import { dynamicFormsSymbol } from '@/useApi';
import {
deepClone,
hasValue,
isEvent,
removeEmpty,
} from '@/core/utils/helpers';
import { deepClone, hasValue, removeEmpty } from '@/core/utils/helpers';
import { FieldControl } from '@/core/factories';
import { useDebounceFn } from '@/composables/use-debounce';
const props = {
form: {
Expand All @@ -86,6 +82,7 @@ const components = {
*/
export default defineComponent({
name: 'asDynamicForm',
inheritAttrs: false,
props,
components,
setup(props, ctx) {
Expand Down Expand Up @@ -197,14 +194,20 @@ export default defineComponent({
return updatedCtrl;
}
function valueChange(event: any) {
function emitChanges(changes: FormChanges) {
ctx.emit('change', changes);
}
const debounceEmitChanges = useDebounceFn(emitChanges, 300);
function valueChange(event: InputEvent) {
if (hasValue(event.value)) {
const updatedCtrl = findControlByName(event.name);
if (updatedCtrl) {
updatedCtrl.value = event.value as string;
updatedCtrl.dirty = true;
}
ctx.emit('change', formValues.value);
debounceEmitChanges(formValues.value);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/components/dynamic-input/DynamicInput.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
/* eslint-disable @typescript-eslint/no-use-before-define */
import { defineComponent, PropType, computed, h, inject } from 'vue';
import { defineComponent, PropType, computed, h } from 'vue';
import TextInputComponent from '../text-input/TextInput.vue';
import SelectInputComponent from '../select-input/SelectInput.vue';
import TextAreaInputComponent from '../text-area-input/TextAreaInput.vue';
Expand All @@ -28,7 +28,7 @@ import {
InputEvent,
} from '@/core/models';
import { isArray, isEvent, isObject } from '@/core/utils/helpers';
import { isArray, isObject } from '@/core/utils/helpers';
import { useInputEvents } from '@/composables/input-events';
const components = {
Expand Down Expand Up @@ -61,9 +61,10 @@ export type ControlAttribute<T extends InputType> = {
export default defineComponent({
name: 'asDynamicInput',
components,
inheritAttrs: false,
props,
setup(props, { emit, slots }) {
const { onFocus, onInput, onChange, onBlur } = useInputEvents(props, emit);
const { onFocus, onInput, onBlur } = useInputEvents(props, emit);
let component;
Expand Down
1 change: 1 addition & 0 deletions src/components/number-input/NumberInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const props = {
export default defineComponent({
name: 'asNumberInput',
inheritAttrs: false,
props,
setup(props, { emit }) {
const { onInput, onChange, onFocus, onBlur } = useInputEvents(props, emit);
Expand Down
1 change: 1 addition & 0 deletions src/components/radio-input/RadioInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const props = {
export default defineComponent({
name: 'asRadioInput',
inheritAttrs: false,
props,
setup(props, { emit }) {
const { onChange, onInput, onFocus, onBlur } = useInputEvents(props, emit);
Expand Down
1 change: 1 addition & 0 deletions src/components/select-input/SelectInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const props = {
export default defineComponent({
name: 'asSelectInput',
inheritAttrs: false,
props,
setup(props, { emit }) {
return () => {
Expand Down
1 change: 1 addition & 0 deletions src/components/text-area-input/TextAreaInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const props = {
export default defineComponent({
name: 'asTextAreaInput',
inheritAttrs: false,
props,
setup(props, { emit }) {
const { onInput, onChange, onFocus, onBlur } = useInputEvents(props, emit);
Expand Down
1 change: 1 addition & 0 deletions src/components/text-input/TextInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const props = {
export default defineComponent({
name: 'asTextInput',
inheritAttrs: false,
props,
setup(props, { emit }) {
const { onInput, onChange, onFocus, onBlur, getClasses } = useInputEvents(
Expand Down
8 changes: 6 additions & 2 deletions src/composables/input-events.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { computed, ComputedRef, watch } from 'vue';
import { hasValue } from '../core/utils/helpers';

Expand All @@ -13,7 +14,7 @@ interface InputEventsComposition {
getClasses: ComputedRef<(string | { [key: string]: boolean })[]>;
}

export function useInputEvents(props: any, emit: any): InputEventsComposition {
export function useInputEvents(props, emit): InputEventsComposition {
const { validate, getValidationClasses } = useInputValidation(props, emit);

function onInput($event: Event): void {
Expand Down Expand Up @@ -72,13 +73,16 @@ export function useInputEvents(props: any, emit: any): InputEventsComposition {
watch(
() => props?.control?.value,
(curr, prev) => {
if (prev !== undefined && hasValue(curr) && curr !== prev) {
if (hasValue(curr) && curr !== prev) {
emit('change', {
name: props.control.name,
value: props.control.value,
});
}
},
{
immediate: true,
},
);

return {
Expand Down
55 changes: 55 additions & 0 deletions src/composables/use-debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
export type FunctionArgs<Args extends any[] = any[], Return = void> = (
...args: Args
) => Return;

export interface FunctionWrapperOptions<
Args extends any[] = any[],
This = any
> {
fn: FunctionArgs<Args, This>;
args: Args;
thisArg: This;
}

export type EventFilter<Args extends any[] = any[], This = any> = (
invoke: () => void,
options: FunctionWrapperOptions<Args, This>,
) => void;

/**
* @internal
*/
export function createFilterWrapper<T extends FunctionArgs>(
filter: EventFilter,
fn: T,
) {
function wrapper(this: any, ...args: any[]) {
filter(() => fn.apply(this, args), { fn, thisArg: this, args });
}

return (wrapper as any) as T;
}

export const bypassFilter: EventFilter = invoke => {
return invoke();
};

export function debounceFilter(ms: number): EventFilter {
if (ms <= 0) return bypassFilter;

let timer: ReturnType<typeof setTimeout> | undefined;

const filter: EventFilter = invoke => {
if (timer) clearTimeout(timer);

timer = setTimeout(invoke, ms);
};

return filter;
}

export function useDebounceFn<T extends FunctionArgs>(fn: T, ms = 200): T {
return createFilterWrapper(debounceFilter(ms), fn);
}
6 changes: 5 additions & 1 deletion src/core/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export type FormFields = {
[key: string]: InputType;
};

export type FormChanges = {
[key: string]: string | number | null | undefined;
};
export interface DynamicForm {
id: string;
fields: FormFields;
Expand All @@ -36,7 +39,7 @@ export type ValidationErrors = {
};

export type BindingObject = {
[key: string]: any;
[key: string]: never;
};

export interface ValidatorFn {
Expand All @@ -53,6 +56,7 @@ export interface FormValidator {

export interface InputEvent {
name: string;
value?: unknown;
}

export type ValidationEvent = InputEvent & {
Expand Down

0 comments on commit 1274df7

Please sign in to comment.