Skip to content
Merged
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
24 changes: 1 addition & 23 deletions src/Body/BodyRow.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import classNames from 'classnames';
import ResizeObserver from 'rc-resize-observer';
import Cell from '../Cell';
import TableContext from '../context/TableContext';
import BodyContext from '../context/BodyContext';
Expand All @@ -13,7 +12,6 @@ import {
Key,
GetRowKey,
} from '../interface';
import ResizeContext from '../context/ResizeContext';
import { getCellFixedInfo } from '../utils/fixUtil';
import ExpandedRow from './ExpandedRow';

Expand All @@ -22,8 +20,6 @@ export interface BodyRowProps<RecordType> {
index: number;
className?: string;
style?: React.CSSProperties;
/** Set if need collect column width info */
measureColumnWidth: boolean;
stickyOffsets: StickyOffsets;
recordKey: Key;
expandedKeys: Set<Key>;
Expand Down Expand Up @@ -51,7 +47,6 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
indent = 0,
rowComponent: RowComponent,
cellComponent,
measureColumnWidth,
childrenColumnName,
} = props;
const { prefixCls } = React.useContext(TableContext);
Expand All @@ -70,7 +65,6 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
expandedRowRender,
expandIconColumnIndex,
} = React.useContext(BodyContext);
const { onColumnResize } = React.useContext(ResizeContext);
const [expandRended, setExpandRended] = React.useState(false);

const expanded = props.expandedKeys.has(props.recordKey);
Expand Down Expand Up @@ -165,7 +159,7 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
additionalCellProps = column.onCell(record, index);
}

const cellNode = (
return (
<Cell
className={columnClassName}
ellipsis={column.ellipsis}
Expand All @@ -182,21 +176,6 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
additionalProps={additionalCellProps}
/>
);

if (measureColumnWidth) {
return (
<ResizeObserver
key={key}
onResize={({ width }) => {
onColumnResize(key, width);
}}
>
{cellNode}
</ResizeObserver>
);
}

return cellNode;
})}
</RowComponent>
);
Expand Down Expand Up @@ -243,7 +222,6 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
recordKey={subKey}
index={subIndex}
indent={indent + 1}
measureColumnWidth={false}
/>
);
},
Expand Down
29 changes: 27 additions & 2 deletions src/Body/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as React from 'react';
import ResizeObserver from 'rc-resize-observer';
import BodyRow from './BodyRow';
import TableContext from '../context/TableContext';
import { GetRowKey, StickyOffsets, Key, GetComponentProps } from '../interface';
import ExpandedRow from './ExpandedRow';
import BodyContext from '../context/BodyContext';
import { getColumnsKey } from '../utils/valueUtil';
import ResizeContext from '../context/ResizeContext';

