From 94bc0b08d860bf68a638a703ab2ba0c0c98ec879 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Mon, 27 May 2024 00:55:02 +0530 Subject: [PATCH] SimpleChat:UI:Select: dict-name-value, value wrt default, change Take a dict/object of name-value pairs instead of just names. Inturn specify the actual value wrt default, rather than the string representing that value. Trap the needed change event rather than click wrt select. --- examples/server/public_simplechat/simplechat.js | 2 +- examples/server/public_simplechat/ui.mjs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/server/public_simplechat/simplechat.js b/examples/server/public_simplechat/simplechat.js index d96142ea2..2e1d7c092 100644 --- a/examples/server/public_simplechat/simplechat.js +++ b/examples/server/public_simplechat/simplechat.js @@ -586,7 +586,7 @@ class Me { }); elDiv.appendChild(bb); - let sel = ui.el_create_select("SetChatHistoryInCtxt", Object.keys(this.sRecentUserMsgCnt), "Last0", (val)=>{ + let sel = ui.el_create_select("SetChatHistoryInCtxt", this.sRecentUserMsgCnt, this.iRecentUserMsgCnt, (val)=>{ this.iRecentUserMsgCnt = this.sRecentUserMsgCnt[val]; }); elDiv.appendChild(sel); diff --git a/examples/server/public_simplechat/ui.mjs b/examples/server/public_simplechat/ui.mjs index e7aa20c16..ea235b15a 100644 --- a/examples/server/public_simplechat/ui.mjs +++ b/examples/server/public_simplechat/ui.mjs @@ -86,19 +86,19 @@ export function el_create_boolbutton(id, texts, defaultValue, cb) { /** * @param {string} id - * @param {string[]} options - * @param {string} defaultOption + * @param {Object} options + * @param {*} defaultOption * @param {function(string):void} cb */ export function el_create_select(id, options, defaultOption, cb) { let el = document.createElement("select"); el["xselected"] = defaultOption; el["xoptions"] = structuredClone(options); - for(let cur of options) { + for(let cur of Object.keys(options)) { let op = document.createElement("option"); op.value = cur; op.innerText = cur; - if (cur == defaultOption) { + if (options[cur] == defaultOption) { op.selected = true; } el.appendChild(op); @@ -107,7 +107,7 @@ export function el_create_select(id, options, defaultOption, cb) { el.id = id; el.name = id; } - el.addEventListener('click', (ev)=>{ + el.addEventListener('change', (ev)=>{ let target = /** @type{HTMLSelectElement} */(ev.target); console.log("DBUG:UI:Select:", id, ":", target.value); cb(target.value);