Skip to content

Commit

Permalink
Adds rowAutoHeight prop and removes isOnlyRow prop.
Browse files Browse the repository at this point in the history
  • Loading branch information
natalyjazzviolin committed Feb 4, 2025
1 parent e9d3f52 commit acc1208
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 131 deletions.
3 changes: 2 additions & 1 deletion src/components/Grid/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const Cell = memo(
showHeader,
rowHeight,
rowStart,
rowAutoHeight,
} = data;

const currentRowIndex = rowIndex + rowStart;
Expand Down Expand Up @@ -78,7 +79,7 @@ export const Cell = memo(
data-grid-row={currentRowIndex}
data-grid-column={columnIndex}
$showBorder
$isOnlyRow={rowCount === 1}
$rowAutoHeight={rowAutoHeight}
{...props}
/>
</div>
Expand Down
117 changes: 8 additions & 109 deletions src/components/Grid/Grid.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ 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, width, ...props }) => {
const Cell: CellProps = ({
type,
rowIndex,
columnIndex,
isScrolling,
width,
...props
}) => {
return (
<div
data-scrolling={isScrolling}
Expand All @@ -20,7 +27,6 @@ interface Props {
row: number;
column: number;
};
autoheight?: boolean;
}
const Grid = ({ columnCount, rowCount, focus: focusProp, ...props }: Props) => {
const [focus, setFocus] = useState(focusProp);
Expand Down Expand Up @@ -72,7 +78,6 @@ const Grid = ({ columnCount, rowCount, focus: focusProp, ...props }: Props) => {
});
}}
getMenuOptions={getMenuOptions}
autoheight={props.autoheight}
{...props}
/>
</div>
Expand Down Expand Up @@ -123,109 +128,3 @@ export const Playground = {
},
},
};

export const AutoHeightWithVariableData = {
args: {
rowCount: 10,
columnCount: 5,
autoheight: true,
rowStart: 0,
},
parameters: {
docs: {
source: {
transform: (_: string, story: { args: Props; [x: string]: unknown }) => {
const { rowCount, columnCount, autoheight, ...props } = story.args;
return `
const VariableCell: CellProps = ({ type, rowIndex, columnIndex, isScrolling, width, ...props }) => {
let content = \`Row \${rowIndex}, Col \${columnIndex}\${rowIndex % 2 === 0 ? '\\nExtra line' : ''}\`;
if (rowIndex === 0 && columnIndex === 0) {
content = \`CREATE TABLE random_user_events (
user_id UInt32,
event_time DateTime,
event_type Enum8('click' = 1, 'view' = 2, 'purchase' = 3),
item_id String,
price Decimal(10,2),
quantity UInt16
) ENGINE = MergeTree()
ORDER BY (user_id, event_time)
PARTITION BY toYYYYMM(event_time)
SETTINGS index_granularity = 8192;\`;
}
return (
<div
data-scrolling={isScrolling}
style={{
whiteSpace: 'pre-wrap',
padding: '8px',
borderBottom: '1px solid #ccc',
fontFamily: rowIndex === 0 && columnIndex === 0 ? 'monospace' : 'inherit',
fontSize: rowIndex === 0 && columnIndex === 0 ? '12px' : 'inherit',
}}
{...props}
>
{content}
</div>
);
};
<Grid
cell={VariableCell}
focus={{ row: 0, column: 0 }}
columnWidth={() => 300}
autoheight={${autoheight}}
${Object.entries(props)
.flatMap(([key, value]) =>
typeof value === "boolean"
? value
? ` ${key}`
: []
: ` ${key}=${typeof value == "string" ? `"${value}"` : `{${value}}`}`
)
.join("\n")}
/>
`;
},
},
},
},
render: (args) => {
const VariableCell: CellProps = ({ type, rowIndex, columnIndex, isScrolling, width, ...props }) => {
let content = `Row ${rowIndex}, Col ${columnIndex}${rowIndex % 2 === 0 ? '\nExtra line' : ''}`;

if (rowIndex === 0 && columnIndex === 0) {
content = `CREATE TABLE random_user_events (
user_id UInt32,
event_time DateTime,
event_type Enum8('click' = 1, 'view' = 2, 'purchase' = 3),
item_id String,
price Decimal(10,2),
quantity UInt16
) ENGINE = MergeTree()
ORDER BY (user_id, event_time)
PARTITION BY toYYYYMM(event_time)
SETTINGS index_granularity = 8192;`;
}

return (
<div
data-scrolling={isScrolling}
style={{
whiteSpace: 'pre-wrap',
padding: '8px',
borderBottom: '1px solid #ccc',
fontFamily: rowIndex === 0 && columnIndex === 0 ? 'monospace' : 'inherit',
fontSize: rowIndex === 0 && columnIndex === 0 ? '12px' : 'inherit',
}}
{...props}
>
{content}
</div>
);
};

return <Grid {...args} cell={VariableCell} columnWidth={() => 300} />;
},
};
16 changes: 3 additions & 13 deletions src/components/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
headerHeight,
rowNumberWidth,
rowStart,
rowAutoHeight,
};

