Skip to content

Commit f9e7c88

Browse files
authored
Add header table (#1074)
* feat: add header table * feat: add header table * feat: test * feat: add createTable * feat: add data * feat: test * feat: 精简代码
1 parent 90458be commit f9e7c88

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

docs/demo/components.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: components
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/components.tsx"></code>

docs/examples/components.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import Table from 'rc-table';
3+
4+
const data = [];
5+
for (let i = 0; i < 100; i += 1) {
6+
data.push({
7+
key: i,
8+
a: `a${i}`,
9+
b: `b${i}`,
10+
c: `c${i}`,
11+
});
12+
}
13+
14+
const table = (props: any) => {
15+
return (
16+
<>
17+
<div style={{ background: '#fff' }}>header table</div>
18+
<table className={props.className}>{props.children}</table>
19+
</>
20+
);
21+
};
22+
23+
const Demo = () => {
24+
return (
25+
<div>
26+
<Table
27+
components={{ header: { table } }}
28+
sticky
29+
columns={[
30+
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
31+
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
32+
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
33+
]}
34+
data={data}
35+
/>
36+
</div>
37+
);
38+
};
39+
40+
export default Demo;

src/FixedHolder/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<unknown>>(
6363
...restProps
6464
} = props;
6565

66-
const { prefixCls, scrollbarSize, isSticky } = useContext(TableContext, [
66+
const { prefixCls, scrollbarSize, isSticky, getComponent } = useContext(TableContext, [
6767
'prefixCls',
6868
'scrollbarSize',
6969
'isSticky',
70+
'getComponent',
7071
]);
72+
const TableComponent = getComponent(['header', 'table'], 'table');
7173

7274
const combinationScrollBarSize = isSticky && !fixHeader ? 0 : scrollbarSize;
7375

@@ -146,7 +148,7 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<unknown>>(
146148
[stickyClassName]: !!stickyClassName,
147149
})}
148150
>
149-
<table
151+
<TableComponent
150152
style={{
151153
tableLayout: 'fixed',
152154
visibility: noData || mergedColumnWidth ? null : 'hidden',
@@ -165,7 +167,7 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<unknown>>(
165167
columns: columnsWithScrollbar,
166168
flattenColumns: flattenColumnsWithScrollbar,
167169
})}
168-
</table>
170+
</TableComponent>
169171
</div>
170172
);
171173
});

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export type CustomizeScrollBody<RecordType> = (
155155
export interface TableComponents<RecordType> {
156156
table?: CustomizeComponent;
157157
header?: {
158+
table?: CustomizeComponent;
158159
wrapper?: CustomizeComponent;
159160
row?: CustomizeComponent;
160161
cell?: CustomizeComponent;

tests/FixedColumn.spec.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,18 @@ describe('Table.FixedColumn', () => {
284284
});
285285
});
286286
});
287+
describe('components.table by sticky', () => {
288+
it('render', async () => {
289+
const table = props => {
290+
return (
291+
<>
292+
<div className="healer-table">header table</div>
293+
<table className={props.className}>{props.children}</table>
294+
</>
295+
);
296+
};
297+
const { container } = render(<Table sticky components={{ header: { table } }} />);
298+
expect(container.querySelector('.healer-table')).toBeTruthy();
299+
});
300+
});
287301
});

0 commit comments

Comments
 (0)