1
0
mirror of synced 2025-02-16 20:53:13 +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') @Provide('formularioRegister')
register (field: string, component: FormularioInput): void { register (field: string, component: FormularioInput): void {
this.registry.register(field, component) this.registry.add(field, component)
} }
@Provide('formularioDeregister') @Provide('formularioDeregister')
@ -154,7 +154,7 @@ export default class FormularioForm extends Vue {
const newValue = getNested(values, registryKey) const newValue = getNested(values, registryKey)
if (!shallowEqualObjects(newValue, oldValue)) { if (!shallowEqualObjects(newValue, oldValue)) {
this.setFieldValue(registryKey, newValue, false) this.setFieldValue(registryKey, newValue)
proxyHasChanges = true proxyHasChanges = true
} }
@ -171,8 +171,7 @@ export default class FormularioForm extends Vue {
} }
} }
@Provide('formularioSetter') setFieldValue (field: string, value: any): void {
setFieldValue (field: string, value: any, emit = true): void {
if (value === undefined) { if (value === undefined) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const { [field]: value, ...proxy } = this.proxy const { [field]: value, ...proxy } = this.proxy
@ -180,10 +179,12 @@ export default class FormularioForm extends Vue {
} else { } else {
setNested(this.proxy, field, value) setNested(this.proxy, field, value)
} }
}
if (emit) { @Provide('formularioSetter')
this.$emit('input', Object.assign({}, this.proxy)) 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 { 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 { get model (): any {
const model = this.hasModel ? 'value' : 'proxy' const model = this.hasModel ? 'value' : 'proxy'
if (this[model] === undefined) { return this[model] !== undefined ? this[model] : ''
return ''
}
return this[model]
} }
set model (value: any) { 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 { add (field: string, component: FormularioInput): void {
this.registry.set(name, component) 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()) 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. * Reduce the registry.
* @param {function} callback * @param {function} callback