Skip to content

Commit 4aed816

Browse files
feat(ui5-search-item-show-more): introduce new click event (#12410)
1 parent 9256d97 commit 4aed816

File tree

5 files changed

+118
-21
lines changed

5 files changed

+118
-21
lines changed

packages/fiori/cypress/specs/Search.cy.tsx

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,64 @@ describe("Events", () => {
623623
}));
624624
});
625625

626+
it("click event on show-more-item selection with mouse", () => {
627+
cy.mount(
628+
<Search>
629+
<SearchItem text="List Item"></SearchItem>
630+
<SearchItemShowMore></SearchItemShowMore>
631+
</Search>
632+
);
633+
634+
cy.get("[ui5-search-item-show-more]")
635+
.invoke("on", "ui5-click", cy.spy().as("clickSpy"));
636+
637+
cy.get("[ui5-search]")
638+
.shadow()
639+
.find("input")
640+
.realClick();
641+
642+
cy.get("[ui5-search]")
643+
.should("be.focused");
644+
645+
cy.realType("l");
646+
647+
cy.get("[ui5-search-item-show-more]")
648+
.realClick();
649+
650+
cy.get("@clickSpy")
651+
.should("have.been.calledOnce");
652+
});
653+
654+
it("click event on show-more-item selection with Enter", () => {
655+
cy.mount(
656+
<Search>
657+
<SearchItem text="List Item"></SearchItem>
658+
<SearchItemShowMore></SearchItemShowMore>
659+
</Search>
660+
);
661+
662+
cy.get("[ui5-search-item-show-more]")
663+
.invoke("on", "ui5-click", cy.spy().as("clickSpy"));
664+
665+
cy.get("[ui5-search]")
666+
.shadow()
667+
.find("input")
668+
.realClick();
669+
670+
cy.get("[ui5-search]")
671+
.should("be.focused");
672+
673+
cy.realType("l");
674+
675+
cy.realPress("ArrowDown");
676+
cy.realPress("ArrowDown");
677+
678+
cy.realPress("Enter");
679+
680+
cy.get("@clickSpy")
681+
.should("have.been.calledOnce");
682+
});
683+
626684
it("search event prevention", () => {
627685
cy.mount(
628686
<Search>
@@ -1090,7 +1148,7 @@ describe("Events", () => {
10901148
.shadow()
10911149
.find("input")
10921150
.as("input");
1093-
1151+
10941152
cy.get("@input")
10951153
.realClick();
10961154

@@ -1102,7 +1160,7 @@ describe("Events", () => {
11021160

11031161
cy.get("@search")
11041162
.should("have.value", "Item 1");
1105-
1163+
11061164
cy.get("[ui5-search-item]").eq(0)
11071165
.should("have.attr", "highlight-text", "I");
11081166

@@ -1137,7 +1195,7 @@ describe("Events", () => {
11371195
.shadow()
11381196
.find("input")
11391197
.as("input");
1140-
1198+
11411199
cy.get("@input")
11421200
.realClick();
11431201

@@ -1172,7 +1230,7 @@ describe("Events", () => {
11721230
.shadow()
11731231
.find("input")
11741232
.as("input");
1175-
1233+
11761234
cy.get("@input")
11771235
.realClick();
11781236

packages/fiori/src/SearchItemShowMore.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
22
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
3+
import event from "@ui5/webcomponents-base/dist/decorators/event-strict.js";
34
import ListItemBase from "@ui5/webcomponents/dist/ListItemBase.js";
45
import jsxRenderer from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js";
56
import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js";
@@ -8,6 +9,11 @@ import SearchItemShowMoreTemplate from "./SearchItemShowMoreTemplate.js";
89
import SearchItemCss from "./generated/themes/SearchItem.css.js";
910
import SearchItemShowMoreCss from "./generated/themes/SearchItemShowMore.css.js";
1011
import { SEARCH_ITEM_SHOW_MORE_COUNT, SEARCH_ITEM_SHOW_MORE_NO_COUNT } from "./generated/i18n/i18n-defaults.js";
12+
import { isEnter, isSpace } from "@ui5/webcomponents-base/dist/Keys.js";
13+
14+
type ShowMoreItemClickEventDetail = {
15+
fromKeyboard: boolean;
16+
}
1117

1218
/**
1319
* @class
@@ -25,7 +31,6 @@ import { SEARCH_ITEM_SHOW_MORE_COUNT, SEARCH_ITEM_SHOW_MORE_NO_COUNT } from "./g
2531
* @since 2.14.0
2632
* @experimental
2733
*/
28-
2934
@customElement({
3035
tag: "ui5-search-item-show-more",
3136
languageAware: true,
@@ -38,7 +43,23 @@ import { SEARCH_ITEM_SHOW_MORE_COUNT, SEARCH_ITEM_SHOW_MORE_NO_COUNT } from "./g
3843
],
3944
})
4045

46+
/**
47+
* Fired when the component is activated, either with a mouse/tap
48+
* or by pressing the Enter or Space keys.
49+
*
50+
* @public
51+
* @param {boolean} fromKeyboard Indicates whether the event was fired
52+
* due to keyboard interaction (Enter or Space) rather than mouse/tap.
53+
*/
54+
@event("click", {
55+
bubbles: true,
56+
cancelable: true,
57+
})
58+
4159
class SearchItemShowMore extends ListItemBase {
60+
eventDetails!: ListItemBase["eventDetails"] & {
61+
"click": ShowMoreItemClickEventDetail;
62+
}
4263
/**
4364
* Specifies the number of additional items available to show.
4465
* This value replaces the placeholder (N) in the "Show more (N)" text.
@@ -76,8 +97,31 @@ class SearchItemShowMore extends ListItemBase {
7697
_onfocusout() {
7798
this.selected = false;
7899
}
100+
101+
_onclick(e: MouseEvent | KeyboardEvent, fromKeyboard = false) {
102+
e.stopImmediatePropagation();
103+
this.fireDecoratorEvent("click", { fromKeyboard });
104+
}
105+
106+
_onkeydown(e: KeyboardEvent) {
107+
if (isEnter(e)) {
108+
this._onclick(e, true);
109+
e.preventDefault();
110+
}
111+
}
112+
113+
_onkeyup(e: KeyboardEvent) {
114+
if (isSpace(e)) {
115+
this._onclick(e, true);
116+
e.preventDefault();
117+
}
118+
}
79119
}
80120

81121
SearchItemShowMore.define();
82122

83123
export default SearchItemShowMore;
124+
125+
export type {
126+
ShowMoreItemClickEventDetail,
127+
};

packages/fiori/src/SearchItemShowMoreTemplate.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export default function SearchItemShowMoreTemplate(this: SearchItemShowMore) {
99
aria-selected={this.selected}
1010
onFocusIn={this._onfocusin}
1111
onFocusOut={this._onfocusout}
12+
onClick={this._onclick}
13+
onKeyDown={this._onkeydown}
14+
onKeyUp={this._onkeyup}
1215
>
1316
<span class="ui5-search-item-show-more-text">{this.showMoreTextCount}</span>
1417
</li>

packages/fiori/test/pages/Search.html

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
{ text: "List Item 6", icon: "globe" }
7777
];
7878

79+
const searchShow = document.getElementById("searchShowMore");
7980
const group1 = document.getElementById("group1");
8081
const visibleCount = 3;
8182

@@ -121,13 +122,9 @@
121122
}
122123
}
123124

124-
showMoreEl.addEventListener("click", () => expandHiddenItems());
125-
126-
showMoreEl.addEventListener("keydown", (e) => {
127-
if (e.key === "Enter") {
128-
expandHiddenItems({ focusFirst: true })
129-
}
130-
});
125+
showMoreEl.addEventListener("ui5-click", (e) => {
126+
expandHiddenItems({ focusFirst: e.detail.fromKeyboard });
127+
});
131128
}
132129
}
133130

@@ -411,7 +408,7 @@
411408
item.remove();
412409
});
413410
}, 100)
414-
411+
415412

416413
if(e.target.value.length){
417414
// simulate lazy loading

packages/website/docs/_samples/fiori/Search/ShowMore/main.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import "@ui5/webcomponents-fiori/dist/SearchItemGroup.js";
1313
{ text: "List Item 5", icon: "search" },
1414
{ text: "List Item 6", icon: "globe" }
1515
];
16-
16+
const searchShow = document.getElementById("searchShowMore");
1717
const group1 = document.getElementById("group1");
1818
const visibleCount = 3;
1919

@@ -59,13 +59,8 @@ import "@ui5/webcomponents-fiori/dist/SearchItemGroup.js";
5959
}, 0);
6060
}
6161
}
62-
63-
showMoreEl.addEventListener("click", () => expandHiddenItems());
64-
65-
showMoreEl.addEventListener("keydown", (e) => {
66-
if (e.key === "Enter") {
67-
expandHiddenItems({ focusFirst: true })
68-
}
62+
showMoreEl.addEventListener("ui5-click", (e) => {
63+
expandHiddenItems({ focusFirst: e.detail.fromKeyboard });
6964
});
7065
}
7166
}

0 commit comments

Comments
 (0)