index.jsx 4.6 KB

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