Skip to content

Commit 23a2df8

Browse files
authored
fix(ui5-toolbar-select): add invalidateOnChildChange (#12676)
Problem: - When programmatically changing the selected property on a ToolbarSelectOption, the ToolbarSelect component's display was not updating to reflect the change. - This occurred because the ToolbarSelect was not invalidating and re-rendering when its child options' properties changed. Solution: - Added invalidateOnChildChange: true to the options slot decorator in ToolbarSelect to trigger re-rendering when child ToolbarSelectOption properties change - Also added cypress test Fixes: #12619
1 parent f01b2eb commit 23a2df8

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

packages/main/cypress/specs/ToolbarSelect.cy.tsx

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Toolbar from "../../src/Toolbar.js";
22
import ToolbarSelect from "../../src/ToolbarSelect.js";
33
import ToolbarSelectOption from "../../src/ToolbarSelectOption.js";
4+
import Button from "../../src/Button.js";
45

56
describe("Toolbar general interaction", () => {
67
it("Should render the select with the correct attributes", () => {
@@ -264,10 +265,10 @@ describe("Toolbar general interaction", () => {
264265
cy.mount(
265266
<Toolbar id="otb_d">
266267
<ToolbarSelect style="width: 201px;" id="toolbar-select">
267-
<ToolbarSelectOption>1</ToolbarSelectOption>
268-
<ToolbarSelectOption selected>2</ToolbarSelectOption>
269-
<ToolbarSelectOption>3</ToolbarSelectOption>
270-
</ToolbarSelect>
268+
<ToolbarSelectOption>1</ToolbarSelectOption>
269+
<ToolbarSelectOption selected>2</ToolbarSelectOption>
270+
<ToolbarSelectOption>3</ToolbarSelectOption>
271+
</ToolbarSelect>
271272
</Toolbar>
272273
);
273274
cy.viewport(220, 1080); // Set a small viewport width to trigger overflow
@@ -282,4 +283,65 @@ describe("Toolbar general interaction", () => {
282283
// Verify the toolbar-select is rendered inside the popover
283284
cy.get("ui5-toolbar-select").should("be.visible");
284285
});
286+
287+
it("Should update selection when option's selected property is changed programmatically", () => {
288+
cy.mount(
289+
<>
290+
<Toolbar>
291+
<ToolbarSelect>
292+
<ToolbarSelectOption>1</ToolbarSelectOption>
293+
<ToolbarSelectOption id="opt2">2</ToolbarSelectOption>
294+
<ToolbarSelectOption>3</ToolbarSelectOption>
295+
<ToolbarSelectOption>4</ToolbarSelectOption>
296+
</ToolbarSelect>
297+
</Toolbar>
298+
<Button id="btn">select option 2</Button>
299+
</>
300+
);
301+
302+
// Set up button click handler
303+
cy.get("#btn").then($btn => {
304+
$btn.get(0).addEventListener("ui5-click", () => {
305+
// First, deselect all options
306+
const select = document.querySelector("ui5-toolbar-select");
307+
const options = select?.querySelectorAll("ui5-toolbar-select-option");
308+
options?.forEach(opt => {
309+
(opt as ToolbarSelectOption).selected = false;
310+
});
311+
// Then select option 2
312+
const opt2 = document.getElementById("opt2") as ToolbarSelectOption;
313+
opt2.selected = true;
314+
});
315+
});
316+
317+
// Verify initial state - first option has selected attribute
318+
cy.get("[ui5-toolbar]")
319+
.find("[ui5-toolbar-select]")
320+
.shadow()
321+
.find("[ui5-select]")
322+
.find("[ui5-option]")
323+
.eq(0)
324+
.should("have.attr", "selected");
325+
326+
// Click button using realClick
327+
cy.get("#btn").realClick();
328+
329+
// Verify option 2 now has the selected attribute
330+
cy.get("[ui5-toolbar]")
331+
.find("[ui5-toolbar-select]")
332+
.shadow()
333+
.find("[ui5-select]")
334+
.find("[ui5-option]")
335+
.eq(1)
336+
.should("have.attr", "selected");
337+
338+
// Verify option 1 no longer has selected attribute
339+
cy.get("[ui5-toolbar]")
340+
.find("[ui5-toolbar-select]")
341+
.shadow()
342+
.find("[ui5-select]")
343+
.find("[ui5-option]")
344+
.eq(0)
345+
.should("not.have.attr", "selected");
346+
});
285347
});

packages/main/src/ToolbarSelect.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class ToolbarSelect extends ToolbarItem {
9191
@slot({
9292
"default": true,
9393
type: HTMLElement,
94+
invalidateOnChildChange: true,
9495
})
9596
options!: Array<ToolbarSelectOption>;
9697

0 commit comments

Comments
 (0)