Move js to form component

This commit is contained in:
George Drummond
2019-05-03 14:34:41 -04:00
parent 094550f99c
commit 334f3d8ed3
3 changed files with 33 additions and 41 deletions

View File

@@ -1,23 +1,53 @@
export default {
props: {
initialSelectedSection: String,
},
mounted: function() {
this.$root.$on('field-change', this.handleFieldChange)
},
created: function() {
this.$root.$on('field-mount', this.handleFieldMount)
},
methods: {
handleFieldChange: function(event) {
const { value, name } = event
const { name, valid, parent_uid } = event
if (typeof this[name] !== undefined) {
this[name] = value
if (event['parent_uid'] === this._uid) {
this.fields[name] = valid
if (parent_uid === this._uid) {
this.changed = true
}
}
this.validateForm()
},
handleFieldMount: function(event) {
const { name, optional } = event
this.fields[name] = optional
},
validateForm: function() {
const valid = !Object.values(this.fields).some(field => field === false)
this.invalid = !valid
return valid
},
handleSubmit: function(event) {
if (this.invalid) {
event.preventDefault()
}
},
},
data: function() {
return {
changed: this.hasChanges,
fields: {},
invalid: true,
}
},