vuetify
Version:
Vue.js 2 Semantic Component Framework
145 lines (121 loc) • 3.96 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _console = require('../util/console');
exports.default = {
name: 'validatable',
data: function data() {
return {
errorBucket: [],
hasFocused: false,
hasInput: false,
shouldValidate: false,
valid: false
};
},
props: {
error: {
type: Boolean
},
errorMessages: {
type: [String, Array],
default: function _default() {
return [];
}
},
rules: {
type: Array,
default: function _default() {
return [];
}
},
validateOnBlur: Boolean
},
computed: {
validations: function validations() {
if (!Array.isArray(this.errorMessages)) {
return [this.errorMessages];
} else if (this.errorMessages.length > 0) {
return this.errorMessages;
} else if (this.shouldValidate) {
return this.errorBucket;
} else {
return [];
}
},
hasError: function hasError() {
return this.validations.length > 0 || this.errorMessages.length > 0 || this.error;
}
},
watch: {
rules: {
handler: function handler(newVal, oldVal) {
// TODO: This handler seems to trigger when input changes, even though
// rules array stays the same? Solved it like this for now
if (newVal.length === oldVal.length) return;
this.validate();
},
deep: true
},
inputValue: function inputValue(val) {
// If it's the first time we're setting input,
// mark it with hasInput
if (!!val && !this.hasInput) this.hasInput = true;
if (this.hasInput && !this.validateOnBlur) this.shouldValidate = true;
},
isFocused: function isFocused(val) {
// If we're not focused, and it's the first time
// we're defocusing, set shouldValidate to true
if (!val && !this.hasFocused) {
this.hasFocused = true;
this.shouldValidate = true;
this.$emit('update:error', this.errorBucket.length > 0);
}
},
hasError: function hasError(val) {
if (this.shouldValidate) {
this.$emit('update:error', val);
}
},
error: function error(val) {
this.shouldValidate = !!val;
}
},
mounted: function mounted() {
this.shouldValidate = !!this.error;
this.validate();
},
methods: {
reset: function reset() {
var _this = this;
// TODO: Do this another way!
// This is so that we can reset all types of inputs
this.$emit('input', this.isMultiple ? [] : null);
this.$emit('change', null);
this.$nextTick(function () {
_this.shouldValidate = false;
_this.hasFocused = false;
_this.validate();
});
},
validate: function validate() {
var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.inputValue;
if (force) this.shouldValidate = true;
this.errorBucket = [];
for (var index = 0; index < this.rules.length; index++) {
var rule = this.rules[index];
var valid = typeof rule === 'function' ? rule(value) : rule;
if (valid === false || typeof valid === 'string') {
this.errorBucket.push(valid);
} else if (valid !== true) {
(0, _console.consoleError)('Rules should return a string or boolean, received \'' + (typeof valid === 'undefined' ? 'undefined' : _typeof(valid)) + '\' instead', this);
}
}
this.valid = this.errorBucket.length === 0;
return this.valid;
}
}
};