Skip to content

Commit

Permalink
Add header table (#1074)
Browse files Browse the repository at this point in the history
* feat: add header table

* feat: add header table

* feat: test

* feat: add createTable

* feat: add data

* feat: test

* feat: 精简代码
  • Loading branch information
crazyair authored Feb 2, 2024
1 parent 90458be commit f9e7c88
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/demo/components.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: components
nav:
title: Demo
path: /demo
---

<code src="../examples/components.tsx"></code>
40 changes: 40 additions & 0 deletions docs/examples/components.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import Table from 'rc-table';

const data = [];
for (let i = 0; i < 100; i += 1) {
data.push({
key: i,
a: `a${i}`,
b: `b${i}`,
c: `c${i}`,
});
}

const table = (props: any) => {
return (
<>
<div style={{ background: '#fff' }}>header table</div>
<table className={props.className}>{props.children}</table>
</>
);
};

const Demo = () => {
return (
<div>
<Table
components={{ header: { table } }}
sticky
columns={[
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
]}
data={data}
/>
</div>
);
};

export default Demo;
8 changes: 5 additions & 3 deletions src/FixedHolder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<unknown>>(
...restProps
} = props;

const { prefixCls, scrollbarSize, isSticky } = useContext(TableContext, [
const { prefixCls, scrollbarSize, isSticky, getComponent } = useContext(TableContext, [
'prefixCls',
'scrollbarSize',
'isSticky',
'getComponent',
]);
const TableComponent = getComponent(['header', 'table'], 'table');

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

Expand Down Expand Up @@ -146,7 +148,7 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<unknown>>(
[stickyClassName]: !!stickyClassName,
})}
>
<table
<TableComponent
style={{
tableLayout: 'fixed',
visibility: noData || mergedColumnWidth ? null : 'hidden',
Expand All @@ -165,7 +167,7 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<unknown>>(
columns: columnsWithScrollbar,
flattenColumns: flattenColumnsWithScrollbar,
})}
</table>
</TableComponent>
</div>
);
});
Expand Down
1 change: 1 addition & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export type CustomizeScrollBody<RecordType> = (
export interface TableComponents<RecordType> {
table?: CustomizeComponent;
header?: {
table?: CustomizeComponent;
wrapper?: CustomizeComponent;
row?: CustomizeComponent;
cell?: CustomizeComponent;
Expand Down
14 changes: 14 additions & 0 deletions tests/FixedColumn.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,18 @@ describe('Table.FixedColumn', () => {
});
});
});
describe('components.table by sticky', () => {
it('render', async () => {
const table = props => {
return (
<>
<div className="healer-table">header table</div>
<table className={props.className}>{props.children}</table>
</>
);
};
const { container } = render(<Table sticky components={{ header: { table } }} />);
expect(container.querySelector('.healer-table')).toBeTruthy();
});
});
});

0 comments on commit f9e7c88

Please sign in to comment.