Skip to content

Commit acc1208

Browse files
Adds rowAutoHeight prop and removes isOnlyRow prop.
1 parent e9d3f52 commit acc1208

File tree

6 files changed

+28
-131
lines changed

6 files changed

+28
-131
lines changed

src/components/Grid/Cell.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const Cell = memo(
1919
showHeader,
2020
rowHeight,
2121
rowStart,
22+
rowAutoHeight,
2223
} = data;
2324

2425
const currentRowIndex = rowIndex + rowStart;
@@ -78,7 +79,7 @@ export const Cell = memo(
7879
data-grid-row={currentRowIndex}
7980
data-grid-column={columnIndex}
8081
$showBorder
81-
$isOnlyRow={rowCount === 1}
82+
$rowAutoHeight={rowAutoHeight}
8283
{...props}
8384
/>
8485
</div>

src/components/Grid/Grid.stories.tsx

+8-109
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { useCallback, useEffect, useState } from "react";
22
import { CellProps, GridContextMenuItemProps, SelectedRegion, SelectionFocus } from "..";
33
import { Grid as CUIGrid } from "./Grid";
44

5-
const Cell: CellProps = ({ type, rowIndex, columnIndex, isScrolling, width, ...props }) => {
5+
const Cell: CellProps = ({
6+
type,
7+
rowIndex,
8+
columnIndex,
9+
isScrolling,
10+
width,
11+
...props
12+
}) => {
613
return (
714
<div
815
data-scrolling={isScrolling}
@@ -20,7 +27,6 @@ interface Props {
2027
row: number;
2128
column: number;
2229
};
23-
autoheight?: boolean;
2430
}
2531
const Grid = ({ columnCount, rowCount, focus: focusProp, ...props }: Props) => {
2632
const [focus, setFocus] = useState(focusProp);
@@ -72,7 +78,6 @@ const Grid = ({ columnCount, rowCount, focus: focusProp, ...props }: Props) => {
7278
});
7379
}}
7480
getMenuOptions={getMenuOptions}
75-
autoheight={props.autoheight}
7681
{...props}
7782
/>
7883
</div>
@@ -123,109 +128,3 @@ export const Playground = {
123128
},
124129
},
125130
};
126-
127-
export const AutoHeightWithVariableData = {
128-
args: {
129-
rowCount: 10,
130-
columnCount: 5,
131-
autoheight: true,
132-
rowStart: 0,
133-
},
134-
parameters: {
135-
docs: {
136-
source: {
137-
transform: (_: string, story: { args: Props; [x: string]: unknown }) => {
138-
const { rowCount, columnCount, autoheight, ...props } = story.args;
139-
return `
140-
const VariableCell: CellProps = ({ type, rowIndex, columnIndex, isScrolling, width, ...props }) => {
141-
let content = \`Row \${rowIndex}, Col \${columnIndex}\${rowIndex % 2 === 0 ? '\\nExtra line' : ''}\`;
142-
143-
if (rowIndex === 0 && columnIndex === 0) {
144-
content = \`CREATE TABLE random_user_events (
145-
user_id UInt32,
146-
event_time DateTime,
147-
event_type Enum8('click' = 1, 'view' = 2, 'purchase' = 3),
148-
item_id String,
149-
price Decimal(10,2),
150-
quantity UInt16
151-
) ENGINE = MergeTree()
152-
ORDER BY (user_id, event_time)
153-
PARTITION BY toYYYYMM(event_time)
154-
SETTINGS index_granularity = 8192;\`;
155-
}
156-
157-
return (
158-
<div
159-
data-scrolling={isScrolling}
160-
style={{
161-
whiteSpace: 'pre-wrap',
162-
padding: '8px',
163-
borderBottom: '1px solid #ccc',
164-
fontFamily: rowIndex === 0 && columnIndex === 0 ? 'monospace' : 'inherit',
165-
fontSize: rowIndex === 0 && columnIndex === 0 ? '12px' : 'inherit',
166-
}}
167-
{...props}
168-
>
169-
{content}
170-
</div>
171-
);
172-
};
173-
174-
<Grid
175-
cell={VariableCell}
176-
focus={{ row: 0, column: 0 }}
177-
columnWidth={() => 300}
178-
autoheight={${autoheight}}
179-
${Object.entries(props)
180-
.flatMap(([key, value]) =>
181-
typeof value === "boolean"
182-
? value
183-
? ` ${key}`
184-
: []
185-
: ` ${key}=${typeof value == "string" ? `"${value}"` : `{${value}}`}`
186-
)
187-
.join("\n")}
188-
/>
189-
`;
190-
},
191-
},
192-
},
193-
},
194-
render: (args) => {
195-
const VariableCell: CellProps = ({ type, rowIndex, columnIndex, isScrolling, width, ...props }) => {
196-
let content = `Row ${rowIndex}, Col ${columnIndex}${rowIndex % 2 === 0 ? '\nExtra line' : ''}`;
197-
198-
if (rowIndex === 0 && columnIndex === 0) {
199-
content = `CREATE TABLE random_user_events (
200-
user_id UInt32,
201-
event_time DateTime,
202-
event_type Enum8('click' = 1, 'view' = 2, 'purchase' = 3),
203-
item_id String,
204-
price Decimal(10,2),
205-
quantity UInt16
206-
) ENGINE = MergeTree()
207-
ORDER BY (user_id, event_time)
208-
PARTITION BY toYYYYMM(event_time)
209-
SETTINGS index_granularity = 8192;`;
210-
}
211-
212-
return (
213-
<div
214-
data-scrolling={isScrolling}
215-
style={{
216-
whiteSpace: 'pre-wrap',
217-
padding: '8px',
218-
borderBottom: '1px solid #ccc',
219-
fontFamily: rowIndex === 0 && columnIndex === 0 ? 'monospace' : 'inherit',
220-
fontSize: rowIndex === 0 && columnIndex === 0 ? '12px' : 'inherit',
221-
}}
222-
{...props}
223-
>
224-
{content}
225-
</div>
226-
);
227-
};
228-
229-
return <Grid {...args} cell={VariableCell} columnWidth={() => 300} />;
230-
},
231-
};

