Skip to content

Commit

Permalink
refactor: move rowClass in
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Aug 20, 2023
1 parent ba03688 commit c9a7126
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
3 changes: 2 additions & 1 deletion docs/examples/virtual.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ const Demo = () => {
expandedRowRender: () => 2333,
columnWidth: 60,
}}
onRow={() => ({ className: 'rowed' })}
// onRow={() => ({ className: 'rowed' })}
rowClassName="nice-try"
/>
</div>
);
Expand Down
11 changes: 1 addition & 10 deletions src/Body/BodyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
cellComponent,
scopeCellComponent,
} = props;
const rowInfo = useRowInfo(record, rowKey, index);
const rowInfo = useRowInfo(record, rowKey, index, indent);
const {
prefixCls,
flattenColumns,
rowClassName,
expandedRowClassName,
expandedRowRender,
rowProps,
Expand All @@ -130,13 +129,6 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
}, [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 = (
<RowComponent
{...rowProps}
Expand All @@ -145,7 +137,6 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
className,
`${prefixCls}-row`,
`${prefixCls}-row-level-${indent}`,
computeRowClassName,
rowProps?.className,
)}
style={{
Expand Down
9 changes: 5 additions & 4 deletions src/StaticTable/BodyLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((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) {
Expand All @@ -55,12 +55,13 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>

const rowNode = (
<div
{...rowProps}
{...restProps}
ref={rowSupportExpand ? null : ref}
className={classNames(className, `${prefixCls}-row`, {
className={classNames(className, `${prefixCls}-row`, rowProps?.className, {
[`${prefixCls}-row-extra`]: extra,
})}
style={rowStyle}
style={{ ...rowStyle, ...rowProps?.style }}
>
{flattenColumns.map((column, colIndex) => {
return (
Expand Down
12 changes: 12 additions & 0 deletions src/hooks/useRowInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecordType>(
record: RecordType,
rowKey: React.Key,
recordIndex: number,
indent: number,
): Pick<
TableContextProps,
| 'prefixCls'
Expand Down Expand Up @@ -63,6 +65,7 @@ export default function useRowInfo<RecordType>(
rowExpandable,
onRow,
expandRowByClick,
rowClassName,
} = context;

// ======================= Expandable =======================
Expand Down Expand Up @@ -90,6 +93,14 @@ export default function useRowInfo<RecordType>(
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);

Expand All @@ -105,6 +116,7 @@ export default function useRowInfo<RecordType>(
expandable: mergedExpandable,
rowProps: {
...rowProps,
className: classNames(computeRowClassName, rowProps?.className),
onClick,
},
};
Expand Down

0 comments on commit c9a7126

Please sign in to comment.