vdr-react-form-manager
Version:
84 lines (83 loc) • 2.98 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const FormInputProperties_1 = require("./FormInputProperties");
const array_utils_1 = require("../utils/array.utils");
const formInputsValidator_utils_1 = require("../utils/formInputsValidator.utils");
const formInputProperties_utils_1 = require("../utils/formInputProperties.utils");
const FormManager_constant_1 = require("../constant/FormManager.constant");
class FormInputPropertiesBuilder {
constructor(name) {
this.value = '';
this.label = '';
this.isValid = true;
this.errors = [];
this.disabled = false;
this.classNames = [];
this.validators = [];
this.availableValues = [];
this.originalDisabledValue = false;
this.updateId = '';
this.originalValue = '';
this.isTouched = false;
this.isPristine = true;
this.name = name;
this.customProps = {};
}
addId(id) {
this.id = id;
return this;
}
addValue(value) {
this.value = value;
return this;
}
addDisabled(disabled) {
this.disabled = typeof disabled !== FormManager_constant_1.typeBoolean ? false : disabled;
return this;
}
addClassNames(classNames) {
if (array_utils_1.isValidArray(classNames)) {
this.classNames = classNames;
}
return this;
}
addValidators(validators) {
this.validators = formInputProperties_utils_1.getInputValidators(validators);
return this;
}
addAvailableValue({ value, label }) {
return this.addAvailableValueList([{ value, label }]);
}
addAvailableValueList(valueList) {
const newValues = formInputProperties_utils_1.getInputAvailableValues(valueList);
if (!newValues.length) {
return this;
}
if (!this.availableValues) {
this.availableValues = [];
}
this.availableValues = this.availableValues.concat(valueList);
return this;
}
addLabel(label) {
this.label = label;
return this;
}
addProperty(inputPropertiesIn) {
this.customProps = Object.assign(Object.assign({}, this.customProps), inputPropertiesIn);
return this;
}
removeProperty(propertyName) {
delete this.customProps[propertyName];
return this;
}
build() {
const formInputProperties = new FormInputProperties_1.FormInputProperties(this);
formInputProperties.updateId = formInputProperties_utils_1.createUpdateId(this.value);
formInputProperties.originalValue = this.value;
formInputProperties.errors = formInputsValidator_utils_1.validateFormInput(this.value, this.validators);
formInputProperties.isValid = formInputProperties.errors.length === 0;
return { [this.name]: formInputProperties };
}
}
exports.FormInputPropertiesBuilder = FormInputPropertiesBuilder;