Skip to content

Commit

Permalink
chore: collect rows
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Aug 17, 2023
1 parent e3e81d8 commit 3b653f1
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions src/StaticTable/BodyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import VirtualList, { type ListProps, type ListRef } from 'rc-virtual-list';
import * as React from 'react';
import TableContext from '../context/TableContext';
import useFlattenRecords, { type FlattenData } from '../hooks/useFlattenRecords';
import type { OnCustomizeScroll } from '../interface';
import type { ColumnType, OnCustomizeScroll } from '../interface';
import BodyLine from './BodyLine';
import StaticContext from './StaticContext';

Expand Down Expand Up @@ -70,35 +70,58 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
});

// ======================= Col/Row Span =======================
const isPureRow = (record: any, index: number) =>
flattenColumns.every(({ onCell }) => {
if (onCell) {
const cellProps = onCell(record, index) as React.TdHTMLAttributes<HTMLElement>;
return cellProps?.rowSpan !== 0;
}
return true;
});
// const isPureRow = (record: any, index: number) =>
// flattenColumns.every(({ onCell }) => {
// if (onCell) {
// const cellProps = onCell(record, index) as React.TdHTMLAttributes<HTMLElement>;
// return cellProps?.rowSpan !== 0;
// }
// return true;
// });

const isColumnHaveRowSpan = (column: ColumnType<any>, record: any, index: number) => {
const { onCell } = column;

if (onCell) {
const cellProps = onCell(record, index) as React.TdHTMLAttributes<HTMLElement>;
return cellProps?.rowSpan === 0;
}

return false;
};

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

// Find first rowSpan column
const firstRecord = flattenData[start]?.record;
const firstRowSpanColumns = flattenColumns.filter(column =>
isColumnHaveRowSpan(column, firstRecord, start),
);

let startIndex = start;
let endIndex = end;

// Collect rowSpan records range
for (let i = start; i >= 0; i -= 1) {
const { record } = flattenData[i];

if (isPureRow(record, i)) {
if (firstRowSpanColumns.every(column => !isColumnHaveRowSpan(column, record, i))) {
startIndex = i;
break;
}
}

// Find last rowSpan column
const lastRecord = flattenData[end]?.record;
const lastRowSpanColumns = flattenColumns.filter(column =>
isColumnHaveRowSpan(column, lastRecord, end),
);

let endIndex = end;

for (let i = end; i < flattenData.length; i += 1) {
const { record } = flattenData[i];

if (isPureRow(record, i)) {
if (lastRowSpanColumns.every(column => !isColumnHaveRowSpan(column, record, i))) {
endIndex = i;
break;
}
Expand Down

0 comments on commit 3b653f1

Please sign in to comment.