Skip to content

Commit 0539cf6

Browse files
author
Roman Snapko
authored
Add cellClassName prop to DataTable component with column meta support (#2197)
<!-- Ensure the title clearly reflects what was changed. Provide a clear and concise description of the changes made. The PR should only contain the changes related to the issue, and no other unrelated changes. --> Fixes OPS-3928
1 parent ed9ced3 commit 0539cf6

2 files changed

Lines changed: 44 additions & 21 deletions

File tree

packages/ui-components/src/stories/data-table.stories.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ export const WithFetcher: Story = {
129129
},
130130
};
131131

132+
export const MutedBackground: Story = {
133+
args: {
134+
loading: false,
135+
cellClassName: 'bg-muted',
136+
},
137+
};
138+
132139
export const StickyHeader: Story = {
133140
args: {
134141
loading: false,

packages/ui-components/src/ui/data-table.tsx

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ interface DataTableProps<
9999
actions?: DataTableAction<TData>[];
100100
stickyHeader?: boolean;
101101
border?: boolean;
102+
cellClassName?: string;
102103
emptyStateComponent?: React.ReactNode;
103104
getRowHref?: (row: RowDataWithActions<TData>) => string | undefined;
104105
navigationExcludedColumns?: string[];
@@ -122,6 +123,7 @@ export function DataTable<
122123
onSelectedRowsChange,
123124
stickyHeader = false,
124125
border = true,
126+
cellClassName,
125127
emptyStateComponent,
126128
getRowHref,
127129
navigationExcludedColumns,
@@ -329,8 +331,12 @@ export function DataTable<
329331
{table.getHeaderGroups().map((headerGroup) => (
330332
<TableRow key={headerGroup.id}>
331333
{headerGroup.headers.map((header) => {
334+
const meta = header.column.columnDef.meta as
335+
| { className?: string }
336+
| undefined;
337+
332338
return (
333-
<TableHead key={header.id}>
339+
<TableHead key={header.id} className={meta?.className}>
334340
{header.isPlaceholder
335341
? null
336342
: flexRender(
@@ -364,28 +370,38 @@ export function DataTable<
364370
className={onRowClick ? 'cursor-pointer' : ''}
365371
data-state={row.getIsSelected() && 'selected'}
366372
>
367-
{row.getVisibleCells().map((cell) => (
368-
<TableCell key={cell.id}>
369-
{rowHref &&
370-
!navigationExcludedColumns?.includes(cell.column.id) ? (
371-
<Link
372-
to={rowHref}
373-
onClick={(e) => e.stopPropagation()}
374-
rel="noopener noreferrer"
375-
>
376-
{flexRender(
373+
{row.getVisibleCells().map((cell) => {
374+
const meta = cell.column.columnDef.meta as
375+
| { className?: string }
376+
| undefined;
377+
return (
378+
<TableCell
379+
key={cell.id}
380+
className={cn(meta?.className, cellClassName)}
381+
>
382+
{rowHref &&
383+
!navigationExcludedColumns?.includes(
384+
cell.column.id,
385+
) ? (
386+
<Link
387+
to={rowHref}
388+
onClick={(e) => e.stopPropagation()}
389+
rel="noopener noreferrer"
390+
>
391+
{flexRender(
392+
cell.column.columnDef.cell,
393+
cell.getContext(),
394+
)}
395+
</Link>
396+
) : (
397+
flexRender(
377398
cell.column.columnDef.cell,
378399
cell.getContext(),
379-
)}
380-
</Link>
381-
) : (
382-
flexRender(
383-
cell.column.columnDef.cell,
384-
cell.getContext(),
385-
)
386-
)}
387-
</TableCell>
388-
))}
400+
)
401+
)}
402+
</TableCell>
403+
);
404+
})}
389405
</TableRow>
390406
);
391407
})

0 commit comments

Comments
 (0)