Skip to content

Commit b463ef2

Browse files
committed
Introduce autocomplete_max_options
1 parent f2f436e commit b463ef2

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

src/Autocomplete/assets/dist/controller.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare class export_default extends Controller {
1212
#private;
1313
static values: {
1414
url: StringConstructor;
15+
maxOptions: NumberConstructor;
1516
optionsAsHtml: BooleanConstructor;
1617
loadingMoreText: StringConstructor;
1718
noResultsFoundText: StringConstructor;
@@ -22,6 +23,7 @@ declare class export_default extends Controller {
2223
preload: StringConstructor;
2324
};
2425
readonly urlValue: string;
26+
readonly maxOptionsValue: number;
2527
readonly optionsAsHtmlValue: boolean;
2628
readonly loadingMoreTextValue: string;
2729
readonly noMoreResultsTextValue: string;

src/Autocomplete/assets/dist/controller.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ var controller_default = class extends Controller {
8888
this.resetTomSelect();
8989
}
9090
getMaxOptions() {
91-
return this.selectElement ? this.selectElement.options.length : 50;
91+
if (this.maxOptionsValue) {
92+
return this.maxOptionsValue;
93+
}
94+
if (this.selectElement) {
95+
return this.selectElement.options.length;
96+
}
97+
return 50;
9298
}
9399
/**
94100
* Returns the element, but only if it's a select element.
@@ -342,7 +348,7 @@ createAutocompleteWithRemoteData_fn = function(autocompleteEndpointUrl, minChara
342348
}
343349
return query.length >= 3;
344350
},
345-
maxOptions: null,
351+
maxOptions: this.getMaxOptions(),
346352
optgroupField: "group_by",
347353
// avoid extra filtering after results are returned
348354
score: (search) => (item) => 1,
@@ -399,6 +405,7 @@ createTomSelect_fn = function(options) {
399405
};
400406
controller_default.values = {
401407
url: String,
408+
maxOptions: Number,
402409
optionsAsHtml: Boolean,
403410
loadingMoreText: String,
404411
noResultsFoundText: String,

src/Autocomplete/assets/src/controller.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface OptionDataStructure {
2525
export default class extends Controller {
2626
static values = {
2727
url: String,
28+
maxOptions: Number,
2829
optionsAsHtml: Boolean,
2930
loadingMoreText: String,
3031
noResultsFoundText: String,
@@ -36,6 +37,7 @@ export default class extends Controller {
3637
};
3738

3839
declare readonly urlValue: string;
40+
declare readonly maxOptionsValue: number;
3941
declare readonly optionsAsHtmlValue: boolean;
4042
declare readonly loadingMoreTextValue: string;
4143
declare readonly noMoreResultsTextValue: string;
@@ -292,7 +294,7 @@ export default class extends Controller {
292294

293295
return query.length >= 3;
294296
},
295-
maxOptions: null,
297+
maxOptions: this.getMaxOptions(),
296298
optgroupField: 'group_by',
297299
// avoid extra filtering after results are returned
298300
score: (search: string) => (item: any) => 1,
@@ -319,7 +321,15 @@ export default class extends Controller {
319321
}
320322

321323
private getMaxOptions(): number {
322-
return this.selectElement ? this.selectElement.options.length : 50;
324+
if (this.maxOptionsValue) {
325+
return this.maxOptionsValue;
326+
}
327+
328+
if (this.selectElement) {
329+
return this.selectElement.options.length;
330+
}
331+
332+
return 50;
323333
}
324334

325335
#stripTags(string: string): string {

src/Autocomplete/doc/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ e.g. ``FoodAutocompleteField`` from above):
216216
an autocomplete-Ajax endpoint (e.g. for a custom ``ChoiceType``), then set this
217217
to change the field into an AJAX-powered select.
218218

219+
``autocomplete_max_options`` (default: ``50``)
220+
Limits the maximum number of results displayed in the autocomplete dropdown at once.
221+
219222
``loading_more_text`` (default: 'Loading more results...')
220223
Rendered at the bottom of the list while fetching more results. This message is
221224
automatically translated using the ``AutocompleteBundle`` domain.

src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public function finishView(FormView $view, FormInterface $form, array $options):
6464
$values['url'] = $form->getConfig()->getAttribute('autocomplete_url');
6565
}
6666

67+
if ($options['autocomplete_max_options']) {
68+
$values['max-options'] = $options['autocomplete_max_options'];
69+
}
70+
6771
if ($options['options_as_html']) {
6872
$values['options-as-html'] = '';
6973
}
@@ -142,6 +146,7 @@ public function configureOptions(OptionsResolver $resolver): void
142146
$resolver->setDefaults([
143147
'autocomplete' => false,
144148
'autocomplete_url' => null,
149+
'autocomplete_max_options' => 50,
145150
'tom_select_options' => [],
146151
'options_as_html' => false,
147152
'allow_options_create' => false,

0 commit comments

Comments
 (0)