All files / src/form/cells TextCell.tsx

77.77% Statements 14/18
94.44% Branches 17/18
25% Functions 1/4
77.77% Lines 14/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                  66x 24x 24x   24x 24x 24x 2x   22x 1x                                 21x   21x 21x 21x                                                                     21x        
import React, { ReactElement } from "react";
import { Alert, Popover, Tag } from "antd";
import { useTranslation } from 'react-i18next';
import { MaxTextCellDirectDisplayCharLength } from '@config/base';
import { CopyOutlined } from '@ant-design/icons';
import { wrapAsHtml } from "@utils/ComponentUtils";
import { copyToClipBoard } from "@utils/StringUtils";
import { openSuccessMessage } from '@utils/NotificationUtils';
import {stopPropagationAndPreventDefault} from "@utils/ObjectUtils";
 
interface TextCellRenderProps {
  value: string;
  length?: number;
  placeholder?: string;
  displayMode?: "exception";
}
 
const TextCell = (props: TextCellRenderProps): ReactElement => {
  const { value, length, placeholder, displayMode } = props;
  const maxDisplay = (length != null) ?
    length : MaxTextCellDirectDisplayCharLength;
  const style = (maxDisplay === 999) ? "detail-string" : "";
  const { t } = useTranslation();
  if (value == null) {
    return <div className="string-cell" />;
  }
  if (value === "null") {
    return <Popover
      title={undefined}
      trigger="hover"
      placement="left"
      content={
        <Alert
          message="本次执行返回值为 null(空对象)"
          description="根据不同业务场景,前台可能隐藏该对象、显示空字符串,或报错"
          className="light-bg-alert"
          type="info"
          showIcon={true}
        />
      }
    >
      <Tag className="string-cell-placeholder">{placeholder ?? 'NULL'}</Tag>
    </Popover>;
  }
  const className = (displayMode === 'exception')
    ? "string-popover-stacktrace" : "string-popover";
  const classNames = `${className} ${style}`;
  const toHtmlValue = /<\/?[a-z][\s\S]*>/i.test(value) ? wrapAsHtml(value) : value;
  const content = (value.length > maxDisplay) ?
    (<div><Popover
      title={undefined}
      trigger={["hover", "click"]}
      placement="left"
      content={
        <>
        <CopyOutlined
        style={{
          position: "absolute",
          right: "20px",
          top: "36px",
          fontSize: "150%"
        }}
        onClick={(e: React.MouseEvent<unknown>) => {
          e.stopPropagation();
          copyToClipBoard(value).then(() => {
            openSuccessMessage(t('Success copy to clipboard'));
          });
        }}
        title={t('Copy to clipboard')}
        />
        <pre
          onClick={(e: React.MouseEvent<unknown>) => {
              stopPropagationAndPreventDefault(e);
          }}
          className={classNames}
        >
          {toHtmlValue}
        </pre>
        </>
      }
    >
      <Tag className="string-cell-placeholder">{placeholder ?? '...'}</Tag>
    </Popover></div>) : <span style={{ whiteSpace: "pre-line" }}>{toHtmlValue}</span>;
  return (<div  key={Math.random().toString()} className={`string-cell ${style}`}>{content}</div>);
};
 
export default TextCell;