Skip to content

Commit

Permalink
frontend: Fix column visibility handled by internal Table state
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Dubenko <[email protected]>
  • Loading branch information
sniok committed Dec 13, 2024
1 parent 22688fa commit 3432c25
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions frontend/src/components/common/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
MRT_ColumnDef as MaterialTableColumn,
MRT_Header,
MRT_Localization,
MRT_Row,
MRT_TableBodyCell,
MRT_TableHeadCell,
MRT_TableInstance,
Expand Down Expand Up @@ -191,34 +190,6 @@ export default function Table<RowItem extends Record<string, any>>({
return (tableProps.data ?? []).filter(it => filterFunction(it));
}, [tableProps.data, filterFunction]);

const gridTemplateColumns = useMemo(() => {
let preGridTemplateColumns = tableProps.columns
.filter(it => {
const isHidden = tableProps.state?.columnVisibility?.[it.id!] === false;
return !isHidden;
})
.map(it => {
if (typeof it.gridTemplate === 'number') {
return `${it.gridTemplate}fr`;
}
return it.gridTemplate ?? '1fr';
})
.join(' ');
if (tableProps.enableRowActions) {
preGridTemplateColumns = `${preGridTemplateColumns} 0.05fr`;
}
if (tableProps.enableRowSelection) {
preGridTemplateColumns = `44px ${preGridTemplateColumns}`;
}

return preGridTemplateColumns;
}, [
tableProps.columns,
tableProps.state?.columnVisibility,
tableProps.enableRowActions,
tableProps.enableRowSelection,
]);

const paginationSelectProps = import.meta.env.UNDER_TEST
? {
inputProps: {
Expand Down Expand Up @@ -325,6 +296,38 @@ export default function Table<RowItem extends Record<string, any>>({
},
});

const gridTemplateColumns = useMemo(() => {
let preGridTemplateColumns = tableProps.columns
.filter((it, i) => {
const id = it.id ?? String(i);
const isHidden =
table.getState().columnVisibility?.[id] === false ||
tableProps.state?.columnVisibility?.[id] === false;
return !isHidden;
})
.map(it => {
if (typeof it.gridTemplate === 'number') {
return `${it.gridTemplate}fr`;
}
return it.gridTemplate ?? '1fr';
})
.join(' ');
if (tableProps.enableRowActions) {
preGridTemplateColumns = `${preGridTemplateColumns} 0.05fr`;
}
if (tableProps.enableRowSelection) {
preGridTemplateColumns = `44px ${preGridTemplateColumns}`;
}

return preGridTemplateColumns;
}, [
tableProps.columns,
table.getState()?.columnVisibility,
tableProps.state?.columnVisibility,
tableProps.enableRowActions,
tableProps.enableRowSelection,
]);

const rows = useMRT_Rows(table);

if (!!errorMessage) {
Expand Down Expand Up @@ -382,7 +385,12 @@ export default function Table<RowItem extends Record<string, any>>({
</TableHead>
<StyledBody>
{rows.map(row => (
<Row key={row.id} row={row} table={table} isSelected={row.getIsSelected()} />
<Row
key={row.id}
cells={row.getVisibleCells()}
table={table}
isSelected={row.getIsSelected()}
/>
))}
</StyledBody>
</MuiTable>
Expand Down Expand Up @@ -417,16 +425,16 @@ const MemoHeadCell = memo(

const Row = memo(
({
row,
cells,
table,
isSelected,
}: {
table: MRT_TableInstance<any>;
row: MRT_Row<any>;
cells: MRT_Cell<any, unknown>[];
isSelected: boolean;
}) => (
<StyledRow data-selected={isSelected}>
{row.getVisibleCells().map(cell => (
{cells.map(cell => (
<MemoCell
cell={cell}
table={table}
Expand Down

0 comments on commit 3432c25

Please sign in to comment.