1 line
4.8 KiB
JavaScript
1 line
4.8 KiB
JavaScript
import{j as e,a as b}from"./vendor-react-YMhRUmcb.js";import{D as O}from"./dropdown-DLDDI8f2.js";import{B as I,d as N}from"./index-D2uzyr9g.js";import{X as L,c as R,i as C}from"./vendor-icons-DINCNwpk.js";import{I as B}from"./input-DL1WY4HG.js";function P({displayValue:t,placeholder:r,value:n,multiple:c,disabled:l,open:s,className:h,ariaLabel:d,ariaLabelledBy:m,onClear:x}){const u=n&&(Array.isArray(n)&&n.length>0||!Array.isArray(n)),g=c?!u:!t;return e.jsxs(I,{variant:"outline",disabled:l,className:N("w-full justify-between",g?"text-muted-foreground":"",h),type:"button","aria-label":d,"aria-labelledby":m,"aria-haspopup":"listbox","aria-expanded":s,children:[e.jsx("span",{className:"truncate",children:g?r:t}),e.jsxs("div",{className:"flex items-center gap-1 ml-2",children:[u&&e.jsx(L,{className:"h-4 w-4 shrink-0 opacity-50 hover:opacity-100",onClick:x}),e.jsx(R,{className:"h-4 w-4 shrink-0 opacity-50"})]})]})}function A({option:t,isSelected:r,multiple:n,onSelect:c}){const l=s=>{t.disabled||(s.key==="Enter"||s.key===" ")&&(s.preventDefault(),c(t.value))};return e.jsxs("div",{role:"option","aria-selected":r,className:N("relative flex items-center px-4 py-2 text-sm cursor-pointer","hover:bg-muted hover:text-foreground","focus:bg-muted focus:text-foreground","transition-colors text-foreground",r&&"bg-primary/10 text-primary",t.disabled&&"opacity-50 cursor-not-allowed pointer-events-none"),onClick:()=>!t.disabled&&c(t.value),onKeyDown:l,tabIndex:t.disabled?-1:0,children:[n&&e.jsx("div",{className:N("mr-2 flex h-4 w-4 items-center justify-center rounded-sm border border-border",r&&"bg-primary border-border text-primary-foreground"),children:r&&e.jsx(C,{className:"h-3 w-3"})}),e.jsx("span",{className:"flex-1",children:t.label}),!n&&r&&e.jsx(C,{className:"h-4 w-4 text-primary"})]})}function K({searchable:t,search:r,onSearchChange:n,searchInputRef:c,filteredOptions:l,multiple:s,isSelected:h,onSelect:d,ariaLabel:m,name:x,placeholder:u}){const g=l.ungrouped.length>0||Object.keys(l.groups).length>0;return e.jsxs("div",{className:"w-full min-w-48 max-h-72 overflow-y-auto",role:"listbox","aria-label":m||x||u,children:[t&&e.jsx("div",{className:"p-2 border-b",children:e.jsx(B,{ref:c,type:"text",placeholder:"Search...",value:r,onChange:o=>n(o.target.value),onClick:o=>o.stopPropagation(),className:"w-full"})}),l.ungrouped.length>0&&e.jsx("div",{className:"py-1",children:l.ungrouped.map(o=>e.jsx(A,{option:o,isSelected:h(o.value),multiple:s,onSelect:d},o.value))}),Object.entries(l.groups).map(([o,p])=>e.jsxs("div",{className:"py-1",children:[e.jsx("div",{className:"px-4 py-1.5 text-xs font-semibold text-muted-foreground uppercase",children:o}),p.map(y=>e.jsx(A,{option:y,isSelected:h(y.value),multiple:s,onSelect:d},y.value))]},o)),!g&&e.jsx("div",{className:"px-4 py-2 text-sm text-muted-foreground text-center",children:"No options found"})]})}function M({options:t,value:r,onChange:n,multiple:c=!1,searchable:l=!1}){const[s,h]=b.useState(!1),[d,m]=b.useState(""),x=b.useRef(null),u=b.useMemo(()=>{const a={},f=[];return t.forEach(i=>{i.group?(a[i.group]||(a[i.group]=[]),a[i.group].push(i)):f.push(i)}),{groups:a,ungrouped:f}},[t]),g=b.useMemo(()=>{if(!l||!d)return u;const a=d.toLowerCase(),f={};Object.entries(u.groups).forEach(([j,D])=>{const S=D.filter(E=>E.label.toLowerCase().includes(a));S.length>0&&(f[j]=S)});const i=u.ungrouped.filter(j=>j.label.toLowerCase().includes(a));return{groups:f,ungrouped:i}},[l,d,u]),p=r?(Array.isArray(r)?r:[r]).map(f=>t.find(i=>i.value===f)?.label).filter(Boolean):[],y=c?p.length>0?`${p.length} selected`:"":p[0]||"",w=a=>r?c?Array.isArray(r)&&r.includes(a):r===a:!1,v=a=>{if(c){const f=Array.isArray(r)?r:[],i=f.includes(a)?f.filter(j=>j!==a):[...f,a];n(i)}else n(a),h(!1),m("")},k=a=>{a.stopPropagation(),n(c?[]:"")};return b.useEffect(()=>{s&&l&&x.current&&x.current.focus()},[s,l]),b.useEffect(()=>{s||m("")},[s]),{open:s,setOpen:h,search:d,setSearch:m,searchInputRef:x,groupedOptions:u,filteredOptions:g,displayValue:y,selectedLabels:p,isSelected:w,handleSelect:v,handleClear:k}}function $({placeholder:t="Select an option...",name:r,"aria-label":n,"aria-labelledby":c,className:l,...s}){const{open:h,setOpen:d,search:m,setSearch:x,searchInputRef:u,filteredOptions:g,displayValue:o,isSelected:p,handleSelect:y,handleClear:w}=M({...s}),v=e.jsx(P,{displayValue:o,placeholder:t,value:s.value,multiple:s.multiple??!1,disabled:s.disabled??!1,open:h,className:l,ariaLabel:n,ariaLabelledBy:c,onClear:w});return e.jsxs(e.Fragment,{children:[e.jsx(O,{trigger:v,align:"left",onOpenChange:d,className:"w-full",children:e.jsx(K,{searchable:s.searchable??!1,search:m,onSearchChange:x,searchInputRef:u,filteredOptions:g,multiple:s.multiple??!1,isSelected:p,onSelect:y,ariaLabel:n,name:r,placeholder:t})}),r&&e.jsx("input",{type:"hidden",name:r,value:Array.isArray(s.value)?s.value.join(","):s.value??""})]})}export{$ as S};
|