All files / src/form DataCollectFormMasterPanel.tsx

50% Statements 8/16
52.38% Branches 11/21
25% Functions 1/4
50% Lines 8/16

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                                              66x                                       38x                                   35x 35x 35x 35x 35x   35x                                                                                                                                            
//TODO Move to a separate file
import { Button, Form, FormInstance, Space, Tooltip } from "antd";
import {
  ContinueOperate,
  FormType,
  GroupedFormFields,
  OperationPage,
  SaveRecordProps,
  Store
} from "@props/RecordProps";
import { ValidateErrorEntity } from "rc-field-form/lib/interface";
import React, { ReactElement } from "react";
import { useTranslation } from "react-i18next";
import { DefaultFormLayout, FormLayoutConfig, SpecialFormLayout } from "@config/base";
import {
  DoubleLeftOutlined,
  NodeCollapseOutlined,
  NodeExpandOutlined,
  SaveOutlined
} from "@ant-design/icons";
import { stopPropagationAndPreventDefault } from "@utils/ObjectUtils";
import { debounce } from "lodash";
 
const DataCollectFormMasterPanel = (props: {
  domainName: string,
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  form: FormInstance<any>,
  onValuesChange: (changedValues: Store, allValues: Store) => void,
  onFinish: (result: SaveRecordProps, continueOperate?: ContinueOperate) => void,
  onFinishFailed: (result: ValidateErrorEntity) => void,
  formFieldElems: JSX.Element | GroupedFormFields | Array<React.ReactElement>,
  collapseMasterCallback: () => void;
  expandGroupsCallback: () => void;
  collapseGroupsCallback: () => void;
  hasGroup?: boolean,
  labelAlign?: "left" | "right",
  initialValues?: SaveRecordProps,
  readonly?: boolean,
  hasDetail: boolean,
  formType?: FormType,
  showBottomSaveButton?: boolean,
  page: OperationPage,
}): ReactElement => {
  const { t } = useTranslation();
  const {
    labelAlign,
    formFieldElems,
    onFinish,
    onValuesChange,
    onFinishFailed,
    form,
    initialValues,
    domainName,
    collapseMasterCallback,
    collapseGroupsCallback,
    expandGroupsCallback,
    hasGroup,
    hasDetail,
    formType,
    showBottomSaveButton,
    page,
  } = props;
  const effLayout = FormLayoutConfig[formType as string] || SpecialFormLayout[domainName] || DefaultFormLayout;
  const title = t(`Save`);
  const pagesArray = ['master-detail', 'detail-drawer', 'wizard', 'user-profile-edit'];
  const pageConst = (page === 'action') ? "vertical" : (pagesArray.includes(page) ? "horizontal" : "inline");
  //FIXME 这里如果在 Modal 中弹出的,那么不应该显示 save 按钮
  return (<>
    {(hasDetail || hasGroup) &&
      <div className="master-operation-container">
        <Space direction="horizontal">
          {collapseMasterCallback && hasDetail &&
            <Tooltip
              title={t('Click to collapse panel')}
              placement='left'
            >
              <DoubleLeftOutlined
                onClick={(e) => {
                  stopPropagationAndPreventDefault(e);
                  collapseMasterCallback();
                }}
              />
            </Tooltip>
          }
          {hasGroup && <NodeExpandOutlined onClick={expandGroupsCallback} />}
          {hasGroup && <NodeCollapseOutlined onClick={collapseGroupsCallback} />}
        </Space>
      </div>
    }
    <Form
      {...effLayout}
      form={form}
      labelAlign={labelAlign ?? "right"}
      name="basic"
      colon={false}
      layout={pageConst}
      initialValues={initialValues}
      scrollToFirstError={true}
      onValuesChange={debounce(
        onValuesChange, 1000)
      }
      onFinish={(values) => {
        try {
          onFinish(values);
        } catch (error) {
          console.error(error);
        }
      }
      }
      onFinishFailed={(values) => {
        try {
          onFinishFailed(values);
        } catch (error) {
          console.error(error);
        }
      }}
    >
      {formFieldElems}
      {(showBottomSaveButton === true) && (
        <div className="save-button-container">
          <Button
            className="modal-title-button"
            type="default"
            htmlType="submit"
            size="middle"
            title={title}
            icon={<SaveOutlined />}
          >
            <span>{title}</span>
          </Button>
        </div>
      )}
    </Form>
  </>
  );
};
export default DataCollectFormMasterPanel;