- Notifications
You must be signed in to change notification settings - Fork277
[Feat]: Add virtualization for Table Component#1989
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
raheeliftikhar5 merged 3 commits intolowcoder-org:devfromiamfaran:feat/table-main-virtualizationSep 16, 2025
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
222 changes: 222 additions & 0 deletionsclient/packages/lowcoder/src/comps/comps/tableComp/ResizeableTable.tsx
This file contains hidden or 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,222 @@ | ||
import { default as Table, TableProps, ColumnType } from "antd/es/table"; | ||
import React, { useCallback, useMemo, useRef, useState } from "react"; | ||
import { Resizable } from "react-resizable"; | ||
import styled from "styled-components"; | ||
import _ from "lodash"; | ||
import { useUserViewMode } from "util/hooks"; | ||
import { ReactRef, ResizeHandleAxis } from "layout/gridLayoutPropTypes"; | ||
import { COL_MIN_WIDTH, RecordType, CustomColumnType } from "./tableUtils"; | ||
import { RowColorViewType, RowHeightViewType } from "./tableTypes"; | ||
import { TableColumnStyleType, TableColumnLinkStyleType } from "comps/controls/styleControlConstants"; | ||
import { CellColorViewType } from "./column/tableColumnComp"; | ||
import { TableCellView } from "./TableCell"; | ||
import { TableRowView } from "./TableRow"; | ||
const TitleResizeHandle = styled.span` | ||
position: absolute; | ||
top: 0; | ||
right: -5px; | ||
width: 10px; | ||
height: 100%; | ||
cursor: col-resize; | ||
z-index: 1; | ||
`; | ||
const TableTh = styled.th<{ width?: number }>` | ||
overflow: hidden; | ||
> div { | ||
overflow: hidden; | ||
white-space: pre; | ||
text-overflow: ellipsis; | ||
} | ||
${(props) => props.width && `width: ${props.width}px`}; | ||
`; | ||
const ResizeableTitle = React.forwardRef<HTMLTableHeaderCellElement, any>((props, ref) => { | ||
const { onResize, onResizeStop, width, viewModeResizable, ...restProps } = props; | ||
const [childWidth, setChildWidth] = useState(0); | ||
const resizeRef = useRef<HTMLTableHeaderCellElement>(null); | ||
const isUserViewMode = useUserViewMode(); | ||
const updateChildWidth = useCallback(() => { | ||
if (resizeRef.current) { | ||
const width = resizeRef.current.getBoundingClientRect().width; | ||
setChildWidth(width); | ||
} | ||
}, []); | ||
React.useEffect(() => { | ||
updateChildWidth(); | ||
const resizeObserver = new ResizeObserver(() => { | ||
updateChildWidth(); | ||
}); | ||
if (resizeRef.current) { | ||
resizeObserver.observe(resizeRef.current); | ||
} | ||
return () => { | ||
resizeObserver.disconnect(); | ||
}; | ||
}, [updateChildWidth]); | ||
React.useImperativeHandle(ref, () => resizeRef.current!, []); | ||
const isNotDataColumn = _.isNil(restProps.title); | ||
if ((isUserViewMode && !restProps.viewModeResizable) || isNotDataColumn) { | ||
return <TableTh ref={resizeRef} {...restProps} width={width} />; | ||
} | ||
return ( | ||
<Resizable | ||
width={width > 0 ? width : childWidth} | ||
height={0} | ||
onResize={(e: React.SyntheticEvent, { size }: { size: { width: number } }) => { | ||
e.stopPropagation(); | ||
onResize(size.width); | ||
}} | ||
onResizeStart={(e: React.SyntheticEvent) => { | ||
updateChildWidth(); | ||
e.stopPropagation(); | ||
e.preventDefault(); | ||
}} | ||
onResizeStop={onResizeStop} | ||
draggableOpts={{ enableUserSelectHack: false }} | ||
handle={(axis: ResizeHandleAxis, ref: ReactRef<HTMLDivElement>) => ( | ||
<TitleResizeHandle | ||
ref={ref} | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
}} | ||
/> | ||
)} | ||
> | ||
<TableTh ref={resizeRef} {...restProps} title="" /> | ||
</Resizable> | ||
); | ||
}); | ||
type CustomTableProps<RecordType> = Omit<TableProps<RecordType>, "components" | "columns"> & { | ||
columns: CustomColumnType<RecordType>[]; | ||
viewModeResizable: boolean; | ||
visibleResizables: boolean; | ||
rowColorFn: RowColorViewType; | ||
rowHeightFn: RowHeightViewType; | ||
columnsStyle: TableColumnStyleType; | ||
size?: string; | ||
rowAutoHeight?: boolean; | ||
customLoading?: boolean; | ||
onCellClick: (columnName: string, dataIndex: string) => void; | ||
virtual?: boolean; | ||
scroll?: { | ||
x?: number | string; | ||
y?: number | string; | ||
}; | ||
}; | ||
function ResizeableTableComp<RecordType extends object>(props: CustomTableProps<RecordType>) { | ||
const { | ||
columns, | ||
viewModeResizable, | ||
visibleResizables, | ||
rowColorFn, | ||
rowHeightFn, | ||
columnsStyle, | ||
size, | ||
rowAutoHeight, | ||
customLoading, | ||
onCellClick, | ||
...restProps | ||
} = props; | ||
const [resizeData, setResizeData] = useState({ index: -1, width: -1 }); | ||
// Memoize resize handlers | ||
const handleResize = useCallback((width: number, index: number) => { | ||
setResizeData({ index, width }); | ||
}, []); | ||
const handleResizeStop = useCallback((width: number, index: number, onWidthResize?: (width: number) => void) => { | ||
setResizeData({ index: -1, width: -1 }); | ||
if (onWidthResize) { | ||
onWidthResize(width); | ||
} | ||
}, []); | ||
// Memoize cell handlers | ||
const createCellHandler = useCallback((col: CustomColumnType<RecordType>) => { | ||
return (record: RecordType, index: number) => ({ | ||
record, | ||
title: String(col.dataIndex), | ||
rowColorFn, | ||
rowHeightFn, | ||
cellColorFn: col.cellColorFn, | ||
rowIndex: index, | ||
columnsStyle, | ||
columnStyle: col.style, | ||
linkStyle: col.linkStyle, | ||
tableSize: size, | ||
autoHeight: rowAutoHeight, | ||
onClick: () => onCellClick(col.titleText, String(col.dataIndex)), | ||
loading: customLoading, | ||
customAlign: col.align, | ||
}); | ||
}, [rowColorFn, rowHeightFn, columnsStyle, size, rowAutoHeight, onCellClick, customLoading]); | ||
// Memoize header cell handlers | ||
const createHeaderCellHandler = useCallback((col: CustomColumnType<RecordType>, index: number, resizeWidth: number) => { | ||
return () => ({ | ||
width: resizeWidth, | ||
title: col.titleText, | ||
viewModeResizable, | ||
onResize: (width: React.SyntheticEvent) => { | ||
if (width) { | ||
handleResize(Number(width), index); | ||
} | ||
}, | ||
onResizeStop: (e: React.SyntheticEvent, { size }: { size: { width: number } }) => { | ||
handleResizeStop(size.width, index, col.onWidthResize); | ||
}, | ||
}); | ||
}, [viewModeResizable, handleResize, handleResizeStop]); | ||
// Memoize columns to prevent unnecessary re-renders | ||
const memoizedColumns = useMemo(() => { | ||
return columns.map((col, index) => { | ||
const { width, style, linkStyle, cellColorFn, onWidthResize, ...restCol } = col; | ||
const resizeWidth = (resizeData.index === index ? resizeData.width : col.width) ?? 0; | ||
const column: ColumnType<RecordType> = { | ||
...restCol, | ||
width: typeof resizeWidth === "number" && resizeWidth > 0 ? resizeWidth : undefined, | ||
minWidth: typeof resizeWidth === "number" && resizeWidth > 0 ? undefined : COL_MIN_WIDTH, | ||
onCell: (record: RecordType, index?: number) => createCellHandler(col)(record, index ?? 0), | ||
onHeaderCell: () => createHeaderCellHandler(col, index, Number(resizeWidth))(), | ||
}; | ||
return column; | ||
}); | ||
}, [columns, resizeData, createCellHandler, createHeaderCellHandler]); | ||
return ( | ||
<Table<RecordType> | ||
components={{ | ||
header: { | ||
cell: ResizeableTitle, | ||
}, | ||
body: { | ||
cell: TableCellView, | ||
row: TableRowView, | ||
}, | ||
}} | ||
{...restProps} | ||
pagination={false} | ||
columns={memoizedColumns} | ||
/> | ||
); | ||
} | ||
ResizeableTableComp.whyDidYouRender = true; | ||
export const ResizeableTable = React.memo(ResizeableTableComp) as typeof ResizeableTableComp; | ||
export type { CustomTableProps }; |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.