Skip to content

Commit b8afb63

Browse files
committed
chore: optimize code
1 parent b32e21c commit b8afb63

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

packages/components/table/BaseTable.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ const BaseTable = forwardRef<BaseTableRef, BaseTableProps>((originalProps, ref)
5454
const bottomContentRef = useRef<HTMLDivElement>(null);
5555
const [tableFootHeight, setTableFootHeight] = useState(0);
5656

57-
const allTableClasses = useClassName();
5857
const { classPrefix, virtualScrollClasses, tableLayoutClasses, tableBaseClass, tableColFixedClasses } =
59-
allTableClasses;
58+
useClassName();
6059
// 表格基础样式类
6160
const { tableClasses, sizeClassNames, tableContentStyles, tableElementStyles } = useStyle(props);
6261
const { isMultipleHeader, spansAndLeafNodes, thList } = useTableHeader({ columns: props.columns });
@@ -489,7 +488,6 @@ const BaseTable = forwardRef<BaseTableRef, BaseTableProps>((originalProps, ref)
489488
tableContentRef,
490489
tableWidth,
491490
isWidthOverflow,
492-
allTableClasses,
493491
rowKey,
494492
scroll: props.scroll,
495493
cellEmptyContent: props.cellEmptyContent,
@@ -537,7 +535,6 @@ const BaseTable = forwardRef<BaseTableRef, BaseTableProps>((originalProps, ref)
537535
),
538536
// eslint-disable-next-line
539537
[
540-
allTableClasses,
541538
tableBodyProps.ellipsisOverlayClassName,
542539
tableBodyProps.rowAndColFixedPosition,
543540
tableBodyProps.showColumnShadow,

packages/components/table/Cell.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import React, { MouseEvent, MutableRefObject, ReactNode } from 'react';
22
import classNames from 'classnames';
3-
import { isFunction, get } from 'lodash-es';
3+
import { get, isFunction } from 'lodash-es';
4+
45
import TEllipsis from './Ellipsis';
5-
import { BaseTableCellParams, RowspanColspan, TableRowData, TdBaseTableProps } from './type';
6-
import { RowAndColFixedPosition } from './interface';
6+
import useClassName from './hooks/useClassName';
77
import { getColumnFixedStyles } from './hooks/useFixed';
8-
import { TableClassName } from './hooks/useClassName';
98
import { formatClassNames } from './utils';
10-
import { TooltipProps } from '../tooltip';
11-
import { PaginationProps } from '../pagination';
9+
10+
import type { PaginationProps } from '../pagination';
11+
import type { TooltipProps } from '../tooltip';
12+
import type { RowAndColFixedPosition } from './interface';
13+
import type { BaseTableCellParams, RowspanColspan, TableRowData, TdBaseTableProps } from './type';
1214

1315
export interface RenderEllipsisCellParams {
1416
cellNode: ReactNode;
@@ -25,7 +27,6 @@ export interface CellProps {
2527
dataLength: number;
2628
cellSpans: RowspanColspan;
2729
cellEmptyContent: TdBaseTableProps['cellEmptyContent'];
28-
tableClassNames: TableClassName;
2930
tableRef?: MutableRefObject<HTMLDivElement>;
3031
classPrefix?: string;
3132
overlayClassName?: string;
@@ -94,11 +95,11 @@ function renderEllipsisCell(cellParams: BaseTableCellParams<TableRowData>, param
9495
}
9596

9697
const Cell = (props: CellProps) => {
97-
const { cellParams, tableClassNames, tableRef, columnLength, classPrefix, overlayClassName, pagination } = props;
98+
const { cellParams, tableRef, columnLength, classPrefix, overlayClassName, pagination } = props;
9899
const { col, colIndex, rowIndex } = cellParams;
99100
const { cellSpans, dataLength, rowAndColFixedPosition, cellEmptyContent, rowspanAndColspan, onClick } = props;
100101
const { tableColFixedClasses, tdEllipsisClass, tableBaseClass, tdAlignClasses, tableDraggableClasses } =
101-
tableClassNames;
102+
useClassName();
102103

103104
const cellNode = renderCell(cellParams, { cellEmptyContent, pagination });
104105
const tdStyles = getColumnFixedStyles(col, colIndex, rowAndColFixedPosition, tableColFixedClasses);

packages/components/table/TBody.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import React, { type CSSProperties, type MutableRefObject, type ReactNode, useMemo } from 'react';
33
import classNames from 'classnames';
44
import { camelCase, get, pick } from 'lodash-es';
5+
56
import { useLocaleReceiver } from '../locale/LocalReceiver';
6-
import { TableClassName } from './hooks/useClassName';
7+
import useClassName from './hooks/useClassName';
78
import useRowspanAndColspan from './hooks/useRowspanAndColspan';
89
import TR, { ROW_LISTENERS, TABLE_PROPS, type TrProps } from './TR';
910

@@ -25,7 +26,6 @@ export interface TableBodyProps extends BaseTableProps {
2526
isWidthOverflow?: boolean;
2627
virtualConfig: VirtualScrollConfig;
2728
pagination?: PaginationProps;
28-
allTableClasses?: TableClassName;
2929
handleRowMounted?: (params: RowMountedParams) => void;
3030
}
3131

@@ -74,7 +74,7 @@ const trProperties = [
7474

7575
export default function TBody(props: TableBodyProps) {
7676
// 如果不是变量复用,没必要对每一个参数进行解构(解构过程需要单独的内存空间存储临时变量)
77-
const { data, columns, rowKey, firstFullRow, lastFullRow, virtualConfig, allTableClasses } = props;
77+
const { data, columns, rowKey, firstFullRow, lastFullRow, virtualConfig } = props;
7878

7979
const { isVirtualScroll } = virtualConfig;
8080
const renderData = isVirtualScroll ? virtualConfig.visibleData : data;
@@ -84,7 +84,7 @@ export default function TBody(props: TableBodyProps) {
8484
const { skipSpansMap } = useRowspanAndColspan(data, columns, rowKey, props.rowspanAndColspan);
8585
const isSkipSnapsMapNotFinish = Boolean(props.rowspanAndColspan && !skipSpansMap.size);
8686

87-
const { tableFullRowClasses, tableBaseClass } = allTableClasses;
87+
const { tableFullRowClasses, tableBaseClass } = useClassName();
8888
const tbodyClasses = useMemo(() => [tableBaseClass.body], [tableBaseClass.body]);
8989
const hasFullRowConfig = useMemo(() => firstFullRow || lastFullRow, [firstFullRow, lastFullRow]);
9090

@@ -217,7 +217,7 @@ export default function TBody(props: TableBodyProps) {
217217

218218
// 垫上隐藏的 tr 元素高度
219219
const translate = `translateY(${virtualConfig.translateY}px)`;
220-
const posStyle: CSSProperties = isVirtualScroll
220+
const posStyle = isVirtualScroll
221221
? ({
222222
transform: translate,
223223
msTransform: translate,

packages/components/table/TR.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default function TR(props: TrProps) {
8787
const classNames = useClassName();
8888

8989
const trStyles = getRowFixedStyles(
90-
get(row, rowKey || 'id'),
90+
get(row, rowKey),
9191
rowIndex,
9292
dataLength,
9393
fixedRows,
@@ -102,7 +102,7 @@ export default function TR(props: TrProps) {
102102
);
103103

104104
const classes = useMemo(() => {
105-
const customClasses = formatRowClassNames(rowClassName, { row, rowIndex, rowKey, type: 'body' }, rowKey || 'id');
105+
const customClasses = formatRowClassNames(rowClassName, { row, rowIndex, rowKey, type: 'body' }, rowKey);
106106
return [trStyles?.classes, customClasses];
107107
}, [row, rowClassName, rowIndex, rowKey, trStyles?.classes]);
108108

@@ -151,7 +151,6 @@ export default function TR(props: TrProps) {
151151
columnLength={props.columns.length}
152152
cellSpans={cellSpans}
153153
cellEmptyContent={props.cellEmptyContent}
154-
tableClassNames={classNames}
155154
rowspanAndColspan={props.rowspanAndColspan}
156155
onClick={onClick}
157156
tableRef={props.tableRef}

0 commit comments

Comments
 (0)