@vendasta/store
Version:
Components and data for Store
40 lines (39 loc) • 1.36 kB
JavaScript
import { Injectable } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
var FieldService = /** @class */ (function () {
function FieldService() {
}
FieldService.prototype.createFormControl = function (field) {
if (field.required) {
return new FormControl(field.value, Validators.required);
}
else {
return new FormControl(field.value);
}
};
FieldService.prototype.toFormGroup = function (formFields, subscriptions) {
var _this = this;
var group = {};
formFields.forEach(function (field) {
var formControl;
if (field.controlType === 'checkbox') {
formControl = new FormControl(field.value);
}
else {
formControl = _this.createFormControl(field);
}
group[field.id] = formControl;
if (subscriptions) {
subscriptions.push(formControl.valueChanges.subscribe(function (value) { return field.value = value; }));
}
});
return new FormGroup(group);
};
FieldService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
FieldService.ctorParameters = function () { return []; };
return FieldService;
}());
export { FieldService };