some fixes to the input

This commit is contained in:
Andrew Croce 2018-08-09 13:46:20 -04:00
parent c2851c2e86
commit 86088ac150

View File

@ -14,7 +14,7 @@ export default {
type: String, type: String,
default: () => 'anything' default: () => 'anything'
}, },
value: { setValue: {
type: String, type: String,
default: () => '' default: () => ''
} }
@ -27,21 +27,24 @@ export default {
mask: inputValidations[this.validation].mask, mask: inputValidations[this.validation].mask,
pipe: inputValidations[this.validation].pipe || undefined, pipe: inputValidations[this.validation].pipe || undefined,
keepCharPositions: inputValidations[this.validation].keepCharPositions || false, keepCharPositions: inputValidations[this.validation].keepCharPositions || false,
renderedValue: this.value value: this.setValue
} }
}, },
mounted: function () { mounted: function () {
const value = this.$refs.input.value if (this.value && this.mask) {
if (value) { this._checkIfValid({ value: this.value, invalidate: true })
this._checkIfValid({ value, invalidate: true }) this.value = conformToMask(this.value, this.mask).conformedValue
this.renderedValue = conformToMask(value, this.mask).conformedValue
} }
}, },
methods: { methods: {
// When user types a character // When user types a character
onInput: function (value) { onInput: function (e) {
// When we use the native textarea element, we receive an event object
// When we use the masked-input component, we receive the value directly
const value = typeof e === 'object' ? e.target.value : e
this.value = value
this._checkIfValid({ value }) this._checkIfValid({ value })
}, },
@ -72,13 +75,14 @@ export default {
}) })
}, },
_validate: function (value) { _rawValue: function (value) {
// Strip out all the mask characters return inputValidations[this.validation].unmask.reduce((currentValue, character) => {
let rawValue = inputValidations[this.validation].unmask.reduce((currentValue, character) => {
return currentValue.split(character).join('') return currentValue.split(character).join('')
}, value) }, value)
},
return inputValidations[this.validation].match.test(rawValue) _validate: function (value) {
return inputValidations[this.validation].match.test(this._rawValue(value))
} }
} }
} }