-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DataGrid] Add column reorder support (#165)
* [useColumnReorder] Add the ability to reorder columns using drag and drop * [useColumnReorder] Fix review comments * fix broking functionality * Fix issues related to pulling the latest changes * {useColumnReorder] Refactor the feature hook and add drag scroll support for the ColumnsHeader * [useColumnReorder] Fix lint and format issues * [useColumnsReorder] Fix build and formatting * [useColumnReorder] Fix lint errors * Fix styles downgrade when dragging a column cell * [useColumnReorder] Added option to disable the feature, added docs, added tests * [useColumnReorder] Fix formatting and enable all the tests * Remove unnessesary code from the mouse.test.ts file * Fix formatting * fix formatting * Emit event when drag enters a column * Working on PR comments * Fix inifinte loop when dragging smaller cal over large col * Rename drag handlers prefix * Prep codebase for commit and push * Run prettier * Fix lint errors * Rename file to match coding style * Rename columnsHeaderRef to ref * Fix PR comments * Update docs/pages/api-docs/x-grid.md Co-authored-by: Matt <[email protected]> * Update docs/pages/api-docs/x-grid.md Co-authored-by: Matt <[email protected]> * Update docs/pages/api-docs/x-grid.md Co-authored-by: Matt <[email protected]> * Add relevant documentation about coumn reorder on the /components/data-grid/columns/ page * Fix docs formatting * Update docs/src/pages/components/data-grid/columns/columns.md Co-authored-by: Olivier Tassinari <[email protected]> * Update docs/src/pages/components/data-grid/columns/columns.md Co-authored-by: Olivier Tassinari <[email protected]> * Update docs/src/pages/components/data-grid/columns/columns.md Co-authored-by: Olivier Tassinari <[email protected]> * Update docs/src/pages/components/data-grid/columns/columns.md Co-authored-by: Olivier Tassinari <[email protected]> * Update code example * Fix PR comments * Fix the doc example code * Update docs/src/pages/components/data-grid/columns/ColumnOrderingGrid.tsx Co-authored-by: Olivier Tassinari <[email protected]> * Fix column reordering issue while dragging over the same cell * Make scrolling while dragging excelerate * Format and lint the changes * Fix scroll left issue * Fix build * fix scroll smoothness * Fix Col header item alignment visuall regression * Final polishing of the useColumnReorder hook * Fix typings * the logic never runs on Node.js * clear timeout when unmounting, avoid edge-case leak * use design token * no shorthand * Use CSS inherit * simpler class name logic * we have strong constraint that these value should be defined. It should fail if its not the case * no shorthands * the logic depends on the element to be the current target, not the target, remove potential confusion * add visual clue about where the column is, outline shouldn't be visible * Remove unnecessary checks related to disableColumnReorder flag * Fix PR comments related * remove raf Co-authored-by: Matt <[email protected]> Co-authored-by: Olivier Tassinari <[email protected]> Co-authored-by: Danail Hadzhiatanasov <[email protected]>
- Loading branch information
1 parent
5fc0f76
commit 49237cd
Showing
23 changed files
with
609 additions
and
85 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
docs/src/pages/components/data-grid/columns/ColumnOrderingGrid.js
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 * as React from 'react'; | ||
import { XGrid } from '@material-ui/x-grid'; | ||
import { useDemoData } from '@material-ui/x-grid-data-generator'; | ||
|
||
export default function ColumnOrderingGrid() { | ||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 20, | ||
maxColumns: 20, | ||
}); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<XGrid {...data} /> | ||
</div> | ||
); | ||
} |
17 changes: 17 additions & 0 deletions
17
docs/src/pages/components/data-grid/columns/ColumnOrderingGrid.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,17 @@ | ||
import * as React from 'react'; | ||
import { XGrid } from '@material-ui/x-grid'; | ||
import { useDemoData } from '@material-ui/x-grid-data-generator'; | ||
|
||
export default function ColumnOrderingGrid() { | ||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 20, | ||
maxColumns: 20, | ||
}); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<XGrid {...data} /> | ||
</div> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as React from 'react'; | ||
import { COL_REORDER_START, COL_REORDER_STOP, SCROLLING } from '../constants/eventsConstants'; | ||
import { ScrollParams, useApiEventHandler } from '../hooks'; | ||
import { ApiRef } from '../models'; | ||
import { classnames } from '../utils'; | ||
import { ApiContext } from './api-context'; | ||
|
||
const CLIFF = 1; | ||
const SLOP = 1.5; | ||
|
||
interface ScrollAreaProps { | ||
scrollDirection: 'left' | 'right'; | ||
} | ||
|
||
export const ScrollArea = React.memo(function ScrollArea(props: ScrollAreaProps) { | ||
const { scrollDirection } = props; | ||
const rootRef = React.useRef<HTMLDivElement>(null); | ||
const api = React.useContext(ApiContext); | ||
const timeout = React.useRef<number>(); | ||
const [dragging, setDragging] = React.useState<boolean>(false); | ||
const scrollPosition = React.useRef<ScrollParams>({ | ||
left: 0, | ||
top: 0, | ||
}); | ||
|
||
const handleScrolling = React.useCallback((newScrollPosition) => { | ||
scrollPosition.current = newScrollPosition; | ||
}, []); | ||
|
||
const handleDragOver = React.useCallback( | ||
(event) => { | ||
let offset; | ||
|
||
if (scrollDirection === 'left') { | ||
offset = event.clientX - rootRef.current!.getBoundingClientRect().right; | ||
} else if (scrollDirection === 'right') { | ||
offset = Math.max(1, event.clientX - rootRef.current!.getBoundingClientRect().left); | ||
} else { | ||
throw new Error('wrong dir'); | ||
} | ||
|
||
offset = (offset - CLIFF) * SLOP + CLIFF; | ||
|
||
clearTimeout(timeout.current); | ||
// Avoid freeze and inertia. | ||
timeout.current = setTimeout(() => { | ||
api!.current.scroll({ | ||
left: scrollPosition.current.left + offset, | ||
top: scrollPosition.current.top, | ||
}); | ||
}); | ||
}, | ||
[scrollDirection, api], | ||
); | ||
|
||
React.useEffect(() => { | ||
return () => { | ||
clearTimeout(timeout.current); | ||
}; | ||
}, []); | ||
|
||
const toggleDragging = React.useCallback(() => { | ||
setDragging((prevdragging) => !prevdragging); | ||
}, []); | ||
|
||
useApiEventHandler(api as ApiRef, SCROLLING, handleScrolling); | ||
useApiEventHandler(api as ApiRef, COL_REORDER_START, toggleDragging); | ||
useApiEventHandler(api as ApiRef, COL_REORDER_STOP, toggleDragging); | ||
|
||
return dragging ? ( | ||
<div | ||
ref={rootRef} | ||
className={classnames('MuiDataGrid-scrollArea', `MuiDataGrid-scrollArea-${scrollDirection}`)} | ||
onDragOver={handleDragOver} | ||
/> | ||
) : null; | ||
}); |
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
Oops, something went wrong.