diff --git a/dist/ux-select.min.iife.js b/dist/ux-select.min.iife.js index 1eec77d..dfa637c 100644 --- a/dist/ux-select.min.iife.js +++ b/dist/ux-select.min.iife.js @@ -1 +1 @@ -var M=Object.defineProperty;var R=(n,c,r)=>c in n?M(n,c,{enumerable:!0,configurable:!0,writable:!0,value:r}):n[c]=r;var g=(n,c,r)=>(R(n,typeof c!="symbol"?c+"":c,r),r),A=(n,c,r)=>{if(!c.has(n))throw TypeError("Cannot "+r)};var l=(n,c,r)=>(A(n,c,"read from private field"),r?r.call(n):c.get(n)),u=(n,c,r)=>{if(c.has(n))throw TypeError("Cannot add the same private member more than once");c instanceof WeakSet?c.add(n):c.set(n,r)},$=(n,c,r,S)=>(A(n,c,"write to private field"),S?S.call(n,r):c.set(n,r),r);var o=(n,c,r)=>(A(n,c,"access private method"),r);var UxSelect=function(){var s,y,B,m,k,b,z,_,F,E,D,L,H,v,P,w,j,C,I,q,N,T,U,O,V;"use strict";const n="";function c(G){const t=new Event("change");G.dispatchEvent(t)}function r(G){const t=new Event("input");G.dispatchEvent(t)}class S{constructor(t,e){u(this,y);u(this,m);u(this,b);u(this,_);u(this,E);u(this,L);u(this,v);u(this,w);u(this,C);u(this,q);u(this,T);u(this,O);g(this,"el");g(this,"config");g(this,"localization");g(this,"state");g(this,"options");g(this,"groups");u(this,s,void 0);this.el=t,this.config={isSearchable:e.isSearchable||!1,isSearchFocus:e.isSearchFocus||!1,isGroupOptions:e.isGroupOptions||!1,optionStyle:e.optionStyle||"default"},this.localization={placeholder:this.el.dataset.placeholder||e.placeholder||"Select an option",searchText:this.el.dataset.searchText||e.searchText||"Search",clearText:this.el.dataset.clearText||e.clearText||"Clear option(s)",selectedText:this.el.dataset.selectedText||e.selectedText||"Selected:"},this.state={multiple:this.el.multiple,disabled:this.el.disabled},this.options=[],this.groups=[],$(this,s,void 0),o(this,y,B).call(this)}disable(){!this.state.disabled&&l(this,s)&&(this.el.disabled=!0,l(this,s).classList.add("-disabled"),this.state.disabled=!0)}enable(){this.state.disabled&&l(this,s)&&(this.el.disabled=!1,l(this,s).classList.remove("-disabled"),this.state.disabled=!1)}update(t=!0){o(this,m,k).call(this),o(this,E,D).call(this),this.el.disabled?this.disable():this.enable(),t&&c(this.el)}clear(){this.options.forEach(t=>{if(t.attributes.selected&&l(this,s)){t.attributes.selected=!1;const e=l(this,s).querySelector(`[data-value="${t.data.value}"]`);e&&e.classList.remove("-selected"),t.element.removeAttribute("selected")}}),o(this,b,z).call(this),c(this.el)}destroy(){l(this,s)&&(l(this,s).remove(),this.el.style.display="")}}return s=new WeakMap,y=new WeakSet,B=function(){o(this,m,k).call(this),o(this,L,H).call(this),o(this,O,V).call(this)},m=new WeakSet,k=function(){const t=this.el.options;if(t.length<=0)throw Error(`Null options at ${this.el}`);this.options=[];for(const e of t){let i;this.config.isGroupOptions?i=e.dataset.uxSelectGroup?e.dataset.uxSelectGroup:"empty":i="empty",this.groups.indexOf(i)===-1&&this.groups.push(i),this.options.push({attributes:{selected:e.getAttribute("selected")!==null,disabled:e.disabled,group:i},data:{text:e.textContent||"",value:e.value},element:e})}},b=new WeakSet,z=function(t=[]){if(l(this,s)){const e=l(this,s).querySelector(".ux-select__title");if(!e)throw Error("selectTitle is null");t.length===1?(e.textContent=t[0],l(this,s).classList.add("-filled")):t.length>0&&this.state.multiple?(e.textContent=`${this.localization.selectedText} ${t.length}`,l(this,s).classList.add("-filled")):(e.textContent=this.localization.placeholder,l(this,s).classList.remove("-filled"))}},_=new WeakSet,F=function(){new Promise(t=>{const e=[];this.options.forEach(i=>{const a=document.createElement("li");if(a.classList.add("ux-select-group__elem"),a.dataset.value=i.data.value,a.textContent=i.data.text,i.attributes.selected&&(a.classList.add("-selected"),e.push(i.data.text!==null?i.data.text:"")),i.attributes.disabled&&a.classList.add("-disabled"),a.addEventListener("click",o(this,q,N).bind(this)),l(this,s)){const d=l(this,s).querySelector(`[data-ux-group="${i.attributes.group}"] .ux-select-group__list`);d&&d.appendChild(a)}}),o(this,b,z).call(this,e),this.config.isGroupOptions?t(!0):t(!1)}).then(t=>{if(t&&l(this,s)){const e=l(this,s).querySelectorAll(".ux-select-group");e.length>0&&e.forEach(i=>{const a=i.querySelector(".ux-select-group__list");if(a){const d=a.querySelectorAll(".ux-select-group__elem"),h=a.querySelectorAll(".ux-select-group__elem.-disabled");d.length===h.length?i.classList.add("-disabled"):i.classList.remove("-disabled")}})}})},E=new WeakSet,D=function(){if(l(this,s)){const t=l(this,s).querySelector(".ux-select__dropdown");if(!t)throw Error("uxSelectList is null");t.innerHTML="",this.groups.forEach(e=>{const i=document.createElement("div");if(i.classList.add("ux-select__group","ux-select-group"),e==="empty"&&i.classList.add("-empty"),i.dataset.uxGroup=e,e!=="empty"){const d=document.createElement("div");d.classList.add("ux-select-group__title"),d.textContent=e,i.appendChild(d)}const a=document.createElement("ul");a.classList.add("ux-select-group__list"),i.appendChild(a),t.appendChild(i)}),o(this,_,F).call(this)}},L=new WeakSet,H=function(){this.el.style.display="none";const t=document.createElement("div");t.classList.add("ux-select__head");const e=document.createElement("div");e.classList.add("ux-select__title"),e.textContent=this.localization.placeholder;const i=document.createElement("button");i.type="button",i.classList.add("ux-select__clear"),i.title=this.localization.clearText,t.append(e,i);const a=document.createElement("div");if(a.classList.add("ux-select__body"),this.config.isSearchable){const f=document.createElement("div");f.classList.add("ux-select__search");const x=document.createElement("input");x.type="search",x.classList.add("ux-select-search__input"),x.placeholder=this.localization.searchText,f.appendChild(x),a.appendChild(f)}const d=document.createElement("div");d.classList.add("ux-select__dropdown"),a.appendChild(d);const h=document.createElement("div"),p=["ux-select",this.el.classList];this.state.multiple&&p.push("-multiple"),this.state.disabled&&p.push("-disabled"),this.config.optionStyle!=="default"&&p.push(`-${this.config.optionStyle}`),h.className=p.join(" "),h.append(t,a),this.el.insertAdjacentElement("afterend",h),this.el.nextElementSibling&&$(this,s,this.el.nextElementSibling),o(this,E,D).call(this)},v=new WeakSet,P=function(t){t.preventDefault();const e=t.target;if(this.state.disabled)return;if(!l(this,s))throw Error("uxEl is null");const i=l(this,s).querySelector(".ux-select__body");if(!i)throw Error("uxSelectBody is null");if(t.target!==l(this,s).querySelector(".ux-select__clear")&&!i.contains(e)){if(l(this,s).classList.contains("-shown"))l(this,s).classList.remove("-shown");else if(l(this,s).classList.add("-shown"),this.config.isSearchable){const a=l(this,s).querySelector(".ux-select-search__input");if(!a)throw Error("Search input is null");a.value="",a.dispatchEvent(new Event("input")),this.config.isSearchFocus&&a.focus()}}},w=new WeakSet,j=function(t){const e=t.target;if(!l(this,s))throw Error("uxEl is null");l(this,s).contains(e)||l(this,s).classList.remove("-shown")},C=new WeakSet,I=function(t){return t.preventDefault(),this.state.disabled?!1:this.clear()},q=new WeakSet,N=function(t){if(t.preventDefault(),t.target!==null){const e=t.target;if(e.classList.contains("-disabled"))return!1;if(this.state.multiple){t.stopPropagation();const i=this.el.querySelector(`[value="${e.dataset.value}"]`);if(!i)throw Error("Option equal this value not found");e.classList.contains("-selected")?i.removeAttribute("selected"):i.setAttribute("selected","")}else if(!e.classList.contains("-selected"))this.el.querySelectorAll("option").forEach(i=>{i.value===e.dataset.value?i.setAttribute("selected",""):i.removeAttribute("selected")});else return!1;return this.update()}},T=new WeakSet,U=function(t){if(t.target!==null&&l(this,s)){const e=t.target,i=e.value.toLowerCase(),a=l(this,s).querySelectorAll(".ux-select-group");if(i==="")l(this,s).querySelectorAll(".ux-select-group__elem").forEach(h=>h.style.display=""),this.config.isGroupOptions&&a.forEach(h=>h.style.display="");else{const d=new RegExp(i);new Promise(h=>{this.options.forEach(p=>{if(!l(this,s))throw Error("uxEl is null");const f=d.test(p.data.text.toLowerCase()),x=l(this,s).querySelector(`[data-value="${p.data.value}"]`);if(!x)throw Error("uxOption is null");x.style.display=f?"":"none"}),this.config.isGroupOptions?h(!0):h(!1)}).then(h=>{h&&a.forEach(p=>{const f=p.querySelector(".ux-select-group__list");p.style.display="",f&&(p.style.display=f.clientHeight!==0?"":"none")})})}r(this.el)}},O=new WeakSet,V=function(){if(l(this,s)){l(this,s).addEventListener("click",o(this,v,P).bind(this));const t=l(this,s).querySelector(".ux-select__clear");if(t&&t.addEventListener("click",o(this,C,I).bind(this)),window.addEventListener("click",o(this,w,j).bind(this)),this.config.isSearchable){const e=l(this,s).querySelector(".ux-select-search__input");e&&e.addEventListener("input",o(this,T,U).bind(this))}}},S}(); +var R=Object.defineProperty;var W=(n,c,o)=>c in n?R(n,c,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[c]=o;var m=(n,c,o)=>(W(n,typeof c!="symbol"?c+"":c,o),o),k=(n,c,o)=>{if(!c.has(n))throw TypeError("Cannot "+o)};var s=(n,c,o)=>(k(n,c,"read from private field"),o?o.call(n):c.get(n)),d=(n,c,o)=>{if(c.has(n))throw TypeError("Cannot add the same private member more than once");c instanceof WeakSet?c.add(n):c.set(n,o)},$=(n,c,o,y)=>(k(n,c,"write to private field"),y?y.call(n,o):c.set(n,o),o);var r=(n,c,o)=>(k(n,c,"access private method"),o);var UxSelect=function(){var p,i,_,F,S,z,b,D,L,H,E,B,v,P,w,j,C,I,T,N,q,U,O,V,G,M;"use strict";const n="";function c(A){const e=new Event("change");A.dispatchEvent(e)}function o(A){const e=new Event("input");A.dispatchEvent(e)}class y{constructor(e,t){d(this,_);d(this,S);d(this,b);d(this,L);d(this,E);d(this,v);d(this,w);d(this,C);d(this,T);d(this,q);d(this,O);d(this,G);m(this,"el");d(this,p,void 0);m(this,"config");m(this,"localization");m(this,"state");m(this,"options");m(this,"groups");d(this,i,void 0);this.el=e,$(this,p,t||{isSearchable:!1,isSearchFocus:!1,isGroupOptions:!1,optionStyle:"default",placeholder:"Select an option",searchText:"Search",clearText:"Clear option(s)",selectedText:"Selected:"}),this.config={isSearchable:s(this,p).isSearchable||!1,isSearchFocus:s(this,p).isSearchFocus||!1,isGroupOptions:s(this,p).isGroupOptions||!1,optionStyle:s(this,p).optionStyle||"default"},this.localization={placeholder:s(this,p).placeholder||this.el.dataset.placeholder||"Select an option",searchText:s(this,p).searchText||this.el.dataset.searchText||"Search",clearText:s(this,p).clearText||this.el.dataset.clearText||"Clear option(s)",selectedText:s(this,p).selectedText||this.el.dataset.selectedText||"Selected:"},this.state={multiple:this.el.multiple,disabled:this.el.disabled},this.options=[],this.groups=[],$(this,i,void 0),r(this,_,F).call(this)}disable(){!this.state.disabled&&s(this,i)&&(this.el.disabled=!0,s(this,i).classList.add("-disabled"),this.state.disabled=!0)}enable(){this.state.disabled&&s(this,i)&&(this.el.disabled=!1,s(this,i).classList.remove("-disabled"),this.state.disabled=!1)}update(e=!0){r(this,S,z).call(this),r(this,E,B).call(this),this.el.disabled?this.disable():this.enable(),e&&c(this.el)}clear(){this.options.forEach(e=>{if(e.attributes.selected&&s(this,i)){e.attributes.selected=!1;const t=s(this,i).querySelector(`[data-value="${e.data.value}"]`);t&&t.classList.remove("-selected"),e.element.removeAttribute("selected")}}),r(this,b,D).call(this),c(this.el)}destroy(){s(this,i)&&(s(this,i).remove(),this.el.style.display="")}}return p=new WeakMap,i=new WeakMap,_=new WeakSet,F=function(){r(this,S,z).call(this),r(this,v,P).call(this),r(this,G,M).call(this)},S=new WeakSet,z=function(){const e=this.el.options;if(e.length<=0)throw Error(`Null options at ${this.el}`);this.options=[];for(const t of e){let l;this.config.isGroupOptions?l=t.dataset.uxSelectGroup?t.dataset.uxSelectGroup:"empty":l="empty",this.groups.indexOf(l)===-1&&this.groups.push(l),this.options.push({attributes:{selected:t.getAttribute("selected")!==null,disabled:t.disabled,group:l},data:{text:t.textContent||"",value:t.value},element:t})}},b=new WeakSet,D=function(e=[]){if(s(this,i)){const t=s(this,i).querySelector(".ux-select__title");if(!t)throw Error("selectTitle is null");e.length===1?(t.textContent=e[0],s(this,i).classList.add("-filled")):e.length>0&&this.state.multiple?(t.textContent=`${this.localization.selectedText} ${e.length}`,s(this,i).classList.add("-filled")):(t.textContent=this.localization.placeholder,s(this,i).classList.remove("-filled"))}},L=new WeakSet,H=function(){new Promise(e=>{const t=[];this.options.forEach(l=>{const a=document.createElement("li");if(a.classList.add("ux-select-group__elem"),a.dataset.value=l.data.value,a.textContent=l.data.text,l.attributes.selected&&(a.classList.add("-selected"),t.push(l.data.text!==null?l.data.text:"")),l.attributes.disabled&&a.classList.add("-disabled"),a.addEventListener("click",r(this,q,U).bind(this)),s(this,i)){const h=s(this,i).querySelector(`[data-ux-group="${l.attributes.group}"] .ux-select-group__list`);h&&h.appendChild(a)}}),r(this,b,D).call(this,t),this.config.isGroupOptions?e(!0):e(!1)}).then(e=>{if(e&&s(this,i)){const t=s(this,i).querySelectorAll(".ux-select-group");t.length>0&&t.forEach(l=>{const a=l.querySelector(".ux-select-group__list");if(a){const h=a.querySelectorAll(".ux-select-group__elem"),u=a.querySelectorAll(".ux-select-group__elem.-disabled");h.length===u.length?l.classList.add("-disabled"):l.classList.remove("-disabled")}})}})},E=new WeakSet,B=function(){if(s(this,i)){const e=s(this,i).querySelector(".ux-select__dropdown");if(!e)throw Error("uxSelectList is null");e.innerHTML="",this.groups.forEach(t=>{const l=document.createElement("div");if(l.classList.add("ux-select__group","ux-select-group"),t==="empty"&&l.classList.add("-empty"),l.dataset.uxGroup=t,t!=="empty"){const h=document.createElement("div");h.classList.add("ux-select-group__title"),h.textContent=t,l.appendChild(h)}const a=document.createElement("ul");a.classList.add("ux-select-group__list"),l.appendChild(a),e.appendChild(l)}),r(this,L,H).call(this)}},v=new WeakSet,P=function(){this.el.style.display="none";const e=document.createElement("div");e.classList.add("ux-select__head");const t=document.createElement("div");t.classList.add("ux-select__title"),t.textContent=this.localization.placeholder;const l=document.createElement("button");l.type="button",l.classList.add("ux-select__clear"),l.title=this.localization.clearText,e.append(t,l);const a=document.createElement("div");if(a.classList.add("ux-select__body"),this.config.isSearchable){const x=document.createElement("div");x.classList.add("ux-select__search");const g=document.createElement("input");g.type="search",g.classList.add("ux-select-search__input"),g.placeholder=this.localization.searchText,x.appendChild(g),a.appendChild(x)}const h=document.createElement("div");h.classList.add("ux-select__dropdown"),a.appendChild(h);const u=document.createElement("div"),f=["ux-select",this.el.classList];this.state.multiple&&f.push("-multiple"),this.state.disabled&&f.push("-disabled"),this.config.optionStyle!=="default"&&f.push(`-${this.config.optionStyle}`),u.className=f.join(" "),u.append(e,a),this.el.insertAdjacentElement("afterend",u),this.el.nextElementSibling&&$(this,i,this.el.nextElementSibling),r(this,E,B).call(this)},w=new WeakSet,j=function(e){e.preventDefault();const t=e.target;if(this.state.disabled)return;if(!s(this,i))throw Error("uxEl is null");const l=s(this,i).querySelector(".ux-select__body");if(!l)throw Error("uxSelectBody is null");if(e.target!==s(this,i).querySelector(".ux-select__clear")&&!l.contains(t)){if(s(this,i).classList.contains("-shown"))s(this,i).classList.remove("-shown");else if(s(this,i).classList.add("-shown"),this.config.isSearchable){const a=s(this,i).querySelector(".ux-select-search__input");if(!a)throw Error("Search input is null");a.value="",a.dispatchEvent(new Event("input")),this.config.isSearchFocus&&a.focus()}}},C=new WeakSet,I=function(e){const t=e.target;if(!s(this,i))throw Error("uxEl is null");s(this,i).contains(t)||s(this,i).classList.remove("-shown")},T=new WeakSet,N=function(e){return e.preventDefault(),this.state.disabled?!1:this.clear()},q=new WeakSet,U=function(e){if(e.preventDefault(),e.target!==null){const t=e.target;if(t.classList.contains("-disabled"))return!1;if(this.state.multiple){e.stopPropagation();const l=this.el.querySelector(`[value="${t.dataset.value}"]`);if(!l)throw Error("Option equal this value not found");t.classList.contains("-selected")?l.removeAttribute("selected"):l.setAttribute("selected","")}else if(!t.classList.contains("-selected"))this.el.querySelectorAll("option").forEach(l=>{l.value===t.dataset.value?l.setAttribute("selected",""):l.removeAttribute("selected")});else return!1;return this.update()}},O=new WeakSet,V=function(e){if(e.target!==null&&s(this,i)){const t=e.target,l=t.value.toLowerCase(),a=s(this,i).querySelectorAll(".ux-select-group");if(l==="")s(this,i).querySelectorAll(".ux-select-group__elem").forEach(u=>u.style.display=""),this.config.isGroupOptions&&a.forEach(u=>u.style.display="");else{const h=new RegExp(l);new Promise(u=>{this.options.forEach(f=>{if(!s(this,i))throw Error("uxEl is null");const x=h.test(f.data.text.toLowerCase()),g=s(this,i).querySelector(`[data-value="${f.data.value}"]`);if(!g)throw Error("uxOption is null");g.style.display=x?"":"none"}),this.config.isGroupOptions?u(!0):u(!1)}).then(u=>{u&&a.forEach(f=>{const x=f.querySelector(".ux-select-group__list");f.style.display="",x&&(f.style.display=x.clientHeight!==0?"":"none")})})}o(this.el)}},G=new WeakSet,M=function(){if(s(this,i)){s(this,i).addEventListener("click",r(this,w,j).bind(this));const e=s(this,i).querySelector(".ux-select__clear");if(e&&e.addEventListener("click",r(this,T,N).bind(this)),window.addEventListener("click",r(this,C,I).bind(this)),this.config.isSearchable){const t=s(this,i).querySelector(".ux-select-search__input");t&&t.addEventListener("input",r(this,O,V).bind(this))}}},y}(); diff --git a/dist/ux-select.min.js b/dist/ux-select.min.js index 23a18bc..5e2f772 100644 --- a/dist/ux-select.min.js +++ b/dist/ux-select.min.js @@ -1,26 +1,26 @@ (function(){"use strict";try{if(typeof document<"u"){var e=document.createElement("style");e.appendChild(document.createTextNode(`:root{--uxs-padding-x: 12px;--uxs-padding-y: 6px;--uxs-background-color: rgb(255, 255, 255);--uxs-text-color: rgb(34, 34, 34);--uxs-font-size: 16px;--uxs-line-height: 1.5;--uxs-border-width: 1px;--uxs-border-radius: 6px;--uxs-border-color: rgb(233, 233, 233);--uxs-border-hover-color: rgb(189, 193, 198);--uxs-option-hover-bg-color: rgb(244, 244, 244);--uxs-option-select-bg-color: rgb(233, 233, 233);--uxs-option-disable-color: rgb(130, 130, 130);--uxs-option-styled-outline-width: 1px;--uxs-option-styled-checkbox-outline-radius: 4px;--uxs-option-styled-border-width: 3px;--uxs-option-styled-bg: rgb(0, 224, 255);--uxs-dropdown-image: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 24 24' style='enable-background:new 0 0 24 24;' xml:space='preserve'%3E%3Cg%3E%3Cpath d='M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4 l-6,6C12.5,15.9,12.3,16,12,16z'/%3E%3C/g%3E%3C/svg%3E%0A");--uxs-dropdown-image-disable: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 24 24' style='enable-background:new 0 0 24 24;' xml:space='preserve'%3E%3Cg%3E%3Cpath fill='rgb(130 130 130)' d='M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4 l-6,6C12.5,15.9,12.3,16,12,16z'/%3E%3C/g%3E%3C/svg%3E%0A");--uxs-clear-image: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 24 24' style='enable-background:new 0 0 24 24;' xml:space='preserve'%3E%3Cg%3E%3Cpath d='M6,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l12-12c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-12,12C6.5,18.9,6.3,19,6,19z' /%3E%3Cpath d='M18,19c-0.3,0-0.5-0.1-0.7-0.3l-12-12c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l12,12c0.4,0.4,0.4,1,0,1.4 C18.5,18.9,18.3,19,18,19z'/%3E%3C/g%3E%3C/svg%3E%0A");--uxs-clear-image-disable: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 24 24' style='enable-background:new 0 0 24 24;' xml:space='preserve'%3E%3Cg%3E%3Cpath fill='rgb(130 130 130)' d='M6,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l12-12c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-12,12C6.5,18.9,6.3,19,6,19z' /%3E%3Cpath fill='rgb(130 130 130)' d='M18,19c-0.3,0-0.5-0.1-0.7-0.3l-12-12c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l12,12c0.4,0.4,0.4,1,0,1.4 C18.5,18.9,18.3,19,18,19z'/%3E%3C/g%3E%3C/svg%3E%0A");--uxs-search-bg: rgb(255, 255, 255);--uxs-search-color: rgb(34, 34, 34);--uxs-search-placeholder: rgb(169, 169, 169);--uxs-transition-duration: .2s;--uxs-transition-easing: ease-in-out;--uxs-scrollbar-track-color: rgb(189, 189, 189);--uxs-scrollbar-thumb-color: rgb(117, 117, 117);--uxs-scrollbar-thumb-hover-color: rgb(97, 97, 97)}.ux-select{cursor:pointer;position:relative;display:block;width:100%;margin:0;padding:var(--uxs-padding-y) var(--uxs-padding-x);border:var(--uxs-border-width) solid var(--uxs-border-color);border-radius:var(--uxs-border-radius);background-color:var(--uxs-background-color);font-size:var(--uxs-font-size);line-height:var(--uxs-line-height);color:var(--uxs-text-color);transition:border-color var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select.-disabled{cursor:not-allowed;color:var(--uxs-option-disable-color)}.ux-select.-filled .ux-select__clear{display:block}.ux-select:not(.-disabled).-focus{border-color:var(--uxs-border-hover-color)}.ux-select:not(.-disabled).-shown{border-color:var(--uxs-border-hover-color)}.ux-select:not(.-disabled).-shown .ux-select__head:after{transform:rotate(-180deg)}.ux-select:not(.-disabled).-shown .ux-select__body{pointer-events:auto;opacity:1;transform:scale(1) translateY(0)}.ux-select__head{position:relative;display:flex;align-items:center}.ux-select__head:after{content:"";display:flex;align-items:center;justify-content:center;min-width:16px;width:16px;height:16px;margin-left:8px;background:transparent var(--uxs-dropdown-image) center no-repeat;transition:transform var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select.-disabled .ux-select__head:after{background-image:var(--uxs-dropdown-image-disable)}.ux-select__title{user-select:none;flex-grow:1;white-space:nowrap}.ux-select__clear{cursor:pointer;display:none;min-width:16px;width:16px;height:16px;margin-left:8px;border:none;outline:none;background:transparent var(--uxs-clear-image) center no-repeat}.ux-select.-disabled .ux-select__clear{cursor:not-allowed;background-image:var(--uxs-clear-image-disable)}.ux-select__body{pointer-events:none;position:absolute;z-index:5;top:calc(100% + 5px);left:0;overflow:hidden;width:100%;border:var(--uxs-border-width) solid var(--uxs-border-color);border-radius:var(--uxs-border-radius);background-color:var(--uxs-background-color);opacity:0;transform:scale(.75) translateY(-12px);transition:opacity var(--uxs-transition-duration) var(--uxs-transition-easing),transform var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select__search{padding:4px}.ux-select-search__input{width:100%;padding:var(--uxs-padding-y) calc(var(--uxs-padding-x) - 3px);border:var(--uxs-border-width) solid var(--uxs-border-color);border-radius:var(--uxs-border-radius);background-color:var(--uxs-search-bg);color:var(--uxs-search-color);transition:border-color var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select-search__input::placeholder{color:var(--uxs-search-placeholder)}.ux-select-search__input::-ms-clear,.ux-select-search__input::-ms-reveal{display:none;width:0;height:0}.ux-select-search__input::-webkit-search-decoration,.ux-select-search__input::-webkit-search-cancel-button,.ux-select-search__input::-webkit-search-results-button,.ux-select-search__input::-webkit-search-results-decoration{-webkit-appearance:none;appearance:none}.ux-select-search__input:focus-visible{outline:none}.ux-select-search__input:focus{border-color:var(--uxs-border-hover-color)}.ux-select__dropdown{max-height:calc((var(--uxs-font-size) * var(--uxs-line-height) + var(--uxs-padding-y) * 2) * 5);overflow-y:auto;scroll-behavior:smooth;scrollbar-width:thin;scrollbar-color:var(--uxs-scrollbar-track-color) var(--uxs-scrollbar-thumb-color)}.ux-select__dropdown::-webkit-scrollbar{position:absolute;width:7px;height:7px}.ux-select__dropdown::-webkit-scrollbar-track{background-color:var(--uxs-scrollbar-track-color)}.ux-select__dropdown::-webkit-scrollbar-thumb{background-color:var(--uxs-scrollbar-thumb-color)}.ux-select__dropdown::-webkit-scrollbar-thumb:hover{background-color:var(--uxs-scrollbar-thumb-hover-color)}.ux-select-group.-disabled .ux-select-group__title{color:var(--uxs-option-disable-color)}.ux-select-group.-empty .ux-select-group__elem{padding:var(--uxs-padding-y) var(--uxs-padding-x)}.ux-select-group__title{cursor:default;padding:var(--uxs-padding-y) var(--uxs-padding-x);font-weight:700}.ux-select-group__list{margin:0;padding:0;list-style:none}.ux-select-group__elem{user-select:none;padding:var(--uxs-padding-y) var(--uxs-padding-x) var(--uxs-padding-y) calc(var(--uxs-padding-x) * 2);background-color:var(--uxs-background-color);transition:background-color var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select.-radio .ux-select-group__elem,.ux-select.-checkbox .ux-select-group__elem{display:flex;align-items:center;justify-content:space-between}.ux-select.-radio .ux-select-group__elem:after,.ux-select.-checkbox .ux-select-group__elem:after{content:"";display:flex;align-items:center;justify-content:center;width:var(--uxs-font-size);height:var(--uxs-font-size);outline:var(--uxs-option-styled-outline-width) solid var(--uxs-option-styled-bg);border:var(--uxs-option-styled-border-width) solid var(--uxs-background-color);transition:background-color var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select.-radio .ux-select-group__elem:after{border-radius:50%}.ux-select.-checkbox .ux-select-group__elem:after{border-radius:var(--uxs-option-styled-checkbox-outline-radius)}.ux-select-group__elem.-disabled{cursor:not-allowed;color:var(--uxs-option-disable-color)}.ux-select-group__elem:not(.-disabled):hover{background-color:var(--uxs-option-hover-bg-color)}.ux-select.-radio .ux-select-group__elem:not(.-disabled):hover:after,.ux-select.-checkbox .ux-select-group__elem:not(.-disabled):hover:after{border:var(--uxs-option-styled-border-width) solid var(--uxs-option-hover-bg-color)}.ux-select-group__elem:not(.-disabled).-selected{font-weight:700;background-color:var(--uxs-option-select-bg-color)}.ux-select:not(.-multiple) .ux-select-group__elem:not(.-disabled).-selected{cursor:default}.ux-select.-radio .ux-select-group__elem.-selected:after,.ux-select.-checkbox .ux-select-group__elem.-selected:after{border:var(--uxs-option-styled-border-width) solid var(--uxs-option-hover-bg-color);background-color:var(--uxs-option-styled-bg)}`)),document.head.appendChild(e)}}catch(r){console.error("vite-plugin-css-injected-by-js",r)}})(); -var N = Object.defineProperty; -var V = (c, e, t) => e in c ? N(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t; -var f = (c, e, t) => (V(c, typeof e != "symbol" ? e + "" : e, t), t), q = (c, e, t) => { +var V = Object.defineProperty; +var M = (c, e, t) => e in c ? V(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t; +var x = (c, e, t) => (M(c, typeof e != "symbol" ? e + "" : e, t), t), O = (c, e, t) => { if (!e.has(c)) throw TypeError("Cannot " + t); }; -var l = (c, e, t) => (q(c, e, "read from private field"), t ? t.call(c) : e.get(c)), d = (c, e, t) => { +var s = (c, e, t) => (O(c, e, "read from private field"), t ? t.call(c) : e.get(c)), n = (c, e, t) => { if (e.has(c)) throw TypeError("Cannot add the same private member more than once"); e instanceof WeakSet ? e.add(c) : e.set(c, t); -}, T = (c, e, t, s) => (q(c, e, "write to private field"), s ? s.call(c, t) : e.set(c, t), t); -var n = (c, e, t) => (q(c, e, "access private method"), t); -function $(c) { +}, b = (c, e, t, i) => (O(c, e, "write to private field"), i ? i.call(c, t) : e.set(c, t), t); +var o = (c, e, t) => (O(c, e, "access private method"), t); +function k(c) { const e = new Event("change"); c.dispatchEvent(e); } -function M(c) { +function R(c) { const e = new Event("input"); c.dispatchEvent(e); } -var i, b, k, x, O, g, G, E, z, m, A, S, D, y, B, _, F, L, H, v, P, w, j, C, I; -class U { +var h, l, E, z, g, G, m, A, y, D, S, $, _, B, L, F, v, H, w, P, C, j, T, I, q, N; +class W { /** * @param {HTMLSelectElement} element Select element * @param {Object} [options] Props @@ -34,80 +34,90 @@ class U { * @param {String} [options.selectedText] Change text on multiple select, when user select more than 1 option * */ constructor(e, t) { - d(this, b); - d(this, x); - d(this, g); - d(this, E); - d(this, m); - d(this, S); - d(this, y); - d(this, _); - d(this, L); - d(this, v); - d(this, w); - d(this, C); - f(this, "el"); - f(this, "config"); - f(this, "localization"); - f(this, "state"); - f(this, "options"); - f(this, "groups"); - d(this, i, void 0); - this.el = e, this.config = { - isSearchable: t.isSearchable || !1, - isSearchFocus: t.isSearchFocus || !1, - isGroupOptions: t.isGroupOptions || !1, - optionStyle: t.optionStyle || "default" + n(this, E); + n(this, g); + n(this, m); + n(this, y); + n(this, S); + n(this, _); + n(this, L); + n(this, v); + n(this, w); + n(this, C); + n(this, T); + n(this, q); + x(this, "el"); + n(this, h, void 0); + x(this, "config"); + x(this, "localization"); + x(this, "state"); + x(this, "options"); + x(this, "groups"); + n(this, l, void 0); + this.el = e, b(this, h, t || { + isSearchable: !1, + isSearchFocus: !1, + isGroupOptions: !1, + optionStyle: "default", + placeholder: "Select an option", + searchText: "Search", + clearText: "Clear option(s)", + selectedText: "Selected:" + }), this.config = { + isSearchable: s(this, h).isSearchable || !1, + isSearchFocus: s(this, h).isSearchFocus || !1, + isGroupOptions: s(this, h).isGroupOptions || !1, + optionStyle: s(this, h).optionStyle || "default" }, this.localization = { - placeholder: this.el.dataset.placeholder || t.placeholder || "Select an option", - searchText: this.el.dataset.searchText || t.searchText || "Search", - clearText: this.el.dataset.clearText || t.clearText || "Clear option(s)", - selectedText: this.el.dataset.selectedText || t.selectedText || "Selected:" + placeholder: s(this, h).placeholder || this.el.dataset.placeholder || "Select an option", + searchText: s(this, h).searchText || this.el.dataset.searchText || "Search", + clearText: s(this, h).clearText || this.el.dataset.clearText || "Clear option(s)", + selectedText: s(this, h).selectedText || this.el.dataset.selectedText || "Selected:" }, this.state = { multiple: this.el.multiple, disabled: this.el.disabled - }, this.options = [], this.groups = [], T(this, i, void 0), n(this, b, k).call(this); + }, this.options = [], this.groups = [], b(this, l, void 0), o(this, E, z).call(this); } disable() { - !this.state.disabled && l(this, i) && (this.el.disabled = !0, l(this, i).classList.add("-disabled"), this.state.disabled = !0); + !this.state.disabled && s(this, l) && (this.el.disabled = !0, s(this, l).classList.add("-disabled"), this.state.disabled = !0); } enable() { - this.state.disabled && l(this, i) && (this.el.disabled = !1, l(this, i).classList.remove("-disabled"), this.state.disabled = !1); + this.state.disabled && s(this, l) && (this.el.disabled = !1, s(this, l).classList.remove("-disabled"), this.state.disabled = !1); } /** * * @param {Boolean} [isTriggerChange] Skip triggering "change" event */ update(e = !0) { - n(this, x, O).call(this), n(this, m, A).call(this), this.el.disabled ? this.disable() : this.enable(), e && $(this.el); + o(this, g, G).call(this), o(this, S, $).call(this), this.el.disabled ? this.disable() : this.enable(), e && k(this.el); } clear() { this.options.forEach((e) => { - if (e.attributes.selected && l(this, i)) { + if (e.attributes.selected && s(this, l)) { e.attributes.selected = !1; - const t = l(this, i).querySelector(`[data-value="${e.data.value}"]`); + const t = s(this, l).querySelector(`[data-value="${e.data.value}"]`); t && t.classList.remove("-selected"), e.element.removeAttribute("selected"); } - }), n(this, g, G).call(this), $(this.el); + }), o(this, m, A).call(this), k(this.el); } destroy() { - l(this, i) && (l(this, i).remove(), this.el.style.display = ""); + s(this, l) && (s(this, l).remove(), this.el.style.display = ""); } } -i = new WeakMap(), b = new WeakSet(), k = function() { - n(this, x, O).call(this), n(this, S, D).call(this), n(this, C, I).call(this); -}, x = new WeakSet(), O = function() { +h = new WeakMap(), l = new WeakMap(), E = new WeakSet(), z = function() { + o(this, g, G).call(this), o(this, _, B).call(this), o(this, q, N).call(this); +}, g = new WeakSet(), G = function() { const e = this.el.options; if (e.length <= 0) throw Error(`Null options at ${this.el}`); this.options = []; for (const t of e) { - let s; - this.config.isGroupOptions ? s = t.dataset.uxSelectGroup ? t.dataset.uxSelectGroup : "empty" : s = "empty", this.groups.indexOf(s) === -1 && this.groups.push(s), this.options.push({ + let i; + this.config.isGroupOptions ? i = t.dataset.uxSelectGroup ? t.dataset.uxSelectGroup : "empty" : i = "empty", this.groups.indexOf(i) === -1 && this.groups.push(i), this.options.push({ attributes: { selected: t.getAttribute("selected") !== null, disabled: t.disabled, - group: s + group: i }, data: { text: t.textContent || "", @@ -116,152 +126,152 @@ i = new WeakMap(), b = new WeakSet(), k = function() { element: t }); } -}, g = new WeakSet(), G = function(e = []) { - if (l(this, i)) { - const t = l(this, i).querySelector(".ux-select__title"); +}, m = new WeakSet(), A = function(e = []) { + if (s(this, l)) { + const t = s(this, l).querySelector(".ux-select__title"); if (!t) throw Error("selectTitle is null"); - e.length === 1 ? (t.textContent = e[0], l(this, i).classList.add("-filled")) : e.length > 0 && this.state.multiple ? (t.textContent = `${this.localization.selectedText} ${e.length}`, l(this, i).classList.add("-filled")) : (t.textContent = this.localization.placeholder, l(this, i).classList.remove("-filled")); + e.length === 1 ? (t.textContent = e[0], s(this, l).classList.add("-filled")) : e.length > 0 && this.state.multiple ? (t.textContent = `${this.localization.selectedText} ${e.length}`, s(this, l).classList.add("-filled")) : (t.textContent = this.localization.placeholder, s(this, l).classList.remove("-filled")); } -}, E = new WeakSet(), z = function() { +}, y = new WeakSet(), D = function() { new Promise((e) => { const t = []; - this.options.forEach((s) => { + this.options.forEach((i) => { const a = document.createElement("li"); - if (a.classList.add("ux-select-group__elem"), a.dataset.value = s.data.value, a.textContent = s.data.text, s.attributes.selected && (a.classList.add("-selected"), t.push(s.data.text !== null ? s.data.text : "")), s.attributes.disabled && a.classList.add("-disabled"), a.addEventListener("click", n(this, v, P).bind(this)), l(this, i)) { - const r = l(this, i).querySelector( - `[data-ux-group="${s.attributes.group}"] .ux-select-group__list` + if (a.classList.add("ux-select-group__elem"), a.dataset.value = i.data.value, a.textContent = i.data.text, i.attributes.selected && (a.classList.add("-selected"), t.push(i.data.text !== null ? i.data.text : "")), i.attributes.disabled && a.classList.add("-disabled"), a.addEventListener("click", o(this, C, j).bind(this)), s(this, l)) { + const r = s(this, l).querySelector( + `[data-ux-group="${i.attributes.group}"] .ux-select-group__list` ); r && r.appendChild(a); } - }), n(this, g, G).call(this, t), this.config.isGroupOptions ? e(!0) : e(!1); + }), o(this, m, A).call(this, t), this.config.isGroupOptions ? e(!0) : e(!1); }).then((e) => { - if (e && l(this, i)) { - const t = l(this, i).querySelectorAll(".ux-select-group"); - t.length > 0 && t.forEach((s) => { - const a = s.querySelector(".ux-select-group__list"); + if (e && s(this, l)) { + const t = s(this, l).querySelectorAll(".ux-select-group"); + t.length > 0 && t.forEach((i) => { + const a = i.querySelector(".ux-select-group__list"); if (a) { - const r = a.querySelectorAll(".ux-select-group__elem"), o = a.querySelectorAll(".ux-select-group__elem.-disabled"); - r.length === o.length ? s.classList.add("-disabled") : s.classList.remove("-disabled"); + const r = a.querySelectorAll(".ux-select-group__elem"), d = a.querySelectorAll(".ux-select-group__elem.-disabled"); + r.length === d.length ? i.classList.add("-disabled") : i.classList.remove("-disabled"); } }); } }); -}, m = new WeakSet(), A = function() { - if (l(this, i)) { - const e = l(this, i).querySelector(".ux-select__dropdown"); +}, S = new WeakSet(), $ = function() { + if (s(this, l)) { + const e = s(this, l).querySelector(".ux-select__dropdown"); if (!e) throw Error("uxSelectList is null"); e.innerHTML = "", this.groups.forEach((t) => { - const s = document.createElement("div"); - if (s.classList.add("ux-select__group", "ux-select-group"), t === "empty" && s.classList.add("-empty"), s.dataset.uxGroup = t, t !== "empty") { + const i = document.createElement("div"); + if (i.classList.add("ux-select__group", "ux-select-group"), t === "empty" && i.classList.add("-empty"), i.dataset.uxGroup = t, t !== "empty") { const r = document.createElement("div"); - r.classList.add("ux-select-group__title"), r.textContent = t, s.appendChild(r); + r.classList.add("ux-select-group__title"), r.textContent = t, i.appendChild(r); } const a = document.createElement("ul"); - a.classList.add("ux-select-group__list"), s.appendChild(a), e.appendChild(s); - }), n(this, E, z).call(this); + a.classList.add("ux-select-group__list"), i.appendChild(a), e.appendChild(i); + }), o(this, y, D).call(this); } -}, S = new WeakSet(), D = function() { +}, _ = new WeakSet(), B = function() { this.el.style.display = "none"; const e = document.createElement("div"); e.classList.add("ux-select__head"); const t = document.createElement("div"); t.classList.add("ux-select__title"), t.textContent = this.localization.placeholder; - const s = document.createElement("button"); - s.type = "button", s.classList.add("ux-select__clear"), s.title = this.localization.clearText, e.append(t, s); + const i = document.createElement("button"); + i.type = "button", i.classList.add("ux-select__clear"), i.title = this.localization.clearText, e.append(t, i); const a = document.createElement("div"); if (a.classList.add("ux-select__body"), this.config.isSearchable) { - const u = document.createElement("div"); - u.classList.add("ux-select__search"); - const p = document.createElement("input"); - p.type = "search", p.classList.add("ux-select-search__input"), p.placeholder = this.localization.searchText, u.appendChild(p), a.appendChild(u); + const p = document.createElement("div"); + p.classList.add("ux-select__search"); + const f = document.createElement("input"); + f.type = "search", f.classList.add("ux-select-search__input"), f.placeholder = this.localization.searchText, p.appendChild(f), a.appendChild(p); } const r = document.createElement("div"); r.classList.add("ux-select__dropdown"), a.appendChild(r); - const o = document.createElement("div"), h = ["ux-select", this.el.classList]; - this.state.multiple && h.push("-multiple"), this.state.disabled && h.push("-disabled"), this.config.optionStyle !== "default" && h.push(`-${this.config.optionStyle}`), o.className = h.join(" "), o.append(e, a), this.el.insertAdjacentElement("afterend", o), this.el.nextElementSibling && T(this, i, this.el.nextElementSibling), n(this, m, A).call(this); -}, y = new WeakSet(), B = function(e) { + const d = document.createElement("div"), u = ["ux-select", this.el.classList]; + this.state.multiple && u.push("-multiple"), this.state.disabled && u.push("-disabled"), this.config.optionStyle !== "default" && u.push(`-${this.config.optionStyle}`), d.className = u.join(" "), d.append(e, a), this.el.insertAdjacentElement("afterend", d), this.el.nextElementSibling && b(this, l, this.el.nextElementSibling), o(this, S, $).call(this); +}, L = new WeakSet(), F = function(e) { e.preventDefault(); const t = e.target; if (this.state.disabled) return; - if (!l(this, i)) + if (!s(this, l)) throw Error("uxEl is null"); - const s = l(this, i).querySelector(".ux-select__body"); - if (!s) + const i = s(this, l).querySelector(".ux-select__body"); + if (!i) throw Error("uxSelectBody is null"); - if (e.target !== l(this, i).querySelector(".ux-select__clear") && !s.contains(t)) { - if (l(this, i).classList.contains("-shown")) - l(this, i).classList.remove("-shown"); - else if (l(this, i).classList.add("-shown"), this.config.isSearchable) { - const a = l(this, i).querySelector(".ux-select-search__input"); + if (e.target !== s(this, l).querySelector(".ux-select__clear") && !i.contains(t)) { + if (s(this, l).classList.contains("-shown")) + s(this, l).classList.remove("-shown"); + else if (s(this, l).classList.add("-shown"), this.config.isSearchable) { + const a = s(this, l).querySelector(".ux-select-search__input"); if (!a) throw Error("Search input is null"); a.value = "", a.dispatchEvent(new Event("input")), this.config.isSearchFocus && a.focus(); } } -}, _ = new WeakSet(), F = function(e) { +}, v = new WeakSet(), H = function(e) { const t = e.target; - if (!l(this, i)) + if (!s(this, l)) throw Error("uxEl is null"); - l(this, i).contains(t) || l(this, i).classList.remove("-shown"); -}, L = new WeakSet(), H = function(e) { + s(this, l).contains(t) || s(this, l).classList.remove("-shown"); +}, w = new WeakSet(), P = function(e) { return e.preventDefault(), this.state.disabled ? !1 : this.clear(); -}, v = new WeakSet(), P = function(e) { +}, C = new WeakSet(), j = function(e) { if (e.preventDefault(), e.target !== null) { const t = e.target; if (t.classList.contains("-disabled")) return !1; if (this.state.multiple) { e.stopPropagation(); - const s = this.el.querySelector(`[value="${t.dataset.value}"]`); - if (!s) + const i = this.el.querySelector(`[value="${t.dataset.value}"]`); + if (!i) throw Error("Option equal this value not found"); - t.classList.contains("-selected") ? s.removeAttribute("selected") : s.setAttribute("selected", ""); + t.classList.contains("-selected") ? i.removeAttribute("selected") : i.setAttribute("selected", ""); } else if (!t.classList.contains("-selected")) - this.el.querySelectorAll("option").forEach((s) => { - s.value === t.dataset.value ? s.setAttribute("selected", "") : s.removeAttribute("selected"); + this.el.querySelectorAll("option").forEach((i) => { + i.value === t.dataset.value ? i.setAttribute("selected", "") : i.removeAttribute("selected"); }); else return !1; return this.update(); } -}, w = new WeakSet(), j = function(e) { - if (e.target !== null && l(this, i)) { - const t = e.target, s = t.value.toLowerCase(), a = l(this, i).querySelectorAll(".ux-select-group"); - if (s === "") - l(this, i).querySelectorAll(".ux-select-group__elem").forEach((o) => o.style.display = ""), this.config.isGroupOptions && a.forEach((o) => o.style.display = ""); +}, T = new WeakSet(), I = function(e) { + if (e.target !== null && s(this, l)) { + const t = e.target, i = t.value.toLowerCase(), a = s(this, l).querySelectorAll(".ux-select-group"); + if (i === "") + s(this, l).querySelectorAll(".ux-select-group__elem").forEach((d) => d.style.display = ""), this.config.isGroupOptions && a.forEach((d) => d.style.display = ""); else { - const r = new RegExp(s); - new Promise((o) => { - this.options.forEach((h) => { - if (!l(this, i)) + const r = new RegExp(i); + new Promise((d) => { + this.options.forEach((u) => { + if (!s(this, l)) throw Error("uxEl is null"); - const u = r.test(h.data.text.toLowerCase()), p = l(this, i).querySelector(`[data-value="${h.data.value}"]`); - if (!p) + const p = r.test(u.data.text.toLowerCase()), f = s(this, l).querySelector(`[data-value="${u.data.value}"]`); + if (!f) throw Error("uxOption is null"); - p.style.display = u ? "" : "none"; - }), this.config.isGroupOptions ? o(!0) : o(!1); - }).then((o) => { - o && a.forEach((h) => { - const u = h.querySelector(".ux-select-group__list"); - h.style.display = "", u && (h.style.display = u.clientHeight !== 0 ? "" : "none"); + f.style.display = p ? "" : "none"; + }), this.config.isGroupOptions ? d(!0) : d(!1); + }).then((d) => { + d && a.forEach((u) => { + const p = u.querySelector(".ux-select-group__list"); + u.style.display = "", p && (u.style.display = p.clientHeight !== 0 ? "" : "none"); }); }); } - M(this.el); + R(this.el); } -}, C = new WeakSet(), I = function() { - if (l(this, i)) { - l(this, i).addEventListener("click", n(this, y, B).bind(this)); - const e = l(this, i).querySelector(".ux-select__clear"); - if (e && e.addEventListener("click", n(this, L, H).bind(this)), window.addEventListener("click", n(this, _, F).bind(this)), this.config.isSearchable) { - const t = l(this, i).querySelector(".ux-select-search__input"); - t && t.addEventListener("input", n(this, w, j).bind(this)); +}, q = new WeakSet(), N = function() { + if (s(this, l)) { + s(this, l).addEventListener("click", o(this, L, F).bind(this)); + const e = s(this, l).querySelector(".ux-select__clear"); + if (e && e.addEventListener("click", o(this, w, P).bind(this)), window.addEventListener("click", o(this, v, H).bind(this)), this.config.isSearchable) { + const t = s(this, l).querySelector(".ux-select-search__input"); + t && t.addEventListener("input", o(this, T, I).bind(this)); } } }; export { - U as default + W as default }; diff --git a/dist/ux-select.min.umd.cjs b/dist/ux-select.min.umd.cjs index 8dd75bb..899b89e 100644 --- a/dist/ux-select.min.umd.cjs +++ b/dist/ux-select.min.umd.cjs @@ -1,2 +1,2 @@ (function(){"use strict";try{if(typeof document<"u"){var e=document.createElement("style");e.appendChild(document.createTextNode(`:root{--uxs-padding-x: 12px;--uxs-padding-y: 6px;--uxs-background-color: rgb(255, 255, 255);--uxs-text-color: rgb(34, 34, 34);--uxs-font-size: 16px;--uxs-line-height: 1.5;--uxs-border-width: 1px;--uxs-border-radius: 6px;--uxs-border-color: rgb(233, 233, 233);--uxs-border-hover-color: rgb(189, 193, 198);--uxs-option-hover-bg-color: rgb(244, 244, 244);--uxs-option-select-bg-color: rgb(233, 233, 233);--uxs-option-disable-color: rgb(130, 130, 130);--uxs-option-styled-outline-width: 1px;--uxs-option-styled-checkbox-outline-radius: 4px;--uxs-option-styled-border-width: 3px;--uxs-option-styled-bg: rgb(0, 224, 255);--uxs-dropdown-image: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 24 24' style='enable-background:new 0 0 24 24;' xml:space='preserve'%3E%3Cg%3E%3Cpath d='M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4 l-6,6C12.5,15.9,12.3,16,12,16z'/%3E%3C/g%3E%3C/svg%3E%0A");--uxs-dropdown-image-disable: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 24 24' style='enable-background:new 0 0 24 24;' xml:space='preserve'%3E%3Cg%3E%3Cpath fill='rgb(130 130 130)' d='M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4 l-6,6C12.5,15.9,12.3,16,12,16z'/%3E%3C/g%3E%3C/svg%3E%0A");--uxs-clear-image: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 24 24' style='enable-background:new 0 0 24 24;' xml:space='preserve'%3E%3Cg%3E%3Cpath d='M6,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l12-12c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-12,12C6.5,18.9,6.3,19,6,19z' /%3E%3Cpath d='M18,19c-0.3,0-0.5-0.1-0.7-0.3l-12-12c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l12,12c0.4,0.4,0.4,1,0,1.4 C18.5,18.9,18.3,19,18,19z'/%3E%3C/g%3E%3C/svg%3E%0A");--uxs-clear-image-disable: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 24 24' style='enable-background:new 0 0 24 24;' xml:space='preserve'%3E%3Cg%3E%3Cpath fill='rgb(130 130 130)' d='M6,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l12-12c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-12,12C6.5,18.9,6.3,19,6,19z' /%3E%3Cpath fill='rgb(130 130 130)' d='M18,19c-0.3,0-0.5-0.1-0.7-0.3l-12-12c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l12,12c0.4,0.4,0.4,1,0,1.4 C18.5,18.9,18.3,19,18,19z'/%3E%3C/g%3E%3C/svg%3E%0A");--uxs-search-bg: rgb(255, 255, 255);--uxs-search-color: rgb(34, 34, 34);--uxs-search-placeholder: rgb(169, 169, 169);--uxs-transition-duration: .2s;--uxs-transition-easing: ease-in-out;--uxs-scrollbar-track-color: rgb(189, 189, 189);--uxs-scrollbar-thumb-color: rgb(117, 117, 117);--uxs-scrollbar-thumb-hover-color: rgb(97, 97, 97)}.ux-select{cursor:pointer;position:relative;display:block;width:100%;margin:0;padding:var(--uxs-padding-y) var(--uxs-padding-x);border:var(--uxs-border-width) solid var(--uxs-border-color);border-radius:var(--uxs-border-radius);background-color:var(--uxs-background-color);font-size:var(--uxs-font-size);line-height:var(--uxs-line-height);color:var(--uxs-text-color);transition:border-color var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select.-disabled{cursor:not-allowed;color:var(--uxs-option-disable-color)}.ux-select.-filled .ux-select__clear{display:block}.ux-select:not(.-disabled).-focus{border-color:var(--uxs-border-hover-color)}.ux-select:not(.-disabled).-shown{border-color:var(--uxs-border-hover-color)}.ux-select:not(.-disabled).-shown .ux-select__head:after{transform:rotate(-180deg)}.ux-select:not(.-disabled).-shown .ux-select__body{pointer-events:auto;opacity:1;transform:scale(1) translateY(0)}.ux-select__head{position:relative;display:flex;align-items:center}.ux-select__head:after{content:"";display:flex;align-items:center;justify-content:center;min-width:16px;width:16px;height:16px;margin-left:8px;background:transparent var(--uxs-dropdown-image) center no-repeat;transition:transform var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select.-disabled .ux-select__head:after{background-image:var(--uxs-dropdown-image-disable)}.ux-select__title{user-select:none;flex-grow:1;white-space:nowrap}.ux-select__clear{cursor:pointer;display:none;min-width:16px;width:16px;height:16px;margin-left:8px;border:none;outline:none;background:transparent var(--uxs-clear-image) center no-repeat}.ux-select.-disabled .ux-select__clear{cursor:not-allowed;background-image:var(--uxs-clear-image-disable)}.ux-select__body{pointer-events:none;position:absolute;z-index:5;top:calc(100% + 5px);left:0;overflow:hidden;width:100%;border:var(--uxs-border-width) solid var(--uxs-border-color);border-radius:var(--uxs-border-radius);background-color:var(--uxs-background-color);opacity:0;transform:scale(.75) translateY(-12px);transition:opacity var(--uxs-transition-duration) var(--uxs-transition-easing),transform var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select__search{padding:4px}.ux-select-search__input{width:100%;padding:var(--uxs-padding-y) calc(var(--uxs-padding-x) - 3px);border:var(--uxs-border-width) solid var(--uxs-border-color);border-radius:var(--uxs-border-radius);background-color:var(--uxs-search-bg);color:var(--uxs-search-color);transition:border-color var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select-search__input::placeholder{color:var(--uxs-search-placeholder)}.ux-select-search__input::-ms-clear,.ux-select-search__input::-ms-reveal{display:none;width:0;height:0}.ux-select-search__input::-webkit-search-decoration,.ux-select-search__input::-webkit-search-cancel-button,.ux-select-search__input::-webkit-search-results-button,.ux-select-search__input::-webkit-search-results-decoration{-webkit-appearance:none;appearance:none}.ux-select-search__input:focus-visible{outline:none}.ux-select-search__input:focus{border-color:var(--uxs-border-hover-color)}.ux-select__dropdown{max-height:calc((var(--uxs-font-size) * var(--uxs-line-height) + var(--uxs-padding-y) * 2) * 5);overflow-y:auto;scroll-behavior:smooth;scrollbar-width:thin;scrollbar-color:var(--uxs-scrollbar-track-color) var(--uxs-scrollbar-thumb-color)}.ux-select__dropdown::-webkit-scrollbar{position:absolute;width:7px;height:7px}.ux-select__dropdown::-webkit-scrollbar-track{background-color:var(--uxs-scrollbar-track-color)}.ux-select__dropdown::-webkit-scrollbar-thumb{background-color:var(--uxs-scrollbar-thumb-color)}.ux-select__dropdown::-webkit-scrollbar-thumb:hover{background-color:var(--uxs-scrollbar-thumb-hover-color)}.ux-select-group.-disabled .ux-select-group__title{color:var(--uxs-option-disable-color)}.ux-select-group.-empty .ux-select-group__elem{padding:var(--uxs-padding-y) var(--uxs-padding-x)}.ux-select-group__title{cursor:default;padding:var(--uxs-padding-y) var(--uxs-padding-x);font-weight:700}.ux-select-group__list{margin:0;padding:0;list-style:none}.ux-select-group__elem{user-select:none;padding:var(--uxs-padding-y) var(--uxs-padding-x) var(--uxs-padding-y) calc(var(--uxs-padding-x) * 2);background-color:var(--uxs-background-color);transition:background-color var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select.-radio .ux-select-group__elem,.ux-select.-checkbox .ux-select-group__elem{display:flex;align-items:center;justify-content:space-between}.ux-select.-radio .ux-select-group__elem:after,.ux-select.-checkbox .ux-select-group__elem:after{content:"";display:flex;align-items:center;justify-content:center;width:var(--uxs-font-size);height:var(--uxs-font-size);outline:var(--uxs-option-styled-outline-width) solid var(--uxs-option-styled-bg);border:var(--uxs-option-styled-border-width) solid var(--uxs-background-color);transition:background-color var(--uxs-transition-duration) var(--uxs-transition-easing)}.ux-select.-radio .ux-select-group__elem:after{border-radius:50%}.ux-select.-checkbox .ux-select-group__elem:after{border-radius:var(--uxs-option-styled-checkbox-outline-radius)}.ux-select-group__elem.-disabled{cursor:not-allowed;color:var(--uxs-option-disable-color)}.ux-select-group__elem:not(.-disabled):hover{background-color:var(--uxs-option-hover-bg-color)}.ux-select.-radio .ux-select-group__elem:not(.-disabled):hover:after,.ux-select.-checkbox .ux-select-group__elem:not(.-disabled):hover:after{border:var(--uxs-option-styled-border-width) solid var(--uxs-option-hover-bg-color)}.ux-select-group__elem:not(.-disabled).-selected{font-weight:700;background-color:var(--uxs-option-select-bg-color)}.ux-select:not(.-multiple) .ux-select-group__elem:not(.-disabled).-selected{cursor:default}.ux-select.-radio .ux-select-group__elem.-selected:after,.ux-select.-checkbox .ux-select-group__elem.-selected:after{border:var(--uxs-option-styled-border-width) solid var(--uxs-option-hover-bg-color);background-color:var(--uxs-option-styled-bg)}`)),document.head.appendChild(e)}}catch(r){console.error("vite-plugin-css-injected-by-js",r)}})(); -var M=Object.defineProperty;var R=(n,a,r)=>a in n?M(n,a,{enumerable:!0,configurable:!0,writable:!0,value:r}):n[a]=r;var m=(n,a,r)=>(R(n,typeof a!="symbol"?a+"":a,r),r),A=(n,a,r)=>{if(!a.has(n))throw TypeError("Cannot "+r)};var l=(n,a,r)=>(A(n,a,"read from private field"),r?r.call(n):a.get(n)),h=(n,a,r)=>{if(a.has(n))throw TypeError("Cannot add the same private member more than once");a instanceof WeakSet?a.add(n):a.set(n,r)},$=(n,a,r,S)=>(A(n,a,"write to private field"),S?S.call(n,r):a.set(n,r),r);var o=(n,a,r)=>(A(n,a,"access private method"),r);(function(n,a){typeof exports=="object"&&typeof module<"u"?module.exports=a():typeof define=="function"&&define.amd?define(a):(n=typeof globalThis<"u"?globalThis:n||self,n.UxSelect=a())})(this,function(){var s,y,B,g,k,b,z,_,j,E,D,L,F,v,H,w,P,C,I,q,N,T,U,O,V;"use strict";const n="";function a(G){const t=new Event("change");G.dispatchEvent(t)}function r(G){const t=new Event("input");G.dispatchEvent(t)}class S{constructor(t,e){h(this,y);h(this,g);h(this,b);h(this,_);h(this,E);h(this,L);h(this,v);h(this,w);h(this,C);h(this,q);h(this,T);h(this,O);m(this,"el");m(this,"config");m(this,"localization");m(this,"state");m(this,"options");m(this,"groups");h(this,s,void 0);this.el=t,this.config={isSearchable:e.isSearchable||!1,isSearchFocus:e.isSearchFocus||!1,isGroupOptions:e.isGroupOptions||!1,optionStyle:e.optionStyle||"default"},this.localization={placeholder:this.el.dataset.placeholder||e.placeholder||"Select an option",searchText:this.el.dataset.searchText||e.searchText||"Search",clearText:this.el.dataset.clearText||e.clearText||"Clear option(s)",selectedText:this.el.dataset.selectedText||e.selectedText||"Selected:"},this.state={multiple:this.el.multiple,disabled:this.el.disabled},this.options=[],this.groups=[],$(this,s,void 0),o(this,y,B).call(this)}disable(){!this.state.disabled&&l(this,s)&&(this.el.disabled=!0,l(this,s).classList.add("-disabled"),this.state.disabled=!0)}enable(){this.state.disabled&&l(this,s)&&(this.el.disabled=!1,l(this,s).classList.remove("-disabled"),this.state.disabled=!1)}update(t=!0){o(this,g,k).call(this),o(this,E,D).call(this),this.el.disabled?this.disable():this.enable(),t&&a(this.el)}clear(){this.options.forEach(t=>{if(t.attributes.selected&&l(this,s)){t.attributes.selected=!1;const e=l(this,s).querySelector(`[data-value="${t.data.value}"]`);e&&e.classList.remove("-selected"),t.element.removeAttribute("selected")}}),o(this,b,z).call(this),a(this.el)}destroy(){l(this,s)&&(l(this,s).remove(),this.el.style.display="")}}return s=new WeakMap,y=new WeakSet,B=function(){o(this,g,k).call(this),o(this,L,F).call(this),o(this,O,V).call(this)},g=new WeakSet,k=function(){const t=this.el.options;if(t.length<=0)throw Error(`Null options at ${this.el}`);this.options=[];for(const e of t){let i;this.config.isGroupOptions?i=e.dataset.uxSelectGroup?e.dataset.uxSelectGroup:"empty":i="empty",this.groups.indexOf(i)===-1&&this.groups.push(i),this.options.push({attributes:{selected:e.getAttribute("selected")!==null,disabled:e.disabled,group:i},data:{text:e.textContent||"",value:e.value},element:e})}},b=new WeakSet,z=function(t=[]){if(l(this,s)){const e=l(this,s).querySelector(".ux-select__title");if(!e)throw Error("selectTitle is null");t.length===1?(e.textContent=t[0],l(this,s).classList.add("-filled")):t.length>0&&this.state.multiple?(e.textContent=`${this.localization.selectedText} ${t.length}`,l(this,s).classList.add("-filled")):(e.textContent=this.localization.placeholder,l(this,s).classList.remove("-filled"))}},_=new WeakSet,j=function(){new Promise(t=>{const e=[];this.options.forEach(i=>{const c=document.createElement("li");if(c.classList.add("ux-select-group__elem"),c.dataset.value=i.data.value,c.textContent=i.data.text,i.attributes.selected&&(c.classList.add("-selected"),e.push(i.data.text!==null?i.data.text:"")),i.attributes.disabled&&c.classList.add("-disabled"),c.addEventListener("click",o(this,q,N).bind(this)),l(this,s)){const d=l(this,s).querySelector(`[data-ux-group="${i.attributes.group}"] .ux-select-group__list`);d&&d.appendChild(c)}}),o(this,b,z).call(this,e),this.config.isGroupOptions?t(!0):t(!1)}).then(t=>{if(t&&l(this,s)){const e=l(this,s).querySelectorAll(".ux-select-group");e.length>0&&e.forEach(i=>{const c=i.querySelector(".ux-select-group__list");if(c){const d=c.querySelectorAll(".ux-select-group__elem"),u=c.querySelectorAll(".ux-select-group__elem.-disabled");d.length===u.length?i.classList.add("-disabled"):i.classList.remove("-disabled")}})}})},E=new WeakSet,D=function(){if(l(this,s)){const t=l(this,s).querySelector(".ux-select__dropdown");if(!t)throw Error("uxSelectList is null");t.innerHTML="",this.groups.forEach(e=>{const i=document.createElement("div");if(i.classList.add("ux-select__group","ux-select-group"),e==="empty"&&i.classList.add("-empty"),i.dataset.uxGroup=e,e!=="empty"){const d=document.createElement("div");d.classList.add("ux-select-group__title"),d.textContent=e,i.appendChild(d)}const c=document.createElement("ul");c.classList.add("ux-select-group__list"),i.appendChild(c),t.appendChild(i)}),o(this,_,j).call(this)}},L=new WeakSet,F=function(){this.el.style.display="none";const t=document.createElement("div");t.classList.add("ux-select__head");const e=document.createElement("div");e.classList.add("ux-select__title"),e.textContent=this.localization.placeholder;const i=document.createElement("button");i.type="button",i.classList.add("ux-select__clear"),i.title=this.localization.clearText,t.append(e,i);const c=document.createElement("div");if(c.classList.add("ux-select__body"),this.config.isSearchable){const f=document.createElement("div");f.classList.add("ux-select__search");const x=document.createElement("input");x.type="search",x.classList.add("ux-select-search__input"),x.placeholder=this.localization.searchText,f.appendChild(x),c.appendChild(f)}const d=document.createElement("div");d.classList.add("ux-select__dropdown"),c.appendChild(d);const u=document.createElement("div"),p=["ux-select",this.el.classList];this.state.multiple&&p.push("-multiple"),this.state.disabled&&p.push("-disabled"),this.config.optionStyle!=="default"&&p.push(`-${this.config.optionStyle}`),u.className=p.join(" "),u.append(t,c),this.el.insertAdjacentElement("afterend",u),this.el.nextElementSibling&&$(this,s,this.el.nextElementSibling),o(this,E,D).call(this)},v=new WeakSet,H=function(t){t.preventDefault();const e=t.target;if(this.state.disabled)return;if(!l(this,s))throw Error("uxEl is null");const i=l(this,s).querySelector(".ux-select__body");if(!i)throw Error("uxSelectBody is null");if(t.target!==l(this,s).querySelector(".ux-select__clear")&&!i.contains(e)){if(l(this,s).classList.contains("-shown"))l(this,s).classList.remove("-shown");else if(l(this,s).classList.add("-shown"),this.config.isSearchable){const c=l(this,s).querySelector(".ux-select-search__input");if(!c)throw Error("Search input is null");c.value="",c.dispatchEvent(new Event("input")),this.config.isSearchFocus&&c.focus()}}},w=new WeakSet,P=function(t){const e=t.target;if(!l(this,s))throw Error("uxEl is null");l(this,s).contains(e)||l(this,s).classList.remove("-shown")},C=new WeakSet,I=function(t){return t.preventDefault(),this.state.disabled?!1:this.clear()},q=new WeakSet,N=function(t){if(t.preventDefault(),t.target!==null){const e=t.target;if(e.classList.contains("-disabled"))return!1;if(this.state.multiple){t.stopPropagation();const i=this.el.querySelector(`[value="${e.dataset.value}"]`);if(!i)throw Error("Option equal this value not found");e.classList.contains("-selected")?i.removeAttribute("selected"):i.setAttribute("selected","")}else if(!e.classList.contains("-selected"))this.el.querySelectorAll("option").forEach(i=>{i.value===e.dataset.value?i.setAttribute("selected",""):i.removeAttribute("selected")});else return!1;return this.update()}},T=new WeakSet,U=function(t){if(t.target!==null&&l(this,s)){const e=t.target,i=e.value.toLowerCase(),c=l(this,s).querySelectorAll(".ux-select-group");if(i==="")l(this,s).querySelectorAll(".ux-select-group__elem").forEach(u=>u.style.display=""),this.config.isGroupOptions&&c.forEach(u=>u.style.display="");else{const d=new RegExp(i);new Promise(u=>{this.options.forEach(p=>{if(!l(this,s))throw Error("uxEl is null");const f=d.test(p.data.text.toLowerCase()),x=l(this,s).querySelector(`[data-value="${p.data.value}"]`);if(!x)throw Error("uxOption is null");x.style.display=f?"":"none"}),this.config.isGroupOptions?u(!0):u(!1)}).then(u=>{u&&c.forEach(p=>{const f=p.querySelector(".ux-select-group__list");p.style.display="",f&&(p.style.display=f.clientHeight!==0?"":"none")})})}r(this.el)}},O=new WeakSet,V=function(){if(l(this,s)){l(this,s).addEventListener("click",o(this,v,H).bind(this));const t=l(this,s).querySelector(".ux-select__clear");if(t&&t.addEventListener("click",o(this,C,I).bind(this)),window.addEventListener("click",o(this,w,P).bind(this)),this.config.isSearchable){const e=l(this,s).querySelector(".ux-select-search__input");e&&e.addEventListener("input",o(this,T,U).bind(this))}}},S}); +var R=Object.defineProperty;var W=(n,a,o)=>a in n?R(n,a,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[a]=o;var g=(n,a,o)=>(W(n,typeof a!="symbol"?a+"":a,o),o),k=(n,a,o)=>{if(!a.has(n))throw TypeError("Cannot "+o)};var s=(n,a,o)=>(k(n,a,"read from private field"),o?o.call(n):a.get(n)),d=(n,a,o)=>{if(a.has(n))throw TypeError("Cannot add the same private member more than once");a instanceof WeakSet?a.add(n):a.set(n,o)},$=(n,a,o,y)=>(k(n,a,"write to private field"),y?y.call(n,o):a.set(n,o),o);var r=(n,a,o)=>(k(n,a,"access private method"),o);(function(n,a){typeof exports=="object"&&typeof module<"u"?module.exports=a():typeof define=="function"&&define.amd?define(a):(n=typeof globalThis<"u"?globalThis:n||self,n.UxSelect=a())})(this,function(){var p,i,_,F,S,z,b,D,L,j,E,B,v,H,w,P,C,I,T,N,q,U,O,V,G,M;"use strict";const n="";function a(A){const e=new Event("change");A.dispatchEvent(e)}function o(A){const e=new Event("input");A.dispatchEvent(e)}class y{constructor(e,t){d(this,_);d(this,S);d(this,b);d(this,L);d(this,E);d(this,v);d(this,w);d(this,C);d(this,T);d(this,q);d(this,O);d(this,G);g(this,"el");d(this,p,void 0);g(this,"config");g(this,"localization");g(this,"state");g(this,"options");g(this,"groups");d(this,i,void 0);this.el=e,$(this,p,t||{isSearchable:!1,isSearchFocus:!1,isGroupOptions:!1,optionStyle:"default",placeholder:"Select an option",searchText:"Search",clearText:"Clear option(s)",selectedText:"Selected:"}),this.config={isSearchable:s(this,p).isSearchable||!1,isSearchFocus:s(this,p).isSearchFocus||!1,isGroupOptions:s(this,p).isGroupOptions||!1,optionStyle:s(this,p).optionStyle||"default"},this.localization={placeholder:s(this,p).placeholder||this.el.dataset.placeholder||"Select an option",searchText:s(this,p).searchText||this.el.dataset.searchText||"Search",clearText:s(this,p).clearText||this.el.dataset.clearText||"Clear option(s)",selectedText:s(this,p).selectedText||this.el.dataset.selectedText||"Selected:"},this.state={multiple:this.el.multiple,disabled:this.el.disabled},this.options=[],this.groups=[],$(this,i,void 0),r(this,_,F).call(this)}disable(){!this.state.disabled&&s(this,i)&&(this.el.disabled=!0,s(this,i).classList.add("-disabled"),this.state.disabled=!0)}enable(){this.state.disabled&&s(this,i)&&(this.el.disabled=!1,s(this,i).classList.remove("-disabled"),this.state.disabled=!1)}update(e=!0){r(this,S,z).call(this),r(this,E,B).call(this),this.el.disabled?this.disable():this.enable(),e&&a(this.el)}clear(){this.options.forEach(e=>{if(e.attributes.selected&&s(this,i)){e.attributes.selected=!1;const t=s(this,i).querySelector(`[data-value="${e.data.value}"]`);t&&t.classList.remove("-selected"),e.element.removeAttribute("selected")}}),r(this,b,D).call(this),a(this.el)}destroy(){s(this,i)&&(s(this,i).remove(),this.el.style.display="")}}return p=new WeakMap,i=new WeakMap,_=new WeakSet,F=function(){r(this,S,z).call(this),r(this,v,H).call(this),r(this,G,M).call(this)},S=new WeakSet,z=function(){const e=this.el.options;if(e.length<=0)throw Error(`Null options at ${this.el}`);this.options=[];for(const t of e){let l;this.config.isGroupOptions?l=t.dataset.uxSelectGroup?t.dataset.uxSelectGroup:"empty":l="empty",this.groups.indexOf(l)===-1&&this.groups.push(l),this.options.push({attributes:{selected:t.getAttribute("selected")!==null,disabled:t.disabled,group:l},data:{text:t.textContent||"",value:t.value},element:t})}},b=new WeakSet,D=function(e=[]){if(s(this,i)){const t=s(this,i).querySelector(".ux-select__title");if(!t)throw Error("selectTitle is null");e.length===1?(t.textContent=e[0],s(this,i).classList.add("-filled")):e.length>0&&this.state.multiple?(t.textContent=`${this.localization.selectedText} ${e.length}`,s(this,i).classList.add("-filled")):(t.textContent=this.localization.placeholder,s(this,i).classList.remove("-filled"))}},L=new WeakSet,j=function(){new Promise(e=>{const t=[];this.options.forEach(l=>{const c=document.createElement("li");if(c.classList.add("ux-select-group__elem"),c.dataset.value=l.data.value,c.textContent=l.data.text,l.attributes.selected&&(c.classList.add("-selected"),t.push(l.data.text!==null?l.data.text:"")),l.attributes.disabled&&c.classList.add("-disabled"),c.addEventListener("click",r(this,q,U).bind(this)),s(this,i)){const h=s(this,i).querySelector(`[data-ux-group="${l.attributes.group}"] .ux-select-group__list`);h&&h.appendChild(c)}}),r(this,b,D).call(this,t),this.config.isGroupOptions?e(!0):e(!1)}).then(e=>{if(e&&s(this,i)){const t=s(this,i).querySelectorAll(".ux-select-group");t.length>0&&t.forEach(l=>{const c=l.querySelector(".ux-select-group__list");if(c){const h=c.querySelectorAll(".ux-select-group__elem"),u=c.querySelectorAll(".ux-select-group__elem.-disabled");h.length===u.length?l.classList.add("-disabled"):l.classList.remove("-disabled")}})}})},E=new WeakSet,B=function(){if(s(this,i)){const e=s(this,i).querySelector(".ux-select__dropdown");if(!e)throw Error("uxSelectList is null");e.innerHTML="",this.groups.forEach(t=>{const l=document.createElement("div");if(l.classList.add("ux-select__group","ux-select-group"),t==="empty"&&l.classList.add("-empty"),l.dataset.uxGroup=t,t!=="empty"){const h=document.createElement("div");h.classList.add("ux-select-group__title"),h.textContent=t,l.appendChild(h)}const c=document.createElement("ul");c.classList.add("ux-select-group__list"),l.appendChild(c),e.appendChild(l)}),r(this,L,j).call(this)}},v=new WeakSet,H=function(){this.el.style.display="none";const e=document.createElement("div");e.classList.add("ux-select__head");const t=document.createElement("div");t.classList.add("ux-select__title"),t.textContent=this.localization.placeholder;const l=document.createElement("button");l.type="button",l.classList.add("ux-select__clear"),l.title=this.localization.clearText,e.append(t,l);const c=document.createElement("div");if(c.classList.add("ux-select__body"),this.config.isSearchable){const x=document.createElement("div");x.classList.add("ux-select__search");const m=document.createElement("input");m.type="search",m.classList.add("ux-select-search__input"),m.placeholder=this.localization.searchText,x.appendChild(m),c.appendChild(x)}const h=document.createElement("div");h.classList.add("ux-select__dropdown"),c.appendChild(h);const u=document.createElement("div"),f=["ux-select",this.el.classList];this.state.multiple&&f.push("-multiple"),this.state.disabled&&f.push("-disabled"),this.config.optionStyle!=="default"&&f.push(`-${this.config.optionStyle}`),u.className=f.join(" "),u.append(e,c),this.el.insertAdjacentElement("afterend",u),this.el.nextElementSibling&&$(this,i,this.el.nextElementSibling),r(this,E,B).call(this)},w=new WeakSet,P=function(e){e.preventDefault();const t=e.target;if(this.state.disabled)return;if(!s(this,i))throw Error("uxEl is null");const l=s(this,i).querySelector(".ux-select__body");if(!l)throw Error("uxSelectBody is null");if(e.target!==s(this,i).querySelector(".ux-select__clear")&&!l.contains(t)){if(s(this,i).classList.contains("-shown"))s(this,i).classList.remove("-shown");else if(s(this,i).classList.add("-shown"),this.config.isSearchable){const c=s(this,i).querySelector(".ux-select-search__input");if(!c)throw Error("Search input is null");c.value="",c.dispatchEvent(new Event("input")),this.config.isSearchFocus&&c.focus()}}},C=new WeakSet,I=function(e){const t=e.target;if(!s(this,i))throw Error("uxEl is null");s(this,i).contains(t)||s(this,i).classList.remove("-shown")},T=new WeakSet,N=function(e){return e.preventDefault(),this.state.disabled?!1:this.clear()},q=new WeakSet,U=function(e){if(e.preventDefault(),e.target!==null){const t=e.target;if(t.classList.contains("-disabled"))return!1;if(this.state.multiple){e.stopPropagation();const l=this.el.querySelector(`[value="${t.dataset.value}"]`);if(!l)throw Error("Option equal this value not found");t.classList.contains("-selected")?l.removeAttribute("selected"):l.setAttribute("selected","")}else if(!t.classList.contains("-selected"))this.el.querySelectorAll("option").forEach(l=>{l.value===t.dataset.value?l.setAttribute("selected",""):l.removeAttribute("selected")});else return!1;return this.update()}},O=new WeakSet,V=function(e){if(e.target!==null&&s(this,i)){const t=e.target,l=t.value.toLowerCase(),c=s(this,i).querySelectorAll(".ux-select-group");if(l==="")s(this,i).querySelectorAll(".ux-select-group__elem").forEach(u=>u.style.display=""),this.config.isGroupOptions&&c.forEach(u=>u.style.display="");else{const h=new RegExp(l);new Promise(u=>{this.options.forEach(f=>{if(!s(this,i))throw Error("uxEl is null");const x=h.test(f.data.text.toLowerCase()),m=s(this,i).querySelector(`[data-value="${f.data.value}"]`);if(!m)throw Error("uxOption is null");m.style.display=x?"":"none"}),this.config.isGroupOptions?u(!0):u(!1)}).then(u=>{u&&c.forEach(f=>{const x=f.querySelector(".ux-select-group__list");f.style.display="",x&&(f.style.display=x.clientHeight!==0?"":"none")})})}o(this.el)}},G=new WeakSet,M=function(){if(s(this,i)){s(this,i).addEventListener("click",r(this,w,P).bind(this));const e=s(this,i).querySelector(".ux-select__clear");if(e&&e.addEventListener("click",r(this,T,N).bind(this)),window.addEventListener("click",r(this,C,I).bind(this)),this.config.isSearchable){const t=s(this,i).querySelector(".ux-select-search__input");t&&t.addEventListener("input",r(this,O,V).bind(this))}}},y}); diff --git a/package-lock.json b/package-lock.json index cf743e9..68fd19f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ux-select", - "version": "1.6.3", + "version": "1.6.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "ux-select", - "version": "1.6.3", + "version": "1.6.4", "license": "MIT", "devDependencies": { "@types/node": "^20.4.4", diff --git a/package.json b/package.json index 2ca5004..a443ef0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ux-select", - "version": "1.6.3", + "version": "1.6.4", "description": "UX Select is a lightweight Native JavaScript plugin that replace native select elements with customization", "keywords": [ "web", diff --git a/src/ux-select.ts b/src/ux-select.ts index bb956d5..19a9865 100644 --- a/src/ux-select.ts +++ b/src/ux-select.ts @@ -16,11 +16,12 @@ import triggerInput from "./utils/events/triggerInput.ts"; */ export default class UxSelect { el: HTMLSelectElement; + #options: UxSelectConstructorOptions; config: { - optionStyle: "checkbox" | "radio" | "default"; - isGroupOptions: boolean; isSearchable: boolean; isSearchFocus: boolean; + isGroupOptions: boolean; + optionStyle: "checkbox" | "radio" | "default"; }; localization: { placeholder: string; @@ -47,18 +48,30 @@ export default class UxSelect { * */ constructor(element: HTMLSelectElement, options: UxSelectConstructorOptions) { this.el = element; + + this.#options = options || { + isSearchable: false, + isSearchFocus: false, + isGroupOptions: false, + optionStyle: "default", + placeholder: "Select an option", + searchText: "Search", + clearText: "Clear option(s)", + selectedText: "Selected:", + }; + this.config = { - isSearchable: options.isSearchable || false, - isSearchFocus: options.isSearchFocus || false, - isGroupOptions: options.isGroupOptions || false, - optionStyle: options.optionStyle || "default", + isSearchable: this.#options.isSearchable || false, + isSearchFocus: this.#options.isSearchFocus || false, + isGroupOptions: this.#options.isGroupOptions || false, + optionStyle: this.#options.optionStyle || "default", }; this.localization = { - placeholder: this.el.dataset.placeholder || options.placeholder || "Select an option", - searchText: this.el.dataset.searchText || options.searchText || "Search", - clearText: this.el.dataset.clearText || options.clearText || "Clear option(s)", - selectedText: this.el.dataset.selectedText || options.selectedText || "Selected:", + placeholder: this.#options.placeholder || this.el.dataset.placeholder || "Select an option", + searchText: this.#options.searchText || this.el.dataset.searchText || "Search", + clearText: this.#options.clearText || this.el.dataset.clearText || "Clear option(s)", + selectedText: this.#options.selectedText || this.el.dataset.selectedText || "Selected:", }; this.state = {