Skip to content

Commit

Permalink
fix: πŸ› [Table] Offset calculation logic with MUI breakpoints
Browse files Browse the repository at this point in the history
βœ… Closes: 278
  • Loading branch information
luciob committed Nov 24, 2022
1 parent c12f72a commit f668f37
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
24 changes: 10 additions & 14 deletions src/components/Table/components/Head/index.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import React, { CSSProperties, FC, useMemo } from "react";
import { TableHead as MUITableHead, TableRow as MUITableRow } from "@material-ui/core";

import { useMosaicContext } from "../../../../hooks/useMosaicContext";
import { ITableHead } from "../../../../types/Table";
import { TOOLBAR_HEIGHT, TOOLBAR_HEIGHT_MOBILE } from "../../utils";
import TableHeadFilterCell from "../HeadFilter";

const TableHead: FC<ITableHead> = ({ children, columns, dataCy, showFilters, sticky }) => {
const {
view: { mobile },
} = useMosaicContext();

const toolbarHeight = useMemo(() => (mobile ? TOOLBAR_HEIGHT_MOBILE : TOOLBAR_HEIGHT), [mobile]);

const headerStyle = useMemo((): CSSProperties | undefined => {
if (!showFilters || !sticky) {
return;
}

// mui table set sticky position at cell level (see HeadCell component).
// When filters row is present, we need to set sticky positioning to the whole
// table head in order to avoid tracking the actual height of the first header
// row (which can vary depending on table width and cell content length).

if (showFilters && sticky) {
return { top: toolbarHeight, position: "sticky", zIndex: 1 };
}
return undefined;
}, [showFilters, sticky, toolbarHeight]);
return {
position: "sticky",
top: 0,
zIndex: 1,
};
}, [showFilters, sticky]);

return (
<MUITableHead data-cy={dataCy} style={headerStyle}>
Expand Down
44 changes: 27 additions & 17 deletions src/components/Table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
TableBody as MUITableBody,
TableContainer as MUITableContainer,
TableRow as MUITableRow,
useMediaQuery,
useTheme,
} from "@material-ui/core";

import { useMosaicContext } from "../../hooks/useMosaicContext";
import { ITable, ITableAction, ITableOnSortCallback } from "../../types/Table";
import { getComposedDataCy } from "../../utils";
import localized, { ILocalizableProperty } from "../../utils/hocs/localized";
Expand All @@ -26,6 +26,7 @@ import TableToolbar from "./components/Toolbar";
import {
COLUMN_CHECKBOX_PATH,
COLUMN_ROW_ACTIONS_PATH,
PAGINATION_TOOLBAR_BORDER,
PAGINATION_TOOLBAR_HEIGHT,
TOOLBAR_HEIGHT,
TOOLBAR_HEIGHT_MOBILE,
Expand Down Expand Up @@ -91,6 +92,8 @@ const Table: FC<ITable> = ({
}) => {
const theme = useTheme();

const mobile = useMediaQuery(theme.breakpoints.down("xs"));

const getRows = useCallback((rows: any) => [...rows].map((row, index) => ({ ...row, __mosaicTableId: index })), []);

const getSelectedRowsIndexes = useCallback(
Expand Down Expand Up @@ -218,7 +221,11 @@ const Table: FC<ITable> = ({
if (!!rowActions.length) {
columns = [
...columns,
{ label: "", path: COLUMN_ROW_ACTIONS_PATH, width: `${ROW_ACTION_DIMENSION * rowActions.length}px` },
{
label: "",
path: COLUMN_ROW_ACTIONS_PATH,
width: `${ROW_ACTION_DIMENSION * rowActions.length}px`,
},
];
}

Expand Down Expand Up @@ -263,6 +270,8 @@ const Table: FC<ITable> = ({
[onSortChange]
);

const paginated = useMemo(() => !!onPageChange || !!onPageSizeChange, [onPageChange, onPageSizeChange]);

const paginationStyle = useMemo((): CSSProperties => {
const DEFAULT_PAGINATION_STYLE: CSSProperties = { backgroundColor: "inherit", position: "inherit" };
if (!sticky) {
Expand All @@ -287,31 +296,34 @@ const Table: FC<ITable> = ({
[externalStyle, height, sticky]
);

const {
view: { mobile },
} = useMosaicContext();

const scrollContainerStyle = useMemo((): CSSProperties | undefined => {
const toolbarHeight = mobile ? TOOLBAR_HEIGHT_MOBILE : TOOLBAR_HEIGHT;
const extraOffset = mobile ? 9 : 2;
const scrollContainerStyle = useMemo((): CSSProperties => {
let toolbarHeight = TOOLBAR_HEIGHT;
if (mobile) {
toolbarHeight = TOOLBAR_HEIGHT_MOBILE;
}

let style: CSSProperties | undefined = undefined;
let offset = toolbarHeight;
if (paginated) {
offset += PAGINATION_TOOLBAR_BORDER + PAGINATION_TOOLBAR_HEIGHT;
}

let style: CSSProperties = {};
if (sticky) {
const offset = toolbarHeight + PAGINATION_TOOLBAR_HEIGHT + extraOffset;

style = {
height: `calc(100% - ${offset}px)`,
overflowY: "auto",
};
}

if (tableLayout === "auto") {
return { ...style, overflowX: "auto" };
return {
...style,
overflowX: "auto",
};
}

return style;
}, [mobile, sticky, tableLayout]);
}, [mobile, paginated, sticky, tableLayout]);

const showHeaderFilters = useMemo(
() => showFilters && columns.some((column) => !!column.renderFilter),
Expand Down Expand Up @@ -355,11 +367,9 @@ const Table: FC<ITable> = ({
) : (
rows.map(({ __mosaicTableId, ...row }, rowIndex) => {
const key = `row-${__mosaicTableId}`;

const rowSelected = isRowSelected(__mosaicTableId);
const rowCallbackOptions = { indexes: [rowIndex], multiple: false };
const style = getRowStyle ? getRowStyle(row, rowCallbackOptions) : {};

const onRowSelection = () => onSelection(__mosaicTableId);

return (
Expand Down Expand Up @@ -410,7 +420,7 @@ const Table: FC<ITable> = ({
</MUITableBody>
</MUITable>
</div>
{(onPageChange || onPageSizeChange) && (
{paginated && (
<TablePagination
dataCy={dataCy}
onPageChange={onPageChange!}
Expand Down
1 change: 1 addition & 0 deletions src/components/Table/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export const COLUMN_ROW_ACTIONS_PATH = "row-actions";
export const TOOLBAR_HEIGHT = 64;
export const TOOLBAR_HEIGHT_MOBILE = 56;

export const PAGINATION_TOOLBAR_BORDER = 1;
export const PAGINATION_TOOLBAR_HEIGHT = 64;

0 comments on commit f668f37

Please sign in to comment.