index.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React, { useMemo, useState, useEffect, useRef } from 'react';
  2. import { Table, Tooltip, Tag, Avatar } from '../../../index';
  3. Demo.parameters = {
  4. chromatic: { disableSnapshot: true },
  5. };
  6. export default function Demo() {
  7. const [counter, setCounter] = useState(1);
  8. const scrollingRef = useRef(false);
  9. const columns = [
  10. {
  11. title: 'Name',
  12. dataIndex: 'name',
  13. width: 150,
  14. fixed: true,
  15. filterMultiple: false,
  16. filters: [
  17. {
  18. // text: <span style={{ display: 'inline-flex', width: '100%', height: '100%' }}></span>,
  19. text: '',
  20. value: '',
  21. },
  22. {
  23. text: 'Code 45',
  24. value: '45',
  25. },
  26. {
  27. text: 'King 4',
  28. value: 'King 4',
  29. },
  30. ],
  31. onFilter: (value, record) => record.name.includes(value),
  32. render: text => (
  33. <span>
  34. <Avatar src={'https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/Lark20190614-154048.png'} />
  35. {text}
  36. </span>
  37. ),
  38. },
  39. {
  40. title: 'Age',
  41. dataIndex: 'age',
  42. width: 150,
  43. sorter: (a, b) => (a.age - b.age > 0 ? 1 : -1),
  44. },
  45. {
  46. title: 'Address',
  47. width: 200,
  48. dataIndex: 'address',
  49. },
  50. {
  51. title: 'Description',
  52. // width: 400,
  53. dataIndex: 'description',
  54. },
  55. {
  56. fixed: 'right',
  57. width: 250,
  58. render: (text, record) => (
  59. <Tooltip content={record.description}>
  60. <Tag color="green">Show Info</Tag>
  61. </Tooltip>
  62. ),
  63. },
  64. ];
  65. const dataSource = useMemo(() => {
  66. const data = [];
  67. for (let i = 0; i < 10 * counter; i++) {
  68. let age = 40 + Math.ceil(i / 3);
  69. let name = `Edward King ${i}`;
  70. data.push({
  71. key: `${ i}`,
  72. name,
  73. age,
  74. address: `London, Park Lane no. ${i}`,
  75. description: `My name is ${name}, I am ${age} years old, living in New York No. ${i + 1} Lake Park.`,
  76. });
  77. }
  78. return data;
  79. }, [counter]);
  80. useEffect(() => {
  81. const timer = setInterval(() => {
  82. if (!scrollingRef.current) {
  83. console.log('new data added');
  84. setCounter(counter + 1);
  85. }
  86. }, 2000);
  87. return () => clearInterval(timer);
  88. }, [counter]);
  89. return (
  90. <Table
  91. style={{ width: 800 }}
  92. scroll={{ y: 600, x: 1500 }}
  93. dataSource={dataSource}
  94. columns={columns}
  95. virtualized={
  96. {
  97. // onScroll: () => (scrollingRef.current = true),
  98. }
  99. }
  100. pagination={false}
  101. />
  102. );
  103. }