export interface BodyProps<RecordType> {
data: RecordType[];
Expand All @@ -28,6 +31,7 @@ function Body<RecordType>({
emptyNode,
childrenColumnName,
}: BodyProps<RecordType>) {
const { onColumnResize } = React.useContext(ResizeContext);
const { prefixCls, getComponent } = React.useContext(TableContext);
const { fixHeader, fixColumn, flattenColumns, componentWidth } = React.useContext(BodyContext);

Expand All @@ -48,7 +52,6 @@ function Body<RecordType>({
record={record}
recordKey={key}
index={index}
measureColumnWidth={measureColumnWidth && index === 0}
rowComponent={trComponent}
cellComponent={tdComponent}
stickyOffsets={stickyOffsets}
Expand Down Expand Up @@ -78,7 +81,29 @@ function Body<RecordType>({
);
}

return <WrapperComponent className={`${prefixCls}-tbody`}>{rows}</WrapperComponent>;
const columnsKey = getColumnsKey(flattenColumns);

return (
<WrapperComponent className={`${prefixCls}-tbody`}>
{/* Measure body column width with additional hidden col */}
{measureColumnWidth && (
<tr aria-hidden="true" className={`${prefixCls}-measure-row`}>
{columnsKey.map(columnKey => (
<ResizeObserver
key={columnKey}
onResize={({ width }) => {
onColumnResize(columnKey, width);
}}
>
<td style={{ padding: 0, border: 0, height: 0 }} />
</ResizeObserver>
))}
</tr>
)}

{rows}
</WrapperComponent>
);
}, [
data,
prefixCls,
Expand Down
17 changes: 1 addition & 16 deletions src/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import TableContext from '../context/TableContext';

function parseHeaderRows<RecordType>(
rootColumns: ColumnsType<RecordType>,
measureColumnWidth: boolean,
): CellType<RecordType>[][] {
const rows: CellType<RecordType>[][] = [];

// Record which cell should wrapped with resize observer used for fixed
const measureCells: boolean[] = [];

function fillRowCells(
columns: ColumnsType<RecordType>,
colIndex: number,
Expand Down Expand Up @@ -54,12 +50,6 @@ function parseHeaderRows<RecordType>(

currentColIndex += colSpan;

// Measure only happen on single node
if (measureColumnWidth && colSpan === 1 && !measureCells[cell.colStart]) {
cell.measure = true;
measureCells[cell.colStart] = true;
}

return colSpan;
});

Expand Down Expand Up @@ -87,22 +77,17 @@ export interface HeaderProps<RecordType> {
columns: ColumnsType<RecordType>;
flattenColumns: ColumnType<RecordType>[];
stickyOffsets: StickyOffsets;
measureColumnWidth?: boolean;
onHeaderRow: GetComponentProps<ColumnType<RecordType>[]>;
}

function Header<RecordType>({
stickyOffsets,
columns,
flattenColumns,
measureColumnWidth,
onHeaderRow,
}: HeaderProps<RecordType>): React.ReactElement {
const { getComponent } = React.useContext(TableContext);
const rows: CellType<RecordType>[][] = React.useMemo(
() => parseHeaderRows(columns, measureColumnWidth),
[columns],
);
const rows: CellType<RecordType>[][] = React.useMemo(() => parseHeaderRows(columns), [columns]);

const WrapperComponent = getComponent(['header', 'wrapper'], 'thead');
const trComponent = getComponent(['header', 'row'], 'tr');
Expand Down
22 changes: 2 additions & 20 deletions src/Header/HeaderRow.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import ResizeObserver from 'rc-resize-observer';
import Cell from '../Cell';
import {
CellType,
Expand All @@ -10,7 +9,6 @@ import {
} from '../interface';
import TableContext from '../context/TableContext';
import { getCellFixedInfo } from '../utils/fixUtil';
import ResizeContext from '../context/ResizeContext';
import { getColumnsKey } from '../utils/valueUtil';

export interface RowProps<RecordType> {
Expand All @@ -33,7 +31,6 @@ function HeaderRow<RecordType>({
index,
}: RowProps<RecordType>) {
const { prefixCls } = React.useContext(TableContext);
const { onColumnResize } = React.useContext(ResizeContext);

let rowProps: React.HTMLAttributes<HTMLElement>;
if (onHeaderRow) {
Expand All @@ -45,7 +42,7 @@ function HeaderRow<RecordType>({
return (
<RowComponent {...rowProps}>
{cells.map((cell: CellType<RecordType>, cellIndex) => {
const { column, measure } = cell;
const { column } = cell;
const fixedInfo = getCellFixedInfo(
cell.colStart,
cell.colEnd,
Expand All @@ -58,7 +55,7 @@ function HeaderRow<RecordType>({
additionalProps = cell.column.onHeaderCell(column);
}

let cellNode = (
return (
<Cell
{...cell}
ellipsis={column.ellipsis}
Expand All @@ -70,21 +67,6 @@ function HeaderRow<RecordType>({
additionalProps={additionalProps}
/>
);

if (measure) {
cellNode = (
<ResizeObserver
key={cellIndex}
onResize={({ width }) => {
onColumnResize(columnsKey[cell.colStart], width);
}}
>
{cellNode}
</ResizeObserver>
);
}

return cellNode;
})}
</RowComponent>
);
Expand Down
8 changes: 1 addition & 7 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
>
<TableComponent style={{ ...scrollTableStyle, tableLayout: mergedTableLayout }}>
{bodyColGroup}
{showHeader !== false && (
<Header
{...headerProps}
{...columnContext}
measureColumnWidth={!hasData && fixColumn}
/>
)}
{showHeader !== false && <Header {...headerProps} {...columnContext} />}
{bodyTable}
{footerTable}
</TableComponent>
Expand Down
1 change: 0 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export interface CellType<RecordType> {
hasSubColumns?: boolean;
colStart?: number;
colEnd?: number;
measure?: boolean;
}

export interface RenderedCell<RecordType> {
Expand Down
17 changes: 17 additions & 0 deletions tests/__snapshots__/ExpandRow.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ exports[`Table.Expand renders fixed column correctly work 1`] = `
<tbody
class="rc-table-tbody"
>
<tr
aria-hidden="true"
class="rc-table-measure-row"
>
<td
style="padding: 0px; border: 0px; height: 0px;"
/>
<td
style="padding: 0px; border: 0px; height: 0px;"
/>
<td
style="padding: 0px; border: 0px; height: 0px;"
/>
<td
style="padding: 0px; border: 0px; height: 0px;"
/>
</tr>
<tr
class="rc-table-row rc-table-row-level-0"
data-row-key="0"
Expand Down
Loading