-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add virtualization in sites table #1054
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
78e7d88
Add virtualization in sites table
K-Markopoulos 9dfca99
Fix scrolling issue
K-Markopoulos f4c5a9f
Merge branch 'master' into sites-list-virtualization
ericboucher becd0e2
Move map query after dispatch
K-Markopoulos 48b3707
Rename props
K-Markopoulos 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,80 @@ | ||
import { Table, TableBody, TableContainer } from '@material-ui/core'; | ||
import React, { useState, useContext, forwardRef } from 'react'; | ||
import { FixedSizeList, FixedSizeListProps } from 'react-window'; | ||
|
||
/** Context for cross component communication */ | ||
const VirtualTableContext = React.createContext<{ | ||
top: number; | ||
setTop: (top: number) => void; | ||
header: React.ReactNode; | ||
footer: React.ReactNode; | ||
}>({ | ||
top: 0, | ||
setTop: () => {}, | ||
header: <></>, | ||
footer: <></>, | ||
}); | ||
|
||
type VirtualTableProps = { | ||
header?: React.ReactNode; | ||
footer?: React.ReactNode; | ||
row: FixedSizeListProps['children']; | ||
} & Omit<FixedSizeListProps, 'children' | 'innerElementType'>; | ||
|
||
/** | ||
* Displays a virtualized list as table with optional header and footer. | ||
* It basically accepts all of the same params as the original FixedSizeList. | ||
* From: https://codesandbox.io/p/sandbox/react-window-with-table-elements-d861o | ||
* */ | ||
export const VirtualTable = forwardRef<FixedSizeList, VirtualTableProps>( | ||
({ row, header, footer, ...rest }, ref) => { | ||
const [top, setTop] = useState(0); | ||
|
||
return ( | ||
<VirtualTableContext.Provider value={{ top, setTop, header, footer }}> | ||
<FixedSizeList | ||
{...rest} | ||
innerElementType={Inner} | ||
onItemsRendered={(props) => { | ||
// @ts-ignore | ||
// eslint-disable-next-line no-underscore-dangle | ||
const style = ref.current?._getItemStyle(props.overscanStartIndex); | ||
setTop(style?.top ?? 0); | ||
|
||
// Call the original callback | ||
rest.onItemsRendered?.(props); | ||
}} | ||
ref={ref} | ||
> | ||
{row} | ||
</FixedSizeList> | ||
</VirtualTableContext.Provider> | ||
); | ||
}, | ||
); | ||
|
||
/** | ||
* The Inner component of the virtual list. This is the "Magic". | ||
* Capture what would have been the top elements position and apply it to the table. | ||
* Other than that, render an optional header and footer. | ||
* */ | ||
const Inner = React.forwardRef<HTMLDivElement, React.HTMLProps<HTMLDivElement>>( | ||
function Inner({ children, ...rest }, ref) { | ||
const { header, footer, top } = useContext(VirtualTableContext); | ||
return ( | ||
<TableContainer {...rest} ref={ref}> | ||
<Table | ||
style={{ | ||
top, | ||
position: 'absolute', | ||
width: '100%', | ||
}} | ||
> | ||
{header} | ||
<TableBody style={{ position: 'relative' }}>{children}</TableBody> | ||
{footer} | ||
</Table> | ||
</TableContainer> | ||
); | ||
}, | ||
); |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.
Nice comment here with reference link
◽ Compliment
Frank T