From c9a7126b31271c37c3bc2b817d11d89c0fbf82ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Sun, 20 Aug 2023 23:37:50 +0800 Subject: [PATCH] refactor: move rowClass in --- docs/examples/virtual.tsx | 3 ++- src/Body/BodyRow.tsx | 11 +---------- src/StaticTable/BodyLine.tsx | 9 +++++---- src/hooks/useRowInfo.tsx | 12 ++++++++++++ 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/docs/examples/virtual.tsx b/docs/examples/virtual.tsx index 8338c313c..c3d165e6a 100644 --- a/docs/examples/virtual.tsx +++ b/docs/examples/virtual.tsx @@ -205,7 +205,8 @@ const Demo = () => { expandedRowRender: () => 2333, columnWidth: 60, }} - onRow={() => ({ className: 'rowed' })} + // onRow={() => ({ className: 'rowed' })} + rowClassName="nice-try" /> ); diff --git a/src/Body/BodyRow.tsx b/src/Body/BodyRow.tsx index 623cafb4c..baf6f9e5f 100644 --- a/src/Body/BodyRow.tsx +++ b/src/Body/BodyRow.tsx @@ -103,11 +103,10 @@ function BodyRow( cellComponent, scopeCellComponent, } = props; - const rowInfo = useRowInfo(record, rowKey, index); + const rowInfo = useRowInfo(record, rowKey, index, indent); const { prefixCls, flattenColumns, - rowClassName, expandedRowClassName, expandedRowRender, rowProps, @@ -130,13 +129,6 @@ function BodyRow( }, [expanded]); // ======================== Base tr row ======================== - let computeRowClassName: string; - if (typeof rowClassName === 'string') { - computeRowClassName = rowClassName; - } else if (typeof rowClassName === 'function') { - computeRowClassName = rowClassName(record, index, indent); - } - const baseRowNode = ( ( className, `${prefixCls}-row`, `${prefixCls}-row-level-${indent}`, - computeRowClassName, rowProps?.className, )} style={{ diff --git a/src/StaticTable/BodyLine.tsx b/src/StaticTable/BodyLine.tsx index 490f39165..90028926f 100644 --- a/src/StaticTable/BodyLine.tsx +++ b/src/StaticTable/BodyLine.tsx @@ -31,10 +31,10 @@ const BodyLine = React.forwardRef((props, ref) => ]); const { scrollX } = useContext(StaticContext, ['scrollX']); - const rowInfo = useRowInfo(record, rowKey, index); + const rowInfo = useRowInfo(record, rowKey, index, indent); // ========================== Expand ========================== - const { rowSupportExpand, expanded } = rowInfo; + const { rowSupportExpand, expanded, rowProps } = rowInfo; let expandRowNode: React.ReactElement; if (rowSupportExpand && expanded) { @@ -55,12 +55,13 @@ const BodyLine = React.forwardRef((props, ref) => const rowNode = (
{flattenColumns.map((column, colIndex) => { return ( diff --git a/src/hooks/useRowInfo.tsx b/src/hooks/useRowInfo.tsx index ed2015f8a..bba123a89 100644 --- a/src/hooks/useRowInfo.tsx +++ b/src/hooks/useRowInfo.tsx @@ -3,11 +3,13 @@ import type { TableContextProps } from '../context/TableContext'; import TableContext from '../context/TableContext'; import { getColumnsKey } from '../utils/valueUtil'; import { useEvent } from 'rc-util'; +import classNames from 'classnames'; export default function useRowInfo( record: RecordType, rowKey: React.Key, recordIndex: number, + indent: number, ): Pick< TableContextProps, | 'prefixCls' @@ -63,6 +65,7 @@ export default function useRowInfo( rowExpandable, onRow, expandRowByClick, + rowClassName, } = context; // ======================= Expandable ======================= @@ -90,6 +93,14 @@ export default function useRowInfo( onRowClick?.(event, ...args); }; + // ====================== RowClassName ====================== + let computeRowClassName: string; + if (typeof rowClassName === 'string') { + computeRowClassName = rowClassName; + } else if (typeof rowClassName === 'function') { + computeRowClassName = rowClassName(record, recordIndex, indent); + } + // ========================= Column ========================= const columnsKey = getColumnsKey(flattenColumns); @@ -105,6 +116,7 @@ export default function useRowInfo( expandable: mergedExpandable, rowProps: { ...rowProps, + className: classNames(computeRowClassName, rowProps?.className), onClick, }, };