| 1 |
import{j as r}from"./jsx-runtime.js?ver=1.6.4";import"./index6.js?ver=1.6.4";import{b as T}from"./BlockEditorTranslator.js?ver=1.6.4";const{useState:u,useRef:h,useEffect:E}=wp.element;function D({options:k=[],defaultValue:j=[],onChange:i,customKeys:t={key:"value",label:"label"},isMulti:c=!1}){const[b,g]=u(!1),[m,w]=u(""),s=h(null),v=h(null),x=h(null),d=h(null),[L,$]=u("auto");E(()=>{p();const e=n=>{x.current&&!x.current.contains(n.target)&&g(!1)};return document.addEventListener("mousedown",e),()=>{document.removeEventListener("mousedown",e)}},[]);const[l,o]=u(c?k.filter(e=>j.includes(e[t.key])):k.find(e=>e[t.key]===j)||null),y=k.filter(e=>e[t.label].toLowerCase().includes(m.toLowerCase())),O=e=>l?c?l.some(n=>n[t.key]===e[t.key]):l[t.key]===e[t.key]:!1,R=e=>{var n;if(g(!1),(n=s.current)==null||n.blur(),c){const N=l.some(a=>a[t.key]===e[t.key])?l.filter(a=>a[t.key]!==e[t.key]):[...l,e];o(N),i(N.map(a=>a[t.key]))}else o(e),i(e[t.key]);w(""),setTimeout(()=>{p()},0)},p=()=>{if(!c||!d.current)return;const{height:e,width:n}=d.current.getBoundingClientRect();d.current&&(n<80?(s.current.style.paddingLeft=`${n+5}px`,v.current.style.paddingTop="0",s.current.style.paddingTop="4px",s.current.style.height="42px"):s.current.style.paddingLeft="4px",e>=24&&n>80&&(s.current.style.paddingTop=`${e+5}px`,s.current.style.height=`${e+30}px`))},C=e=>{if(!c)return;const n=l.filter(f=>f[t.key]!==e[t.key]);o(n),i(n.map(f=>f[t.key])),setTimeout(()=>{p()},0)},S=e=>{c&&e.key==="Backspace"&&m===""&&l.length&&C(l[l.length-1])};return r.jsxs("div",{className:"fct-custom-select-wrap",ref:x,children:[r.jsxs("div",{className:"fct-custom-select-input-wrap",onClick:()=>{var e;g(n=>!n),b?(e=s.current)==null||e.blur():s.current.focus()},ref:v,style:{height:L},children:[r.jsx("div",{className:"fct-custom-select-selected-items "+(c?" multi":"single"),ref:d,children:c?l.map(e=>r.jsxs("span",{children:[e[t.label],r.jsx("button",{onClick:n=>{n.stopPropagation(),C(e)},children:"×"})]},e[t.key])):l&&r.jsxs("span",{className:"fct-custom-select-selected-single-item",onClick:()=>{},children:[l[t.label],r.jsx("button",{onClick:e=>{e.stopPropagation(),o(null),i(null),setTimeout(()=>{p()},0)},children:"×"})]},l[t.key])}),r.jsx("input",{ref:s,value:m,onChange:e=>w(e.target.value),onKeyDown:S,onClick:()=>{},placeholder:c&&l.length?"":T("Select...")})]}),b&&r.jsx("ul",{className:"fct-custom-select-result-list",children:y.length>0?y.map(e=>r.jsx("li",{onClick:()=>R(e),className:`rs-option ${O(e)?"selected":""}`,children:e[t.label]},e[t.key])):r.jsx("li",{className:"empty-options",children:T("No options found")})})]})}export{D as C}; |