index.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React, { useState, useEffect, useRef, useMemo } from 'react';
  2. import { Table, Switch, Avatar, Tooltip } from '../../../index';
  3. // eslint-disable-next-line max-lines-per-function
  4. function Demo() {
  5. const rowKey = record => `${record.city && record.city.toLowerCase()}-${record.job && record.job.toLowerCase()}`;
  6. const [dynamic, setDynamic] = useState(false);
  7. const [times, setTimes] = useState(1);
  8. const { current: cache } = useRef({ times, size: 1, interval: null, intervalTimeout: 3000 });
  9. useEffect(() => {
  10. if (dynamic) {
  11. cache.interval = setInterval(() => {
  12. cache.times++;
  13. setTimes(cache.times);
  14. }, cache.intervalTimeout);
  15. }
  16. return () => {
  17. clearInterval(cache.interval);
  18. };
  19. // eslint-disable-next-line react-hooks/exhaustive-deps
  20. }, [dynamic]);
  21. const data = useMemo(() => {
  22. /**
  23. * FE => frontend engineer
  24. * BE => backend engineer
  25. * Andoird => android engineer
  26. * IOS => ios engineer
  27. * SE => software engineer
  28. */
  29. const rawData = [
  30. { city: 'Beijing', job: 'FE', department: 'IES' },
  31. { city: 'Beijing', job: 'BE', department: 'IES' },
  32. { city: 'Shanghai', job: 'Android', department: 'IES' },
  33. { city: 'Tokyo', job: 'Android', department: 'IES' },
  34. { city: 'Shanghai', job: 'IOS', department: 'EE' },
  35. { city: 'LA', job: 'SE', department: 'EE' },
  36. { city: 'Beijing', job: 'Android', department: 'EE' },
  37. { city: 'Tokyo', job: 'IOS', department: 'EE' },
  38. { city: 'Tokyo', job: 'SE', department: 'DATA' },
  39. { city: 'Shanghai', job: 'BE', department: 'DATA' },
  40. { city: 'LA', job: 'Android', department: 'DATA' },
  41. { city: 'LA', job: 'IOS', department: 'DATA' },
  42. ];
  43. const start = 0;
  44. const end = times * cache.size;
  45. const newData = [];
  46. for (let i = start + 1; i <= end; i++) {
  47. newData.push(
  48. ...rawData.map(item => ({
  49. ...item,
  50. job: `${item.job } L${i}`,
  51. }))
  52. );
  53. }
  54. console.log('newData: ', newData);
  55. return newData;
  56. // eslint-disable-next-line react-hooks/exhaustive-deps
  57. }, [times]);
  58. const columns = [
  59. {
  60. dataIndex: 'city',
  61. title: 'City',
  62. width: 200,
  63. sorter: (a, b) => (a.city > b.city ? 1 : -1),
  64. fixed: 'left',
  65. },
  66. {
  67. dataIndex: 'job',
  68. title: 'Job',
  69. filters: [{ text: 'IOS', value: 'IOS' }, { text: 'Android', value: 'Android' }],
  70. onFilter: (value, record) => record.job && record.job.indexOf(value) === 0,
  71. },
  72. {
  73. dataIndex: 'department',
  74. title: 'Department',
  75. fixed: 'right',
  76. width: 200,
  77. render: text => (
  78. <span>
  79. <Avatar src={'https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/Lark20190614-154048.png'} />
  80. <Tooltip content={text}>
  81. <span>{text}</span>
  82. </Tooltip>
  83. </span>
  84. ),
  85. },
  86. ];
  87. return (
  88. <div style={{ padding: '20px 0px', width: 600 }}>
  89. <div>
  90. <span>
  91. 动态增加数据:
  92. <Switch checked={dynamic} onChange={checked => setDynamic(checked)} />
  93. </span>
  94. </div>
  95. <Table
  96. dataSource={data}
  97. rowKey={rowKey}
  98. groupBy={'city'}
  99. columns={columns}
  100. renderGroupSection={groupKey => <strong>Jobs in {groupKey}:</strong>}
  101. onGroupedRow={(group, index) => ({
  102. // onMouseEnter: () => {
  103. // console.log(`Grouped row mouse enter: `, group, index);
  104. // },
  105. // onMouseLeave: () => {
  106. // console.log(`Grouped row mouse leave: `, group, index);
  107. // },
  108. onClick: () => {
  109. console.log('Grouped row mouse click: ', group, index);
  110. },
  111. })}
  112. style={{ width: 800, margin: 20 }}
  113. // bordered
  114. defaultExpandAllRows
  115. clickGroupedRowToExpand
  116. scroll={{
  117. x: 1200,
  118. y: 600,
  119. }}
  120. virtualized
  121. pagination={false}
  122. />
  123. </div>
  124. );
  125. }
  126. export default Demo;