Skip to content

Commit 260b888

Browse files
feat(demo): name typeahead on mixture-mode ingredient rows too (#69)
Refactor the datalist typeahead into wireSuggest(input) and attach it to every mixture ingredient row as well as the main search bar. Signed-off-by: Austin L. <86896075+rvnminers-A-and-N@users.noreply.github.com>
1 parent 53d5cb2 commit 260b888

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

training/workbench.html

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -340,25 +340,30 @@ <h2>Aroma</h2>
340340
if(nb && nb.dataset.smi){ q.value = nb.dataset.smi; window.scrollTo({top:0,behavior:'smooth'}); run(); }
341341
});
342342

343-
// --- typeahead over the curated flavor-volatile list ---
343+
// --- name typeahead (datalist) over the curated flavor-volatile list, on any input ---
344344
let suggestT;
345-
q.addEventListener('input', ()=>{
346-
clearTimeout(suggestT);
347-
suggestT=setTimeout(async ()=>{
348-
const v=q.value.trim();
349-
if(v.length<2){ $('suggest').innerHTML=''; return; }
350-
try{
351-
const r=await fetch('/api/suggest?qs='+encodeURIComponent(v));
352-
const d=await r.json();
353-
$('suggest').innerHTML=(d.items||[]).map(it=>`<option value="${it.name}">`).join('');
354-
}catch(_){}
355-
}, 160);
356-
});
345+
function wireSuggest(input){
346+
input.setAttribute('list','suggest');
347+
input.addEventListener('input', ()=>{
348+
clearTimeout(suggestT);
349+
suggestT=setTimeout(async ()=>{
350+
const v=input.value.trim();
351+
if(v.length<2){ $('suggest').innerHTML=''; return; }
352+
try{
353+
const r=await fetch('/api/suggest?qs='+encodeURIComponent(v));
354+
const d=await r.json();
355+
$('suggest').innerHTML=(d.items||[]).map(it=>`<option value="${it.name}">`).join('');
356+
}catch(_){}
357+
}, 160);
358+
});
359+
}
360+
wireSuggest(q);
357361

358362
// --- mixture mode ---
359363
function mixAddRow(val){
360364
const div=document.createElement('div'); div.className='mix-row';
361365
const inp=document.createElement('input'); inp.placeholder='name or SMILES'; inp.value=val||'';
366+
wireSuggest(inp);
362367
const rm=document.createElement('button'); rm.className='rm'; rm.textContent='×'; rm.title='remove';
363368
rm.onclick=()=>div.remove();
364369
div.append(inp, rm); $('mixRows').appendChild(div);

0 commit comments

Comments
 (0)