Skip to content

Commit 4beffa5

Browse files
committed
chore: Code Optimization
1 parent fa5f1db commit 4beffa5

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

src/Body/BodyRow.tsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import devRenderTimes from '../hooks/useRenderTimes';
66
import useRowInfo from '../hooks/useRowInfo';
77
import type { ColumnType, CustomizeComponent, GetRowKey } from '../interface';
88
import ExpandedRow from './ExpandedRow';
9+
import { computedExpandedClassName } from '@/utils/expandUtil';
910

1011
export interface BodyRowProps<RecordType> {
1112
record: RecordType;
@@ -126,15 +127,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
126127

127128
// 若没有 expandedRowRender 参数, 将使用 baseRowNode 渲染 Children
128129
// 此时如果 level > 1 则说明是 expandedRow, 一样需要附加 computedExpandedRowClassName
129-
const computedExpandedRowClassName = React.useMemo<string>(() => {
130-
if (typeof expandedRowClassName === 'string') {
131-
return expandedRowClassName;
132-
}
133-
if (typeof expandedRowClassName === 'function') {
134-
return expandedRowClassName(record, index, indent);
135-
}
136-
return '';
137-
}, [expandedRowClassName, record, index, indent]);
130+
const expandedClsName = computedExpandedClassName(expandedRowClassName, record, index, indent);
138131

139132
// ======================== Base tr row ========================
140133
const baseRowNode = (
@@ -147,13 +140,10 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
147140
`${prefixCls}-row-level-${indent}`,
148141
rowProps?.className,
149142
{
150-
[computedExpandedRowClassName]: indent >= 1,
143+
[expandedClsName]: indent >= 1,
151144
},
152145
)}
153-
style={{
154-
...style,
155-
...rowProps?.style,
156-
}}
146+
style={{ ...style, ...rowProps?.style }}
157147
>
158148
{flattenColumns.map((column: ColumnType<RecordType>, colIndex) => {
159149
const { render, dataIndex, className: columnClassName } = column;
@@ -201,7 +191,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
201191
className={classNames(
202192
`${prefixCls}-expanded-row`,
203193
`${prefixCls}-expanded-row-level-${indent + 1}`,
204-
computedExpandedRowClassName,
194+
expandedClsName,
205195
)}
206196
prefixCls={prefixCls}
207197
component={RowComponent}

src/VirtualTable/BodyLine.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { FlattenData } from '../hooks/useFlattenRecords';
77
import useRowInfo from '../hooks/useRowInfo';
88
import VirtualCell from './VirtualCell';
99
import { StaticContext } from './context';
10+
import { computedExpandedClassName } from '@/utils/expandUtil';
1011

1112
export interface BodyLineProps<RecordType = any> {
1213
data: FlattenData<RecordType>;
@@ -42,14 +43,7 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
4243
if (rowSupportExpand && expanded) {
4344
const expandContent = expandedRowRender(record, index, indent + 1, expanded);
4445

45-
let computedExpandedRowClassName = '';
46-
47-
if (typeof expandedRowClassName === 'string') {
48-
computedExpandedRowClassName = expandedRowClassName;
49-
}
50-
if (typeof expandedRowClassName === 'function') {
51-
computedExpandedRowClassName = expandedRowClassName(record, index, indent);
52-
}
46+
const expandedClsName = computedExpandedClassName(expandedRowClassName, record, index, indent);
5347

5448
let additionalProps: React.TdHTMLAttributes<HTMLElement> = {};
5549
if (fixColumn) {
@@ -67,7 +61,7 @@ const BodyLine = React.forwardRef<HTMLDivElement, BodyLineProps>((props, ref) =>
6761
className={classNames(
6862
`${prefixCls}-expanded-row`,
6963
`${prefixCls}-expanded-row-level-${indent + 1}`,
70-
computedExpandedRowClassName,
64+
expandedClsName,
7165
)}
7266
>
7367
<Cell

src/utils/expandUtil.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import classNames from 'classnames';
3-
import type { RenderExpandIconProps, Key, GetRowKey } from '../interface';
3+
import type { RenderExpandIconProps, Key, GetRowKey, ExpandableConfig } from '../interface';
44

55
export function renderExpandIcon<RecordType>({
66
prefixCls,
@@ -50,3 +50,19 @@ export function findAllChildrenKeys<RecordType>(
5050

5151
return keys;
5252
}
53+
54+
export function computedExpandedClassName<RecordType>(
55+
cls: ExpandableConfig<RecordType>['expandedRowClassName'],
56+
record: RecordType,
57+
index: number,
58+
indent: number,
59+
) {
60+
let resultClsName = '';
61+
if (typeof cls === 'string') {
62+
resultClsName = cls;
63+
}
64+
if (typeof cls === 'function') {
65+
resultClsName = cls(record, index, indent);
66+
}
67+
return resultClsName;
68+
}

0 commit comments

Comments
 (0)