vuetify
Version:
Vue.js 2 Semantic Component Framework
190 lines (182 loc) • 6.75 kB
JavaScript
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; };
import { deepEqual } from '../util/helpers';
import { inject as RegistrableInject } from './registrable';
import { consoleError } from '../util/console';
// Mixins
import Colorable from './colorable';
/* @vue/component */
export default {
name: 'validatable',
mixins: [Colorable, RegistrableInject('form')],
props: {
disabled: Boolean,
error: Boolean,
errorCount: {
type: [Number, String],
default: 1
},
errorMessages: {
type: [String, Array],
default: function _default() {
return [];
}
},
messages: {
type: [String, Array],
default: function _default() {
return [];
}
},
readonly: Boolean,
rules: {
type: Array,
default: function _default() {
return [];
}
},
success: Boolean,
successMessages: {
type: [String, Array],
default: function _default() {
return [];
}
},
validateOnBlur: Boolean
},
data: function data() {
return {
errorBucket: [],
hasColor: false,
hasFocused: false,
hasInput: false,
isFocused: false,
isResetting: false,
valid: false
};
},
computed: {
hasError: function hasError() {
return this.internalErrorMessages.length > 0 || this.errorBucket.length > 0 || this.error;
},
externalError: function externalError() {
return this.internalErrorMessages.length > 0 || this.error;
},
// TODO: Add logic that allows the user to enable based
// upon a good validation
hasSuccess: function hasSuccess() {
return this.successMessages.length > 0 || this.success;
},
hasMessages: function hasMessages() {
return this.validations.length > 0;
},
hasState: function hasState() {
return this.hasSuccess || this.shouldValidate && this.hasError;
},
internalErrorMessages: function internalErrorMessages() {
return this.errorMessages || '';
},
shouldValidate: function shouldValidate() {
return this.externalError || !this.isResetting && (this.validateOnBlur ? this.hasFocused && !this.isFocused : this.hasInput || this.hasFocused);
},
validations: function validations() {
return this.validationTarget.slice(0, this.errorCount);
},
validationState: function validationState() {
if (this.hasError && this.shouldValidate) return 'error';
if (this.hasSuccess) return 'success';
if (this.hasColor) return this.color;
return null;
},
validationTarget: function validationTarget() {
var target = this.internalErrorMessages.length > 0 ? this.errorMessages : this.successMessages.length > 0 ? this.successMessages : this.messages;
// String
if (!Array.isArray(target)) {
return [target];
// Array with items
} else if (target.length > 0) {
return target;
// Currently has validation
} else if (this.shouldValidate) {
return this.errorBucket;
} else {
return [];
}
}
},
watch: {
rules: {
handler: function handler(newVal, oldVal) {
if (deepEqual(newVal, oldVal)) return;
this.validate();
},
deep: true
},
internalValue: function internalValue() {
// If it's the first time we're setting input,
// mark it with hasInput
this.hasInput = true;
this.validateOnBlur || this.$nextTick(this.validate);
},
isFocused: function isFocused(val) {
// Should not check validation
// if disabled or readonly
if (!val && !this.disabled && !this.readonly) {
this.hasFocused = true;
this.validateOnBlur && this.validate();
}
},
isResetting: function isResetting() {
var _this = this;
setTimeout(function () {
_this.hasInput = false;
_this.hasFocused = false;
_this.isResetting = false;
}, 0);
},
hasError: function hasError(val) {
if (this.shouldValidate) {
this.$emit('update:error', val);
}
}
},
beforeMount: function beforeMount() {
this.validate();
},
created: function created() {
this.form && this.form.register(this);
},
beforeDestroy: function beforeDestroy() {
this.form && this.form.unregister(this);
},
methods: {
/** @public */
reset: function reset() {
this.isResetting = true;
this.internalValue = Array.isArray(this.internalValue) ? [] : undefined;
},
/** @public */
resetValidation: function resetValidation() {
this.isResetting = true;
},
/** @public */
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.internalValue;
var errorBucket = [];
if (force) this.hasInput = this.hasFocused = true;
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') {
errorBucket.push(valid);
} else if (valid !== true) {
consoleError('Rules should return a string or boolean, received \'' + (typeof valid === 'undefined' ? 'undefined' : _typeof(valid)) + '\' instead', this);
}
}
this.errorBucket = errorBucket;
this.valid = errorBucket.length === 0;
return this.valid;
}
}
};
//# sourceMappingURL=validatable.js.map