-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathdropdownSearch.ts
More file actions
126 lines (113 loc) · 3.55 KB
/
dropdownSearch.ts
File metadata and controls
126 lines (113 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React from 'react';
import {
Search,
AllSubstringsIndexStrategy,
UnorderedSearchIndex,
} from 'js-search';
import {sanitizeOptions} from './optionTypes';
import {DetailedOption, DropdownProps} from '../types';
// Custom tokenizer, see https://github.com/bvaughn/js-search/issues/43
// Split on spaces
const REGEX = /\s+/;
const TOKENIZER = {
tokenize(text: string) {
return text.split(REGEX).filter(
// Filter empty tokens
text => text
);
},
};
interface FilteredOptionsResult {
sanitizedOptions: DetailedOption[];
filteredOptions: DetailedOption[];
}
/**
* Creates filtered dropdown options using js-search with the exact same behavior
* as react-select-fast-filter-options
*/
export function createFilteredOptions(
options: DropdownProps['options'],
searchable: boolean,
searchValue?: string
): FilteredOptionsResult {
// Sanitize and prepare options
let sanitized = sanitizeOptions(options);
const indexes = ['value'];
let hasElement = false,
hasSearch = false;
sanitized = Array.isArray(sanitized)
? sanitized.map(option => {
if (option.search) {
hasSearch = true;
}
if (React.isValidElement(option.label)) {
hasElement = true;
}
return option;
})
: sanitized;
if (!hasElement) {
indexes.push('label');
}
if (hasSearch) {
indexes.push('search');
}
// If not searchable or no search value, return all sanitized options
if (!searchable || !searchValue) {
return {
sanitizedOptions: sanitized || [],
filteredOptions: sanitized || [],
};
}
// Create js-search instance exactly like react-select-fast-filter-options
const search = new Search('value'); // valueKey defaults to 'value'
search.searchIndex = new UnorderedSearchIndex();
search.indexStrategy = new AllSubstringsIndexStrategy();
search.tokenizer = TOKENIZER;
// Add indexes
indexes.forEach(index => {
search.addIndex(index);
});
// Add documents
if (sanitized && sanitized.length > 0) {
search.addDocuments(sanitized);
}
const filtered = search.search(searchValue) as DetailedOption[];
// Convert to lowercase for case insensitive comparison
const searchLower = searchValue.toLowerCase();
const labelMap = new Map(
filtered.map(opt => [
opt.value,
String(opt.label ?? opt.value).toLowerCase(),
])
);
// Sort results by match relevance
const sorted = filtered.sort((a, b) => {
const aLabel = labelMap.get(a.value)!;
const bLabel = labelMap.get(b.value)!;
// Label starts with search value
const aStartsWith = aLabel.startsWith(searchLower);
const bStartsWith = bLabel.startsWith(searchLower);
if (aStartsWith && !bStartsWith) {
return -1;
}
if (!aStartsWith && bStartsWith) {
return 1;
}
// Check for word boundary match (space followed by search term)
const aWordStart = aLabel.includes(' ' + searchLower);
const bWordStart = bLabel.includes(' ' + searchLower);
if (aWordStart && !bWordStart) {
return -1;
}
if (!aWordStart && bWordStart) {
return 1;
}
// Everything else (substring match)
return 0;
});
return {
sanitizedOptions: sanitized || [],
filteredOptions: sorted || [],
};
}