File size: 1,815 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React from 'react';
import { Flex } from 'antd';
import type { FlexProps } from 'antd';
import { createStyles } from 'antd-style';
import classNames from 'classnames';

import ImagePreview from '../ImagePreview';
import type { ImagePreviewProps } from '../ImagePreview';

const isNotEmpty = (val: any) => {
  return typeof val !== 'undefined' && val !== null && val !== '';
};

const useStyle = createStyles(({ css, token }) => {
  return {
    wrapper: css`
      color: ${token.colorText};
      font-size: ${token.fontSize}px;
      line-height: 2;
    `,
    title: css`
      margin: 1em 0;
    `,
    description: css`
      margin: 1em 0;
      padding-inline-start: 0.8em;
      color: ${token.colorTextSecondary};
      font-size: 90%;
      border-inline-start: 4px solid ${token.colorSplit};
      p {
        margin: 0;
      }
    `,
  };
});

interface FlexWithImagePreviewProps {
  imagePreviewProps?: ImagePreviewProps;
  title?: string;
  description?: string;
}

const FlexWithImagePreview: React.FC<
  FlexWithImagePreviewProps & React.PropsWithChildren<FlexProps>
> = (props) => {
  const { imagePreviewProps, title, description, className, style, children, ...rest } = props;
  const { styles } = useStyle();
  if (!title && !description) {
    return <ImagePreview {...imagePreviewProps}>{children}</ImagePreview>;
  }
  return (
    <Flex className={classNames(styles.wrapper, className)} style={style} {...rest}>
      <Flex align="flex-start" justify="flex-start" vertical>
        {isNotEmpty(title) && <div className={styles.title}>{title}</div>}
        {isNotEmpty(description) && <div className={styles.description}>{description}</div>}
      </Flex>
      <ImagePreview {...imagePreviewProps}>{children}</ImagePreview>
    </Flex>
  );
};

export default FlexWithImagePreview;