index.js 3.8 KB
Newer Older
1 2 3 4
import React, { Fragment, useEffect, useRef } from 'react';
import { WhiteSpace, WingBlank } from 'antd-mobile';

export default function MobileCard(props) {
5
  const { value = {}, onChange, json, getArguments, tableComProps, dataColumn } = props;
6 7 8 9
  let otherProps = {};
  if (json?.otherProps) {
    otherProps = json?.otherProps;
    try {
10
      let arg = {};
11 12
      if (typeof getArguments === 'function') {
        arg = getArguments({ base52: dataColumn.base52 }, function() {});
13
      }
14 15 16 17
      otherProps = new Function('functionArguments', 'tableComProps', otherProps)(
        arg,
        tableComProps,
      );
18 19 20 21 22
    } catch (e) {
      console.log('MobileCard组件获取otherProps 报错', e);
      otherProps = null;
    }
  }
23
  if (!otherProps) {
24 25 26
    return null;
  }
  const dataSource = value?.dataSource || otherProps?.defaultDataSource || [];
27
  const { columns = [], primaryKey = '', style = {}, handleClick, emptyDataInfo } = otherProps;
28 29 30
  return (
    <>
      {dataSource.map((g, index) => {
31 32 33 34 35 36 37
        const clickFunction =  // 点击整个块
          typeof handleClick === 'function'
            ? e => {
              if (e && e.stopPropagation) {
                e.stopPropagation();
                e.preventDefault();
              }
钟是志's avatar
钟是志 committed
38
              handleClick(g,value);
39 40
            }
            : undefined;
41
        return (
42
          <div key={g[primaryKey]} style={style} onClick={clickFunction}>
43 44 45
            <WingBlank>
              {columns.map((c, cIndex) => {
                const { style: cStyle, data: cData } = c;
钟是志's avatar
钟是志 committed
46
                const clickOneRow = // 点击某一行
47 48 49 50 51 52 53 54 55
                  typeof c.handleClick === 'function'
                    ? e => {
                      if (e && e.stopPropagation) {
                        e.stopPropagation();
                        e.preventDefault();
                      }
                      return c.handleClick(g, c, value);
                    }
                    : undefined;
56
                return (
钟是志's avatar
钟是志 committed
57
                  <div key={c.key} style={cStyle || {}} onClick={clickOneRow}>
58 59
                    {cData.map((h, hIndex) => {
                      let hProps = h.props || {};
钟是志's avatar
钟是志 committed
60 61
                      const clickOneCell = // 点击某一个单元格
                        typeof h.handleClick === 'function'
62 63 64 65 66
                          ? e => {
                              if (e && e.stopPropagation) {
                                e.stopPropagation();
                                e.preventDefault();
                              }
钟是志's avatar
钟是志 committed
67
                              return h.handleClick(g, h.key || h, value);
68 69 70 71 72 73
                            }
                          : undefined;
                      if (h.diyProps && typeof h.diyProps === 'function') {
                        hProps = h.diyProps(g, h.key || h, value) || {};
                      }
                      return (
钟是志's avatar
钟是志 committed
74
                        <div style={hProps.style} onClick={clickOneCell} key={h.key || h.value}>
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                          {hProps.children ? (
                            <>{hProps.children}</>
                          ) : (
                            <>
                              {h.type === 'dataIndex' && index + 1}
                              {(h.type === 'dataSource' && g[h.key]) || ''}
                              {h.type === 'chinese' && h.value}
                            </>
                          )}
                        </div>
                      );
                    })}
                  </div>
                );
              })}
            </WingBlank>
          </div>
        );
93
      })}
94 95 96 97 98 99 100 101
      {  // 没有数据的时候显示提示文字
        (!Array.isArray(dataSource) || !dataSource.length) && !!emptyDataInfo &&
          <div style={emptyDataInfo.style || {}}>
            {emptyDataInfo.children}
          </div>
      }


102 103 104
    </>
  );
}