Skip to content

Commit

Permalink
feat(ui5-shellbar): new event added (#8197)
Browse files Browse the repository at this point in the history
New event search-button-click is added, which allows different actions to be triggered
when it is fired on the search button inside the ui5-shellbar.
If its preventDefault method is called, the default action of the search button
(expanding/collapsing the input) won`t be triggered, but let the users trigger their own.

Related: #6273
  • Loading branch information
yanaminkova committed Feb 1, 2024
1 parent 43d32f0 commit 4e864e9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
32 changes: 32 additions & 0 deletions packages/fiori/src/ShellBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ type ShellBarMenuItemClickEventDetail = {
item: HTMLElement;
};

type ShellBarSearchButtonEventDetail = {
targetRef: HTMLElement;
searchFieldVisible: boolean;
};

type ShellBarCoPilot = {
animated?: boolean,
animationValues?: string,
Expand Down Expand Up @@ -279,6 +284,23 @@ const HANDLE_RESIZE_DEBOUNCE_RATE = 200; // ms
},
})

/**
* Fired, when the search button is activated.
* <b>Note:</b> You can prevent expanding/collapsing of the search field by calling <code>event.preventDefault()</code>.
*
* @allowPreventDefault
* @param {HTMLElement} targetRef dom ref of the activated element
* @param {Boolean} searchFieldVisible whether the search field is visible
* @public
*/

@event<ShellBarSearchButtonEventDetail>("search-button-click", {
detail: {
targetRef: { type: HTMLElement },
searchFieldVisible: { type: Boolean },
},
})

class ShellBar extends UI5Element {
/**
* Defines the <code>primaryTitle</code>.
Expand Down Expand Up @@ -796,6 +818,15 @@ class ShellBar extends UI5Element {
}

_handleSearchIconPress() {
const searchButtonRef = this.shadowRoot!.querySelector<Button>(".ui5-shellbar-search-button")!;
const defaultPrevented = !this.fireEvent<ShellBarSearchButtonEventDetail>("search-button-click", {
targetRef: searchButtonRef,
searchFieldVisible: this.showSearchField,
}, true);

if (defaultPrevented) {
return;
}
this.showSearchField = !this.showSearchField;

if (!this.showSearchField) {
Expand Down Expand Up @@ -1328,4 +1359,5 @@ export type {
ShellBarAccessibilityAttributes,
ShellBarAccessibilityRoles,
ShellBarAccessibilityTexts,
ShellBarSearchButtonEventDetail,
};
16 changes: 14 additions & 2 deletions packages/fiori/test/pages/ShellBar.html
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,11 @@ <h3>ShellBar in Compact</h3>
<ui5-avatar slot="profile" icon="customer"></ui5-avatar>
</ui5-shellbar>

<ui5-shellbar id="sbAccRoles">
<ui5-shellbar id="sb">
<img src="https://upload.wikimedia.org/wikipedia/commons/5/59/SAP_2011_logo.svg" slot="logo"/>
<ui5-input slot="searchField">
<ui5-li icon="world" description="travel the world" additional-text="explore" additional-text-state="Success">1</ui5-li>
</ui5-input>
</ui5-shellbar>

<script>
Expand Down Expand Up @@ -269,6 +272,10 @@ <h3>ShellBar in Compact</h3>
window["press-input"].value = "CoPilot";
});

shellbar.addEventListener("ui5-search-button-click", function(event) {
window["press-input"].value = "Search Button";
});

shellbar.addEventListener("ui5-menu-item-click", function(event) {
if (checkKeepPopoverOpen.checked) {
event.preventDefault();
Expand Down Expand Up @@ -322,9 +329,14 @@ <h3>ShellBar in Compact</h3>
logoTitle: "Custom logo title",
};

sbAccRoles.accessibilityRoles = {
sb.accessibilityRoles = {
logoRole: "link"
};

sb.addEventListener("ui5-search-button-click", function(event) {
event.preventDefault();
});

</script>
</body>
</html>
26 changes: 25 additions & 1 deletion packages/fiori/test/specs/ShellBar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("Component Behavior", () => {
});

it("tests acc custom roles", async () => {
const sb = await browser.$("#sbAccRoles");
const sb = await browser.$("#sb");
const logoDOM = await sb.shadow$(".ui5-shellbar-logo");

// assertHANDLE_RESIZE_DEBOUNCE_RATE_WAIT
Expand Down Expand Up @@ -389,6 +389,30 @@ describe("Component Behavior", () => {
assert.strictEqual(await input.getValue(), "CoPilot", "Input value is set by click event of CoPilot");
});

it("tests search-button-click event", async () => {
setTimeout(async () => {
const searchIcon = await browser.$("#shellbar").shadow$(".ui5-shellbar-search-button");
const input = await browser.$("#press-input");

await searchIcon.click();
assert.strictEqual(await input.getValue(), "Search Button", "Input value is set by click event of Search Button");
}, HANDLE_RESIZE_DEBOUNCE_RATE_WAIT);

});

it("tests search-button-click event", async () => {
setTimeout(async () => {
const searchButton = await browser.$("#sb").shadow$(".ui5-shellbar-search-button");
const searchField = await browser.$("#sb").shadow$(".ui5-shellbar-search-field");
assert.strictEqual(await searchField.isDisplayed(), false, "Search is hidden by default");

await searchButton .click();
assert.notOk(await searchField.isDisplayed(), "Search field should not be opened");
}, HANDLE_RESIZE_DEBOUNCE_RATE_WAIT);

});


it("tests menuItemClick event", async () => {
const primaryTitle = await browser.$("#shellbar").shadow$(".ui5-shellbar-menu-button");
const staticAreaItemClassName = await browser.getStaticAreaItemClassName("#shellbar");
Expand Down

0 comments on commit 4e864e9

Please sign in to comment.