Skip to content

Commit 322cb80

Browse files
committed
Fix #700 - Hydration error in row dragging example
1 parent 4c2acfd commit 322cb80

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.draggableRow[data-is-dragging='true'] td {
2+
opacity: 0.75;
3+
border: rem(1px) solid light-dark(var(--mantine-color-gray-3), var(--mantine-color-dark-4));
4+
}

app/examples/row-dragging/RowDraggingExample.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
'use client';
22

33
import { DragDropContext, Draggable, type DropResult, Droppable } from '@hello-pangea/dnd';
4-
import { TableTd } from '@mantine/core';
4+
import { Center, TableTd } from '@mantine/core';
55
import { notifications } from '@mantine/notifications';
66
import { IconGripVertical } from '@tabler/icons-react';
77
import type { DataTableColumn } from '__PACKAGE__';
88
import { DataTable, DataTableDraggableRow } from '__PACKAGE__';
9+
import clsx from 'clsx';
910
import { useState } from 'react';
1011
import companies from '~/data/companies.json';
12+
import classes from './RowDraggingExample.module.css';
1113

1214
interface RecordData {
1315
id: string;
@@ -69,10 +71,18 @@ export function RowDraggingExample() {
6971
rowFactory={({ record, index, rowProps, children }) => (
7072
<Draggable key={record.id} draggableId={record.id} index={index}>
7173
{(provided, snapshot) => (
72-
<DataTableDraggableRow isDragging={snapshot.isDragging} {...rowProps} {...provided.draggableProps}>
73-
{/** custom drag handle */}
74-
<TableTd {...provided.dragHandleProps} ref={provided.innerRef}>
75-
<IconGripVertical size={16} />
74+
<DataTableDraggableRow
75+
isDragging={snapshot.isDragging}
76+
{...rowProps}
77+
// 👇 custom class name for styling
78+
className={clsx(rowProps.className, classes.draggableRow)}
79+
{...provided.draggableProps}
80+
>
81+
<TableTd>
82+
{/** 👇 custom drag handle */}
83+
<Center {...provided.dragHandleProps} ref={provided.innerRef}>
84+
<IconGripVertical size={16} />
85+
</Center>
7686
</TableTd>
7787
{children}
7888
</DataTableDraggableRow>

app/examples/row-dragging/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const metadata = getRouteMetadata(PATH);
1717
export default async function BasicUsageExamplePage() {
1818
const code = await allPromiseProps({
1919
'RowDraggingExample.tsx': readCodeFile<string>(`${PATH}/RowDraggingExample.tsx`),
20+
'RowDraggingExample.module.css': readCodeFile<string>(`${PATH}/RowDraggingExample.module.css`),
2021
'companies.json': readCodeFile<string>('/../data/companies.json'),
2122
});
2223

@@ -30,7 +31,7 @@ export default async function BasicUsageExamplePage() {
3031
<br />
3132
Here is how you would implement it in your project:
3233
</Txt>
33-
<CodeBlock tabs={{ code, keys: ['RowDraggingExample.tsx', 'companies.json'] }} />
34+
<CodeBlock tabs={{ code, keys: ['RowDraggingExample.tsx', 'RowDraggingExample.module.css', 'companies.json'] }} />
3435
<Txt>The code above will produce the following result:</Txt>
3536
<RowDraggingExample />
3637

package/DataTableDraggableRow.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import { TableTr } from '@mantine/core';
22
import { useMergedRef } from '@mantine/hooks';
3-
import { forwardRef, useEffect, useRef } from 'react';
3+
import type { Ref } from 'react';
4+
import { useEffect, useRef } from 'react';
45
import type { DataTableDraggableRowProps } from './types';
56

6-
const DataTableDraggableRow = forwardRef<HTMLTableRowElement, DataTableDraggableRowProps>(function (
7-
{ children, isDragging, ...props },
8-
passedRef
9-
) {
7+
export function DataTableDraggableRow({
8+
className,
9+
children,
10+
isDragging,
11+
ref: refProp,
12+
...otherProps
13+
}: DataTableDraggableRowProps & {
14+
ref?: Ref<HTMLTableRowElement>;
15+
}) {
1016
const ref = useRef<HTMLTableRowElement>(null);
11-
const mergedRef = useMergedRef(ref, passedRef);
17+
const mergedRef = useMergedRef(ref, refProp);
1218

1319
useEffect(() => {
1420
// a simple fix to keep column width as in table
@@ -34,12 +40,8 @@ const DataTableDraggableRow = forwardRef<HTMLTableRowElement, DataTableDraggable
3440
}, [isDragging, children]);
3541

3642
return (
37-
<TableTr data-is-dragging={isDragging} ref={mergedRef} {...props}>
43+
<TableTr data-is-dragging={isDragging} ref={mergedRef} {...otherProps} className={className}>
3844
{children}
3945
</TableTr>
4046
);
41-
});
42-
43-
DataTableDraggableRow.displayName = 'DataTableDraggableRow';
44-
45-
export { DataTableDraggableRow };
47+
}

package/types/DataTableDraggableRowProps.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { TableTrProps } from '@mantine/core';
22

33
export type DataTableDraggableRowProps = {
4+
/**
5+
* Optional class name.
6+
*/
7+
className?: string;
8+
49
/**
510
* Current dragging status.
611
*/

0 commit comments

Comments
 (0)