ms-react-reactive-form
Version:
an implementation of the Reactive Form concept in React, supporting Javascript and Typescript
41 lines (40 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class FormControl {
constructor(value, validators = [], type = "string") {
this.value = value;
this.validity = true;
this.errors = {};
this.requiredValidations = {};
this.type = type;
this.setValidators(validators);
}
;
removeValidators(validators) {
validators.forEach((validator) => {
Object.keys(validator).forEach((item) => {
if (this.requiredValidations[item]) {
delete this.requiredValidations[item];
this.errors = {};
this.validity = true;
}
});
});
}
;
setValue(value = null) {
this.value = value;
this.validity = true;
this.errors = {};
}
setValidators(validators) {
validators.forEach((validator) => {
Object.keys(validator).forEach((item) => {
if (!this.requiredValidations[item]) {
this.requiredValidations[item] = validator[item];
}
});
});
}
}
exports.default = FormControl;