-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[DataGrid] Improve sizing logic #350
Merged
oliviertassinari
merged 9 commits into
mui:master
from
oliviertassinari:fix-autosize-issue
Oct 1, 2020
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8966e5d
[DataGrid] Improve sizing logic
oliviertassinari 0b16678
fix types
oliviertassinari 597c258
fix tests
oliviertassinari 2188df0
Damien's review
oliviertassinari 8cbc40c
Update packages/grid/_modules_/grid/hooks/utils/useLogger.ts
oliviertassinari b47b11d
Update packages/grid/_modules_/grid/components/default-footer.tsx
oliviertassinari 30c67d1
fix ci
oliviertassinari d6c28a2
increase consistency
oliviertassinari fb2687e
fix ci
oliviertassinari 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 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,159 @@ | ||
import * as React from 'react'; | ||
import { useForkRef, ownerWindow, useEventCallback } from '@material-ui/core/utils'; | ||
import createDetectElementResize from '../lib/createDetectElementResize'; | ||
// TODO replace with https://caniuse.com/resizeobserver. | ||
|
||
export interface AutoSizerSize { | ||
height: number; | ||
width: number; | ||
} | ||
|
||
// Credit to https://github.com/bvaughn/react-virtualized/blob/master/source/AutoSizer/AutoSizer.js | ||
// for the sources. | ||
|
||
export interface AutoSizerProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> { | ||
/** | ||
* Function responsible for rendering children. | ||
*/ | ||
children: (size: AutoSizerSize) => React.ReactNode; | ||
/** | ||
* Default height to use for initial render; useful for SSR. | ||
* @default 0 | ||
*/ | ||
defaultHeight?: number; | ||
/** | ||
* Default width to use for initial render; useful for SSR. | ||
* @default 0 | ||
*/ | ||
defaultWidth?: number; | ||
/** | ||
* If `true`, disable dynamic :height property. | ||
* @default false | ||
*/ | ||
disableHeight?: boolean; | ||
/** | ||
* If `true`, disable dynamic :width property. | ||
* @default false | ||
*/ | ||
disableWidth?: boolean; | ||
/** | ||
* Nonce of the inlined stylesheet for Content Security Policy. | ||
*/ | ||
nonce?: string; | ||
/** | ||
* Callback to be invoked on-resize. | ||
*/ | ||
onResize?: (size: AutoSizerSize) => void; | ||
} | ||
|
||
// TODO v5: replace with @material-ui/core/utils/useEnhancedEffect. | ||
const useEnhancedEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect; | ||
|
||
export const AutoSizer = React.forwardRef<HTMLDivElement, AutoSizerProps>(function AutoSizer( | ||
props, | ||
ref, | ||
) { | ||
const { | ||
children, | ||
defaultHeight = 0, | ||
defaultWidth = 0, | ||
disableHeight = false, | ||
disableWidth = false, | ||
nonce, | ||
onResize, | ||
style, | ||
...other | ||
} = props; | ||
|
||
const [state, setState] = React.useState({ | ||
height: defaultHeight, | ||
width: defaultWidth, | ||
}); | ||
|
||
const rootRef = React.useRef<HTMLDivElement>(null); | ||
const parentElement = React.useRef(null) as React.MutableRefObject<HTMLElement | null>; | ||
|
||
const handleResize = useEventCallback(() => { | ||
// Guard against AutoSizer component being removed from the DOM immediately after being added. | ||
// This can result in invalid style values which can result in NaN values if we don't handle them. | ||
// See issue #150 for more context. | ||
if (parentElement.current) { | ||
const height = parentElement.current.offsetHeight || 0; | ||
const width = parentElement.current.offsetWidth || 0; | ||
|
||
const win = ownerWindow(parentElement.current); | ||
const computedStyle = win.getComputedStyle(parentElement.current); | ||
const paddingLeft = parseInt(computedStyle.paddingLeft, 10) || 0; | ||
const paddingRight = parseInt(computedStyle.paddingRight, 10) || 0; | ||
const paddingTop = parseInt(computedStyle.paddingTop, 10) || 0; | ||
const paddingBottom = parseInt(computedStyle.paddingBottom, 10) || 0; | ||
|
||
const newHeight = height - paddingTop - paddingBottom; | ||
const newWidth = width - paddingLeft - paddingRight; | ||
|
||
if ( | ||
(!disableHeight && state.height !== newHeight) || | ||
(!disableWidth && state.width !== newWidth) | ||
) { | ||
setState({ | ||
height: height - paddingTop - paddingBottom, | ||
width: width - paddingLeft - paddingRight, | ||
}); | ||
|
||
if (onResize) { | ||
onResize({ height, width }); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
useEnhancedEffect(() => { | ||
parentElement.current = rootRef.current!.parentElement; | ||
|
||
if (!parentElement) { | ||
return undefined; | ||
} | ||
|
||
const win = ownerWindow(parentElement.current ?? undefined); | ||
|
||
const detectElementResize = createDetectElementResize(nonce, win); | ||
detectElementResize.addResizeListener(parentElement.current, handleResize); | ||
// @ts-expect-error fixed in v5 | ||
handleResize(); | ||
|
||
return () => { | ||
detectElementResize.removeResizeListener(parentElement.current, handleResize); | ||
}; | ||
}, [nonce, handleResize]); | ||
|
||
// Outer div should not force width/height since that may prevent containers from shrinking. | ||
// Inner component should overflow and use calculated width/height. | ||
// See issue #68 for more information. | ||
const outerStyle: any = { overflow: 'visible' }; | ||
const childParams: any = {}; | ||
|
||
if (!disableHeight) { | ||
outerStyle.height = 0; | ||
childParams.height = state.height; | ||
} | ||
|
||
if (!disableWidth) { | ||
outerStyle.width = 0; | ||
childParams.width = state.width; | ||
} | ||
|
||
const handleRef = useForkRef(rootRef, ref); | ||
|
||
return ( | ||
<div | ||
ref={handleRef} | ||
style={{ | ||
...outerStyle, | ||
...style, | ||
}} | ||
{...other} | ||
> | ||
{state.height === 0 && state.width === 0 ? null : children(childParams)} | ||
</div> | ||
); | ||
}); |
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
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.
The fix is to remove
style={{ height: 'unset', width: 'unset' }}