Skip to content

feat: horizontal virtual #1112

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions docs/examples/virtual.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import '../../assets/index.less';
import { VirtualTable } from '../../src';
import type { ColumnsType, Reference } from '../../src/interface';
Expand All @@ -11,7 +11,7 @@ interface RecordType {
indexKey: string;
}

const columns: ColumnsType = [
const defaultColumns: ColumnsType = [
// { title: 'title1', dataIndex: 'a', key: 'a', width: 100,},
// { title: 'title1', dataIndex: 'a', key: 'a', width: 100, },
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100, fixed: 'left' },
Expand Down Expand Up @@ -162,15 +162,6 @@ const columns: ColumnsType = [
},
];

export function cleanOnCell(cols: any = []) {
cols.forEach(col => {
delete (col as any).onCell;

cleanOnCell((col as any).children);
});
}
cleanOnCell(columns);

const data: RecordType[] = new Array(4 * 10000).fill(null).map((_, index) => ({
a: `a${index}`,
b: `b${index}`,
Expand All @@ -190,9 +181,21 @@ const data: RecordType[] = new Array(4 * 10000).fill(null).map((_, index) => ({

const Demo = () => {
const tblRef = React.useRef<Reference>();
const [enableColRowSpan, setEnableColRowSpan] = useState(false);

function cleanOnCell(cols: ColumnsType) {
return cols?.map(({ onCell, ...col }: any) => {
return { ...col, children: cleanOnCell(col.children) };
});
}

const columns = enableColRowSpan ? defaultColumns : cleanOnCell(defaultColumns);

return (
<div style={{ width: 800, padding: `0 64px` }}>
<button onClick={() => setEnableColRowSpan(!enableColRowSpan)}>
Toggle RowSpan and ColSpan
</button>
<button
onClick={() => {
tblRef.current?.scrollTo({
Expand All @@ -214,6 +217,7 @@ const Demo = () => {
</button>

<VirtualTable
virtual={{ x: true }}
columns={columns}
// expandedRowRender={({ b, c }) => b || c}
scroll={{ x: 1300, y: 200 }}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"classnames": "^2.2.5",
"rc-resize-observer": "^1.1.0",
"rc-util": "^5.37.0",
"rc-virtual-list": "^3.11.1"
"rc-virtual-list": "^3.13.0"
},
"devDependencies": {
"@rc-component/father-plugin": "^1.0.2",
Expand Down
3 changes: 2 additions & 1 deletion src/VirtualTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
};

const extraRender: ListProps<any>['extraRender'] = info => {
const { start, end, getSize, offsetY } = info;
const { start, end, getSize, offsetX, offsetY } = info;

// Do nothing if no data
if (end < 0) {
Expand Down Expand Up @@ -189,6 +189,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
style={{
top: -offsetY + sizeInfo.top,
}}
offsetX={offsetX}
extra
getHeight={getHeight}
/>
Expand Down
48 changes: 34 additions & 14 deletions src/VirtualTable/BodyLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,41 @@ import type { FlattenData } from '../hooks/useFlattenRecords';
import useRowInfo from '../hooks/useRowInfo';
import VirtualCell from './VirtualCell';
import { StaticContext } from './context';
import { getCellProps } from '../Body/BodyRow';
import VirtualRow from './VirtualRow';

export interface BodyLineProps<RecordType = any> {
data: FlattenData<RecordType>;
index: number;
className?: string;
style?: React.CSSProperties;
rowKey: React.Key;
offsetX: number;

/** Render cell only when it has `rowSpan > 1` */
extra?: boolean;
getHeight?: (rowSpan: number) => number;
}

const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) => {
const { data, index, className, rowKey, style, extra, getHeight, ...restProps } = props;
const { data, index, className, rowKey, style, extra, getHeight, offsetX, ...restProps } = props;
const { record, indent, index: renderIndex } = data;

const { scrollX, flattenColumns, prefixCls, fixColumn, componentWidth } = useContext(
TableContext,
['prefixCls', 'flattenColumns', 'fixColumn', 'componentWidth', 'scrollX'],
);
const { getComponent } = useContext(StaticContext, ['getComponent']);
const { getComponent, horizontalVirtual } = useContext(StaticContext, [
'getComponent',
'horizontalVirtual',
]);

const rowInfo = useRowInfo(record, rowKey, index, indent);

const cellPropsCollections = flattenColumns.map((column, colIndex) =>
getCellProps(rowInfo, column, colIndex, indent, index),
);

const RowComponent = getComponent(['body', 'row'], 'div');
const cellComponent = getComponent(['body', 'cell'], 'div');

Expand Down Expand Up @@ -87,6 +97,16 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
rowStyle.pointerEvents = 'none';
}

const shareCellProps = {
index,
renderIndex,
inverse: extra,
record,
rowInfo,
component: cellComponent,
getHeight,
};

const rowNode = (
<RowComponent
{...rowProps}
Expand All @@ -98,23 +118,23 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
})}
style={{ ...rowStyle, ...rowProps?.style }}
>
{flattenColumns.map((column, colIndex) => {
return (
{horizontalVirtual ? (
<VirtualRow
cellPropsCollections={cellPropsCollections}
offsetX={offsetX}
{...shareCellProps}
/>
) : (
flattenColumns.map((column, colIndex) => (
<VirtualCell
key={colIndex}
component={cellComponent}
rowInfo={rowInfo}
column={column}
colIndex={colIndex}
indent={indent}
index={index}
renderIndex={renderIndex}
record={record}
inverse={extra}
getHeight={getHeight}
cellProps={cellPropsCollections[colIndex]}
{...shareCellProps}
/>
);
})}
))
)}
</RowComponent>
);

Expand Down
14 changes: 4 additions & 10 deletions src/VirtualTable/VirtualCell.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from '@rc-component/context';
import classNames from 'classnames';
import * as React from 'react';
import { getCellProps } from '../Body/BodyRow';
import type { getCellProps } from '../Body/BodyRow';
import Cell from '../Cell';
import type useRowInfo from '../hooks/useRowInfo';
import type { ColumnType, CustomizeComponent } from '../interface';
Expand All @@ -11,12 +11,12 @@ export interface VirtualCellProps<RecordType> {
rowInfo: ReturnType<typeof useRowInfo<RecordType>>;
column: ColumnType<RecordType>;
colIndex: number;
indent: number;
index: number;
component?: CustomizeComponent;
/** Used for `column.render` */
renderIndex: number;
record: RecordType;
cellProps: ReturnType<typeof getCellProps>;

// Follow props is used for RowSpanVirtualCell only
style?: React.CSSProperties;
Expand All @@ -41,28 +41,22 @@ function VirtualCell<RecordType = any>(props: VirtualCellProps<RecordType>) {
rowInfo,
column,
colIndex,
indent,
index,
component,
renderIndex,
record,
style,
className,
inverse,
cellProps,
getHeight,
} = props;

const { render, dataIndex, className: columnClassName, width: colWidth } = column;

const { columnsOffset } = useContext(GridContext, ['columnsOffset']);

const { key, fixedInfo, appendCellNode, additionalCellProps } = getCellProps(
rowInfo,
column,
colIndex,
indent,
index,
);
const { key, fixedInfo, appendCellNode, additionalCellProps } = cellProps;

const { style: cellStyle, colSpan = 1, rowSpan = 1 } = additionalCellProps;

Expand Down
44 changes: 44 additions & 0 deletions src/VirtualTable/VirtualRow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as React from 'react';
import { useContext } from '@rc-component/context';
import TableContext from '../context/TableContext';
import useHorizontalVirtual from './useHorizontalVirtual';
import type { getCellProps } from '../Body/BodyRow';
import VirtualCell from './VirtualCell';
import type useRowInfo from '../hooks/useRowInfo';
import type { CustomizeComponent } from '../interface';

export interface VirtualRowProps {
cellPropsCollections: ReturnType<typeof getCellProps>[];
offsetX: number;
index: number;
renderIndex: number;
inverse: boolean;
record: any;
rowInfo: ReturnType<typeof useRowInfo>;
component: CustomizeComponent;
getHeight: (rowSpan: number) => number;
}

export default function VirtualRow({
cellPropsCollections,
offsetX,
...restProps
}: VirtualRowProps) {
const { flattenColumns } = useContext(TableContext, ['flattenColumns']);

const [startIndex, virtualOffset, showColumnIndexes] = useHorizontalVirtual(
cellPropsCollections,
offsetX,
);

return showColumnIndexes.map(colIndex => (
<VirtualCell
key={colIndex}
column={flattenColumns[colIndex]}
colIndex={colIndex}
style={startIndex === colIndex ? { marginLeft: virtualOffset } : {}}
cellProps={cellPropsCollections[colIndex]}
{...restProps}
/>
));
}
1 change: 1 addition & 0 deletions src/VirtualTable/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface StaticContextProps {
scrollY: number;
listItemHeight: number;
sticky: boolean | TableSticky;
horizontalVirtual: boolean;
getComponent: GetComponent;
onScroll?: React.UIEventHandler<HTMLDivElement>;
}
Expand Down
17 changes: 14 additions & 3 deletions src/VirtualTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface VirtualTableProps<RecordType> extends Omit<TableProps<RecordTyp
x?: number;
y: number;
};
virtual?: { x?: boolean };
listItemHeight?: number;
}

Expand All @@ -33,6 +34,7 @@ function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>, ref: Rea
className,
listItemHeight,
components,
virtual,
onScroll,
} = props;

Expand Down Expand Up @@ -63,10 +65,19 @@ function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>, ref: Rea
// Memo this
const onInternalScroll = useEvent(onScroll);

const horizontalVirtual = !!virtual?.x;

// ========================= Context ==========================
const context = React.useMemo(
() => ({ sticky, scrollY, listItemHeight, getComponent, onScroll: onInternalScroll }),
[sticky, scrollY, listItemHeight, getComponent, onInternalScroll],
() => ({
sticky,
scrollY,
listItemHeight,
horizontalVirtual,
getComponent,
onScroll: onInternalScroll,
}),
[sticky, scrollY, listItemHeight, horizontalVirtual, getComponent, onInternalScroll],
);

// ========================== Render ==========================
Expand All @@ -93,7 +104,7 @@ function VirtualTable<RecordType>(props: VirtualTableProps<RecordType>, ref: Rea
}

export type ForwardGenericVirtualTable = (<RecordType>(
props: TableProps<RecordType> & React.RefAttributes<Reference>,
props: VirtualTableProps<RecordType> & React.RefAttributes<Reference>,
) => React.ReactElement) & { displayName?: string };

const RefVirtualTable = React.forwardRef(VirtualTable) as ForwardGenericVirtualTable;
Expand Down
Loading
Loading