Skip to content

Commit 4d90f32

Browse files
committed
[Autocomplete] Avoid destroying on disconnect
When moving the element, the disconnect/connect process can cause the underlying value to be lost
1 parent 97fe2cd commit 4d90f32

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

src/Autocomplete/assets/dist/controller.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,21 @@ class default_1 extends Controller {
4949
}
5050
}
5151
connect() {
52-
if (this.urlValue) {
53-
this.tomSelect = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createAutocompleteWithRemoteData).call(this, this.urlValue, this.hasMinCharactersValue ? this.minCharactersValue : null);
54-
return;
55-
}
56-
if (this.optionsAsHtmlValue) {
57-
this.tomSelect = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createAutocompleteWithHtmlContents).call(this);
58-
return;
52+
if (!this.tomSelect) {
53+
if (this.urlValue) {
54+
this.tomSelect = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createAutocompleteWithRemoteData).call(this, this.urlValue, this.hasMinCharactersValue ? this.minCharactersValue : null);
55+
return;
56+
}
57+
if (this.optionsAsHtmlValue) {
58+
this.tomSelect = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createAutocompleteWithHtmlContents).call(this);
59+
return;
60+
}
61+
this.tomSelect = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createAutocomplete).call(this);
5962
}
60-
this.tomSelect = __classPrivateFieldGet(this, _default_1_instances, "m", _default_1_createAutocomplete).call(this);
6163
this.startMutationObserver();
6264
}
6365
disconnect() {
6466
this.stopMutationObserver();
65-
this.tomSelect.destroy();
6667
}
6768
getMaxOptions() {
6869
return this.selectElement ? this.selectElement.options.length : 50;

src/Autocomplete/assets/src/controller.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,30 @@ export default class extends Controller {
6363
}
6464

6565
connect() {
66-
if (this.urlValue) {
67-
this.tomSelect = this.#createAutocompleteWithRemoteData(
68-
this.urlValue,
69-
this.hasMinCharactersValue ? this.minCharactersValue : null
70-
);
66+
if (!this.tomSelect) {
67+
if (this.urlValue) {
68+
this.tomSelect = this.#createAutocompleteWithRemoteData(
69+
this.urlValue,
70+
this.hasMinCharactersValue ? this.minCharactersValue : null
71+
);
72+
73+
return;
74+
}
7175

72-
return;
73-
}
76+
if (this.optionsAsHtmlValue) {
77+
this.tomSelect = this.#createAutocompleteWithHtmlContents();
7478

75-
if (this.optionsAsHtmlValue) {
76-
this.tomSelect = this.#createAutocompleteWithHtmlContents();
79+
return;
80+
}
7781

78-
return;
82+
this.tomSelect = this.#createAutocomplete();
7983
}
8084

81-
this.tomSelect = this.#createAutocomplete();
8285
this.startMutationObserver();
8386
}
8487

8588
disconnect() {
8689
this.stopMutationObserver();
87-
this.tomSelect.destroy();
8890
}
8991

9092
#getCommonConfig(): Partial<TomSettings> {

0 commit comments

Comments
 (0)