Skip to content

Commit

Permalink
refactor: row in
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Aug 20, 2023
1 parent 3d35faf commit ba03688
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 29 deletions.
28 changes: 6 additions & 22 deletions src/Body/BodyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from 'react';
import Cell from '../Cell';
import { responseImmutable } from '../context/TableContext';
import devRenderTimes from '../hooks/useRenderTimes';
import type { ColumnType, CustomizeComponent, GetComponentProps, GetRowKey } from '../interface';
import type { ColumnType, CustomizeComponent, GetRowKey } from '../interface';
import ExpandedRow from './ExpandedRow';
import useRowInfo from '../hooks/useRowInfo';

Expand All @@ -16,7 +16,6 @@ export interface BodyRowProps<RecordType> {
rowComponent: CustomizeComponent;
cellComponent: CustomizeComponent;
scopeCellComponent: CustomizeComponent;
onRow: GetComponentProps<RecordType>;
indent?: number;
rowKey: React.Key;
getRowKey: GetRowKey<RecordType>;
Expand Down Expand Up @@ -99,26 +98,23 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
index,
renderIndex,
rowKey,
onRow,
indent = 0,
rowComponent: RowComponent,
cellComponent,
scopeCellComponent,
} = props;
const rowInfo = useRowInfo(record, rowKey);
const rowInfo = useRowInfo(record, rowKey, index);
const {
prefixCls,
flattenColumns,
expandRowByClick,
onTriggerExpand,
rowClassName,
expandedRowClassName,
expandedRowRender,
rowProps,

// Misc
expanded,
rowSupportExpand,
expandable,
} = rowInfo;

const [expandRended, setExpandRended] = React.useState(false);
Expand All @@ -133,17 +129,6 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
}
}, [expanded]);

// =========================== onRow ===========================
const additionalProps = onRow?.(record, index);

const onClick: React.MouseEventHandler<HTMLElement> = (event, ...args) => {
if (expandRowByClick && expandable) {
onTriggerExpand(record, event);
}

additionalProps?.onClick?.(event, ...args);
};

