Skip to content
Draft
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
3 changes: 0 additions & 3 deletions docs/demo/colspan-rowspan-legacy.md

This file was deleted.

3 changes: 3 additions & 0 deletions docs/demo/hover-perf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## hover-perf

<code src="../examples/hover-perf.tsx">
150 changes: 0 additions & 150 deletions docs/examples/colspan-rowspan-legacy.tsx

This file was deleted.

64 changes: 16 additions & 48 deletions src/Cell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { supportRef } from 'rc-util/lib/ref';
import type {
DataIndex,
ColumnType,
RenderedCell,
CustomizeComponent,
CellType,
DefaultRecordType,
AlignType,
CellEllipsisType,
Expand All @@ -17,7 +15,6 @@ import StickyContext from '../context/StickyContext';
import HoverContext from '../context/HoverContext';
import type { HoverContextProps } from '../context/HoverContext';
import warning from 'rc-util/lib/warning';
import PerfContext from '../context/PerfContext';
import { useContextSelector } from '../ContextSelector';

/** Check if cell is in hover range */
Expand All @@ -26,9 +23,7 @@ function inHoverRange(cellStartRow: number, cellRowSpan: number, startRow: numbe
return cellStartRow <= endRow && cellEndRow >= startRow;
}

function isRenderCell<RecordType>(
data: React.ReactNode | RenderedCell<RecordType>,
): data is RenderedCell<RecordType> {
function isRenderCell(data: any) {
return data && typeof data === 'object' && !Array.isArray(data) && !React.isValidElement(data);
}

Expand Down Expand Up @@ -139,53 +134,35 @@ function Cell<RecordType extends DefaultRecordType>(
): React.ReactElement {
const cellPrefixCls = `${prefixCls}-cell`;

const perfRecord = React.useContext(PerfContext);
const supportSticky = React.useContext(StickyContext);

// ==================== Child Node ====================
const [childNode, legacyCellProps] = React.useMemo<
[React.ReactNode, CellType<RecordType>] | [React.ReactNode]
>(() => {
const childNode = React.useMemo<React.ReactNode>(() => {
if (validateValue(children)) {
return [children];
return children;
}

const value = getPathValue<Record<string, unknown> | React.ReactNode, RecordType>(
record,
dataIndex,
);
const value = getPathValue<React.ReactNode, RecordType>(record, dataIndex);

// Customize render node
let returnChildNode = value;
let returnCellProps: CellType<RecordType> | undefined = undefined;

if (render) {
const renderData = render(value, record, renderIndex);

if (isRenderCell(renderData)) {
if (process.env.NODE_ENV !== 'production') {
warning(
false,
'`columns.render` return cell props is deprecated with perf issue, please use `onCell` instead.',
);
}
returnChildNode = renderData.children;
returnCellProps = renderData.props;
perfRecord.renderWithProps = true;
} else {
returnChildNode = renderData;
const renderNode = render(value, record, renderIndex);
if (process.env.NODE_ENV !== 'production' && isRenderCell(renderNode)) {
warning(
false,
'`column.render` do not support cell props any more, please use `onCell` instead.',
);
}
return renderNode;
}

return [returnChildNode, returnCellProps];
return value;
}, [
/* eslint-disable react-hooks/exhaustive-deps */
// Always re-render if `renderWithProps`
perfRecord.renderWithProps ? Math.random() : 0,
/* eslint-enable */
children,
dataIndex,
perfRecord,
record,
render,
renderIndex,
Expand All @@ -206,15 +183,8 @@ function Cell<RecordType extends DefaultRecordType>(
mergedChildNode = <span className={`${cellPrefixCls}-content`}>{mergedChildNode}</span>;
}

const {
colSpan: cellColSpan,
rowSpan: cellRowSpan,
style: cellStyle,
className: cellClassName,
...restCellProps
} = legacyCellProps || {};
const mergedColSpan = (cellColSpan !== undefined ? cellColSpan : colSpan) ?? 1;
const mergedRowSpan = (cellRowSpan !== undefined ? cellRowSpan : rowSpan) ?? 1;
const mergedColSpan = colSpan ?? 1;
const mergedRowSpan = rowSpan ?? 1;

if (mergedColSpan === 0 || mergedRowSpan === 0) {
return null;
Expand Down Expand Up @@ -269,7 +239,6 @@ function Cell<RecordType extends DefaultRecordType>(
ref: React.Ref<any>;
} = {
title,
...restCellProps,
...additionalProps,
colSpan: mergedColSpan !== 1 ? mergedColSpan : null,
rowSpan: mergedRowSpan !== 1 ? mergedRowSpan : null,
Expand All @@ -286,12 +255,11 @@ function Cell<RecordType extends DefaultRecordType>(
[`${cellPrefixCls}-ellipsis`]: ellipsis,
[`${cellPrefixCls}-with-append`]: appendNode,
[`${cellPrefixCls}-fix-sticky`]: (isFixLeft || isFixRight) && isSticky && supportSticky,
[`${cellPrefixCls}-row-hover`]: !legacyCellProps && hovering,
[`${cellPrefixCls}-row-hover`]: hovering,
},
additionalProps.className,
cellClassName,
),
style: { ...additionalProps.style, ...alignStyle, ...fixedStyle, ...cellStyle },
style: { ...additionalProps.style, ...alignStyle, ...fixedStyle },
onMouseEnter,
onMouseLeave,
ref: isRefComponent(Component) ? ref : null,
Expand Down
12 changes: 11 additions & 1 deletion src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import Summary from './Footer/Summary';
import StickyContext from './context/StickyContext';
import ExpandedRowContext from './context/ExpandedRowContext';
import { EXPAND_COLUMN } from './constant';
import RenderContext from './context/RenderContext';

// Used for conditions cache
const EMPTY_DATA = [];
Expand Down Expand Up @@ -171,7 +172,7 @@ export interface TableProps<RecordType = unknown>
sticky?: boolean | TableSticky;
}

function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordType>) {
function InternalTable<RecordType extends DefaultRecordType>(props: TableProps<RecordType>) {
const {
prefixCls,
className,
Expand Down Expand Up @@ -863,6 +864,15 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
);
}

function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordType>) {
// Tell cell that update is from parent
return (
<RenderContext.Provider value={Math.random()}>
<InternalTable {...props} />
</RenderContext.Provider>
);
}

Table.EXPAND_COLUMN = EXPAND_COLUMN;

Table.Column = Column;
Expand Down
5 changes: 5 additions & 0 deletions src/context/RenderContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as React from 'react';

const RenderContext = React.createContext<number>(null);

export default RenderContext;
11 changes: 1 addition & 10 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ export interface CellType<RecordType> {
colEnd?: number;
}

export interface RenderedCell<RecordType> {
props?: CellType<RecordType>;
children?: React.ReactNode;
}

export type DataIndex = string | number | readonly (string | number)[];

export type CellEllipsisType = { showTitle?: boolean } | boolean;
Expand All @@ -76,11 +71,7 @@ export type AlignType = 'left' | 'center' | 'right';
export interface ColumnType<RecordType> extends ColumnSharedType<RecordType> {
colSpan?: number;
dataIndex?: DataIndex;
render?: (
value: any,
record: RecordType,
index: number,
) => React.ReactNode | RenderedCell<RecordType>;
render?: (value: any, record: RecordType, index: number) => React.ReactNode;
shouldCellUpdate?: (record: RecordType, prevRecord: RecordType) => boolean;
rowSpan?: number;
width?: number | string;
Expand Down
Loading