Skip to content

Commit

Permalink
fix: use just CSS for cell highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
0ces committed Nov 21, 2024
1 parent c3fb6f0 commit 50b9c12
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
11 changes: 9 additions & 2 deletions src/components/Table/GridTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
defaultStyle,
dragHandleColumn,
emptyCell,
getTableStyles,
GridCellAlignment,
GridColumn,
GridDataRow,
Expand Down Expand Up @@ -2204,7 +2205,7 @@ export function EditableRows() {
),
editableOnHover: true,
}),
w: "100px",
w: "120px",
};

const date1Column: GridColumn<EditableRow> = {
Expand Down Expand Up @@ -2241,11 +2242,17 @@ export function EditableRows() {
w: "120px",
};

const style = getTableStyles({ bordered: true, allWhite: true });

return (
<GridTable
columns={[nameColumn, selectColumn, date1Column, date2Column]}
rows={rows}
style={{ bordered: true, allWhite: true, rowHoverColor: Palette.Blue50 }}
style={{
...style,
rowHoverColor: Palette.Blue50,
rowEditableCellBorderColor: Palette.Blue300,
}}
/>
);
}
1 change: 1 addition & 0 deletions src/components/Table/GridTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export function GridTable<R extends Kinded, X extends Only<GridTableXss, X> = an
};

const style = resolveStyles(maybeStyle);
console.log(style);
const { tableState } = api;

tableState.onRowSelect = onRowSelect;
Expand Down
1 change: 1 addition & 0 deletions src/components/Table/TableStyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ function memoizedTableStyles() {
presentationSettings: { borderless: true, typeScale: "xs", wrap: rowHeight === "flexible" },
levels: grouped ? groupedLevels : defaultLevels,
rowHoverColor: Palette.Blue100,
rowEditableCellBorderColor: Palette.Blue300,
keptGroupRowCss: Css.bgYellow100.gray900.xsMd.df.aic.$,
keptLastRowCss: Css.boxShadow("inset 0px -14px 8px -11px rgba(63,63,63,.18)").$,
};
Expand Down
33 changes: 11 additions & 22 deletions src/components/Table/components/Row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {
[`:hover > .${revealOnRowHoverClass} > *`]: Css.vv.$,
},
...{
[`.textFieldBaseWrapper`]: Css.pl1.mlPx(-8).br4.ba.bcTransparent.$,
[`:hover > .${editableOnRowHoverClass} .textFieldBaseWrapper`]: Css.ba.bc(
style.rowEditableCellBorderColor ?? Palette.Blue300,
).$,
[`.textFieldBaseWrapper`]: Css.pl1.mlPx(-8).br4.ba.bcTransparent.bgTransparent.$,
[`.textFieldBaseWrapper > *`]: Css.bgTransparent.$,
[`.${editableOnRowHoverClass}:hover .textFieldBaseWrapper`]: Css.bgBlue100.$,
[`:hover:not(:has(.${editableOnRowHoverClass}:hover)) > .${editableOnRowHoverClass} .textFieldBaseWrapper, .${editableOnRowHoverClass}:hover .textFieldBaseWrapper`]:
Css.ba.bc(style.rowEditableCellBorderColor ?? Palette.Blue300).$,
},
...(isLastKeptRow && Css.addIn("&>*", style.keptLastRowCss).$),
};
Expand Down Expand Up @@ -231,7 +232,7 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {
? numExpandedColumns + 1
: 1;
const revealOnRowHover = isGridCellContent(maybeContent) ? maybeContent.revealOnRowHover : false;
const editableOnRowHover = isGridCellContent(maybeContent) ? maybeContent.editableOnHover : false;
const editableOnRowHover = isGridCellContent(maybeContent) && !!maybeContent.editableOnHover;

const canSortColumn =
(sortOn === "client" && column.clientSideSort !== false) ||
Expand Down Expand Up @@ -334,6 +335,8 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {
...(isGridCellContent(maybeContent) && maybeContent.css ? maybeContent.css : {}),
// Apply cell highlight styles to active cell and hover
...Css.if(applyCellHighlight && isCellActive).br4.boxShadow(`inset 0 0 0 1px ${Palette.Blue700}`).$,
...Css.if(editableOnRowHover && isCellActive).addIn("& .textFieldBaseWrapper", Css.bgBlue50.ba.bcBlue500.$)
.$,
// Define the width of the column on each cell. Supports col spans.
// 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(" + ")}${
Expand All @@ -343,14 +346,10 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {

const cellClassNames = [
...(revealOnRowHover ? [revealOnRowHoverClass] : []),
...(editableOnRowHover && (isCellActive || !tableState.activeCellId) ? [editableOnRowHoverClass] : []),
...(editableOnRowHover ? [editableOnRowHoverClass] : []),
].join(" ");

const cellOnHover =
isGridCellContent(maybeContent) && maybeContent.editableOnHover
? (enter: boolean) => (enter ? api.setActiveCellId(cellId) : api.setActiveCellId(undefined))
: undefined;
const cellOnClick = applyCellHighlight ? () => api.setActiveCellId(cellId) : undefined;
const cellOnClick = applyCellHighlight || editableOnRowHover ? () => api.setActiveCellId(cellId) : undefined;
const tooltip = isGridCellContent(maybeContent) ? maybeContent.tooltip : undefined;

const renderFn: RenderCellFn<any> =
Expand All @@ -362,17 +361,7 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {
? rowClickRenderFn(as, api, currentColspan)
: defaultRenderFn(as, currentColspan);

return renderFn(
columnIndex,
cellCss,
content,
row,
rowStyle,
cellClassNames,
cellOnClick,
cellOnHover,
tooltip,
);
return renderFn(columnIndex, cellCss, content, row, rowStyle, cellClassNames, cellOnClick, tooltip);
})
)}
</RowTag>
Expand Down
6 changes: 1 addition & 5 deletions src/components/Table/components/cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,19 @@ export type RenderCellFn<R extends Kinded> = (
rowStyle: RowStyle<R> | undefined,
classNames: string | undefined,
onClick: VoidFunction | undefined,
onHover: ((enter: boolean) => void) | undefined,
tooltip: ReactNode | undefined,
) => ReactNode;

/** Renders our default cell element, i.e. if no row links and no custom renderCell are used. */
export const defaultRenderFn: (as: RenderAs, colSpan: number) => RenderCellFn<any> =
(as: RenderAs, colSpan) =>
(key, css, content, row, rowStyle, classNames: string | undefined, onClick, onHover, tooltip) => {
(as: RenderAs, colSpan) => (key, css, content, row, rowStyle, classNames: string | undefined, onClick, tooltip) => {
const Cell = as === "table" ? "td" : "div";
return (
<Cell
key={key}
css={{ ...css, ...Css.cursor("default").$ }}
className={classNames}
onClick={onClick}
onMouseEnter={() => onHover?.(true)}
onMouseLeave={() => onHover?.(false)}
{...(as === "table" && { colSpan })}
>
{content}
Expand Down

0 comments on commit 50b9c12

Please sign in to comment.