5
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
6
*/
7
7
8
- import { FunctionComponent , useCallback , useEffect , useMemo , useRef } from 'react' ;
8
+ import { FunctionComponent , useCallback , useMemo , useRef , useState } from 'react' ;
9
9
import { FormattedMessage } from 'react-intl' ;
10
10
import { CustomAGGrid } from '@gridsuite/commons-ui' ;
11
11
import { Grid , Typography } from '@mui/material' ;
12
12
import { AgGridReact } from 'ag-grid-react' ;
13
13
import { ColDef , GetRowIdParams , GridReadyEvent } from 'ag-grid-community' ;
14
14
import { defaultColDef , defaultRowSelection } from './table-config' ;
15
- import { useTableSelection } from '../../utils/hooks' ;
16
15
17
16
export interface TableSelectionProps {
18
17
itemName : string ;
@@ -21,13 +20,25 @@ export interface TableSelectionProps {
21
20
onSelectionChanged : ( selectedItems : string [ ] ) => void ;
22
21
}
23
22
23
+ const rowSelection = {
24
+ ...defaultRowSelection ,
25
+ headerCheckbox : false ,
26
+ } ;
27
+
24
28
const TableSelection : FunctionComponent < TableSelectionProps > = ( props ) => {
25
- const { rowsSelection , onSelectionChanged : handleSelection , onFilterChanged } = useTableSelection < { id : string } > ( ) ;
29
+ const [ selectedRowsLength , setSelectedRowsLength ] = useState ( 0 ) ;
26
30
const gridRef = useRef < AgGridReact > ( null ) ;
27
31
28
- useEffect ( ( ) => {
29
- props . onSelectionChanged ( rowsSelection . map ( ( r ) => r . id ) ) ;
30
- } , [ rowsSelection , props ] ) ;
32
+ const handleEquipmentSelectionChanged = useCallback ( ( ) => {
33
+ const selectedRows = gridRef . current ?. api . getSelectedRows ( ) ;
34
+ if ( selectedRows == null ) {
35
+ setSelectedRowsLength ( 0 ) ;
36
+ props . onSelectionChanged ( [ ] ) ;
37
+ } else {
38
+ setSelectedRowsLength ( selectedRows . length ) ;
39
+ props . onSelectionChanged ( selectedRows . map ( ( r ) => r . id ) ) ;
40
+ }
41
+ } , [ props ] ) ;
31
42
32
43
const rowData = useMemo ( ( ) => {
33
44
return props . tableItems . map ( ( str ) => ( { id : str } ) ) ;
@@ -66,7 +77,7 @@ const TableSelection: FunctionComponent<TableSelectionProps> = (props) => {
66
77
< Grid item >
67
78
< Typography variant = "subtitle1" >
68
79
< FormattedMessage id = { props . itemName } > </ FormattedMessage >
69
- { ` (${ rowsSelection ?. length } / ${ rowData ?. length ?? 0 } )` }
80
+ { ` (${ selectedRowsLength } / ${ rowData ?. length ?? 0 } )` }
70
81
</ Typography >
71
82
</ Grid >
72
83
< Grid item xs >
@@ -76,10 +87,9 @@ const TableSelection: FunctionComponent<TableSelectionProps> = (props) => {
76
87
rowData = { rowData }
77
88
columnDefs = { columnDefs }
78
89
defaultColDef = { defaultColDef }
79
- rowSelection = { defaultRowSelection }
90
+ rowSelection = { rowSelection }
80
91
getRowId = { getRowId }
81
- onSelectionChanged = { handleSelection }
82
- onFilterChanged = { onFilterChanged }
92
+ onSelectionChanged = { handleEquipmentSelectionChanged }
83
93
onGridReady = { onGridReady }
84
94
accentedSort = { true }
85
95
/>
0 commit comments