Skip to content

Commit

Permalink
respect rowHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
mckervinc committed Dec 12, 2024
1 parent 027ca10 commit c5b91be
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## 1.0.1

### Bugfix

- `rowHeight` is now respected
- when there is overflow-x, borders should remain

## 1.0.0

_2024-12-12_
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-fluid-table",
"version": "1.0.0",
"version": "1.0.1",
"description": "A React table inspired by @tanstack/react-virtual",
"author": "Mckervin Ceme <[email protected]>",
"license": "MIT",
Expand Down
28 changes: 16 additions & 12 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type HeaderProps<T> = {
uuid: string;
columns: ColumnProps<T>[];
pixelWidths: number[];
showRowWrapper: boolean;
className?: string;
style?: React.CSSProperties;
sortColumn?: string;
Expand All @@ -69,6 +70,7 @@ const Header = forwardRef(function <T>(
{
uuid,
columns,
showRowWrapper,
pixelWidths,
className,
style,
Expand Down Expand Up @@ -111,18 +113,20 @@ const Header = forwardRef(function <T>(

return (
<div ref={ref} className="rft-sticky-header" data-header-key={`${uuid}-header`}>
<div className={cx("rft-header", className)} style={style}>
{columns.map((c, i) => (
<HeaderCell
key={c.key}
width={pixelWidths[i]}
sortedCol={sortedCol}
sortedDir={sortedDir}
column={c as ColumnProps<any>}
onHeaderClick={onHeaderClick as any}
prevWidth={c.frozen ? pixelWidths.slice(0, i).reduce((pv, c) => pv + c, 0) : 0}
/>
))}
<div className={cx(showRowWrapper && "rft-row-wrapper")}>
<div className={cx("rft-header", className)} style={style}>
{columns.map((c, i) => (
<HeaderCell
key={c.key}
width={pixelWidths[i]}
sortedCol={sortedCol}
sortedDir={sortedDir}
column={c as ColumnProps<any>}
onHeaderClick={onHeaderClick as any}
prevWidth={c.frozen ? pixelWidths.slice(0, i).reduce((pv, c) => pv + c, 0) : 0}
/>
))}
</div>
</div>
</div>
);
Expand Down
10 changes: 8 additions & 2 deletions src/components/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
useRef,
useState
} from "react";
import { useResizeDetector } from "react-resize-detector";
import { ColumnProps, ScrollAlignment, TableProps, TableRef } from "../..";
import { DEFAULT_ROW_HEIGHT } from "../constants";
import { arraysMatch, calculateColumnWidths, cx, findColumnWidthConstants } from "../util";
Expand Down Expand Up @@ -61,6 +62,7 @@ const List = forwardRef(function <T>(
// hooks
const parentRef = useRef<HTMLDivElement | null>(null);
const headerRef = useRef<HTMLDivElement | null>(null);
const { ref: innerRef, width: innerWidth = 0 } = useResizeDetector<HTMLDivElement>();
const [pixelWidths, setPixelWidths] = useState<number[]>([]);
const [widthConstants, setWidthConstants] = useState(findColumnWidthConstants(columns));
const [expandedCache, setExpandedCache] = useState<Record<string | number, boolean>>({});
Expand All @@ -76,6 +78,7 @@ const List = forwardRef(function <T>(
});

// constants
const showRowWrapper = (innerRef.current?.scrollWidth || 0) > innerWidth;
const items = virtualizer.getVirtualItems();
const { fixedWidth, remainingCols } = widthConstants;

Expand Down Expand Up @@ -157,6 +160,7 @@ const List = forwardRef(function <T>(
<Header
ref={headerRef}
uuid={uuid}
showRowWrapper={showRowWrapper}
pixelWidths={pixelWidths}
columns={columns as ColumnProps<any>[]}
className={headerClassname}
Expand All @@ -167,12 +171,13 @@ const List = forwardRef(function <T>(
/>
<div className="rft-outer-container" style={{ height: virtualizer.getTotalSize() }}>
<div
ref={innerRef}
className="rft-inner-container"
style={{
transform: `translateY(${items[0]?.start ?? 0}px)`
}}
>
<div className="rft-row-wrapper">
<div className={cx(showRowWrapper && "rft-row-wrapper")}>
{items.map(item => {
const row = data[item.index];
const key = generateKeyFromRow(row, item.index);
Expand All @@ -182,6 +187,8 @@ const List = forwardRef(function <T>(
const style = typeof rowStyle === "function" ? rowStyle(item.index) : rowStyle;
return (
<Row
ref={virtualizer.measureElement}
rowHeight={rowHeight}
key={key}
row={row}
uuid={uuid}
Expand All @@ -196,7 +203,6 @@ const List = forwardRef(function <T>(
columns={columns as any}
pixelWidths={pixelWidths}
subComponent={subComponent as any}
ref={virtualizer.measureElement}
/>
);
})}
Expand Down
10 changes: 6 additions & 4 deletions src/components/Row.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { forwardRef, memo, useCallback } from "react";
import React, { forwardRef, JSX, memo, useCallback } from "react";
import { ColumnProps, RowRenderProps, SubComponentProps } from "../..";
import Minus from "../svg/minus-circle.svg";
import Plus from "../svg/plus-circle.svg";
import { cx } from "../util";
import { cx, positive } from "../util";

type TableCellProps<T> = {
row: T;
Expand Down Expand Up @@ -138,6 +138,7 @@ type RowProps<T> = {
pixelWidths: number[];
columns: ColumnProps<T>[];
isExpanded: boolean;
rowHeight?: number;
onExpand: (
row: T,
index: number,
Expand All @@ -162,12 +163,13 @@ const Row = forwardRef(function <T>(
row,
rowKey,
columns,
rowHeight,
pixelWidths,
isExpanded,
onExpand,
onRowClick,
className,
style,
style = {},
rowRenderer,
subComponent: SubComponent
}: RowProps<T>,
Expand All @@ -184,7 +186,7 @@ const Row = forwardRef(function <T>(
row={row}
index={index}
className={cx("rft-row", className)}
style={style}
style={{ height: positive(rowHeight) ? rowHeight : undefined, ...style }}
onRowClick={onRowClick}
rowRenderer={rowRenderer}
>
Expand Down

0 comments on commit c5b91be

Please sign in to comment.