1
1
import { enrichEventWithDetails } from '@ui5/webcomponents-react-base/lib/Utils' ;
2
2
import { TableSelectionBehavior } from '@ui5/webcomponents-react/lib/TableSelectionBehavior' ;
3
3
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
- } ;
20
4
21
5
const tagNamesWhichShouldNotSelectARow = new Set ( [
22
6
'UI5-INPUT' ,
@@ -33,70 +17,68 @@ const tagNamesWhichShouldNotSelectARow = new Set([
33
17
'UI5-TOGGLEBUTTON'
34
18
] ) ;
35
19
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
+ }
39
36
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
+ }
49
42
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 ) {
53
45
return ;
54
46
}
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 ) ;
65
58
}
66
- } else {
67
- row . toggleRowSelected ( ) ;
68
59
}
60
+ instance . toggleRowSelected ( row . id ) ;
61
+
62
+ // fire event
69
63
if ( typeof onRowSelected === 'function' ) {
70
64
const payload = {
71
65
row,
72
- isSelected : ! row . isSelected
66
+ isSelected : ! row . isSelected ,
67
+ selectedFlatRows : ! row . isSelected ? [ row . id ] : [ ]
73
68
} ;
74
- const payloadWithFlatRows = {
75
- ...payload ,
76
- selectedFlatRows : ! row . isSelected
69
+ if ( selectionMode === TableSelectionMode . MULTI_SELECT ) {
70
+ payload . selectedFlatRows = ! row . isSelected
77
71
? [ ...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 ) ) ;
88
75
}
89
76
}
90
- } ,
91
- [ selectionMode , isTreeTable , dispatch , selectedFlatRows , onRowSelected , toggleRowSelected ]
92
- ) ;
93
-
94
- Object . assign ( instance , { selectSingleRow } ) ;
77
+ }
78
+ ] ;
95
79
} ;
96
80
97
81
export const useSingleRowStateSelection = ( hooks ) => {
98
- hooks . useInstance . push ( useInstance ) ;
99
- hooks . prepareRow . push ( prepareRow ) ;
100
82
hooks . getRowProps . push ( getRowProps ) ;
101
83
} ;
102
84
useSingleRowStateSelection . pluginName = 'useSingleRowStateSelection' ;
0 commit comments