index.jsx 4.95 KB
Newer Older
wuhao's avatar
wuhao committed
1
import { doFetch } from "@/utils/doFetch";
wuhao's avatar
wuhao committed
2
import { ProForm, ProFormDependency } from "@ant-design/pro-components";
wuhao's avatar
wuhao committed
3
import { Button } from "@mui/material";
wuhao's avatar
wuhao committed
4 5 6
import { createElement, memo, useRef } from "react";
import FormItems from "./FormItems";
import "./index.less";
wuhao's avatar
wuhao committed
7 8 9 10 11

function upperCase(str) {
  const newStr = str.slice(0, 1).toUpperCase() + str.slice(1);
  return newStr;
}
wuhao's avatar
wuhao committed
12

wuhao's avatar
wuhao committed
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
let FormRender = memo(({ fields = [], colProps, proformRef }) => {
  return (
    <>
      {fields
        .filter((it) => it.hideInForm !== true)
        .map((item, index) => {
          let key = item?.valueType ? upperCase(item?.valueType) : "Input";
          let { hideInForm } = item;
          item.formItemProps = item.formItemProps ?? { rules: [] };
          if (item.valueType == "split") {
            return (
              <div
                className="title"
                style={{ borderWidth: index == 0 ? 0 : 1 }}
              >
                {item.title}
              </div>
            );
          }
          if (hideInForm && Object.keys(hideInForm)) {
            return (
              <ProFormDependency name={Object.keys(hideInForm)}>
                {(params) => {
                  let ifs = true;
                  let res = Object.keys(hideInForm).map((its) => {
                    if (Array.isArray(hideInForm[its])) {
                      return !hideInForm[its].includes(params[its]);
                    } else {
                      let vals = hideInForm[its].reverse; //取反 即不存在当前数组中的
                      return vals.indexOf(params[its]) != -1;
wuhao's avatar
wuhao committed
43
                    }
wuhao's avatar
wuhao committed
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
                  });
                  ifs = res.includes(false);
                  if (ifs) {
                    return;
                  } else {
                    return (
                      <>
                        {createElement(FormItems[key], {
                          item: item,
                          colProps: colProps,
                          key: item.dataIndex,
                          formRef: proformRef,
                        })}
                      </>
                    );
                  }
                }}
              </ProFormDependency>
            );
          } else {
            return (
              <>
                {createElement(FormItems[key], {
                  item: {
                    ...item,
                    fieldProps: {
                      ...item.fieldProps,
                      size: "large",
                    },
                  },
                  colProps: colProps,
                  key: item.dataIndex,
                  formRef: proformRef,
                })}
              </>
            );
          }
        })}
    </>
  );
});

function InitForm({
  formRef,
krysent's avatar
krysent committed
88
  onFinish = (vals, extra) => {
wuhao's avatar
wuhao committed
89
    //console.log(vals, extra);
wuhao's avatar
wuhao committed
90 91 92
  },
  formKey,
  params = {},
wuhao's avatar
wuhao committed
93
  style = {},
wuhao's avatar
wuhao committed
94 95 96 97 98 99
  detailpath = "",
  defaultFormValue = {},
  submitter,
  fields,
  colProps = { xs: 24, sm: 24, md: 12, lg: 12, xl: 12, xxl: 12 },
  onValuesChange = (changedValues, allValues) => {
wuhao's avatar
wuhao committed
100
    //console.log(changedValues, allValues);
wuhao's avatar
wuhao committed
101
  },
wuhao's avatar
wuhao committed
102
  disabled
wuhao's avatar
wuhao committed
103 104 105 106 107 108
}) {
  let proformRef = useRef();
  proformRef = formRef ?? proformRef;

  return (
    <ProForm
wuhao's avatar
wuhao committed
109
      style={{ ...style, overflow: "hidden" }}
wuhao's avatar
wuhao committed
110 111 112 113 114 115 116 117 118 119 120 121 122
      formRef={proformRef}
      onFinish={onFinish}
      formKey={formKey ?? parseInt(Math.random() * 1000000)}
      params={params}
      submitter={
        submitter || submitter === false
          ? submitter
          : {
              render: (props, doms) => {
                return [
                  <Button
                    type="reset"
                    key="rest"
wuhao's avatar
wuhao committed
123
                    disabled={disabled}
wuhao's avatar
wuhao committed
124 125 126 127 128 129 130 131
                    onClick={() => props.form?.resetFields()}
                  >
                    重置
                  </Button>,
                  <Button
                    type="submit"
                    key="submit"
                    variant="contained"
wuhao's avatar
wuhao committed
132
                    disabled={disabled}
krysent's avatar
krysent committed
133 134
                    onClick={(e) => {
                      e.preventDefault();
wuhao's avatar
wuhao committed
135
                      return props.form?.submit?.();
krysent's avatar
krysent committed
136
                    }}
wuhao's avatar
wuhao committed
137 138 139 140 141
                  >
                    提交
                  </Button>,
                ];
              },
wuhao's avatar
wuhao committed
142
            }
wuhao's avatar
wuhao committed
143 144 145 146 147 148 149 150 151 152 153 154
      }
      grid={true}
      rowProps={{
        gutter: 12,
      }}
      request={async (params) => {
        if (detailpath) {
          let res = await doFetch({ url: detailpath, params });
          return {
            ...defaultFormValue,
            ...(res?.data?.data ?? {}),
          };
wuhao's avatar
wuhao committed
155
        } else {
wuhao's avatar
wuhao committed
156 157 158
          return {
            ...defaultFormValue,
          };
wuhao's avatar
wuhao committed
159
        }
wuhao's avatar
wuhao committed
160 161 162
      }}
      autoFocusFirstInput
      onValuesChange={(changedValues, allValues) => {
wuhao's avatar
wuhao committed
163
        onValuesChange?.(changedValues, allValues,proformRef);
wuhao's avatar
wuhao committed
164 165 166 167 168 169 170 171 172
      }}
    >
      <FormRender
        fields={fields.filter((it) => it.valueType != "option")}
        colProps={colProps}
        proformRef={proformRef}
      />
    </ProForm>
  );
wuhao's avatar
wuhao committed
173 174
}

wuhao's avatar
wuhao committed
175
export default InitForm;