slim-select 3.1.4 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +93 -77
- package/dist/react/index.umd.js +2 -2
- package/dist/slimselect.cjs.js +1 -1
- package/dist/slimselect.cjs.js.map +1 -1
- package/dist/slimselect.es.js +41 -25
- package/dist/slimselect.es.js.map +1 -1
- package/dist/slimselect.iife.js +1 -1
- package/dist/slimselect.iife.js.map +1 -1
- package/dist/slimselect.js +1 -1
- package/dist/slimselect.js.map +1 -1
- package/dist/vue/index.d.ts +2 -0
- package/dist/vue/index.js +159 -143
- package/dist/vue/index.umd.js +1 -1
- package/package.json +10 -10
- package/src/slim-select/index.test.ts +86 -0
- package/src/slim-select/index.ts +2 -2
- package/src/slim-select/render.ts +20 -2
- package/src/slim-select/select.ts +14 -11
- package/src/slim-select/settings.test.ts +1 -0
- package/src/slim-select/settings.ts +2 -0
package/dist/vue/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(v,O){typeof exports=="object"&&typeof module<"u"?O(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],O):(v=typeof globalThis<"u"?globalThis:v||self,O(v.SlimSelectVue={},v.Vue))})(this,function(v,O){"use strict";class T{main;placeholder;values;single;max;value;valueText;valueDelete;valueOut;deselect;deselectPath;arrow;arrowClose;arrowOpen;content;contentOpen;dirAbove;dirBelow;search;searchHighlighter;searching;addable;addablePath;list;optgroup;optgroupLabel;optgroupLabelText;optgroupActions;optgroupSelectAll;optgroupSelectAllBox;optgroupSelectAllCheck;optgroupClosable;option;optionDelete;highlighted;mainOpen;close;selected;error;disabled;hide;constructor(e){e||(e={}),this.main=e.main||"ss-main",this.placeholder=e.placeholder||"ss-placeholder",this.values=e.values||"ss-values",this.single=e.single||"ss-single",this.max=e.max||"ss-max",this.value=e.value||"ss-value",this.valueText=e.valueText||"ss-value-text",this.valueDelete=e.valueDelete||"ss-value-delete",this.valueOut=e.valueOut||"ss-value-out",this.deselect=e.deselect||"ss-deselect",this.deselectPath=e.deselectPath||"M10,10 L90,90 M10,90 L90,10",this.arrow=e.arrow||"ss-arrow",this.arrowClose=e.arrowClose||"M10,30 L50,70 L90,30",this.arrowOpen=e.arrowOpen||"M10,70 L50,30 L90,70",this.content=e.content||"ss-content",this.contentOpen=e.contentOpen||"ss-open",this.dirAbove=e.dirAbove||"ss-dir-above",this.dirBelow=e.dirBelow||"ss-dir-below",this.search=e.search||"ss-search",this.searchHighlighter=e.searchHighlighter||"ss-search-highlight",this.searching=e.searching||"ss-searching",this.addable=e.addable||"ss-addable",this.addablePath=e.addablePath||"M50,10 L50,90 M10,50 L90,50",this.list=e.list||"ss-list",this.optgroup=e.optgroup||"ss-optgroup",this.optgroupLabel=e.optgroupLabel||"ss-optgroup-label",this.optgroupLabelText=e.optgroupLabelText||"ss-optgroup-label-text",this.optgroupActions=e.optgroupActions||"ss-optgroup-actions",this.optgroupSelectAll=e.optgroupSelectAll||"ss-selectall",this.optgroupSelectAllBox=e.optgroupSelectAllBox||"M60,10 L10,10 L10,90 L90,90 L90,50",this.optgroupSelectAllCheck=e.optgroupSelectAllCheck||"M30,45 L50,70 L90,10",this.optgroupClosable=e.optgroupClosable||"ss-closable",this.option=e.option||"ss-option",this.optionDelete=e.optionDelete||"M10,10 L90,90 M10,90 L90,10",this.highlighted=e.highlighted||"ss-highlighted",this.mainOpen=e.mainOpen||"ss-open",this.close=e.close||"ss-close",this.selected=e.selected||"ss-selected",this.error=e.error||"ss-error",this.disabled=e.disabled||"ss-disabled",this.hide=e.hide||"ss-hide"}}function L(){return Math.random().toString(36).substring(2,10)}function D(h,e){function t(i,l){return l&&i&&i.classList&&i.classList.contains(l)||l&&i&&i.dataset&&i.dataset.id&&i.dataset.id===e?i:null}function s(i,l){return!i||i===document?null:t(i,l)?i:s(i.parentNode,l)}return t(h,e)||s(h,e)}function C(h,e=50,t=!1){let s;return function(...i){const l=self,a=()=>{s=null,t||h.apply(l,i)},n=t&&!s;clearTimeout(s),s=setTimeout(a,e),n&&h.apply(l,i)}}function A(h,e){return JSON.stringify(h)===JSON.stringify(e)}function k(h){const e=h.replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,t=>"-"+t.toLowerCase());return h[0]===h[0].toUpperCase()?e.substring(1):e}class p{id;value;text;html;defaultSelected;selected;display;disabled;placeholder;class;style;data;mandatory;constructor(e){this.id=!e.id||e.id===""?L():e.id,this.value=e.value===void 0?e.text||"":e.value||"",this.text=e.text||"",this.html=e.html||"",this.defaultSelected=e.defaultSelected!==void 0?e.defaultSelected:!1,this.selected=e.selected!==void 0?e.selected:!1,this.display=e.display!==void 0?e.display:!0,this.disabled=e.disabled!==void 0?e.disabled:!1,this.mandatory=e.mandatory!==void 0?e.mandatory:!1,this.placeholder=e.placeholder!==void 0?e.placeholder:!1,this.class=e.class||"",this.style=e.style||"",this.data=e.data||{}}}class g{id;label;selectAll;selectAllText;closable;options;constructor(e){if(this.id=!e.id||e.id===""?L():e.id,this.label=e.label||"",this.selectAll=e.selectAll===void 0?!1:e.selectAll,this.selectAllText=e.selectAllText||"Select All",this.closable=e.closable||"off",this.options=[],e.options)for(const t of e.options)this.options.push(new p(t))}}class N{selectType="single";data=[];selectedOrder=[];constructor(e,t){this.selectType=e,this.setData(t)}validateDataArray(e){if(!Array.isArray(e))return new Error("Data must be an array");for(let t of e)if(t)if(t instanceof g||"label"in t){if(!("label"in t))return new Error("Optgroup must have a label");if("options"in t&&t.options)for(let s of t.options){const i=this.validateOption(s);if(i)return i}}else if(t instanceof p||"text"in t){const s=this.validateOption(t);if(s)return s}else return new Error("Data object must be a valid optgroup or option");return null}validateOption(e){return"text"in e?null:new Error("Option must have a text")}partialToFullData(e){let t=[];return e.forEach(s=>{if(s){if(s instanceof g||"label"in s){let i=[];"options"in s&&s.options&&s.options.forEach(l=>{i.push(new p(l))}),i.length>0&&t.push(new g(s))}(s instanceof p||"text"in s)&&t.push(new p(s))}}),t}setData(e,t=!1){const s=this.partialToFullData(e);if(t){const i=this.getSelectedOptions(),l=[];i.forEach(a=>{let n=!1;for(const o of s){if(o instanceof p&&o.id===a.id){n=!0;break}if(o instanceof g){for(const c of o.options)if(c.id===a.id){n=!0;break}}}n||l.push(a)}),this.data=[...l,...s]}else this.data=s;this.selectType==="single"&&this.setSelectedBy("id",this.getSelected())}getData(){return this.filter(null,!0)}getDataOptions(){return this.filter(null,!1)}addOption(e,t=!1){if(t){let s=[new p(e)];this.setData(s.concat(this.getData()))}else this.setData(this.getData().concat(new p(e)))}setSelectedBy(e,t){let s=null,i=!1;const l=[];for(let n of this.data){if(n instanceof g)for(let o of n.options){s||(s=o);let c=o[e]||"";o.selected=i?!1:t.includes(c),o.selected&&(l.push(o),this.selectType==="single"&&(i=!0))}n instanceof p&&(s||(s=n),n.selected=i?!1:t.includes(n[e]),n.selected&&(l.push(n),this.selectType==="single"&&(i=!0)))}this.selectType==="single"&&s&&!i&&(s.selected=!0,l.push(s));const a=t.map(n=>l.find(o=>o[e]===n)?.id||"");this.selectedOrder=a}getSelected(){return this.getSelectedOptions().map(e=>e.id)}getSelectedValues(){return this.getSelectedOptions().map(e=>e.value)}getSelectedOptions(){return this.filter(e=>e.selected,!1)}getOptgroupByID(e){for(let t of this.data)if(t instanceof g&&t.id===e)return t;return null}getOptionByID(e){let t=this.filter(s=>s.id===e,!1);return t.length?t[0]:null}getSelectType(){return this.selectType}getFirstOption(){let e=null;for(let t of this.data)if(t instanceof g?e=t.options[0]:t instanceof p&&(e=t),e)break;return e}search(e,t){return e=e.trim(),e===""?this.getData():this.filter(s=>t(s,e),!0)}filter(e,t){const s=[];return this.data.forEach(i=>{if(i instanceof g){let l=[];if(i.options.forEach(n=>{(!e||e(n))&&(t?l.push(new p(n)):s.push(new p(n)))}),l.length>0){let n=new g(i);n.options=l,s.push(n)}}i instanceof p&&(!e||e(i))&&s.push(new p(i))}),s}selectedOrderOptions(e){const t=[];return this.selectedOrder.forEach(s=>{const i=e.find(l=>l.id===s);i&&t.push(i)}),e.forEach(s=>{let i=!1;t.forEach(l=>{if(s.id===l.id){i=!0;return}}),i||t.push(s)}),t}}class M{settings;store;callbacks;lastSelectedOption;main;content;classes;constructor(e,t,s,i){this.store=s,this.settings=e,this.classes=t,this.callbacks=i,this.lastSelectedOption=null,this.main=this.mainDiv(),this.content=this.contentDiv(),this.updateClassStyles(),this.updateAriaAttributes(),this.settings.contentPosition!=="relative"&&(this.content.main.style.top="-9999px",this.content.main.style.left="-9999px",this.content.main.style.margin="0",this.content.main.style.width="auto"),this.settings.contentLocation&&this.settings.contentLocation.appendChild(this.content.main)}enable(){this.main.main.classList.remove(this.classes.disabled),this.main.main.setAttribute("aria-disabled","false"),this.content.search.input.disabled=!1}disable(){this.main.main.classList.add(this.classes.disabled),this.main.main.setAttribute("aria-disabled","true"),this.content.search.input.disabled=!0}open(){this.main.arrow.path.setAttribute("d",this.classes.arrowOpen),this.main.main.setAttribute("aria-expanded","true");const t=this.settings.openPosition==="up"?this.classes.dirAbove:this.classes.dirBelow;this.main.main.classList.add(t),this.content.main.classList.add(t),this.content.main.classList.add(this.classes.contentOpen),this.content.search.input.removeAttribute("aria-hidden"),this.moveContent();const s=this.store.getSelectedOptions();if(s.length){const i=s[s.length-1].id,l=this.content.list.querySelector('[data-id="'+i+'"]');l&&this.ensureElementInView(this.content.list,l)}}close(){this.main.main.setAttribute("aria-expanded","false"),this.main.arrow.path.setAttribute("d",this.classes.arrowClose),this.content.main.classList.remove(this.classes.contentOpen),this.content.search.input.setAttribute("aria-hidden","true"),this.main.main.removeAttribute("aria-activedescendant"),setTimeout(()=>{this.main.main.classList.remove(this.classes.dirAbove,this.classes.dirBelow),this.content.main.classList.remove(this.classes.dirAbove,this.classes.dirBelow)},100)}updateClassStyles(){if(this.main.main.className="",this.main.main.removeAttribute("style"),this.content.main.className="",this.content.main.removeAttribute("style"),this.main.main.classList.add(this.classes.main),this.content.main.classList.add(this.classes.content),this.settings.style!==""&&(this.main.main.style.cssText=this.settings.style,this.content.main.style.cssText=this.settings.style),this.settings.class.length)for(const e of this.settings.class)e.trim()!==""&&(this.main.main.classList.add(e.trim()),this.content.main.classList.add(e.trim()));(this.settings.contentPosition==="relative"||this.settings.contentPosition==="fixed")&&this.content.main.classList.add("ss-"+this.settings.contentPosition)}updateAriaAttributes(){const e=this.content.list.id;this.main.main.role="combobox",this.main.main.setAttribute("aria-haspopup","listbox"),this.main.main.setAttribute("aria-controls",e),this.main.main.setAttribute("aria-expanded","false"),this.content.list.setAttribute("role","listbox"),this.content.list.setAttribute("aria-label",this.settings.ariaLabel+" listbox"),this.settings.isMultiple&&this.content.list.setAttribute("aria-multiselectable","true"),this.content.search.input.setAttribute("aria-controls",e)}mainDiv(){const e=document.createElement("div");e.dataset.id=this.settings.id,e.setAttribute("aria-label",this.settings.ariaLabel),e.tabIndex=0,e.onkeydown=c=>{switch(c.key){case"ArrowUp":case"ArrowDown":return this.callbacks.open(),c.key==="ArrowDown"?this.highlight("down"):this.highlight("up"),!1;case"Tab":return this.callbacks.close(),!0;case"Enter":case" ":this.callbacks.open();const r=this.content.list.querySelector("."+this.classes.highlighted);return r&&r.click(),!1;case"Escape":return this.callbacks.close(),!1}return c.key.length===1&&this.callbacks.open(),!0},e.onclick=c=>{this.settings.disabled||(this.settings.isOpen?this.callbacks.close():this.callbacks.open())};const t=document.createElement("div");t.classList.add(this.classes.values),e.appendChild(t);const s=document.createElement("div");s.classList.add(this.classes.deselect);const i=this.store?.getSelectedOptions();!this.settings.allowDeselect||this.settings.isMultiple&&i&&i.length<=0?s.classList.add(this.classes.hide):s.classList.remove(this.classes.hide),s.onclick=c=>{if(c.stopPropagation(),this.settings.disabled)return;let r=!0;const d=this.store.getSelectedOptions(),f=[];if(this.callbacks.beforeChange&&(r=this.callbacks.beforeChange(f,d)===!0),r){if(this.settings.isMultiple)this.callbacks.setSelected([],!1),this.updateDeselectAll();else{const u=this.store.getFirstOption(),m=u?u.id:"";this.callbacks.setSelected(m,!1)}this.settings.closeOnSelect&&this.callbacks.close(),this.callbacks.afterChange&&this.callbacks.afterChange(this.store.getSelectedOptions())}};const l=document.createElementNS("http://www.w3.org/2000/svg","svg");l.setAttribute("viewBox","0 0 100 100");const a=document.createElementNS("http://www.w3.org/2000/svg","path");a.setAttribute("d",this.classes.deselectPath),l.appendChild(a),s.appendChild(l),e.appendChild(s);const n=document.createElementNS("http://www.w3.org/2000/svg","svg");n.classList.add(this.classes.arrow),n.setAttribute("viewBox","0 0 100 100");const o=document.createElementNS("http://www.w3.org/2000/svg","path");return o.setAttribute("d",this.classes.arrowClose),this.settings.alwaysOpen&&n.classList.add(this.classes.hide),n.appendChild(o),e.appendChild(n),{main:e,values:t,deselect:{main:s,svg:l,path:a},arrow:{main:n,path:o}}}mainFocus(e){e!=="click"&&this.main.main.focus({preventScroll:!0})}placeholder(){const e=this.store.filter(i=>i.placeholder,!1);let t=this.settings.placeholderText;e.length&&(e[0].html!==""?t=e[0].html:e[0].text!==""&&(t=e[0].text));const s=document.createElement("div");return s.classList.add(this.classes.placeholder),s.innerHTML=t,s}renderValues(){if(!this.settings.isMultiple){this.renderSingleValue();return}this.renderMultipleValues(),this.updateDeselectAll()}renderSingleValue(){const e=this.store.filter(s=>s.selected&&!s.placeholder,!1),t=e.length>0?e[0]:null;if(!t)this.main.values.innerHTML=this.placeholder().outerHTML;else{const s=document.createElement("div");s.classList.add(this.classes.single),t.html?s.innerHTML=t.html:s.innerText=t.text,this.main.values.innerHTML=s.outerHTML}!this.settings.allowDeselect||!e.length?this.main.deselect.main.classList.add(this.classes.hide):this.main.deselect.main.classList.remove(this.classes.hide)}renderMultipleValues(){let e=this.main.values.childNodes,t=this.store.filter(i=>i.selected&&i.display,!1);if(t.length===0){this.main.values.innerHTML=this.placeholder().outerHTML;return}else{const i=this.main.values.querySelector("."+this.classes.placeholder);i&&i.remove()}if(t.length>this.settings.maxValuesShown){const i=document.createElement("div");i.classList.add(this.classes.max),i.textContent=this.settings.maxValuesMessage.replace("{number}",t.length.toString()),this.main.values.innerHTML=i.outerHTML;return}else{const i=this.main.values.querySelector("."+this.classes.max);i&&i.remove()}this.settings.keepOrder&&(t=this.store.selectedOrderOptions(t));let s=[];for(let i=0;i<e.length;i++){const l=e[i],a=l.getAttribute("data-id");a&&(t.filter(o=>o.id===a,!1).length||s.push(l))}for(const i of s)i.classList.add(this.classes.valueOut),setTimeout(()=>{this.main.values.hasChildNodes()&&this.main.values.contains(i)&&this.main.values.removeChild(i)},100);e=this.main.values.childNodes;for(let i=0;i<t.length;i++){let l=!0;for(let a=0;a<e.length;a++)t[i].id===String(e[a].dataset.id)&&(l=!1);l&&(this.settings.keepOrder?this.main.values.appendChild(this.multipleValue(t[i])):e.length===0?this.main.values.appendChild(this.multipleValue(t[i])):i===0?this.main.values.insertBefore(this.multipleValue(t[i]),e[i]):e[i-1].insertAdjacentElement("afterend",this.multipleValue(t[i])))}}multipleValue(e){const t=document.createElement("div");t.classList.add(this.classes.value),t.dataset.id=e.id;const s=document.createElement("div");if(s.classList.add(this.classes.valueText),s.textContent=e.text,t.appendChild(s),!e.mandatory){const i=document.createElement("div");i.classList.add(this.classes.valueDelete),i.setAttribute("tabindex","0"),i.onclick=n=>{if(n.preventDefault(),n.stopPropagation(),this.settings.disabled)return;let o=!0;const c=this.store.getSelectedOptions(),r=c.filter(d=>d.selected&&d.id!==e.id,!0);if(!(this.settings.minSelected&&r.length<this.settings.minSelected)&&(this.callbacks.beforeChange&&(o=this.callbacks.beforeChange(r,c)===!0),o)){let d=[];for(const f of r){if(f instanceof g)for(const u of f.options)u.id&&d.push(u.id);f instanceof p&&d.push(f.id)}this.callbacks.setSelected(d,!1),this.settings.closeOnSelect&&this.callbacks.close(),this.callbacks.afterChange&&this.callbacks.afterChange(r),this.updateDeselectAll()}};const l=document.createElementNS("http://www.w3.org/2000/svg","svg");l.setAttribute("viewBox","0 0 100 100");const a=document.createElementNS("http://www.w3.org/2000/svg","path");a.setAttribute("d",this.classes.optionDelete),l.appendChild(a),i.appendChild(l),t.appendChild(i),i.onkeydown=n=>{n.key==="Enter"&&i.click()}}return t}contentDiv(){const e=document.createElement("div");e.dataset.id=this.settings.id;const t=this.searchDiv();e.appendChild(t.main);const s=this.listDiv();return e.appendChild(s),{main:e,search:t,list:s}}moveContent(){if(this.settings.contentPosition==="relative"){this.moveContentBelow();return}if(this.settings.openPosition==="down"){this.moveContentBelow();return}else if(this.settings.openPosition==="up"){this.moveContentAbove();return}this.putContent()==="up"?this.moveContentAbove():this.moveContentBelow()}searchDiv(){const e=document.createElement("div"),t=document.createElement("input"),s=document.createElement("div");e.classList.add(this.classes.search);const i={main:e,input:t};if(this.settings.showSearch||(e.classList.add(this.classes.hide),t.readOnly=!0),t.type="search",t.placeholder=this.settings.searchPlaceholder,t.tabIndex=-1,t.setAttribute("aria-label",this.settings.searchPlaceholder),t.setAttribute("aria-autocomplete","list"),t.setAttribute("autocapitalize","off"),t.setAttribute("autocomplete","off"),t.setAttribute("autocorrect","off"),t.setAttribute("aria-hidden","true"),t.oninput=C(l=>{this.callbacks.search(l.target.value)},100),t.onkeydown=l=>{switch(l.key){case"ArrowUp":case"ArrowDown":return l.key==="ArrowDown"?this.highlight("down"):this.highlight("up"),!1;case"Tab":return this.callbacks.close(),!0;case"Escape":return this.callbacks.close(),!1;case" ":const a=this.content.list.querySelector("."+this.classes.highlighted);return a?(a.click(),!1):!0;case"Enter":const n=this.content.list.querySelector("."+this.classes.highlighted);return n?(n.click(),!1):this.callbacks.addable?(s.click(),!1):!0}return!0},e.appendChild(t),this.callbacks.addable){s.classList.add(this.classes.addable);const l=document.createElementNS("http://www.w3.org/2000/svg","svg");l.setAttribute("viewBox","0 0 100 100");const a=document.createElementNS("http://www.w3.org/2000/svg","path");a.setAttribute("d",this.classes.addablePath),l.appendChild(a),s.appendChild(l),s.onclick=n=>{if(n.preventDefault(),n.stopPropagation(),!this.callbacks.addable)return;const o=this.content.search.input.value.trim();if(o===""){this.content.search.input.focus();return}const c=d=>{let f=new p(d);if(this.callbacks.addOption(f),this.settings.isMultiple){let u=this.store.getSelected();u.push(f.id),this.callbacks.setSelected(u,!0)}else this.callbacks.setSelected([f.id],!0);this.callbacks.search(""),this.settings.closeOnSelect&&setTimeout(()=>{this.callbacks.close()},100)},r=this.callbacks.addable(o);r===!1||r===void 0||r===null||(r instanceof Promise?r.then(d=>{typeof d=="string"?c({text:d,value:d}):r instanceof Error?this.renderError(r.message):c(d)}):typeof r=="string"?c({text:r,value:r}):r instanceof Error?this.renderError(r.message):c(r))},e.appendChild(s),i.addable={main:s,svg:l,path:a}}return i}searchFocus(){this.content.search.input.focus()}getOptions(e=!1,t=!1,s=!1){let i="."+this.classes.option;return e&&(i+=":not(."+this.classes.placeholder+")"),t&&(i+=":not(."+this.classes.disabled+")"),s&&(i+=":not(."+this.classes.hide+")"),Array.from(this.content.list.querySelectorAll(i))}highlight(e){const t=this.getOptions(!0,!0,!0);if(t.length===0)return;if(t.length===1&&!t[0].classList.contains(this.classes.highlighted)){t[0].classList.add(this.classes.highlighted);return}let s=!1;for(const l of t)l.classList.contains(this.classes.highlighted)&&(s=!0);if(!s){for(const l of t)if(l.classList.contains(this.classes.selected)){l.classList.add(this.classes.highlighted);break}}for(let l=0;l<t.length;l++)if(t[l].classList.contains(this.classes.highlighted)){const a=t[l];a.classList.remove(this.classes.highlighted);const n=a.parentElement;if(n&&n.classList.contains(this.classes.mainOpen)){const r=n.querySelector("."+this.classes.optgroupLabel);r&&r.click()}let o=t[e==="down"?l+1<t.length?l+1:0:l-1>=0?l-1:t.length-1];o.classList.add(this.classes.highlighted),this.ensureElementInView(this.content.list,o),o.id&&this.main.main.setAttribute("aria-activedescendant",o.id);const c=o.parentElement;if(c&&c.classList.contains(this.classes.close)){const r=c.querySelector("."+this.classes.optgroupLabel);r&&r.click()}return}const i=t[e==="down"?0:t.length-1];i.classList.add(this.classes.highlighted),i.id&&this.main.main.setAttribute("aria-activedescendant",i.id),this.ensureElementInView(this.content.list,i)}listDiv(){const e=document.createElement("div");e.classList.add(this.classes.list);const t=this.settings.id+"-list";return e.id=t,e.dataset.id=t,e}renderError(e){this.content.list.innerHTML="";const t=document.createElement("div");t.classList.add(this.classes.error),t.textContent=e,this.content.list.appendChild(t)}renderSearching(){this.content.list.innerHTML="";const e=document.createElement("div");e.classList.add(this.classes.searching),e.textContent=this.settings.searchingText,this.content.list.appendChild(e)}renderOptions(e){if(this.content.list.innerHTML="",e.length===0){const s=document.createElement("div");s.classList.add(this.classes.search),this.callbacks.addable?s.innerHTML=this.settings.addableText.replace("{value}",this.content.search.input.value):s.innerHTML=this.settings.searchText,this.content.list.appendChild(s);return}this.settings.allowDeselect&&!this.settings.isMultiple&&(this.store.filter(i=>i.placeholder,!1).length||this.store.addOption(new p({text:"",value:"",selected:!1,placeholder:!0}),!0));const t=document.createDocumentFragment();for(const s of e){if(s instanceof g){const i=document.createElement("div");i.classList.add(this.classes.optgroup);const l=document.createElement("div");l.classList.add(this.classes.optgroupLabel),i.appendChild(l);const a=document.createElement("div");a.classList.add(this.classes.optgroupLabelText),a.textContent=s.label,l.appendChild(a);const n=document.createElement("div");if(n.classList.add(this.classes.optgroupActions),l.appendChild(n),this.settings.isMultiple&&s.selectAll){const o=document.createElement("div");o.classList.add(this.classes.optgroupSelectAll);let c=!0;for(const m of s.options)if(!m.selected){c=!1;break}c&&o.classList.add(this.classes.selected);const r=document.createElement("span");r.textContent=s.selectAllText,o.appendChild(r);const d=document.createElementNS("http://www.w3.org/2000/svg","svg");d.setAttribute("viewBox","0 0 100 100"),o.appendChild(d);const f=document.createElementNS("http://www.w3.org/2000/svg","path");f.setAttribute("d",this.classes.optgroupSelectAllBox),d.appendChild(f);const u=document.createElementNS("http://www.w3.org/2000/svg","path");u.setAttribute("d",this.classes.optgroupSelectAllCheck),d.appendChild(u),o.addEventListener("click",m=>{m.preventDefault(),m.stopPropagation();const b=this.store.getSelected();if(c){const S=b.filter(y=>{for(const w of s.options)if(y===w.id)return!1;return!0});this.callbacks.setSelected(S,!0);return}else{let S=s.options.map(w=>w.id).filter(w=>w!==void 0);const y=b.concat(S);for(const w of s.options)w.id&&!this.store.getOptionByID(w.id)&&this.callbacks.addOption(new p(w));this.callbacks.setSelected(y,!0);return}}),n.appendChild(o)}if(s.closable!=="off"){const o=document.createElement("div");o.classList.add(this.classes.optgroupClosable);const c=document.createElementNS("http://www.w3.org/2000/svg","svg");c.setAttribute("viewBox","0 0 100 100"),c.classList.add(this.classes.arrow),o.appendChild(c);const r=document.createElementNS("http://www.w3.org/2000/svg","path");c.appendChild(r),s.options.some(d=>d.selected)||this.content.search.input.value.trim()!==""?(o.classList.add(this.classes.mainOpen),r.setAttribute("d",this.classes.arrowOpen)):s.closable==="open"?(i.classList.add(this.classes.mainOpen),r.setAttribute("d",this.classes.arrowOpen)):s.closable==="close"&&(i.classList.add(this.classes.close),r.setAttribute("d",this.classes.arrowClose)),l.addEventListener("click",d=>{d.preventDefault(),d.stopPropagation(),i.classList.contains(this.classes.close)?(i.classList.remove(this.classes.close),i.classList.add(this.classes.mainOpen),r.setAttribute("d",this.classes.arrowOpen)):(i.classList.remove(this.classes.mainOpen),i.classList.add(this.classes.close),r.setAttribute("d",this.classes.arrowClose))}),n.appendChild(o)}i.appendChild(l);for(const o of s.options)i.appendChild(this.option(new p(o))),t.appendChild(i)}s instanceof p&&t.appendChild(this.option(s))}this.content.list.appendChild(t)}option(e){if(e.placeholder){const s=document.createElement("div");return s.classList.add(this.classes.option),s.classList.add(this.classes.hide),s}const t=document.createElement("div");return t.dataset.id=e.id,t.id=this.settings.id+"-"+e.id,t.classList.add(this.classes.option),t.setAttribute("role","option"),e.class&&e.class.split(" ").forEach(s=>{t.classList.add(s)}),e.style&&(t.style.cssText=e.style),this.settings.searchHighlight&&this.content.search.input.value.trim()!==""?t.innerHTML=this.highlightText(e.html!==""?e.html:e.text,this.content.search.input.value,this.classes.searchHighlighter):e.html!==""?t.innerHTML=e.html:t.textContent=e.text,this.settings.showOptionTooltips&&t.textContent&&t.setAttribute("title",t.textContent),e.display||t.classList.add(this.classes.hide),e.disabled&&t.classList.add(this.classes.disabled),e.selected&&this.settings.hideSelected&&t.classList.add(this.classes.hide),e.selected?(t.classList.add(this.classes.selected),t.setAttribute("aria-selected","true"),this.main.main.setAttribute("aria-activedescendant",t.id)):(t.classList.remove(this.classes.selected),t.setAttribute("aria-selected","false")),t.addEventListener("click",s=>{s.preventDefault(),s.stopPropagation();const i=this.store.getSelected(),l=s.currentTarget,a=String(l.dataset.id),n=s.ctrlKey||s.metaKey;if(e.disabled||!this.settings.isMultiple&&e.selected&&!this.settings.allowDeselect||e.selected&&e.mandatory||this.settings.isMultiple&&this.settings.maxSelected<=i.length&&!e.selected||this.settings.isMultiple&&this.settings.minSelected>=i.length&&e.selected&&!n)return;let o=!1;const c=this.store.getSelectedOptions();let r=[];if(this.settings.isMultiple){const d=c.some(u=>u.id===a);if(s.shiftKey&&this.lastSelectedOption){const u=this.store.getDataOptions(),m=u.findIndex(S=>S.id===this.lastSelectedOption.id),b=u.findIndex(S=>S.id===e.id);if(m>=0&&b>=0){const S=Math.min(m,b),y=Math.max(m,b),E=u.slice(S,y+1).filter(R=>!c.find(q=>q.id===R.id));c.length+E.length<=this.settings.maxSelected?r=c.concat(E):r=c}else r=c}else n?(d?r=c.filter(u=>u.id!==a):r=c.concat(e),this.lastSelectedOption=e):(d?r=c.filter(u=>u.id!==a):r=c.concat(e),this.lastSelectedOption=e)}if(this.settings.isMultiple||(e.selected?r=[]:r=[e]),this.callbacks.beforeChange||(o=!0),this.callbacks.beforeChange&&(this.callbacks.beforeChange(r,c)===!1?o=!1:o=!0),o){this.store.getOptionByID(a)||this.callbacks.addOption(e),this.callbacks.setSelected(r.map(u=>u.id),!1);const d=s.ctrlKey||s.metaKey||s.shiftKey;this.settings.closeOnSelect&&!(this.settings.isMultiple&&d)&&this.callbacks.close(),this.callbacks.afterChange&&this.callbacks.afterChange(r)}}),t}destroy(){this.main.main.remove(),this.content.main.remove()}highlightText(e,t,s){const i=t.trim();if(i==="")return e;const l=i.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),a=document.createElement("div");a.innerHTML=e;const n=o=>{if(o.nodeType===Node.TEXT_NODE){const c=o.textContent||"",r=new RegExp("("+l+")","i");if(r.test(c)){const d=document.createElement("span");c.split(r).forEach((u,m)=>{if(u&&r.test(u)){const b=document.createElement("mark");b.className=s,b.textContent=u,d.appendChild(b)}else u&&d.appendChild(document.createTextNode(u))}),o.parentNode?.replaceChild(d,o)}}else o.nodeType===Node.ELEMENT_NODE&&Array.from(o.childNodes).forEach(c=>n(c))};return Array.from(a.childNodes).forEach(o=>n(o)),a.innerHTML}moveContentAbove(){const e=this.main.main.offsetHeight,t=this.content.main.offsetHeight;this.main.main.classList.remove(this.classes.dirBelow),this.main.main.classList.add(this.classes.dirAbove),this.content.main.classList.remove(this.classes.dirBelow),this.content.main.classList.add(this.classes.dirAbove);const s=this.main.main.getBoundingClientRect();this.content.main.style.margin="-"+(e+t-1)+"px 0px 0px 0px",this.content.main.style.top=s.top+s.height+(this.settings.contentPosition==="fixed"?0:window.scrollY)+"px",this.content.main.style.left=s.left+(this.settings.contentPosition==="fixed"?0:window.scrollX)+"px",this.content.main.style.width=s.width+"px"}moveContentBelow(){this.main.main.classList.remove(this.classes.dirAbove),this.main.main.classList.add(this.classes.dirBelow),this.content.main.classList.remove(this.classes.dirAbove),this.content.main.classList.add(this.classes.dirBelow);const e=this.main.main.getBoundingClientRect();this.content.main.style.margin="-1px 0px 0px 0px",this.settings.contentPosition!=="relative"&&(this.content.main.style.top=e.top+e.height+(this.settings.contentPosition==="fixed"?0:window.scrollY)+"px",this.content.main.style.left=e.left+(this.settings.contentPosition==="fixed"?0:window.scrollX)+"px",this.content.main.style.width=e.width+"px")}ensureElementInView(e,t){const s=e.scrollTop+e.offsetTop,i=s+e.clientHeight,l=t.offsetTop,a=l+t.clientHeight;l<s?e.scrollTop-=s-l:a>i&&(e.scrollTop+=a-i)}putContent(){const e=this.main.main.offsetHeight,t=this.main.main.getBoundingClientRect(),s=this.content.main.offsetHeight;return window.innerHeight-(t.top+e)<=s&&t.top>s?"up":"down"}updateDeselectAll(){if(!this.store||!this.settings)return;const e=this.store.getSelectedOptions(),t=e&&e.length>0,s=this.settings.isMultiple,i=this.settings.allowDeselect,l=this.main.deselect.main,a=this.classes.hide;i&&!(s&&!t)?l.classList.remove(a):l.classList.add(a)}}class P{select;onValueChange;onClassChange;onDisabledChange;onOptionsChange;onLabelClick;listen=!1;observer=null;isUpdating=!1;pendingOptionsChange=null;preventNativeSelect=null;preventNativeSelectMousedown=null;preventNativeSelectFocus=null;constructor(e){this.select=e,this.valueChange=this.valueChange.bind(this),this.select.addEventListener("change",this.valueChange,{passive:!0}),this.observer=new MutationObserver(this.observeCall.bind(this)),this.changeListen(!0)}enable(){this.select.disabled=!1}disable(){this.select.disabled=!0}hideUI(){this.select.tabIndex=-1,this.select.style.position="absolute",this.select.style.width="1px",this.select.style.height="1px",this.select.style.opacity="0",this.select.style.overflow="hidden",this.select.style.pointerEvents="none",this.select.style.margin="0",this.select.style.padding="0",this.select.style.borderWidth="0",this.select.style.clip="rect(0 0 0 0)",this.select.setAttribute("aria-hidden","true"),this.preventNativeSelect||(this.preventNativeSelect=e=>{e.preventDefault(),e.stopPropagation(),e.stopImmediatePropagation()},this.preventNativeSelectMousedown=e=>{e.preventDefault(),e.stopPropagation(),e.stopImmediatePropagation()},this.preventNativeSelectFocus=e=>{e.preventDefault(),e.stopPropagation(),e.stopImmediatePropagation()},this.select.addEventListener("click",this.preventNativeSelect,{capture:!0,passive:!1}),this.select.addEventListener("mousedown",this.preventNativeSelectMousedown,{capture:!0,passive:!1}),this.select.addEventListener("focus",this.preventNativeSelectFocus,{capture:!0,passive:!1}))}showUI(){this.select.removeAttribute("tabindex"),this.select.style.position="",this.select.style.width="",this.select.style.height="",this.select.style.opacity="",this.select.style.overflow="",this.select.style.pointerEvents="",this.select.style.margin="",this.select.style.padding="",this.select.style.borderWidth="",this.select.style.clip="",this.select.removeAttribute("aria-hidden"),this.preventNativeSelect&&(this.select.removeEventListener("click",this.preventNativeSelect,{capture:!0}),this.preventNativeSelect=null),this.preventNativeSelectMousedown&&(this.select.removeEventListener("mousedown",this.preventNativeSelectMousedown,{capture:!0}),this.preventNativeSelectMousedown=null),this.preventNativeSelectFocus&&(this.select.removeEventListener("focus",this.preventNativeSelectFocus,{capture:!0}),this.preventNativeSelectFocus=null)}changeListen(e){this.listen=e,e&&this.observer&&this.observer.observe(this.select,{subtree:!0,childList:!0,attributes:!0}),e||this.observer&&this.observer.disconnect()}valueChange(e){return this.listen&&this.onValueChange&&this.onValueChange(this.getSelectedOptions()),!0}observeCall(e){if(!this.listen)return;let t=!1,s=!1,i=!1;for(const l of e){if(l.target===this.select&&(l.attributeName==="disabled"&&(s=!0),l.attributeName==="class"&&(t=!0),l.type==="childList")){for(const a of Array.from(l.addedNodes))if(a.nodeName==="OPTION"&&a.value===this.select.value){this.select.dispatchEvent(new Event("change"));break}i=!0}(l.target.nodeName==="OPTGROUP"||l.target.nodeName==="OPTION")&&(i=!0)}if(t&&this.onClassChange&&this.onClassChange(this.select.className.split(" ")),s&&this.onDisabledChange&&(this.changeListen(!1),this.onDisabledChange(this.select.disabled),this.changeListen(!0)),i&&this.onOptionsChange){if(this.isUpdating){if(this.select.options.length>0){const l=this.getData();l.length>0&&(this.pendingOptionsChange=l)}return}this.changeListen(!1),this.onOptionsChange(this.getData()),this.changeListen(!0)}}getData(){let e=[];const t=this.select.childNodes;for(const s of t)s.nodeName==="OPTGROUP"&&e.push(this.getDataFromOptgroup(s)),s.nodeName==="OPTION"&&e.push(this.getDataFromOption(s));return e}getDataFromOptgroup(e){let t={id:e.id,label:e.label,selectAll:e.dataset?e.dataset.selectall==="true":!1,selectAllText:e.dataset?e.dataset.selectalltext:"Select all",closable:e.dataset?e.dataset.closable:"off",options:[]};const s=e.childNodes;for(const i of s)i.nodeName==="OPTION"&&t.options.push(this.getDataFromOption(i));return t}getDataFromOption(e){return{id:e.id,value:e.value,text:e.text,html:e.dataset&&e.dataset.html?e.dataset.html:"",defaultSelected:e.defaultSelected,selected:e.selected,display:e.style.display!=="none",disabled:e.disabled,mandatory:e.dataset?e.dataset.mandatory==="true":!1,placeholder:e.dataset.placeholder==="true",class:e.className,style:e.style.cssText,data:e.dataset}}getSelectedOptions(){let e=[];const t=this.select.childNodes;for(const s of t){if(s.nodeName==="OPTGROUP"){const i=s.childNodes;for(const l of i)if(l.nodeName==="OPTION"){const a=l;a.selected&&e.push(this.getDataFromOption(a))}}if(s.nodeName==="OPTION"){const i=s;i.selected&&e.push(this.getDataFromOption(i))}}return e}getSelectedValues(){return this.getSelectedOptions().map(e=>e.value)}setSelected(e){this.changeListen(!1);const t=this.select.childNodes;for(const s of t){if(s.nodeName==="OPTGROUP"){const l=s.childNodes;for(const a of l)if(a.nodeName==="OPTION"){const n=a;n.selected=e.includes(n.id)}}if(s.nodeName==="OPTION"){const i=s;i.selected=e.includes(i.id)}}this.changeListen(!0)}setSelectedByValue(e){this.changeListen(!1);const t=this.select.childNodes;for(const s of t){if(s.nodeName==="OPTGROUP"){const l=s.childNodes;for(const a of l)if(a.nodeName==="OPTION"){const n=a;n.selected=e.includes(n.value)}}if(s.nodeName==="OPTION"){const i=s;i.selected=e.includes(i.value)}}this.changeListen(!0)}updateSelect(e,t,s){this.changeListen(!1),e&&(this.select.dataset.id=e),t&&(this.select.style.cssText=t),s&&(this.select.className="",s.forEach(i=>{i.trim()!==""&&this.select.classList.add(i.trim())})),this.changeListen(!0)}updateOptions(e){if(!(!e||e.length===0)){this.isUpdating=!0,this.pendingOptionsChange=null,this.changeListen(!1),this.select.innerHTML="";for(const t of e)t instanceof g&&this.select.appendChild(this.createOptgroup(t)),t instanceof p&&this.select.appendChild(this.createOption(t));if(this.select.dispatchEvent(new Event("change",{bubbles:!0})),this.changeListen(!0),this.isUpdating=!1,this.pendingOptionsChange!==null){const t=this.pendingOptionsChange;t.length>0&&this.onOptionsChange?(this.pendingOptionsChange=null,this.changeListen(!1),this.onOptionsChange(t),this.changeListen(!0)):this.pendingOptionsChange=null}}}createOptgroup(e){const t=document.createElement("optgroup");if(t.id=e.id,t.label=e.label,e.selectAll&&(t.dataset.selectAll="true"),e.closable!=="off"&&(t.dataset.closable=e.closable),e.options)for(const s of e.options)t.appendChild(this.createOption(s));return t}createOption(e){const t=document.createElement("option");return t.id=e.id,t.value=e.value,t.textContent=e.text,e.html!==""&&t.setAttribute("data-html",e.html),t.defaultSelected=e.defaultSelected,t.selected=e.selected,e.disabled&&(t.disabled=!0),e.display||(t.style.display="none"),e.placeholder&&t.setAttribute("data-placeholder","true"),e.mandatory&&t.setAttribute("data-mandatory","true"),e.class&&e.class.split(" ").forEach(s=>{t.classList.add(s)}),e.data&&typeof e.data=="object"&&Object.keys(e.data).forEach(s=>{t.setAttribute("data-"+k(s),e.data[s])}),t}setupLabelHandlers(){const e=[],t=this.select.id;t&&document.querySelectorAll(`label[for="${t}"]`).forEach(a=>e.push(a));let s=this.select.parentElement;for(;s&&s!==document.body;){if(s.tagName==="LABEL"){e.push(s);break}s=s.parentElement}Array.from(new Set(e)).forEach(l=>{if(l.__slimSelectLabelHandler)return;const a=n=>{n.target===l&&(n.preventDefault(),this.onLabelClick&&this.onLabelClick())};l.__slimSelectLabelHandler=a,l.addEventListener("click",a,{capture:!0,passive:!1})})}removeLabelHandlers(){const e=[],t=this.select.id;t&&document.querySelectorAll(`label[for="${t}"]`).forEach(a=>e.push(a));let s=this.select.parentElement;for(;s&&s!==document.body;){if(s.tagName==="LABEL"){e.push(s);break}s=s.parentElement}Array.from(new Set(e)).forEach(l=>{const a=l.__slimSelectLabelHandler;a&&(l.removeEventListener("click",a,{capture:!0}),delete l.__slimSelectLabelHandler)})}destroy(){this.changeListen(!1),this.select.removeEventListener("change",this.valueChange),this.preventNativeSelect&&(this.select.removeEventListener("click",this.preventNativeSelect,{capture:!0}),this.preventNativeSelect=null),this.preventNativeSelectMousedown&&(this.select.removeEventListener("mousedown",this.preventNativeSelectMousedown,{capture:!0}),this.preventNativeSelectMousedown=null),this.preventNativeSelectFocus&&(this.select.removeEventListener("focus",this.preventNativeSelectFocus,{capture:!0}),this.preventNativeSelectFocus=null),this.observer&&(this.observer.disconnect(),this.observer=null),this.removeLabelHandlers(),delete this.select.dataset.id,this.showUI()}}class x{id="";style="";class=[];isMultiple=!1;isOpen=!1;isFullOpen=!1;intervalMove=null;disabled;alwaysOpen;showSearch;focusSearch;ariaLabel;searchPlaceholder;searchText;searchingText;searchHighlight;closeOnSelect;contentLocation;contentPosition;openPosition;placeholderText;allowDeselect;hideSelected;keepOrder;showOptionTooltips;minSelected;maxSelected;timeoutDelay;maxValuesShown;maxValuesMessage;addableText;constructor(e){e||(e={}),this.id="ss-"+L(),this.style=e.style||"",this.class=e.class||[],this.disabled=e.disabled!==void 0?e.disabled:!1,this.alwaysOpen=e.alwaysOpen!==void 0?e.alwaysOpen:!1,this.showSearch=e.showSearch!==void 0?e.showSearch:!0,this.focusSearch=e.focusSearch!==void 0?e.focusSearch:!0,this.ariaLabel=e.ariaLabel||"Combobox",this.searchPlaceholder=e.searchPlaceholder||"Search...",this.searchText=e.searchText||"No Results",this.searchingText=e.searchingText||"Searching...",this.searchHighlight=e.searchHighlight!==void 0?e.searchHighlight:!1,this.closeOnSelect=e.closeOnSelect!==void 0?e.closeOnSelect:!0,this.contentLocation=e.contentLocation||document.body,this.contentPosition=e.contentPosition||"absolute",this.openPosition=e.openPosition||"auto",this.placeholderText=e.placeholderText!==void 0?e.placeholderText:"Select Value",this.allowDeselect=e.allowDeselect!==void 0?e.allowDeselect:!1,this.hideSelected=e.hideSelected!==void 0?e.hideSelected:!1,this.keepOrder=e.keepOrder!==void 0?e.keepOrder:!1,this.showOptionTooltips=e.showOptionTooltips!==void 0?e.showOptionTooltips:!1,this.minSelected=e.minSelected||0,this.maxSelected=e.maxSelected||1e3,this.timeoutDelay=e.timeoutDelay||200,this.maxValuesShown=e.maxValuesShown||20,this.maxValuesMessage=e.maxValuesMessage||"{number} selected",this.addableText=e.addableText||'Press "Enter" to add {value}'}}let V=class{selectEl;settings;cssClasses;select;store;render;openTimeout=null;closeTimeout=null;events={search:void 0,searchFilter:(e,t)=>e.text.toLowerCase().indexOf(t.toLowerCase())!==-1,addable:void 0,beforeChange:void 0,afterChange:void 0,beforeOpen:void 0,afterOpen:void 0,beforeClose:void 0,afterClose:void 0};constructor(e){if(this.selectEl=typeof e.select=="string"?document.querySelector(e.select):e.select,!this.selectEl){e.events&&e.events.error&&e.events.error(new Error("Could not find select element"));return}if(this.selectEl.tagName!=="SELECT"){e.events&&e.events.error&&e.events.error(new Error("Element isnt of type select"));return}this.selectEl.dataset.ssid&&this.destroy(),this.settings=new x(e.settings),this.cssClasses=new T(e.cssClasses);const t=["beforeOpen","afterOpen","beforeClose","afterClose"];for(const n in e.events)e.events.hasOwnProperty(n)&&(t.indexOf(n)!==-1?this.events[n]=C(e.events[n],100):this.events[n]=e.events[n]);this.settings.disabled=e.settings?.disabled?e.settings.disabled:this.selectEl.disabled,this.settings.isMultiple=this.selectEl.multiple,this.settings.style=this.selectEl.style.cssText,this.settings.class=this.selectEl.className.split(" "),this.select=new P(this.selectEl),this.selectEl.id||(this.selectEl.id=this.settings.id),this.select.updateSelect(this.settings.id,this.settings.style,this.settings.class),this.select.hideUI(),this.select.onValueChange=n=>{this.setSelected(n.map(o=>o.id))},this.select.onClassChange=n=>{this.settings.class=n,this.render.updateClassStyles()},this.select.onDisabledChange=n=>{n?this.disable():this.enable()},this.select.onOptionsChange=n=>{this.setData(n||[])},this.select.onLabelClick=()=>{this.settings.disabled||(this.settings.isOpen?this.close():this.open())};const s=e.data?e.data:this.select.getData();this.store=new N(this.settings.isMultiple?"multiple":"single",s),e.data&&this.select.updateOptions(this.store.getData());const i={open:this.open.bind(this),close:this.close.bind(this),addable:this.events.addable?this.events.addable:void 0,setSelected:this.setSelected.bind(this),addOption:this.addOption.bind(this),search:this.search.bind(this),beforeChange:this.events.beforeChange,afterChange:this.events.afterChange};this.render=new M(this.settings,this.cssClasses,this.store,i),this.render.renderValues(),this.render.renderOptions(this.store.getData());const l=this.selectEl.getAttribute("aria-label"),a=this.selectEl.getAttribute("aria-labelledby");l?this.render.main.main.setAttribute("aria-label",l):a&&this.render.main.main.setAttribute("aria-labelledby",a),this.selectEl.parentNode&&this.selectEl.parentNode.insertBefore(this.render.main.main,this.selectEl.nextSibling),window.addEventListener("resize",this.windowResize,!1),this.settings.openPosition==="auto"&&window.addEventListener("scroll",this.windowScroll,!1),document.addEventListener("visibilitychange",this.windowVisibilityChange),this.settings.disabled&&this.disable(),this.settings.alwaysOpen&&this.open(),this.select.setupLabelHandlers(),this.selectEl.slim=this}enable(){this.settings.disabled=!1,this.select.enable(),this.render.enable()}disable(){this.settings.disabled=!0,this.select.disable(),this.render.disable()}getData(){return this.store.getData()}setData(e){const t=this.store.getSelected(),s=this.store.validateDataArray(e);if(s){this.events.error&&this.events.error(s);return}this.store.setData(e);const i=this.store.getData();this.select.updateOptions(i),this.render.renderValues(),this.render.renderOptions(i),this.events.afterChange&&!A(t,this.store.getSelected())&&this.events.afterChange(this.store.getSelectedOptions())}getSelected(){let e=this.store.getSelectedOptions();return this.settings.keepOrder&&(e=this.store.selectedOrderOptions(e)),e.map(t=>t.value)}setSelected(e,t=!0){const s=this.store.getSelected(),i=this.store.getDataOptions();e=Array.isArray(e)?e:[e];const l=[];for(const n of e){if(i.find(o=>o.id==n)){l.push(n);continue}for(const o of i.filter(c=>c.value==n))l.push(o.id)}this.store.setSelectedBy("id",l);const a=this.store.getData();this.select.updateOptions(a),this.render.renderValues(),this.render.content.search.input.value!==""?this.search(this.render.content.search.input.value):this.render.renderOptions(a),t&&this.events.afterChange&&!A(s,this.store.getSelected())&&this.events.afterChange(this.store.getSelectedOptions())}addOption(e){const t=this.store.getSelected();this.store.getDataOptions().some(i=>i.value===(e.value??e.text))||this.store.addOption(e);const s=this.store.getData();this.select.updateOptions(s),this.render.renderValues(),this.render.renderOptions(s),this.events.afterChange&&!A(t,this.store.getSelected())&&this.events.afterChange(this.store.getSelectedOptions())}open(){this.settings.disabled||this.settings.isOpen||(this.events.beforeOpen&&this.events.beforeOpen(),this.render.open(),this.settings.showSearch&&this.settings.focusSearch&&this.render.searchFocus(),this.settings.isOpen=!0,this.openTimeout=setTimeout(()=>{this.events.afterOpen&&this.events.afterOpen(),this.settings.isOpen&&(this.settings.isFullOpen=!0),document.addEventListener("click",this.documentClick)},this.settings.timeoutDelay),this.settings.contentPosition==="absolute"&&(this.settings.intervalMove&&clearInterval(this.settings.intervalMove),this.settings.intervalMove=setInterval(this.render.moveContent.bind(this.render),500)))}close(e=null){!this.settings.isOpen||this.settings.alwaysOpen||(this.events.beforeClose&&this.events.beforeClose(),this.render.close(),this.render.content.search.input.value!==""&&this.search(""),this.render.mainFocus(e),this.settings.isOpen=!1,this.settings.isFullOpen=!1,this.closeTimeout=setTimeout(()=>{this.events.afterClose&&this.events.afterClose(),document.removeEventListener("click",this.documentClick)},this.settings.timeoutDelay),this.settings.intervalMove&&clearInterval(this.settings.intervalMove))}search(e){if(this.render.content.search.input.value!==e&&(this.render.content.search.input.value=e),e===""){this.render.renderOptions(this.store.getData());return}if(!this.events.search){const s=e===""?this.store.getData():this.store.search(e,this.events.searchFilter);this.render.renderOptions(s);return}this.render.renderSearching();const t=this.events.search(e,this.store.getSelectedOptions());if(t instanceof Promise){t.then(s=>{this.store.setData(s,!0),this.select.updateOptions(this.store.getData()),this.render.renderOptions(this.store.getData())}).catch(s=>{this.render.renderError(typeof s=="string"?s:s.message)});return}else Array.isArray(t)?(this.store.setData(t,!0),this.select.updateOptions(this.store.getData()),this.render.renderOptions(this.store.getData())):this.render.renderError("Search event must return a promise or an array of data")}destroy(){this.openTimeout&&(clearTimeout(this.openTimeout),this.openTimeout=null),this.closeTimeout&&(clearTimeout(this.closeTimeout),this.closeTimeout=null),this.settings.intervalMove&&(clearInterval(this.settings.intervalMove),this.settings.intervalMove=null),document.removeEventListener("click",this.documentClick),window.removeEventListener("resize",this.windowResize,!1),this.settings.openPosition==="auto"&&window.removeEventListener("scroll",this.windowScroll,!1),document.removeEventListener("visibilitychange",this.windowVisibilityChange),this.store.setData([]),this.render.destroy(),this.select.destroy()}windowResize=C(()=>{!this.settings.isOpen&&!this.settings.isFullOpen||this.render.moveContent()});windowScroll=C(()=>{!this.settings.isOpen&&!this.settings.isFullOpen||this.render.moveContent()});documentClick=e=>{this.settings.isOpen&&e.target&&!D(e.target,this.settings.id)&&this.close(e.type)};windowVisibilityChange=()=>{document.hidden&&this.close()}};const B=O.defineComponent({name:"SlimSelect",props:{modelValue:{type:[String,Array,void 0]},multiple:{type:Boolean,default:!1},data:{type:Array},settings:{type:Object},events:{type:Object,default:()=>({})}},emits:["update:modelValue"],data(){return{slim:null}},mounted(){this.$slots.default&&this.$slots.default().length>0&&this.data&&console.warn("[SlimSelect Vue] Both slot content and data prop are provided. Data prop will take precedence and slot content will be ignored.");let e={select:this.$refs.slim};this.data&&(e.data=this.data),this.settings&&(e.settings=this.settings);const t=this.events?.afterChange;e.events={...this.events,afterChange:s=>{this.handleAfterChange(s,t)}},this.slim=new V(e),this.syncModelValueToSlimSelect(!1)},updated(){if(this.slim&&!this.data){const h=this.slim.getSelected(),e=Array.isArray(this.value)?this.value:[this.value];JSON.stringify(h.sort())!==JSON.stringify(e.sort())&&this.syncModelValueToSlimSelect(!1)}},beforeUnmount(){this.slim&&this.slim.destroy()},watch:{modelValue:{handler:function(h){this.slim&&this.syncModelValueToSlimSelect(!1)}},data:{handler:function(h){this.slim&&this.slim.setData(h)},deep:!0}},computed:{value:{get(){return this.getCleanValue(this.$props.modelValue)},set(h){this.$emit("update:modelValue",h)}}},methods:{getSlimSelect(){return this.slim},handleAfterChange(h,e){if(!this.slim)return;const t=this.multiple?h.map(o=>o.value):h.length>0?h[0].value:"",s=this.getCleanValue(this.$props.modelValue),i=this.slim.store.getDataOptions(),l=Array.isArray(s)?s.length>0&&s.every(o=>i.some(c=>c.value===o)):s!==""&&i.some(o=>o.value===s),a=Array.isArray(t)?t.length>0&&t.every(o=>i.some(c=>c.value===o)):t!==""&&i.some(o=>o.value===t);(Array.isArray(t)&&Array.isArray(this.value)?JSON.stringify(t.sort())!==JSON.stringify(this.value.sort()):this.value!==t)&&(l||a)&&(this.value=t),e&&e(h)},getCleanValue(h){const e=this.$props.multiple;return typeof h=="string"?e?[h]:h:Array.isArray(h)?e?h:h[0]:e?[]:""},syncModelValueToSlimSelect(h=!1){if(!this.slim||this.$props.modelValue===void 0)return;const e=this.getCleanValue(this.$props.modelValue),s=this.slim.getData().flatMap(l=>"label"in l?l.options:[l]);if(!(Array.isArray(e)?e.length>0&&e.every(l=>s.some(a=>a.value===l)):e!==""&&s.some(l=>l.value===e))&&!Array.isArray(e)&&!s.some(a=>a.placeholder)){const a=this.slim.getData(),n={value:"",text:"",placeholder:!0};this.slim.setData([n].concat(a))}this.slim.setSelected(e,h)}}}),I=(h,e)=>{const t=h.__vccOpts||h;for(const[s,i]of e)t[s]=i;return t},H=["multiple"];function F(h,e,t,s,i,l){return O.openBlock(),O.createElementBlock("select",{multiple:h.multiple,ref:"slim"},[O.renderSlot(h.$slots,"default")],8,H)}const _=I(B,[["render",F]]);v.Optgroup=g,v.Option=p,v.Settings=x,v.default=_,Object.defineProperties(v,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
1
|
+
(function(v,O){typeof exports=="object"&&typeof module<"u"?O(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],O):(v=typeof globalThis<"u"?globalThis:v||self,O(v.SlimSelectVue={},v.Vue))})(this,function(v,O){"use strict";class T{main;placeholder;values;single;max;value;valueText;valueDelete;valueOut;deselect;deselectPath;arrow;arrowClose;arrowOpen;content;contentOpen;dirAbove;dirBelow;search;searchHighlighter;searching;addable;addablePath;list;optgroup;optgroupLabel;optgroupLabelText;optgroupActions;optgroupSelectAll;optgroupSelectAllBox;optgroupSelectAllCheck;optgroupClosable;option;optionDelete;highlighted;mainOpen;close;selected;error;disabled;hide;constructor(e){e||(e={}),this.main=e.main||"ss-main",this.placeholder=e.placeholder||"ss-placeholder",this.values=e.values||"ss-values",this.single=e.single||"ss-single",this.max=e.max||"ss-max",this.value=e.value||"ss-value",this.valueText=e.valueText||"ss-value-text",this.valueDelete=e.valueDelete||"ss-value-delete",this.valueOut=e.valueOut||"ss-value-out",this.deselect=e.deselect||"ss-deselect",this.deselectPath=e.deselectPath||"M10,10 L90,90 M10,90 L90,10",this.arrow=e.arrow||"ss-arrow",this.arrowClose=e.arrowClose||"M10,30 L50,70 L90,30",this.arrowOpen=e.arrowOpen||"M10,70 L50,30 L90,70",this.content=e.content||"ss-content",this.contentOpen=e.contentOpen||"ss-open",this.dirAbove=e.dirAbove||"ss-dir-above",this.dirBelow=e.dirBelow||"ss-dir-below",this.search=e.search||"ss-search",this.searchHighlighter=e.searchHighlighter||"ss-search-highlight",this.searching=e.searching||"ss-searching",this.addable=e.addable||"ss-addable",this.addablePath=e.addablePath||"M50,10 L50,90 M10,50 L90,50",this.list=e.list||"ss-list",this.optgroup=e.optgroup||"ss-optgroup",this.optgroupLabel=e.optgroupLabel||"ss-optgroup-label",this.optgroupLabelText=e.optgroupLabelText||"ss-optgroup-label-text",this.optgroupActions=e.optgroupActions||"ss-optgroup-actions",this.optgroupSelectAll=e.optgroupSelectAll||"ss-selectall",this.optgroupSelectAllBox=e.optgroupSelectAllBox||"M60,10 L10,10 L10,90 L90,90 L90,50",this.optgroupSelectAllCheck=e.optgroupSelectAllCheck||"M30,45 L50,70 L90,10",this.optgroupClosable=e.optgroupClosable||"ss-closable",this.option=e.option||"ss-option",this.optionDelete=e.optionDelete||"M10,10 L90,90 M10,90 L90,10",this.highlighted=e.highlighted||"ss-highlighted",this.mainOpen=e.mainOpen||"ss-open",this.close=e.close||"ss-close",this.selected=e.selected||"ss-selected",this.error=e.error||"ss-error",this.disabled=e.disabled||"ss-disabled",this.hide=e.hide||"ss-hide"}}function L(){return Math.random().toString(36).substring(2,10)}function D(h,e){function t(i,l){return l&&i&&i.classList&&i.classList.contains(l)||l&&i&&i.dataset&&i.dataset.id&&i.dataset.id===e?i:null}function s(i,l){return!i||i===document?null:t(i,l)?i:s(i.parentNode,l)}return t(h,e)||s(h,e)}function C(h,e=50,t=!1){let s;return function(...i){const l=self,n=()=>{s=null,t||h.apply(l,i)},a=t&&!s;clearTimeout(s),s=setTimeout(n,e),a&&h.apply(l,i)}}function A(h,e){return JSON.stringify(h)===JSON.stringify(e)}function k(h){const e=h.replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,t=>"-"+t.toLowerCase());return h[0]===h[0].toUpperCase()?e.substring(1):e}class p{id;value;text;html;defaultSelected;selected;display;disabled;placeholder;class;style;data;mandatory;constructor(e){this.id=!e.id||e.id===""?L():e.id,this.value=e.value===void 0?e.text||"":e.value||"",this.text=e.text||"",this.html=e.html||"",this.defaultSelected=e.defaultSelected!==void 0?e.defaultSelected:!1,this.selected=e.selected!==void 0?e.selected:!1,this.display=e.display!==void 0?e.display:!0,this.disabled=e.disabled!==void 0?e.disabled:!1,this.mandatory=e.mandatory!==void 0?e.mandatory:!1,this.placeholder=e.placeholder!==void 0?e.placeholder:!1,this.class=e.class||"",this.style=e.style||"",this.data=e.data||{}}}class m{id;label;selectAll;selectAllText;closable;options;constructor(e){if(this.id=!e.id||e.id===""?L():e.id,this.label=e.label||"",this.selectAll=e.selectAll===void 0?!1:e.selectAll,this.selectAllText=e.selectAllText||"Select All",this.closable=e.closable||"off",this.options=[],e.options)for(const t of e.options)this.options.push(new p(t))}}class N{selectType="single";data=[];selectedOrder=[];constructor(e,t){this.selectType=e,this.setData(t)}validateDataArray(e){if(!Array.isArray(e))return new Error("Data must be an array");for(let t of e)if(t)if(t instanceof m||"label"in t){if(!("label"in t))return new Error("Optgroup must have a label");if("options"in t&&t.options)for(let s of t.options){const i=this.validateOption(s);if(i)return i}}else if(t instanceof p||"text"in t){const s=this.validateOption(t);if(s)return s}else return new Error("Data object must be a valid optgroup or option");return null}validateOption(e){return"text"in e?null:new Error("Option must have a text")}partialToFullData(e){let t=[];return e.forEach(s=>{if(s){if(s instanceof m||"label"in s){let i=[];"options"in s&&s.options&&s.options.forEach(l=>{i.push(new p(l))}),i.length>0&&t.push(new m(s))}(s instanceof p||"text"in s)&&t.push(new p(s))}}),t}setData(e,t=!1){const s=this.partialToFullData(e);if(t){const i=this.getSelectedOptions(),l=[];i.forEach(n=>{let a=!1;for(const o of s){if(o instanceof p&&o.id===n.id){a=!0;break}if(o instanceof m){for(const c of o.options)if(c.id===n.id){a=!0;break}}}a||l.push(n)}),this.data=[...l,...s]}else this.data=s;this.selectType==="single"&&this.setSelectedBy("id",this.getSelected())}getData(){return this.filter(null,!0)}getDataOptions(){return this.filter(null,!1)}addOption(e,t=!1){if(t){let s=[new p(e)];this.setData(s.concat(this.getData()))}else this.setData(this.getData().concat(new p(e)))}setSelectedBy(e,t){let s=null,i=!1;const l=[];for(let a of this.data){if(a instanceof m)for(let o of a.options){s||(s=o);let c=o[e]||"";o.selected=i?!1:t.includes(c),o.selected&&(l.push(o),this.selectType==="single"&&(i=!0))}a instanceof p&&(s||(s=a),a.selected=i?!1:t.includes(a[e]),a.selected&&(l.push(a),this.selectType==="single"&&(i=!0)))}this.selectType==="single"&&s&&!i&&(s.selected=!0,l.push(s));const n=t.map(a=>l.find(o=>o[e]===a)?.id||"");this.selectedOrder=n}getSelected(){return this.getSelectedOptions().map(e=>e.id)}getSelectedValues(){return this.getSelectedOptions().map(e=>e.value)}getSelectedOptions(){return this.filter(e=>e.selected,!1)}getOptgroupByID(e){for(let t of this.data)if(t instanceof m&&t.id===e)return t;return null}getOptionByID(e){let t=this.filter(s=>s.id===e,!1);return t.length?t[0]:null}getSelectType(){return this.selectType}getFirstOption(){let e=null;for(let t of this.data)if(t instanceof m?e=t.options[0]:t instanceof p&&(e=t),e)break;return e}search(e,t){return e=e.trim(),e===""?this.getData():this.filter(s=>t(s,e),!0)}filter(e,t){const s=[];return this.data.forEach(i=>{if(i instanceof m){let l=[];if(i.options.forEach(a=>{(!e||e(a))&&(t?l.push(new p(a)):s.push(new p(a)))}),l.length>0){let a=new m(i);a.options=l,s.push(a)}}i instanceof p&&(!e||e(i))&&s.push(new p(i))}),s}selectedOrderOptions(e){const t=[];return this.selectedOrder.forEach(s=>{const i=e.find(l=>l.id===s);i&&t.push(i)}),e.forEach(s=>{let i=!1;t.forEach(l=>{if(s.id===l.id){i=!0;return}}),i||t.push(s)}),t}}class M{settings;store;callbacks;lastSelectedOption;main;content;classes;constructor(e,t,s,i){this.store=s,this.settings=e,this.classes=t,this.callbacks=i,this.lastSelectedOption=null,this.main=this.mainDiv(),this.content=this.contentDiv(),this.updateClassStyles(),this.updateAriaAttributes(),this.settings.contentPosition!=="relative"&&(this.content.main.style.top="-9999px",this.content.main.style.left="-9999px",this.content.main.style.margin="0",this.content.main.style.width="auto"),this.settings.contentLocation&&this.settings.contentLocation.appendChild(this.content.main)}enable(){this.main.main.classList.remove(this.classes.disabled),this.main.main.setAttribute("aria-disabled","false"),this.content.search.input.disabled=!1}disable(){this.main.main.classList.add(this.classes.disabled),this.main.main.setAttribute("aria-disabled","true"),this.content.search.input.disabled=!0}open(){this.main.arrow.path.setAttribute("d",this.classes.arrowOpen),this.main.main.setAttribute("aria-expanded","true");const t=this.settings.openPosition==="up"?this.classes.dirAbove:this.classes.dirBelow;this.main.main.classList.add(t),this.content.main.classList.add(t),this.content.main.classList.add(this.classes.contentOpen),this.content.search.input.removeAttribute("aria-hidden"),this.moveContent();const s=this.store.getSelectedOptions();if(s.length){const i=s[s.length-1].id,l=this.content.list.querySelector('[data-id="'+i+'"]');l&&this.ensureElementInView(this.content.list,l)}}close(){this.main.main.setAttribute("aria-expanded","false"),this.main.arrow.path.setAttribute("d",this.classes.arrowClose),this.content.main.classList.remove(this.classes.contentOpen),this.content.search.input.setAttribute("aria-hidden","true"),this.main.main.removeAttribute("aria-activedescendant");const e=this.getAnimationTiming();setTimeout(()=>{this.main.main.classList.remove(this.classes.dirAbove,this.classes.dirBelow),this.content.main.classList.remove(this.classes.dirAbove,this.classes.dirBelow)},e)}getAnimationTiming(){const t=getComputedStyle(this.content.main).getPropertyValue("--ss-animation-timing").trim();if(t){if(t.endsWith("ms"))return parseFloat(t);if(t.endsWith("s"))return parseFloat(t)*1e3}return 200}updateClassStyles(){if(this.main.main.className="",this.main.main.removeAttribute("style"),this.content.main.className="",this.content.main.removeAttribute("style"),this.main.main.classList.add(this.classes.main),this.content.main.classList.add(this.classes.content),this.settings.style!==""&&(this.main.main.style.cssText=this.settings.style,this.content.main.style.cssText=this.settings.style),this.settings.class.length)for(const e of this.settings.class)e.trim()!==""&&(this.main.main.classList.add(e.trim()),this.content.main.classList.add(e.trim()));(this.settings.contentPosition==="relative"||this.settings.contentPosition==="fixed")&&this.content.main.classList.add("ss-"+this.settings.contentPosition)}updateAriaAttributes(){const e=this.content.list.id;this.main.main.role="combobox",this.main.main.setAttribute("aria-haspopup","listbox"),this.main.main.setAttribute("aria-controls",e),this.main.main.setAttribute("aria-expanded","false"),this.content.list.setAttribute("role","listbox"),this.content.list.setAttribute("aria-label",this.settings.ariaLabel+" listbox"),this.settings.isMultiple&&this.content.list.setAttribute("aria-multiselectable","true"),this.content.search.input.setAttribute("aria-controls",e)}mainDiv(){const e=document.createElement("div");e.dataset.id=this.settings.id,e.setAttribute("aria-label",this.settings.ariaLabel),e.tabIndex=0,e.onkeydown=c=>{switch(c.key){case"ArrowUp":case"ArrowDown":return this.callbacks.open(),c.key==="ArrowDown"?this.highlight("down"):this.highlight("up"),!1;case"Tab":return this.callbacks.close(),!0;case"Enter":case" ":this.callbacks.open();const r=this.content.list.querySelector("."+this.classes.highlighted);return r&&r.click(),!1;case"Escape":return this.callbacks.close(),!1}return c.key.length===1&&this.callbacks.open(),!0},e.onclick=c=>{this.settings.disabled||(this.settings.isOpen?this.callbacks.close():this.callbacks.open())};const t=document.createElement("div");t.classList.add(this.classes.values),e.appendChild(t);const s=document.createElement("div");s.classList.add(this.classes.deselect);const i=this.store?.getSelectedOptions();!this.settings.allowDeselect||this.settings.isMultiple&&i&&i.length<=0?s.classList.add(this.classes.hide):s.classList.remove(this.classes.hide),s.onclick=c=>{if(c.stopPropagation(),this.settings.disabled)return;let r=!0;const d=this.store.getSelectedOptions(),f=[];if(this.callbacks.beforeChange&&(r=this.callbacks.beforeChange(f,d)===!0),r){if(this.settings.isMultiple)this.callbacks.setSelected([],!1),this.updateDeselectAll();else{const u=this.store.getFirstOption(),g=u?u.id:"";this.callbacks.setSelected(g,!1)}this.settings.closeOnSelect&&this.callbacks.close(),this.callbacks.afterChange&&this.callbacks.afterChange(this.store.getSelectedOptions())}};const l=document.createElementNS("http://www.w3.org/2000/svg","svg");l.setAttribute("viewBox","0 0 100 100");const n=document.createElementNS("http://www.w3.org/2000/svg","path");n.setAttribute("d",this.classes.deselectPath),l.appendChild(n),s.appendChild(l),e.appendChild(s);const a=document.createElementNS("http://www.w3.org/2000/svg","svg");a.classList.add(this.classes.arrow),a.setAttribute("viewBox","0 0 100 100");const o=document.createElementNS("http://www.w3.org/2000/svg","path");return o.setAttribute("d",this.classes.arrowClose),this.settings.alwaysOpen&&a.classList.add(this.classes.hide),a.appendChild(o),e.appendChild(a),{main:e,values:t,deselect:{main:s,svg:l,path:n},arrow:{main:a,path:o}}}mainFocus(e){e!=="click"&&this.main.main.focus({preventScroll:!0})}placeholder(){const e=this.store.filter(i=>i.placeholder,!1);let t=this.settings.placeholderText;e.length&&(e[0].html!==""?t=e[0].html:e[0].text!==""&&(t=e[0].text));const s=document.createElement("div");return s.classList.add(this.classes.placeholder),s.innerHTML=t,s}renderValues(){if(!this.settings.isMultiple){this.renderSingleValue();return}this.renderMultipleValues(),this.updateDeselectAll()}renderSingleValue(){const e=this.store.filter(s=>s.selected&&!s.placeholder,!1),t=e.length>0?e[0]:null;if(!t)this.main.values.innerHTML=this.placeholder().outerHTML;else{const s=document.createElement("div");s.classList.add(this.classes.single),t.html?s.innerHTML=t.html:s.innerText=t.text,this.main.values.innerHTML=s.outerHTML}!this.settings.allowDeselect||!e.length?this.main.deselect.main.classList.add(this.classes.hide):this.main.deselect.main.classList.remove(this.classes.hide)}renderMultipleValues(){let e=this.main.values.childNodes,t=this.store.filter(i=>i.selected&&i.display,!1);if(t.length===0){this.main.values.innerHTML=this.placeholder().outerHTML;return}else{const i=this.main.values.querySelector("."+this.classes.placeholder);i&&i.remove()}if(t.length>this.settings.maxValuesShown){const i=document.createElement("div");i.classList.add(this.classes.max),i.textContent=this.settings.maxValuesMessage.replace("{number}",t.length.toString()),this.main.values.innerHTML=i.outerHTML;return}else{const i=this.main.values.querySelector("."+this.classes.max);i&&i.remove()}this.settings.keepOrder&&(t=this.store.selectedOrderOptions(t));let s=[];for(let i=0;i<e.length;i++){const l=e[i],n=l.getAttribute("data-id");n&&(t.filter(o=>o.id===n,!1).length||s.push(l))}for(const i of s)i.classList.add(this.classes.valueOut),setTimeout(()=>{this.main.values.hasChildNodes()&&this.main.values.contains(i)&&this.main.values.removeChild(i)},100);e=this.main.values.childNodes;for(let i=0;i<t.length;i++){let l=!0;for(let n=0;n<e.length;n++)t[i].id===String(e[n].dataset.id)&&(l=!1);l&&(this.settings.keepOrder?this.main.values.appendChild(this.multipleValue(t[i])):e.length===0?this.main.values.appendChild(this.multipleValue(t[i])):i===0?this.main.values.insertBefore(this.multipleValue(t[i]),e[i]):e[i-1].insertAdjacentElement("afterend",this.multipleValue(t[i])))}}multipleValue(e){const t=document.createElement("div");t.classList.add(this.classes.value),t.dataset.id=e.id;const s=document.createElement("div");if(s.classList.add(this.classes.valueText),s.textContent=e.text,t.appendChild(s),!e.mandatory){const i=document.createElement("div");i.classList.add(this.classes.valueDelete),i.setAttribute("tabindex","0"),i.onclick=a=>{if(a.preventDefault(),a.stopPropagation(),this.settings.disabled)return;let o=!0;const c=this.store.getSelectedOptions(),r=c.filter(d=>d.selected&&d.id!==e.id,!0);if(!(this.settings.minSelected&&r.length<this.settings.minSelected)&&(this.callbacks.beforeChange&&(o=this.callbacks.beforeChange(r,c)===!0),o)){let d=[];for(const f of r){if(f instanceof m)for(const u of f.options)u.id&&d.push(u.id);f instanceof p&&d.push(f.id)}this.callbacks.setSelected(d,!1),this.settings.closeOnSelect&&this.callbacks.close(),this.callbacks.afterChange&&this.callbacks.afterChange(r),this.updateDeselectAll()}};const l=document.createElementNS("http://www.w3.org/2000/svg","svg");l.setAttribute("viewBox","0 0 100 100");const n=document.createElementNS("http://www.w3.org/2000/svg","path");n.setAttribute("d",this.classes.optionDelete),l.appendChild(n),i.appendChild(l),t.appendChild(i),i.onkeydown=a=>{a.key==="Enter"&&i.click()}}return t}contentDiv(){const e=document.createElement("div");e.dataset.id=this.settings.id;const t=this.searchDiv();e.appendChild(t.main);const s=this.listDiv();return e.appendChild(s),{main:e,search:t,list:s}}moveContent(){if(this.settings.contentPosition==="relative"){this.moveContentBelow();return}if(this.settings.openPosition==="down"){this.moveContentBelow();return}else if(this.settings.openPosition==="up"){this.moveContentAbove();return}this.putContent()==="up"?this.moveContentAbove():this.moveContentBelow()}searchDiv(){const e=document.createElement("div"),t=document.createElement("input"),s=document.createElement("div");e.classList.add(this.classes.search);const i={main:e,input:t};if(this.settings.showSearch||(e.classList.add(this.classes.hide),t.readOnly=!0),t.type="search",t.placeholder=this.settings.searchPlaceholder,t.tabIndex=-1,t.setAttribute("aria-label",this.settings.searchPlaceholder),t.setAttribute("aria-autocomplete","list"),t.setAttribute("autocapitalize","off"),t.setAttribute("autocomplete","off"),t.setAttribute("autocorrect","off"),t.setAttribute("aria-hidden","true"),t.oninput=C(l=>{this.callbacks.search(l.target.value)},100),t.onkeydown=l=>{switch(l.key){case"ArrowUp":case"ArrowDown":return l.key==="ArrowDown"?this.highlight("down"):this.highlight("up"),!1;case"Tab":return this.callbacks.close(),!0;case"Escape":return this.callbacks.close(),!1;case" ":const n=this.content.list.querySelector("."+this.classes.highlighted);return n?(n.click(),!1):!0;case"Enter":const a=this.content.list.querySelector("."+this.classes.highlighted);return a?(a.click(),!1):this.callbacks.addable?(s.click(),!1):!0}return!0},e.appendChild(t),this.callbacks.addable){s.classList.add(this.classes.addable);const l=document.createElementNS("http://www.w3.org/2000/svg","svg");l.setAttribute("viewBox","0 0 100 100");const n=document.createElementNS("http://www.w3.org/2000/svg","path");n.setAttribute("d",this.classes.addablePath),l.appendChild(n),s.appendChild(l),s.onclick=a=>{if(a.preventDefault(),a.stopPropagation(),!this.callbacks.addable)return;const o=this.content.search.input.value.trim();if(o===""){this.content.search.input.focus();return}const c=d=>{let f=new p(d);if(this.callbacks.addOption(f),this.settings.isMultiple){let u=this.store.getSelected();u.push(f.id),this.callbacks.setSelected(u,!0)}else this.callbacks.setSelected([f.id],!0);this.callbacks.search(""),this.settings.closeOnSelect&&setTimeout(()=>{this.callbacks.close()},100)},r=this.callbacks.addable(o);r===!1||r===void 0||r===null||(r instanceof Promise?r.then(d=>{typeof d=="string"?c({text:d,value:d}):r instanceof Error?this.renderError(r.message):c(d)}):typeof r=="string"?c({text:r,value:r}):r instanceof Error?this.renderError(r.message):c(r))},e.appendChild(s),i.addable={main:s,svg:l,path:n}}return i}searchFocus(){this.content.search.input.focus({preventScroll:!0})}getOptions(e=!1,t=!1,s=!1){let i="."+this.classes.option;return e&&(i+=":not(."+this.classes.placeholder+")"),t&&(i+=":not(."+this.classes.disabled+")"),s&&(i+=":not(."+this.classes.hide+")"),Array.from(this.content.list.querySelectorAll(i))}highlight(e){const t=this.getOptions(!0,!0,!0);if(t.length===0)return;if(t.length===1&&!t[0].classList.contains(this.classes.highlighted)){t[0].classList.add(this.classes.highlighted);return}let s=!1;for(const l of t)l.classList.contains(this.classes.highlighted)&&(s=!0);if(!s){for(const l of t)if(l.classList.contains(this.classes.selected)){l.classList.add(this.classes.highlighted);break}}for(let l=0;l<t.length;l++)if(t[l].classList.contains(this.classes.highlighted)){const n=t[l];n.classList.remove(this.classes.highlighted);const a=n.parentElement;if(a&&a.classList.contains(this.classes.mainOpen)){const r=a.querySelector("."+this.classes.optgroupLabel);r&&r.click()}let o=t[e==="down"?l+1<t.length?l+1:0:l-1>=0?l-1:t.length-1];o.classList.add(this.classes.highlighted),this.ensureElementInView(this.content.list,o),o.id&&this.main.main.setAttribute("aria-activedescendant",o.id);const c=o.parentElement;if(c&&c.classList.contains(this.classes.close)){const r=c.querySelector("."+this.classes.optgroupLabel);r&&r.click()}return}const i=t[e==="down"?0:t.length-1];i.classList.add(this.classes.highlighted),i.id&&this.main.main.setAttribute("aria-activedescendant",i.id),this.ensureElementInView(this.content.list,i)}listDiv(){const e=document.createElement("div");e.classList.add(this.classes.list);const t=this.settings.id+"-list";return e.id=t,e.dataset.id=t,e}renderError(e){this.content.list.innerHTML="";const t=document.createElement("div");t.classList.add(this.classes.error),t.textContent=e,this.content.list.appendChild(t)}renderSearching(){this.content.list.innerHTML="";const e=document.createElement("div");e.classList.add(this.classes.searching),e.textContent=this.settings.searchingText,this.content.list.appendChild(e)}renderOptions(e){if(this.content.list.innerHTML="",e.length===0){const s=document.createElement("div");s.classList.add(this.classes.search),this.callbacks.addable?s.innerHTML=this.settings.addableText.replace("{value}",this.content.search.input.value):s.innerHTML=this.settings.searchText,this.content.list.appendChild(s);return}this.settings.allowDeselect&&!this.settings.isMultiple&&(this.store.filter(i=>i.placeholder,!1).length||this.store.addOption(new p({text:"",value:"",selected:!1,placeholder:!0}),!0));const t=document.createDocumentFragment();for(const s of e){if(s instanceof m){const i=document.createElement("div");i.classList.add(this.classes.optgroup);const l=document.createElement("div");l.classList.add(this.classes.optgroupLabel),i.appendChild(l);const n=document.createElement("div");n.classList.add(this.classes.optgroupLabelText),n.textContent=s.label,l.appendChild(n);const a=document.createElement("div");if(a.classList.add(this.classes.optgroupActions),l.appendChild(a),this.settings.isMultiple&&s.selectAll){const o=document.createElement("div");o.classList.add(this.classes.optgroupSelectAll);let c=!0;for(const g of s.options)if(!g.selected){c=!1;break}c&&o.classList.add(this.classes.selected);const r=document.createElement("span");r.textContent=s.selectAllText,o.appendChild(r);const d=document.createElementNS("http://www.w3.org/2000/svg","svg");d.setAttribute("viewBox","0 0 100 100"),o.appendChild(d);const f=document.createElementNS("http://www.w3.org/2000/svg","path");f.setAttribute("d",this.classes.optgroupSelectAllBox),d.appendChild(f);const u=document.createElementNS("http://www.w3.org/2000/svg","path");u.setAttribute("d",this.classes.optgroupSelectAllCheck),d.appendChild(u),o.addEventListener("click",g=>{g.preventDefault(),g.stopPropagation();const b=this.store.getSelected();if(c){const w=b.filter(y=>{for(const S of s.options)if(y===S.id)return!1;return!0});this.callbacks.setSelected(w,!0);return}else{let w=s.options.map(S=>S.id).filter(S=>S!==void 0);const y=b.concat(w);for(const S of s.options)S.id&&!this.store.getOptionByID(S.id)&&this.callbacks.addOption(new p(S));this.callbacks.setSelected(y,!0);return}}),a.appendChild(o)}if(s.closable!=="off"){const o=document.createElement("div");o.classList.add(this.classes.optgroupClosable);const c=document.createElementNS("http://www.w3.org/2000/svg","svg");c.setAttribute("viewBox","0 0 100 100"),c.classList.add(this.classes.arrow),o.appendChild(c);const r=document.createElementNS("http://www.w3.org/2000/svg","path");c.appendChild(r),s.options.some(d=>d.selected)||this.content.search.input.value.trim()!==""?(o.classList.add(this.classes.mainOpen),r.setAttribute("d",this.classes.arrowOpen)):s.closable==="open"?(i.classList.add(this.classes.mainOpen),r.setAttribute("d",this.classes.arrowOpen)):s.closable==="close"&&(i.classList.add(this.classes.close),r.setAttribute("d",this.classes.arrowClose)),l.addEventListener("click",d=>{d.preventDefault(),d.stopPropagation(),i.classList.contains(this.classes.close)?(i.classList.remove(this.classes.close),i.classList.add(this.classes.mainOpen),r.setAttribute("d",this.classes.arrowOpen)):(i.classList.remove(this.classes.mainOpen),i.classList.add(this.classes.close),r.setAttribute("d",this.classes.arrowClose))}),a.appendChild(o)}i.appendChild(l);for(const o of s.options)i.appendChild(this.option(new p(o))),t.appendChild(i)}s instanceof p&&t.appendChild(this.option(s))}this.content.list.appendChild(t)}option(e){if(e.placeholder){const s=document.createElement("div");return s.classList.add(this.classes.option),s.classList.add(this.classes.hide),s}const t=document.createElement("div");return t.dataset.id=e.id,t.id=this.settings.id+"-"+e.id,t.classList.add(this.classes.option),t.setAttribute("role","option"),e.class&&e.class.split(" ").forEach(s=>{t.classList.add(s)}),e.style&&(t.style.cssText=e.style),this.settings.searchHighlight&&this.content.search.input.value.trim()!==""?t.innerHTML=this.highlightText(e.html!==""?e.html:e.text,this.content.search.input.value,this.classes.searchHighlighter):e.html!==""?t.innerHTML=e.html:t.textContent=e.text,this.settings.showOptionTooltips&&t.textContent&&t.setAttribute("title",t.textContent),e.display||t.classList.add(this.classes.hide),e.disabled&&t.classList.add(this.classes.disabled),e.selected&&this.settings.hideSelected&&t.classList.add(this.classes.hide),e.selected?(t.classList.add(this.classes.selected),t.setAttribute("aria-selected","true"),this.main.main.setAttribute("aria-activedescendant",t.id)):(t.classList.remove(this.classes.selected),t.setAttribute("aria-selected","false")),t.addEventListener("click",s=>{s.preventDefault(),s.stopPropagation();const i=this.store.getSelected(),l=s.currentTarget,n=String(l.dataset.id),a=s.ctrlKey||s.metaKey;if(e.disabled||!this.settings.isMultiple&&e.selected&&!this.settings.allowDeselect||e.selected&&e.mandatory||this.settings.isMultiple&&this.settings.maxSelected<=i.length&&!e.selected||this.settings.isMultiple&&this.settings.minSelected>=i.length&&e.selected&&!a)return;let o=!1;const c=this.store.getSelectedOptions();let r=[];if(this.settings.isMultiple){const d=c.some(u=>u.id===n);if(s.shiftKey&&this.lastSelectedOption){const u=this.store.getDataOptions(),g=u.findIndex(w=>w.id===this.lastSelectedOption.id),b=u.findIndex(w=>w.id===e.id);if(g>=0&&b>=0){const w=Math.min(g,b),y=Math.max(g,b),x=u.slice(w,y+1).filter(R=>!c.find(q=>q.id===R.id));c.length+x.length<=this.settings.maxSelected?r=c.concat(x):r=c}else r=c}else a?(d?r=c.filter(u=>u.id!==n):r=c.concat(e),this.lastSelectedOption=e):(d?r=c.filter(u=>u.id!==n):r=c.concat(e),this.lastSelectedOption=e)}if(this.settings.isMultiple||(e.selected?r=[]:r=[e]),this.callbacks.beforeChange||(o=!0),this.callbacks.beforeChange&&(this.callbacks.beforeChange(r,c)===!1?o=!1:o=!0),o){this.store.getOptionByID(n)||this.callbacks.addOption(e),this.callbacks.setSelected(r.map(u=>u.id),!1);const d=s.ctrlKey||s.metaKey||s.shiftKey;this.settings.closeOnSelect&&!(this.settings.isMultiple&&d)&&this.callbacks.close(),this.callbacks.afterChange&&this.callbacks.afterChange(r)}}),t}destroy(){this.main.main.remove(),this.content.main.remove()}highlightText(e,t,s){const i=t.trim();if(i==="")return e;const l=i.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),n=document.createElement("div");n.innerHTML=e;const a=o=>{if(o.nodeType===Node.TEXT_NODE){const c=o.textContent||"",r=new RegExp("("+l+")","i");if(r.test(c)){const d=document.createElement("span");c.split(r).forEach((u,g)=>{if(u&&r.test(u)){const b=document.createElement("mark");b.className=s,b.textContent=u,d.appendChild(b)}else u&&d.appendChild(document.createTextNode(u))}),o.parentNode?.replaceChild(d,o)}}else o.nodeType===Node.ELEMENT_NODE&&Array.from(o.childNodes).forEach(c=>a(c))};return Array.from(n.childNodes).forEach(o=>a(o)),n.innerHTML}moveContentAbove(){const e=this.main.main.offsetHeight,t=this.content.main.offsetHeight;this.main.main.classList.remove(this.classes.dirBelow),this.main.main.classList.add(this.classes.dirAbove),this.content.main.classList.remove(this.classes.dirBelow),this.content.main.classList.add(this.classes.dirAbove);const s=this.main.main.getBoundingClientRect();this.content.main.style.margin="-"+(e+t-1)+"px 0px 0px 0px",this.content.main.style.top=s.top+s.height+(this.settings.contentPosition==="fixed"?0:window.scrollY)+"px",this.content.main.style.left=s.left+(this.settings.contentPosition==="fixed"?0:window.scrollX)+"px",this.content.main.style.width=s.width+"px"}moveContentBelow(){this.main.main.classList.remove(this.classes.dirAbove),this.main.main.classList.add(this.classes.dirBelow),this.content.main.classList.remove(this.classes.dirAbove),this.content.main.classList.add(this.classes.dirBelow);const e=this.main.main.getBoundingClientRect();this.content.main.style.margin="-1px 0px 0px 0px",this.settings.contentPosition!=="relative"&&(this.content.main.style.top=e.top+e.height+(this.settings.contentPosition==="fixed"?0:window.scrollY)+"px",this.content.main.style.left=e.left+(this.settings.contentPosition==="fixed"?0:window.scrollX)+"px",this.content.main.style.width=e.width+"px")}ensureElementInView(e,t){const s=e.scrollTop+e.offsetTop,i=s+e.clientHeight,l=t.offsetTop,n=l+t.clientHeight;l<s?e.scrollTop-=s-l:n>i&&(e.scrollTop+=n-i)}putContent(){const e=this.main.main.offsetHeight,t=this.main.main.getBoundingClientRect(),s=this.content.main.offsetHeight;return window.innerHeight-(t.top+e)<=s&&t.top>s?"up":"down"}updateDeselectAll(){if(!this.store||!this.settings)return;const e=this.store.getSelectedOptions(),t=e&&e.length>0,s=this.settings.isMultiple,i=this.settings.allowDeselect,l=this.main.deselect.main,n=this.classes.hide;i&&!(s&&!t)?l.classList.remove(n):l.classList.add(n)}}class P{select;onValueChange;onClassChange;onDisabledChange;onOptionsChange;onLabelClick;listen=!1;observer=null;isUpdating=!1;pendingOptionsChange=null;preventNativeSelect=null;preventNativeSelectMousedown=null;preventNativeSelectFocus=null;constructor(e){this.select=e,this.valueChange=this.valueChange.bind(this),this.select.addEventListener("change",this.valueChange,{passive:!0}),this.observer=new MutationObserver(this.observeCall.bind(this)),this.changeListen(!0)}enable(){this.select.disabled=!1}disable(){this.select.disabled=!0}hideUI(){this.select.tabIndex=-1,this.select.style.position="absolute",this.select.style.width="1px",this.select.style.height="1px",this.select.style.opacity="0",this.select.style.overflow="hidden",this.select.style.pointerEvents="none",this.select.style.margin="0",this.select.style.padding="0",this.select.style.borderWidth="0",this.select.style.clip="rect(0 0 0 0)",this.select.setAttribute("aria-hidden","true"),this.preventNativeSelect||(this.preventNativeSelect=e=>{e.preventDefault(),e.stopPropagation(),e.stopImmediatePropagation()},this.preventNativeSelectMousedown=e=>{e.preventDefault(),e.stopPropagation(),e.stopImmediatePropagation()},this.preventNativeSelectFocus=e=>{e.preventDefault(),e.stopPropagation(),e.stopImmediatePropagation()},this.select.addEventListener("click",this.preventNativeSelect,{capture:!0,passive:!1}),this.select.addEventListener("mousedown",this.preventNativeSelectMousedown,{capture:!0,passive:!1}),this.select.addEventListener("focus",this.preventNativeSelectFocus,{capture:!0,passive:!1}))}showUI(){this.select.removeAttribute("tabindex"),this.select.style.position="",this.select.style.width="",this.select.style.height="",this.select.style.opacity="",this.select.style.overflow="",this.select.style.pointerEvents="",this.select.style.margin="",this.select.style.padding="",this.select.style.borderWidth="",this.select.style.clip="",this.select.removeAttribute("aria-hidden"),this.preventNativeSelect&&(this.select.removeEventListener("click",this.preventNativeSelect,{capture:!0}),this.preventNativeSelect=null),this.preventNativeSelectMousedown&&(this.select.removeEventListener("mousedown",this.preventNativeSelectMousedown,{capture:!0}),this.preventNativeSelectMousedown=null),this.preventNativeSelectFocus&&(this.select.removeEventListener("focus",this.preventNativeSelectFocus,{capture:!0}),this.preventNativeSelectFocus=null)}changeListen(e){this.listen=e,e&&this.observer&&this.observer.observe(this.select,{subtree:!0,childList:!0,attributes:!0}),e||this.observer&&this.observer.disconnect()}valueChange(e){return this.listen&&this.onValueChange&&this.onValueChange(this.getSelectedOptions()),!0}observeCall(e){if(!this.listen)return;let t=!1,s=!1,i=!1,l=!1;for(const n of e){if(n.target===this.select&&(n.attributeName==="disabled"&&(s=!0),n.attributeName==="class"&&(t=!0),n.type==="childList")){for(const a of Array.from(n.addedNodes))if(a.nodeName==="OPTION"&&a.value===this.select.value){l=!0;break}i=!0}(n.target.nodeName==="OPTGROUP"||n.target.nodeName==="OPTION")&&(i=!0)}if(t&&this.onClassChange&&this.onClassChange(this.select.className.split(" ")),s&&this.onDisabledChange&&(this.changeListen(!1),this.onDisabledChange(this.select.disabled),this.changeListen(!0)),i&&this.onOptionsChange){if(this.isUpdating){if(this.select.options.length>0){const n=this.getData();n.length>0&&(this.pendingOptionsChange=n)}l&&this.select.dispatchEvent(new Event("change"));return}this.changeListen(!1),this.onOptionsChange(this.getData()),this.changeListen(!0)}l&&this.select.dispatchEvent(new Event("change"))}getData(){let e=[];const t=this.select.childNodes;for(const s of t)s.nodeName==="OPTGROUP"&&e.push(this.getDataFromOptgroup(s)),s.nodeName==="OPTION"&&e.push(this.getDataFromOption(s));return e}getDataFromOptgroup(e){let t={id:e.id,label:e.label,selectAll:e.dataset?e.dataset.selectall==="true":!1,selectAllText:e.dataset?e.dataset.selectalltext:"Select all",closable:e.dataset?e.dataset.closable:"off",options:[]};const s=e.childNodes;for(const i of s)i.nodeName==="OPTION"&&t.options.push(this.getDataFromOption(i));return t}getDataFromOption(e){return{id:e.id,value:e.value,text:e.text,html:e.dataset&&e.dataset.html?e.dataset.html:"",defaultSelected:e.defaultSelected,selected:e.selected,display:e.style.display!=="none",disabled:e.disabled,mandatory:e.dataset?e.dataset.mandatory==="true":!1,placeholder:e.dataset.placeholder==="true",class:e.className,style:e.style.cssText,data:e.dataset}}getSelectedOptions(){let e=[];const t=this.select.childNodes;for(const s of t){if(s.nodeName==="OPTGROUP"){const i=s.childNodes;for(const l of i)if(l.nodeName==="OPTION"){const n=l;n.selected&&e.push(this.getDataFromOption(n))}}if(s.nodeName==="OPTION"){const i=s;i.selected&&e.push(this.getDataFromOption(i))}}return e}getSelectedValues(){return this.getSelectedOptions().map(e=>e.value)}setSelected(e){this.changeListen(!1);const t=this.select.childNodes;for(const s of t){if(s.nodeName==="OPTGROUP"){const l=s.childNodes;for(const n of l)if(n.nodeName==="OPTION"){const a=n;a.selected=e.includes(a.id)}}if(s.nodeName==="OPTION"){const i=s;i.selected=e.includes(i.id)}}this.changeListen(!0)}setSelectedByValue(e){this.changeListen(!1);const t=this.select.childNodes;for(const s of t){if(s.nodeName==="OPTGROUP"){const l=s.childNodes;for(const n of l)if(n.nodeName==="OPTION"){const a=n;a.selected=e.includes(a.value)}}if(s.nodeName==="OPTION"){const i=s;i.selected=e.includes(i.value)}}this.changeListen(!0)}updateSelect(e,t,s){this.changeListen(!1),e&&(this.select.dataset.id=e),t&&(this.select.style.cssText=t),s&&(this.select.className="",s.forEach(i=>{i.trim()!==""&&this.select.classList.add(i.trim())})),this.changeListen(!0)}updateOptions(e){if(!(!e||e.length===0)){this.isUpdating=!0,this.pendingOptionsChange=null,this.changeListen(!1),this.select.innerHTML="";for(const t of e)t instanceof m&&this.select.appendChild(this.createOptgroup(t)),t instanceof p&&this.select.appendChild(this.createOption(t));if(this.select.dispatchEvent(new Event("change",{bubbles:!0})),this.changeListen(!0),this.isUpdating=!1,this.pendingOptionsChange!==null){const t=this.pendingOptionsChange;t.length>0&&this.onOptionsChange?(this.pendingOptionsChange=null,this.changeListen(!1),this.onOptionsChange(t),this.changeListen(!0)):this.pendingOptionsChange=null}}}createOptgroup(e){const t=document.createElement("optgroup");if(t.id=e.id,t.label=e.label,e.selectAll&&(t.dataset.selectAll="true"),e.closable!=="off"&&(t.dataset.closable=e.closable),e.options)for(const s of e.options)t.appendChild(this.createOption(s));return t}createOption(e){const t=document.createElement("option");return t.id=e.id,t.value=e.value,t.textContent=e.text,e.html!==""&&t.setAttribute("data-html",e.html),t.defaultSelected=e.defaultSelected,t.selected=e.selected,e.disabled&&(t.disabled=!0),e.display||(t.style.display="none"),e.placeholder&&t.setAttribute("data-placeholder","true"),e.mandatory&&t.setAttribute("data-mandatory","true"),e.class&&e.class.split(" ").forEach(s=>{t.classList.add(s)}),e.data&&typeof e.data=="object"&&Object.keys(e.data).forEach(s=>{t.setAttribute("data-"+k(s),e.data[s])}),t}setupLabelHandlers(){const e=[],t=this.select.id;t&&document.querySelectorAll(`label[for="${t}"]`).forEach(n=>e.push(n));let s=this.select.parentElement;for(;s&&s!==document.body;){if(s.tagName==="LABEL"){e.push(s);break}s=s.parentElement}Array.from(new Set(e)).forEach(l=>{if(l.__slimSelectLabelHandler)return;const n=a=>{const o=a.target;a.preventDefault(),o===l&&this.onLabelClick&&this.onLabelClick()};l.__slimSelectLabelHandler=n,l.addEventListener("click",n,{capture:!0,passive:!1})})}removeLabelHandlers(){const e=[],t=this.select.id;t&&document.querySelectorAll(`label[for="${t}"]`).forEach(n=>e.push(n));let s=this.select.parentElement;for(;s&&s!==document.body;){if(s.tagName==="LABEL"){e.push(s);break}s=s.parentElement}Array.from(new Set(e)).forEach(l=>{const n=l.__slimSelectLabelHandler;n&&(l.removeEventListener("click",n,{capture:!0}),delete l.__slimSelectLabelHandler)})}destroy(){this.changeListen(!1),this.select.removeEventListener("change",this.valueChange),this.preventNativeSelect&&(this.select.removeEventListener("click",this.preventNativeSelect,{capture:!0}),this.preventNativeSelect=null),this.preventNativeSelectMousedown&&(this.select.removeEventListener("mousedown",this.preventNativeSelectMousedown,{capture:!0}),this.preventNativeSelectMousedown=null),this.preventNativeSelectFocus&&(this.select.removeEventListener("focus",this.preventNativeSelectFocus,{capture:!0}),this.preventNativeSelectFocus=null),this.observer&&(this.observer.disconnect(),this.observer=null),this.removeLabelHandlers(),delete this.select.dataset.id,this.showUI()}}class E{id="";style="";class=[];isMultiple=!1;isOpen=!1;isFullOpen=!1;intervalMove=null;disabled;alwaysOpen;showSearch;focusSearch;keepSearch;ariaLabel;searchPlaceholder;searchText;searchingText;searchHighlight;closeOnSelect;contentLocation;contentPosition;openPosition;placeholderText;allowDeselect;hideSelected;keepOrder;showOptionTooltips;minSelected;maxSelected;timeoutDelay;maxValuesShown;maxValuesMessage;addableText;constructor(e){e||(e={}),this.id="ss-"+L(),this.style=e.style||"",this.class=e.class||[],this.disabled=e.disabled!==void 0?e.disabled:!1,this.alwaysOpen=e.alwaysOpen!==void 0?e.alwaysOpen:!1,this.showSearch=e.showSearch!==void 0?e.showSearch:!0,this.focusSearch=e.focusSearch!==void 0?e.focusSearch:!0,this.keepSearch=e.keepSearch!==void 0?e.keepSearch:!1,this.ariaLabel=e.ariaLabel||"Combobox",this.searchPlaceholder=e.searchPlaceholder||"Search...",this.searchText=e.searchText||"No Results",this.searchingText=e.searchingText||"Searching...",this.searchHighlight=e.searchHighlight!==void 0?e.searchHighlight:!1,this.closeOnSelect=e.closeOnSelect!==void 0?e.closeOnSelect:!0,this.contentLocation=e.contentLocation||document.body,this.contentPosition=e.contentPosition||"absolute",this.openPosition=e.openPosition||"auto",this.placeholderText=e.placeholderText!==void 0?e.placeholderText:"Select Value",this.allowDeselect=e.allowDeselect!==void 0?e.allowDeselect:!1,this.hideSelected=e.hideSelected!==void 0?e.hideSelected:!1,this.keepOrder=e.keepOrder!==void 0?e.keepOrder:!1,this.showOptionTooltips=e.showOptionTooltips!==void 0?e.showOptionTooltips:!1,this.minSelected=e.minSelected||0,this.maxSelected=e.maxSelected||1e3,this.timeoutDelay=e.timeoutDelay||200,this.maxValuesShown=e.maxValuesShown||20,this.maxValuesMessage=e.maxValuesMessage||"{number} selected",this.addableText=e.addableText||'Press "Enter" to add {value}'}}let V=class{selectEl;settings;cssClasses;select;store;render;openTimeout=null;closeTimeout=null;events={search:void 0,searchFilter:(e,t)=>e.text.toLowerCase().indexOf(t.toLowerCase())!==-1,addable:void 0,beforeChange:void 0,afterChange:void 0,beforeOpen:void 0,afterOpen:void 0,beforeClose:void 0,afterClose:void 0};constructor(e){if(this.selectEl=typeof e.select=="string"?document.querySelector(e.select):e.select,!this.selectEl){e.events&&e.events.error&&e.events.error(new Error("Could not find select element"));return}if(this.selectEl.tagName!=="SELECT"){e.events&&e.events.error&&e.events.error(new Error("Element isnt of type select"));return}this.selectEl.dataset.ssid&&this.destroy(),this.settings=new E(e.settings),this.cssClasses=new T(e.cssClasses);const t=["beforeOpen","afterOpen","beforeClose","afterClose"];for(const a in e.events)e.events.hasOwnProperty(a)&&(t.indexOf(a)!==-1?this.events[a]=C(e.events[a],100):this.events[a]=e.events[a]);this.settings.disabled=e.settings?.disabled?e.settings.disabled:this.selectEl.disabled,this.settings.isMultiple=this.selectEl.multiple,this.settings.style=this.selectEl.style.cssText,this.settings.class=this.selectEl.className.split(" "),this.select=new P(this.selectEl),this.selectEl.id||(this.selectEl.id=this.settings.id),this.select.updateSelect(this.settings.id,this.settings.style,this.settings.class),this.select.hideUI(),this.select.onValueChange=a=>{this.setSelected(a.map(o=>o.id))},this.select.onClassChange=a=>{this.settings.class=a,this.render.updateClassStyles()},this.select.onDisabledChange=a=>{a?this.disable():this.enable()},this.select.onOptionsChange=a=>{this.setData(a||[])},this.select.onLabelClick=()=>{this.settings.disabled||(this.settings.isOpen?this.close():this.open())};const s=e.data?e.data:this.select.getData();this.store=new N(this.settings.isMultiple?"multiple":"single",s),e.data&&this.select.updateOptions(this.store.getData());const i={open:this.open.bind(this),close:this.close.bind(this),addable:this.events.addable?this.events.addable:void 0,setSelected:this.setSelected.bind(this),addOption:this.addOption.bind(this),search:this.search.bind(this),beforeChange:this.events.beforeChange,afterChange:this.events.afterChange};this.render=new M(this.settings,this.cssClasses,this.store,i),this.render.renderValues(),this.render.renderOptions(this.store.getData());const l=this.selectEl.getAttribute("aria-label"),n=this.selectEl.getAttribute("aria-labelledby");l?this.render.main.main.setAttribute("aria-label",l):n&&this.render.main.main.setAttribute("aria-labelledby",n),this.selectEl.parentNode&&this.selectEl.parentNode.insertBefore(this.render.main.main,this.selectEl.nextSibling),window.addEventListener("resize",this.windowResize,!1),this.settings.openPosition==="auto"&&window.addEventListener("scroll",this.windowScroll,!1),document.addEventListener("visibilitychange",this.windowVisibilityChange),this.settings.disabled&&this.disable(),this.settings.alwaysOpen&&this.open(),this.select.setupLabelHandlers(),this.selectEl.slim=this}enable(){this.settings.disabled=!1,this.select.enable(),this.render.enable()}disable(){this.settings.disabled=!0,this.select.disable(),this.render.disable()}getData(){return this.store.getData()}setData(e){const t=this.store.getSelected(),s=this.store.validateDataArray(e);if(s){this.events.error&&this.events.error(s);return}this.store.setData(e);const i=this.store.getData();this.select.updateOptions(i),this.render.renderValues(),this.render.renderOptions(i),this.events.afterChange&&!A(t,this.store.getSelected())&&this.events.afterChange(this.store.getSelectedOptions())}getSelected(){let e=this.store.getSelectedOptions();return this.settings.keepOrder&&(e=this.store.selectedOrderOptions(e)),e.map(t=>t.value)}setSelected(e,t=!0){const s=this.store.getSelected(),i=this.store.getDataOptions();e=Array.isArray(e)?e:[e];const l=[];for(const a of e){if(i.find(o=>o.id==a)){l.push(a);continue}for(const o of i.filter(c=>c.value==a))l.push(o.id)}this.store.setSelectedBy("id",l);const n=this.store.getData();this.select.updateOptions(n),this.render.renderValues(),this.render.content.search.input.value!==""?this.search(this.render.content.search.input.value):this.render.renderOptions(n),t&&this.events.afterChange&&!A(s,this.store.getSelected())&&this.events.afterChange(this.store.getSelectedOptions())}addOption(e){const t=this.store.getSelected();this.store.getDataOptions().some(i=>i.value===(e.value??e.text))||this.store.addOption(e);const s=this.store.getData();this.select.updateOptions(s),this.render.renderValues(),this.render.renderOptions(s),this.events.afterChange&&!A(t,this.store.getSelected())&&this.events.afterChange(this.store.getSelectedOptions())}open(){this.settings.disabled||this.settings.isOpen||(this.events.beforeOpen&&this.events.beforeOpen(),this.render.open(),this.settings.showSearch&&this.settings.focusSearch&&this.render.searchFocus(),this.settings.isOpen=!0,this.openTimeout=setTimeout(()=>{this.events.afterOpen&&this.events.afterOpen(),this.settings.isOpen&&(this.settings.isFullOpen=!0),document.addEventListener("click",this.documentClick)},this.settings.timeoutDelay),this.settings.contentPosition==="absolute"&&(this.settings.intervalMove&&clearInterval(this.settings.intervalMove),this.settings.intervalMove=setInterval(this.render.moveContent.bind(this.render),500)))}close(e=null){!this.settings.isOpen||this.settings.alwaysOpen||(this.events.beforeClose&&this.events.beforeClose(),this.render.close(),!this.settings.keepSearch&&this.render.content.search.input.value!==""&&this.search(""),this.render.mainFocus(e),this.settings.isOpen=!1,this.settings.isFullOpen=!1,this.closeTimeout=setTimeout(()=>{this.events.afterClose&&this.events.afterClose(),document.removeEventListener("click",this.documentClick)},this.settings.timeoutDelay),this.settings.intervalMove&&clearInterval(this.settings.intervalMove))}search(e){if(this.render.content.search.input.value!==e&&(this.render.content.search.input.value=e),e===""){this.render.renderOptions(this.store.getData());return}if(!this.events.search){const s=e===""?this.store.getData():this.store.search(e,this.events.searchFilter);this.render.renderOptions(s);return}this.render.renderSearching();const t=this.events.search(e,this.store.getSelectedOptions());if(t instanceof Promise){t.then(s=>{this.store.setData(s,!0),this.select.updateOptions(this.store.getData()),this.render.renderOptions(this.store.getData())}).catch(s=>{this.render.renderError(typeof s=="string"?s:s.message)});return}else Array.isArray(t)?(this.store.setData(t,!0),this.select.updateOptions(this.store.getData()),this.render.renderOptions(this.store.getData())):this.render.renderError("Search event must return a promise or an array of data")}destroy(){this.openTimeout&&(clearTimeout(this.openTimeout),this.openTimeout=null),this.closeTimeout&&(clearTimeout(this.closeTimeout),this.closeTimeout=null),this.settings.intervalMove&&(clearInterval(this.settings.intervalMove),this.settings.intervalMove=null),document.removeEventListener("click",this.documentClick),window.removeEventListener("resize",this.windowResize,!1),this.settings.openPosition==="auto"&&window.removeEventListener("scroll",this.windowScroll,!1),document.removeEventListener("visibilitychange",this.windowVisibilityChange),this.store.setData([]),this.render.destroy(),this.select.destroy()}windowResize=C(()=>{!this.settings.isOpen&&!this.settings.isFullOpen||this.render.moveContent()});windowScroll=C(()=>{!this.settings.isOpen&&!this.settings.isFullOpen||this.render.moveContent()});documentClick=e=>{this.settings.isOpen&&e.target&&!D(e.target,this.settings.id)&&this.close(e.type)};windowVisibilityChange=()=>{document.hidden&&this.close()}};const B=O.defineComponent({name:"SlimSelect",props:{modelValue:{type:[String,Array,void 0]},multiple:{type:Boolean,default:!1},data:{type:Array},settings:{type:Object},events:{type:Object,default:()=>({})}},emits:["update:modelValue"],data(){return{slim:null}},mounted(){this.$slots.default&&this.$slots.default().length>0&&this.data&&console.warn("[SlimSelect Vue] Both slot content and data prop are provided. Data prop will take precedence and slot content will be ignored.");let e={select:this.$refs.slim};this.data&&(e.data=this.data),this.settings&&(e.settings=this.settings);const t=this.events?.afterChange;e.events={...this.events,afterChange:s=>{this.handleAfterChange(s,t)}},this.slim=new V(e),this.syncModelValueToSlimSelect(!1)},updated(){if(this.slim&&!this.data){const h=this.slim.getSelected(),e=Array.isArray(this.value)?this.value:[this.value];JSON.stringify(h.sort())!==JSON.stringify(e.sort())&&this.syncModelValueToSlimSelect(!1)}},beforeUnmount(){this.slim&&this.slim.destroy()},watch:{modelValue:{handler:function(h){this.slim&&this.syncModelValueToSlimSelect(!1)}},data:{handler:function(h){this.slim&&this.slim.setData(h)},deep:!0}},computed:{value:{get(){return this.getCleanValue(this.$props.modelValue)},set(h){this.$emit("update:modelValue",h)}}},methods:{getSlimSelect(){return this.slim},handleAfterChange(h,e){if(!this.slim)return;const t=this.multiple?h.map(o=>o.value):h.length>0?h[0].value:"",s=this.getCleanValue(this.$props.modelValue),i=this.slim.store.getDataOptions(),l=Array.isArray(s)?s.length>0&&s.every(o=>i.some(c=>c.value===o)):s!==""&&i.some(o=>o.value===s),n=Array.isArray(t)?t.length>0&&t.every(o=>i.some(c=>c.value===o)):t!==""&&i.some(o=>o.value===t);(Array.isArray(t)&&Array.isArray(this.value)?JSON.stringify(t.sort())!==JSON.stringify(this.value.sort()):this.value!==t)&&(l||n)&&(this.value=t),e&&e(h)},getCleanValue(h){const e=this.$props.multiple;return typeof h=="string"?e?[h]:h:Array.isArray(h)?e?h:h[0]:e?[]:""},syncModelValueToSlimSelect(h=!1){if(!this.slim||this.$props.modelValue===void 0)return;const e=this.getCleanValue(this.$props.modelValue),s=this.slim.getData().flatMap(l=>"label"in l?l.options:[l]);if(!(Array.isArray(e)?e.length>0&&e.every(l=>s.some(n=>n.value===l)):e!==""&&s.some(l=>l.value===e))&&!Array.isArray(e)&&!s.some(n=>n.placeholder)){const n=this.slim.getData(),a={value:"",text:"",placeholder:!0};this.slim.setData([a].concat(n))}this.slim.setSelected(e,h)}}}),I=(h,e)=>{const t=h.__vccOpts||h;for(const[s,i]of e)t[s]=i;return t},H=["multiple"];function F(h,e,t,s,i,l){return O.openBlock(),O.createElementBlock("select",{multiple:h.multiple,ref:"slim"},[O.renderSlot(h.$slots,"default")],8,H)}const _=I(B,[["render",F]]);v.Optgroup=m,v.Option=p,v.Settings=E,v.default=_,Object.defineProperties(v,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "slim-select",
|
|
3
3
|
"description": "Slim advanced select dropdown",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Brian Voelker <brian@webiswhatido.com> (http://webiswhatido.com)",
|
|
7
7
|
"homepage": "https://slimselectjs.com",
|
|
@@ -72,13 +72,13 @@
|
|
|
72
72
|
"@testing-library/user-event": "^14.6.1",
|
|
73
73
|
"@types/downloadjs": "^1.4.6",
|
|
74
74
|
"@types/node": "^24.10.1",
|
|
75
|
-
"@types/react": "^19.2.
|
|
75
|
+
"@types/react": "^19.2.6",
|
|
76
76
|
"@types/react-dom": "^19.2.3",
|
|
77
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
78
|
-
"@typescript-eslint/parser": "^8.
|
|
77
|
+
"@typescript-eslint/eslint-plugin": "^8.47.0",
|
|
78
|
+
"@typescript-eslint/parser": "^8.47.0",
|
|
79
79
|
"@vitejs/plugin-react": "^5.1.1",
|
|
80
|
-
"@vitejs/plugin-vue": "^6.0.
|
|
81
|
-
"@vitest/coverage-v8": "^4.0.
|
|
80
|
+
"@vitejs/plugin-vue": "^6.0.2",
|
|
81
|
+
"@vitest/coverage-v8": "^4.0.10",
|
|
82
82
|
"@vue/test-utils": "^2.4.6",
|
|
83
83
|
"axe-core": "^4.11.0",
|
|
84
84
|
"clipboard": "^2.0.11",
|
|
@@ -92,18 +92,18 @@
|
|
|
92
92
|
"prettier": "^3.6.2",
|
|
93
93
|
"react": "^19.2.0",
|
|
94
94
|
"react-dom": "^19.2.0",
|
|
95
|
-
"rimraf": "^6.1.
|
|
96
|
-
"sass": "^1.94.
|
|
95
|
+
"rimraf": "^6.1.2",
|
|
96
|
+
"sass": "^1.94.2",
|
|
97
97
|
"shiki": "^3.15.0",
|
|
98
98
|
"terser": "^5.44.1",
|
|
99
99
|
"tslib": "^2.8.1",
|
|
100
100
|
"typescript": "^5.9.3",
|
|
101
101
|
"vite": "^7.2.2",
|
|
102
102
|
"vite-plugin-dts": "^4.5.4",
|
|
103
|
-
"vitest": "^4.0.
|
|
103
|
+
"vitest": "^4.0.10",
|
|
104
104
|
"vue": "^3.5.24",
|
|
105
105
|
"vue-router": "^4.6.3",
|
|
106
|
-
"vue-tsc": "^3.1.
|
|
106
|
+
"vue-tsc": "^3.1.4",
|
|
107
107
|
"vuex": "^4.0.2"
|
|
108
108
|
},
|
|
109
109
|
"peerDependencies": {
|
|
@@ -503,6 +503,41 @@ describe('SlimSelect Module', () => {
|
|
|
503
503
|
expect(slim.render.content.search.input.value).toBe('')
|
|
504
504
|
})
|
|
505
505
|
|
|
506
|
+
test('should keep search input when keepSearch is true', async () => {
|
|
507
|
+
// Create new instance with keepSearch enabled
|
|
508
|
+
const select = document.createElement('select')
|
|
509
|
+
select.innerHTML = `
|
|
510
|
+
<option value="a">A</option>
|
|
511
|
+
<option value="b">B</option>
|
|
512
|
+
<option value="c">C</option>
|
|
513
|
+
`
|
|
514
|
+
document.body.appendChild(select)
|
|
515
|
+
|
|
516
|
+
const slimWithKeepSearch = new SlimSelect({
|
|
517
|
+
select: select,
|
|
518
|
+
settings: {
|
|
519
|
+
keepSearch: true
|
|
520
|
+
}
|
|
521
|
+
})
|
|
522
|
+
|
|
523
|
+
// Open dropdown
|
|
524
|
+
slimWithKeepSearch.open()
|
|
525
|
+
|
|
526
|
+
// Search for something
|
|
527
|
+
slimWithKeepSearch.search('test')
|
|
528
|
+
expect(slimWithKeepSearch.render.content.search.input.value).toBe('test')
|
|
529
|
+
|
|
530
|
+
// Close dropdown
|
|
531
|
+
slimWithKeepSearch.close()
|
|
532
|
+
|
|
533
|
+
// Search input should NOT be cleared when keepSearch is true
|
|
534
|
+
expect(slimWithKeepSearch.render.content.search.input.value).toBe('test')
|
|
535
|
+
|
|
536
|
+
// Cleanup
|
|
537
|
+
slimWithKeepSearch.destroy()
|
|
538
|
+
document.body.removeChild(select)
|
|
539
|
+
})
|
|
540
|
+
|
|
506
541
|
test('should persist search results in store across close/open cycles', () => {
|
|
507
542
|
// Search for something
|
|
508
543
|
slim.search('te')
|
|
@@ -1213,5 +1248,56 @@ describe('SlimSelect Module', () => {
|
|
|
1213
1248
|
|
|
1214
1249
|
slim.destroy()
|
|
1215
1250
|
})
|
|
1251
|
+
|
|
1252
|
+
test('should handle option change after first render', async () => {
|
|
1253
|
+
// Test scenario: start with 2 options
|
|
1254
|
+
document.body.innerHTML = `
|
|
1255
|
+
<select id="test-select">
|
|
1256
|
+
<option value="opt1">Option 1</option>
|
|
1257
|
+
<option value="opt2">Option 2</option>
|
|
1258
|
+
</select>
|
|
1259
|
+
`
|
|
1260
|
+
|
|
1261
|
+
const selectElement = document.getElementById('test-select') as HTMLSelectElement
|
|
1262
|
+
const slim = new SlimSelect({
|
|
1263
|
+
select: selectElement
|
|
1264
|
+
})
|
|
1265
|
+
|
|
1266
|
+
// Set an initial value
|
|
1267
|
+
selectElement.value = 'opt2'
|
|
1268
|
+
|
|
1269
|
+
// Verify initial state
|
|
1270
|
+
expect(selectElement.options.length).toBe(2)
|
|
1271
|
+
expect(selectElement.value).toBe('opt2')
|
|
1272
|
+
expect(slim.getData().length).toBe(2)
|
|
1273
|
+
|
|
1274
|
+
// Wait for all mutations to process
|
|
1275
|
+
await new Promise((r) => setTimeout(r, 200))
|
|
1276
|
+
|
|
1277
|
+
// Rebuild options
|
|
1278
|
+
document.getElementById('test-select')!.innerHTML = `
|
|
1279
|
+
<option value="opt1">Option 1 updated</option>
|
|
1280
|
+
<option value="opt2" selected>Option 2 updated</option>
|
|
1281
|
+
`;
|
|
1282
|
+
|
|
1283
|
+
// Wait for all mutations to process
|
|
1284
|
+
await new Promise((r) => setTimeout(r, 200))
|
|
1285
|
+
|
|
1286
|
+
// Verify options are still there
|
|
1287
|
+
expect(selectElement.options.length).toBe(2)
|
|
1288
|
+
expect(selectElement.options[0].textContent).toBe("Option 1 updated")
|
|
1289
|
+
expect(selectElement.options[1].textContent).toBe("Option 2 updated")
|
|
1290
|
+
expect(selectElement.value).toBe('opt2')
|
|
1291
|
+
|
|
1292
|
+
// Verify SlimSelect also has the options
|
|
1293
|
+
const data = slim.getData()
|
|
1294
|
+
expect(data.length).toBe(2)
|
|
1295
|
+
|
|
1296
|
+
// Verify selected value is empty
|
|
1297
|
+
const selected = slim.getSelected()
|
|
1298
|
+
expect(selected).toEqual(["opt2"])
|
|
1299
|
+
|
|
1300
|
+
slim.destroy()
|
|
1301
|
+
})
|
|
1216
1302
|
})
|
|
1217
1303
|
})
|
package/src/slim-select/index.ts
CHANGED
|
@@ -419,8 +419,8 @@ export default class SlimSelect {
|
|
|
419
419
|
// Tell render to close
|
|
420
420
|
this.render.close()
|
|
421
421
|
|
|
422
|
-
// Clear search only if not empty
|
|
423
|
-
if (this.render.content.search.input.value !== '') {
|
|
422
|
+
// Clear search only if not empty and keepSearch is false
|
|
423
|
+
if (!this.settings.keepSearch && this.render.content.search.input.value !== '') {
|
|
424
424
|
this.search('') // Clear search
|
|
425
425
|
}
|
|
426
426
|
|
|
@@ -157,10 +157,28 @@ export default class Render {
|
|
|
157
157
|
this.main.main.removeAttribute('aria-activedescendant')
|
|
158
158
|
|
|
159
159
|
// Remove direction class from main and content after animation is complete
|
|
160
|
+
const animationTiming = this.getAnimationTiming()
|
|
160
161
|
setTimeout(() => {
|
|
161
162
|
this.main.main.classList.remove(this.classes.dirAbove, this.classes.dirBelow)
|
|
162
163
|
this.content.main.classList.remove(this.classes.dirAbove, this.classes.dirBelow)
|
|
163
|
-
},
|
|
164
|
+
}, animationTiming)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private getAnimationTiming(): number {
|
|
168
|
+
const computedStyle = getComputedStyle(this.content.main)
|
|
169
|
+
const cssValue = computedStyle.getPropertyValue('--ss-animation-timing').trim()
|
|
170
|
+
|
|
171
|
+
if (cssValue) {
|
|
172
|
+
// Parse CSS time value (e.g., "0.2s" or "200ms")
|
|
173
|
+
if (cssValue.endsWith('ms')) {
|
|
174
|
+
return parseFloat(cssValue)
|
|
175
|
+
} else if (cssValue.endsWith('s')) {
|
|
176
|
+
return parseFloat(cssValue) * 1000
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Fall back to default 200ms
|
|
181
|
+
return 200
|
|
164
182
|
}
|
|
165
183
|
|
|
166
184
|
public updateClassStyles(): void {
|
|
@@ -881,7 +899,7 @@ export default class Render {
|
|
|
881
899
|
}
|
|
882
900
|
|
|
883
901
|
public searchFocus(): void {
|
|
884
|
-
this.content.search.input.focus()
|
|
902
|
+
this.content.search.input.focus({ preventScroll: true })
|
|
885
903
|
}
|
|
886
904
|
|
|
887
905
|
public getOptions(notPlaceholder = false, notDisabled = false, notHidden = false): HTMLDivElement[] {
|
|
@@ -167,6 +167,7 @@ export default class Select {
|
|
|
167
167
|
let classChanged = false
|
|
168
168
|
let disabledChanged = false
|
|
169
169
|
let optgroupOptionChanged = false
|
|
170
|
+
let selectionChanged = false;
|
|
170
171
|
|
|
171
172
|
// Loop through mutations and check various things
|
|
172
173
|
for (const m of mutations) {
|
|
@@ -186,7 +187,7 @@ export default class Select {
|
|
|
186
187
|
for (const n of Array.from(m.addedNodes)) {
|
|
187
188
|
if (n.nodeName === 'OPTION' && (<HTMLOptionElement>n).value === this.select.value) {
|
|
188
189
|
// we added a new option that's now the select value
|
|
189
|
-
|
|
190
|
+
selectionChanged = true;
|
|
190
191
|
break
|
|
191
192
|
}
|
|
192
193
|
}
|
|
@@ -227,6 +228,9 @@ export default class Select {
|
|
|
227
228
|
this.pendingOptionsChange = currentData
|
|
228
229
|
}
|
|
229
230
|
}
|
|
231
|
+
if(selectionChanged) {
|
|
232
|
+
this.select.dispatchEvent(new Event('change'))
|
|
233
|
+
}
|
|
230
234
|
return
|
|
231
235
|
}
|
|
232
236
|
|
|
@@ -234,6 +238,10 @@ export default class Select {
|
|
|
234
238
|
this.onOptionsChange(this.getData())
|
|
235
239
|
this.changeListen(true)
|
|
236
240
|
}
|
|
241
|
+
|
|
242
|
+
if(selectionChanged) {
|
|
243
|
+
this.select.dispatchEvent(new Event('change'))
|
|
244
|
+
}
|
|
237
245
|
}
|
|
238
246
|
|
|
239
247
|
// From the select element pull optgroup and options into data
|
|
@@ -558,20 +566,15 @@ export default class Select {
|
|
|
558
566
|
const labelClickHandler = (e: MouseEvent) => {
|
|
559
567
|
const target = e.target as HTMLElement
|
|
560
568
|
|
|
561
|
-
// Only continue if the target is the label
|
|
562
|
-
if (target !== label) {
|
|
563
|
-
return
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
// At this point, we know the click is directly on the label element
|
|
567
569
|
// Prevent default label behavior (focusing the select)
|
|
568
|
-
//
|
|
570
|
+
// This needs to happen for all clicks on the label or its children
|
|
571
|
+
// to prevent the browser from focusing the hidden select
|
|
569
572
|
e.preventDefault()
|
|
570
573
|
// e.stopPropagation() // dont stop propagation
|
|
571
574
|
|
|
572
|
-
//
|
|
573
|
-
if (this.onLabelClick) {
|
|
574
|
-
this.onLabelClick
|
|
575
|
+
// Only handle the click if it's directly on the label
|
|
576
|
+
if (target === label && this.onLabelClick) {
|
|
577
|
+
this.onLabelClick()
|
|
575
578
|
}
|
|
576
579
|
}
|
|
577
580
|
|
|
@@ -16,6 +16,7 @@ export default class Settings {
|
|
|
16
16
|
public alwaysOpen: boolean
|
|
17
17
|
public showSearch: boolean
|
|
18
18
|
public focusSearch: boolean
|
|
19
|
+
public keepSearch: boolean
|
|
19
20
|
public ariaLabel: string
|
|
20
21
|
public searchPlaceholder: string
|
|
21
22
|
public searchText: string
|
|
@@ -50,6 +51,7 @@ export default class Settings {
|
|
|
50
51
|
this.alwaysOpen = settings.alwaysOpen !== undefined ? settings.alwaysOpen : false
|
|
51
52
|
this.showSearch = settings.showSearch !== undefined ? settings.showSearch : true
|
|
52
53
|
this.focusSearch = settings.focusSearch !== undefined ? settings.focusSearch : true
|
|
54
|
+
this.keepSearch = settings.keepSearch !== undefined ? settings.keepSearch : false
|
|
53
55
|
this.ariaLabel = settings.ariaLabel || 'Combobox'
|
|
54
56
|
this.searchPlaceholder = settings.searchPlaceholder || 'Search...'
|
|
55
57
|
this.searchText = settings.searchText || 'No Results'
|