Skip to content

Commit 28c4181

Browse files
authored
feat(ui5-list): added new param for selectionChange event (#798)
1 parent 8420492 commit 28c4181

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

packages/main/src/List.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,15 @@ const metadata = {
167167
* @event
168168
* @param {Array} selectedItems an array of the selected items.
169169
* @param {Array} previouslySelectedItems an array of the previously selected items.
170+
* @param {Boolean} selectionComponentPressed a boolean indicating if the user used the selection components
171+
* in SingleSelection(ui5-radiobutton) and MultiSelection(ui5-checkbox) modes to change the selection.
170172
* @public
171173
*/
172174
selectionChange: {
173175
detail: {
174176
selectedItems: { type: Array },
175177
previouslySelectedItems: { type: Array },
178+
selectionComponentPressed: { type: Boolean },
176179
},
177180
},
178181
},
@@ -289,11 +292,15 @@ class List extends UI5Element {
289292
this._selectionRequested = true;
290293

291294
if (this[`handle${this.mode}`]) {
292-
selectionChange = this[`handle${this.mode}`](event.detail.item, event.selected);
295+
selectionChange = this[`handle${this.mode}`](event.detail.item, event.detail.selected);
293296
}
294297

295298
if (selectionChange) {
296-
this.fireEvent("selectionChange", { selectedItems: this.getSelectedItems(), previouslySelectedItems });
299+
this.fireEvent("selectionChange", {
300+
selectedItems: this.getSelectedItems(),
301+
previouslySelectedItems,
302+
selectionComponentPressed: event.detail.selectionComponentPressed,
303+
});
297304
}
298305
}
299306

@@ -448,8 +455,9 @@ class List extends UI5Element {
448455
this.onSelectionRequested({
449456
detail: {
450457
item: pressedItem,
458+
selectionComponentPressed: false,
459+
selected: !pressedItem.selected,
451460
},
452-
selected: !pressedItem.selected,
453461
});
454462
}
455463

packages/main/src/ListItem.hbs

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@
3333
<ui5-radiobutton
3434
id="{{_id}}-singleSelectionElement"
3535
class="ui5-li-singlesel-radiobtn"
36-
?selected="{{selected}}">
36+
?selected="{{selected}}"
37+
@click="{{onSingleSelectionComponentPress}}">
3738
</ui5-radiobutton>
3839
{{/if}}
3940

4041
{{#if modeMultiSelect}}
4142
<ui5-checkbox
4243
id="{{_id}}-multiSelectionElement"
4344
class="ui5-li-multisel-cb"
44-
?checked="{{selected}}">
45+
?checked="{{selected}}"
46+
@click="{{onMultiSelectionComponentPress}}">
4547
</ui5-checkbox>
4648
{{/if}}
4749

@@ -51,7 +53,7 @@
5153
id="{{_id}}-deleteSelectionElement"
5254
design="Transparent"
5355
icon="sap-icon://decline"
54-
@ui5-press="{{_onDelete}}"
56+
@ui5-press="{{onDelete}}"
5557
></ui5-button>
5658
</div>
5759
{{/if}}

packages/main/src/ListItem.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,26 @@ class ListItem extends ListItemBase {
182182
this.fireItemPress();
183183
}
184184

185+
/*
186+
* Called when selection components in Single (ui5-radiobutton)
187+
* and Multi (ui5-checkbox) selection modes are used.
188+
*/
189+
onMultiSelectionComponentPress(event) {
190+
this.fireEvent("_selectionRequested", { item: this, selected: !event.target.checked, selectionComponentPressed: true });
191+
}
192+
193+
onSingleSelectionComponentPress(event) {
194+
this.fireEvent("_selectionRequested", { item: this, selected: !event.target.selected, selectionComponentPressed: true });
195+
}
196+
185197
activate() {
186198
if (this.type === ListItemType.Active) {
187199
this.active = true;
188200
}
189201
}
190202

191-
_onDelete(event) {
192-
this.fireEvent("_selectionRequested", { item: this, selected: event.selected });
203+
onDelete(event) {
204+
this.fireEvent("_selectionRequested", { item: this, selectionComponentPressed: false });
193205
}
194206

195207
fireItemPress() {

packages/main/test/sap/ui/webcomponents/main/pages/List_test_page.html

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
<ui5-li image="./img/HT-2002.jpg" description="2GB RAM, Intel i7 4.5 GHz">DVD set</ui5-li>
4343
</ui5-list>
4444

45+
<ui5-list id="listMultiSel" mode="MultiSelect">
46+
<ui5-li id="option1">Option #1</ui5-li>
47+
<ui5-li>Option #2</ui5-li>
48+
<ui5-li>Option #3</ui5-li>
49+
</ui5-list>
50+
51+
<ui5-input id="fieldMultiSelResult"></ui5-input>
52+
4553
<script>
4654
'use strict';
4755

@@ -68,6 +76,10 @@
6876
listEvents.addEventListener("ui5-selectionChange", function(event) {
6977
selectionChangeResultPreviousItemsParameter.value = event.detail.previouslySelectedItems[0].id;
7078
})
79+
80+
listMultiSel.addEventListener("ui5-selectionChange", function(event) {
81+
fieldMultiSelResult.value = event.detail.selectionComponentPressed;
82+
})
7183
</script>
7284
</body>
7385
</html>

packages/main/test/specs/List.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ describe("Date Picker Tests", () => {
2828
assert.strictEqual(secondItem.getProperty("id"), selectionChangeResultPreviousItemsParameter.getProperty("value"));
2929
});
3030

31+
it("selectionChange using selection component", () => {
32+
const fieldResult = $("#fieldMultiSelResult");
33+
const firstItemSelectionComponent = $("#listMultiSel #option1").shadow$(".ui5-li-multisel-cb");
34+
35+
firstItemSelectionComponent.click();
36+
37+
assert.strictEqual(fieldResult.getProperty("value"), "true");
38+
});
39+
3140
it("header text", () => {
3241
list.id = "#list1";
3342

0 commit comments

Comments
 (0)