@govbr-ds/core
Version:
Padrão Digital de Governo
2 lines • 3.34 kB
JavaScript
!function e(t,s){"object"==typeof exports&&"object"==typeof module?module.exports=s():"function"==typeof define&&define.amd?define("core",[],s):"object"==typeof exports?exports.core=s():t.core=s()}(self,(()=>(()=>{var e=()=>{},t,s=()=>{};(()=>{class e{constructor(e,t){this.name=e,this.component=t,this._currentFocus=-1,this._setBehavior()}_setBehavior(){this._setPasswordViewBehavior(),this._setAutocompleteBehavior()}_setPasswordViewBehavior(){for(const e of this.component.querySelectorAll('input[type="password"]'))if(!e.disabled)for(const t of e.parentNode.querySelectorAll(".br-button"))t.addEventListener("click",(e=>{this._toggleShowPassword(e)}),!1)}_toggleShowPassword(e){for(const t of e.currentTarget.querySelectorAll(".fas"))if(t.classList.contains("fa-eye")){t.classList.remove("fa-eye"),t.classList.add("fa-eye-slash");for(const e of this.component.querySelectorAll('input[type="password"]'))e.setAttribute("type","text");for(const e of this.component.querySelectorAll('button[aria-label="Exibir senha"]'))e.setAttribute("aria-checked","true")}else if(t.classList.contains("fa-eye-slash")){t.classList.remove("fa-eye-slash"),t.classList.add("fa-eye");for(const e of this.component.querySelectorAll('input[type="text"]'))e.setAttribute("type","password");for(const e of this.component.querySelectorAll('button[aria-label="Exibir senha"]'))e.setAttribute("aria-checked","false")}}_setAutocompleteBehavior(){for(const e of this.component.querySelectorAll("input.search-autocomplete"))e.addEventListener("input",(e=>{this._clearSearchItems(),this._buildSearchItems(e.currentTarget)}),!1),e.addEventListener("keydown",(e=>{this._handleArrowKeys(e)}),!1)}_buildSearchItems(e){const t=window.document.createElement("div");if(t.setAttribute("class","search-items"),this.component.appendChild(t),""!==e.value){for(const s of this.dataList)if(s.substr(0,e.value.length).toUpperCase()===e.value.toUpperCase()){const o=window.document.createElement("div");o.innerHTML=`<strong>${s.substr(0,e.value.length)}</strong>`,o.innerHTML+=s.substr(e.value.length),o.innerHTML+=`<input type="hidden" value="${s}">`,o.addEventListener("click",(t=>{for(const s of t.currentTarget.querySelectorAll('input[type="hidden"]'))e.value=s.value;this._clearSearchItems()}),!1),t.appendChild(o)}}else this._clearSearchItems()}_clearSearchItems(){for(const e of this.component.querySelectorAll(".search-items")){for(const t of e.querySelectorAll("div"))e.removeChild(t);this.component.removeChild(e)}}_handleArrowKeys(e){switch(e.keyCode){case 13:if(this._currentFocus>-1){e.preventDefault();for(const e of this.component.querySelectorAll(".search-items"))for(const t of e.querySelectorAll("div.is-active"))t.click();this._currentFocus=-1}break;case 38:this._currentFocus>0&&(this._currentFocus-=1),this._switchFocus();break;case 40:for(const e of this.component.querySelectorAll(".search-items"))this._currentFocus<e.querySelectorAll("div").length-1&&(this._currentFocus+=1);this._switchFocus()}}_switchFocus(){for(const e of this.component.querySelectorAll(".search-items"))for(const[t,s]of e.querySelectorAll("div").entries())t===this._currentFocus&&s.classList.add("is-active"),t!==this._currentFocus&&s.classList.remove("is-active")}setAutocompleteData(e){this.dataList=e}}var t=null})(),e();var o={};return s(),o})()));
//# sourceMappingURL=input.min.js.map