const InnerElementType = forwardRef<HTMLDivElement, InnerElementTypeTypes>(
Expand Down Expand Up @@ -436,6 +437,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
showHeader={showHeader}
rowStart={rowStart}
showBorder={showBorder}
rowAutoHeight={rowAutoHeight}
/>
)}

Expand Down Expand Up @@ -778,7 +780,6 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
}, [rowStart, onItemsRendered]);

const CellWithWidth = (args: GridChildComponentProps<ItemDataType>): JSX.Element => {
console.log("Cell with width ", rowAutoHeight)
const width = columnWidth(args.columnIndex);
return (
<Cell
Expand All @@ -796,17 +797,6 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
}
}, [rowCount]);

const getRowHeight = useCallback(
(index: number, parentHeight: number): number => {
if (rowCount === 1 && index === 0) {
return parentHeight - rowHeight * 2;
}

return rowHeight;
},
[rowCount, rowHeight]
);

return (
<ContextMenu
modal={false}
Expand Down Expand Up @@ -841,7 +831,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
height={height}
width={width}
columnCount={columnCount}
rowHeight={index => getRowHeight(index, height)}
rowHeight={() => rowHeight}
useIsScrolling={useIsScrolling}
innerElementType={InnerElementType}
itemData={data}
Expand Down
6 changes: 6 additions & 0 deletions src/components/Grid/RowNumberColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface RowNumberColumnProps {
scrolledHorizontal: boolean;
rowStart: number;
showBorder: boolean;
rowAutoHeight?: boolean;
}
interface RowNumberProps
extends Pick<
Expand All @@ -56,6 +57,7 @@ interface RowNumberProps
rowIndex: number;
isLastRow: boolean;
isFirstRow: boolean;
rowAutoHeight?: boolean;
}
const RowNumber = ({
rowIndex,
Expand All @@ -65,6 +67,7 @@ const RowNumber = ({
isFirstRow,
showBorder,
rowStart,
rowAutoHeight
}: RowNumberProps) => {
const currentRowIndex = rowIndex + rowStart;
const selectionType = getSelectionType({
Expand Down Expand Up @@ -96,6 +99,7 @@ const RowNumber = ({
$isLastRow={isLastRow}
$isSelectedLeft={isSelected}
$isSelectedTop={isSelectedTop}
$rowAutoHeight={rowAutoHeight}
data-selected={isSelected}
data-grid-row={currentRowIndex}
data-grid-column={-1}
Expand All @@ -121,6 +125,7 @@ const RowNumberColumn = ({
scrolledHorizontal,
rowStart = 0,
showBorder,
rowAutoHeight
}: RowNumberColumnProps) => {
return (
<RowNumberColumnContainer
Expand All @@ -139,6 +144,7 @@ const RowNumberColumn = ({
isFirstRow={!showHeader && rowIndex === 0}
showBorder={showBorder}
rowStart={rowStart}
rowAutoHeight={rowAutoHeight}
/>
)
)}
Expand Down
16 changes: 8 additions & 8 deletions src/components/Grid/StyledCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const StyledCell = styled.div<{
$height: number;
$type?: "body" | "header";
$showBorder: boolean;
$isOnlyRow?: boolean;
$rowAutoHeight?: boolean;
}>`
display: block;
text-align: left;
Expand All @@ -35,10 +35,10 @@ export const StyledCell = styled.div<{
$height,
$type = "body",
$showBorder,
$isOnlyRow,
$rowAutoHeight,
}) => `
height: ${$isOnlyRow ? "auto" : `${$height}px`};
overflow-y: ${$isOnlyRow ? "auto" : "hidden"};
height: ${$rowAutoHeight ? "auto" : `${$height}px`};
overflow-y: ${$rowAutoHeight ? "auto" : "hidden"};
background: ${theme.click.grid[$type].cell.color.background[$selectionType]};
color: ${
$type === "header"
Expand Down Expand Up @@ -89,7 +89,7 @@ export const StyledCell = styled.div<{
`
: "border-right: none;"
}
${$isOnlyRow && "border: none;"}
${$rowAutoHeight && "border: none;"}
`}
${({
theme,
Expand All @@ -99,12 +99,12 @@ export const StyledCell = styled.div<{
$type = "body",
$isSelectedTop,
$isSelectedLeft,
$isOnlyRow,
$rowAutoHeight,
}) =>
$isSelectedTop ||
$isSelectedLeft ||
($selectionType === "selectDirect" && ($isLastRow || $isLastColumn)) ||
$isOnlyRow
$rowAutoHeight
? `
&::before {
content: "";
Expand Down Expand Up @@ -133,7 +133,7 @@ export const StyledCell = styled.div<{
? `border-right: 1px solid ${theme.click.grid[$type].cell.color.stroke.selectDirect};`
: ""
}
${$isOnlyRow && "border: none;"}
${$rowAutoHeight && "border: none;"}
}
`
: ""};
Expand Down
1 change: 1 addition & 0 deletions src/components/Grid/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export interface ItemDataType {
headerHeight: number;
rowNumberWidth: number;
rowStart: number;
rowAutoHeight?: boolean;
}

export interface GridContextMenuItemProps extends Omit<ContextMenuItemProps, "children"> {
Expand Down

0 comments on commit acc1208

Please sign in to comment.