Comments (2)
Hey @marcosperez thanks for the help but this isn't working as I expected it to work..
from react-window.
Hi navneetbz ! this is my code and Its worked for me, I hope can be usefull for you
import React from 'react'
import { VariableSizeGrid as Grid } from 'react-window'
function getCellIndicies(child) {
return { row: child.props.rowIndex, column: child.props.columnIndex }
}
function getShownIndicies(children) {
let minRow = Infinity
let maxRow = -Infinity
let minColumn = Infinity
let maxColumn = -Infinity
React.Children.forEach(children, child => {
const { row, column } = getCellIndicies(child)
minRow = Math.min(minRow, row)
maxRow = Math.max(maxRow, row)
minColumn = Math.min(minColumn, column)
maxColumn = Math.max(maxColumn, column)
})
return {
from: {
row: minRow,
column: minColumn,
},
to: {
row: maxRow,
column: maxColumn,
},
}
}
function useInnerElementType(Cell, columnWidth, rowHeight, fixedColumns = 1) {
return React.useMemo(
() =>
React.forwardRef((props, ref) => {
function sumRowsHeights(index) {
let sum = 0
while (index > 1) {
sum += rowHeight(index - 1)
index -= 1
}
return sum
}
function sumColumnWidthsFT(from, to) {
let sum = 0
for (let index = from; index < to; index++) {
sum += columnWidth(index)
}
return sum
}
const shownIndecies = getShownIndicies(props.children)
const showFixedColumns = fixedColumns - 1
const children = React.Children.map(props.children, child => {
const { column, row } = getCellIndicies(child)
if (column < fixedColumns || row === 0) {
return null
}
return child
})
for (let fixColIdx = 0; fixColIdx <= showFixedColumns; fixColIdx++) {
children.push(
React.createElement(Cell, {
key: `0:${fixColIdx}`,
rowIndex: 0,
columnIndex: fixColIdx,
style: {
display: 'inline-flex',
width: columnWidth(fixColIdx),
height: rowHeight(0),
position: 'sticky',
top: 0,
left: fixColIdx === 0 ? 0 : sumColumnWidthsFT(0, fixColIdx),
zIndex: 4,
},
})
)
}
const shownColumnsCount =
shownIndecies.to.column - shownIndecies.from.column
for (let i = fixedColumns; i <= shownColumnsCount; i += 1) {
const columnIndex = i + shownIndecies.from.column
const rowIndex = 0
const width = columnWidth(columnIndex)
const height = rowHeight(rowIndex)
const marginLeft =
shownIndecies.from.column !== 0 &&
columnIndex === shownIndecies.from.column + fixedColumns
? sumColumnWidthsFT(
fixedColumns,
fixedColumns + shownIndecies.from.column
)
: 0
const colsWidth = sumColumnWidthsFT(0, columnIndex + 1) - marginLeft
const marginRight =
columnIndex === shownIndecies.to.column
? `calc( 100% - ${colsWidth}px)`
: 0
children.push(
React.createElement(Cell, {
key: `${rowIndex}:${columnIndex}`,
rowIndex,
columnIndex,
style: {
marginLeft,
marginRight,
display: 'inline-flex',
width,
height,
position: 'sticky',
top: 0,
zIndex: 3,
},
})
)
}
const shownRowsCount = shownIndecies.to.row - shownIndecies.from.row
for (let i = 1; i <= shownRowsCount; i += 1) {
for (let j = 0; j <= showFixedColumns; j += 1) {
const columnIndex = j
const rowIndex = i + shownIndecies.from.row
const width = columnWidth(columnIndex)
const height = rowHeight(rowIndex)
const left =
columnIndex === 0 ? 0 : sumColumnWidthsFT(0, columnIndex)
const colsWidth = sumColumnWidthsFT(0, columnIndex + 1)
const marginRight =
showFixedColumns === columnIndex
? `calc( 100% - ${colsWidth}px)`
: undefined
const marginTop = i === 1 ? sumRowsHeights(rowIndex) : undefined
children.push(
React.createElement(Cell, {
key: `${rowIndex}:${columnIndex}`,
rowIndex,
columnIndex,
style: {
marginTop,
display: 'inline-flex',
width,
height,
position: 'sticky',
left,
zIndex: 2,
overflow: 'hidden',
marginRight,
},
})
)
}
}
return (
<div ref={ref} {...props}>
{children}
</div>
)
}),
[Cell, columnWidth, rowHeight]
)
}
function StickyGridFn(props, ref) {
return (
<Grid
ref={ref}
{...props}
innerElementType={useInnerElementType(
props.children,
props.columnWidth,
props.rowHeight,
props.fixedColumns
)}
/>
)
}
export const StickyGrid = React.forwardRef(StickyGridFn)
from react-window.
Related Issues (20)
- `<FixedSizeList/>` fails the typing if the version of `@types/react` is `18.8.24` or later
- Can I replace the blank spaces which is caused by scrolling fast with other components? HOT 2
- Remove nodes in pairs to avoid zebra stripe flashing HOT 1
- Need access to current scroll offset to avoid two lists scrolling each other exceed maximum rerender depth. HOT 1
- Scroll synchronization of List and Grid lagging HOT 4
- Why is the onscroll method triggered when the page is first rendered HOT 1
- Styling are not added properly in Table body
- Add "behavior" option for scroll methods
- VariableSizeGrid has issue when row count over 1000
- Section Header HOT 1
- Deteriorated performance on lists exceeding 1000 elements
- Can't use react-window in vite react-ts HOT 1
- innerElementType doesn't work HOT 1
- Input is losing focus , when wrapped with VariableSizeList HOT 2
- when i use "WindowScroller", why the page is constantly refreshing?
- Shifting all items in a list
- Loading indicator while it loads all 1m rows HOT 1
- onClick inside <List /> element doesn't work HOT 2
- Blanks appear on fast scrolling in react 18 use createRoot HOT 2
- add new item error when use VariableSizeList HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-window.