Skip to content

Commit 7f4b817

Browse files
Fixed dropdown search and tags lose option when re-render
1 parent df963c0 commit 7f4b817

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/scripts/Providers/OSUI/Dropdown/VirtualSelect/AbstractVirtualSelect.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,23 @@ namespace Providers.OSUI.Dropdown.VirtualSelect {
135135
* @memberof Providers.OSUI.Dropdown.VirtualSelect.AbstractVirtualSelect
136136
*/
137137
protected createProviderInstance(): void {
138-
// Create the provider instance
139-
this.provider = window.VirtualSelect.init(this.virtualselectOpts);
138+
let selectedValue: unknown = this.virtualselectOpts.selectedValue;
139+
140+
// Stores the values selected before provider recreation
141+
// when there is a rerender of this component.
142+
if (this.provider !== undefined) {
143+
const selectedOptions = this.provider.getSelectedOptions();
144+
if (selectedOptions !== undefined) {
145+
if (Array.isArray(selectedOptions)) {
146+
selectedValue = selectedOptions.map((selectedOption) => selectedOption.value);
147+
} else {
148+
selectedValue = selectedOptions.value;
149+
}
150+
}
151+
}
152+
153+
// Create the provider instance using the starting value or the previous selected values
154+
this.provider = window.VirtualSelect.init({ ...this.virtualselectOpts, selectedValue });
140155

141156
/* NOTE: When user change the URL and then click at browser back button we're getting an error. This happen because library (VS - VirtualSelect) creates a new instance of the same object and assign it into an array of VS objects that are in the same screen (in this case 2 equal VS objects since we're creating a new VS instance for each Dropdown), that said and in order to avoid this issue, we must follow this approach.
142157
Again, this only happens when user change directly the URL! */

src/scripts/Providers/OSUI/Dropdown/VirtualSelect/VirtualSelect.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace Providers.OSUI.Dropdown.VirtualSelect {
4040
getDisplayValue: Function;
4141
getNewValue: Function;
4242
getSelectedOptions: Function;
43+
setSelectedOptions: Function;
4344
hide: Function;
4445
isAllSelected: Function;
4546
open: Function;

0 commit comments

Comments
 (0)