Skip to content

Commit 76f808d

Browse files
authored
fix(contentlayout): scroll calculation (#458)
* fix(contentlayout): use props scroll when isExist and calc scroll when props scroll is undefined * feat(contentlayout): support scroll.y defaultValue and cover by otherProps.scroll * fix(contentlayout): add table size judge and use React.forwardRef
1 parent 02311ee commit 76f808d

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

src/contentLayout/__tests__/contentLayout.test.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { render } from '@testing-library/react';
3+
import { Pagination } from 'antd';
34

45
import ContentLayout from '..';
56

@@ -50,4 +51,50 @@ describe('test contentLayout', () => {
5051
'500px'
5152
);
5253
});
54+
test('should support contentLayout default table height', () => {
55+
const { container } = render(
56+
<ContentLayout>
57+
<ContentLayout.Header>Header</ContentLayout.Header>
58+
<ContentLayout.Table
59+
rowKey={'key'}
60+
columns={[
61+
{
62+
title: 'Age',
63+
dataIndex: 'age',
64+
key: 'age',
65+
},
66+
]}
67+
dataSource={[{ key: 1, age: 19 }]}
68+
footer={() => <Pagination />}
69+
/>
70+
</ContentLayout>
71+
);
72+
expect(container.querySelector<HTMLDivElement>('.ant-table-body')?.style.maxHeight).toBe(
73+
'calc(calc(calc(100vh - 96px) - 0px) - 88px)'
74+
);
75+
});
76+
test('should support contentLayout small table height', () => {
77+
const { container } = render(
78+
<ContentLayout>
79+
<ContentLayout.Header>Header</ContentLayout.Header>
80+
<ContentLayout.Table
81+
rowKey={'key'}
82+
size="small"
83+
className="dt-pagination-small"
84+
columns={[
85+
{
86+
title: 'Age',
87+
dataIndex: 'age',
88+
key: 'age',
89+
},
90+
]}
91+
dataSource={[{ key: 1, age: 19 }]}
92+
footer={() => <Pagination />}
93+
/>
94+
</ContentLayout>
95+
);
96+
expect(container.querySelector<HTMLDivElement>('.ant-table-body')?.style.maxHeight).toBe(
97+
'calc(calc(calc(100vh - 96px) - 0px) - 72px)'
98+
);
99+
});
53100
});

src/contentLayout/components.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,34 @@ type ITableProps<T> = {
1616
} & TableProps<T> &
1717
Omit<ContentLayoutChildProps, 'ref'>;
1818

19-
export const TableLayout = ({ height, ...otherProps }: ITableProps<any>) => {
20-
let lineHeight = 44;
19+
export const TableLayout = ({ height, size, className, ...otherProps }: ITableProps<any>) => {
20+
let lineHeight = size === 'small' ? 36 : 44;
2121

2222
if (otherProps.footer) {
23-
lineHeight = lineHeight * 2;
23+
let footerHeight = 44;
24+
if (className?.includes('dt-pagination-small')) footerHeight = 36;
25+
lineHeight = lineHeight + footerHeight;
2426
}
25-
const scroll: TableProps<any>['scroll'] = otherProps?.scroll ? { ...otherProps.scroll } : {};
26-
scroll.y = `calc(${height} - ${lineHeight}px)`;
2727

28-
return <Table {...otherProps} scroll={scroll} />;
28+
const scroll: TableProps<any>['scroll'] = {
29+
y: `calc(${height} - ${lineHeight}px)`,
30+
...otherProps.scroll,
31+
};
32+
33+
return <Table {...otherProps} size={size} className={className} scroll={scroll} />;
2934
};
3035

3136
interface HeaderProps extends ContentLayoutChildProps {
3237
style?: React.CSSProperties;
3338
className?: string;
3439
}
3540

36-
export const Header = ({ ref, className, style, children }: HeaderProps) => {
37-
return (
38-
<div className={classNames(`${NAME}__header`, className)} style={style} ref={ref}>
39-
{children}
40-
</div>
41-
);
42-
};
41+
export const Header = React.forwardRef<HTMLDivElement, HeaderProps>(
42+
({ className, style, children }, ref) => {
43+
return (
44+
<div className={classNames(`${NAME}__header`, className)} style={style} ref={ref}>
45+
{children}
46+
</div>
47+
);
48+
}
49+
);

src/contentLayout/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ const ContentLayout = (props: IProps) => {
2828
Children.forEach(children, (child) => {
2929
if (child?.type === Header) {
3030
header = cloneElement(child, {
31+
key: 'header',
3132
ref: headerRef,
3233
});
3334
}
3435
if (child?.type === TableLayout) {
3536
content.push(
3637
cloneElement(child, {
38+
key: 'table',
3739
height: contentHeight,
3840
})
3941
);

0 commit comments

Comments
 (0)