formgen/source/logic/input/checkbox.ts

126 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

2026-04-22 07:47:23 +02:00
namespace formgen.input
{
/**
*/
export class class_input_checkbox implements interface_input<boolean>
{
/**
*/
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(
{
"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.additional_classes = ["formgen-input-checkbox"].concat(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
target : Element
) : Promise<void>
{
const id : string = formgen.helpers.string.generate();
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", "checkbox");
element_input.setAttribute("id", id);
element_container.appendChild(element_input);
this.element_input = element_input;
}
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<boolean>
{
const value : boolean = (this.element_input as HTMLInputElement).checked;
return Promise.resolve<boolean>(value);
}
/**
* [implementation]
*/
public write(
value : boolean
) : Promise<void>
{
(this.element_input as HTMLInputElement).checked = value;
return Promise.resolve<void>(undefined);
}
}
}