generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Table): fix drag when virtualization enabled (#7)
Co-authored-by: kseniyakuzina <[email protected]>
- Loading branch information
Showing
7 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/components/__stories__/ReorderingWithVirtualizationDemo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React from 'react'; | ||
|
||
import type {TableProps} from '../../components/Table'; | ||
import {Table} from '../../components/Table'; | ||
import {withTableReorder} from '../../hocs'; | ||
import type {SortableListDragResult} from '../SortableList'; | ||
import {WindowVirtualizationProvider} from '../WindowVirtualizationProvider'; | ||
|
||
const GridWithReordering = withTableReorder(Table); | ||
|
||
const rowHeight = 20; | ||
const estimateRowSize = () => rowHeight; | ||
|
||
export const ReorderingWithVirtualizationDemo = <ItemType extends unknown>( | ||
props: TableProps<ItemType>, | ||
) => { | ||
const {getRowId} = props; | ||
|
||
const [data, setData] = React.useState(props.data); | ||
|
||
const handleDragEnd = React.useCallback( | ||
({draggedItemKey, baseItemKey}: SortableListDragResult) => { | ||
setData((data) => { | ||
const dataClone = data.slice(); | ||
|
||
const index = dataClone.findIndex((item) => getRowId(item) === draggedItemKey); | ||
if (index >= 0) { | ||
const dragged = dataClone.splice(index, 1)[0]!; | ||
|
||
const insertIndex = dataClone.findIndex( | ||
(value) => getRowId(value) === baseItemKey, | ||
); | ||
if (insertIndex >= 0) { | ||
dataClone.splice(insertIndex + 1, 0, dragged); | ||
} else { | ||
dataClone.unshift(dragged); | ||
} | ||
} | ||
|
||
return dataClone; | ||
}); | ||
}, | ||
[getRowId], | ||
); | ||
|
||
return ( | ||
<WindowVirtualizationProvider | ||
estimateRowSize={estimateRowSize} | ||
overscanRowCount={5} | ||
rowsCount={data.length} | ||
> | ||
<GridWithReordering<ItemType> {...props} data={data} onReorder={handleDragEnd} /> | ||
</WindowVirtualizationProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {defaultRangeExtractor} from '@tanstack/react-virtual'; | ||
import type {Range} from '@tanstack/react-virtual'; | ||
|
||
export function getVirtualRangeExtractor(container?: HTMLElement | null) { | ||
return function (range: Range) { | ||
const table = container?.querySelector('[data-draggable-index]'); | ||
const draggableItemIndex = Number(table?.getAttribute('data-draggable-index') ?? -1); | ||
|
||
const result = defaultRangeExtractor(range); | ||
|
||
if (draggableItemIndex !== -1 && !result.includes(draggableItemIndex)) { | ||
result.push(draggableItemIndex); | ||
} | ||
|
||
return result; | ||
}; | ||
} |