11import { enrichEventWithDetails } from '@ui5/webcomponents-react-base/lib/Utils' ;
22import { TableSelectionBehavior } from '@ui5/webcomponents-react/lib/TableSelectionBehavior' ;
33import { 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
215const 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
9781export const useSingleRowStateSelection = ( hooks ) => {
98- hooks . useInstance . push ( useInstance ) ;
99- hooks . prepareRow . push ( prepareRow ) ;
10082 hooks . getRowProps . push ( getRowProps ) ;
10183} ;
10284useSingleRowStateSelection . pluginName = 'useSingleRowStateSelection' ;
0 commit comments