vue-forms-builder
Version:
Typescript forms builder package written specifically for Vue
182 lines • 6.06 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormGroup = void 0;
var FormGroup = /** @class */ (function () {
/**
* Initialize the FormGroup instance
* @param controls - controls for the FormGroup
*/
function FormGroup(controls) {
this.controls = controls;
}
Object.defineProperty(FormGroup.prototype, "value", {
/**
* Getter for the controls value
* @readonly
* @type {*}
* @memberof FormGroup
*/
get: function () {
return this._getValueAsObject();
},
enumerable: false,
configurable: true
});
Object.defineProperty(FormGroup.prototype, "valid", {
/**
* Getter for controls validity
* @readonly
* @type {boolean}
* @memberof FormGroup
*/
get: function () {
return this._checkIsValid();
},
enumerable: false,
configurable: true
});
Object.defineProperty(FormGroup.prototype, "touched", {
/**
* Getter for the group touched value
* @readonly
* @type {boolean}
* @memberof FormGroup
*/
get: function () {
return this._checkIsTouched();
},
enumerable: false,
configurable: true
});
/**
* Get control by given name
* @param controlName - name of the control
* @returns AbstractControl if exist, if not method returns undefined
*/
FormGroup.prototype.get = function (controlName, formGroup) {
var controls = (formGroup === null || formGroup === void 0 ? void 0 : formGroup.controls) || this.controls;
var controlNameArray = controlName.split('.');
for (var control in controls) {
if (controls.hasOwnProperty(control)) {
var controlElement = controls[control];
if (control === controlNameArray[0]) {
if (controlNameArray.length === 1) {
return controlElement;
}
else {
controlNameArray.shift();
return this.get(controlNameArray.join('.'), controlElement);
}
}
}
}
};
/**
* Marks all the controls in this group as touched
*/
FormGroup.prototype.markAllAsTouched = function () {
var _this = this;
Object.keys(this.controls).forEach(function (control) {
_this.controls[control].markAllAsTouched();
});
};
/**
* Patches the value of this group
* @param value - object that matches the structure of the group
*/
FormGroup.prototype.patchValue = function (value) {
var _this = this;
Object.keys(value).forEach(function (key) {
var _a;
(_a = _this.get(key)) === null || _a === void 0 ? void 0 : _a.patchValue(value[key]);
});
};
/**
* Resets all the controls in this group to the initial value,
* setting all of it as untouched, resetting error
* and setting validators to the initial value
*/
FormGroup.prototype.reset = function () {
var _this = this;
Object.keys(this.controls).forEach(function (control) {
_this.controls[control].reset();
});
};
/**
* Add a control to this group
* @param name - name of a new control
* @param control - control for the given name
*/
FormGroup.prototype.addControl = function (name, control) {
this.controls[name] = control;
};
/**
* Remove a control from this group
* @param name - name of a control to be removed
*/
FormGroup.prototype.removeControl = function (name) {
delete this.controls[name];
};
/**
* Check if this group contains a specific control
* @param name - name of a control
* @returns if control exist returns true, if not returns false
*/
FormGroup.prototype.contains = function (name) {
return this.controls[name] !== undefined;
};
/**
* Private method for checking validaty of the controls in this group
* @returns if all controls are valid returns true, if not return false
*/
FormGroup.prototype._checkIsValid = function () {
var valid = true;
for (var control in this.controls) {
if (!this.controls[control].valid) {
valid = false;
}
}
return valid;
};
/**
* Private method for getting controls as object of values
* @returns object of controls value
*/
FormGroup.prototype._getValueAsObject = function () {
var _this = this;
var valueObject = {};
Object.keys(this.controls).forEach(function (control) {
var _a;
valueObject = __assign(__assign({}, valueObject), (_a = {}, _a[control] = _this.controls[control].value, _a));
});
return valueObject;
};
/**
* Private method for checking touched of the controls in this group
* @returns true if any control in this group is touched, false if all controls are untouched.
*/
FormGroup.prototype._checkIsTouched = function () {
var _this = this;
var touched = false;
Object.keys(this.controls).forEach(function (control) {
if (_this.controls[control].touched) {
touched = true;
}
});
return touched;
};
return FormGroup;
}());
exports.FormGroup = FormGroup;
//# sourceMappingURL=FormGroup.js.map