index.tsx 4.29 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Component, Fragment } from 'react';
ddcat1115's avatar
ddcat1115 committed
2
import { Table, Alert } from 'antd';
陈帅's avatar
陈帅 committed
3
import { TableProps, ColumnProps, SorterResult } from 'antd/lib/table';
4
import styles from './index.less';
陈帅's avatar
陈帅 committed
5
import { TableListItem } from '../../data';
6

陈帅's avatar
陈帅 committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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: {
    list: Array<TableListItem>;
    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
29
  columns.forEach(column => {
ddcat1115's avatar
ddcat1115 committed
30 31 32 33 34 35 36
    if (column.needTotal) {
      totalList.push({ ...column, total: 0 });
    }
  });
  return totalList;
}

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

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

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

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

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

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

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

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

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

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

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

export default StandardTable;