Skip to content

Commit

Permalink
Expose width size to grid cell (#437)
Browse files Browse the repository at this point in the history
* Adding width size in px to the grid cell as prop

* fixing import

* fixing ts issues
  • Loading branch information
fneves authored Jun 24, 2024
1 parent ec3bd6f commit 697b6c0
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"test": "vitest",
"dev": "vite",
"generate-tokens": "node build-tokens.js",
"typecheck": "tsc --noEmit",
"build": "tsc && vite build",
"build:watch": "watch 'npm run build' ./src",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
Expand Down
6 changes: 5 additions & 1 deletion src/components/Grid/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { GridChildComponentProps, areEqual } from "react-window";
import { ItemDataType } from "./types";
import { StyledCell } from "./StyledCell";

type CellProps = GridChildComponentProps<ItemDataType> & {
width: number;
}

export const Cell = memo(
({
data,
rowIndex,
columnIndex,
style,
...props
}: GridChildComponentProps<ItemDataType>) => {
}: CellProps) => {
const {
cell: CellData,
getSelectionType,
Expand Down
4 changes: 2 additions & 2 deletions src/components/Grid/Grid.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { useCallback, useEffect, useState } from "react";
import { CellProps, GridContextMenuItemProps, SelectedRegion, SelectionFocus } from "..";
import { Grid as CUIGrid } from "./Grid";

const Cell: CellProps = ({ type, rowIndex, columnIndex, isScrolling, ...props }) => {
const Cell: CellProps = ({ type, rowIndex, columnIndex, isScrolling, width, ...props }) => {
return (
<div
data-scrolling={isScrolling}
{...props}
>
{rowIndex} {columnIndex} - {type}
{rowIndex} {columnIndex} - {type} - {width}px
</div>
);
};
Expand Down
13 changes: 12 additions & 1 deletion src/components/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
GridOnScrollProps,
VariableSizeGrid,
GridOnItemsRenderedProps,
GridChildComponentProps,
} from "react-window";
import AutoSizer, { Size } from "react-virtualized-auto-sizer";
import RowNumberColumn from "./RowNumberColumn";
Expand Down Expand Up @@ -771,6 +772,16 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
}
}, [rowStart, onItemsRendered]);

const CellWithWidth = (args: GridChildComponentProps<ItemDataType>): JSX.Element => {
const width = columnWidth(args.columnIndex);
return (
<Cell
{...args}
width={width}
/>
);
}

return (
<ContextMenu
modal={false}
Expand Down Expand Up @@ -823,7 +834,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
onItemsRendered={onItemsRendered}
{...props}
>
{Cell}
{CellWithWidth}
</VariableSizeGrid>
)}
</AutoSizer>
Expand Down
1 change: 1 addition & 0 deletions src/components/Grid/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const Column = ({
data-grid-column={columnIndex}
data-selected={isSelected}
$showBorder={showBorder}
width={columnWidth(columnIndex)}
/>
<ColumnResizer
height={height}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Grid/copyGridElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const addCellToRow = (
const td = document.createElement("td");
// const root = createRoot(td);
const html = renderToStaticMarkup(
createElement(cell, { rowIndex, columnIndex, type: "row-cell" })
createElement(cell, { rowIndex, columnIndex, width: 1000, type: "row-cell" })
);
td.innerHTML = html;

Expand Down
2 changes: 2 additions & 0 deletions src/components/Grid/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ interface CellCommonProps extends HTMLAttributes<HTMLElement> {
interface CellHeaderProps extends CellCommonProps {
rowIndex?: never;
type: "header-cell";
width: number;
}

interface CellBodyProps extends CellCommonProps {
rowIndex: number;
type: "row-cell";
width: number;
}

type CellComponentProps = CellHeaderProps | CellBodyProps;
Expand Down
4 changes: 2 additions & 2 deletions src/examples/GridExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import {
Pagination,
} from "..";

const Cell: CellProps = ({ type, rowIndex, columnIndex, isScrolling, ...props }) => {
const Cell: CellProps = ({ type, rowIndex, columnIndex, isScrolling, width, ...props }) => {
return (
<span
data-scrolling={isScrolling}
{...props}
>
{rowIndex} {columnIndex} - {type}
{rowIndex} {columnIndex} - {type} {width}px
</span>
);
};
Expand Down

0 comments on commit 697b6c0

Please sign in to comment.