|
| 1 | +import React, { useEffect, useRef } from 'react'; |
| 2 | +import { VirtualItem } from 'react-virtual'; |
| 3 | + |
| 4 | +interface RowSubComponent { |
| 5 | + subComponentsHeight: Record<string, { rowId: string; subComponentHeight?: number }>; |
| 6 | + virtualRow: VirtualItem; |
| 7 | + dispatch: (e: { type: string; payload?: any }) => void; |
| 8 | + row: any; |
| 9 | + rowHeight: number; |
| 10 | + children: any; |
| 11 | + rows: any[]; |
| 12 | + alwaysShowSubComponent: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +export const RowSubComponent = (props: RowSubComponent) => { |
| 16 | + const { subComponentsHeight, virtualRow, dispatch, row, rowHeight, children, rows, alwaysShowSubComponent } = props; |
| 17 | + const subComponentRef = useRef(null); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const subComponentHeightObserver = new ResizeObserver((entries) => { |
| 21 | + entries.forEach((entry) => { |
| 22 | + if (entry.borderBoxSize) { |
| 23 | + // Firefox implements `borderBoxSize` as a single content rect, rather than an array |
| 24 | + const borderBoxSize = Array.isArray(entry.borderBoxSize) ? entry.borderBoxSize[0] : entry.borderBoxSize; |
| 25 | + if ( |
| 26 | + subComponentsHeight?.[virtualRow.index]?.subComponentHeight !== borderBoxSize.blockSize && |
| 27 | + borderBoxSize.blockSize !== 0 |
| 28 | + ) { |
| 29 | + // use most common sub-component height of first 10 sub-components as default height |
| 30 | + if (alwaysShowSubComponent && subComponentsHeight && Object.keys(subComponentsHeight).length === 10) { |
| 31 | + const objGroupedByHeight = Object.values(subComponentsHeight).reduce((acc, cur) => { |
| 32 | + const count = acc?.[cur.subComponentHeight]; |
| 33 | + if (typeof count === 'number') { |
| 34 | + return { ...acc, [cur.subComponentHeight]: count + 1 }; |
| 35 | + } |
| 36 | + return { ...acc, [cur.subComponentHeight]: 1 }; |
| 37 | + }, {}); |
| 38 | + |
| 39 | + const mostUsedHeight = Object.keys(objGroupedByHeight).reduce((a, b) => |
| 40 | + objGroupedByHeight[a] > objGroupedByHeight[b] ? a : b |
| 41 | + ); |
| 42 | + const estimatedHeights = rows.reduce((acc, cur, index) => { |
| 43 | + acc[index] = { subComponentHeight: parseInt(mostUsedHeight), rowId: cur.id }; |
| 44 | + return acc; |
| 45 | + }, {}); |
| 46 | + dispatch({ |
| 47 | + type: 'SUB_COMPONENTS_HEIGHT', |
| 48 | + payload: { ...estimatedHeights, ...subComponentsHeight } |
| 49 | + }); |
| 50 | + } else { |
| 51 | + dispatch({ |
| 52 | + type: 'SUB_COMPONENTS_HEIGHT', |
| 53 | + payload: { |
| 54 | + ...subComponentsHeight, |
| 55 | + [virtualRow.index]: { subComponentHeight: borderBoxSize.blockSize, rowId: row.id } |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + }); |
| 62 | + }); |
| 63 | + if (subComponentRef.current?.firstChild) { |
| 64 | + subComponentHeightObserver.observe(subComponentRef.current?.firstChild); |
| 65 | + } |
| 66 | + return () => { |
| 67 | + subComponentHeightObserver.disconnect(); |
| 68 | + }; |
| 69 | + }, [ |
| 70 | + subComponentRef.current?.firstChild, |
| 71 | + subComponentsHeight, |
| 72 | + row.id, |
| 73 | + subComponentsHeight?.[virtualRow.index]?.subComponentHeight, |
| 74 | + virtualRow.index |
| 75 | + ]); |
| 76 | + |
| 77 | + return ( |
| 78 | + <div |
| 79 | + ref={subComponentRef} |
| 80 | + style={{ |
| 81 | + transform: `translateY(${rowHeight}px)`, |
| 82 | + position: 'absolute', |
| 83 | + width: '100%' |
| 84 | + }} |
| 85 | + > |
| 86 | + {children} |
| 87 | + </div> |
| 88 | + ); |
| 89 | +}; |
0 commit comments