Reply To: Filled TextInput

PennController for IBEX Forums Support Filled TextInput Reply To: Filled TextInput

#7205
Jeremy
Keymaster

Hi Ana,

Sure, you’ll need to adapt the javascript code to the specific DOM stucture of a PennController trial, but it’s pretty much the same logic as what you found on w3schools. Here’s an example:

newTrial(
    newTextInput("country")
        .print()
    ,
    newFunction( ()=>{
        let sug = [];
        let idx = 0;
        const ipt = document.querySelector(".PennController-TextInput-container.PennController-country-container textarea");
        const countries = ["Afghanistan","Albania","Algeria","Andorra","Angola","Anguilla","Antigua & Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central Arfrican Republic","Chad","Chile","China","Colombia","Congo","Cook Islands","Costa Rica","Cote D Ivoire","Croatia","Cuba","Curacao","Cyprus","Czech Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Falkland Islands","Faroe Islands","Fiji","Finland","France","French Polynesia","French West Indies","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guam","Guatemala","Guernsey","Guinea","Guinea Bissau","Guyana","Haiti","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Isle of Man","Israel","Italy","Jamaica","Japan","Jersey","Jordan","Kazakhstan","Kenya","Kiribati","Kosovo","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia","Moldova","Monaco","Mongolia","Montenegro","Montserrat","Morocco","Mozambique","Myanmar","Namibia","Nauro","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","North Korea","Norway","Oman","Pakistan","Palau","Palestine","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russia","Rwanda","Saint Pierre & Miquelon","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal","Serbia","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Korea","South Sudan","Spain","Sri Lanka","St Kitts & Nevis","St Lucia","St Vincent","Sudan","Suriname","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Timor L'Este","Togo","Tonga","Trinidad & Tobago","Tunisia","Turkey","Turkmenistan","Turks & Caicos","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States of America","Uruguay","Uzbekistan","Vanuatu","Vatican City","Venezuela","Vietnam","Virgin Islands (US)","Yemen","Zambia","Zimbabwe"];
        const select = s=>{
            ipt.value = s;
            if (ipt.parentElement==div.parentElement)
                ipt.parentElement.removeChild(div);
        };
        const div = document.createElement("DIV");
        div.classList.add("PennController-suggestions")
        let previousIdx = 0;
        const refreshSug = ()=>{
            if (div.parentElement!=ipt.parentElement) return;
            if (idx!=previousIdx){
                div.childNodes.forEach((v,i)=>{
                    if (i==idx) {
                        v.style["background-color"] = "pink";
                        v.scrollIntoView({block: "nearest"});
                    }
                    else v.style["background-color"] = "transparent";
                });
                previousIdx = idx;
            }
            window.requestAnimationFrame(refreshSug);
        }
        ipt.addEventListener("keydown", e=>{
            if (div.parentElement!=ipt.parentElement) return true;
            if (e.keyCode==40) idx = (idx+1)%sug.length;
            else if (e.keyCode==38) idx = (idx-1 < 0 ? sug.length-1 : idx-1);
            else if (e.keyCode==13) select(sug[idx]);
            else return true;
            e.preventDefault();
            return false;
        })
        ipt.addEventListener("input", ()=>{
            sug = countries.filter(v=>v.match(new RegExp(ipt.value, "i")));
            if (sug.length){
                div.innerHTML = "";
                sug.forEach((v,i)=>{
                    const s = document.createElement("SPAN");
                    s.innerText = v;
                    s.addEventListener('mouseenter', ()=>idx=i);
                    s.addEventListener('mousedown', ()=>select(v));
                    div.appendChild(s);
                });
                idx = -1;
                ipt.parentElement.appendChild(div);
                refreshSug();
            } else if (div.parentElement==ipt.parentElement)
                ipt.parentElement.removeChild(div);
        });
        ipt.addEventListener("blur", ()=>ipt.parentElement==div.parentElement && ipt.parentElement.removeChild(div));
    }).call()
    ,
    newButton("Hello world")
        .print()
        .wait()
)

You'll also need to add those CSS rules to Aesthetics/PennController.css:

.suggestions {
    position: absolute;
    transform: translateY(-100%);
    display: flex;
    flex-direction: column;
    max-height: 5em;
    overflow-y: scroll;
    background-color: lightgray;
}

Let me know if you have questions

Jeremy