Skip to content

Commit a8aaaad

Browse files
committed
Add sort relevance
1 parent 83fe8c9 commit a8aaaad

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

components/dash-core-components/src/utils/dropdownSearch.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)