index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import Assets from '@src/components/Assets';
  4. import CheckboxItem from '../CheckboxItem';
  5. import Icon from '../Icon';
  6. import UserPagination from '../UserPagination';
  7. export default class UserTable extends Component {
  8. onSort(key, value) {
  9. const { sortMap = {}, onSort } = this.props;
  10. sortMap[key] = value;
  11. if (onSort) onSort(sortMap);
  12. }
  13. onSelect(checked, key) {
  14. const { selectList = [] } = this.props;
  15. if (checked) {
  16. selectList.push(key);
  17. } else {
  18. selectList.splice(selectList.indexOf(key), 1);
  19. }
  20. if (this.props.onSelect) this.props.onSelect(selectList);
  21. }
  22. render() {
  23. const {
  24. columns = [],
  25. rowKey = 'key',
  26. data = [],
  27. select,
  28. selectList = [],
  29. current,
  30. total,
  31. pageSize,
  32. size = 'basic',
  33. even = 'even',
  34. theme = 'defalut',
  35. border = true,
  36. header = true,
  37. sortMap = {},
  38. maxHeight,
  39. onChange,
  40. } = this.props;
  41. return (
  42. <div className={`user-table ${size} ${even} ${theme} ${border ? 'border' : ''}`}>
  43. <table>
  44. {header && (
  45. <thead>
  46. <tr>
  47. {columns.map((item, i) => {
  48. return (
  49. <th
  50. className={`${item.className || ''} ${i === 0 && select ? 'check' : ''}`}
  51. width={item.width}
  52. align={item.align}
  53. >
  54. {item.title}
  55. {item.fixSort &&
  56. (sortMap[item.key] ? (
  57. <Icon active name="arrow-down" onClick={() => this.onSort(item.key, '')} />
  58. ) : (
  59. <Icon name="arrow-up" onClick={() => this.onSort(item.key, 'desc')} />
  60. ))}
  61. {item.sort &&
  62. (sortMap[item.key] ? (
  63. <Assets
  64. name={sortMap[item.key] === 'asc' ? 'seqencing2_up_select' : 'seqencing2_down_select'}
  65. onClick={() => this.onSort(item.key, sortMap[item.key] === 'asc' ? 'desc' : '')}
  66. />
  67. ) : (
  68. <Assets name="seqencing2_normal" onClick={() => this.onSort(item.key, 'asc')} />
  69. ))}
  70. </th>
  71. );
  72. })}
  73. </tr>
  74. </thead>
  75. )}
  76. <tbody style={{ maxHeight }}>
  77. {data.map(row => {
  78. const checked = selectList.indexOf(row[rowKey]) >= 0;
  79. return (
  80. <tr>
  81. {columns.map((item, i) => {
  82. return (
  83. <td
  84. className={`${item.className || ''} ${i === 0 && select ? 'check' : ''}`}
  85. width={item.width}
  86. align={item.align}
  87. >
  88. {i === 0 && select && (
  89. <CheckboxItem
  90. theme="white"
  91. checked={checked}
  92. onClick={value => this.onSelect(value, row[rowKey])}
  93. />
  94. )}
  95. {item.render ? item.render(row[item.key], row) : row[item.key]}
  96. </td>
  97. );
  98. })}
  99. </tr>
  100. );
  101. })}
  102. </tbody>
  103. </table>
  104. {data.length === 0 && <div className="empty">暂无数据</div>}
  105. {total > 0 && data.length > 0 && <UserPagination total={total} pageSize={pageSize} current={current} onChange={onChange} />}
  106. </div>
  107. );
  108. }
  109. }