index.js 4.0 KB

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