@@ -85,8 +85,42 @@ export function createFilteredOptions(
8585
8686 const filtered = search . search ( searchValue ) as DetailedOption [ ] ;
8787
88+ // Convert to lowercase for case insensitive comparison
89+ const searchLower = searchValue . toLowerCase ( ) ;
90+ const labelMap = new Map (
91+ filtered . map ( opt => [
92+ opt . value ,
93+ String ( opt . label ?? opt . value ) . toLowerCase ( ) ,
94+ ] )
95+ ) ;
96+ // Sort results by match relevance
97+ const sorted = filtered . sort ( ( a , b ) => {
98+ const aLabel = labelMap . get ( a . value ) ! ;
99+ const bLabel = labelMap . get ( b . value ) ! ;
100+ // Label starts with search value
101+ const aStartsWith = aLabel . startsWith ( searchLower ) ;
102+ const bStartsWith = bLabel . startsWith ( searchLower ) ;
103+ if ( aStartsWith && ! bStartsWith ) {
104+ return - 1 ;
105+ }
106+ if ( ! aStartsWith && bStartsWith ) {
107+ return 1 ;
108+ }
109+ // Check for word boundary match (space followed by search term)
110+ const aWordStart = aLabel . includes ( ' ' + searchLower ) ;
111+ const bWordStart = bLabel . includes ( ' ' + searchLower ) ;
112+ if ( aWordStart && ! bWordStart ) {
113+ return - 1 ;
114+ }
115+ if ( ! aWordStart && bWordStart ) {
116+ return 1 ;
117+ }
118+ // Everything else (substring match)
119+ return 0 ;
120+ } ) ;
121+
88122 return {
89123 sanitizedOptions : sanitized || [ ] ,
90- filteredOptions : filtered || [ ] ,
124+ filteredOptions : sorted || [ ] ,
91125 } ;
92126}
0 commit comments