Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug #1041 #1217 #1361

Open
wants to merge 5 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions packages/material-react-table/src/hooks/useMRT_RowVirtualizer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import { useCallback, useMemo } from 'react';
import { type Range, useVirtualizer } from '@tanstack/react-virtual';
import {
type MRT_Row,
Expand Down Expand Up @@ -36,7 +36,19 @@ export const useMRT_RowVirtualizer = <
table,
});

const rowCount = rows?.length ?? getRowModel().rows.length;
const realRows = rows ?? getRowModel().rows;
/**
* when filtering, should find the correct index in filtered rows
*/
const draggingRowIndex = useMemo(
() =>
draggingRow?.id
? realRows.findIndex((r) => r.id === draggingRow?.id)
: undefined,
[realRows, draggingRow?.id],
);

const rowCount = realRows.length;

const normalRowHeight =
density === 'compact' ? 37 : density === 'comfortable' ? 58 : 73;
Expand All @@ -58,9 +70,9 @@ export const useMRT_RowVirtualizer = <
overscan: 4,
rangeExtractor: useCallback(
(range: Range) => {
return extraIndexRangeExtractor(range, draggingRow?.index ?? 0);
return extraIndexRangeExtractor(range, draggingRowIndex);
},
[draggingRow],
[draggingRowIndex],
),
...rowVirtualizerProps,
}) as unknown as MRT_RowVirtualizer<TScrollElement, TItemElement>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { type Range, defaultRangeExtractor } from '@tanstack/react-virtual';

/**
* When scroll, the `draggingRow` or `draggingColumn` can be removed from document because of virtualization,
* then, the `dragEnd` event on `MRT_TableBodyRowGrabHandle` or `MRT_TableHeadCellGrabHandle` will not fire.
* We should keep the `draggingRow` or `draggingColumn` in `getVirtualItems()` to avoid this thing.
*/
export const extraIndexRangeExtractor = (
range: Range,
draggingIndex?: number,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { faker } from '@faker-js/faker';
import { type Meta } from '@storybook/react';
import { useState } from 'react';
import {
MaterialReactTable,
useMaterialReactTable,
type MRT_ColumnDef,
} from '../../src';
const meta: Meta = {
title: 'Fixed Bugs/dragging virtual when filtered',
};
export default meta;
const initData = [...Array(25)].map(() => ({
age: faker.number.int(20) + 18,
email: faker.internet.email(),
firstName: faker.person.firstName(),
id: faker.string.alphanumeric(6),
lastName: faker.person.lastName(),
}));
initData.push({
age: 18,
email: 'info@example.com',
firstName: 'Foobar',
lastName: 'Baz',
id: '1',
});
const columns: MRT_ColumnDef<(typeof initData)[0]>[] = [
{
accessorKey: 'id',
header: 'ID',
},
{
accessorKey: 'firstName',
header: 'First Name',
},
{
accessorKey: 'lastName',
header: 'Last Name',
},
{
accessorKey: 'email',
header: 'Email Address',
},
{
accessorKey: 'age',
header: 'Age',
},
{
accessorKey: 'state',
header: 'State',
},
];
export const DraggingRowWhenFiltered = () => {
const [data, _setData] = useState(() => initData);
const t = useMaterialReactTable({
enableRowVirtualization: true,
enableRowNumbers: true,
columns: columns,
data: data,
enableRowDragging: true,
initialState: {
density: 'compact',
columnFilters: [{ id: 'firstName', value: 'foo' }],
showColumnFilters: true,
},
});
return <MaterialReactTable table={t} />;
};
Loading