Skip to content

Commit 58a0505

Browse files
committed
fix(ui5-toolbar-select): sync programmatic selection
1 parent 5f8bf37 commit 58a0505

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

packages/main/src/ToolbarSelect.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,7 @@ class ToolbarSelect extends ToolbarItem {
201201
_syncOptions(selectedOption: HTMLElement): void {
202202
const selectedOptionIndex = Number(selectedOption?.getAttribute("data-ui5-external-action-item-index"));
203203
this.options.forEach((option: ToolbarSelectOption, index: number) => {
204-
if (index === selectedOptionIndex) {
205-
option.setAttribute("selected", "");
206-
} else {
207-
option.removeAttribute("selected");
208-
}
204+
option.selected = index === selectedOptionIndex;
209205
});
210206
}
211207

packages/main/src/ToolbarSelectOption.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
22
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
33
import slot from "@ui5/webcomponents-base/dist/decorators/slot.js";
44
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
5+
import type ToolbarSelect from "./ToolbarSelect.js";
56

67
/**
78
* @class
@@ -23,7 +24,32 @@ class ToolbarSelectOption extends UI5Element {
2324
* @public
2425
*/
2526
@property({ type: Boolean })
26-
selected = false;
27+
set selected(value: boolean) {
28+
if (value) {
29+
this.setAttribute("selected", "");
30+
this._clearSiblingsAndSync();
31+
} else {
32+
this.removeAttribute("selected");
33+
}
34+
}
35+
36+
get selected(): boolean {
37+
return this.hasAttribute("selected");
38+
}
39+
40+
_clearSiblingsAndSync(): void {
41+
const parent = this.parentElement as ToolbarSelect;
42+
if (parent) {
43+
parent.options?.forEach(option => {
44+
if (option !== this) {
45+
option.removeAttribute("selected");
46+
}
47+
});
48+
if (parent.select) {
49+
parent.select.value = this.textContent || "";
50+
}
51+
}
52+
}
2753

2854
/**
2955
* Defines the text of the component.

0 commit comments

Comments
 (0)