atst/js/mixins/form.js
leigh-mil bc0382834b Remove old field-mount and field-change emitters and listeners.
Replace FormMixin with new functionality.
2019-11-19 14:49:11 -05:00

55 lines
993 B
JavaScript

export default {
props: {
initialSelectedSection: String,
hasChanges: {
type: Boolean,
default: false,
},
enableSave: {
type: Boolean,
default: false,
},
},
data: function() {
return {
changed: this.hasChanges,
valid: false,
}
},
mounted: function() {
this.$on('field-change', this.handleFieldChange)
this.valid = this.validateFields()
},
methods: {
handleFieldChange: function(event) {
this.valid = this.validateFields()
this.changed = true
},
validateFields: function() {
return this.$children.every(child => child.valid)
},
handleSubmit: function(event) {
if (!this.valid) {
event.preventDefault()
}
},
},
computed: {
canSave: function() {
if (this.changed && this.valid) {
return true
} else if (this.enableSave && this.valid) {
return true
} else {
return false
}
},
},
}