Skip to content

Commit 5f11181

Browse files
committed
feature #539 [Autocomplete] implement preload option (seb-jean)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Autocomplete] implement `preload` option | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | N/A | License | MIT Added [preload](https://tom-select.js.org/docs/#preload) option to control the preload. Default value is `false` Commits ------- 54b4ef4 [Autocomplete] implement `preload` option
2 parents 13c212c + 54b4ef4 commit 5f11181

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/Autocomplete/assets/dist/controller.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ class default_1 extends Controller {
6464
}
6565
return this.element;
6666
}
67+
get preload() {
68+
if (this.preloadValue == 'false')
69+
return false;
70+
if (this.preloadValue == 'true')
71+
return true;
72+
return this.preloadValue;
73+
}
6774
}
6875
_default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _default_1_getCommonConfig() {
6976
const plugins = {};
@@ -158,7 +165,7 @@ _default_1_instances = new WeakSet(), _default_1_getCommonConfig = function _def
158165
return `<div class="no-results">${this.noResultsFoundTextValue}</div>`;
159166
},
160167
},
161-
preload: 'focus',
168+
preload: this.preload,
162169
});
163170
return __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createTomSelect).call(this, config);
164171
}, _default_1_stripTags = function _default_1_stripTags(string) {
@@ -180,6 +187,7 @@ default_1.values = {
180187
noMoreResultsText: String,
181188
minCharacters: Number,
182189
tomSelectOptions: Object,
190+
preload: String,
183191
};
184192

185193
export { default_1 as default };

src/Autocomplete/assets/src/controller.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default class extends Controller {
1010
noMoreResultsText: String,
1111
minCharacters: Number,
1212
tomSelectOptions: Object,
13+
preload: String,
1314
};
1415

1516
readonly urlValue: string;
@@ -18,6 +19,7 @@ export default class extends Controller {
1819
readonly noResultsFoundTextValue: string;
1920
readonly minCharactersValue: number;
2021
readonly tomSelectOptionsValue: object;
22+
readonly preloadValue: string;
2123
tomSelect: TomSelect;
2224

2325
initialize() {
@@ -174,7 +176,7 @@ export default class extends Controller {
174176
return `<div class="no-results">${this.noResultsFoundTextValue}</div>`;
175177
},
176178
},
177-
preload: 'focus',
179+
preload: this.preload,
178180
});
179181

180182
return this.#createTomSelect(config);
@@ -221,4 +223,16 @@ export default class extends Controller {
221223
#dispatchEvent(name: string, payload: any): void {
222224
this.element.dispatchEvent(new CustomEvent(name, { detail: payload, bubbles: true }));
223225
}
226+
227+
get preload() {
228+
if (this.preloadValue == 'false') {
229+
return false;
230+
}
231+
232+
if (this.preloadValue == 'true') {
233+
return true;
234+
}
235+
236+
return this.preloadValue;
237+
}
224238
}

src/Autocomplete/doc/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ to the options above, you can also pass:
260260
``max_results`` (default: 10)
261261
Allow you to control the max number of results returned by the automatic autocomplete endpoint.
262262

263+
``preload`` (default: ``false``)
264+
Set to ``focus`` to call the ``load`` function when control receives focus.
265+
Set to ``true`` to call the ``load`` upon control initialization (with an empty search).
266+
263267
Using with a TextType Field
264268
---------------------------
265269

src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
7777

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

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

102104
// if autocomplete_url is passed, then HTML options are already supported
103105
$resolver->setNormalizer('options_as_html', function (Options $options, $value) {
104106
return null === $options['autocomplete_url'] ? $value : false;
105107
});
108+
109+
$resolver->setNormalizer('preload', function (Options $options, $value) {
110+
if (\is_bool($value)) {
111+
$value = $value ? 'true' : 'false';
112+
}
113+
114+
return $value;
115+
});
106116
}
107117

108118
private function trans(string $message): string

0 commit comments

Comments
 (0)