Skip to content

Commit

Permalink
frontend: Enhance Table component with column resizing functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
zcyc committed Jan 23, 2025
1 parent f47b2a8 commit ec8f156
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion frontend/src/components/common/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { MRT_Localization_EN } from 'material-react-table/locales/en';
import { MRT_Localization_ES } from 'material-react-table/locales/es';
import { MRT_Localization_FR } from 'material-react-table/locales/fr';
import { MRT_Localization_PT } from 'material-react-table/locales/pt';
import { memo, ReactNode, useEffect, useMemo, useState } from 'react';
import { memo, ReactNode, useCallback,useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import helpers from '../../../helpers';
import { useURLState } from '../../../lib/util';
Expand Down Expand Up @@ -132,9 +132,28 @@ const tableLocalizationMap: Record<string, MRT_Localization> = {
pt: MRT_Localization_PT,
};

// Add new type for column widths state
type ColumnWidths = Record<string, number>;

// Add resizing styles to StyledHeadRow
const StyledHeadRow = styled('tr')(({ theme }) => ({
display: 'contents',
background: theme.palette.tables.head.background,
'& .resizer': {
display: 'inline-block',
width: '5px',
height: '100%',
position: 'absolute',
right: 0,
top: 0,
transform: 'translateX(50%)',
zIndex: 1,
touchAction: 'none',
cursor: 'col-resize',
'&.isResizing': {
background: theme.palette.primary.main,
},
},
}));
const StyledRow = styled('tr')(({ theme }) => ({
display: 'contents',
Expand Down Expand Up @@ -200,6 +219,17 @@ export default function Table<RowItem extends Record<string, any>>({
}
: undefined;

// Add state for column widths
const [columnWidths, setColumnWidths] = useState<ColumnWidths>({});

// Add resize handler
const handleResize = useCallback((columnId: string, width: number) => {
setColumnWidths(prev => ({
...prev,
[columnId]: width,
}));
}, []);

const table = useMaterialReactTable({
...tableProps,
columns: tableColumns ?? [],
Expand Down Expand Up @@ -284,6 +314,7 @@ export default function Table<RowItem extends Record<string, any>>({
width: 'unset',
minWidth: 'unset',
paddingTop: '0.5rem',
position: 'relative', // Add this for resizer positioning
},
},
muiSelectCheckboxProps: {
Expand All @@ -294,6 +325,15 @@ export default function Table<RowItem extends Record<string, any>>({
size: 'small',
sx: { padding: 0 },
},
enableColumnResizing: true,
columnResizeMode: 'onChange',
onColumnSizingChange: updater => {
const newSizing =
typeof updater === 'function' ? updater(table.getState().columnSizing) : updater;
Object.entries(newSizing).forEach(([columnId, width]) => {
handleResize(columnId, width);
});
},
});

const gridTemplateColumns = useMemo(() => {
Expand All @@ -306,6 +346,11 @@ export default function Table<RowItem extends Record<string, any>>({
return !isHidden;
})
.map(it => {
const id = it.id!;
const width = columnWidths[id];
if (width) {
return `${width}px`;
}
if (typeof it.gridTemplate === 'number') {
return `${it.gridTemplate}fr`;
}
Expand All @@ -326,6 +371,7 @@ export default function Table<RowItem extends Record<string, any>>({
tableProps.state?.columnVisibility,
tableProps.enableRowActions,
tableProps.enableRowSelection,
columnWidths,
]);

const rows = useMRT_Rows(table);
Expand Down

0 comments on commit ec8f156

Please sign in to comment.