index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import CheckboxItem from '../CheckboxItem';
  4. import Icon from '../Icon';
  5. export default class UserTable extends Component {
  6. onChangePage(page) {
  7. const { current, total, onChange } = this.props;
  8. if (page < current || page > total) return;
  9. if (onChange) onChange(page);
  10. }
  11. onSort(key, value) {
  12. const { sortMap = {}, onSort } = this.props;
  13. sortMap[key] = value;
  14. if (onSort) onSort(sortMap);
  15. }
  16. onSelect(checked, key) {
  17. const { selectList = [] } = this.props;
  18. if (checked) {
  19. selectList.push(key);
  20. } else {
  21. selectList.splice(selectList.indexOf(key), 1);
  22. }
  23. if (this.props.onSelect) this.props.onSelect(selectList);
  24. }
  25. render() {
  26. const {
  27. columns = [],
  28. rowKey = 'key',
  29. data = [],
  30. select,
  31. selectList = [],
  32. current,
  33. total,
  34. size = 'basic',
  35. even = 'even',
  36. theme = 'defalut',
  37. border = true,
  38. header = true,
  39. sortMap = {},
  40. maxHeight,
  41. } = this.props;
  42. return (
  43. <div className={`user-table ${size} ${even} ${theme} ${border ? 'border' : ''}`}>
  44. <table>
  45. {header && (
  46. <thead>
  47. <tr>
  48. {columns.map((item, i) => {
  49. return (
  50. <th
  51. className={`${item.className} ${i === 0 && select ? 'select' : ''}`}
  52. width={item.width}
  53. align={item.align}
  54. >
  55. {item.title}
  56. {item.fixSort &&
  57. (sortMap[item.key] ? (
  58. <Icon active name="arrow-down" onClick={() => this.onSort(item.key, '')} />
  59. ) : (
  60. <Icon name="arrow-up" onClick={() => this.onSort(item.key, 'desc')} />
  61. ))}
  62. {item.sort &&
  63. (sortMap[item.key] ? (
  64. <Icon
  65. active
  66. name={`arrow-${sortMap[item.key] === 'asc' ? 'up' : 'down'}`}
  67. onClick={() => this.onSort(item.key, sortMap[item.key] === 'asc' ? 'desc' : '')}
  68. />
  69. ) : (
  70. <Icon name="arrow-up" 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 ? 'select' : ''}`}
  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="prev" onClick={() => this.onChangePage(current - 1)} />
  110. <span>
  111. <b>{current}</b>/{total}
  112. </span>
  113. <Icon name="next" onClick={() => this.onChangePage(current + 1)} />
  114. </div>
  115. )}
  116. </div>
  117. );
  118. }
  119. }