Skip to content

Commit 6d5e3d1

Browse files
committed
fixes
Signed-off-by: Ayoub LABIDI <[email protected]>
1 parent 4b23874 commit 6d5e3d1

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

src/pages/common/table-selection.tsx

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

8-
import { FunctionComponent, useCallback, useMemo, useRef, useState } from 'react';
8+
import { FunctionComponent, useCallback, useEffect, useMemo, useRef } 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';
1516

1617
export interface TableSelectionProps {
1718
itemName: string;
@@ -21,19 +22,12 @@ export interface TableSelectionProps {
2122
}
2223

2324
const TableSelection: FunctionComponent<TableSelectionProps> = (props) => {
24-
const [selectedRowsLength, setSelectedRowsLength] = useState(0);
25+
const { rowsSelection, onSelectionChanged: handleSelection, onFilterChanged } = useTableSelection<{ id: string }>();
2526
const gridRef = useRef<AgGridReact>(null);
2627

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

3832
const rowData = useMemo(() => {
3933
return props.tableItems.map((str) => ({ id: str }));
@@ -72,7 +66,7 @@ const TableSelection: FunctionComponent<TableSelectionProps> = (props) => {
7266
<Grid item>
7367
<Typography variant="subtitle1">
7468
<FormattedMessage id={props.itemName}></FormattedMessage>
75-
{` (${selectedRowsLength} / ${rowData?.length ?? 0})`}
69+
{` (${rowsSelection?.length} / ${rowData?.length ?? 0})`}
7670
</Typography>
7771
</Grid>
7872
<Grid item xs>
@@ -84,7 +78,8 @@ const TableSelection: FunctionComponent<TableSelectionProps> = (props) => {
8478
defaultColDef={defaultColDef}
8579
rowSelection={defaultRowSelection}
8680
getRowId={getRowId}
87-
onSelectionChanged={handleEquipmentSelectionChanged}
81+
onSelectionChanged={handleSelection}
82+
onFilterChanged={onFilterChanged}
8883
onGridReady={onGridReady}
8984
/>
9085
</Grid>

src/pages/groups/groups-table.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const GroupsTable: FunctionComponent<GroupsTableProps> = (props) => {
2727
const intl = useIntl();
2828
const { snackError } = useSnackMessage();
2929

30-
const { rowsSelection, onSelectionChanged } = useTableSelection<GroupInfos>();
30+
const { rowsSelection, onSelectionChanged, onFilterChanged } = useTableSelection<GroupInfos>();
3131
const [showDeletionDialog, setShowDeletionDialog] = useState(false);
3232

3333
function getRowId(params: GetRowIdParams<GroupInfos>): string {
@@ -110,6 +110,7 @@ const GroupsTable: FunctionComponent<GroupsTableProps> = (props) => {
110110
rowSelection={defaultRowSelection}
111111
onRowClicked={props.onRowClicked}
112112
onSelectionChanged={onSelectionChanged}
113+
onFilterChanged={onFilterChanged}
113114
>
114115
<GridButton
115116
labelId="groups.table.toolbar.add.label"

src/pages/profiles/profiles-table.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const ProfilesTable: FunctionComponent<ProfilesTableProps> = (props) => {
2727
const intl = useIntl();
2828
const { snackError } = useSnackMessage();
2929

30-
const { rowsSelection, onSelectionChanged } = useTableSelection<UserProfile>();
30+
const { rowsSelection, onSelectionChanged, onFilterChanged } = useTableSelection<UserProfile>();
3131
const [showDeletionDialog, setShowDeletionDialog] = useState(false);
3232

3333
function getRowId(params: GetRowIdParams<UserProfile>): string {
@@ -125,6 +125,7 @@ const ProfilesTable: FunctionComponent<ProfilesTableProps> = (props) => {
125125
rowSelection={defaultRowSelection}
126126
onRowClicked={props.onRowClicked}
127127
onSelectionChanged={onSelectionChanged}
128+
onFilterChanged={onFilterChanged}
128129
>
129130
<GridButton
130131
labelId="profiles.table.toolbar.add.label"

src/pages/users/users-table.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const UsersTable: FunctionComponent<UsersTableProps> = (props) => {
3333
const intl = useIntl();
3434
const { snackError } = useSnackMessage();
3535

36-
const { rowsSelection, onSelectionChanged } = useTableSelection<UserInfos>();
36+
const { rowsSelection, onSelectionChanged, onFilterChanged } = useTableSelection<UserInfos>();
3737
const [showDeletionDialog, setShowDeletionDialog] = useState(false);
3838

3939
function getRowId(params: GetRowIdParams<UserInfos>): string {
@@ -142,6 +142,7 @@ const UsersTable: FunctionComponent<UsersTableProps> = (props) => {
142142
rowSelection={defaultRowSelection}
143143
onRowClicked={props.onRowClicked}
144144
onSelectionChanged={onSelectionChanged}
145+
onFilterChanged={onFilterChanged}
145146
tooltipShowDelay={1000}
146147
>
147148
<GridButton

src/utils/hooks.ts

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

8-
import { SelectionChangedEvent } from "ag-grid-community";
9-
import { useCallback, useState } from "react";
8+
import { FilterChangedEvent, GridApi, IRowNode, SelectionChangedEvent } from 'ag-grid-community';
9+
import { useCallback, useState } from 'react';
1010

1111
export function useDebugRender(label: string) {
1212
// uncomment when you want the output in the console
@@ -19,21 +19,35 @@ export function useDebugRender(label: string) {
1919

2020
/**
2121
* Custom hook to handle table row selection with proper filtering support
22-
* @returns Selection state and handler for AG Grid's onSelectionChanged
22+
* @returns Selection state and handlers for AG Grid's selection and filter changes
2323
*/
2424
export function useTableSelection<T>() {
2525
const [rowsSelection, setRowsSelection] = useState<T[]>([]);
2626

27-
const onSelectionChanged = useCallback((event: SelectionChangedEvent<T, {}>) => {
28-
// Get only selected rows that are currently visible (after filtering)
27+
// update visible selections based on current filter state
28+
const updateVisibleSelection = useCallback((api: GridApi) => {
2929
const visibleSelectedRows: T[] = [];
30-
event.api.forEachNodeAfterFilterAndSort((node) => {
30+
api.forEachNodeAfterFilterAndSort((node: IRowNode) => {
3131
if (node.isSelected() && node.data) {
3232
visibleSelectedRows.push(node.data);
3333
}
3434
});
3535
setRowsSelection(visibleSelectedRows);
3636
}, []);
3737

38-
return { rowsSelection, setRowsSelection, onSelectionChanged };
38+
const onSelectionChanged = useCallback(
39+
(event: SelectionChangedEvent) => {
40+
updateVisibleSelection(event.api);
41+
},
42+
[updateVisibleSelection]
43+
);
44+
45+
const onFilterChanged = useCallback(
46+
(event: FilterChangedEvent) => {
47+
updateVisibleSelection(event.api);
48+
},
49+
[updateVisibleSelection]
50+
);
51+
52+
return { rowsSelection, onSelectionChanged, onFilterChanged };
3953
}

0 commit comments

Comments
 (0)