Skip to content

Commit 534e64c

Browse files
authored
fix(ui5-table): register capture individually for selection (#11899)
* fix(ui5-table): register capture individually for selection * fix: remove captures for keyup/down * chore: adjust event registration
1 parent 1a28847 commit 534e64c

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

packages/main/src/Table.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ class Table extends UI5Element {
419419
}
420420

421421
onEnterDOM() {
422-
this._events.forEach(eventType => this.addEventListener(eventType, this._onEventBound, { capture: true }));
422+
this._events.forEach(eventType => this.addEventListener(eventType, this._onEventBound));
423423
this.features.forEach(feature => feature.onTableActivate?.(this));
424424
this._tableNavigation = new TableNavigation(this);
425425
this._tableDragAndDrop = new TableDragAndDrop(this);

packages/main/src/TableSelection.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ class TableSelection extends UI5Element implements ITableFeature {
8888
_rowsLength = 0;
8989
_rangeSelection?: {selected: boolean, isUp: boolean | null, rows: TableRow[], isMouse: boolean, shiftPressed: boolean} | null;
9090

91+
onClickCaptureBound: (e: MouseEvent) => void;
92+
93+
constructor() {
94+
super();
95+
this.onClickCaptureBound = this._onClickCapture.bind(this);
96+
}
97+
9198
onTableActivate(table: Table) {
9299
this._table = table;
93100
this._invalidateTableAndRows();
@@ -108,6 +115,12 @@ class TableSelection extends UI5Element implements ITableFeature {
108115
this._rowsLength = this._table.rows.length;
109116
this._table.headerRow[0]._invalidate++;
110117
}
118+
119+
this._table?.removeEventListener("click", this.onClickCaptureBound);
120+
}
121+
122+
onTableAfterRendering(): void {
123+
this._table?.addEventListener("click", this.onClickCaptureBound, { capture: true });
111124
}
112125

113126
isSelectable(): boolean {
@@ -272,7 +285,7 @@ class TableSelection extends UI5Element implements ITableFeature {
272285
}
273286
}
274287

275-
_onclick(e: MouseEvent) {
288+
_onClickCapture(e: MouseEvent) {
276289
if (!this._table || this.mode !== TableSelectionMode.Multiple) {
277290
return;
278291
}
@@ -294,11 +307,13 @@ class TableSelection extends UI5Element implements ITableFeature {
294307
const startIndex = this._table.rows.indexOf(startRow);
295308
const endIndex = this._table.rows.indexOf(row);
296309

310+
const selectionState = this.isSelected(startRow);
311+
297312
// When doing a range selection and clicking on an already selected row, the checked status should not change
298313
// Therefore, we need to manually set the checked attribute again, as clicking it would deselect it and leads to
299314
// a visual inconsistency.
300-
row.shadowRoot?.querySelector("#selection-component")?.toggleAttribute("checked", true);
301-
e.stopImmediatePropagation();
315+
row.shadowRoot?.querySelector("#selection-component")?.toggleAttribute("checked", selectionState);
316+
e.stopPropagation();
302317

303318
if (startIndex === -1 || endIndex === -1 || row.rowKey === startRow.rowKey || row.rowKey === this._rangeSelection.rows[this._rangeSelection.rows.length - 1].rowKey) {
304319
return;

packages/main/src/TableSelectionMulti.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,24 @@ class TableSelectionMulti extends TableSelectionBase {
6767
shiftPressed: boolean
6868
} | null;
6969

70+
_onClickCaptureBound: (e: MouseEvent) => void;
71+
72+
constructor() {
73+
super();
74+
this._onClickCaptureBound = this._onclickCapture.bind(this);
75+
}
76+
7077
onTableBeforeRendering() {
7178
if (this._table && this._table.headerRow[0] && this._rowsLength !== this._table.rows.length) {
7279
this._rowsLength = this._table.rows.length;
7380
this._table.headerRow[0]._invalidate++;
7481
}
82+
83+
this._table?.removeEventListener("click", this._onClickCaptureBound);
84+
}
85+
86+
onTableAfterRendering() {
87+
this._table?.addEventListener("click", this._onClickCaptureBound, { capture: true });
7588
}
7689

7790
isMultiSelectable(): boolean {
@@ -197,7 +210,7 @@ class TableSelectionMulti extends TableSelectionBase {
197210
}
198211
}
199212

200-
_onclick(e: MouseEvent) {
213+
_onclickCapture(e: MouseEvent) {
201214
if (!this._table) {
202215
return;
203216
}
@@ -219,11 +232,14 @@ class TableSelectionMulti extends TableSelectionBase {
219232
const startIndex = this._table.rows.indexOf(startRow);
220233
const endIndex = this._table.rows.indexOf(row);
221234

235+
// Set checkbox to the selection state of the start row (if it is selected)
236+
const selectionState = this.isSelected(startRow);
237+
222238
// When doing a range selection and clicking on an already selected row, the checked status should not change
223239
// Therefore, we need to manually set the checked attribute again, as clicking it would deselect it and leads to
224240
// a visual inconsistency.
225-
row.shadowRoot?.querySelector("#selection-component")?.toggleAttribute("checked", true);
226-
e.stopImmediatePropagation();
241+
row.shadowRoot?.querySelector("#selection-component")?.toggleAttribute("checked", selectionState);
242+
e.stopPropagation();
227243

228244
if (startIndex === -1 || endIndex === -1 || row.rowKey === startRow.rowKey || row.rowKey === this._rangeSelection.rows[this._rangeSelection.rows.length - 1].rowKey) {
229245
return;

0 commit comments

Comments
 (0)