textinput vue component
This commit is contained in:
parent
476eb042c1
commit
526b6c7653
@ -1,65 +1,75 @@
|
|||||||
import MaskedInput from 'vue-text-mask'
|
import MaskedInput, { conformToMask } from 'vue-text-mask'
|
||||||
import inputValidations from '../lib/input_validations'
|
import inputValidations from '../lib/input_validations'
|
||||||
|
|
||||||
const validationTypes = {
|
|
||||||
matchAnything: /^(?!\s*$).+/,
|
|
||||||
matchDigits: /^\d+$/,
|
|
||||||
matchNumbers: /^-?\d+\.?\d*$/,
|
|
||||||
matchUrl: /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/,
|
|
||||||
matchEmail: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'textinput',
|
name: 'textinput',
|
||||||
data: () =>({
|
|
||||||
valid: false,
|
|
||||||
filled: false,
|
|
||||||
showError: false,
|
|
||||||
showValid: false,
|
|
||||||
validationType: inputValidations.anything.match,
|
|
||||||
mask: inputValidations.anything.mask
|
|
||||||
}),
|
|
||||||
components: {
|
components: {
|
||||||
MaskedInput
|
MaskedInput
|
||||||
},
|
},
|
||||||
|
|
||||||
|
props: {
|
||||||
|
validation: {
|
||||||
|
type: String,
|
||||||
|
default: () => 'anything'
|
||||||
|
},
|
||||||
|
value: {
|
||||||
|
type: String,
|
||||||
|
default: () => ''
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
data: function () {
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
showError: false,
|
||||||
|
showValid: false,
|
||||||
|
mask: inputValidations[this.validation].mask
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
mounted: function () {
|
||||||
|
const value = this.$refs.input.value
|
||||||
|
if (value) {
|
||||||
|
this._checkIfValid({ value, invalidate: true })
|
||||||
|
this.value = conformToMask(value, this.mask).conformedValue
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
// When user types a character
|
// When user types a character
|
||||||
onInput: function (value) {
|
onInput: function (value) {
|
||||||
this._checkIfFilled(value)
|
this._checkIfValid({ value })
|
||||||
this._checkIfValid(value)
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// When field is blurred (un-focused)
|
// When field is blurred (un-focused)
|
||||||
onChange: function (value) {
|
onChange: function (e) {
|
||||||
this._checkIfInvalid(value)
|
// Only invalidate the field when it blurs
|
||||||
|
this._checkIfValid({ value: e.target.value, invalidate: true })
|
||||||
},
|
},
|
||||||
|
|
||||||
_checkIfFilled: function (value) {
|
//
|
||||||
this.filled = (value && value !== '') ? true : false
|
_checkIfValid: function ({ value, invalidate = false}) {
|
||||||
},
|
// Validate the value
|
||||||
|
|
||||||
_checkIfValid: function (value) {
|
|
||||||
const valid = this._validate(value)
|
const valid = this._validate(value)
|
||||||
|
|
||||||
|
// Show error messages or not
|
||||||
if (valid) {
|
if (valid) {
|
||||||
this.showError = false
|
this.showError = false
|
||||||
}
|
} else if (invalidate) {
|
||||||
this.valid = valid
|
|
||||||
this.showValid = valid
|
|
||||||
},
|
|
||||||
|
|
||||||
_checkIfInvalid: function (value) {
|
|
||||||
const valid = this._validate(value)
|
|
||||||
if (!valid) {
|
|
||||||
this.showError = true
|
this.showError = true
|
||||||
} else {
|
|
||||||
this.showError = false
|
|
||||||
}
|
}
|
||||||
this.valid = valid
|
this.valid = valid
|
||||||
this.showValid = valid
|
this.showValid = valid
|
||||||
},
|
},
|
||||||
|
|
||||||
_validate: function (value) {
|
_validate: function (value) {
|
||||||
return this.validationType.test(value)
|
// Strip out all the mask characters
|
||||||
|
let rawValue = inputValidations[this.validation].unmask.reduce((currentValue, character) => {
|
||||||
|
return currentValue.split(character).join('')
|
||||||
|
}, value)
|
||||||
|
|
||||||
|
return inputValidations[this.validation].match.test(rawValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user