All files / src/form/action ActionButton.tsx

82.92% Statements 34/41
66.66% Branches 26/39
57.14% Functions 4/7
82.92% Lines 34/41

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                                                    66x   66x         5x   5x   5x   5x   5x   5x     5x   5x 2x 1x 1x 1x 1x             5x 2x   3x 3x 3x   3x 3x 3x     3x 3x 3x   3x       1x     1x 1x 1x       1x                                 3x   3x                                                          
import { ActionExtInfoProp, ActionMode, ActionProps, RecordProps, TableMetaProps } from '@props/RecordProps';
import { fetchDynamicActionParameters } from '@utils/FetchUtils';
import { stopPropagationAndPreventDefault } from '@utils/ObjectUtils';
import { Space } from 'antd';
import { ActionIcon } from '../../components/icons';
import React, { ReactElement, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import DrawerActionButton from './DrawerActionButton';
import PopoverActionButton from './PopoverActionButton';
 
export interface ActionButtonProps {
  action: ActionProps;
  fetchDataCallback: () => void;
  domainName: string;
  zIndex: number;
  selectedData?: Array<RecordProps>;
  displayLabel: boolean;
  mode: ActionMode;
  visiblePopover: string | undefined;
  setVisiblePopoverCallback: (key?: string) => void;
  ownerClass?: string;
  ownerId?: number;
  columnNameInOwnerClass?: string;
  labelField?: string;
}
 
export interface SpecActionButtonProps extends ActionButtonProps {
  closeCallback: () => void;
  parameters?: Array<TableMetaProps>;
  loadingParameters?: boolean;
  actionElem?: ReactElement;
  open?: boolean;
}
 
const DefaultActionExtInfo: ActionExtInfoProp = { displayLabel: true, refreshPage: true };
 
const ActionButton = (props: ActionButtonProps): ReactElement => {
 
  const {
    action, displayLabel, mode, visiblePopover, setVisiblePopoverCallback,
    labelField,
  } = props;
 
  const { id, label, icon, name, helpText, confirmType, extInfo, group } = action;
 
  const { displayLabel: displayLabelFromExtInfo } = extInfo ?? DefaultActionExtInfo;
 
  const [loadingParameters, setLoadingParameters] = useState<boolean>();
 
  const [display, setDisplay] = useState<boolean>(false);
 
  const { t } = useTranslation();
 
  //执行参数
  const [parameters, setParameters] = useState<Array<TableMetaProps>>();
 
  useEffect(() => {
    if (parameters == null) {
      setLoadingParameters(true);
      fetchDynamicActionParameters({ actionId: id }).then(parameters => {
        setParameters(parameters);
        setLoadingParameters(false);
      }).catch(e => console.error(`Failed to get execute parameters for action ${id}: ${e} `));
    }
  }, [
    id, parameters
  ]);
 
  if (loadingParameters) {
    return <></>;
  }
  const visible = (visiblePopover === id.toString());
  const classNameBasedOnActive = `${display && visible ? "action-button-active" : "action-button-inactive"}`;
  const className = `link-icon-container ${classNameBasedOnActive}`;
  // 标识是否是只显示图标,不显示 Popup 的简易模式
  const isSimpleAction = (confirmType === 'NO_POPUP_NO_CONFIRM');
  const isSingleDisplayMode = (mode === 'single');
  const spaceGap = isSingleDisplayMode ? 8 : 2;
  // 对于 Simple Action 类型的 action, 默认不显示 Label,
  // 除非 extInfo 中的 displayLabel 为 true, 或者父组件传递的 displayLabel 为 true
  const displayLabelForSimpleAction = (isSimpleAction && (displayLabelFromExtInfo === true)) || (displayLabel === true);
  const calcDisplayLabel = (!isSimpleAction || displayLabelForSimpleAction) && (displayLabel);
  const actionLabelWithTrans = calcDisplayLabel ? t(`dynamicAction:${label ?? name}`) : "";
 
  const actionElem = (<span
    className={className}
    title={helpText}
    onClick={(e: React.MouseEvent<HTMLElement>) => {
      Iif (isSimpleAction) {
        // 如果是 Simple Action, 什么也不用做, callback 在 "simple-action-icon" 中调用
      } else {
        stopPropagationAndPreventDefault(e);
        if (display === false) {
          setVisiblePopoverCallback(id.toString());
        } else Eif (display === true) {
          setVisiblePopoverCallback(undefined);
        }
        setDisplay(!display);
      }
    }}
  >
    <Space direction="horizontal" size={spaceGap}>
      {<ActionIcon icon={icon} />}
      <span className="action-label">
        {actionLabelWithTrans}
      </span>
    </Space>
  </span>);
 
  // If action
  // 1. has parameter,
  // 2. or is single mode,
  // 3. or is grouped,
  // use Drawer mode
  const displayUseDrawer = ((parameters?.length ?? 0) > 0) || isSingleDisplayMode || (group != null);
 
  return displayUseDrawer ? (
    <DrawerActionButton
      {...props}
      parameters={parameters}
      loadingParameters={loadingParameters}
      actionElem={actionElem}
      open={display && visible}
      closeCallback={() => {
        setDisplay(false);
        setVisiblePopoverCallback(undefined);
      }}
      labelField={labelField}
    />
  ) :
    (<PopoverActionButton
      {...props}
      parameters={parameters}
      loadingParameters={loadingParameters}
      actionElem={actionElem}
      open={display && visible}
      closeCallback={() => {
        setDisplay(false);
        setVisiblePopoverCallback(undefined);
      }}
      labelField={labelField}
    />);
};
 
export default ActionButton;