|
1 | 1 | import React, { Children, cloneElement, useCallback, useMemo, useRef } from 'react'; |
| 2 | +import { Table, TableProps } from 'antd'; |
2 | 3 | import classNames from 'classnames'; |
3 | 4 |
|
4 | | -import { Header, TableLayout } from './components'; |
5 | 5 | import './index.scss'; |
6 | 6 |
|
7 | 7 | interface IProps { |
| 8 | + header?: React.ReactElement; |
| 9 | + content?: React.ReactElement; |
| 10 | + footer?: React.ReactElement; |
8 | 11 | height?: string; |
9 | | - children?: React.ReactElement | React.ReactElement[]; |
| 12 | + children?: React.ReactElement[]; |
10 | 13 | style?: React.CSSProperties; |
11 | 14 | className?: string; |
| 15 | + padding?: number | string; |
12 | 16 | } |
13 | 17 |
|
14 | | -export const NAME = 'dtc-content-layout'; |
| 18 | +const NAME = 'dtc-content-layout'; |
15 | 19 |
|
16 | 20 | 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; |
18 | 22 | const headerRef: any = useRef(null); |
| 23 | + const footerRef: any = useRef(null); |
19 | 24 |
|
20 | 25 | 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]); |
23 | 30 |
|
24 | 31 | 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 | + ); |
28 | 39 | 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, |
33 | 43 | }); |
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) { |
36 | 49 | content.push( |
37 | | - cloneElement(child, { |
38 | | - key: 'table', |
| 50 | + cloneElement(child as React.ReactElement<any>, { |
39 | 51 | height: contentHeight, |
40 | 52 | }) |
41 | 53 | ); |
42 | 54 | } |
43 | 55 | }); |
44 | 56 |
|
45 | | - return [header, ...content]; |
| 57 | + return [herder, ...content, footer]; |
46 | 58 | }, [children, contentHeight]); |
47 | 59 |
|
48 | 60 | return ( |
49 | 61 | <div |
50 | 62 | className={classNames(NAME, className)} |
51 | | - style={height ? { ...style, height } : { ...style }} |
| 63 | + style={{ |
| 64 | + ...style, |
| 65 | + height, |
| 66 | + padding, |
| 67 | + }} |
52 | 68 | > |
53 | 69 | {render()} |
54 | 70 | </div> |
55 | 71 | ); |
56 | 72 | }; |
57 | 73 |
|
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; |
62 | 129 | } |
63 | 130 |
|
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 | +}; |
67 | 141 |
|
68 | | -export default LayoutWrapper as LayoutInterface; |
| 142 | +export default ContentLayout; |
0 commit comments