Skip to content

Commit 931ed44

Browse files
committed
opt
1 parent ebfaa66 commit 931ed44

7 files changed

Lines changed: 38 additions & 36 deletions

File tree

src/Body/BodyRow.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
9393
if (process.env.NODE_ENV !== 'production') {
9494
devRenderTimes(props);
9595
}
96+
9697
const {
9798
className,
9899
style,
@@ -143,15 +144,15 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
143144
`${prefixCls}-row`,
144145
`${prefixCls}-row-level-${indent}`,
145146
rowProps?.className,
146-
classNames?.row,
147+
classNames.row,
147148
{
148149
[expandedClsName]: indent >= 1,
149150
},
150151
)}
151152
style={{
152153
...style,
153154
...rowProps?.style,
154-
...styles?.row,
155+
...styles.row,
155156
}}
156157
>
157158
{flattenColumns.map((column: ColumnType<RecordType>, colIndex) => {
@@ -167,8 +168,8 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
167168

168169
return (
169170
<Cell<RecordType>
170-
className={cls(columnClassName, classNames?.cell)}
171-
style={styles?.cell}
171+
className={cls(columnClassName, classNames.cell)}
172+
style={styles.cell}
172173
ellipsis={column.ellipsis}
173174
align={column.align}
174175
scope={column.rowScope}

src/Body/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
4646
'classNames',
4747
'styles',
4848
]);
49-
const { body: bodyCls } = classNames || {};
50-
const { body: bodyStyles } = styles || {};
49+
const { body: bodyCls = {} } = classNames || {};
50+
const { body: bodyStyles = {} } = styles || {};
5151

5252
const flattenData: { record: RecordType; indent: number; index: number }[] =
5353
useFlattenRecords<RecordType>(data, childrenColumnName, expandedKeys, getRowKey);
@@ -107,8 +107,8 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
107107
return (
108108
<PerfContext.Provider value={perfRef.current}>
109109
<WrapperComponent
110-
className={cls(`${prefixCls}-tbody`, bodyCls?.wrapper)}
111-
style={bodyStyles?.wrapper}
110+
className={cls(`${prefixCls}-tbody`, bodyCls.wrapper)}
111+
style={bodyStyles.wrapper}
112112
>
113113
{/* Measure body column width with additional hidden col */}
114114
{measureColumnWidth && (

src/Cell/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ function Cell<RecordType>(props: CellProps<RecordType>) {
125125

126126
const cellPrefixCls = `${prefixCls}-cell`;
127127

128-
const { allColumnsFixedLeft, rowHoverable, classNames, styles } = useContext(TableContext, [
129-
'allColumnsFixedLeft',
130-
'rowHoverable',
131-
'classNames',
132-
'styles',
133-
]);
128+
const {
129+
allColumnsFixedLeft,
130+
rowHoverable,
131+
classNames = {},
132+
styles = {},
133+
} = useContext(TableContext, ['allColumnsFixedLeft', 'rowHoverable', 'classNames', 'styles']);
134134

135135
// ====================== Value =======================
136136
const [childNode, legacyCellProps] = useCellRender(
@@ -219,7 +219,7 @@ function Cell<RecordType>(props: CellProps<RecordType>) {
219219
// >>>>> ClassName
220220
const mergedClassName = cls(
221221
cellPrefixCls,
222-
classNames?.item,
222+
classNames.cell,
223223
className,
224224
{
225225
// Fixed
@@ -255,7 +255,7 @@ function Cell<RecordType>(props: CellProps<RecordType>) {
255255
...fixedStyle,
256256
...alignStyle,
257257
...additionalProps.style,
258-
...styles?.item,
258+
...styles.cell,
259259
...style,
260260
};
261261

src/Header/Header.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function parseHeaderRows<RecordType>(
3333
const colSpans: number[] = columns.filter(Boolean).map(column => {
3434
const cell: CellType<RecordType> = {
3535
key: column.key,
36-
className: cls(column.className, classNames?.cell) || '',
37-
style: styles?.cell,
36+
className: cls(column.className, classNames.cell) || '',
37+
style: styles.cell,
3838
children: column.title,
3939
column,
4040
colStart: currentColIndex,
@@ -108,8 +108,8 @@ const Header = <RecordType extends any>(props: HeaderProps<RecordType>) => {
108108
'classNames',
109109
'styles',
110110
]);
111-
const { header: headerCls } = classNames || {};
112-
const { header: headerStyles } = styles || {};
111+
const { header: headerCls = {} } = classNames || {};
112+
const { header: headerStyles = {} } = styles || {};
113113
const rows = React.useMemo<CellType<RecordType>[][]>(
114114
() => parseHeaderRows(columns, headerCls, headerStyles),
115115
[columns, headerCls, headerStyles],
@@ -121,12 +121,14 @@ const Header = <RecordType extends any>(props: HeaderProps<RecordType>) => {
121121

122122
return (
123123
<WrapperComponent
124-
className={cls(`${prefixCls}-thead`, headerCls?.wrapper)}
125-
style={headerStyles?.wrapper}
124+
className={cls(`${prefixCls}-thead`, headerCls.wrapper)}
125+
style={headerStyles.wrapper}
126126
>
127127
{rows.map((row, rowIndex) => {
128128
const rowNode = (
129129
<HeaderRow
130+
classNames={headerCls}
131+
styles={headerStyles}
130132
key={rowIndex}
131133
flattenColumns={flattenColumns}
132134
cells={row}

src/Header/HeaderRow.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
} from '../interface';
1212
import { getCellFixedInfo } from '../utils/fixUtil';
1313
import { getColumnsKey } from '../utils/valueUtil';
14+
import { TableProps } from '..';
1415

1516
export interface RowProps<RecordType> {
1617
cells: readonly CellType<RecordType>[];
@@ -20,6 +21,8 @@ export interface RowProps<RecordType> {
2021
cellComponent: CustomizeComponent;
2122
onHeaderRow: GetComponentProps<readonly ColumnType<RecordType>[]>;
2223
index: number;
24+
classNames?: TableProps['classNames']['header'];
25+
styles?: TableProps['styles']['header'];
2326
}
2427

2528
const HeaderRow = <RecordType extends any>(props: RowProps<RecordType>) => {
@@ -31,14 +34,10 @@ const HeaderRow = <RecordType extends any>(props: RowProps<RecordType>) => {
3134
cellComponent: CellComponent,
3235
onHeaderRow,
3336
index,
37+
classNames,
38+
styles,
3439
} = props;
35-
const { prefixCls, styles, classNames } = useContext(TableContext, [
36-
'prefixCls',
37-
'styles',
38-
'classNames',
39-
]);
40-
const { header: headerCls } = classNames || {};
41-
const { header: headerStyles } = styles || {};
40+
const { prefixCls } = useContext(TableContext, ['prefixCls']);
4241
let rowProps: React.HTMLAttributes<HTMLElement>;
4342
if (onHeaderRow) {
4443
rowProps = onHeaderRow(
@@ -50,7 +49,7 @@ const HeaderRow = <RecordType extends any>(props: RowProps<RecordType>) => {
5049
const columnsKey = getColumnsKey(cells.map(cell => cell.column));
5150

5251
return (
53-
<RowComponent {...rowProps} className={headerCls?.row} style={headerStyles?.row}>
52+
<RowComponent {...rowProps} className={classNames.row} style={styles.row}>
5453
{cells.map((cell: CellType<RecordType>, cellIndex) => {
5554
const { column } = cell;
5655
const fixedInfo = getCellFixedInfo(

src/Table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const EMPTY_DATA = [];
8585
// Used for customize scroll
8686
const EMPTY_SCROLL_TARGET = {};
8787

88-
export type SemanticName = 'section' | 'title' | 'footer' | 'content' | 'item';
88+
export type SemanticName = 'section' | 'title' | 'footer' | 'content' | 'cell';
8989
export type ComponentsSemantic = 'wrapper' | 'cell' | 'row';
9090
export interface TableProps<RecordType = any>
9191
extends Omit<LegacyExpandableProps<RecordType>, 'showExpandColumn'> {

tests/semantic.spec.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('support classNames and styles', () => {
5555
title: 'test-title',
5656
footer: 'test-footer',
5757
content: 'test-content',
58-
item: 'test-item',
58+
cell: 'test-cell',
5959
body: {
6060
wrapper: 'test-body-wrapper',
6161
cell: 'test-body-cell',
@@ -72,7 +72,7 @@ describe('support classNames and styles', () => {
7272
title: { background: 'green' },
7373
footer: { background: 'pink' },
7474
content: { background: 'purple' },
75-
item: { fontSize: '19px' },
75+
cell: { fontSize: '19px' },
7676
body: {
7777
wrapper: { background: 'cyan' },
7878
cell: { background: 'lime' },
@@ -91,7 +91,7 @@ describe('support classNames and styles', () => {
9191
const title = container.querySelector('.rc-table-title');
9292
const footer = container.querySelector('.rc-table-footer');
9393
const content = container.querySelector('.rc-table-content');
94-
const item = container.querySelector('.rc-table-cell');
94+
const cell = container.querySelector('.rc-table-cell');
9595
const headerWrapper = container.querySelector('.rc-table-thead');
9696
const headerCell = container.querySelector('.rc-table-cell');
9797
const headerRow = container.querySelector('tr');
@@ -106,8 +106,8 @@ describe('support classNames and styles', () => {
106106
expect(footer).toHaveStyle(testStyles.footer);
107107
expect(content).toHaveClass(testClassNames.content);
108108
expect(content).toHaveStyle(testStyles.content);
109-
expect(item).toHaveClass(testClassNames.item);
110-
expect(item).toHaveStyle({ fontSize: testStyles.item.fontSize });
109+
expect(cell).toHaveClass(testClassNames.cell);
110+
expect(cell).toHaveStyle({ fontSize: testStyles.cell.fontSize });
111111

112112
expect(headerWrapper).toHaveClass(testClassNames.header.wrapper);
113113
expect(headerWrapper).toHaveStyle(testStyles.header.wrapper);

0 commit comments

Comments
 (0)