5
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
6
*/
7
7
8
- import { FunctionComponent , useCallback , useMemo , useRef , useState } from 'react' ;
8
+ import { FunctionComponent , useCallback , useEffect , useMemo , useRef } 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' ;
15
16
16
17
export interface TableSelectionProps {
17
18
itemName : string ;
@@ -21,19 +22,12 @@ export interface TableSelectionProps {
21
22
}
22
23
23
24
const TableSelection : FunctionComponent < TableSelectionProps > = ( props ) => {
24
- const [ selectedRowsLength , setSelectedRowsLength ] = useState ( 0 ) ;
25
+ const { rowsSelection , onSelectionChanged : handleSelection , onFilterChanged } = useTableSelection < { id : string } > ( ) ;
25
26
const gridRef = useRef < AgGridReact > ( null ) ;
26
27
27
- const handleEquipmentSelectionChanged = useCallback ( ( ) => {
28
- const selectedRows = gridRef . current ?. api . getSelectedRows ( ) ;
29
- if ( selectedRows == null ) {
30
- setSelectedRowsLength ( 0 ) ;
31
- props . onSelectionChanged ( [ ] ) ;
32
- } else {
33
- setSelectedRowsLength ( selectedRows . length ) ;
34
- props . onSelectionChanged ( selectedRows . map ( ( r ) => r . id ) ) ;
35
- }
36
- } , [ props ] ) ;
28
+ useEffect ( ( ) => {
29
+ props . onSelectionChanged ( rowsSelection . map ( ( r ) => r . id ) ) ;
30
+ } , [ rowsSelection , props ] ) ;
37
31
38
32
const rowData = useMemo ( ( ) => {
39
33
return props . tableItems . map ( ( str ) => ( { id : str } ) ) ;
@@ -72,7 +66,7 @@ const TableSelection: FunctionComponent<TableSelectionProps> = (props) => {
72
66
< Grid item >
73
67
< Typography variant = "subtitle1" >
74
68
< FormattedMessage id = { props . itemName } > </ FormattedMessage >
75
- { ` (${ selectedRowsLength } / ${ rowData ?. length ?? 0 } )` }
69
+ { ` (${ rowsSelection ?. length } / ${ rowData ?. length ?? 0 } )` }
76
70
</ Typography >
77
71
</ Grid >
78
72
< Grid item xs >
@@ -84,7 +78,8 @@ const TableSelection: FunctionComponent<TableSelectionProps> = (props) => {
84
78
defaultColDef = { defaultColDef }
85
79
rowSelection = { defaultRowSelection }
86
80
getRowId = { getRowId }
87
- onSelectionChanged = { handleEquipmentSelectionChanged }
81
+ onSelectionChanged = { handleSelection }
82
+ onFilterChanged = { onFilterChanged }
88
83
onGridReady = { onGridReady }
89
84
/>
90
85
</ Grid >
0 commit comments