React Window Sortable
This library provides drag and drop sort functionality for very large lists.
This library wraps Brian Vaughn's excellent library react-window and
adds drag and drop sorting.
Features
- Drag and drop to re-order list items
- Start drag with customized element or button
- Auto-scrolls when you drag something near the start or end of scroll view
- Can handle giant lists (+100 elements)
- Handles vertical lists only
- Very customizable
Install
# yarn
yarn add react-window-sortable
# npm
npm install --save react-window-sortable
Usage
Fixed Size List (for rows of equal height) full example
<SortableFixedSizeList
height={height}
width={width}
itemCount={n}
itemSize={30}
itemData={this.state.data}
onSortOrderChanged={({originalIndex, newIndex}) => {
move(this.state.data, originalIndex, newIndex);
this.setState({
data: this.state.data.slice(0),
})
}}
>
{React.forwardRef(({data, index, style, onSortMouseDown}: ChildrenProps, ref: Ref<any>) => (
<div ref={ref} style={style}>
<button onMouseDown={onSortMouseDown}>drag handle</button>
{data[index]}
</div>
))}
</SortableFixedSizeList>
Variable Size List (for rows of variable height) full example
<SortableVariableSizeList
height={height}
width={width}
itemCount={n}
itemSize={index => index % 2 === 0 ? 30 : 50}
itemData={this.state.data}
onSortOrderChanged={({originalIndex, newIndex}: {originalIndex: number, newIndex: number}) => {
move(this.state.data, originalIndex, newIndex);
this.setState({
data: this.state.data.slice(0),
})
}}
>
{React.forwardRef(({data, index, style, onSortMouseDown}: ChildrenProps, ref: Ref<any>) => (
<div ref={ref} style={style}>
<button onMouseDown={onSortMouseDown}>drag handle</button>
{data[index]}
</div>
))}
</SortableVariableSizeList>
API
interface Props {
children: React.ComponentType<ChildrenProps>;
autoScrollWhenDistanceLessThan?: number;
autoScrollSpeed?: number;
draggingElementClassName?: string;
draggingElementStyle?: CSSProperties;
dropElement?: any;
onSortOrderChanged(params: { originalIndex: number; newIndex: number }): void;
height: number;
width: number;
itemCount: number;
itemSize: number | (index) => number;
itemData: any[];
}