Refactor emit field change into a utility function

This commit is contained in:
leigh-mil 2019-04-04 10:31:02 -04:00
parent 1aaf4421ab
commit 3f2beb44b2
6 changed files with 18 additions and 22 deletions

View File

@ -1,3 +1,5 @@
import { emitFieldChange } from '../lib/emitters'
export default { export default {
name: 'checkboxinput', name: 'checkboxinput',
@ -7,11 +9,7 @@ export default {
methods: { methods: {
onInput: function(e) { onInput: function(e) {
this.$root.$emit('field-change', { emitFieldChange(this, { value: e.target.checked, name: this.name })
value: e.target.checked,
name: this.name,
parent_uid: this.$parent._uid,
})
}, },
}, },
} }

View File

@ -1,5 +1,6 @@
import Vue from 'vue' import Vue from 'vue'
import { getDaysInMonth } from 'date-fns' import { getDaysInMonth } from 'date-fns'
import { emitFieldChange } from '../lib/emitters'
var paddedNumber = function(number) { var paddedNumber = function(number) {
if ((number + '').length === 1) { if ((number + '').length === 1) {
@ -135,11 +136,7 @@ export default {
methods: { methods: {
_emitChange: function(name, value, valid) { _emitChange: function(name, value, valid) {
this.$root.$emit('field-change', { emitFieldChange(this, { value, name })
value,
name,
parent_uid: this.$parent && this.$parent._uid,
})
}, },
}, },

View File

@ -1,5 +1,6 @@
import optionsinput from '../components/options_input' import optionsinput from '../components/options_input'
import textinput from '../components/text_input' import textinput from '../components/text_input'
import { emitFieldChange } from '../lib/emitters'
export default { export default {
name: 'multicheckboxinput', name: 'multicheckboxinput',
@ -40,11 +41,7 @@ export default {
methods: { methods: {
onInput: function(e) { onInput: function(e) {
this.$root.$emit('field-change', { emitFieldChange(this, { value: e.target.value, name: this.name })
value: e.target.value,
name: this.name,
parent_uid: this.$parent._uid,
})
this.showError = false this.showError = false
this.showValid = true this.showValid = true
}, },

View File

@ -1,3 +1,5 @@
import { emitFieldChange } from '../lib/emitters'
export default { export default {
name: 'optionsinput', name: 'optionsinput',
@ -21,11 +23,7 @@ export default {
methods: { methods: {
onInput: function(e) { onInput: function(e) {
this.$root.$emit('field-change', { emitFieldChange(this, { value: e.target.value, name: this.name })
value: e.target.value,
name: this.name,
parent_uid: this.$parent._uid,
})
this.showError = false this.showError = false
this.showValid = true this.showValid = true
}, },

View File

@ -1,6 +1,7 @@
import MaskedInput, { conformToMask } from 'vue-text-mask' import MaskedInput, { conformToMask } from 'vue-text-mask'
import inputValidations from '../lib/input_validations' import inputValidations from '../lib/input_validations'
import { formatDollars } from '../lib/dollars' import { formatDollars } from '../lib/dollars'
import { emitFieldChange } from '../lib/emitters'
export default { export default {
name: 'textinput', name: 'textinput',
@ -124,11 +125,10 @@ export default {
this.showValid = this.value != '' && valid this.showValid = this.value != '' && valid
// Emit a change event // Emit a change event
this.$root.$emit('field-change', { emitFieldChange(this, {
value: this._rawValue(value), value: this._rawValue(value),
valid, valid,
name: this.name, name: this.name,
parent_uid: this.$parent._uid,
}) })
}, },

6
js/lib/emitters.js Normal file
View File

@ -0,0 +1,6 @@
export const emitFieldChange = (el, data) => {
el.$root.$emit('field-change', {
...data,
parent_uid: el.$parent && el.$parent._uid,
})
}