Skip to content

Commit 7a308c4

Browse files
committed
feat(autocomplete): add option to specify minimum characters for autocomplete
1 parent 7a2eda6 commit 7a308c4

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/Autocomplete/assets/dist/controller.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ _instances = new WeakSet(), _getCommonConfig = function _getCommonConfig() {
134134
.then(json => { this.setNextUrl(query, json.next_page); callback(json.results); })
135135
.catch(() => callback());
136136
},
137+
shouldLoad: (query) => {
138+
if (query.length < this.minCharactersTextValue) {
139+
return;
140+
}
141+
},
137142
score: function (search) {
138143
return function (item) {
139144
return 1;
@@ -173,6 +178,7 @@ default_1.values = {
173178
optionsAsHtml: Boolean,
174179
noResultsFoundText: String,
175180
noMoreResultsText: String,
181+
minCharacters: Number,
176182
tomSelectOptions: Object,
177183
};
178184

src/Autocomplete/assets/src/controller.ts

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

1415
readonly urlValue: string;
1516
readonly optionsAsHtmlValue: boolean;
1617
readonly noMoreResultsTextValue: string;
1718
readonly noResultsFoundTextValue: string;
19+
readonly minCharactersTextValue: number;
1820
readonly tomSelectOptionsValue: object;
1921
tomSelect: TomSelect;
2022

@@ -142,6 +144,11 @@ export default class extends Controller {
142144
.then(json => { this.setNextUrl(query, json.next_page); callback(json.results) })
143145
.catch(() => callback());
144146
},
147+
shouldLoad: (query: string) => {
148+
if (query.length < this.minCharactersTextValue) {
149+
return;
150+
}
151+
},
145152
// avoid extra filtering after results are returned
146153
score: function(search: string) {
147154
return function(item: any) {

src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public function finishView(FormView $view, FormInterface $form, array $options)
7171
$values['max-results'] = $options['max_results'];
7272
}
7373

74+
if ($options['min_characters']) {
75+
$values['min-characters'] = $options['min_characters'];
76+
}
77+
7478
$values['no-results-found-text'] = $this->trans($options['no_results_found_text']);
7579
$values['no-more-results-text'] = $this->trans($options['no_more_results_text']);
7680

@@ -91,6 +95,7 @@ public function configureOptions(OptionsResolver $resolver)
9195
'allow_options_create' => false,
9296
'no_results_found_text' => 'No results found',
9397
'no_more_results_text' => 'No more results',
98+
'min_characters' => 3,
9499
'max_results' => 10,
95100
]);
96101

0 commit comments

Comments
 (0)