Skip to content

Commit 4904a4d

Browse files
authored
feat(contentlayout): rewrite contentLayout to use Content and Footer (#618)
1 parent 3b09b0a commit 4904a4d

File tree

3 files changed

+103
-78
lines changed

3 files changed

+103
-78
lines changed

src/contentLayout/__tests__/contentLayout.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('test contentLayout', () => {
7070
</ContentLayout>
7171
);
7272
expect(container.querySelector<HTMLDivElement>('.ant-table-body')?.style.maxHeight).toBe(
73-
'calc(calc(calc(100vh - 96px) - 0px) - 88px)'
73+
'calc(calc(calc(100vh - 96px) - 0px - 0px) - 88px)'
7474
);
7575
});
7676
test('should support contentLayout small table height', () => {
@@ -94,7 +94,7 @@ describe('test contentLayout', () => {
9494
</ContentLayout>
9595
);
9696
expect(container.querySelector<HTMLDivElement>('.ant-table-body')?.style.maxHeight).toBe(
97-
'calc(calc(calc(100vh - 96px) - 0px) - 72px)'
97+
'calc(calc(calc(100vh - 96px) - 0px - 0px) - 88px)'
9898
);
9999
});
100100
});

src/contentLayout/components.tsx

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/contentLayout/index.tsx

Lines changed: 101 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,142 @@
11
import React, { Children, cloneElement, useCallback, useMemo, useRef } from 'react';
2+
import { Table, TableProps } from 'antd';
23
import classNames from 'classnames';
34

4-
import { Header, TableLayout } from './components';
55
import './index.scss';
66

77
interface IProps {
8+
header?: React.ReactElement;
9+
content?: React.ReactElement;
10+
footer?: React.ReactElement;
811
height?: string;
9-
children?: React.ReactElement | React.ReactElement[];
12+
children?: React.ReactElement[];
1013
style?: React.CSSProperties;
1114
className?: string;
15+
padding?: number | string;
1216
}
1317

14-
export const NAME = 'dtc-content-layout';
18+
const NAME = 'dtc-content-layout';
1519

1620
const ContentLayout = (props: IProps) => {
17-
const { height = 'calc(100vh - 96px)', style, className, children } = props;
21+
const { height = 'calc(100vh - 96px)', className, style, padding, children } = props;
1822
const headerRef: any = useRef(null);
23+
const footerRef: any = useRef(null);
1924

2025
const contentHeight = useMemo(() => {
21-
return `calc(${height} - ${headerRef?.current?.clientHeight || 0}px)`;
22-
}, [height, headerRef?.current]);
26+
return `calc(${height} - ${headerRef?.current?.clientHeight || 0}px - ${
27+
footerRef?.current?.clientHeight || 0
28+
}px)`;
29+
}, [height, headerRef?.current, footerRef?.current]);
2330

2431
const render = useCallback(() => {
25-
let header;
26-
const content: React.ReactElement<any, string | React.JSXElementConstructor<any>>[] = [];
27-
32+
let herder = props?.header && (
33+
<ContentLayout.Header thisRef={headerRef}>{props?.header}</ContentLayout.Header>
34+
);
35+
const content = [props?.content];
36+
let footer = props?.footer && (
37+
<ContentLayout.Footer thisRef={footerRef}>{props?.footer}</ContentLayout.Footer>
38+
);
2839
Children.forEach(children, (child) => {
29-
if (child?.type === Header) {
30-
header = cloneElement(child, {
31-
key: 'header',
32-
ref: headerRef,
40+
if (child?.type === ContentLayout.Header) {
41+
herder = cloneElement(child, {
42+
thisRef: headerRef,
3343
});
34-
}
35-
if (child?.type === TableLayout) {
44+
} else if (child?.type === ContentLayout.Footer) {
45+
footer = cloneElement(child, {
46+
thisRef: footerRef,
47+
});
48+
} else if (child) {
3649
content.push(
37-
cloneElement(child, {
38-
key: 'table',
50+
cloneElement(child as React.ReactElement<any>, {
3951
height: contentHeight,
4052
})
4153
);
4254
}
4355
});
4456

45-
return [header, ...content];
57+
return [herder, ...content, footer];
4658
}, [children, contentHeight]);
4759

4860
return (
4961
<div
5062
className={classNames(NAME, className)}
51-
style={height ? { ...style, height } : { ...style }}
63+
style={{
64+
...style,
65+
height,
66+
padding,
67+
}}
5268
>
5369
{render()}
5470
</div>
5571
);
5672
};
5773

58-
type OriginalInterface = typeof ContentLayout;
59-
interface LayoutInterface extends OriginalInterface {
60-
Header: typeof Header;
61-
Table: typeof TableLayout;
74+
interface ContentLayoutChildProps {
75+
thisRef?: React.Ref<HTMLDivElement>;
76+
children?: React.ReactElement | React.ReactElement[] | React.ReactNode;
77+
style?: React.CSSProperties;
78+
}
79+
interface HeaderProps extends ContentLayoutChildProps {}
80+
81+
ContentLayout.Header = ({ thisRef, children, style }: HeaderProps) => {
82+
return (
83+
<div className={`${NAME}__header`} ref={thisRef} style={style}>
84+
{children}
85+
</div>
86+
);
87+
};
88+
89+
type ITableProps<T> = {
90+
height?: string;
91+
children?: any;
92+
} & TableProps<T> &
93+
ContentLayoutChildProps;
94+
95+
ContentLayout.Table = ({ height, children, ...otherProps }: ITableProps<any>) => {
96+
let lineHeight = 44;
97+
98+
if (otherProps.footer) {
99+
lineHeight = lineHeight * 2;
100+
}
101+
const scroll: TableProps<any>['scroll'] = {
102+
y: `calc(${height} - ${lineHeight}px)`,
103+
...otherProps?.scroll,
104+
};
105+
106+
return children ? (
107+
cloneElement(children, {
108+
scroll,
109+
})
110+
) : (
111+
<Table {...otherProps} scroll={scroll} />
112+
);
113+
};
114+
115+
interface FooterProps extends ContentLayoutChildProps {}
116+
117+
ContentLayout.Footer = ({ thisRef, children }: FooterProps) => {
118+
return (
119+
<div className={`${NAME}__footer`} ref={thisRef}>
120+
{children}
121+
</div>
122+
);
123+
};
124+
125+
interface ContentProps extends ContentLayoutChildProps {
126+
style?: React.CSSProperties;
127+
className?: string;
128+
height?: string | number;
62129
}
63130

64-
const LayoutWrapper = ContentLayout;
65-
(LayoutWrapper as LayoutInterface).Header = Header;
66-
(LayoutWrapper as LayoutInterface).Table = TableLayout;
131+
ContentLayout.Content = ({ style = {}, className, height, children }: ContentProps) => {
132+
return (
133+
<div
134+
className={classNames(`${NAME}__content`, className)}
135+
style={height ? { ...style, height } : { ...style }}
136+
>
137+
{children}
138+
</div>
139+
);
140+
};
67141

68-
export default LayoutWrapper as LayoutInterface;
142+
export default ContentLayout;

0 commit comments

Comments
 (0)