@agile-ts/multieditor
Version:
Simple Form Manager for UI-Frameworks
87 lines (84 loc) • 2.45 kB
JavaScript
import { State } from '@agile-ts/core';
import { defineConfig, isFunction } from '@agile-ts/utils';
import { logCodeManager } from './logCodeManager.js';
import { Validator } from './validator/validator.js';
import { Status } from './status/status.js';
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
class Item extends State {
constructor(editor, initialValue, config) {
super(editor.agileInstance(), initialValue, {
key: config.key
});
this.isValid = false;
this.touched = false;
config = defineConfig(config, {
canBeEdited: true,
validator: new Validator()
});
this.editor = () => editor;
this.validator = config.validator;
this.config = config;
this.status = new Status(this);
this.addSideEffect("validateItem", () => __async(this, null, function* () {
if (this.editor().canAssignStatusToItemOnChange(this))
this.status.config.display = true;
yield this.validate();
this.editor().recomputeValidatedState({ validate: false });
this.editor().recomputeModifiedState();
}));
}
validate() {
return __async(this, null, function* () {
const isValid = yield this.validator.validate(this, this.value);
this.isValid = isValid;
return isValid;
});
}
reset(config = {}) {
this.set(this.initialStateValue, config);
this.status.config.display = false;
this.touched = false;
return this;
}
computeValue(method) {
if (!isFunction(method)) {
logCodeManager.log("00:03:01", {
replacers: ["Compute Value Method", "function"]
});
return this;
}
this.computeValueMethod = method;
this.set(this.nextStateValue);
return this;
}
blur() {
this.touched = true;
if (this.editor().canAssignStatusToItemOnBlur(this)) {
this.status.config.display = true;
this.status.ingest({
force: true
});
}
return this;
}
}
export { Item };