the-omelet-ui 1.4.1 → 1.4.4
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.
|
@@ -141,6 +141,10 @@ interface SearchInputProps {
|
|
|
141
141
|
* แสดง hint text ด้านล่าง (เช่น keyboard shortcuts)
|
|
142
142
|
*/
|
|
143
143
|
hintText?: string;
|
|
144
|
+
/**
|
|
145
|
+
* Auto-select option แรกเมื่อมีผลลัพธ์ (default: false)
|
|
146
|
+
*/
|
|
147
|
+
autoSelectFirst?: boolean;
|
|
144
148
|
}
|
|
145
149
|
declare const SearchInput: React__default.ForwardRefExoticComponent<SearchInputProps & React__default.RefAttributes<SearchInputRef>>;
|
|
146
150
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import {a}from'../chunk-6GLPXMGB.js';import {forwardRef,useState,useRef,useImperativeHandle,useCallback,useEffect}from'react';import*as
|
|
1
|
+
import {a}from'../chunk-6GLPXMGB.js';import {memo,forwardRef,useState,useRef,useImperativeHandle,useCallback,useMemo,useEffect}from'react';import*as h from'@radix-ui/react-popover';import {jsx,jsxs}from'react/jsx-runtime';var Y=memo(()=>jsx("svg",{width:"20",height:"20",viewBox:"0 0 20 20",fill:"none",className:"text-gray-400",children:jsx("path",{d:"M9 17A8 8 0 1 0 9 1a8 8 0 0 0 0 16zM18.5 18.5l-4-4",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})}));Y.displayName="SearchIcon";var Z=memo(()=>jsx("svg",{width:"16",height:"16",viewBox:"0 0 16 16",fill:"none",children:jsx("path",{d:"M12 4L4 12M4 4l8 8",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})}));Z.displayName="ClearIcon";var _=forwardRef(({placeholder:u="\u0E04\u0E49\u0E19\u0E2B\u0E32...",defaultValue:O="",value:r,onChange:a$1,onSelect:m,onSubmit:s,options:p=[],onSearch:i,debounceMs:g=300,isLoading:x=false,disabled:P=false,className:L,inputClassName:ee,popoverClassName:te,optionClassName:ne,clearButtonClassName:re,leftIconClassName:ae,showClearButton:H=true,showSearchIcon:T=true,leftIcon:E,emptyMessage:oe="\u0E44\u0E21\u0E48\u0E1E\u0E1A\u0E1C\u0E25\u0E01\u0E32\u0E23\u0E04\u0E49\u0E19\u0E2B\u0E32",loadingMessage:se="\u0E01\u0E33\u0E25\u0E31\u0E07\u0E04\u0E49\u0E19\u0E2B\u0E32...",maxResults:M=10,renderOption:ie,minSearchLength:w=0,hintText:Q,autoSelectFirst:N=false},le)=>{let[ce,A]=useState(O),[j,c]=useState(false),[o,C]=useState([]),[de,U]=useState(false),[I,$]=useState(N?0:-1),f=useRef(null),k=useRef(void 0),l=r!==void 0?r:ce,z=x||de;useImperativeHandle(le,()=>({focus:()=>{f.current?.focus();},blur:()=>{f.current?.blur();},clear:()=>{J();},getInputElement:()=>f.current}),[]);let F=useCallback(e=>{if(!e||e.length<w)return [];let t=e.toLowerCase();return p.filter(v=>v.label.toLowerCase().includes(t)||v.value.toLowerCase().includes(t)||v.category?.toLowerCase().includes(t)).slice(0,M)},[p,M,w]),G=useCallback(async e=>{if(!e||e.length<w){C([]),c(false);return}if(!i){let t=F(e);C(t),c(t.length>0);return}k.current&&window.clearTimeout(k.current),k.current=window.setTimeout(async()=>{U(true);try{let t=await i(e);C(t.slice(0,M)),c(t.length>0);}catch{C([]);}finally{U(false);}},g);},[i,F,g,M,w]),ue=useCallback(e=>{let t=e.target.value;r===void 0&&A(t),a$1?.(t),G(t),$(N?0:-1);},[r,a$1,G,N]),K=useCallback(e=>{let t=e.label;r===void 0&&A(t),a$1?.(t),m?.(e),c(false),f.current?.blur();},[r,a$1,m]),J=useCallback(()=>{r===void 0&&A(""),a$1?.(""),C([]),c(false),f.current?.focus();},[r,a$1]),B=useCallback(e=>{s?.(e),c(false),f.current?.blur();},[s]),pe=useCallback(e=>{if(!j||o.length===0){e.key==="Enter"&&(e.preventDefault(),s&&B(l));return}switch(e.key){case "ArrowDown":e.preventDefault(),$(t=>t===-1?0:t<o.length-1?t+1:t);break;case "ArrowUp":e.preventDefault(),$(t=>t<=0?N?0:-1:t-1);break;case "Enter":e.preventDefault(),I>=0&&o[I]?K(o[I]):s&&B(l);break;case "Escape":e.preventDefault(),c(false),f.current?.blur();break}},[j,o,I,N,s,l,K,B]),S=useMemo(()=>o.reduce((e,t)=>{let v=t.category||"default";return e[v]||(e[v]=[]),e[v].push(t),e},{}),[o]),ge=useMemo(()=>Object.keys(S).length>1||Object.keys(S).length===1&&!S.default,[S]);useEffect(()=>()=>{k.current&&window.clearTimeout(k.current);},[]);let fe=useMemo(()=>({paddingLeft:T||E?"3.5rem":"1.5rem",paddingRight:H&&l?"3.5rem":"1.5rem"}),[T,E,H,l]);return jsxs("div",{className:"w-full",children:[jsxs(h.Root,{open:j,onOpenChange:c,children:[jsx(h.Anchor,{asChild:true,children:jsxs("div",{className:a("relative",L),children:[(T||E)&&jsx("div",{className:a("absolute top-1/2 -translate-y-1/2 pointer-events-none",ae),style:{left:"1rem"},children:E||jsx(Y,{})}),jsx("input",{ref:f,type:"text",value:l,onChange:ue,onKeyDown:pe,onFocus:useCallback(()=>{l&&o.length>0&&c(true);},[l,o.length]),placeholder:u,disabled:P,style:fe,className:a("w-full h-14 rounded-full border border-gray-300","text-base","focus:outline-none focus:border-gray-900 focus:ring-2 focus:ring-gray-100","disabled:bg-gray-50 disabled:cursor-not-allowed","transition-all",ee)}),H&&l&&!P&&jsx("button",{type:"button",onClick:J,className:a("absolute top-1/2 -translate-y-1/2","w-8 h-8 rounded-full","flex items-center justify-center","bg-gray-200 hover:bg-gray-300","transition-colors",re),style:{right:"1rem"},"aria-label":"Clear search",children:jsx(Z,{})})]})}),jsx(h.Portal,{children:jsxs(h.Content,{side:"bottom",align:"start",sideOffset:8,className:a("w-[var(--radix-popover-trigger-width)]","bg-white rounded-2xl shadow-lg border border-gray-200","max-h-[400px] overflow-y-auto","z-50","animate-in fade-in-0 zoom-in-95",te),onOpenAutoFocus:e=>e.preventDefault(),children:[z&&jsx("div",{className:"p-4 text-center text-gray-500 text-sm",children:jsxs("div",{className:"flex items-center justify-center gap-2",children:[jsx("div",{className:"w-4 h-4 border-2 border-gray-300 border-t-gray-600 rounded-full animate-spin"}),jsx("span",{children:se})]})}),!z&&o.length===0&&l.length>=w&&jsx("div",{className:"p-4 text-center text-gray-500 text-sm",children:oe}),!z&&o.length>0&&jsx(q,{hasCategories:ge,groupedOptions:S,filteredOptions:o,highlightedIndex:I,renderOption:ie,optionClassName:ne,onSelect:K})]})})]}),Q&&jsx("div",{className:"mt-3 text-center text-sm text-gray-400",children:Q})]})});_.displayName="SearchInput";var q=memo(({hasCategories:u,groupedOptions:O,filteredOptions:r,highlightedIndex:a,renderOption:m,optionClassName:s,onSelect:p})=>jsx("div",{className:"py-2",children:u?Object.entries(O).map(([i,g])=>jsxs("div",{children:[i!=="default"&&jsx("div",{className:"px-4 py-2 text-xs font-semibold text-gray-400 uppercase",children:i}),g.map((x,P)=>{let L=r.indexOf(x);return jsx(W,{option:x,index:L,isHighlighted:L===a,renderOption:m,optionClassName:s,onSelect:p},`${x.value}-${P}`)})]},i)):r.map((i,g)=>jsx(W,{option:i,index:g,isHighlighted:g===a,renderOption:m,optionClassName:s,onSelect:p},`${i.value}-${g}`))}));q.displayName="OptionsList";var W=memo(({option:u,index:O,isHighlighted:r,renderOption:a$1,optionClassName:m,onSelect:s})=>{let p=useCallback(()=>{s(u);},[u,s]);return a$1?jsx("div",{onClick:p,children:a$1(u,O)}):jsx("button",{type:"button",onClick:p,className:a("w-full px-4 py-3 text-left text-sm","hover:bg-gray-50 transition-colors",r&&"bg-gray-100",m),children:u.label})});W.displayName="OptionItem";var be=_;export{be as default};
|