Skip to content

Add CalculatedColumn.cellStyle and RowRendererProps.rowStyle attributes #2984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -234,6 +234,10 @@ function MyGrid() {

###### `rowClass?: Maybe<(row: R) => Maybe<string>>`

###### `rowStyle?: Maybe<(row: R) => Maybe<CSSProperties>>`

This property sets the passed JSX style to the row.

##### `direction?: Maybe<'ltr' | 'rtl'>`

This property sets the text direction of the grid, it defaults to `'ltr'` (left-to-right). Setting `direction` to `'rtl'` has the following effects:
5 changes: 3 additions & 2 deletions src/Cell.tsx
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ function Cell<R, SR>({
}: CellRendererProps<R, SR>) {
const { ref, tabIndex, onFocus } = useRovingCellRef(isCellSelected);

const { cellClass } = column;
const { cellClass, cellStyle } = column;
const className = getCellClassname(
column,
{
@@ -46,6 +46,7 @@ function Cell<R, SR>({
},
typeof cellClass === 'function' ? cellClass(row) : cellClass
);
const style = typeof cellStyle === 'function' ? cellStyle(row) : cellStyle;

function selectCellWrapper(openEditor?: boolean | null) {
selectCell(row, column, openEditor);
@@ -79,7 +80,7 @@ function Cell<R, SR>({
ref={ref}
tabIndex={tabIndex}
className={className}
style={getCellStyle(column, colSpan)}
style={getCellStyle(column, colSpan, style)}
onClick={handleClick}
onDoubleClick={handleDoubleClick}
onContextMenu={handleContextMenu}
5 changes: 4 additions & 1 deletion src/DataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { forwardRef, useState, useRef, useImperativeHandle, useCallback, useMemo } from 'react';
import type { Key, RefAttributes } from 'react';
import type { Key, RefAttributes, CSSProperties } from 'react';
import clsx from 'clsx';

import {
@@ -175,6 +175,7 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
*/
renderers?: Maybe<Renderers<R, SR>>;
rowClass?: Maybe<(row: R) => Maybe<string>>;
rowStyle?: Maybe<CSSProperties | ((row: R) => Maybe<CSSProperties>)>;
direction?: Maybe<Direction>;
'data-testid'?: Maybe<string>;
}
@@ -224,6 +225,7 @@ function DataGrid<R, SR, K extends Key>(
className,
style,
rowClass,
rowStyle,
direction,
// ARIA
'aria-label': ariaLabel,
@@ -1070,6 +1072,7 @@ function DataGrid<R, SR, K extends Key>(
onRowClick,
onRowDoubleClick,
rowClass,
rowStyle,
gridRowStart,
height: getRowHeight(rowIdx),
copiedCellIdx:
5 changes: 3 additions & 2 deletions src/EditCell.tsx
Original file line number Diff line number Diff line change
@@ -95,13 +95,14 @@ export default function EditCell<R, SR>({
}
}

const { cellClass } = column;
const { cellClass, cellStyle } = column;
const className = getCellClassname(
column,
'rdg-editor-container',
!column.editorOptions?.renderFormatter && cellEditing,
typeof cellClass === 'function' ? cellClass(row) : cellClass
);
const style = typeof cellStyle === 'function' ? cellStyle(row) : cellStyle;

return (
<div
@@ -110,7 +111,7 @@ export default function EditCell<R, SR>({
aria-colspan={colSpan}
aria-selected
className={className}
style={getCellStyle(column, colSpan)}
style={getCellStyle(column, colSpan, style)}
onKeyDown={onKeyDown}
onMouseDownCapture={commitOnOutsideClick ? cancelFrameRequest : undefined}
>
2 changes: 1 addition & 1 deletion src/HeaderCell.tsx
Original file line number Diff line number Diff line change
@@ -174,7 +174,7 @@ export default function HeaderCell<R, SR>({
tabIndex={shouldFocusGrid ? 0 : tabIndex}
className={className}
style={{
...getCellStyle(column, colSpan),
...getCellStyle(column, colSpan, column.headerCellStyle),
minWidth: column.minWidth,
maxWidth: column.maxWidth ?? undefined
}}
5 changes: 4 additions & 1 deletion src/Row.tsx
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ function Row<R, SR>(
onRowClick,
onRowDoubleClick,
rowClass,
rowStyle,
setDraggedOverRowIdx,
onMouseEnter,
onRowChange,
@@ -53,6 +54,8 @@ function Row<R, SR>(
className
);

rowStyle = typeof rowStyle === 'function' ? rowStyle(row) : rowStyle;

const cells = [];

for (let index = 0; index < viewportColumns.length; index++) {
@@ -94,7 +97,7 @@ function Row<R, SR>(
ref={ref}
className={className}
onMouseEnter={handleDragEnter}
style={getRowStyle(gridRowStart, height)}
style={getRowStyle(gridRowStart, height, rowStyle)}
{...props}
>
{cells}
5 changes: 3 additions & 2 deletions src/SummaryCell.tsx
Original file line number Diff line number Diff line change
@@ -27,12 +27,13 @@ function SummaryCell<R, SR>({
selectCell
}: SummaryCellProps<R, SR>) {
const { ref, tabIndex, onFocus } = useRovingCellRef(isCellSelected);
const { summaryCellClass } = column;
const { summaryCellClass, summaryCellStyle } = column;
const className = getCellClassname(
column,
summaryCellClassname,
typeof summaryCellClass === 'function' ? summaryCellClass(row) : summaryCellClass
);
const style = typeof summaryCellStyle === 'function' ? summaryCellStyle(row) : summaryCellStyle;

function onClick() {
selectCell(row, column);
@@ -47,7 +48,7 @@ function SummaryCell<R, SR>({
ref={ref}
tabIndex={tabIndex}
className={className}
style={getCellStyle(column, colSpan)}
style={getCellStyle(column, colSpan, style)}
onClick={onClick}
onFocus={onFocus}
>
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Key, ReactElement, ReactNode, RefObject } from 'react';
import type { Key, ReactElement, ReactNode, RefObject, CSSProperties } from 'react';

export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

@@ -15,9 +15,14 @@ export interface Column<TRow, TSummaryRow = unknown> {
readonly minWidth?: Maybe<number>;
/** Maximum column width in px. */
readonly maxWidth?: Maybe<number>;
/** Standard, header and summary cell style */
readonly cellClass?: Maybe<string | ((row: TRow) => Maybe<string>)>;
readonly headerCellClass?: Maybe<string>;
readonly summaryCellClass?: Maybe<string | ((row: TSummaryRow) => Maybe<string>)>;
/** Standard, header and summary cell style */
readonly cellStyle?: Maybe<CSSProperties | ((row: TRow) => Maybe<CSSProperties>)>;
readonly headerCellStyle?: Maybe<CSSProperties>;
readonly summaryCellStyle?: Maybe<CSSProperties | ((row: TSummaryRow) => Maybe<CSSProperties>)>;
/** Formatter to be used to render the cell content */
readonly formatter?: Maybe<(props: FormatterProps<TRow, TSummaryRow>) => ReactNode>;
/** Formatter to be used to render the summary cell content */
@@ -143,6 +148,7 @@ export interface RowRendererProps<TRow, TSummaryRow = unknown>
onRowClick: Maybe<(row: TRow, column: CalculatedColumn<TRow, TSummaryRow>) => void>;
onRowDoubleClick: Maybe<(row: TRow, column: CalculatedColumn<TRow, TSummaryRow>) => void>;
rowClass: Maybe<(row: TRow) => Maybe<string>>;
rowStyle: Maybe<CSSProperties | ((row: TRow) => Maybe<CSSProperties>)>;
setDraggedOverRowIdx: ((overRowIdx: number) => void) | undefined;
selectCell: (
row: TRow,
17 changes: 12 additions & 5 deletions src/utils/styleUtils.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import type { CSSProperties } from 'react';
import clsx from 'clsx';

import type { CalculatedColumn } from '../types';
import type { Maybe, CalculatedColumn } from '../types';
import { cellClassname, cellFrozenClassname, cellFrozenLastClassname } from '../style';

export function getRowStyle(rowIdx: number, height?: number): CSSProperties {
export function getRowStyle(
rowIdx: number,
height?: number,
extraStyles?: Maybe<CSSProperties>
): CSSProperties {
if (height !== undefined) {
return {
'--rdg-grid-row-start': rowIdx,
'--rdg-row-height': `${height}px`
'--rdg-row-height': `${height}px`,
...extraStyles
} as unknown as CSSProperties;
}
return { '--rdg-grid-row-start': rowIdx } as unknown as CSSProperties;
}

export function getCellStyle<R, SR>(
column: CalculatedColumn<R, SR>,
colSpan?: number
colSpan?: number,
extraStyles?: Maybe<CSSProperties>
): React.CSSProperties {
return {
gridColumnStart: column.idx + 1,
gridColumnEnd: colSpan !== undefined ? `span ${colSpan}` : undefined,
insetInlineStart: column.frozen ? `var(--rdg-frozen-left-${column.idx})` : undefined
insetInlineStart: column.frozen ? `var(--rdg-frozen-left-${column.idx})` : undefined,
...extraStyles
};
}