Skip to content

Commit

Permalink
fix(TableVirtualized): support row height (#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
talkor authored Jan 21, 2024
1 parent fc126ff commit 9e4cfa0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
3 changes: 0 additions & 3 deletions src/components/Table/Table/Table.module.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
.table {
--row-size-medium: 40px;
--row-size-large: 48px;

background-color: var(--primary-background-color);
overflow: auto;
width: 100%;
Expand Down
7 changes: 4 additions & 3 deletions src/components/Table/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ITableBodyProps } from "../TableBody/TableBody";
import { getTableRowLayoutStyles } from "./tableHelpers";
import { getTestId } from "../../../tests/test-ids-utils";
import { ComponentDefaultTestId } from "../../../tests/constants";
import { RowSizes } from "./TableConsts";
import { RowHeights, RowSizes } from "./TableConsts";
import styles from "./Table.module.scss";

export type TableLoadingStateType = "long-text" | "medium-text" | "circle" | "rectangle";
Expand Down Expand Up @@ -44,6 +44,7 @@ interface ITableContext {
dataState?: ITableProps["dataState"];
emptyState: ITableProps["emptyState"];
errorState: ITableProps["errorState"];
size: ITableProps["size"];
}

export const TableContext = React.createContext<ITableContext>(null);
Expand Down Expand Up @@ -76,14 +77,14 @@ const Table: VibeComponent<ITableProps, HTMLDivElement> & {
*/
const calculatedStyle = {
"--table-grid-template-columns": gridTemplateColumns,
"--table-row-size": size == Table.sizes.MEDIUM ? "var(--row-size-medium)" : "var(--row-size-large)",
"--table-row-size": `${RowHeights[size]}px`,
...style
} as React.CSSProperties;

const testId = dataTestId || getTestId(ComponentDefaultTestId.TABLE, id);

return (
<TableContext.Provider value={{ columns, emptyState, errorState, dataState }}>
<TableContext.Provider value={{ columns, emptyState, errorState, dataState, size }}>
<div ref={ref} id={id} className={classNames} data-testid={testId} role="table" style={calculatedStyle}>
{children}
</div>
Expand Down
5 changes: 4 additions & 1 deletion src/components/Table/Table/TableConsts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ export enum RowSizes {
LARGE = "large"
}

export type RowSize = typeof RowSizes | number;
export const RowHeights = {
[RowSizes.MEDIUM]: 40,
[RowSizes.LARGE]: 48
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { ComponentProps, CSSProperties, FC, useCallback } from "react";
import React, { ComponentProps, CSSProperties, FC, useCallback, useContext } from "react";
import { VibeComponentProps } from "../../../types";
import VirtualizedList, { VirtualizedListItem } from "../../VirtualizedList/VirtualizedList";
import TableBody from "../TableBody/TableBody";
import styles from "./TableVirtualizedBody.module.scss";
import { ScrollDirection } from "react-window";
import { TableContext } from "../Table/Table";
import { RowHeights } from "../Table/TableConsts";

export interface ITableVirtualizedBodyProps extends VibeComponentProps {
items: ComponentProps<typeof VirtualizedList>["items"];
Expand All @@ -19,13 +21,14 @@ const TableVirtualizedBody: FC<ITableVirtualizedBodyProps> = ({ items, rowRender
},
[rowRenderer]
);
const { size } = useContext(TableContext);

return (
<TableBody className={styles.tableBody}>
<VirtualizedList
items={items}
itemRenderer={itemRenderer}
getItemHeight={() => 40}
getItemHeight={() => RowHeights[size]}
layout="vertical"
onScroll={onScroll}
/>
Expand Down

0 comments on commit 9e4cfa0

Please sign in to comment.