Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add row reordering example #2111

Merged
merged 6 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ function Cell<R, SR>({
);
}

export default memo(forwardRef(Cell)) as <R, SR = unknown>(props: CellRendererProps<R, SR>) => JSX.Element;
export default memo(forwardRef(Cell)) as <R, SR = unknown>(props: CellRendererProps<R, SR> & { ref?: React.Ref<HTMLDivElement> }) => JSX.Element;
7 changes: 4 additions & 3 deletions src/Row.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo } from 'react';
import React, { memo, forwardRef } from 'react';
import clsx from 'clsx';

import Cell from './Cell';
Expand All @@ -25,7 +25,7 @@ function Row<R, SR = unknown>({
'aria-rowindex': ariaRowIndex,
'aria-selected': ariaSelected,
...props
}: RowRendererProps<R, SR>) {
}: RowRendererProps<R, SR>, ref: React.Ref<HTMLDivElement>) {
function handleDragEnter() {
setDraggedOverRowIdx?.(rowIdx);
}
Expand All @@ -43,6 +43,7 @@ function Row<R, SR = unknown>({
role="row"
aria-rowindex={ariaRowIndex}
aria-selected={ariaSelected}
ref={ref}
className={className}
onMouseEnter={wrapEvent(handleDragEnter, onMouseEnter)}
style={{ top }}
Expand All @@ -67,4 +68,4 @@ function Row<R, SR = unknown>({
);
}

export default memo(Row) as <R, SR>(props: RowRendererProps<R, SR>) => JSX.Element;
export default memo(forwardRef(Row)) as <R, SR = unknown>(props: RowRendererProps<R, SR> & { ref?: React.Ref<HTMLDivElement> }) => JSX.Element;
4 changes: 2 additions & 2 deletions stories/demos/ColumnsReordering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function createRows(): Row[] {
id: i,
task: `Task ${i}`,
complete: Math.min(100, Math.round(Math.random() * 110)),
priority: ['Critical', 'High', 'Medium', 'Low'][Math.floor((Math.random() * 3) + 1)],
issueType: ['Bug', 'Improvement', 'Epic', 'Story'][Math.floor((Math.random() * 3) + 1)]
priority: ['Critical', 'High', 'Medium', 'Low'][Math.round(Math.random() * 3)],
issueType: ['Bug', 'Improvement', 'Epic', 'Story'][Math.round(Math.random() * 3)]
});
}

Expand Down
78 changes: 78 additions & 0 deletions stories/demos/RowsReordering.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React, { useState } from 'react';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';

import { DraggableRowRenderer } from './components/RowRenderers';
import DataGrid, { Column } from '../../src';

interface Row {
id: number;
task: string;
complete: number;
priority: string;
issueType: string;
}

function createRows(): readonly Row[] {
const rows = [];
for (let i = 1; i < 500; i++) {
rows.push({
id: i,
task: `Task ${i}`,
complete: Math.min(100, Math.round(Math.random() * 110)),
priority: ['Critical', 'High', 'Medium', 'Low'][Math.round(Math.random() * 3)],
issueType: ['Bug', 'Improvement', 'Epic', 'Story'][Math.round(Math.random() * 3)]
});
}

return rows;
}

const columns: readonly Column<Row>[] = [
{
key: 'id',
name: 'ID',
width: 80
},
{
key: 'task',
name: 'Title'
},
{
key: 'priority',
name: 'Priority'
},
{
key: 'issueType',
name: 'Issue Type'
},
{
key: 'complete',
name: '% Complete'
}
];

export default function RowsReordering() {
const [rows, setRows] = useState(createRows);

function onRowReorder(fromIndex: number, toIndex: number) {
const newRows = [...rows];
newRows.splice(
toIndex,
0,
newRows.splice(fromIndex, 1)[0]
);

setRows(newRows);
}

return (
<DndProvider backend={HTML5Backend}>
<DataGrid
columns={columns}
rows={rows}
rowRenderer={p => <DraggableRowRenderer {...p} onRowReorder={onRowReorder} />}
/>
</DndProvider>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,12 @@ import React from 'react';
import { useDrag, useDrop, DragObjectWithType } from 'react-dnd';

import { HeaderRendererProps } from '../../../../src';

import { useCombinedRefs } from '../../../../src/hooks';

interface ColumnDragObject extends DragObjectWithType {
key: string;
}

function wrapRefs<T>(...refs: React.Ref<T>[]) {
return (handle: T | null) => {
for (const ref of refs) {
if (typeof ref === 'function') {
ref(handle);
} else if (ref !== null) {
// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/31065
(ref as React.MutableRefObject<T | null>).current = handle;
}
}
};
}

export function DraggableHeaderRenderer<R>({ onColumnsReorder, ...props }: HeaderRendererProps<R> & { onColumnsReorder: (sourceKey: string, targetKey: string) => void }) {
const [{ isDragging }, drag] = useDrag({
item: { key: props.column.key, type: 'COLUMN_DRAG' },
Expand All @@ -44,7 +31,7 @@ export function DraggableHeaderRenderer<R>({ onColumnsReorder, ...props }: Heade

return (
<div
ref={wrapRefs(drag, drop)}
ref={useCombinedRefs(drag, drop)}
style={{
opacity: isDragging ? 0.5 : 1,
backgroundColor: isOver ? '#ececec' : 'inherit',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.rdg-row-dragging {
opacity: 0.5;
}

.rdg-row-over {
background-color: #ececec;
}
62 changes: 62 additions & 0 deletions stories/demos/components/RowRenderers/DraggableRowRenderer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';
import { useDrag, useDrop, DragObjectWithType } from 'react-dnd';
import clsx from 'clsx';

import { Row, RowRendererProps } from '../../../../src';
import { useCombinedRefs } from '../../../../src/hooks';

import './DraggableRowRenderer.less';

interface RowDragObject extends DragObjectWithType {
index: number;
}

interface DraggableRowRenderProps<R, SR> extends RowRendererProps<R, SR> {
onRowReorder: (sourceIndex: number, targetIndex: number) => void;
}

export function DraggableRowRenderer<R, SR = unknown>({
rowIdx,
isRowSelected,
className,
onRowReorder,
...props
}: DraggableRowRenderProps<R, SR>) {
const [{ isDragging }, drag] = useDrag({
item: { index: rowIdx, type: 'ROW_DRAG' },
collect: monitor => ({
isDragging: monitor.isDragging()
})
});

const [{ isOver }, drop] = useDrop({
accept: 'ROW_DRAG',
drop({ index, type }: RowDragObject) {
if (type === 'ROW_DRAG') {
onRowReorder(index, rowIdx);
}
},
collect: monitor => ({
isOver: monitor.isOver(),
canDrop: monitor.canDrop()
})
});

className = clsx(
className,
{
'rdg-row-dragging': isDragging,
'rdg-row-over': isOver
}
);

return (
<Row
ref={useCombinedRefs(drag, drop)}
rowIdx={rowIdx}
isRowSelected={isRowSelected}
className={className}
{...props}
/>
);
}
1 change: 1 addition & 0 deletions stories/demos/components/RowRenderers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './DraggableRowRenderer';
2 changes: 2 additions & 0 deletions stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ScrollToRow from './demos/ScrollToRow';
import CellNavigation from './demos/CellNavigation';
import HeaderFilters from './demos/HeaderFilters';
import ColumnsReordering from './demos/ColumnsReordering';
import RowsReordering from './demos/RowsReordering';
import LegacyGrouping from './demos/LegacyGrouping';

storiesOf('Demos', module)
Expand All @@ -29,4 +30,5 @@ storiesOf('Demos', module)
.add('Cell Navigation', () => <CellNavigation />)
.add('Header Filters', () => <HeaderFilters />)
.add('Columns Reordering', () => <ColumnsReordering />)
.add('Rows Reordering', () => <RowsReordering />)
.add('Legacy Grouping', () => <LegacyGrouping />);