2019-10-07 17:24:30 +03:00
|
|
|
<template>
|
2019-10-09 06:54:16 +03:00
|
|
|
<form
|
|
|
|
@submit.prevent="formSubmitted"
|
|
|
|
>
|
2019-10-07 17:24:30 +03:00
|
|
|
<slot />
|
|
|
|
</form>
|
|
|
|
</template>
|
2019-10-09 06:54:16 +03:00
|
|
|
|
|
|
|
<script>
|
2019-10-30 06:33:31 +03:00
|
|
|
import { shallowEqualObjects } from './libs/utils'
|
|
|
|
|
2019-10-09 06:54:16 +03:00
|
|
|
export default {
|
|
|
|
provide () {
|
|
|
|
return {
|
|
|
|
formulateFormSetter: this.setFieldValue,
|
|
|
|
formulateFormRegister: this.register
|
|
|
|
}
|
|
|
|
},
|
|
|
|
name: 'FormulateForm',
|
|
|
|
model: {
|
|
|
|
prop: 'formulateValue',
|
|
|
|
event: 'input'
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
name: {
|
|
|
|
type: [String, Boolean],
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
formulateValue: {
|
|
|
|
type: Object,
|
|
|
|
default: () => ({})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
2019-11-05 18:52:05 +03:00
|
|
|
registry: {},
|
|
|
|
internalFormModelProxy: {}
|
2019-10-09 06:54:16 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2019-10-30 06:33:31 +03:00
|
|
|
hasFormulateValue () {
|
|
|
|
return this.formulateValue && typeof this.formulateValue === 'object'
|
|
|
|
},
|
|
|
|
isVmodeled () {
|
|
|
|
return !!(this.$options.propsData.hasOwnProperty('formulateValue') &&
|
|
|
|
this._events &&
|
|
|
|
Array.isArray(this._events.input) &&
|
|
|
|
this._events.input.length)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
formulateValue: {
|
|
|
|
handler (newValue, oldValue) {
|
|
|
|
if (this.isVmodeled &&
|
|
|
|
newValue &&
|
|
|
|
typeof newValue === 'object'
|
|
|
|
) {
|
|
|
|
for (const field in newValue) {
|
2019-11-05 18:52:05 +03:00
|
|
|
if (this.registry.hasOwnProperty(field) &&
|
|
|
|
!shallowEqualObjects(newValue[field], this.internalFormModelProxy[field]) &&
|
|
|
|
!shallowEqualObjects(newValue[field], this.registry[field].internalModelProxy[field])
|
|
|
|
) {
|
|
|
|
this.setFieldValue(field, newValue[field])
|
2019-10-30 06:33:31 +03:00
|
|
|
this.registry[field].context.model = newValue[field]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-11-05 18:52:05 +03:00
|
|
|
deep: true,
|
|
|
|
immediate: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created () {
|
|
|
|
if (this.$options.propsData.hasOwnProperty('formulateValue')) {
|
|
|
|
this.internalFormModelProxy = Object.assign({}, this.formulateValue)
|
2019-10-09 06:54:16 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
setFieldValue (field, value) {
|
2019-11-05 18:52:05 +03:00
|
|
|
Object.assign(this.internalFormModelProxy, { [field]: value })
|
|
|
|
this.$emit('input', Object.assign({}, this.internalFormModelProxy))
|
2019-10-09 06:54:16 +03:00
|
|
|
},
|
|
|
|
register (field, component) {
|
|
|
|
this.registry[field] = component
|
2019-10-30 06:33:31 +03:00
|
|
|
if (!component.$options.propsData.hasOwnProperty('formulateValue') && this.hasFormulateValue && this.formulateValue[field]) {
|
|
|
|
// In the case that the form is carrying an initial value and the
|
|
|
|
// element is not, set it directly.
|
|
|
|
component.context.model = this.formulateValue[field]
|
2019-10-31 17:24:18 +03:00
|
|
|
} else if (component.$options.propsData.hasOwnProperty('formulateValue') && !shallowEqualObjects(component.internalModelProxy, this.formulateValue[field])) {
|
|
|
|
this.setFieldValue(field, component.internalModelProxy)
|
2019-10-30 06:33:31 +03:00
|
|
|
}
|
2019-10-31 17:24:18 +03:00
|
|
|
},
|
|
|
|
formSubmitted () {
|
|
|
|
// perform validation here
|
|
|
|
this.$emit('submit', this.formModel)
|
2019-10-09 06:54:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|