index.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { Table, Button } from '@douyinfe/semi-ui';
  2. import React, { useMemo, useState, useEffect } from 'react';
  3. function App() {
  4. const [dataTotalSize, setTotalSize] = useState(46);
  5. const [pagination, setPagination] = useState({ pageSize: 12, total: dataTotalSize, currentPage: 1 });
  6. const [dataSource, setDataSource] = useState([]);
  7. const defaultSelectedRowKeys = useMemo(() => ['1', '8', '17'], []);
  8. const [selectedRowKeys, setSelectedRowKeys] = useState(defaultSelectedRowKeys);
  9. const footerText = useMemo(() => `当前选中 ${selectedRowKeys.length} 项`, [selectedRowKeys]);
  10. const randomTotalSize = () => {
  11. setTotalSize(40 + Math.round(Math.random() * 100));
  12. };
  13. const columns = useMemo(() => [
  14. {
  15. title: 'Name',
  16. dataIndex: 'name',
  17. render: text => <a>{text}</a>,
  18. },
  19. {
  20. title: 'Age',
  21. dataIndex: 'age',
  22. },
  23. {
  24. title: 'Address',
  25. dataIndex: 'address',
  26. },
  27. {
  28. render: () => (
  29. <input
  30. type="checkbox"
  31. onClick={(...args) => {
  32. console.log('onClick: ', ...args);
  33. }}
  34. onChange={(...args) => {
  35. console.log('onChange: ', ...args);
  36. }}
  37. />
  38. ),
  39. },
  40. ]);
  41. const data = useMemo(() => {
  42. const _data = [];
  43. for (let i = 0; i < dataTotalSize; i++) {
  44. let age = (i * 1000) % 149 ;
  45. let name = `Edward King ${i}`;
  46. _data.push({
  47. key: '' + i,
  48. name,
  49. age,
  50. address: `London, Park Lane no. ${i} Lake Park`,
  51. description: `My name is ${name}, I am ${age} years old, living in New York No. ${i + 1} Lake Park.`,
  52. });
  53. }
  54. return _data;
  55. }, [dataTotalSize]);
  56. useEffect(() => {
  57. setPagination({ ...pagination, total: dataTotalSize });
  58. }, [dataTotalSize]);
  59. useEffect(() => {
  60. const dataSource = [];
  61. const { pageSize } = pagination;
  62. dataSource.push(...data.slice(0, pageSize));
  63. setDataSource(dataSource);
  64. }, [data]);
  65. const rowSelection = {
  66. onChange: (selectedRowKeys, selectedRows) => {
  67. console.log(
  68. `rowSelection.onChanged: selectedRowKeys: ${JSON.stringify(selectedRowKeys)}`,
  69. 'selectedRows: ',
  70. selectedRows
  71. );
  72. setSelectedRowKeys(selectedRowKeys);
  73. },
  74. onSelectAll: (selected, selectedRows, changedRows) => {
  75. console.log(
  76. `rowSelection.onSelectAll: selected :${selected}, selectedRows: ${selectedRows}, changedRows: ${changedRows}`
  77. );
  78. },
  79. getCheckboxProps: record => ({
  80. // disabled: record.name === 'Michael James', // Column configuration not to be checked
  81. disabled: true,
  82. name: record.name,
  83. onClick: (...args) => {
  84. console.log('Clicked checkbox: ', ...args);
  85. },
  86. }),
  87. onSelect: (record, selected) => {
  88. console.log('onSelect: ', record, selected);
  89. },
  90. disabled: true,
  91. selectedRowKeys,
  92. defaultSelectedRowKeys,
  93. };
  94. const onChange = (props = {}) => {
  95. const { pagination = {} } = props;
  96. const { currentPage, pageSize, total } = pagination;
  97. // console.log(`Table changed to: `, data);
  98. setDataSource(data.slice((currentPage - 1) * pageSize, currentPage * pageSize));
  99. setPagination({ currentPage, pageSize, total });
  100. };
  101. return (
  102. <div>
  103. <div>
  104. <Button onClick={randomTotalSize}>随机数据量</Button>
  105. </div>
  106. <Table
  107. rowKey={record => record.key}
  108. columns={columns}
  109. dataSource={dataSource}
  110. rowSelection={rowSelection}
  111. pagination={pagination}
  112. onChange={onChange}
  113. footer={footerText}
  114. onRow={(record, index) => {
  115. return {
  116. onClick: () => {
  117. console.log(`onRow.onClick: `, record, index);
  118. },
  119. };
  120. }}
  121. />
  122. </div>
  123. );
  124. }
  125. export default App;