namespace formgen.input { /** */ export class class_input_simple implements interface_input { /** */ private type : string; /** */ private value_encode : ((type_value) => string); /** */ private value_decode : ((string) => type_value); /** */ private additional_classes : Array; /** */ private label : (null | string); /** */ 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, } : { additional_classes ?: Array, label ?: (null | string); } = { } ) { this.type = type; this.value_encode = value_encode; this.value_decode = value_decode; this.additional_classes = additional_classes; this.label = label; this.element_input = null; } /** * [implementation] */ public setup( element_target : Element ) : Promise { const id : string = formgen.helpers.string.generate({"prefix": "input-label-pair-"}); 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 { if (this.label === null) { // do nothing } else { const element_label : Element = document.createElement("label"); element_label.setAttribute("for", id); element_label.textContent = this.label; element_container.appendChild(element_label); } } // 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); return Promise.resolve(undefined); } /** * [implementation] */ public read( ) : Promise { const raw : string = (this.element_input as HTMLInputElement).value; const value : type_value = this.value_decode(raw); return Promise.resolve(value); } /** * [implementation] */ public write( value : type_value ) : Promise { const raw : string = this.value_encode(value); (this.element_input as HTMLInputElement).value = raw; return Promise.resolve(undefined); } } }