formgen/source/logic/input/simple.ts

149 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-04-22 07:47:23 +02:00
namespace formgen.input
{
/**
*/
export class class_input_simple<type_value> implements interface_input<type_value>
{
/**
*/
private type : string;
/**
*/
private value_encode : ((type_value) => string);
/**
*/
private value_decode : ((string) => type_value);
/**
*/
private additional_classes : Array<string>;
/**
*/
private label : (null | string);
2026-04-22 22:53:40 +02:00
/**
*/
private hint : (null | string);
2026-04-22 07:47:23 +02:00
/**
*/
private element_input : (null | Element);
/**
*/
public constructor(
type : string,
value_encode : ((type_value) => string),
value_decode : ((string) => type_value),
{
"additional_classes": additional_classes = [],
"label": label = null,
2026-04-22 22:53:40 +02:00
"hint": hint = null,
2026-04-22 07:47:23 +02:00
}
:
{
additional_classes ?: Array<string>,
label ?: (null | string);
2026-04-22 22:53:40 +02:00
hint ?: (null | string);
2026-04-22 07:47:23 +02:00
}
=
{
}
)
{
this.type = type;
this.value_encode = value_encode;
this.value_decode = value_decode;
this.additional_classes = additional_classes;
this.label = label;
2026-04-22 22:53:40 +02:00
this.hint = hint;
2026-04-22 07:47:23 +02:00
this.element_input = null;
}
/**
* [implementation]
*/
2026-04-22 22:53:40 +02:00
public async setup(
2026-04-22 07:47:23 +02:00
element_target : Element
) : Promise<void>
{
2026-04-22 22:24:32 +02:00
const id : string = formgen.helpers.string.generate({"prefix": "input-label-pair-"});
2026-04-22 07:47:23 +02:00
const element_container : Element = document.createElement("div");
element_container.classList.add("formgen-input");
for (const class_ of this.additional_classes)
{
element_container.classList.add(class_);
}
// label
{
2026-04-22 22:53:40 +02:00
const element_label : Element = document.createElement("label");
element_label.setAttribute("for", id);
element_label.textContent = (this.label ?? "");
// hint
2026-04-22 07:47:23 +02:00
{
2026-04-22 22:53:40 +02:00
if (this.hint === null)
{
// do nothing
}
else
{
await (new formgen.info.class_info(this.hint)).setup(element_label);
}
2026-04-22 07:47:23 +02:00
}
2026-04-22 22:53:40 +02:00
element_container.appendChild(element_label);
2026-04-22 07:47:23 +02:00
}
// input
{
const element_input : Element = document.createElement("input");
element_input.setAttribute("type", this.type);
element_input.setAttribute("id", id);
element_container.appendChild(element_input);
this.element_input = element_input;
}
element_target.appendChild(element_container);
2026-04-22 22:53:40 +02:00
// return Promise.resolve<void>(undefined);
2026-04-22 07:47:23 +02:00
}
/**
* [implementation]
*/
public read(
) : Promise<type_value>
{
const raw : string = (this.element_input as HTMLInputElement).value;
const value : type_value = this.value_decode(raw);
return Promise.resolve<type_value>(value);
}
/**
* [implementation]
*/
public write(
value : type_value
) : Promise<void>
{
const raw : string = this.value_encode(value);
(this.element_input as HTMLInputElement).value = raw;
return Promise.resolve<void>(undefined);
}
}
}