@cosva-lab/form-builder
Version:
React form builder.
152 lines (149 loc) • 5.58 kB
JavaScript
import { __extends, __decorate } from '../../_virtual/_tslib.js';
import { toJS, observable, action, makeObservable } from 'mobx';
import InputsValidator from '../validate/InputsValidator.js';
var FieldsBuilder = /** @class */ (function (_super) {
__extends(FieldsBuilder, _super);
function FieldsBuilder(props) {
var _this = _super.call(this, props) || this;
_this._ns = undefined;
makeObservable(_this);
var ns = props.ns, globalProps = props.globalProps;
_this._ns = ns;
_this.globalProps = globalProps;
for (var _i = 0, _a = _this.fields; _i < _a.length; _i++) {
var field = _a[_i];
field.fieldsBuilder = _this;
}
_this.validate = _this._validate;
_this.saveData = _this.saveData.bind(_this);
_this.restore = _this.restore.bind(_this);
_this.restoreLast = _this.restoreLast.bind(_this);
_this.onChangeField = _this.onChangeField.bind(_this);
_this.onChangeFields = _this.onChangeFields.bind(_this);
_this.setValidation = _this.setValidation.bind(_this);
_this.setErrors = _this.setErrors.bind(_this);
_this.getErrors = _this.getErrors.bind(_this);
_this.getValues = _this.getValues.bind(_this);
_this.get = _this.get.bind(_this);
_this.getField = _this.getField.bind(_this);
return _this;
}
Object.defineProperty(FieldsBuilder.prototype, "ns", {
get: function () {
return this._ns;
},
set: function (ns) {
this._ns = ns;
},
enumerable: false,
configurable: true
});
Object.defineProperty(FieldsBuilder.prototype, "values", {
get: function () {
return this.getValues();
},
enumerable: false,
configurable: true
});
FieldsBuilder.prototype.setField = function (fieldOriginal) {
var field = this.fields.find(function (_a) {
var name = _a.name;
return fieldOriginal.name === name;
});
if (field) {
field.fieldsBuilder = this;
field.value = fieldOriginal.value;
field.errors = fieldOriginal.errors;
}
};
FieldsBuilder.prototype.setFields = function (fields) {
var _this = this;
fields.forEach(function (fieldOriginal) { return _this.setField(fieldOriginal); });
};
FieldsBuilder.prototype.saveData = function () {
var _a = this, fields = _a.fields, ns = _a.ns, validate = _a.validate;
this.paramsLast = {
fields: toJS(fields),
ns: ns,
validate: validate,
};
};
FieldsBuilder.prototype.restore = function () {
for (var _i = 0, _a = this.fields; _i < _a.length; _i++) {
var field = _a[_i];
field.reset();
}
};
FieldsBuilder.prototype.restoreLast = function () {
if (this.paramsLast) {
var fields = this.paramsLast.fields;
this.setFields(fields);
this.paramsLast = undefined;
}
};
FieldsBuilder.prototype.getValues = function () {
var values = Object.create(null);
for (var _i = 0, _a = this.fields; _i < _a.length; _i++) {
var _b = _a[_i], name_1 = _b.name, value = _b.value;
values[name_1] = value;
}
return toJS(values);
};
FieldsBuilder.prototype.get = function (fieldName) {
return this.fieldsMap[fieldName];
};
FieldsBuilder.prototype.getField = function (fieldName) {
return this.fieldsMap[fieldName];
};
FieldsBuilder.prototype.onChangeField = function (event) {
var value = event.value, name = event.name;
var field = this.fieldsMap[name];
if (field)
field.setValue(value);
else
console.warn("Field ".concat(name.toString(), " not found"));
};
FieldsBuilder.prototype.onChangeFields = function (events) {
var _this = this;
events.forEach(function (event) { return _this.onChangeField(event); });
};
FieldsBuilder.prototype.changeValue = function (_a) {
var name = _a.name, value = _a.value;
var field = this.get(name);
if (field)
field.value = value;
else
console.warn("Field ".concat(name, " not found"));
};
FieldsBuilder.prototype.changeValues = function (events) {
var _this = this;
events.forEach(function (event) { return _this.changeValue(event); });
};
FieldsBuilder.prototype.setValidation = function (validate) {
this.validate = validate;
};
__decorate([
observable
], FieldsBuilder.prototype, "_ns", void 0);
__decorate([
observable
], FieldsBuilder.prototype, "actionsExtra", void 0);
__decorate([
action
], FieldsBuilder.prototype, "onChangeField", null);
__decorate([
action
], FieldsBuilder.prototype, "onChangeFields", null);
__decorate([
action
], FieldsBuilder.prototype, "changeValue", null);
__decorate([
action
], FieldsBuilder.prototype, "changeValues", null);
__decorate([
action
], FieldsBuilder.prototype, "setValidation", null);
return FieldsBuilder;
}(InputsValidator));
export { FieldsBuilder, FieldsBuilder as default };
//# sourceMappingURL=FieldsBuilder.js.map