From 904119deb809667ca835cb84bd5b4644a19d2c57 Mon Sep 17 00:00:00 2001 From: Zaytsev Kirill Date: Wed, 28 Oct 2020 22:46:29 +0300 Subject: [PATCH] refactor: Renamed register() method to add(), old add() method removed --- src/FormularioForm.vue | 15 ++++----- src/FormularioInput.vue | 5 +-- src/form/registry.ts | 67 ++++++++++++++++++----------------------- 3 files changed, 39 insertions(+), 48 deletions(-) diff --git a/src/FormularioForm.vue b/src/FormularioForm.vue index 3935375..4dfc006 100644 --- a/src/FormularioForm.vue +++ b/src/FormularioForm.vue @@ -126,7 +126,7 @@ export default class FormularioForm extends Vue { @Provide('formularioRegister') register (field: string, component: FormularioInput): void { - this.registry.register(field, component) + this.registry.add(field, component) } @Provide('formularioDeregister') @@ -154,7 +154,7 @@ export default class FormularioForm extends Vue { const newValue = getNested(values, registryKey) if (!shallowEqualObjects(newValue, oldValue)) { - this.setFieldValue(registryKey, newValue, false) + this.setFieldValue(registryKey, newValue) proxyHasChanges = true } @@ -171,8 +171,7 @@ export default class FormularioForm extends Vue { } } - @Provide('formularioSetter') - setFieldValue (field: string, value: any, emit = true): void { + setFieldValue (field: string, value: any): void { if (value === undefined) { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { [field]: value, ...proxy } = this.proxy @@ -180,10 +179,12 @@ export default class FormularioForm extends Vue { } else { setNested(this.proxy, field, value) } + } - if (emit) { - this.$emit('input', Object.assign({}, this.proxy)) - } + @Provide('formularioSetter') + setFieldValueAndEmit (field: string, value: any): void { + this.setFieldValue(field, value) + this.$emit('input', { ...this.proxy }) } setErrors ({ formErrors, inputErrors }: { formErrors?: string[]; inputErrors?: Record }): void { diff --git a/src/FormularioInput.vue b/src/FormularioInput.vue index c2247a3..bbfc003 100644 --- a/src/FormularioInput.vue +++ b/src/FormularioInput.vue @@ -69,10 +69,7 @@ export default class FormularioInput extends Vue { get model (): any { const model = this.hasModel ? 'value' : 'proxy' - if (this[model] === undefined) { - return '' - } - return this[model] + return this[model] !== undefined ? this[model] : '' } set model (value: any) { diff --git a/src/form/registry.ts b/src/form/registry.ts index 8319922..98e917b 100644 --- a/src/form/registry.ts +++ b/src/form/registry.ts @@ -20,10 +20,37 @@ export default class Registry { } /** - * Add an item to the registry. + * Fully register a component. + * @param {string} field name of the field. + * @param {FormularioForm} component the actual component instance. */ - add (name: string, component: FormularioInput): void { - this.registry.set(name, component) + add (field: string, component: FormularioInput): void { + if (this.registry.has(field)) { + return + } + this.registry.set(field, component) + const hasModel = has(component.$options.propsData || {}, 'value') + if ( + !hasModel && + // @ts-ignore + this.ctx.hasInitialValue && + // @ts-ignore + getNested(this.ctx.initialValues, field) !== undefined + ) { + // In the case that the form is carrying an initial value and the + // element is not, set it directly. + // @ts-ignore + component.context.model = getNested(this.ctx.initialValues, field) + } else if ( + hasModel && + // @ts-ignore + !shallowEqualObjects(component.proxy, getNested(this.ctx.initialValues, field)) + ) { + // In this case, the field is v-modeled or has an initial value and the + // form has no value or a different value, so use the field value + // @ts-ignore + this.ctx.setFieldValueAndEmit(field, component.proxy) + } } /** @@ -103,40 +130,6 @@ export default class Registry { return Array.from(this.registry.keys()) } - /** - * Fully register a component. - * @param {string} field name of the field. - * @param {FormularioForm} component the actual component instance. - */ - register (field: string, component: FormularioInput): void { - if (this.registry.has(field)) { - return - } - this.registry.set(field, component) - const hasModel = has(component.$options.propsData || {}, 'value') - if ( - !hasModel && - // @ts-ignore - this.ctx.hasInitialValue && - // @ts-ignore - getNested(this.ctx.initialValues, field) !== undefined - ) { - // In the case that the form is carrying an initial value and the - // element is not, set it directly. - // @ts-ignore - component.context.model = getNested(this.ctx.initialValues, field) - } else if ( - hasModel && - // @ts-ignore - !shallowEqualObjects(component.proxy, getNested(this.ctx.initialValues, field)) - ) { - // In this case, the field is v-modeled or has an initial value and the - // form has no value or a different value, so use the field value - // @ts-ignore - this.ctx.setFieldValue(field, component.proxy) - } - } - /** * Reduce the registry. * @param {function} callback