Skip to content

Commit

Permalink
feat: Support indentation of rows
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Dow committed Dec 14, 2023
1 parent 70105e7 commit 1dda096
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/components/Table/GridTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,33 @@ export const StyleCard = newStory(() => {
);
}, {});

export const LeveledStyleCard = newStory(() => {
const nameColumn: GridColumn<NestedRow> = {
header: () => "Name",
parent: (row) => row.name,
child: (row) => row.name,
grandChild: (row) => row.name,
add: () => "Add",
};
const valueColumn: GridColumn<NestedRow> = {
header: () => "Value",
parent: (row) => row.name,
child: (row) => row.name,
grandChild: (row) => row.name,
add: () => "Add",
w: "200px",
};
return (
<div css={Css.wPx(550).$}>
<GridTable
style={cardStyle}
columns={[collapseColumn<NestedRow>(), selectColumn<NestedRow>(), nameColumn, valueColumn]}
rows={rowsWithHeader}
/>
</div>
);
}, {});

export const StyleCardWithOneColumn = newStory(() => {
const nameColumn: GridColumn<Row> = { header: "Name", data: ({ name }) => name };
return (
Expand Down
14 changes: 13 additions & 1 deletion src/components/Table/TableStyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ export interface GridStyle {
/** Minimum table width in pixels. Used when calculating columns sizes */
minWidthPx?: number;
/** Css to apply at each level of a parent/child nested table. */
levels?: Record<number, { cellCss?: Properties; firstContentColumn?: Properties }>;
levels?: Record<
number,
{
/** Number of pixels to indent the row. This value will be subtracted from the "first content column" width. First content column is the first column that is not an 'action' column (i.e. non-checkbox or non-collapse button column) */
rowIndent?: number;
cellCss?: Properties;
firstContentColumn?: Properties;
}
>;
/** Allows for customization of the background color used to denote an "active" row */
activeBgColor?: Palette;
/** Defines styles for the group row which holds the selected rows that have been filtered out */
Expand Down Expand Up @@ -223,6 +231,10 @@ export const cardStyle: GridStyle = {
},
rowHoverColor: "none",
nonHeaderRowHoverCss: Css.bshHover.bGray700.$,
levels: {
1: { rowIndent: 24 },
2: { rowIndent: 48 },
},
};

export function resolveStyles(style: GridStyle | GridStyleDef): GridStyle {
Expand Down
8 changes: 7 additions & 1 deletion src/components/Table/components/Row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {
const showRowHoverColor = !reservedRowKinds.includes(row.kind) && !omitRowHover && style.rowHoverColor !== "none";

const rowStyleCellCss = maybeApplyFunction(row as any, rowStyle?.cellCss);
const levelIndent = (style.levels && style.levels[level]?.rowIndent) ?? 0;

const rowCss = {
...(!reservedRowKinds.includes(row.kind) && style.nonHeaderRowCss),
// Optionally include the row hover styles, by default they should be turned on.
Expand All @@ -90,6 +92,7 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {
// Need to spread this to make TS happy.
":hover": { ...style.nonHeaderRowHoverCss },
}),
...(levelIndent && Css.mlPx(levelIndent).$),
// For virtual tables use `display: flex` to keep all cells on the same row. For each cell in the row use `flexNone` to ensure they stay their defined widths
...(as === "table" ? {} : Css.relative.df.fg1.fs1.addIn("&>*", Css.flexNone.$).$),
// Apply `cursorPointer` to the row if it has a link or `onClick` value.
Expand Down Expand Up @@ -289,7 +292,10 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {
// Apply cell highlight styles to active cell and hover
...Css.if(applyCellHighlight && isCellActive).br4.boxShadow(`inset 0 0 0 1px ${Palette.Blue700}`).$,
// Define the width of the column on each cell. Supports col spans.
width: `calc(${columnSizes.slice(columnIndex, columnIndex + currentColspan).join(" + ")})`,
// If we have a 'levelIndent' defined, then subtract that amount from the first content column's width to ensure all columns will still line up properly
width: `calc(${columnSizes.slice(columnIndex, columnIndex + currentColspan).join(" + ")}${
applyFirstContentColumnStyles ? ` - ${levelIndent}px` : ""
})`,
...(typeof column.mw === "string" ? Css.mw(column.mw).$ : {}),
};

Expand Down

0 comments on commit 1dda096

Please sign in to comment.