-
-
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.
- Loading branch information
1 parent
f37aaaf
commit ca1b2ef
Showing
11 changed files
with
186 additions
and
3 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,44 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
import Box from '@mui/material/Box'; | ||
import Stack from '@mui/material/Stack'; | ||
import { DataGrid, useGridAutoHeight, useGridApiRef } from '@mui/x-data-grid'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
const maxHeight = 500; | ||
|
||
export default function AutoHeightGridMaxHeight() { | ||
const [nbRows, setNbRows] = React.useState(3); | ||
const removeRow = () => setNbRows((x) => Math.max(0, x - 1)); | ||
const addRow = () => setNbRows((x) => Math.min(100, x + 1)); | ||
|
||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 100, | ||
maxColumns: 6, | ||
}); | ||
|
||
const apiRef = useGridApiRef(); | ||
const autoHeight = useGridAutoHeight(apiRef, maxHeight); | ||
|
||
return ( | ||
<Box sx={{ width: '100%' }}> | ||
<Stack direction="row" spacing={1} sx={{ mb: 1 }}> | ||
<Button size="small" onClick={removeRow}> | ||
Remove a row | ||
</Button> | ||
<Button size="small" onClick={addRow}> | ||
Add a row | ||
</Button> | ||
</Stack> | ||
<div style={{ height: autoHeight ? 'auto' : maxHeight }}> | ||
<DataGrid | ||
apiRef={apiRef} | ||
autoHeight={autoHeight} | ||
{...data} | ||
rows={data.rows.slice(0, nbRows)} | ||
/> | ||
</div> | ||
</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,44 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/material/Button'; | ||
import Box from '@mui/material/Box'; | ||
import Stack from '@mui/material/Stack'; | ||
import { DataGrid, useGridAutoHeight, useGridApiRef } from '@mui/x-data-grid'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
const maxHeight = 500; | ||
|
||
export default function AutoHeightGridMaxHeight() { | ||
const [nbRows, setNbRows] = React.useState(3); | ||
const removeRow = () => setNbRows((x) => Math.max(0, x - 1)); | ||
const addRow = () => setNbRows((x) => Math.min(100, x + 1)); | ||
|
||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 100, | ||
maxColumns: 6, | ||
}); | ||
|
||
const apiRef = useGridApiRef(); | ||
const autoHeight = useGridAutoHeight(apiRef, maxHeight); | ||
|
||
return ( | ||
<Box sx={{ width: '100%' }}> | ||
<Stack direction="row" spacing={1} sx={{ mb: 1 }}> | ||
<Button size="small" onClick={removeRow}> | ||
Remove a row | ||
</Button> | ||
<Button size="small" onClick={addRow}> | ||
Add a row | ||
</Button> | ||
</Stack> | ||
<div style={{ height: autoHeight ? 'auto' : maxHeight }}> | ||
<DataGrid | ||
apiRef={apiRef} | ||
autoHeight={autoHeight} | ||
{...data} | ||
rows={data.rows.slice(0, nbRows)} | ||
/> | ||
</div> | ||
</Box> | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
docs/data/data-grid/layout/AutoHeightGridMaxHeight.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,16 @@ | ||
<Stack direction="row" spacing={1} sx={{ mb: 1 }}> | ||
<Button size="small" onClick={removeRow}> | ||
Remove a row | ||
</Button> | ||
<Button size="small" onClick={addRow}> | ||
Add a row | ||
</Button> | ||
</Stack> | ||
<div style={{ height: autoHeight ? 'auto' : maxHeight }}> | ||
<DataGrid | ||
apiRef={apiRef} | ||
autoHeight={autoHeight} | ||
{...data} | ||
rows={data.rows.slice(0, nbRows)} | ||
/> | ||
</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
45 changes: 45 additions & 0 deletions
45
packages/grid/x-data-grid/src/hooks/utils/useGridAutoHeight.ts
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,45 @@ | ||
import * as React from 'react'; | ||
import { GridApiCommon } from '@mui/x-data-grid-pro'; | ||
import { gridClasses } from '../../constants/gridClasses'; | ||
|
||
export const useGridAutoHeight = ( | ||
apiRef: React.MutableRefObject<GridApiCommon>, | ||
maxHeight: number, | ||
) => { | ||
const [autoHeight, setAutoHeight] = React.useState(true); | ||
const autoHeightRef = React.useRef<boolean>(true); | ||
autoHeightRef.current = autoHeight; | ||
|
||
React.useEffect(() => { | ||
return apiRef.current.subscribeEvent('virtualScrollerContentSizeChange', (params) => { | ||
if (!apiRef.current) { | ||
return; | ||
} | ||
const rootEl = apiRef.current.rootElementRef?.current; | ||
const virtualScrollerEl = rootEl?.querySelector<HTMLElement>( | ||
`.${gridClasses.virtualScroller}`, | ||
); | ||
if (!rootEl || !virtualScrollerEl) { | ||
return; | ||
} | ||
const topPinnedRowsHeight = | ||
virtualScrollerEl.querySelector<HTMLElement>(`:scope > .${gridClasses['pinnedRows--top']}`) | ||
?.offsetHeight || 0; | ||
const bottomPinnedRowsHeight = | ||
virtualScrollerEl.querySelector<HTMLElement>( | ||
`:scope > .${gridClasses['pinnedRows--bottom']}`, | ||
)?.offsetHeight || 0; | ||
|
||
const headerAndFooterHeight = rootEl.offsetHeight - virtualScrollerEl.offsetHeight; | ||
const maxVirtualScrollerContentHeight = | ||
maxHeight - headerAndFooterHeight - topPinnedRowsHeight - bottomPinnedRowsHeight; | ||
if (params.height >= maxVirtualScrollerContentHeight) { | ||
setAutoHeight(false); | ||
} else { | ||
setAutoHeight(true); | ||
} | ||
}); | ||
}, [apiRef, autoHeight, maxHeight]); | ||
|
||
return autoHeight; | ||
}; |
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