Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Autocomplete/assets/dist/controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare class export_default extends Controller {
#private;
static values: {
url: StringConstructor;
maxOptions: NumberConstructor;
optionsAsHtml: BooleanConstructor;
loadingMoreText: StringConstructor;
noResultsFoundText: StringConstructor;
Expand All @@ -22,6 +23,7 @@ declare class export_default extends Controller {
preload: StringConstructor;
};
readonly urlValue: string;
readonly maxOptionsValue: number;
readonly optionsAsHtmlValue: boolean;
readonly loadingMoreTextValue: string;
readonly noMoreResultsTextValue: string;
Expand Down
12 changes: 10 additions & 2 deletions src/Autocomplete/assets/dist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,14 @@ var controller_default = class extends Controller {
urlValueChanged() {
this.resetTomSelect();
}
getMaxOptions() {
return this.selectElement ? this.selectElement.options.length : 50;
getMaxOptions(hasRemoteData = false) {
if (this.maxOptionsValue) {
return this.maxOptionsValue;
}
if (!hasRemoteData && this.selectElement) {
return this.selectElement.options.length;
}
return 50;
}
/**
* Returns the element, but only if it's a select element.
Expand Down Expand Up @@ -342,6 +348,7 @@ createAutocompleteWithRemoteData_fn = function(autocompleteEndpointUrl, minChara
}
return query.length >= 3;
},
maxOptions: this.getMaxOptions(true),
optgroupField: "group_by",
// avoid extra filtering after results are returned
score: (search) => (item) => 1,
Expand Down Expand Up @@ -398,6 +405,7 @@ createTomSelect_fn = function(options) {
};
controller_default.values = {
url: String,
maxOptions: Number,
optionsAsHtml: Boolean,
loadingMoreText: String,
noResultsFoundText: String,
Expand Down
15 changes: 13 additions & 2 deletions src/Autocomplete/assets/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface OptionDataStructure {
export default class extends Controller {
static values = {
url: String,
maxOptions: Number,
optionsAsHtml: Boolean,
loadingMoreText: String,
noResultsFoundText: String,
Expand All @@ -36,6 +37,7 @@ export default class extends Controller {
};

declare readonly urlValue: string;
declare readonly maxOptionsValue: number;
declare readonly optionsAsHtmlValue: boolean;
declare readonly loadingMoreTextValue: string;
declare readonly noMoreResultsTextValue: string;
Expand Down Expand Up @@ -292,6 +294,7 @@ export default class extends Controller {

return query.length >= 3;
},
maxOptions: this.getMaxOptions(true),
optgroupField: 'group_by',
// avoid extra filtering after results are returned
score: (search: string) => (item: any) => 1,
Expand All @@ -317,8 +320,16 @@ export default class extends Controller {
return this.#createTomSelect(config);
}

private getMaxOptions(): number {
return this.selectElement ? this.selectElement.options.length : 50;
private getMaxOptions(hasRemoteData: boolean = false): number {
if (this.maxOptionsValue) {
return this.maxOptionsValue;
}

if (!hasRemoteData && this.selectElement) {
return this.selectElement.options.length;
}

return 50;
}

#stripTags(string: string): string {
Expand Down
3 changes: 3 additions & 0 deletions src/Autocomplete/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ e.g. ``FoodAutocompleteField`` from above):
an autocomplete-Ajax endpoint (e.g. for a custom ``ChoiceType``), then set this
to change the field into an AJAX-powered select.

``autocomplete_max_options`` (default: ``50``)
Limits the maximum number of results displayed in the autocomplete dropdown at once.

``loading_more_text`` (default: 'Loading more results...')
Rendered at the bottom of the list while fetching more results. This message is
automatically translated using the ``AutocompleteBundle`` domain.
Expand Down
5 changes: 5 additions & 0 deletions src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function finishView(FormView $view, FormInterface $form, array $options):
$values['url'] = $form->getConfig()->getAttribute('autocomplete_url');
}

if ($options['autocomplete_max_options']) {
$values['max-options'] = $options['autocomplete_max_options'];
}

if ($options['options_as_html']) {
$values['options-as-html'] = '';
}
Expand Down Expand Up @@ -142,6 +146,7 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setDefaults([
'autocomplete' => false,
'autocomplete_url' => null,
'autocomplete_max_options' => 50,
'tom_select_options' => [],
'options_as_html' => false,
'allow_options_create' => false,
Expand Down
Loading