index.jsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import React, { useState, useCallback, useMemo, useEffect } from 'react';
  2. import { Table, Button } from '@douyinfe/semi-ui';
  3. const Demo = props => {
  4. console.log(props);
  5. const [total, setTotal] = useState(406);
  6. const [pageSize, setPageSize] = useState(props.pageSize || 10);
  7. const [currentPage, setCurrentPage] = useState(props.currentPage || 1);
  8. useEffect(() => {
  9. console.log(`didUpdate`, props.pageSize, props.currentPage);
  10. setPageSize(props.pageSize);
  11. setCurrentPage(props.currentPage);
  12. }, [props.pageSize, props.currentPage]);
  13. const pagination = useMemo(() => {
  14. console.log('pagination', pageSize, currentPage);
  15. return {
  16. showSizeChanger: true,
  17. pageSize,
  18. // currentPage,
  19. total,
  20. onPageChange: currentPage => setCurrentPage(currentPage),
  21. onPageSizeChange: pageSize => setPageSize(pageSize),
  22. };
  23. }, [pageSize, currentPage]);
  24. // const pagination = {
  25. // showSizeChanger: true,
  26. // pageSize,
  27. // // currentPage,
  28. // total,
  29. // onPageChange: currentPage => setCurrentPage(currentPage),
  30. // onPageSizeChange: pageSize => setPageSize(pageSize),
  31. // };
  32. const randomTotal = useCallback(() => {
  33. setTotal(Math.ceil(Math.random() * 100) + 40);
  34. });
  35. const columns = useMemo(() => [
  36. {
  37. title: 'Name',
  38. dataIndex: 'name',
  39. filters: [
  40. {
  41. text: '名字包含"1"',
  42. value: '1',
  43. },
  44. {
  45. text: '名字包含"2"',
  46. value: '2',
  47. },
  48. ],
  49. onFilter: (value, record) => record.name.indexOf(value) > -1,
  50. sorter: (a, b) => a.name.length - b.name.length,
  51. },
  52. {
  53. title: 'Age',
  54. dataIndex: 'age',
  55. sorter: (a, b) => (a.age - b.age > 0 ? 1 : -1),
  56. },
  57. {
  58. title: 'Address',
  59. dataIndex: 'address',
  60. filters: [
  61. {
  62. text: 'London',
  63. value: 'London',
  64. },
  65. {
  66. text: 'New York',
  67. value: 'New York',
  68. },
  69. ],
  70. filterMultiple: false,
  71. onFilter: (value, record) => record.address.indexOf(value) === 0,
  72. sorter: (a, b) => a.address.length - b.address.length,
  73. },
  74. ]);
  75. const data = useMemo(() => {
  76. const data = [];
  77. for (let i = pageSize * (currentPage - 1); i < Math.min(total, currentPage * pageSize); i++) {
  78. data.push({
  79. key: '' + i,
  80. name: `Edward King ${i}`,
  81. age: 32,
  82. address: `London, Park Lane no. ${i}`,
  83. });
  84. }
  85. return data;
  86. }, [total]);
  87. return (
  88. <div>
  89. <Table
  90. columns={columns}
  91. dataSource={data}
  92. pagination={pagination}
  93. onChange={(...args) => console.log(...args)}
  94. />
  95. </div>
  96. );
  97. };
  98. export default function App() {
  99. const demo = props => {
  100. console.log(props);
  101. const [total, setTotal] = useState(406);
  102. // const [pageSize, setPageSize] = useState(10);
  103. // const [currentPage, setCurrentPage] = useState(1);
  104. const [pageSize, setPageSize] = useState(props.pageSize || 10);
  105. const [currentPage, setCurrentPage] = useState(props.currentPage || 1);
  106. const [pagination, setPagination] = useState({});
  107. const [dataSource, setDataSource] = useState([]);
  108. useEffect(() => {
  109. setPageSize(props.pageSize);
  110. setCurrentPage(props.currentPage);
  111. setPagination({
  112. pageSize: props.pageSize,
  113. currentPage: props.currentPage,
  114. total,
  115. onPageChange: currentPage => setCurrentPage(currentPage),
  116. onPageSizeChange: pageSize => setPageSize(pageSize),
  117. showSizeChanger: true,
  118. });
  119. }, [props.pageSize, props.currentPage]);
  120. useEffect(() => {
  121. const data = [];
  122. for (let i = pageSize * (currentPage - 1); i < Math.min(total, currentPage * pageSize); i++) {
  123. data.push({
  124. key: '' + i,
  125. name: `Edward King ${i}`,
  126. age: 32,
  127. address: `London, Park Lane no. ${i}`,
  128. });
  129. }
  130. setDataSource(data);
  131. }, [pagination]);
  132. // const pagination = useMemo(() => {
  133. // console.log('pagination', pageSize, currentPage);
  134. // return {
  135. // showSizeChanger: true,
  136. // pageSize,
  137. // currentPage,
  138. // total,
  139. // onPageChange: currentPage => setCurrentPage(currentPage),
  140. // onPageSizeChange: pageSize => setPageSize(pageSize),
  141. // };
  142. // }, [pageSize, currentPage]);
  143. const randomTotal = useCallback(() => {
  144. setTotal(Math.ceil(Math.random() * 100) + 40);
  145. });
  146. const columns = useMemo(() => [
  147. {
  148. title: 'Name',
  149. dataIndex: 'name',
  150. filters: [
  151. {
  152. text: '名字包含"1"',
  153. value: '1',
  154. },
  155. {
  156. text: '名字包含"2"',
  157. value: '2',
  158. },
  159. ],
  160. onFilter: (value, record) => record.name.indexOf(value) > -1,
  161. sorter: (a, b) => a.name.length - b.name.length,
  162. },
  163. {
  164. title: 'Age',
  165. dataIndex: 'age',
  166. sorter: (a, b) => (a.age - b.age > 0 ? 1 : -1),
  167. },
  168. {
  169. title: 'Address',
  170. dataIndex: 'address',
  171. filters: [
  172. {
  173. text: 'London',
  174. value: 'London',
  175. },
  176. {
  177. text: 'New York',
  178. value: 'New York',
  179. },
  180. ],
  181. filterMultiple: false,
  182. onFilter: (value, record) => record.address.indexOf(value) === 0,
  183. sorter: (a, b) => a.address.length - b.address.length,
  184. },
  185. ]);
  186. // const data = useMemo(() => {
  187. // const data = [];
  188. // for (let i = pageSize * (currentPage - 1); i < Math.min(total, currentPage * pageSize); i++) {
  189. // data.push({
  190. // key: '' + i,
  191. // name: `Edward King ${i}`,
  192. // age: 32,
  193. // address: `London, Park Lane no. ${i}`,
  194. // });
  195. // }
  196. // return data;
  197. // }, [total, currentPage, pageSize]);
  198. return (
  199. <div>
  200. <Table
  201. columns={columns}
  202. dataSource={dataSource}
  203. pagination={pagination}
  204. onChange={(...args) => console.log(...args)}
  205. />
  206. </div>
  207. );
  208. };
  209. const [config, setConfig] = useState({ pageSize: 10, currentPage: 5 });
  210. const [pageSize, setPageSize] = useState(10);
  211. const [currentPage, setCurrentPage] = useState(1);
  212. const onClick = () => {
  213. const arr = [
  214. { pageSize: 5, currentPage: 3 },
  215. { pageSize: 10, currentPage: 8 },
  216. { pageSize: 20, currentPage: 10 },
  217. { pageSize: 50, currentPage: 12 },
  218. { pageSize: 100, currentPage: 15 },
  219. ];
  220. const num = Math.floor(Math.random() * 10) % arr.length;
  221. setConfig(arr[num]);
  222. };
  223. return (
  224. <div>
  225. <Button onClick={onClick}>更新Pagination参数</Button>
  226. {demo(config)}
  227. </div>
  228. );
  229. }