src/components/Grid/Grid.tsx

+3-13
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
406406
headerHeight,
407407
rowNumberWidth,
408408
rowStart,
409+
rowAutoHeight,
409410
};
410411

411412
const InnerElementType = forwardRef<HTMLDivElement, InnerElementTypeTypes>(
@@ -436,6 +437,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
436437
showHeader={showHeader}
437438
rowStart={rowStart}
438439
showBorder={showBorder}
440+
rowAutoHeight={rowAutoHeight}
439441
/>
440442
)}
441443

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

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

799-
const getRowHeight = useCallback(
800-
(index: number, parentHeight: number): number => {
801-
if (rowCount === 1 && index === 0) {
802-
return parentHeight - rowHeight * 2;
803-
}
804-
805-
return rowHeight;
806-
},
807-
[rowCount, rowHeight]
808-
);
809-
810800
return (
811801
<ContextMenu
812802
modal={false}
@@ -841,7 +831,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
841831
height={height}
842832
width={width}
843833
columnCount={columnCount}
844-
rowHeight={index => getRowHeight(index, height)}
834+
rowHeight={() => rowHeight}
845835
useIsScrolling={useIsScrolling}
846836
innerElementType={InnerElementType}
847837
itemData={data}

src/components/Grid/RowNumberColumn.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ interface RowNumberColumnProps {
4747
scrolledHorizontal: boolean;
4848
rowStart: number;
4949
showBorder: boolean;
50+
rowAutoHeight?: boolean;
5051
}
5152
interface RowNumberProps
5253
extends Pick<
@@ -56,6 +57,7 @@ interface RowNumberProps
5657
rowIndex: number;
5758
isLastRow: boolean;
5859
isFirstRow: boolean;
60+
rowAutoHeight?: boolean;
5961
}
6062
const RowNumber = ({
6163
rowIndex,
@@ -65,6 +67,7 @@ const RowNumber = ({
6567
isFirstRow,
6668
showBorder,
6769
rowStart,
70+
rowAutoHeight
6871
}: RowNumberProps) => {
6972
const currentRowIndex = rowIndex + rowStart;
7073
const selectionType = getSelectionType({
@@ -96,6 +99,7 @@ const RowNumber = ({
9699
$isLastRow={isLastRow}
97100
$isSelectedLeft={isSelected}
98101
$isSelectedTop={isSelectedTop}
102+
$rowAutoHeight={rowAutoHeight}
99103
data-selected={isSelected}
100104
data-grid-row={currentRowIndex}
101105
data-grid-column={-1}
@@ -121,6 +125,7 @@ const RowNumberColumn = ({
121125
scrolledHorizontal,
122126
rowStart = 0,
123127
showBorder,
128+
rowAutoHeight
124129
}: RowNumberColumnProps) => {
125130
return (
126131
<RowNumberColumnContainer
@@ -139,6 +144,7 @@ const RowNumberColumn = ({
139144
isFirstRow={!showHeader && rowIndex === 0}
140145
showBorder={showBorder}
141146
rowStart={rowStart}
147+
rowAutoHeight={rowAutoHeight}
142148
/>
143149
)
144150
)}

src/components/Grid/StyledCell.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const StyledCell = styled.div<{
1313
$height: number;
1414
$type?: "body" | "header";
1515
$showBorder: boolean;
16-
$isOnlyRow?: boolean;
16+
$rowAutoHeight?: boolean;
1717
}>`
1818
display: block;
1919
text-align: left;
@@ -35,10 +35,10 @@ export const StyledCell = styled.div<{
3535
$height,
3636
$type = "body",
3737
$showBorder,
38-
$isOnlyRow,
38+
$rowAutoHeight,
3939
}) => `
40-
height: ${$isOnlyRow ? "auto" : `${$height}px`};
41-
overflow-y: ${$isOnlyRow ? "auto" : "hidden"};
40+
height: ${$rowAutoHeight ? "auto" : `${$height}px`};
41+
overflow-y: ${$rowAutoHeight ? "auto" : "hidden"};
4242
background: ${theme.click.grid[$type].cell.color.background[$selectionType]};
4343
color: ${
4444
$type === "header"
@@ -89,7 +89,7 @@ export const StyledCell = styled.div<{
8989
`
9090
: "border-right: none;"
9191
}
92-
${$isOnlyRow && "border: none;"}
92+
${$rowAutoHeight && "border: none;"}
9393
`}
9494
${({
9595
theme,
@@ -99,12 +99,12 @@ export const StyledCell = styled.div<{
9999
$type = "body",
100100
$isSelectedTop,
101101
$isSelectedLeft,
102-
$isOnlyRow,
102+
$rowAutoHeight,
103103
}) =>
104104
$isSelectedTop ||
105105
$isSelectedLeft ||
106106
($selectionType === "selectDirect" && ($isLastRow || $isLastColumn)) ||
107-
$isOnlyRow
107+
$rowAutoHeight
108108
? `
109109
&::before {
110110
content: "";
@@ -133,7 +133,7 @@ export const StyledCell = styled.div<{
133133
? `border-right: 1px solid ${theme.click.grid[$type].cell.color.stroke.selectDirect};`
134134
: ""
135135
}
136-
${$isOnlyRow && "border: none;"}
136+
${$rowAutoHeight && "border: none;"}
137137
}
138138
`
139139
: ""};

src/components/Grid/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export interface ItemDataType {
157157
headerHeight: number;
158158
rowNumberWidth: number;
159159
rowStart: number;
160+
rowAutoHeight?: boolean;
160161
}
161162

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

0 commit comments

Comments
 (0)