sortable updates

This commit is contained in:
Nate Kelley 2025-04-05 19:07:29 -06:00
parent 5796ebf222
commit d1335c7f4c
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
3 changed files with 21 additions and 19 deletions

View File

@ -15,18 +15,24 @@ import {
import { restrictToHorizontalAxis } from '@dnd-kit/modifiers';
import { arrayMove } from '@dnd-kit/sortable';
import { flexRender, Header, Table } from '@tanstack/react-table';
import { useState, useRef, useMemo, useEffect } from 'react';
import React, { useState, useRef, useMemo, useEffect } from 'react';
import { useContextSelector, createContext } from 'use-context-selector';
import { HEADER_HEIGHT } from './constants';
const ACTIVATION_CONSTRAINT = {
activationConstraint: {
distance: 2
}
};
export const SortColumnWrapper: React.FC<{
table: Table<Record<string, string | number | Date | null>>;
sortable: boolean;
draggable: boolean;
children: React.ReactNode;
colOrder: string[];
setColOrder: (colOrder: string[]) => void;
onReorderColumns?: (colOrder: string[]) => void;
}> = ({ table, sortable, children, colOrder, setColOrder, onReorderColumns }) => {
}> = React.memo(({ table, draggable, children, colOrder, setColOrder, onReorderColumns }) => {
// Track active drag item and over target
const [activeId, setActiveId] = useState<string | null>(null);
const [overTargetId, setOverTargetId] = useState<string | null>(null);
@ -42,16 +48,8 @@ export const SortColumnWrapper: React.FC<{
const memoizedModifiers = useMemo(() => [restrictToHorizontalAxis], []);
const sensors = useSensors(
useSensor(MouseSensor, {
activationConstraint: {
distance: 2
}
}),
useSensor(TouchSensor, {
activationConstraint: {
distance: 2
}
}),
useSensor(MouseSensor, ACTIVATION_CONSTRAINT),
useSensor(TouchSensor, ACTIVATION_CONSTRAINT),
useSensor(KeyboardSensor)
);
@ -128,7 +126,7 @@ export const SortColumnWrapper: React.FC<{
};
}, []);
if (!sortable) return <>{children}</>;
if (!draggable) return <>{children}</>;
return (
<DndContext
@ -151,7 +149,9 @@ export const SortColumnWrapper: React.FC<{
</SortColumnContext.Provider>
</DndContext>
);
};
});
SortColumnWrapper.displayName = 'SortColumnWrapper';
type SortColumnContextType = {
overTargetId: string | null;

View File

@ -75,7 +75,8 @@ export const NonResizable: Story = {
args: {
...meta.args,
rows: sampleData,
resizable: false
resizable: false,
draggable: true
}
};
@ -83,7 +84,8 @@ export const NonSortable: Story = {
args: {
...meta.args,
rows: sampleData,
sortable: false
sortable: false,
draggable: true
}
};

View File

@ -16,7 +16,7 @@ import { DataGridRow } from './DataGridRow';
import { CELL_HEIGHT, OVERSCAN } from './constants';
import { initializeColumnWidths } from './initializeColumnWidths';
import { SortColumnWrapper } from './SortColumnWrapper';
import { useDebounceFn, useThrottleFn } from '@/hooks';
import { useDebounceFn } from '@/hooks';
export interface TanStackDataGridProps {
className?: string;
@ -133,7 +133,7 @@ export const TanStackDataGrid: React.FC<TanStackDataGridProps> = React.memo(
<div ref={parentRef} className={cn('h-full w-full overflow-auto', className)}>
<SortColumnWrapper
table={table}
sortable={sortable}
draggable={draggable}
colOrder={colOrder}
setColOrder={setColOrder}
onReorderColumns={onReorderColumns}>