From 1535d59169bc41d03fce6de0364e74ac2455b8bf Mon Sep 17 00:00:00 2001 From: Daniel Haselhan Date: Wed, 8 Jan 2025 13:43:14 -0800 Subject: [PATCH] feat: Dynamically change grid from autoHeight -> Normal Add a new autoHeight feature to BCGridBase that auto changes the dom rendering methods when the number of rows reaches the size of the screen. --- .../src/components/BCDataGrid/BCGridBase.jsx | 87 +++++++++-- .../components/BCDataGrid/BCGridEditor.jsx | 147 +++++++++--------- 2 files changed, 146 insertions(+), 88 deletions(-) diff --git a/frontend/src/components/BCDataGrid/BCGridBase.jsx b/frontend/src/components/BCDataGrid/BCGridBase.jsx index 8ec81244d..4bb61a54a 100644 --- a/frontend/src/components/BCDataGrid/BCGridBase.jsx +++ b/frontend/src/components/BCDataGrid/BCGridBase.jsx @@ -3,27 +3,78 @@ import DataGridLoading from '@/components/DataGridLoading' import { AgGridReact } from '@ag-grid-community/react' import '@ag-grid-community/styles/ag-grid.css' import '@ag-grid-community/styles/ag-theme-material.css' -import { forwardRef, useCallback, useMemo } from 'react' +import { + forwardRef, + useCallback, + useEffect, + useMemo, + useRef, + useState +} from 'react' import { useSearchParams } from 'react-router-dom' -export const BCGridBase = forwardRef(({ autoSizeStrategy, ...props }, ref) => { - const [searchParams] = useSearchParams() - const highlightedId = searchParams.get('hid') +const ROW_HEIGHT = 45 - const loadingOverlayComponent = useMemo(() => DataGridLoading, []) +export const BCGridBase = forwardRef( + ({ autoSizeStrategy, autoHeight, ...props }, ref) => { + const [searchParams] = useSearchParams() + const highlightedId = searchParams.get('hid') - const getRowStyle = useCallback((params) => { - if (params.node.id === highlightedId) { - return { backgroundColor: '#fade81' } - } - }, []) + const loadingOverlayComponent = useMemo(() => DataGridLoading, []) - return ( - <> + const getRowStyle = useCallback((params) => { + if (params.node.id === highlightedId) { + return { backgroundColor: '#fade81' } + } + }, []) + + const gridApiRef = useRef(null) + const [domLayout, setDomLayout] = useState('autoHeight') + const [height, setHeight] = useState('auto') + + const determineHeight = useCallback(() => { + if (!gridApiRef.current || !autoHeight) { + return + } + + const maxVisibleRows = (window.innerHeight * 0.75) / ROW_HEIGHT + + const displayedRowCount = gridApiRef.current.getDisplayedRowCount() + if (displayedRowCount <= maxVisibleRows) { + setDomLayout('autoHeight') + setHeight('auto') + } else { + setDomLayout('normal') + setHeight('75vh') + } + }, []) + + const onGridReady = useCallback( + (params) => { + gridApiRef.current = params.api + determineHeight() + props.onGridReady(params) + }, + [determineHeight] + ) + + useEffect(() => { + const handleResize = () => { + determineHeight() + } + + window.addEventListener('resize', handleResize) + return () => { + window.removeEventListener('resize', handleResize) + } + }, [determineHeight]) + + return ( { enableBrowserTooltips={true} suppressPaginationPanel suppressScrollOnNewData + onRowDataUpdated={determineHeight} getRowStyle={getRowStyle} - rowHeight={45} + rowHeight={ROW_HEIGHT} headerHeight={40} {...props} + onGridReady={onGridReady} /> - - ) -}) + ) + } +) BCGridBase.displayName = 'BCGridBase' diff --git a/frontend/src/components/BCDataGrid/BCGridEditor.jsx b/frontend/src/components/BCDataGrid/BCGridEditor.jsx index 7482add28..113abc81d 100644 --- a/frontend/src/components/BCDataGrid/BCGridEditor.jsx +++ b/frontend/src/components/BCDataGrid/BCGridEditor.jsx @@ -299,84 +299,89 @@ export const BCGridEditor = ({ getRowId={(params) => params.data.id} onCellClicked={onCellClicked} onCellEditingStopped={handleOnCellEditingStopped} + autoHeight={true} {...props} /> - + - {showAddRowsButton && ( - - } - endIcon={ - addMultiRow && ( - - ) - } - onClick={ - addMultiRow ? handleAddRowsClick : () => handleAddRowsInternal(1) - } - > - Add row - - {addMultiRow && ( - + {showAddRowsButton && ( + <> + + } + endIcon={ + addMultiRow && ( + + ) + } + onClick={ + addMultiRow + ? handleAddRowsClick + : () => handleAddRowsInternal(1) + } + > + Add row + + {addMultiRow && ( + + handleAddRowsInternal(1)}> + 1 row + + handleAddRowsInternal(5)}> + 5 rows + + handleAddRowsInternal(10)}> + 10 rows + + + )} + + )} + {saveButtonProps.enabled && ( + <> + - handleAddRowsInternal(1)}> - 1 row - - handleAddRowsInternal(5)}> - 5 rows - - handleAddRowsInternal(10)}> - 10 rows - - - )} - - )} - {saveButtonProps.enabled && ( - <> - - {saveButtonProps.text} - - { - setShowCloseModal(false) - }} - data={{ - title: saveButtonProps.text, - content: saveButtonProps.confirmText, - primaryButtonAction: saveButtonProps.onSave, - primaryButtonText: saveButtonProps.confirmLabel, - secondaryButtonText: t('cancelBtn') - }} - /> - - )} + {saveButtonProps.text} + + { + setShowCloseModal(false) + }} + data={{ + title: saveButtonProps.text, + content: saveButtonProps.confirmText, + primaryButtonAction: saveButtonProps.onSave, + primaryButtonText: saveButtonProps.confirmLabel, + secondaryButtonText: t('cancelBtn') + }} + /> + + )} + ) }