index.jsx 3.22 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7
/* eslint-disable react-hooks/exhaustive-deps */
import { ProDescriptions } from '@ant-design/pro-components';
import React, { useMemo, useState } from 'react';
import { doFetch } from '@/utils/doFetch';
import PropTypes from 'prop-types';
import styles from './index.less';
import { Col } from 'antd';
8
import { utc } from 'moment';
wuhao's avatar
wuhao committed
9 10 11 12 13

const { Item } = ProDescriptions;

function DetailPro(props) {
  let { fields } = props;
TZW's avatar
TZW committed
14 15 16 17
  if (Object.keys(fields[0]).includes('render')) {
    delete fields[0].render;
  }

wuhao's avatar
wuhao committed
18 19 20 21 22 23 24 25 26 27
  const [curitem, setcuritem] = useState(props.detailData ?? {}); //全部数据

  const dataProps = useMemo(() => {
    if (props.detailData) {
      setcuritem(props.detailData);
      return { dataSource: props.detailData };
    } else {
      return {
        request: async () => {
          let res = await doFetch({ url: props.detailpath, params: props.params });
TZW's avatar
TZW committed
28 29
          if (props?.extendField) {
            let obj = {};
wuhao's avatar
wuhao committed
30
            res?.data?.data[props.extendField]?.forEach?.((it) => {
TZW's avatar
TZW committed
31
              obj[it?.fieldId] = it?.fieldRealValue;
TZW's avatar
TZW committed
32
            });
TZW's avatar
TZW committed
33
            //console.log('extendField:', obj);
TZW's avatar
TZW committed
34 35 36 37
            setcuritem({
              ...(res?.data?.data ?? {}),
              ...obj,
            });
TZW's avatar
TZW committed
38 39 40 41
            // console.log('drawerpro:', {
            //   ...(res?.data?.data ?? {}),
            //   ...obj,
            // });
TZW's avatar
TZW committed
42 43 44 45 46 47 48 49 50 51

            return {
              success: res?.code == '0000',
              data:
                {
                  ...(res?.data?.data ?? {}),
                  ...obj,
                } ?? {},
            };
          }
wuhao's avatar
wuhao committed
52 53 54 55 56 57 58
          setcuritem({
            ...(res?.data?.data ?? {}),
          });
          return {
            success: res?.code == '0000',
            data: { ...(res?.data?.data ?? {}) },
          };
wuhao's avatar
wuhao committed
59 60 61 62 63 64 65
        },
      };
    }
  }, [props.detailData]);

  return (
    <ProDescriptions column={3} {...dataProps} title={props.title}>
wuhao's avatar
wuhao committed
66 67 68 69 70 71 72 73 74
      {fields
        ?.filter((it) => !it.hideInDescriptions && it?.valueType != 'option')
        ?.map((it, i) => {
          const dataIndexs =
            !it?.render || it?.valueType == 'option'
              ? { dataIndex: it.dataIndex, valueType: it.valueType }
              : {};
          if (it.valueType == 'split') {
            return (
75 76 77 78 79
              <Col span={24} key={i}>
                <div className={styles.title} style={{ borderWidth: i == 0 ? 0 : 1, '--before': !(it.title || it?.render) ? 'none' : 'block', '--after': !(it.title || it?.render) ? '100%' : 'calc(100% - 160px)', '--padb': !(it.title || it?.render) ? '20px' : '0' }}>
                  {it?.render ? it?.render?.(curitem[it.dataIndex], curitem) : it.title}
                </div>
              </Col>
wuhao's avatar
wuhao committed
80 81
            );
          }
wuhao's avatar
wuhao committed
82
          return (
wuhao's avatar
wuhao committed
83 84 85 86 87 88 89 90
            <Item
              span={it?.span ? it?.span : fields[i + 1]?.valueType == 'split' ? 3 : 1}
              key={it.dataIndex}
              label={it.title}
              {...dataIndexs}
            >
              {it?.render ? it?.render?.(curitem[it.dataIndex], curitem) : ''}
            </Item>
wuhao's avatar
wuhao committed
91
          );
wuhao's avatar
wuhao committed
92
        })}
wuhao's avatar
wuhao committed
93 94 95 96 97 98 99 100 101 102 103 104
    </ProDescriptions>
  );
}

DetailPro.propTypes = {
  detailpath: PropTypes.string,
  params: PropTypes.object,
  detailData: PropTypes.object,
  fields: PropTypes.array,
};

export default DetailPro;