From 655813ce68f651253c8239c25bf055ad2585e902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E8=89=B3=E5=85=B5?= Date: Mon, 28 Sep 2026 10:36:32 +0800 Subject: [PATCH] fix: preserve zero-valued column titles --- src/Body/MeasureCell.tsx | 6 ++++-- src/Header/HeaderRow.tsx | 6 ++++-- tests/renderability.spec.tsx | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 tests/renderability.spec.tsx diff --git a/src/Body/MeasureCell.tsx b/src/Body/MeasureCell.tsx index 275e26d94..a1289f011 100644 --- a/src/Body/MeasureCell.tsx +++ b/src/Body/MeasureCell.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; import ResizeObserver from '@rc-component/resize-observer'; -import { useLayoutEffect } from '@rc-component/util'; +import { isReactRenderable, useLayoutEffect } from '@rc-component/util'; export interface MeasureCellProps { columnKey: React.Key; @@ -25,7 +25,9 @@ const MeasureCell: React.FC = props => { ref={cellRef} style={{ paddingTop: 0, paddingBottom: 0, borderTop: 0, borderBottom: 0, height: 0 }} > -
{title || '\xa0'}
+
+ {isReactRenderable(title) ? title : '\xa0'} +
); diff --git a/src/Header/HeaderRow.tsx b/src/Header/HeaderRow.tsx index 9a84c7ba2..6e4e77abc 100644 --- a/src/Header/HeaderRow.tsx +++ b/src/Header/HeaderRow.tsx @@ -1,4 +1,5 @@ import * as React from 'react'; +import { isReactRenderable } from '@rc-component/util'; import Cell from '../Cell'; import TableContext from '../context/TableContext'; import { useContext } from '@rc-component/context'; @@ -54,12 +55,13 @@ const HeaderRow = (props: RowProps) => { const { column, colStart, colEnd, colSpan } = cell; const fixedInfo = getCellFixedInfo(colStart, colEnd, flattenColumns, stickyOffsets); - const additionalProps: React.HTMLAttributes = column?.onHeaderCell?.(column) || {}; + const additionalProps: React.HTMLAttributes = + column?.onHeaderCell?.(column) || {}; return ( 1 ? 'colgroup' : 'col') : null} + scope={isReactRenderable(column.title) ? (colSpan > 1 ? 'colgroup' : 'col') : null} ellipsis={column.ellipsis} align={column.align} component={CellComponent} diff --git a/tests/renderability.spec.tsx b/tests/renderability.spec.tsx new file mode 100644 index 000000000..59d078d07 --- /dev/null +++ b/tests/renderability.spec.tsx @@ -0,0 +1,38 @@ +import React from 'react'; +import { render } from '@testing-library/react'; +import Table from '../src'; +import MeasureCell from '../src/Body/MeasureCell'; + +it('sets the scope for zero column and group titles', () => { + const { container } = render( + , + ); + const headers = container.querySelectorAll('th'); + expect(headers[0]).toHaveAttribute('scope', 'colgroup'); + expect(headers[1]).toHaveAttribute('scope', 'col'); + expect(headers[1].textContent).toBe('0'); +}); + +it('measures the zero title rather than a blank fallback', () => { + const { container } = render( +
+ + + {}} /> + + +
, + ); + expect(container.querySelector('td').textContent).toBe('0'); +});