|
| 1 | +import { |
| 2 | + PropsWithChildren, |
| 3 | + ReactElement, |
| 4 | + useEffect, |
| 5 | + useRef, |
| 6 | + useState, |
| 7 | +} from 'react'; |
| 8 | +import styles from './styles.module.scss'; |
| 9 | + |
| 10 | +interface ResizableTableProps { |
| 11 | + headers: { name: string; icon?: ReactElement }[]; |
| 12 | +} |
| 13 | + |
| 14 | +function ResizeHandler({ idx }: { idx: number }) { |
| 15 | + const handlerRef = useRef<HTMLDivElement>(null); |
| 16 | + const [resizing, setResizing] = useState(false); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + if (handlerRef.current && resizing) { |
| 20 | + const table = handlerRef.current?.parentNode?.parentNode?.parentNode |
| 21 | + ?.parentNode as HTMLTableElement; |
| 22 | + |
| 23 | + if (table) { |
| 24 | + const onMouseMove = (e: MouseEvent) => |
| 25 | + requestAnimationFrame(() => { |
| 26 | + const scrollOffset = table.scrollLeft; |
| 27 | + const width = |
| 28 | + scrollOffset + |
| 29 | + e.clientX - |
| 30 | + (( |
| 31 | + handlerRef.current?.parentNode as HTMLTableCellElement |
| 32 | + ).getBoundingClientRect().x || 0); |
| 33 | + |
| 34 | + if (table) { |
| 35 | + const columns = table.style.gridTemplateColumns.split(' '); |
| 36 | + columns[idx] = width + 'px'; |
| 37 | + table.style.gridTemplateColumns = columns.join(' '); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + const onMouseUp = () => { |
| 42 | + setResizing(false); |
| 43 | + }; |
| 44 | + |
| 45 | + table.addEventListener('mousemove', onMouseMove); |
| 46 | + table.addEventListener('mouseup', onMouseUp); |
| 47 | + |
| 48 | + return () => { |
| 49 | + table.removeEventListener('mousemove', onMouseMove); |
| 50 | + table.removeEventListener('mouseup', onMouseUp); |
| 51 | + }; |
| 52 | + } |
| 53 | + } |
| 54 | + }, [handlerRef, idx, resizing, setResizing]); |
| 55 | + |
| 56 | + return ( |
| 57 | + <div |
| 58 | + className={styles.handler} |
| 59 | + ref={handlerRef} |
| 60 | + onMouseDown={() => setResizing(true)} |
| 61 | + ></div> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +export default function ResizableTable({ |
| 66 | + headers, |
| 67 | + children, |
| 68 | +}: PropsWithChildren<ResizableTableProps>) { |
| 69 | + const tableRef = useRef<HTMLTableElement>(null); |
| 70 | + const [isGridCSSPrepared, setGridCSSPrepared] = useState(false); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + if (tableRef.current) { |
| 74 | + tableRef.current.style.gridTemplateColumns = |
| 75 | + headers.map(() => '150px').join(' ') || ''; |
| 76 | + setGridCSSPrepared(true); |
| 77 | + } |
| 78 | + }, [headers, tableRef, setGridCSSPrepared]); |
| 79 | + |
| 80 | + return ( |
| 81 | + <table ref={tableRef} className={styles.table}> |
| 82 | + {isGridCSSPrepared && ( |
| 83 | + <> |
| 84 | + <thead> |
| 85 | + <tr> |
| 86 | + {headers.map((header, idx) => ( |
| 87 | + <th key={header.name}> |
| 88 | + <div className={styles.headerContent}> |
| 89 | + <div className={styles.headerContentTitle}> |
| 90 | + {header.name} |
| 91 | + </div> |
| 92 | + {header.icon && ( |
| 93 | + <div className={styles.headerContentIcon}> |
| 94 | + header.icon |
| 95 | + </div> |
| 96 | + )} |
| 97 | + </div> |
| 98 | + <ResizeHandler idx={idx} /> |
| 99 | + </th> |
| 100 | + ))} |
| 101 | + </tr> |
| 102 | + </thead> |
| 103 | + |
| 104 | + <tbody>{children}</tbody> |
| 105 | + </> |
| 106 | + )} |
| 107 | + </table> |
| 108 | + ); |
| 109 | +} |
0 commit comments