Skip to content

Commit e92af42

Browse files
Merge pull request #502 from KevinVandy/414-broken-centered-header
fix: prevent extra padding in centered header cells when no buttons are displayed
2 parents 8d692f0 + b7716f7 commit e92af42

File tree

4 files changed

+5600
-6415
lines changed

4 files changed

+5600
-6415
lines changed

packages/mantine-react-table/src/components/head/MRT_TableHeadCell.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,12 @@ export const MRT_TableHeadCell = <TData extends MRT_RowData>({
129129
const headerPL = useMemo(() => {
130130
let pl = 0;
131131
if (column.getCanSort()) pl++;
132-
if (showColumnButtons) pl += 1.75;
132+
// Only add padding for buttons if they will actually be displayed
133+
if (showColumnButtons && (columnActionsEnabled || showDragHandle))
134+
pl += 1.75;
133135
if (showDragHandle) pl += 1.25;
134136
return pl;
135-
}, [showColumnButtons, showDragHandle]);
137+
}, [showColumnButtons, showDragHandle, columnActionsEnabled]);
136138

137139
const handleDragEnter: DragEventHandler<HTMLTableCellElement> = (_e) => {
138140
if (enableGrouping && hoveredColumn?.id === 'drop-zone') {

packages/mantine-react-table/src/hooks/useMRT_RowVirtualizer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ export const useMRT_RowVirtualizer = <
7373
(row) => row.id === draggingRow?.id,
7474
);
7575

76-
return extraIndexRangeExtractor(range, current_index >= 0 ? current_index: 0);
76+
return extraIndexRangeExtractor(
77+
range,
78+
current_index >= 0 ? current_index : 0,
79+
);
7780
},
7881
[draggingRow],
7982
),
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import { useMemo } from 'react';
2+
3+
import {
4+
MantineReactTable,
5+
type MRT_ColumnDef,
6+
useMantineReactTable,
7+
} from '../../src';
8+
9+
import { type Meta } from '@storybook/react';
10+
11+
const meta: Meta = {
12+
title: 'Fixed Bugs/Cell Alignment',
13+
};
14+
15+
export default meta;
16+
17+
type Person = {
18+
address: string;
19+
city: string;
20+
name: {
21+
firstName: string;
22+
lastName: string;
23+
};
24+
state: string;
25+
};
26+
27+
//nested data is ok, see accessorKeys in ColumnDef below
28+
const data: Person[] = [
29+
{
30+
address: '261 Battle Ford',
31+
city: 'Columbus',
32+
name: {
33+
firstName: 'Zachary',
34+
lastName: 'Davis',
35+
},
36+
state: 'Ohio',
37+
},
38+
{
39+
address: '566 Brakus Inlet',
40+
city: 'Westerville',
41+
name: {
42+
firstName: 'Robert',
43+
lastName: 'Smith',
44+
},
45+
state: 'West Virginia',
46+
},
47+
{
48+
address: '7777 Kuhic Knoll',
49+
city: 'South Linda',
50+
name: {
51+
firstName: 'Kevin',
52+
lastName: 'Yan',
53+
},
54+
state: 'West Virginia',
55+
},
56+
{
57+
address: '722 Emie Stream',
58+
city: 'Huntington',
59+
name: {
60+
firstName: 'John',
61+
lastName: 'Upton',
62+
},
63+
state: 'Washington',
64+
},
65+
{
66+
address: '1 Kuhic Knoll',
67+
city: 'Ohiowa',
68+
name: {
69+
firstName: 'Nathan',
70+
lastName: 'Harris',
71+
},
72+
state: 'Nebraska',
73+
},
74+
];
75+
76+
// Shared columns definition with centered alignment
77+
const createColumns = (): MRT_ColumnDef<Person>[] => [
78+
{
79+
accessorKey: 'name.firstName', //access nested data with dot notation
80+
header: 'First Name',
81+
mantineTableBodyCellProps: {
82+
align: 'center',
83+
},
84+
mantineTableHeadCellProps: {
85+
align: 'center',
86+
},
87+
},
88+
{
89+
accessorKey: 'name.lastName',
90+
header: 'Last Name',
91+
mantineTableBodyCellProps: {
92+
align: 'center',
93+
},
94+
mantineTableHeadCellProps: {
95+
align: 'center',
96+
},
97+
},
98+
{
99+
accessorKey: 'address', //normal accessorKey
100+
header: 'Address',
101+
mantineTableBodyCellProps: {
102+
align: 'center',
103+
},
104+
mantineTableHeadCellProps: {
105+
align: 'center',
106+
},
107+
},
108+
{
109+
accessorKey: 'city',
110+
header: 'City',
111+
mantineTableBodyCellProps: {
112+
align: 'center',
113+
},
114+
mantineTableHeadCellProps: {
115+
align: 'center',
116+
},
117+
},
118+
{
119+
accessorKey: 'state',
120+
header: 'State',
121+
mantineTableBodyCellProps: {
122+
align: 'center',
123+
},
124+
mantineTableHeadCellProps: {
125+
align: 'center',
126+
},
127+
},
128+
];
129+
130+
export const CellAlignment = () => {
131+
//should be memoized or stable
132+
const columns = useMemo<MRT_ColumnDef<Person>[]>(() => createColumns(), []);
133+
134+
const table = useMantineReactTable({
135+
columns,
136+
data,
137+
enableColumnActions: false,
138+
enableSorting: false,
139+
// enableHeaderActionsHoverReveal: true,
140+
mantineTableProps: {
141+
withColumnBorders: true,
142+
withRowBorders: true,
143+
withTableBorder: true,
144+
},
145+
});
146+
147+
return <MantineReactTable table={table} />;
148+
};
149+
150+
export const CellAlignmentWithSorting = () => {
151+
const columns = useMemo<MRT_ColumnDef<Person>[]>(() => createColumns(), []);
152+
153+
const table = useMantineReactTable({
154+
columns,
155+
data,
156+
enableColumnActions: false,
157+
enableSorting: true, // Enable sorting
158+
mantineTableProps: {
159+
withColumnBorders: true,
160+
withRowBorders: true,
161+
withTableBorder: true,
162+
},
163+
});
164+
165+
return <MantineReactTable table={table} />;
166+
};
167+
168+
export const CellAlignmentWithColumnDragging = () => {
169+
const columns = useMemo<MRT_ColumnDef<Person>[]>(() => createColumns(), []);
170+
171+
const table = useMantineReactTable({
172+
columns,
173+
data,
174+
enableColumnActions: true,
175+
enableColumnDragging: true, // Enable column dragging
176+
enableSorting: false,
177+
mantineTableProps: {
178+
withColumnBorders: true,
179+
withRowBorders: true,
180+
withTableBorder: true,
181+
},
182+
});
183+
184+
return <MantineReactTable table={table} />;
185+
};

0 commit comments

Comments
 (0)