index.jsx 5.16 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10
import React, { createElement, memo, useRef, useState } from 'react';
import {
  ProForm,
  ProFormDependency,
  ProFormSelect,
  ProFormText,
  ProFormGroup,
  ProFormList,
  ProCard,
} from '@ant-design/pro-components';
TZW's avatar
TZW committed
11
import moment, { defaultFormat } from 'moment';
wuhao's avatar
wuhao committed
12 13 14
import { doFetch } from '@/utils/doFetch';
import styles from './index.less';
import FormItems from './FormItems';
TZW's avatar
TZW committed
15
import ExtendField from '@/components/ExtendField';
wuhao's avatar
wuhao committed
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

function upperCase(str) {
  const newStr = str.slice(0, 1).toUpperCase() + str.slice(1);
  return newStr;
}

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={styles.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;
                    }
                  });
                  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,
                  colProps: colProps,
                  key: item.dataIndex,
                  formRef: proformRef,
                })}
              </>
            );
          }
        })}
    </>
  );
});

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

wuhao's avatar
wuhao committed
108 109 110 111
  let proformRef = useRef();
  proformRef = formRef ?? proformRef;
  return (
    <ProForm
wuhao's avatar
wuhao committed
112
      style={{ overflow: 'hidden', ...style }}
wuhao's avatar
wuhao committed
113 114 115 116 117 118 119 120 121 122
      formRef={proformRef}
      onFinish={onFinish}
      formKey={formKey ?? parseInt(Math.random() * 1000000)}
      params={params}
      submitter={submitter ?? true}
      grid={true}
      rowProps={{
        gutter: 12,
      }}
      request={async (params) => {
wuhao's avatar
wuhao committed
123
        if (detailpath && val != 'add') {
wuhao's avatar
wuhao committed
124
          let res = await doFetch({ url: detailpath, params });
TZW's avatar
TZW committed
125 126
          if (extendField) {
            let obj = {};
wuhao's avatar
wuhao committed
127
            res?.data?.data[extendField]?.forEach?.((it) => {
TZW's avatar
TZW committed
128
              obj[it?.fieldId] = it?.fieldRealValue;
TZW's avatar
TZW committed
129
            });
wuhao's avatar
wuhao committed
130

TZW's avatar
TZW committed
131 132 133 134 135
            // console.log({
            //   ...obj,
            //   ...defaultFormValue,
            //   ...(res?.data?.data ?? {}),
            // });
wuhao's avatar
wuhao committed
136

TZW's avatar
TZW committed
137 138 139
            return {
              ...obj,
              ...defaultFormValue,
wuhao's avatar
wuhao committed
140
              ...(res?.data?.data ?? {}),
wuhao's avatar
wuhao committed
141
              ...relationSupplierList,
TZW's avatar
TZW committed
142 143
            };
          }
wuhao's avatar
wuhao committed
144 145 146 147 148 149 150 151 152 153 154 155

          //反填参数 格式化
          const relationSupplierList = res?.data?.data?.relationSupplierList
            ? {
                relationSupplierList: res?.data?.data?.relationSupplierList?.map?.((it, i) => {
                  return {
                    ...it,
                    id: it?.supplierId,
                  };
                }),
              }
            : {};
wuhao's avatar
wuhao committed
156 157
          return {
            ...defaultFormValue,
wuhao's avatar
wuhao committed
158 159
            ...(res?.data?.data ?? {}),
            ...relationSupplierList,
wuhao's avatar
wuhao committed
160 161
          };
        } else {
TZW's avatar
TZW committed
162 163 164
          // console.log({
          //   ...defaultFormValue,
          // });
wuhao's avatar
wuhao committed
165 166 167 168 169 170
          return {
            ...defaultFormValue,
          };
        }
      }}
      autoFocusFirstInput
TZW's avatar
TZW committed
171 172 173
      onValuesChange={(changedValues, allValues) => {
        onValuesChange?.(changedValues, allValues);
      }}
wuhao's avatar
wuhao committed
174 175 176 177 178 179 180 181 182 183 184
    >
      <FormRender
        fields={fields.filter((it) => it.valueType != 'option')}
        colProps={colProps}
        proformRef={proformRef}
      />
    </ProForm>
  );
}

export default InitForm;