summary-cell.tsx 3.73 KB
import React, { useEffect, useState } from 'react';
import { Select } from 'antd';
import * as formulajs from '@formulajs/formulajs';
import moment from 'moment';
import _ from 'lodash';
import { DATE_KEYS } from '../util';

const { Option } = Select;

interface SummaryCellProps {
  options: any[];
  property: any;
  item: string;
  padding: number;
  summaryData: any;
}

// const FIELD_LENGTH = 170;

const SummaryCell: React.FC<SummaryCellProps> = (props) => {
  const { options, property, item, summaryData } = props;
  const [selectFx, setSelectFx] = useState<string | number>('');
  const [resultObj, setResultObj] = useState<any>({});

  // const getCellWidth = (schema: any, PADDING: number) => {
  //   return (schema.width ? parseInt(schema.width) : FIELD_LENGTH - PADDING) + "px"
  // }

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const handleFixed = (val: any, _summary: any[]) => {
    if (property?.items?.properties[item]?.type === 'number') {
      const precision =
        property?.items?.properties[item]?.props?.precision || 0;
      return Number(val).toFixed(precision);
    } else {
      return Number(val);
    }
    // const pre = _summary?.[0]?.toString().split('.')?.length > 1 ? 2 : 0;
    // if (typeof val === 'number' || typeof val === 'string') {
    //   return Number(val).toFixed(precision?precision:pre)
    // } else {
    //   return ''
    // }
  };

  useEffect(() => {
    const _obj: any = {};
    // eslint-disable-next-line @typescript-eslint/no-shadow
    options.forEach((item: { value: string }) => {
      let _summary = _.cloneDeep(summaryData[item.value] || []);
      _summary = _summary.filter((it: any) => it !== undefined);
      const [key, fx] = item.value.split('/');
      let isDateTime: boolean = false;
      // eslint-disable-next-line @typescript-eslint/no-shadow
      DATE_KEYS.forEach((item) => {
        if (key.indexOf(item) === 0) {
          isDateTime = true;
        }
      });
      // const isDateTime = key.indexOf('date_') === 0 || key.indexOf('qxDatetime_') === 0 || key.indexOf('created_at_') === 0 || key.indexOf('updated_at_') === 0; // 日期时间转为毫秒数,考虑是复制出来的组件
      if (isDateTime && ['MAX', 'MIN'].includes(fx)) {
        _summary = summaryData[item.value].filter((it: any) => it); // 比较日期的最大最小时,过滤掉空值
        _summary.forEach((it: any, index: number) => {
          if (it) {
            _summary[index] = moment(it);
          }
        });
        // @ts-ignore
        _obj[item.value] = summaryData[item.value]
          ? moment[fx.toLowerCase()](_summary)?._i
          : ' ';
      } else {
        _obj[item.value] = summaryData[item.value]
          ? handleFixed(formulajs[fx](_summary), _summary)
          : ' ';
      }
    });
    setResultObj(_obj);
    setSelectFx(options[0]?.value);
  }, [summaryData, options]);

  return (
    <span
      style={{
        display: 'flex',
        justifyContent: 'space-between',
        alignItems: 'center',
        color: '#7E818A',
      }}
    >
      {options?.length > 1 ? (
        <Select
          bordered={false}
          dropdownClassName={'qx-summary_options'}
          defaultValue={options[0]?.value}
          onChange={(val) => setSelectFx(val)}
          value={selectFx}
        >
          {
            // eslint-disable-next-line @typescript-eslint/no-shadow
            options.map((item) => (
              <Option value={item.value}>{item.label}</Option>
            ))
          }
        </Select>
      ) : (
        <span className={'qx-summary_name'}>{options[0]?.label}</span>
      )}
      <span className={'qx-summary-result'} title={resultObj[selectFx]}>
        {resultObj[selectFx]}
      </span>
    </span>
  );
};

export default SummaryCell;