namespace formgen.input { /** */ type type_option = { value : type_value; label : string; }; /** */ export class class_input_select implements interface_input { /** */ private additional_classes : Array; /** */ private label : (null | string); /** */ private options : Array>; /** */ private value_map : Map; /** */ private element_select : (null | HTMLSelectElement); /** */ public constructor( options : Array>, { "additional_classes": additional_classes = [], "label": label = null, } : { additional_classes ?: Array, label ?: (null | string); } = { } ) { this.options = options; this.additional_classes = ["formgen-input-select"].concat(additional_classes); this.label = label; this.value_map = new Map(); this.element_select = null; } /** * [implementation] */ public setup( target : Element ) : Promise { 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 { 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_select : HTMLSelectElement = (document.createElement("select") as HTMLSelectElement); element_select.setAttribute("id", id); for (const index of formgen.helpers.list.sequence(this.options.length)) { const option : type_option = this.options[index]; const virtual_value : string = formgen.helpers.string.generate(); this.value_map.set(virtual_value, index); const element_option : Element = document.createElement("option"); element_option.textContent = option.label; element_option.setAttribute("value", virtual_value); element_select.appendChild(element_option); } element_container.appendChild(element_select); this.element_select = element_select; } target.appendChild(element_container); return Promise.resolve(undefined); } /** * [implementation] */ public read( ) : Promise { if (this.element_select === null) { return Promise.reject(new Error("not properly initialized")); } else { const virtual_value : string = this.element_select.value; const value : type_value = this.options[this.value_map.get(virtual_value)].value; return Promise.resolve(value); } } /** * [implementation] */ public write( value : type_value ) : Promise { if (this.element_select === null) { return Promise.reject(new Error("not properly initialized")); } else { return Promise.reject(new Error("not implemented")); } } } }