Skip to content

Commit 7c2244b

Browse files
Copilotjacksonkasi1
andcommitted
Fix sub-row data table issues: resolve double indentation, improve selection functionality, and ensure proper TypeScript compilation
Co-authored-by: jacksonkasi1 <109948871+jacksonkasi1@users.noreply.github.com>
1 parent a9b22aa commit 7c2244b

5 files changed

Lines changed: 18 additions & 19 deletions

File tree

src/app/(home)/example/ecommerce-orders/data-table/components/columns.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const baseColumns: ColumnDef<ExportableData>[] = [
2323
);
2424
} else {
2525
return (
26-
<div className="pl-6 text-muted-foreground">
26+
<div className="text-muted-foreground">
2727
{String(data.productName || '')}
2828
</div>
2929
);
@@ -47,7 +47,7 @@ const baseColumns: ColumnDef<ExportableData>[] = [
4747
} else {
4848
const category = String(data.category || '');
4949
return (
50-
<div className="pl-6">
50+
<div>
5151
<Badge className={getCategoryColor(category)}>
5252
{category}
5353
</Badge>
@@ -71,7 +71,7 @@ const baseColumns: ColumnDef<ExportableData>[] = [
7171
);
7272
} else {
7373
return (
74-
<div className="pl-6 text-muted-foreground">
74+
<div className="text-muted-foreground">
7575
{String(data.brand || '')}
7676
</div>
7777
);
@@ -93,7 +93,7 @@ const baseColumns: ColumnDef<ExportableData>[] = [
9393
);
9494
} else {
9595
return (
96-
<div className="pl-6 font-mono">
96+
<div className="font-mono">
9797
${Number(data.price || 0).toFixed(2)} × {Number(data.quantity || 0)}
9898
</div>
9999
);

src/app/(home)/example/ecommerce-orders/data-table/utils/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const tableConfig: TableConfig = {
2020
enableExpanding: true,
2121
paginateExpandedRows: false,
2222
filterFromLeafRows: true,
23+
subRowIndentPx: 24, // 24px indentation for sub-rows
2324
};
2425

2526
export const useExportConfig = () => ({

src/app/(home)/example/users-with-sub-rows/data-table/components/columns.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,23 @@ import { Checkbox } from "@/components/ui/checkbox";
66
import type { ExportableData } from "@/components/data-table/utils/export-utils";
77
import { withExpandingColumn } from "@/components/data-table/utils/expanding-utils";
88

9-
const getBaseColumns = (subRowIndentPx: number = 0): ColumnDef<ExportableData>[] => [
9+
const getBaseColumns = (): ColumnDef<ExportableData>[] => [
1010
{
1111
accessorKey: "name",
1212
header: "Name / Expense",
1313
cell: ({ row }) => {
1414
const data = row.original;
1515
const isUser = 'email' in data && 'department' in data;
16-
const depth = row.depth || 0;
17-
const indentStyle = depth > 0 && subRowIndentPx > 0 ? { paddingLeft: `${depth * subRowIndentPx}px` } : {};
1816

1917
if (isUser) {
2018
return (
21-
<div className="font-medium" style={indentStyle}>
19+
<div className="font-medium">
2220
{String(data.name || '')}
2321
</div>
2422
);
2523
} else {
2624
return (
27-
<div style={indentStyle}>
25+
<div>
2826
{String(data.expenseName || '')}
2927
</div>
3028
);
@@ -100,11 +98,10 @@ const getBaseColumns = (subRowIndentPx: number = 0): ColumnDef<ExportableData>[]
10098
];
10199

102100
export const getColumns = (
103-
handleRowDeselection: ((rowId: string) => void) | null | undefined,
104-
subRowIndentPx: number = 0
101+
handleRowDeselection: ((rowId: string) => void) | null | undefined
105102
): ColumnDef<ExportableData>[] => {
106103
// Start with base columns
107-
let columns = [...getBaseColumns(subRowIndentPx)];
104+
let columns = [...getBaseColumns()];
108105

109106
// Add selection column if row selection is enabled
110107
if (handleRowDeselection !== null) {

src/app/(home)/example/users-with-sub-rows/data-table/utils/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const tableConfig: TableConfig = {
2020
enableExpanding: true,
2121
paginateExpandedRows: false,
2222
filterFromLeafRows: true,
23+
subRowIndentPx: 24, // 24px indentation for sub-rows
2324
};
2425

2526
export const useExportConfig = () => ({

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -506,16 +506,16 @@ export function DataTable<TData extends ExportableData, TValue>({
506506
// Get columns with the deselection handler and sub-row support (memoize to avoid recreation on render)
507507
const columns = useMemo(() => {
508508
// Only pass deselection handler if row selection is enabled
509-
let processedColumns = getColumns(tableConfig.enableRowSelection ? handleRowDeselection : null, tableConfig.subRowIndentPx);
509+
let processedColumns = getColumns(tableConfig.enableRowSelection ? handleRowDeselection : null);
510510

511511
// Process columns for sub-row support if configured
512512
if (subRows) {
513-
processedColumns = subRows.processColumns(processedColumns);
513+
processedColumns = subRows.processColumns(processedColumns as ColumnDef<TData>[]) as ColumnDef<TData, TValue>[];
514514
}
515515

516516
// Add expanding column if expanding is enabled
517517
return withExpandingColumn(processedColumns, tableConfig.enableExpanding || !!effectiveGetSubRows);
518-
}, [getColumns, handleRowDeselection, tableConfig.enableRowSelection, tableConfig.enableExpanding, tableConfig.subRowIndentPx, subRows, effectiveGetSubRows]);
518+
}, [getColumns, handleRowDeselection, tableConfig.enableRowSelection, tableConfig.enableExpanding, subRows, effectiveGetSubRows]);
519519

520520
// Create event handlers using utility functions
521521
const handleSortingChange = useCallback(
@@ -700,7 +700,7 @@ export function DataTable<TData extends ExportableData, TValue>({
700700
manualRows.push(row);
701701

702702
// Check if this row is expanded
703-
const isRowExpanded = expandedState[row.id] || expandedState[row.index];
703+
const isRowExpanded = (expandedState as any)[row.id] || (expandedState as any)[row.index];
704704

705705
if (isRowExpanded && effectiveGetSubRows) {
706706
const subRows = effectiveGetSubRows(row.original);
@@ -744,10 +744,10 @@ export function DataTable<TData extends ExportableData, TValue>({
744744
// Return cells for the sub-row using the same column structure
745745
return columns.map((column, cellIndex) => {
746746
const cellValue = (() => {
747-
if (typeof column.accessorKey === 'string') {
748-
return subRowData[column.accessorKey];
747+
if ('accessorKey' in column && typeof column.accessorKey === 'string') {
748+
return (subRowData as any)[column.accessorKey];
749749
}
750-
if (typeof column.accessorFn === 'function') {
750+
if ('accessorFn' in column && typeof column.accessorFn === 'function') {
751751
return column.accessorFn(subRowData, subIndex);
752752
}
753753
return undefined;

0 commit comments

Comments
 (0)