Skip to content

Commit

Permalink
fix(BaseTable): do not import dnd-kit if reordering is not used (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
beliarh authored Sep 9, 2024
1 parent 0c88835 commit 31ef94d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/components/BaseDraggableRow/BaseDraggableRow.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';

import {useSortable} from '@dnd-kit/sortable';
import {useForkRef} from '@gravity-ui/uikit';
import type {Row as RowType} from '@tanstack/react-table';

Expand All @@ -25,15 +24,25 @@ export const BaseDraggableRow = React.forwardRef(
}: BaseDraggableRowProps<TData, TScrollElement>,
ref: React.Ref<HTMLTableRowElement>,
) => {
const {setNodeRef, transform, transition, isDragging} = useSortable({
id: row.id,
});

const {
isChildMode,
activeItemKey,
targetItemIndex = -1,
// The `useSortable` hook is provided by `@dnd-kit/sortable` library and imported via `SortableListContext`.
// This is a temporary solution to prevent importing the entire `@dnd-kit` library
// when the user doesn't use the reordering feature.
useSortable,
} = React.useContext(SortableListContext) ?? {};

const {
setNodeRef,
transform = null,
transition,
isDragging = false,
} = useSortable?.({
id: row.id,
}) || {};

const {enableNesting} = React.useContext(TableContext);

const isDragActive = Boolean(activeItemKey);
Expand Down
3 changes: 2 additions & 1 deletion src/components/SortableList/SortableList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import {SortableContext, verticalListSortingStrategy} from '@dnd-kit/sortable';
import {SortableContext, useSortable, verticalListSortingStrategy} from '@dnd-kit/sortable';

import type {UseSortableListParams} from '../../hooks';
import {useSortableList} from '../../hooks';
Expand Down Expand Up @@ -39,6 +39,7 @@ export const SortableList = ({
isNextChildMode,
isParentMode,
targetItemIndex,
useSortable,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[activeItemKey, isChildMode, isNextChildMode, isParentMode, targetItemIndex],
Expand Down
6 changes: 5 additions & 1 deletion src/components/SortableListContext/SortableListContext.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import React from 'react';

import type {useSortable} from '@dnd-kit/sortable';

import type {useSortableList} from '../../hooks';

type SortableListContextValue = ReturnType<typeof useSortableList>;
type SortableListContextValue = ReturnType<typeof useSortableList> & {
useSortable?: typeof useSortable;
};

export const SortableListContext = React.createContext<SortableListContextValue | undefined>(
undefined,
Expand Down

0 comments on commit 31ef94d

Please sign in to comment.