Skip to content

[Autocomplete] implement preload option #539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2022
Merged
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
10 changes: 9 additions & 1 deletion src/Autocomplete/assets/dist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class default_1 extends Controller {
}
return this.element;
}
get preload() {
if (this.preloadValue == 'false')
return false;
if (this.preloadValue == 'true')
return true;
return this.preloadValue;
}
}
_default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _default_1_getCommonConfig() {
const plugins = {};
Expand Down Expand Up @@ -158,7 +165,7 @@ _default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _def
return `<div class="no-results">${this.noResultsFoundTextValue}</div>`;
},
},
preload: 'focus',
preload: this.preload,
});
return __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createTomSelect).call(this, config);
}, _default_1_stripTags = function _default_1_stripTags(string) {
Expand All @@ -180,6 +187,7 @@ default_1.values = {
noMoreResultsText: String,
minCharacters: Number,
tomSelectOptions: Object,
preload: String,
};

export { default_1 as default };
16 changes: 15 additions & 1 deletion src/Autocomplete/assets/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class extends Controller {
noMoreResultsText: String,
minCharacters: Number,
tomSelectOptions: Object,
preload: String,
};

readonly urlValue: string;
Expand All @@ -18,6 +19,7 @@ export default class extends Controller {
readonly noResultsFoundTextValue: string;
readonly minCharactersValue: number;
readonly tomSelectOptionsValue: object;
readonly preloadValue: string;
tomSelect: TomSelect;

initialize() {
Expand Down Expand Up @@ -174,7 +176,7 @@ export default class extends Controller {
return `<div class="no-results">${this.noResultsFoundTextValue}</div>`;
},
},
preload: 'focus',
preload: this.preload,
});

return this.#createTomSelect(config);
Expand Down Expand Up @@ -221,4 +223,16 @@ export default class extends Controller {
#dispatchEvent(name: string, payload: any): void {
this.element.dispatchEvent(new CustomEvent(name, { detail: payload, bubbles: true }));
}

get preload() {
if (this.preloadValue == 'false') {
return false;
}

if (this.preloadValue == 'true') {
return true;
}

return this.preloadValue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some { to the if statements to standardize with the coding style? And also an empty break before the return statement. thx :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the if statements.
I don't see where you want me to put the break.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant an empty line break. You did it perfectly 👍

}
}
4 changes: 4 additions & 0 deletions src/Autocomplete/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ to the options above, you can also pass:
``max_results`` (default: 10)
Allow you to control the max number of results returned by the automatic autocomplete endpoint.

``preload`` (default: ``false``)
Set to ``focus`` to call the ``load`` function when control receives focus.
Set to ``true`` to call the ``load`` upon control initialization (with an empty search).

Using with a TextType Field
---------------------------

Expand Down
10 changes: 10 additions & 0 deletions src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)

$values['no-results-found-text'] = $this->trans($options['no_results_found_text']);
$values['no-more-results-text'] = $this->trans($options['no_more_results_text']);
$values['preload'] = $options['preload'];

foreach ($values as $name => $value) {
$attr['data-'.$controllerName.'-'.$name.'-value'] = $value;
Expand All @@ -97,12 +98,21 @@ public function configureOptions(OptionsResolver $resolver)
'no_more_results_text' => 'No more results',
'min_characters' => 3,
'max_results' => 10,
'preload' => false,
]);

// if autocomplete_url is passed, then HTML options are already supported
$resolver->setNormalizer('options_as_html', function (Options $options, $value) {
return null === $options['autocomplete_url'] ? $value : false;
});

$resolver->setNormalizer('preload', function (Options $options, $value) {
if (\is_bool($value)) {
$value = $value ? 'true' : 'false';
}

return $value;
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work if we removed this and, instead, above, did:

$values['preload'] = json_encode($options['preload']);

Copy link
Contributor Author

@seb-jean seb-jean Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For boolean values it works, but not for strings

autocomplete

}

private function trans(string $message): string
Expand Down