-
-
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] Scroll restoration (#16208)
Co-authored-by: Lauri <[email protected]>
- Loading branch information
1 parent
3719061
commit fd668fb
Showing
8 changed files
with
221 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import { | ||
DataGridPro, | ||
useGridApiRef, | ||
gridVisibleColumnDefinitionsSelector, | ||
gridExpandedSortedRowIdsSelector, | ||
} from '@mui/x-data-grid-pro'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
export default function ScrollRestoration() { | ||
const apiRef = useGridApiRef(); | ||
|
||
const [coordinates, setCoordinates] = React.useState({ | ||
rowIndex: 0, | ||
colIndex: 0, | ||
}); | ||
|
||
const { data, loading } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 100, | ||
}); | ||
|
||
React.useEffect(() => { | ||
const { rowIndex, colIndex } = coordinates; | ||
apiRef.current.scrollToIndexes(coordinates); | ||
const id = gridExpandedSortedRowIdsSelector(apiRef)[rowIndex]; | ||
const column = gridVisibleColumnDefinitionsSelector(apiRef)[colIndex]; | ||
apiRef.current.setCellFocus(id, column.field); | ||
}, [apiRef, coordinates]); | ||
|
||
const handleCellClick = (params) => { | ||
const rowIndex = gridExpandedSortedRowIdsSelector(apiRef).findIndex( | ||
(id) => id === params.id, | ||
); | ||
const colIndex = gridVisibleColumnDefinitionsSelector(apiRef).findIndex( | ||
(column) => column.field === params.field, | ||
); | ||
setCoordinates({ rowIndex, colIndex }); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ width: '100%' }}> | ||
<Box sx={{ height: 400 }}> | ||
<DataGridPro | ||
apiRef={apiRef} | ||
onCellClick={handleCellClick} | ||
hideFooter | ||
loading={loading} | ||
{...data} | ||
initialState={{ | ||
...data.initialState, | ||
scroll: { top: 1000, left: 1000 }, | ||
}} | ||
/> | ||
</Box> | ||
</Box> | ||
); | ||
} |
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,60 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import { | ||
DataGridPro, | ||
useGridApiRef, | ||
gridVisibleColumnDefinitionsSelector, | ||
gridExpandedSortedRowIdsSelector, | ||
GridCellParams, | ||
} from '@mui/x-data-grid-pro'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
export default function ScrollRestoration() { | ||
const apiRef = useGridApiRef(); | ||
|
||
const [coordinates, setCoordinates] = React.useState({ | ||
rowIndex: 0, | ||
colIndex: 0, | ||
}); | ||
|
||
const { data, loading } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 100, | ||
}); | ||
|
||
React.useEffect(() => { | ||
const { rowIndex, colIndex } = coordinates; | ||
apiRef.current.scrollToIndexes(coordinates); | ||
const id = gridExpandedSortedRowIdsSelector(apiRef)[rowIndex]; | ||
const column = gridVisibleColumnDefinitionsSelector(apiRef)[colIndex]; | ||
apiRef.current.setCellFocus(id, column.field); | ||
}, [apiRef, coordinates]); | ||
|
||
const handleCellClick = (params: GridCellParams) => { | ||
const rowIndex = gridExpandedSortedRowIdsSelector(apiRef).findIndex( | ||
(id) => id === params.id, | ||
); | ||
const colIndex = gridVisibleColumnDefinitionsSelector(apiRef).findIndex( | ||
(column) => column.field === params.field, | ||
); | ||
setCoordinates({ rowIndex, colIndex }); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ width: '100%' }}> | ||
<Box sx={{ height: 400 }}> | ||
<DataGridPro | ||
apiRef={apiRef} | ||
onCellClick={handleCellClick} | ||
hideFooter | ||
loading={loading} | ||
{...data} | ||
initialState={{ | ||
...data.initialState, | ||
scroll: { top: 1000, left: 1000 }, | ||
}} | ||
/> | ||
</Box> | ||
</Box> | ||
); | ||
} |
13 changes: 13 additions & 0 deletions
13
docs/data/data-grid/scrolling/ScrollRestoration.tsx.preview
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,13 @@ | ||
<Box sx={{ height: 400 }}> | ||
<DataGridPro | ||
apiRef={apiRef} | ||
onCellClick={handleCellClick} | ||
hideFooter | ||
loading={loading} | ||
{...data} | ||
initialState={{ | ||
...data.initialState, | ||
scroll: { top: 1000, left: 1000 }, | ||
}} | ||
/> | ||
</Box> |
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