|
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 { 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 | 15 |
|
16 | | -export interface TableSelectionProps { |
17 | | - itemName: string; |
18 | | - tableItems: string[]; |
19 | | - tableSelectedItems?: string[]; |
20 | | - onSelectionChanged: (selectedItems: string[]) => void; |
| 16 | +/** |
| 17 | + * Generic props for TableSelection component. |
| 18 | + * @template TData - The type of data items in the table |
| 19 | + */ |
| 20 | +export interface TableSelectionProps<TData> { |
| 21 | + /** Translation key for the table title */ |
| 22 | + titleId: string; |
| 23 | + /** Array of data items to display */ |
| 24 | + items: TData[]; |
| 25 | + /** Function to extract the unique ID from each item (used for selection tracking) */ |
| 26 | + getItemId: (item: TData) => string; |
| 27 | + /** Column definitions for the AG Grid */ |
| 28 | + columnDefs: ColDef<TData>[]; |
| 29 | + /** Array of selected item IDs */ |
| 30 | + selectedIds?: string[]; |
| 31 | + /** Callback when selection changes, receives array of selected IDs */ |
| 32 | + onSelectionChanged: (selectedIds: string[]) => void; |
21 | 33 | } |
22 | 34 |
|
23 | 35 | const rowSelection = { |
24 | 36 | ...defaultRowSelection, |
25 | 37 | headerCheckbox: false, |
26 | 38 | }; |
27 | 39 |
|
28 | | -const TableSelection: FunctionComponent<TableSelectionProps> = (props) => { |
29 | | - const [selectedRowsLength, setSelectedRowsLength] = useState(0); |
30 | | - const gridRef = useRef<AgGridReact>(null); |
| 40 | +/** |
| 41 | + * A generic table component with row selection support. |
| 42 | + * Displays data in an AG Grid with checkboxes for selection. |
| 43 | + */ |
| 44 | +function TableSelection<TData>({ |
| 45 | + titleId, |
| 46 | + items, |
| 47 | + getItemId, |
| 48 | + columnDefs, |
| 49 | + selectedIds, |
| 50 | + onSelectionChanged, |
| 51 | +}: Readonly<TableSelectionProps<TData>>) { |
| 52 | + const [selectedCount, setSelectedCount] = useState(0); |
| 53 | + const gridRef = useRef<AgGridReact<TData>>(null); |
| 54 | + |
| 55 | + const getRowId = useCallback( |
| 56 | + (params: GetRowIdParams<TData>): string => { |
| 57 | + return getItemId(params.data); |
| 58 | + }, |
| 59 | + [getItemId] |
| 60 | + ); |
31 | 61 |
|
32 | | - const handleEquipmentSelectionChanged = useCallback(() => { |
| 62 | + const handleSelectionChanged = useCallback(() => { |
33 | 63 | const selectedRows = gridRef.current?.api.getSelectedRows(); |
34 | 64 | if (selectedRows == null) { |
35 | | - setSelectedRowsLength(0); |
36 | | - props.onSelectionChanged([]); |
| 65 | + setSelectedCount(0); |
| 66 | + onSelectionChanged([]); |
37 | 67 | } else { |
38 | | - setSelectedRowsLength(selectedRows.length); |
39 | | - props.onSelectionChanged(selectedRows.map((r) => r.id)); |
| 68 | + setSelectedCount(selectedRows.length); |
| 69 | + onSelectionChanged(selectedRows.map(getItemId)); |
40 | 70 | } |
41 | | - }, [props]); |
42 | | - |
43 | | - const rowData = useMemo(() => { |
44 | | - return props.tableItems.map((str) => ({ id: str })); |
45 | | - }, [props.tableItems]); |
| 71 | + }, [onSelectionChanged, getItemId]); |
46 | 72 |
|
47 | | - const columnDefs = useMemo( |
48 | | - (): ColDef[] => [ |
49 | | - { |
50 | | - field: 'id', |
51 | | - filter: true, |
52 | | - initialSort: 'asc', |
53 | | - tooltipField: 'id', |
54 | | - flex: 1, |
55 | | - }, |
56 | | - ], |
57 | | - [] |
58 | | - ); |
59 | | - |
60 | | - function getRowId(params: GetRowIdParams): string { |
61 | | - return params.data.id; |
62 | | - } |
63 | | - |
64 | | - const onGridReady = useCallback( |
65 | | - ({ api }: GridReadyEvent) => { |
66 | | - api?.forEachNode((n) => { |
67 | | - if (props.tableSelectedItems !== undefined && n.id && props.tableSelectedItems.includes(n.id)) { |
68 | | - n.setSelected(true); |
| 73 | + const handleGridReady = useCallback( |
| 74 | + ({ api }: GridReadyEvent<TData>) => { |
| 75 | + if (!selectedIds?.length) { |
| 76 | + return; |
| 77 | + } |
| 78 | + api.forEachNode((node) => { |
| 79 | + const nodeId = node.id; |
| 80 | + if (nodeId && selectedIds.includes(nodeId)) { |
| 81 | + node.setSelected(true); |
69 | 82 | } |
70 | 83 | }); |
71 | 84 | }, |
72 | | - [props.tableSelectedItems] |
| 85 | + [selectedIds] |
73 | 86 | ); |
74 | 87 |
|
| 88 | + const mergedColumnDefs = useMemo((): ColDef<TData>[] => { |
| 89 | + return columnDefs.map((col, index) => ({ |
| 90 | + filter: true, |
| 91 | + flex: 1, |
| 92 | + ...col, |
| 93 | + // First column gets initial sort if not specified elsewhere |
| 94 | + ...(index === 0 && !columnDefs.some((c) => c.initialSort) ? { initialSort: 'asc' as const } : {}), |
| 95 | + })); |
| 96 | + }, [columnDefs]); |
| 97 | + |
75 | 98 | return ( |
76 | | - <Grid item container direction={'column'} style={{ height: '100%' }}> |
| 99 | + <Grid item container direction="column" style={{ height: '100%' }}> |
77 | 100 | <Grid item> |
78 | 101 | <Typography variant="subtitle1"> |
79 | | - <FormattedMessage id={props.itemName}></FormattedMessage> |
80 | | - {` (${selectedRowsLength} / ${rowData?.length ?? 0})`} |
| 102 | + <FormattedMessage id={titleId} /> |
| 103 | + {` (${selectedCount} / ${items.length})`} |
81 | 104 | </Typography> |
82 | 105 | </Grid> |
83 | 106 | <Grid item xs> |
84 | 107 | <CustomAGGrid |
85 | 108 | gridId="table-selection" |
86 | 109 | ref={gridRef} |
87 | | - rowData={rowData} |
88 | | - columnDefs={columnDefs} |
| 110 | + rowData={items} |
| 111 | + columnDefs={mergedColumnDefs} |
89 | 112 | defaultColDef={defaultColDef} |
90 | 113 | rowSelection={rowSelection} |
91 | 114 | getRowId={getRowId} |
92 | | - onSelectionChanged={handleEquipmentSelectionChanged} |
93 | | - onGridReady={onGridReady} |
94 | | - accentedSort={true} |
| 115 | + onSelectionChanged={handleSelectionChanged} |
| 116 | + onGridReady={handleGridReady} |
| 117 | + accentedSort |
95 | 118 | /> |
96 | 119 | </Grid> |
97 | 120 | </Grid> |
98 | 121 | ); |
99 | | -}; |
| 122 | +} |
| 123 | + |
100 | 124 | export default TableSelection; |
0 commit comments