Skip to content

Commit a3bba58

Browse files
4brotherTENCENT\fourbyliuafc163
authored
修复当自定义表格体功能(components.body)和表头分组功能(column.children)同时使用时,会出现表头和表体不对齐的问题 (#1003)
* 修复当自定义表格体功能(components.body)和表头分组功能(column.children)同时使用时,会出现表头和表体不对齐的问题 * Update tests/Table.spec.jsx --------- Co-authored-by: TENCENT\fourbyliu <[email protected]> Co-authored-by: afc163 <[email protected]>
1 parent ecbf099 commit a3bba58

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

src/Table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
564564
});
565565

566566
headerProps.colWidths = flattenColumns.map(({ width }, index) => {
567-
const colWidth = index === columns.length - 1 ? (width as number) - scrollbarSize : width;
567+
const colWidth = index === flattenColumns.length - 1 ? (width as number) - scrollbarSize : width;
568568
if (typeof colWidth === 'number' && !Number.isNaN(colWidth)) {
569569
return colWidth;
570570
}

tests/Table.spec.jsx

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Table, { INTERNAL_COL_DEFINE } from '../src';
55
import BodyRow from '../src/Body/BodyRow';
66
import Cell from '../src/Cell';
77
import { INTERNAL_HOOKS } from '../src/constant';
8+
import { VariableSizeGrid as Grid } from "react-window";
89

910
describe('Table.Basic', () => {
1011
const data = [
@@ -1216,4 +1217,116 @@ describe('Table.Basic', () => {
12161217

12171218
expect(wrapper.render()).toMatchSnapshot();
12181219
});
1220+
1221+
it('using both column children and component body simultaneously', () => {
1222+
const width = 150;
1223+
const noChildColLen = 4;
1224+
const ChildColLen = 4;
1225+
const buildChildDataIndex = (n) => `col${n}`;
1226+
const columns = Array.from({ length: noChildColLen }, (_, i) => ({
1227+
title: `第 ${i} 列`,
1228+
dataIndex: buildChildDataIndex(i),
1229+
width,
1230+
})).concat(Array.from({ length: ChildColLen }, (_, i) => ({
1231+
title: `第 ${i} 分组`,
1232+
dataIndex: `parentCol${i}`,
1233+
width: width * 2,
1234+
children: [
1235+
{
1236+
title: `第 ${noChildColLen + i} 列`,
1237+
dataIndex: buildChildDataIndex(noChildColLen + i),
1238+
width,
1239+
},
1240+
{
1241+
title: `第 ${noChildColLen + 1 + i} 列`,
1242+
dataIndex: buildChildDataIndex(noChildColLen + 1 + i),
1243+
width,
1244+
},
1245+
]
1246+
})));
1247+
const data = Array.from({ length: 10000 }, (_, r) => {
1248+
const colLen = noChildColLen + ChildColLen * 2;
1249+
const record = {};
1250+
for (let c = 0; c < colLen; c ++) {
1251+
record[buildChildDataIndex(c)] = `r${r}, c${c}`
1252+
}
1253+
return record;
1254+
})
1255+
const Demo = (props) => {
1256+
const gridRef = React.useRef();
1257+
const [connectObject] = React.useState(() => {
1258+
const obj = {};
1259+
Object.defineProperty(obj, "scrollLeft", {
1260+
get: () => {
1261+
if (gridRef.current) {
1262+
return gridRef.current?.state?.scrollLeft;
1263+
}
1264+
return null;
1265+
},
1266+
set: (scrollLeft) => {
1267+
if (gridRef.current) {
1268+
gridRef.current.scrollTo({ scrollLeft });
1269+
}
1270+
}
1271+
});
1272+
1273+
return obj;
1274+
});
1275+
1276+
React.useEffect(() => {
1277+
gridRef.current.resetAfterIndices({
1278+
columnIndex: 0,
1279+
shouldForceUpdate: false
1280+
});
1281+
}, []);
1282+
1283+
const renderVirtualList = (rawData, { scrollbarSize, ref, onScroll }) => {
1284+
ref.current = connectObject;
1285+
return (
1286+
<Grid
1287+
ref={gridRef}
1288+
className="virtual-grid"
1289+
columnCount={columns.length}
1290+
columnWidth={(index) => {
1291+
const { width } = columns[index];
1292+
return index === columns.length - 1
1293+
? width - scrollbarSize - 1
1294+
: width;
1295+
}}
1296+
height={300}
1297+
rowCount={rawData.length}
1298+
rowHeight={() => 50}
1299+
width={800}
1300+
onScroll={({ scrollLeft }) => {
1301+
onScroll({ scrollLeft });
1302+
}}
1303+
>
1304+
{({ columnIndex, rowIndex, style }) => (
1305+
<div
1306+
className={`virtual-cell ${columnIndex === columns.length - 1 ? 'virtual-cell-last' : ''}`}
1307+
style={style}
1308+
>
1309+
r{rowIndex}, c{columnIndex}
1310+
</div>
1311+
)}
1312+
</Grid>
1313+
);
1314+
};
1315+
1316+
return (
1317+
<Table
1318+
style={{ width: 800 }}
1319+
tableLayout="fixed"
1320+
columns={props.columns}
1321+
data={props.data}
1322+
scroll={{ y: 300, x: 300 }}
1323+
components={{
1324+
body: renderVirtualList
1325+
}}
1326+
/>
1327+
);
1328+
};
1329+
const wrapper = mount(<Demo columns={columns} data={data} />);
1330+
expect(wrapper.find('col').at(noChildColLen + ChildColLen * 2 - 1).props().style.width + wrapper.find('col').last().props().style.width).toEqual(width);
1331+
})
12191332
});

0 commit comments

Comments
 (0)