-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add columns reordering example #2007
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4ad5eed
Initial implementation for column reordering
amanmahajan7 ca7cf30
Add DraggableHeaderRenderer component
amanmahajan7 de55fa7
Add sorting
amanmahajan7 4ec57ec
Fix dragging logic
amanmahajan7 c33a2ff
Merge branch 'canary' into am-draggable
amanmahajan7 4e5b55c
Update changelog
amanmahajan7 6f5a0e4
Cleanup types
amanmahajan7 9efa51f
Fix PR number
amanmahajan7 a7b3b3c
Address comments
amanmahajan7 c3c88f3
Remove extra space
amanmahajan7 962ab8e
Remove unnecessary !
amanmahajan7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,130 @@ | ||
import React, { useState, useCallback, useMemo } from 'react'; | ||
import { DndProvider } from 'react-dnd'; | ||
import Backend from 'react-dnd-html5-backend'; | ||
|
||
import { DraggableHeaderRenderer } from './components/HeaderRenderers'; | ||
import DataGrid, { Column, HeaderRendererProps, SortDirection } from '../../src'; | ||
|
||
interface Row { | ||
id: number; | ||
task: string; | ||
complete: number; | ||
priority: string; | ||
issueType: string; | ||
} | ||
|
||
function createRows(): 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.floor((Math.random() * 3) + 1)], | ||
issueType: ['Bug', 'Improvement', 'Epic', 'Story'][Math.floor((Math.random() * 3) + 1)] | ||
}); | ||
} | ||
|
||
return rows; | ||
} | ||
|
||
function createColumns(): Column<Row>[] { | ||
return [ | ||
{ | ||
key: 'id', | ||
name: 'ID', | ||
width: 80 | ||
}, | ||
{ | ||
key: 'task', | ||
name: 'Title', | ||
resizable: true, | ||
sortable: true | ||
}, | ||
{ | ||
key: 'priority', | ||
name: 'Priority', | ||
resizable: true, | ||
sortable: true | ||
}, | ||
{ | ||
key: 'issueType', | ||
name: 'Issue Type', | ||
resizable: true, | ||
sortable: true | ||
}, | ||
{ | ||
key: 'complete', | ||
name: '% Complete', | ||
resizable: true, | ||
sortable: true | ||
} | ||
]; | ||
} | ||
|
||
export default function ColumnsReordering() { | ||
const [rows] = useState(createRows); | ||
const [columns, setColumns] = useState(createColumns); | ||
const [[sortColumn, sortDirection], setSort] = useState<[string, SortDirection]>(['task', 'NONE']); | ||
|
||
const handleSort = useCallback((columnKey: string, direction: SortDirection) => { | ||
setSort([columnKey, direction]); | ||
}, []); | ||
|
||
const draggableColumns = useMemo(() => { | ||
function HeaderRenderer(props: HeaderRendererProps<Row>) { | ||
return <DraggableHeaderRenderer {...props} onColumnsReorder={handleColumnsReorder} />; | ||
} | ||
|
||
function handleColumnsReorder(sourceKey: string, targetKey: string) { | ||
const sourceColumnIndex = columns.findIndex(c => c.key === sourceKey); | ||
const targetColumnIndex = columns.findIndex(c => c.key === targetKey); | ||
const reorderedColumns = [...columns]; | ||
|
||
reorderedColumns.splice( | ||
targetColumnIndex, | ||
0, | ||
reorderedColumns.splice(sourceColumnIndex, 1)[0] | ||
); | ||
|
||
setColumns(reorderedColumns); | ||
} | ||
|
||
return columns.map(c => { | ||
if (c.key === 'id') return c; | ||
return { ...c, headerRenderer: HeaderRenderer }; | ||
}); | ||
}, [columns]); | ||
|
||
const sortedRows = useMemo((): readonly Row[] => { | ||
if (sortDirection === 'NONE') return rows; | ||
|
||
let sortedRows: Row[] = [...rows]; | ||
|
||
switch (sortColumn) { | ||
case 'task': | ||
case 'priority': | ||
case 'issueType': | ||
sortedRows = sortedRows.sort((a, b) => a[sortColumn].localeCompare(b[sortColumn])); | ||
break; | ||
case 'complete': | ||
sortedRows = sortedRows.sort((a, b) => a[sortColumn] - b[sortColumn]); | ||
break; | ||
default: | ||
} | ||
|
||
return sortDirection === 'DESC' ? sortedRows.reverse() : sortedRows; | ||
}, [rows, sortDirection, sortColumn]); | ||
|
||
return ( | ||
<DndProvider backend={Backend}> | ||
<DataGrid | ||
columns={draggableColumns} | ||
rows={sortedRows} | ||
sortColumn={sortColumn} | ||
sortDirection={sortDirection} | ||
onSort={handleSort} | ||
/> | ||
</DndProvider> | ||
); | ||
} |
57 changes: 57 additions & 0 deletions
57
stories/demos/components/HeaderRenderers/DraggableHeaderRenderer.tsx
This file contains hidden or 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,57 @@ | ||
import React from 'react'; | ||
import { useDrag, useDrop, DragObjectWithType } from 'react-dnd'; | ||
|
||
import { HeaderRendererProps } from '../../../../src'; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And we don't need this empty line. |
||
|
||
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' }, | ||
collect: monitor => ({ | ||
isDragging: !!monitor.isDragging() | ||
}) | ||
}); | ||
|
||
const [{ isOver }, drop] = useDrop({ | ||
accept: 'COLUMN_DRAG', | ||
drop({ key, type }: ColumnDragObject) { | ||
if (type === 'COLUMN_DRAG') { | ||
onColumnsReorder(key, props.column.key); | ||
} | ||
}, | ||
collect: monitor => ({ | ||
isOver: !!monitor.isOver(), | ||
canDrop: !!monitor.canDrop() | ||
}) | ||
}); | ||
|
||
return ( | ||
<div | ||
ref={wrapRefs(drag, drop)} | ||
style={{ | ||
opacity: isDragging ? 0.5 : 1, | ||
backgroundColor: isOver ? '#ececec' : 'inherit', | ||
cursor: 'move' | ||
}} | ||
> | ||
{props.column.name} | ||
</div> | ||
); | ||
} |
This file contains hidden or 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 @@ | ||
export * from './DraggableHeaderRenderer'; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just not sure about this file. What do you guys think if we provide this in the DataGrid and export this as a default
DraggableHeaderRenderer
?pros: consumer devs can just use the default column function;
cons: 1. we might need to provide more APIs. 2. We have to include the react-dnd as a dependency...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to keep the dependencies to a minimum and provide a flexible API so users can write their own implementations. Composition is always more maintainable than adding extra props
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it's extra work to maintain the feature + dependency on other libs.
That way users can use whatever implementation they want to header dragging.