// ======================== Base tr row ========================
let computeRowClassName: string;
if (typeof rowClassName === 'string') {
Expand All @@ -154,20 +139,19 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(

const baseRowNode = (
<RowComponent
{...additionalProps}
{...rowProps}
data-row-key={rowKey}
className={classNames(
className,
`${prefixCls}-row`,
`${prefixCls}-row-level-${indent}`,
computeRowClassName,
additionalProps && additionalProps.className,
rowProps?.className,
)}
style={{
...style,
...(additionalProps ? additionalProps.style : null),
...rowProps?.style,
}}
onClick={onClick}
>
{flattenColumns.map((column: ColumnType<RecordType>, colIndex) => {
const { render, dataIndex, className: columnClassName } = column;
Expand Down
5 changes: 1 addition & 4 deletions src/Body/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import PerfContext from '../context/PerfContext';
import TableContext, { responseImmutable } from '../context/TableContext';
import useFlattenRecords from '../hooks/useFlattenRecords';
import devRenderTimes from '../hooks/useRenderTimes';
import type { GetComponentProps } from '../interface';
import { getColumnsKey } from '../utils/valueUtil';
import BodyRow from './BodyRow';
import ExpandedRow from './ExpandedRow';
Expand All @@ -14,7 +13,6 @@ import MeasureRow from './MeasureRow';
export interface BodyProps<RecordType> {
data: readonly RecordType[];
measureColumnWidth: boolean;
onRow: GetComponentProps<RecordType>;
emptyNode: React.ReactNode;
}

Expand All @@ -23,7 +21,7 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
devRenderTimes(props);
}

const { data, measureColumnWidth, onRow, emptyNode } = props;
const { data, measureColumnWidth, emptyNode } = props;

const {
prefixCls,
Expand Down Expand Up @@ -74,7 +72,6 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
rowComponent={trComponent}
cellComponent={tdComponent}
scopeCellComponent={thComponent}
onRow={onRow}
getRowKey={getRowKey}
indent={indent}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/StaticTable/BodyLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
]);
const { scrollX } = useContext(StaticContext, ['scrollX']);

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

// ========================== Expand ==========================
const { rowSupportExpand, expanded } = rowInfo;
Expand Down
4 changes: 2 additions & 2 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,6 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
<Body
data={mergedData}
measureColumnWidth={fixHeader || horizonScroll || isSticky}
onRow={onRow}
emptyNode={emptyNode}
/>
);
Expand Down Expand Up @@ -748,7 +747,6 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
horizonScroll,

// Body

tableLayout: mergedTableLayout,
rowClassName,
expandedRowClassName: expandableConfig.expandedRowClassName,
Expand All @@ -771,6 +769,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
hoverEndRow: endRow,
onHover,
rowExpandable: expandableConfig.rowExpandable,
onRow,

getRowKey,
expandedKeys: mergedExpandedKeys,
Expand Down Expand Up @@ -813,6 +812,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
endRow,
onHover,
expandableConfig.rowExpandable,
onRow,

getRowKey,
mergedExpandedKeys,
Expand Down
2 changes: 2 additions & 0 deletions src/context/TableContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ExpandableType,
ExpandedRowRender,
GetComponent,
GetComponentProps,
GetRowKey,
RenderExpandIcon,
RowClassName,
Expand Down Expand Up @@ -34,6 +35,7 @@ export interface TableContextProps<RecordType = any> {
// Body
rowClassName: string | RowClassName<RecordType>;
expandedRowClassName: RowClassName<RecordType>;
onRow?: GetComponentProps<RecordType>;

tableLayout: TableLayout;

Expand Down
22 changes: 22 additions & 0 deletions src/hooks/useRowInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useEvent } from 'rc-util';
export default function useRowInfo<RecordType>(
record: RecordType,
rowKey: React.Key,
recordIndex: number,
): Pick<
TableContextProps,
| 'prefixCls'
Expand All @@ -23,6 +24,7 @@ export default function useRowInfo<RecordType>(
| 'expandIconColumnIndex'
| 'expandedKeys'
| 'childrenColumnName'
| 'onRow'
> & {
columnsKey: React.Key[];
nestExpandable: boolean;
Expand All @@ -31,6 +33,7 @@ export default function useRowInfo<RecordType>(
record: RecordType;
rowSupportExpand: boolean;
expandable: boolean;
rowProps: React.HTMLAttributes<any> & React.TdHTMLAttributes<any>;
} {
const context: TableContextProps = useContext(TableContext, [
'prefixCls',
Expand All @@ -48,6 +51,7 @@ export default function useRowInfo<RecordType>(
'expandedKeys',
'childrenColumnName',
'rowExpandable',
'onRow',
]);

const {
Expand All @@ -57,6 +61,8 @@ export default function useRowInfo<RecordType>(
childrenColumnName,
onTriggerExpand,
rowExpandable,
onRow,
expandRowByClick,
} = context;

// ======================= Expandable =======================
Expand All @@ -72,6 +78,18 @@ export default function useRowInfo<RecordType>(

const onInternalTriggerExpand = useEvent(onTriggerExpand);

// ========================= onRow ==========================
const rowProps = onRow?.(record, recordIndex);
const onRowClick = rowProps?.onClick;

const onClick: React.MouseEventHandler<HTMLElement> = (event, ...args) => {
if (expandRowByClick && mergedExpandable) {
onTriggerExpand(record, event);
}

onRowClick?.(event, ...args);
};

// ========================= Column =========================
const columnsKey = getColumnsKey(flattenColumns);

Expand All @@ -85,5 +103,9 @@ export default function useRowInfo<RecordType>(
onTriggerExpand: onInternalTriggerExpand,
rowSupportExpand,
expandable: mergedExpandable,
rowProps: {
...rowProps,
onClick,
},
};
}

0 comments on commit ba03688

Please sign in to comment.