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.
This commit is contained in:
HanishKVC 2024-05-27 00:55:02 +05:30
parent 1e47a48b30
commit 94bc0b08d8
2 changed files with 6 additions and 6 deletions

View file

@ -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);

View file

@ -86,19 +86,19 @@ export function el_create_boolbutton(id, texts, defaultValue, cb) {
/**
* @param {string} id
* @param {string[]} options
* @param {string} defaultOption
* @param {Object<string,*>} 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);