|
| 1 | +/** |
| 2 | + * Copyright (c) 2024, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +import { defaultColumnDefinition } from './utils/directory-content-utils'; |
| 9 | +import { |
| 10 | + CustomAGGrid, |
| 11 | + ElementType, |
| 12 | + ElementAttributes, |
| 13 | +} from '@gridsuite/commons-ui'; |
| 14 | +import { AgGridReact } from 'ag-grid-react'; |
| 15 | +import { GetRowIdParams } from 'ag-grid-community/dist/types/core/interfaces/iCallbackParams'; |
| 16 | +import { ColDef, GridReadyEvent, RowClassParams } from 'ag-grid-community'; |
| 17 | +import { RefObject } from 'react'; |
| 18 | + |
| 19 | +interface DirectoryContentTableProps { |
| 20 | + gridRef: RefObject<AgGridReact<ElementAttributes>>; |
| 21 | + rows: ElementAttributes[]; |
| 22 | + handleCellContextualMenu: () => void; |
| 23 | + handleRowSelected: () => void; |
| 24 | + handleCellClick: () => void; |
| 25 | + colDef: ColDef[]; |
| 26 | +} |
| 27 | + |
| 28 | +const onGridReady = ({ api }: GridReadyEvent<ElementAttributes>) => { |
| 29 | + api?.sizeColumnsToFit(); |
| 30 | +}; |
| 31 | + |
| 32 | +const getRowId = (params: GetRowIdParams<ElementAttributes>) => |
| 33 | + params.data?.elementUuid; |
| 34 | + |
| 35 | +export const CUSTOM_ROW_CLASS = 'custom-row-class'; |
| 36 | + |
| 37 | +const getRowStyle = (cellData: RowClassParams<ElementAttributes>) => { |
| 38 | + const style: Record<string, string> = { fontSize: '1rem' }; |
| 39 | + if ( |
| 40 | + cellData.data && |
| 41 | + ![ |
| 42 | + ElementType.CASE, |
| 43 | + ElementType.LOADFLOW_PARAMETERS, |
| 44 | + ElementType.SENSITIVITY_PARAMETERS, |
| 45 | + ElementType.SECURITY_ANALYSIS_PARAMETERS, |
| 46 | + ElementType.VOLTAGE_INIT_PARAMETERS, |
| 47 | + ].includes(cellData.data.type) |
| 48 | + ) { |
| 49 | + style.cursor = 'pointer'; |
| 50 | + } |
| 51 | + return style; |
| 52 | +}; |
| 53 | + |
| 54 | +export const DirectoryContentTable = ({ |
| 55 | + gridRef, |
| 56 | + rows, |
| 57 | + handleCellContextualMenu, |
| 58 | + handleRowSelected, |
| 59 | + handleCellClick, |
| 60 | + colDef, |
| 61 | +}: DirectoryContentTableProps) => { |
| 62 | + return ( |
| 63 | + <CustomAGGrid |
| 64 | + ref={gridRef} |
| 65 | + rowData={rows} |
| 66 | + getRowId={getRowId} |
| 67 | + defaultColDef={defaultColumnDefinition} |
| 68 | + rowSelection="multiple" |
| 69 | + suppressRowClickSelection |
| 70 | + onGridReady={onGridReady} |
| 71 | + onCellContextMenu={handleCellContextualMenu} |
| 72 | + onCellClicked={handleCellClick} |
| 73 | + onRowSelected={handleRowSelected} |
| 74 | + animateRows={true} |
| 75 | + columnDefs={colDef} |
| 76 | + getRowStyle={getRowStyle} |
| 77 | + //We set a custom className for rows in order to easily determine if a context menu event is happening on a row or not |
| 78 | + rowClass={CUSTOM_ROW_CLASS} |
| 79 | + /> |
| 80 | + ); |
| 81 | +}; |
0 commit comments