vue-forms-builder
Version:
Typescript forms builder package written specifically for Vue
189 lines • 6.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormControl = void 0;
var FormControl = /** @class */ (function () {
/**
* Initialize the FormControl instance
* @param value - value for the control
* @param validators - validator or array of validators for the control
*/
function FormControl(value, validators) {
this._value = null;
this._validators = [];
this.touched = false;
this.valid = true;
this.error = {};
this._value = value;
this._defaultValue = JSON.parse(JSON.stringify(value));
if (validators) {
this._validators = validators instanceof Array ? validators : [validators];
this._validateControl();
}
}
Object.defineProperty(FormControl.prototype, "value", {
/**
* Getter for the control value
* @type {*}
* @memberof FormControl
*/
get: function () {
return this._value;
},
/**
* Setter for the control value
* @memberof FormControl
*/
set: function (value) {
this._value = value;
this.markAsTouched();
},
enumerable: false,
configurable: true
});
/**
* Sets a new value for the control
* @param value - new value
*/
FormControl.prototype.setValue = function (value) {
this.value = value;
};
/**
* Patches the value of the control
* @param value - new value
*/
FormControl.prototype.patchValue = function (value) {
this.setValue(value);
};
/**
* Marks the control as touched
*/
FormControl.prototype.markAsTouched = function () {
this.touched = true;
if (this._validators.length) {
this._validateControl();
}
};
/**
* Marks the control as touched
*/
FormControl.prototype.markAllAsTouched = function () {
this.markAsTouched();
};
/**
* Marks the control as untouched
*/
FormControl.prototype.markAsUntouched = function () {
this.touched = false;
};
/**
* Sets validators to this control
* If it has any validators, they will be overwritten
* @param validators - new validator or array of validators
*/
FormControl.prototype.setValidators = function (validators) {
this._validators = validators instanceof Array ? validators : [validators];
this._validateControl();
};
/**
* Add validators to this control
* If it has any validators, they will be added to the existing ones
* @param validators - new validator or array of validators
*/
FormControl.prototype.addValidators = function (validators) {
if (validators instanceof Array) {
this._validators = this._validators.concat(validators);
}
else {
this._validators.push(validators);
}
this._validateControl();
};
/**
* Removes validators from this control
* @param validators - validator or array of validators to be removed
*/
FormControl.prototype.removeValidators = function (validators) {
var _this = this;
if (validators instanceof Array) {
validators.forEach(function (validator) { return _this._removeValidator(validator); });
}
else {
this._removeValidator(validators);
}
this._validateControl();
};
/**
* Checks if validator exist in this control
* @param validators - validator to check
* @returns if validator exist returns true, if not returns false
*/
FormControl.prototype.hasValidator = function (validator) {
return this._validators.some(function (validatorItem) { return validatorItem.name === validator.name; });
};
/**
* Removes all validators from this control
*/
FormControl.prototype.clearValidators = function () {
this._validators = [];
this._validateControl();
};
/**
* Sets error to this control
* It also set validation to be falsy
* @param error - error to be set
*/
FormControl.prototype.setError = function (error) {
this.error = error;
this.valid = false;
};
/**
* Checks if the control have specified error
* @param error - name of error
* @returns if error exist returns true, if not returns false
*/
FormControl.prototype.hasError = function (error) {
return this.error[error];
};
/**
* Resets the control to the initial value,
* setting it as untouched, resetting error
* and setting validators to the initial value
*/
FormControl.prototype.reset = function () {
this.value = this._defaultValue;
this.markAsUntouched();
this._validateControl();
};
/**
* Private method that checks if the control is valid
* and setting error and control validity
*/
FormControl.prototype._validateControl = function () {
var _this = this;
var isFound = false;
this.error = {};
this._validators.forEach(function (validator) {
if (!isFound) {
var error = validator(_this._value);
if (error) {
_this.error = error;
isFound = true;
}
}
});
this.valid = Object.keys(this.error).length === 0;
};
/**
* Private method for removing validator from the control
* @param validator - validator to be removed
*/
FormControl.prototype._removeValidator = function (validator) {
var index = this._validators.findIndex(function (validatorItem) { return validatorItem.name === validator.name; });
if (index > -1) {
this._validators.splice(index, 1);
}
};
return FormControl;
}());
exports.FormControl = FormControl;
//# sourceMappingURL=FormControl.js.map