Skip to content

Commit 69e63d9

Browse files
fix(AnalyticalTable): fix subRows selection in tree table mode (#610)
1 parent cb99e78 commit 69e63d9

File tree

3 files changed

+55
-73
lines changed

3 files changed

+55
-73
lines changed
Lines changed: 46 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
import { enrichEventWithDetails } from '@ui5/webcomponents-react-base/lib/Utils';
22
import { TableSelectionBehavior } from '@ui5/webcomponents-react/lib/TableSelectionBehavior';
33
import { TableSelectionMode } from '@ui5/webcomponents-react/lib/TableSelectionMode';
4-
import { useCallback } from 'react';
5-
6-
const prepareRow = (row, { instance }) => {
7-
row.selectSingleRow = (event, selectionCellClick = false) => {
8-
instance.selectSingleRow(row, event, selectionCellClick);
9-
};
10-
};
11-
12-
const getRowProps = (rowProps, { row }) => {
13-
return [
14-
rowProps,
15-
{
16-
onClick: row.selectSingleRow
17-
}
18-
];
19-
};
204

215
const tagNamesWhichShouldNotSelectARow = new Set([
226
'UI5-INPUT',
@@ -33,70 +17,68 @@ const tagNamesWhichShouldNotSelectARow = new Set([
3317
'UI5-TOGGLEBUTTON'
3418
]);
3519

36-
const useInstance = (instance) => {
37-
const { webComponentsReactProperties, dispatch, toggleRowSelected, selectedFlatRows } = instance;
38-
const { isTreeTable, selectionMode, onRowSelected, selectionBehavior } = webComponentsReactProperties;
20+
const getRowProps = (rowProps, { row, instance }) => {
21+
const { webComponentsReactProperties, toggleRowSelected, selectedFlatRows } = instance;
22+
if (webComponentsReactProperties.selectionMode === TableSelectionMode.NONE) {
23+
return rowProps;
24+
}
25+
return [
26+
rowProps,
27+
{
28+
onClick: (e, selectionCellClick = false) => {
29+
if (
30+
e.target?.dataset?.name !== 'internal_selection_column' &&
31+
!(e.markerAllowTableRowSelection === true || e.nativeEvent?.markerAllowTableRowSelection === true) &&
32+
tagNamesWhichShouldNotSelectARow.has(e.target.tagName)
33+
) {
34+
return;
35+
}
3936

40-
const selectSingleRow = useCallback(
41-
(row, e, selectionCellClick = false) => {
42-
if (
43-
e.target?.dataset?.name !== 'internal_selection_column' &&
44-
!(e.markerAllowTableRowSelection === true || e.nativeEvent?.markerAllowTableRowSelection === true) &&
45-
tagNamesWhichShouldNotSelectARow.has(e.target.tagName)
46-
) {
47-
return;
48-
}
37+
// dont select empty rows
38+
const isEmptyRow = row.original?.emptyRow;
39+
if (isEmptyRow) {
40+
return;
41+
}
4942

50-
const isEmptyRow = row.original?.emptyRow;
51-
if ([TableSelectionMode.SINGLE_SELECT, TableSelectionMode.MULTI_SELECT].includes(selectionMode) && !isEmptyRow) {
52-
if (row.isGrouped || (TableSelectionBehavior.ROW_SELECTOR === selectionBehavior && !selectionCellClick)) {
43+
// dont select grouped rows
44+
if (row.isGrouped) {
5345
return;
5446
}
55-
if (isTreeTable) {
56-
if (selectionMode === TableSelectionMode.MULTI_SELECT) {
57-
dispatch({
58-
type: 'SET_SELECTED_ROWS',
59-
selectedIds: Object.assign({}, ...selectedFlatRows.map((item) => ({ [item.id]: true })), {
60-
[row.id]: !row.isSelected
61-
})
62-
});
63-
} else {
64-
dispatch({ type: 'SET_SELECTED_ROWS', selectedIds: { [row.id]: !row.isSelected } });
47+
48+
const { selectionBehavior, selectionMode, onRowSelected } = webComponentsReactProperties;
49+
50+
// dont continue if the row was clicked and selection mode is row selector only
51+
if (selectionBehavior === TableSelectionBehavior.ROW_SELECTOR && !selectionCellClick) {
52+
return;
53+
}
54+
55+
if (selectionMode === TableSelectionMode.SINGLE_SELECT) {
56+
for (const row of selectedFlatRows) {
57+
toggleRowSelected(row.id, false);
6558
}
66-
} else {
67-
row.toggleRowSelected();
6859
}
60+
instance.toggleRowSelected(row.id);
61+
62+
// fire event
6963
if (typeof onRowSelected === 'function') {
7064
const payload = {
7165
row,
72-
isSelected: !row.isSelected
66+
isSelected: !row.isSelected,
67+
selectedFlatRows: !row.isSelected ? [row.id] : []
7368
};
74-
const payloadWithFlatRows = {
75-
...payload,
76-
selectedFlatRows: !row.isSelected
69+
if (selectionMode === TableSelectionMode.MULTI_SELECT) {
70+
payload.selectedFlatRows = !row.isSelected
7771
? [...selectedFlatRows, row]
78-
: selectedFlatRows.filter((prevRow) => prevRow.id !== row.id)
79-
};
80-
onRowSelected(
81-
enrichEventWithDetails(e, TableSelectionMode.MULTI_SELECT === selectionMode ? payloadWithFlatRows : payload)
82-
);
83-
}
84-
if (selectionMode === TableSelectionMode.SINGLE_SELECT && !isTreeTable) {
85-
selectedFlatRows.forEach(({ id }) => {
86-
toggleRowSelected(id, false);
87-
});
72+
: selectedFlatRows.filter((prevRow) => prevRow.id !== row.id);
73+
}
74+
onRowSelected(enrichEventWithDetails(e, payload));
8875
}
8976
}
90-
},
91-
[selectionMode, isTreeTable, dispatch, selectedFlatRows, onRowSelected, toggleRowSelected]
92-
);
93-
94-
Object.assign(instance, { selectSingleRow });
77+
}
78+
];
9579
};
9680

9781
export const useSingleRowStateSelection = (hooks) => {
98-
hooks.useInstance.push(useInstance);
99-
hooks.prepareRow.push(prepareRow);
10082
hooks.getRowProps.push(getRowProps);
10183
};
10284
useSingleRowStateSelection.pluginName = 'useSingleRowStateSelection';

packages/main/src/components/AnalyticalTable/index.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ const AnalyticalTable: FC<TableProps> = forwardRef((props: TableProps, ref: Ref<
193193
setColumnOrder,
194194
dispatch,
195195
totalColumnsWidth,
196-
selectedFlatRows
196+
toggleRowSelected,
197+
toggleAllRowsSelected
197198
} = useTable(
198199
{
199200
columns,
@@ -204,7 +205,8 @@ const AnalyticalTable: FC<TableProps> = forwardRef((props: TableProps, ref: Ref<
204205
stateReducer,
205206
disableFilters: !filterable,
206207
disableSortBy: !sortable,
207-
disableGroupBy: !groupable,
208+
disableGroupBy: isTreeTable ? true : !groupable,
209+
selectSubRows: false,
208210
webComponentsReactProperties: {
209211
tableRef,
210212
selectionMode,
@@ -266,8 +268,11 @@ const AnalyticalTable: FC<TableProps> = forwardRef((props: TableProps, ref: Ref<
266268
}, [groupBy, dispatch]);
267269

268270
useEffect(() => {
269-
dispatch({ type: 'SET_SELECTED_ROWS', selectedIds: selectedRowIds });
270-
}, [selectedRowIds, dispatch]);
271+
toggleAllRowsSelected(false);
272+
for (const row in selectedRowIds) {
273+
toggleRowSelected(row, selectedRowIds[row]);
274+
}
275+
}, [toggleRowSelected, toggleAllRowsSelected, selectedRowIds]);
271276

272277
const calcRowHeight = parseInt(
273278
getComputedStyle(tableRef.current ?? document.body).getPropertyValue('--sapWcrAnalyticalTableRowHeight') || '44'

packages/main/src/components/AnalyticalTable/tableReducer/stateReducer.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ export const stateReducer = (newState, action) => {
88
return newState;
99
}
1010
return { ...newState, groupBy: payload };
11-
case 'SET_SELECTED_ROWS':
12-
if (deepCompare(newState.selectedRowIds, action.selectedIds)) {
13-
return newState;
14-
}
15-
return { ...newState, selectedRowIds: action.selectedIds };
1611
case 'TABLE_RESIZE':
1712
return { ...newState, tableClientWidth: payload.tableClientWidth };
1813
case 'TABLE_SCROLLING_ENABLED':

0 commit comments

Comments
 (0)