Skip to content

Commit fbc251f

Browse files
fix(apollo-vertex): suppress data-table row onClick after text drag-select
A finished text selection inside a row was firing onRowClick on mouseup. Skip the click when the pointer moved beyond a small threshold AND the resulting selection is a Range. Wires onRowClick into the demo (toggles row expansion) so the behavior is testable. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 68f54f3 commit fbc251f

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

apps/apollo-vertex/registry/data-table/data-table.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ function DataTable<TData, TValue>({
129129
const isRowSelectionEnabled = !!(rowSelection && onRowSelectionChange);
130130
const isPaginationEnabled = !!(pagination && onPaginationChange);
131131

132+
// Distinguish a click from a drag-select. A pure click can still register a
133+
// 1–2px Range due to mouse drift, so we only suppress onRowClick when the
134+
// pointer actually moved beyond a small threshold AND a Range exists.
135+
// https://github.com/mui/mui-x/issues/1172
136+
const pressOriginRef = React.useRef<{ x: number; y: number } | null>(null);
137+
132138
const resetPageIndex = () => {
133139
onPaginationChange?.((prev) =>
134140
prev.pageIndex === 0 ? prev : { ...prev, pageIndex: 0 },
@@ -289,7 +295,27 @@ function DataTable<TData, TValue>({
289295
getRowClassName?.(row.original),
290296
)}
291297
{...(onRowClick && {
292-
onClick: () => onRowClick(row.original),
298+
onMouseDown: (e: React.MouseEvent) => {
299+
pressOriginRef.current = {
300+
x: e.clientX,
301+
y: e.clientY,
302+
};
303+
},
304+
onClick: (e: React.MouseEvent<HTMLTableRowElement>) => {
305+
const origin = pressOriginRef.current;
306+
pressOriginRef.current = null;
307+
const moved =
308+
!!origin &&
309+
(Math.abs(e.clientX - origin.x) > 5 ||
310+
Math.abs(e.clientY - origin.y) > 5);
311+
if (
312+
moved &&
313+
window.getSelection()?.type === "Range"
314+
) {
315+
return;
316+
}
317+
onRowClick(row.original);
318+
},
293319
})}
294320
>
295321
{row.getVisibleCells().map((cell) => (

apps/apollo-vertex/templates/DataTableTemplate.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,15 @@ function DataTableTemplateContent() {
310310
<div className="p-4">
311311
<DataTable
312312
{...tableState}
313+
getRowId={(payment) => payment.id}
313314
expanded={expanded}
314315
onExpandedChange={setExpanded}
316+
onRowClick={(payment) =>
317+
setExpanded((prev) => {
318+
if (prev === true) return { [payment.id]: false };
319+
return { ...prev, [payment.id]: !prev[payment.id] };
320+
})
321+
}
315322
renderExpandedRow={(row) => {
316323
const payment = row.original;
317324
return (

0 commit comments

Comments
 (0)