2 lines
2.4 KiB
JavaScript
2 lines
2.4 KiB
JavaScript
import{r as s,j as t}from"./chunk-UIGDSWPH-BhuNDbxn.js";import{a5 as $}from"./main-af3pcbZa.js";const z=({options:f,placeholder:b="Select an option",onChange:h,className:y="",defaultValue:l="",value:g,disabled:c=!1})=>{const o=g!==void 0,[n,a]=s.useState(!1),[k,m]=s.useState(l),p=o?g||"":k,d=s.useRef(null),i=s.useRef(null);s.useEffect(()=>{o||m(l)},[l,o]);const w=String(p||""),u=f.find(e=>String(e.value||"")===w),v=u?u.label:b,S=!u;s.useEffect(()=>{const e=r=>{d.current&&!d.current.contains(r.target)&&i.current&&!i.current.contains(r.target)&&a(!1)};if(n)return document.addEventListener("mousedown",e),()=>{document.removeEventListener("mousedown",e)}},[n]);const j=e=>{const r=e==null?"":String(e);o||m(r),h(r),a(!1)},N=e=>{e.key==="Escape"?a(!1):(e.key==="Enter"||e.key===" ")&&(e.preventDefault(),a(!n))};return t.jsxs("div",{className:`relative ${y}`,children:[t.jsxs("button",{ref:i,type:"button",onClick:()=>!c&&a(!n),disabled:c,onKeyDown:N,className:`igny8-select-styled h-9 w-full appearance-none rounded-lg border border-gray-300 bg-transparent px-3 py-2 pr-10 text-sm shadow-theme-xs focus:border-brand-300 focus:outline-hidden focus:ring-3 focus:ring-brand-500/10 dark:border-gray-700 dark:bg-gray-900 dark:focus:border-brand-800 ${S?"text-gray-400 dark:text-gray-400":"text-gray-800 dark:text-white/90"} ${n?"border-brand-300 ring-3 ring-brand-500/10 dark:border-brand-800":""} ${c?"opacity-50 cursor-not-allowed":""}`,children:[t.jsx("span",{className:"block text-left truncate",children:v}),t.jsx("span",{className:"absolute inset-y-0 right-0 flex items-center pr-3 pointer-events-none",children:t.jsx($,{className:`h-4 w-4 text-gray-400 transition-transform ${n?"transform rotate-180":""}`})})]}),n&&t.jsx("div",{ref:d,className:"absolute z-50 left-0 right-0 mt-1 rounded-lg border border-gray-200 bg-white shadow-theme-lg dark:border-gray-800 dark:bg-gray-dark overflow-hidden max-h-60 overflow-y-auto",children:t.jsx("div",{className:"py-1",children:f.map(e=>{const r=String(e.value||""),E=String(p||"")===r;return t.jsxs("button",{type:"button",onClick:x=>{x.preventDefault(),x.stopPropagation(),j(r)},className:`w-full text-left px-3 py-2 text-sm transition-colors flex items-center gap-2 ${E?"bg-brand-500 text-white":"text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-800"}`,children:[e.icon&&t.jsx("span",{className:"flex-shrink-0",children:e.icon}),t.jsx("span",{children:e.label})]},`option-${e.value||"empty"}-${e.label}`)})})})]})};export{z as S};
|