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

View File

@ -75,7 +75,8 @@ export const NonResizable: Story = {
args: { args: {
...meta.args, ...meta.args,
rows: sampleData, rows: sampleData,
resizable: false resizable: false,
draggable: true
} }
}; };
@ -83,7 +84,8 @@ export const NonSortable: Story = {
args: { args: {
...meta.args, ...meta.args,
rows: sampleData, 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 { CELL_HEIGHT, OVERSCAN } from './constants';
import { initializeColumnWidths } from './initializeColumnWidths'; import { initializeColumnWidths } from './initializeColumnWidths';
import { SortColumnWrapper } from './SortColumnWrapper'; import { SortColumnWrapper } from './SortColumnWrapper';
import { useDebounceFn, useThrottleFn } from '@/hooks'; import { useDebounceFn } from '@/hooks';
export interface TanStackDataGridProps { export interface TanStackDataGridProps {
className?: string; 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)}> <div ref={parentRef} className={cn('h-full w-full overflow-auto', className)}>
<SortColumnWrapper <SortColumnWrapper
table={table} table={table}
sortable={sortable} draggable={draggable}
colOrder={colOrder} colOrder={colOrder}
setColOrder={setColOrder} setColOrder={setColOrder}
onReorderColumns={onReorderColumns}> onReorderColumns={onReorderColumns}>