vue-forms-builder
Version:
Typescript forms builder package written specifically for Vue
176 lines • 5.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormArray = void 0;
var FormArray = /** @class */ (function () {
/**
* Initialize the FormArray instance
* @param controls - controls for the FormArray
*/
function FormArray(controls) {
this.controls = controls;
}
Object.defineProperty(FormArray.prototype, "length", {
/**
* Getter for the length of controls
* @readonly
* @type {number}
* @memberof FormArray
*/
get: function () {
return this.controls.length;
},
enumerable: false,
configurable: true
});
Object.defineProperty(FormArray.prototype, "value", {
/**
* Getter for the controls value
* @readonly
* @type {any[]}
* @memberof FormArray
*/
get: function () {
return this._getValueAsArray();
},
enumerable: false,
configurable: true
});
Object.defineProperty(FormArray.prototype, "valid", {
/**
* Getter for controls validity
* @readonly
* @type {boolean}
* @memberof FormArray
*/
get: function () {
return this._checkIsValid();
},
enumerable: false,
configurable: true
});
Object.defineProperty(FormArray.prototype, "touched", {
/**
* Getter for the array touched value
* @readonly
* @type {boolean}
* @memberof FormArray
*/
get: function () {
return this._checkIsTouched();
},
enumerable: false,
configurable: true
});
/**
* Get control at the given index in the array of controls
* @param index - the index number of the control
* @returns AbstractControl at the given index
*/
FormArray.prototype.at = function (index) {
return this.controls[index];
};
/**
* Add a new control at the end of the array of controls
* @param control - new AbstractControl
*/
FormArray.prototype.push = function (control) {
this.controls.push(control);
};
/**
* Insert a new control at the given index in the array of controls
* @param index - index number where we want to insert the control
* @param control - new AbstractControl
*/
FormArray.prototype.insert = function (index, control) {
this.controls.splice(index, 0, control);
};
/**
* Remove control at the given index from the array of controls
* @param index - the index number of the control we want to delete
*/
FormArray.prototype.removeAt = function (index) {
this.controls.splice(index, 1);
};
/**
* Replace an existing control at the given index in the array of controls
* @param index - the index number of the control we want to replace
* @param control - new AbstractControl
*/
FormArray.prototype.setControl = function (index, control) {
if (index > 0 && index <= this.length) {
this.controls[index] = control;
}
else {
this.controls.push(control);
}
};
/**
* Resets all the controls in this array to the initial value,
* setting all of it as untouched, resetting error
* and setting validators to the initial value
*/
FormArray.prototype.reset = function () {
this.controls.forEach(function (control) { return control.reset(); });
};
/**
* Remove all controls from this array
*/
FormArray.prototype.clear = function () {
this.controls.splice(0);
};
/**
* Marks all the controls in this array as touched
*/
FormArray.prototype.markAllAsTouched = function () {
this.controls.forEach(function (control) {
control.markAllAsTouched();
});
};
/**
* Patches the value of this array
* @param values - array of values for the controls
*/
FormArray.prototype.patchValue = function (values) {
var _this = this;
values.forEach(function (value, index) {
var _a;
(_a = _this.controls[index]) === null || _a === void 0 ? void 0 : _a.patchValue(value);
});
};
/**
* Private method for checking validaty of the controls
* @returns if all controls are valid returns true, if not return false
*/
FormArray.prototype._checkIsValid = function () {
var valid = true;
this.controls.forEach(function (control) {
if (!control.valid) {
valid = false;
}
});
return valid;
};
/**
* Private method for getting controls as array of values
* @returns array of controls value
*/
FormArray.prototype._getValueAsArray = function () {
return this.controls.map(function (control) { return control.value; });
};
/**
* Private method for checking touched of the controls in this array
* @returns true if any control in this array is touched, false if all controls are untouched.
*/
FormArray.prototype._checkIsTouched = function () {
var touched = false;
this.controls.forEach(function (control) {
if (control.touched) {
touched = true;
}
});
return touched;
};
return FormArray;
}());
exports.FormArray = FormArray;
//# sourceMappingURL=FormArray.js.map