Skip to content

Commit a62feae

Browse files
committed
Fix
Signed-off-by: Ayoub LABIDI <[email protected]>
1 parent 0a0683c commit a62feae

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/pages/common/table-selection.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { FunctionComponent, useCallback, useEffect, useMemo, useRef } from 'react';
8+
import { FunctionComponent, useCallback, useMemo, useRef, useState } from 'react';
99
import { FormattedMessage } from 'react-intl';
1010
import { CustomAGGrid } from '@gridsuite/commons-ui';
1111
import { Grid, Typography } from '@mui/material';
1212
import { AgGridReact } from 'ag-grid-react';
1313
import { ColDef, GetRowIdParams, GridReadyEvent } from 'ag-grid-community';
1414
import { defaultColDef, defaultRowSelection } from './table-config';
15-
import { useTableSelection } from '../../utils/hooks';
1615

1716
export interface TableSelectionProps {
1817
itemName: string;
@@ -21,13 +20,25 @@ export interface TableSelectionProps {
2120
onSelectionChanged: (selectedItems: string[]) => void;
2221
}
2322

23+
const rowSelection = {
24+
...defaultRowSelection,
25+
headerCheckbox: false,
26+
};
27+
2428
const TableSelection: FunctionComponent<TableSelectionProps> = (props) => {
25-
const { rowsSelection, onSelectionChanged: handleSelection, onFilterChanged } = useTableSelection<{ id: string }>();
29+
const [selectedRowsLength, setSelectedRowsLength] = useState(0);
2630
const gridRef = useRef<AgGridReact>(null);
2731

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]);
3142

3243
const rowData = useMemo(() => {
3344
return props.tableItems.map((str) => ({ id: str }));
@@ -66,7 +77,7 @@ const TableSelection: FunctionComponent<TableSelectionProps> = (props) => {
6677
<Grid item>
6778
<Typography variant="subtitle1">
6879
<FormattedMessage id={props.itemName}></FormattedMessage>
69-
{` (${rowsSelection?.length} / ${rowData?.length ?? 0})`}
80+
{` (${selectedRowsLength} / ${rowData?.length ?? 0})`}
7081
</Typography>
7182
</Grid>
7283
<Grid item xs>
@@ -76,10 +87,9 @@ const TableSelection: FunctionComponent<TableSelectionProps> = (props) => {
7687
rowData={rowData}
7788
columnDefs={columnDefs}
7889
defaultColDef={defaultColDef}
79-
rowSelection={defaultRowSelection}
90+
rowSelection={rowSelection}
8091
getRowId={getRowId}
81-
onSelectionChanged={handleSelection}
82-
onFilterChanged={onFilterChanged}
92+
onSelectionChanged={handleEquipmentSelectionChanged}
8393
onGridReady={onGridReady}
8494
accentedSort={true}
8595
/>

0 commit comments

Comments
 (0)