Skip to content

Commit d58e967

Browse files
committed
fixup! feat(autocomplete): add option to specify minimum characters for autocomplete
1 parent 87e1637 commit d58e967

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/Autocomplete/assets/dist/controller.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class default_1 extends Controller {
3939
}
4040
connect() {
4141
if (this.urlValue) {
42-
this.tomSelect = __classPrivateFieldGet(this, _instances, "m", _createAutocompleteWithRemoteData).call(this, this.urlValue);
42+
this.tomSelect = __classPrivateFieldGet(this, _instances, "m", _createAutocompleteWithRemoteData).call(this, this.urlValue, this.minCharactersTextValue);
4343
return;
4444
}
4545
if (this.optionsAsHtmlValue) {
@@ -121,7 +121,7 @@ _instances = new WeakSet(), _getCommonConfig = function _getCommonConfig() {
121121
},
122122
});
123123
return __classPrivateFieldGet(this, _instances, "m", _createTomSelect).call(this, config);
124-
}, _createAutocompleteWithRemoteData = function _createAutocompleteWithRemoteData(autocompleteEndpointUrl) {
124+
}, _createAutocompleteWithRemoteData = function _createAutocompleteWithRemoteData(autocompleteEndpointUrl, minCharacterLength) {
125125
const config = __classPrivateFieldGet(this, _instances, "m", _mergeObjects).call(this, __classPrivateFieldGet(this, _instances, "m", _getCommonConfig).call(this), {
126126
firstUrl: (query) => {
127127
const separator = autocompleteEndpointUrl.includes('?') ? '&' : '?';
@@ -135,9 +135,8 @@ _instances = new WeakSet(), _getCommonConfig = function _getCommonConfig() {
135135
.catch(() => callback());
136136
},
137137
shouldLoad: function (query) {
138-
if (query.length < this.minCharactersTextValue) {
139-
return;
140-
}
138+
const minLength = minCharacterLength ? parseInt(minCharacterLength) : 3;
139+
return query.length >= minLength;
141140
},
142141
score: function (search) {
143142
return function (item) {
@@ -178,7 +177,7 @@ default_1.values = {
178177
optionsAsHtml: Boolean,
179178
noResultsFoundText: String,
180179
noMoreResultsText: String,
181-
minCharacters: Number,
180+
minCharacters: String,
182181
tomSelectOptions: Object,
183182
};
184183

src/Autocomplete/assets/src/controller.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ export default class extends Controller {
88
optionsAsHtml: Boolean,
99
noResultsFoundText: String,
1010
noMoreResultsText: String,
11-
minCharacters: Number,
11+
minCharacters: String,
1212
tomSelectOptions: Object,
1313
}
1414

1515
readonly urlValue: string;
1616
readonly optionsAsHtmlValue: boolean;
1717
readonly noMoreResultsTextValue: string;
1818
readonly noResultsFoundTextValue: string;
19-
readonly minCharactersTextValue: number;
19+
readonly minCharactersTextValue: string;
2020
readonly tomSelectOptionsValue: object;
2121
tomSelect: TomSelect;
2222

@@ -32,7 +32,7 @@ export default class extends Controller {
3232

3333
connect() {
3434
if (this.urlValue) {
35-
this.tomSelect = this.#createAutocompleteWithRemoteData(this.urlValue);
35+
this.tomSelect = this.#createAutocompleteWithRemoteData(this.urlValue, this.minCharactersTextValue);
3636

3737
return;
3838
}
@@ -126,7 +126,7 @@ export default class extends Controller {
126126
return this.#createTomSelect(config);
127127
}
128128

129-
#createAutocompleteWithRemoteData(autocompleteEndpointUrl: string): TomSelect {
129+
#createAutocompleteWithRemoteData(autocompleteEndpointUrl: string, minCharacterLength: string): TomSelect {
130130
const config: Partial<TomSettings> = this.#mergeObjects(this.#getCommonConfig(), {
131131
firstUrl: (query: string) => {
132132
const separator = autocompleteEndpointUrl.includes('?') ? '&' : '?';
@@ -145,9 +145,9 @@ export default class extends Controller {
145145
.catch(() => callback());
146146
},
147147
shouldLoad: function (query: string) {
148-
if (query.length < this.minCharactersTextValue) {
149-
return;
150-
}
148+
const minLength = minCharacterLength ? parseInt(minCharacterLength) : 3;
149+
150+
return query.length >= minLength;
151151
},
152152
// avoid extra filtering after results are returned
153153
score: function(search: string) {

src/Autocomplete/assets/test/controller.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ describe('AutocompleteController', () => {
8181
data-testid="main-element"
8282
data-controller="check autocomplete"
8383
data-autocomplete-url-value="/path/to/autocomplete"
84+
data-autocomplete-min-characters-value="3"
8485
></select>
8586
`);
8687

0 commit comments

Comments
 (0)