Skip to content

Commit fdd67e4

Browse files
authored
fix(ui5-search-field): add scope-value property (#12795)
1 parent e35bf37 commit fdd67e4

File tree

11 files changed

+95
-30
lines changed

11 files changed

+95
-30
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,48 @@ describe("SearchField general interaction", () => {
396396
.should("have.length", 2);
397397
});
398398

399+
it("preselected scope option should be applied", () => {
400+
cy.mount(<SearchField scope-value="products" value="test">
401+
<SearchScope text="Apps" value="apps" slot="scopes"></SearchScope>
402+
<SearchScope text="Products" value="products" slot="scopes"></SearchScope>
403+
</SearchField>);
404+
405+
cy.get("[ui5-search-field]")
406+
.shadow()
407+
.find("[ui5-select]")
408+
.as("select");
409+
410+
cy.get("@select")
411+
.should("have.prop", "value", "products");
412+
});
413+
414+
it("changes scope-value on option select", () => {
415+
cy.mount(<SearchField value="test">
416+
<SearchScope text="Apps" value="apps" slot="scopes"></SearchScope>
417+
<SearchScope text="Products" value="products" slot="scopes"></SearchScope>
418+
</SearchField>);
419+
420+
cy.get("[ui5-search-field]")
421+
.as("searchfield");
422+
423+
cy.get("@searchfield")
424+
.shadow()
425+
.find("[ui5-select]")
426+
.as("scope");
427+
428+
cy.get("@scope")
429+
.realClick();
430+
431+
cy.get("@scope")
432+
.realPress("ArrowDown");
433+
434+
cy.get("@scope")
435+
.realPress("Enter");
436+
437+
cy.get("@searchfield")
438+
.should("have.prop", "scopeValue", "products");
439+
});
440+
399441
it("scope-change event should be fired, when a scope option is selected", () => {
400442
cy.mount(<SearchField value="test">
401443
<SearchScope text="Apps" slot="scopes"></SearchScope>

packages/fiori/src/SearchField.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
*/
2929
interface ISearchScope extends UI5Element {
3030
text?: string,
31-
selected: boolean,
31+
value?: string,
3232
stableDomRef: string,
3333
}
3434

@@ -153,6 +153,20 @@ class SearchField extends UI5Element {
153153
@property()
154154
accessibleDescription?: string;
155155

156+
/**
157+
* Defines the value of the component:
158+
*
159+
* Applications are responsible for setting the correct scope value.
160+
*
161+
* **Note:** If the given value does not match any existing scopes,
162+
* no scope will be selected and the SearchField scope component will be displayed as empty.
163+
* @public
164+
* @default ""
165+
* @since 2.18.0
166+
*/
167+
@property()
168+
scopeValue?: string;
169+
156170
/**
157171
* Defines the component scope options.
158172
* @public
@@ -241,6 +255,12 @@ class SearchField extends UI5Element {
241255

242256
_handleScopeChange(e: CustomEvent<SelectChangeEventDetail>) {
243257
const item = e.detail.selectedOption as IOption & { scopeOption: ISearchScope };
258+
259+
// Set the scopeValue property if the selected scope has a value defined
260+
if (item.value) {
261+
this.scopeValue = item.value;
262+
}
263+
244264
this.fireDecoratorEvent("scope-change", {
245265
scope: item.scopeOption,
246266
});

packages/fiori/src/SearchFieldTemplate.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ export default function SearchFieldTemplate(this: SearchField, options?: SearchF
3636
onChange={this._handleScopeChange}
3737
class="sapUiSizeCompact ui5-search-field-select"
3838
accessibleName={this._translations.scope}
39-
tooltip={this._translations.scope}>
39+
tooltip={this._translations.scope}
40+
value={this.scopeValue}
41+
>
4042
{this.scopes.map(scopeOption => (
4143
<Option
42-
selected={scopeOption.selected}
44+
value={scopeOption.value}
4345
data-ui5-stable={scopeOption.stableDomRef}
4446
ref={this.captureRef.bind(scopeOption)}
4547
>{scopeOption.text}

packages/fiori/src/SearchScope.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ class SearchScope extends UI5Element implements ISearchScope {
2525
text!: string;
2626

2727
/**
28-
* Indicates whether the item is selected
29-
* @default false
28+
* Defines the value of the `ui5-search-scope`.
29+
* Used for selection in Search scopes.
30+
* @default undefined
3031
* @public
3132
*/
32-
@property({ type: Boolean })
33-
selected!: boolean;
33+
@property()
34+
value?: string;
3435

3536
get stableDomRef() {
3637
return this.getAttribute("stable-dom-ref") || `${this._id}-stable-dom-ref`;

packages/fiori/test/pages/Search.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@
213213

214214
<div class="container">
215215
<ui5-label>Search with Scoped Suggestions - Filter by scope</ui5-label>
216-
<ui5-search id="search-scope" show-clear-icon placeholder="Search Apps, Products">
217-
<ui5-search-scope text="All" slot="scopes"></ui5-search-scope>
218-
<ui5-search-scope text="Apps" selected slot="scopes"></ui5-search-scope>
219-
<ui5-search-scope text="Products" slot="scopes"></ui5-search-scope>
216+
<ui5-search id="search-scope" show-clear-icon scope-value="apps" placeholder="Search Apps, Products">
217+
<ui5-search-scope text="All" value="all" slot="scopes"></ui5-search-scope>
218+
<ui5-search-scope text="Apps" value="apps" slot="scopes"></ui5-search-scope>
219+
<ui5-search-scope text="Products" value="products" slot="scopes"></ui5-search-scope>
220220
</ui5-search>
221221
<ui5-text style="padding-top: 0.25rem; font-style: italic;">The examples shows scoped search with scoped suggestions. Change scope to filter suggestions.</ui5-text>
222222
</div>
@@ -324,7 +324,7 @@
324324
const searchScope = document.getElementById('search-scope');
325325
createScopeItems();
326326
searchScope.addEventListener('ui5-scope-change', (event) => {
327-
let scope = event.detail.scope.text === "All" ? "" : event.detail.scope.text.toLowerCase();
327+
let scope = event.detail.scope.value === "all" ? "" : event.detail.scope.value;
328328

329329
searchScope.getSlottedNodes("items").forEach(item => {
330330
item.remove();

packages/fiori/test/pages/SearchField.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
</div>
3333
<div class="container" style="padding-top: 1rem; display: flex; flex-direction: column;">
3434
<ui5-label>Initially expanded fixed search with scope and placeholder:</ui5-label>
35-
<ui5-search-field accessible-name="Global Search" id="scoped-search" fixed expanded mode="Scoped" placeholder="Placeholder">
36-
<ui5-search-scope text="All" slot="scopes"></ui5-search-scope>
37-
<ui5-search-scope text="Apps" selected slot="scopes"></ui5-search-scope>
38-
<ui5-search-scope text="Products" slot="scopes"></ui5-search-scope>
35+
<ui5-search-field accessible-name="Global Search" id="scoped-search" scope-value="apps" fixed expanded mode="Scoped" placeholder="Placeholder">
36+
<ui5-search-scope text="All" value="all" slot="scopes"></ui5-search-scope>
37+
<ui5-search-scope text="Apps" value="apps" slot="scopes"></ui5-search-scope>
38+
<ui5-search-scope text="Products" value="products" slot="scopes"></ui5-search-scope>
3939
</ui5-search-field>
4040
</div>
4141
<ui5-label>Advanced filtering search</ui5-label>

packages/fiori/test/pages/ShellBar_evolution.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139

140140
<ui5-toggle-button slot="content" text="PR9" data-hide-order="9">PR9</ui5-toggle-button>
141141

142-
<ui5-shellbar-search slot="searchField">
142+
<ui5-shellbar-search slot="searchField" scope-value="apps">
143143
<ui5-search-item text="List Item" icon="history" ></ui5-search-item>
144144
<ui5-search-item text="List Item" icon="history" ></ui5-search-item>
145145
<ui5-search-item text="List Item" icon="history" ></ui5-search-item>
@@ -148,9 +148,9 @@
148148
<ui5-search-item text="List Item" icon="globe" ></ui5-search-item>
149149
<ui5-search-item text="List Item" icon="globe" ></ui5-search-item>
150150

151-
<ui5-search-scope text="All" slot="scopes"></ui5-search-scope>
152-
<ui5-search-scope text="Apps" selected slot="scopes"></ui5-search-scope>
153-
<ui5-search-scope text="Products" slot="scopes"></ui5-search-scope>
151+
<ui5-search-scope text="All" value="all" slot="scopes"></ui5-search-scope>
152+
<ui5-search-scope text="Apps" value="apps" slot="scopes"></ui5-search-scope>
153+
<ui5-search-scope text="Products" value="products" slot="scopes"></ui5-search-scope>
154154
</ui5-shellbar-search>
155155

156156
<ui5-avatar slot="profile" icon="customer"></ui5-avatar>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const searchScope = document.getElementById("search-scope");
3434
createScopeItems();
3535

3636
searchScope.addEventListener("ui5-scope-change", (event) => {
37-
const scope = event.detail.scope.text === "All" ? "" : event.detail.scope.text.toLowerCase();
37+
const scope = event.detail.scope.value === "all" ? "" : event.detail.scope.value;
3838

3939
searchScope.items.forEach(item => {
4040
item.remove();

packages/website/docs/_samples/fiori/Search/Basic/sample.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
<body style="background-color: var(--sapBackgroundColor); height: 350px">
1212
<!-- playground-fold-end -->
13-
<ui5-search id="search-scope" show-clear-icon placeholder="Search Apps, Products">
14-
<ui5-search-scope text="All" selected slot="scopes"></ui5-search-scope>
15-
<ui5-search-scope text="Apps" slot="scopes"></ui5-search-scope>
16-
<ui5-search-scope text="Products" slot="scopes"></ui5-search-scope>
13+
<ui5-search id="search-scope" scope-value="all" show-clear-icon placeholder="Search Apps, Products">
14+
<ui5-search-scope text="All" value="all" slot="scopes"></ui5-search-scope>
15+
<ui5-search-scope text="Apps" value="apps" slot="scopes"></ui5-search-scope>
16+
<ui5-search-scope text="Products" value="products" slot="scopes"></ui5-search-scope>
1717
</ui5-search>
1818
</div>
1919
<!-- playground-fold -->

packages/website/docs/_samples/patterns/UXCIntegration/Basic/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ const searchScope = document.getElementById("search-scope");
408408
createScopeItems();
409409

410410
searchScope.addEventListener("ui5-scope-change", (event) => {
411-
const scope = event.detail.scope.text === "All" ? "" : event.detail.scope.text.toLowerCase();
411+
const scope = event.detail.scope.value === "all" ? "" : event.detail.scope.value;
412412

413413
searchScope.items.forEach(item => {
414414
item.remove();

0 commit comments

Comments
 (0)