index.tsx 4.29 KB
Newer Older
陈帅's avatar
陈帅 committed
1 2
import { Alert, Table } from 'antd';
import { ColumnProps, SorterResult, TableProps } from 'antd/es/table';
陈帅's avatar
陈帅 committed
3
import React, { Component, Fragment } from 'react';
陈帅's avatar
陈帅 committed
4

陈帅's avatar
陈帅 committed
5
import { TableListItem } from '../../data';
陈帅's avatar
陈帅 committed
6
import styles from './index.less';
7

陈帅's avatar
陈帅 committed
8 9 10 11 12
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export interface StandardTableProps<T> extends Omit<TableProps<T>, 'columns'> {
  columns: StandardTableColumnProps[];
  data: {
陈帅's avatar
陈帅 committed
13
    list: TableListItem[];
陈帅's avatar
陈帅 committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    pagination: StandardTableProps<TableListItem>['pagination'];
  };
  selectedRows: TableListItem[];
  onSelectRow: (rows: any) => void;
}

export type StandardTableColumnProps = ColumnProps<TableListItem> & {
  needTotal?: boolean;
  total?: number;
};

function initTotalList(columns: StandardTableColumnProps[]) {
  if (!columns) {
    return [];
  }
  const totalList: StandardTableColumnProps[] = [];
jim's avatar
jim committed
30
  columns.forEach(column => {
ddcat1115's avatar
ddcat1115 committed
31 32 33 34 35 36 37
    if (column.needTotal) {
      totalList.push({ ...column, total: 0 });
    }
  });
  return totalList;
}

陈帅's avatar
陈帅 committed
38 39 40 41 42 43 44
interface StandardTableState {
  selectedRowKeys: string[];
  needTotalList: StandardTableColumnProps[];
}

class StandardTable extends Component<StandardTableProps<TableListItem>, StandardTableState> {
  static getDerivedStateFromProps(nextProps: StandardTableProps<TableListItem>) {
jim's avatar
jim committed
45 46 47 48 49 50 51 52 53 54
    // clean state
    if (nextProps.selectedRows.length === 0) {
      const needTotalList = initTotalList(nextProps.columns);
      return {
        selectedRowKeys: [],
        needTotalList,
      };
    }
    return null;
  }
陈帅's avatar
陈帅 committed
55

陈帅's avatar
陈帅 committed
56 57 58 59 60 61 62 63 64 65
  constructor(props: StandardTableProps<TableListItem>) {
    super(props);
    const { columns } = props;
    const needTotalList = initTotalList(columns);

    this.state = {
      selectedRowKeys: [],
      needTotalList,
    };
  }
陈帅's avatar
陈帅 committed
66

陈帅's avatar
陈帅 committed
67
  handleRowSelectChange = (selectedRowKeys: string[], selectedRows: TableListItem[]) => {
陈帅's avatar
陈帅 committed
68
    let { needTotalList } = this.state;
69 70 71 72
    needTotalList = needTotalList.map(item => ({
      ...item,
      total: selectedRows.reduce((sum, val) => sum + parseFloat(val[item.dataIndex], 10), 0),
    }));
陈帅's avatar
陈帅 committed
73 74 75
    const { onSelectRow } = this.props;
    if (onSelectRow) {
      onSelectRow(selectedRows);
76 77
    }

ddcat1115's avatar
ddcat1115 committed
78
    this.setState({ selectedRowKeys, needTotalList });
jim's avatar
jim committed
79
  };
80

陈帅's avatar
陈帅 committed
81 82 83 84 85 86
  handleTableChange = (
    pagination: StandardTableProps<TableListItem>['pagination'],
    filters: Record<keyof TableListItem, string[]>,
    sorter: SorterResult<TableListItem>,
    ...rest
  ) => {
陈帅's avatar
陈帅 committed
87 88
    const { onChange } = this.props;
    if (onChange) {
陈帅's avatar
陈帅 committed
89
      onChange(pagination, filters, sorter, ...rest);
陈帅's avatar
陈帅 committed
90
    }
jim's avatar
jim committed
91
  };
92 93 94

  cleanSelectedKeys = () => {
    this.handleRowSelectChange([], []);
jim's avatar
jim committed
95
  };
96 97

  render() {
ddcat1115's avatar
ddcat1115 committed
98
    const { selectedRowKeys, needTotalList } = this.state;
陈帅's avatar
陈帅 committed
99 100
    const { data, rowKey, ...rest } = this.props;
    const { list = [], pagination = false } = data || {};
101 102 103 104 105 106 107 108 109 110

    const paginationProps = {
      showSizeChanger: true,
      showQuickJumper: true,
      ...pagination,
    };

    const rowSelection = {
      selectedRowKeys,
      onChange: this.handleRowSelectChange,
陈帅's avatar
陈帅 committed
111
      getCheckboxProps: (record: TableListItem) => ({
nikogu's avatar
nikogu committed
112 113
        disabled: record.disabled,
      }),
114 115 116 117 118 119
    };

    return (
      <div className={styles.standardTable}>
        <div className={styles.tableAlert}>
          <Alert
jim's avatar
jim committed
120
            message={
陈帅's avatar
陈帅 committed
121
              <Fragment>
ddcat1115's avatar
ddcat1115 committed
122
                已选择 <a style={{ fontWeight: 600 }}>{selectedRowKeys.length}</a>&nbsp;&nbsp;
陈帅's avatar
陈帅 committed
123
                {needTotalList.map((item, index) => (
jim's avatar
jim committed
124
                  <span style={{ marginLeft: 8 }} key={item.dataIndex}>
陈帅's avatar
陈帅 committed
125 126
                    {item.title}
                    总计&nbsp;
jim's avatar
jim committed
127
                    <span style={{ fontWeight: 600 }}>
陈帅's avatar
陈帅 committed
128 129 130
                      {item.render
                        ? item.render(item.total, item as TableListItem, index)
                        : item.total}
ddcat1115's avatar
ddcat1115 committed
131
                    </span>
jim's avatar
jim committed
132 133 134 135 136
                  </span>
                ))}
                <a onClick={this.cleanSelectedKeys} style={{ marginLeft: 24 }}>
                  清空
                </a>
陈帅's avatar
陈帅 committed
137
              </Fragment>
jim's avatar
jim committed
138
            }
139 140 141 142 143
            type="info"
            showIcon
          />
        </div>
        <Table
144
          rowKey={rowKey || 'key'}
145 146 147 148
          rowSelection={rowSelection}
          dataSource={list}
          pagination={paginationProps}
          onChange={this.handleTableChange}
149
          {...rest}
150 151 152 153 154 155 156
        />
      </div>
    );
  }
}

export default StandardTable;