Skip to content

Commit 20ba5dc

Browse files
fix(autocomplete): replace standard search func
Standard search function no longer takes "image" into consideration: - Improved algorithmic "performance" O(m x 2n) -> O(n); - Localized "lower case" (better i18n).
1 parent f5057d6 commit 20ba5dc

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/autocomplete.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ let _defaults = {
1313
},
1414
minLength: 1, // Min characters before autocomplete starts
1515
isMultiSelect: false,
16-
onSearch: function(text, autocomplete) {
17-
const filteredData = autocomplete.options.data.filter(item => {
18-
return Object.keys(item)
19-
.map(key => item[key].toString().toLowerCase().indexOf(text.toLowerCase()) >= 0)
20-
.some(isMatch => isMatch);
21-
});
22-
autocomplete.setMenuItems(filteredData);
16+
onSearch: (text: string, autocomplete) => {
17+
const normSearch = text.toLocaleLowerCase();
18+
autocomplete.setMenuItems(
19+
autocomplete.options.data.filter((option) =>
20+
option.id.toString().toLocaleLowerCase().includes(normSearch)
21+
|| option.text?.toLocaleLowerCase().includes(normSearch)
22+
)
23+
);
2324
},
2425
maxDropDownHeight: '300px',
2526
allowUnsafeHTML: false
@@ -175,7 +176,7 @@ export class Autocomplete extends Component {
175176
_handleInputKeyupAndFocus = (e: KeyboardEvent) => {
176177
if (e.type === 'keyup') Autocomplete._keydown = false;
177178
this.count = 0;
178-
const actualValue = this.el.value.toLowerCase();
179+
const actualValue = this.el.value.toLocaleLowerCase();
179180
// Don't capture enter or arrow key usage.
180181
if (M.keys.ENTER.includes(e.key) || M.keys.ARROW_UP.includes(e.key) || M.keys.ARROW_DOWN.includes(e.key)) return;
181182
// Check if the input isn't empty
@@ -254,7 +255,7 @@ export class Autocomplete extends Component {
254255
}
255256

256257
_highlightPartialText(input, label) {
257-
const start = label.toLowerCase().indexOf('' + input.toLowerCase() + '');
258+
const start = label.toLocaleLowerCase().indexOf('' + input.toLocaleLowerCase() + '');
258259
const end = start + input.length - 1;
259260
//custom filters may return results where the string does not match any part
260261
if (start == -1 || end == -1) {
@@ -288,7 +289,7 @@ export class Autocomplete extends Component {
288289
}
289290

290291
// Text
291-
const inputText = this.el.value.toLowerCase();
292+
const inputText = this.el.value.toLocaleLowerCase();
292293
const parts = this._highlightPartialText(inputText, (entry.text || entry.id).toString());
293294
const div = document.createElement('div');
294295
div.setAttribute('style', 'line-height:1.2;font-weight:500;');
@@ -378,7 +379,7 @@ export class Autocomplete extends Component {
378379
}
379380

380381
open() {
381-
const inputText = this.el.value.toLowerCase();
382+
const inputText = this.el.value.toLocaleLowerCase();
382383
this._resetAutocomplete();
383384
if (inputText.length >= this.options.minLength) {
384385
this.isOpen = true;

0 commit comments

Comments
 (0)