1
0
mirror of synced 2024-11-22 05:16:05 +03:00

refactor: Renamed register() method to add(), old add() method removed

This commit is contained in:
Zaytsev Kirill 2020-10-28 22:46:29 +03:00
parent edf1fc6340
commit 904119deb8
3 changed files with 39 additions and 48 deletions

View File

@ -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<string, string[]> }): void {

View File

@ -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) {

View File

@ -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