index.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React, { useState, useEffect, useMemo, useCallback } from 'react';
  2. import { Table, Tag, Tooltip, Button } from '../../../index';
  3. export default function App() {
  4. const [data, setData] = useState();
  5. const [expandAllRows, setExpandAllRows] = useState(true);
  6. const columns = useMemo(
  7. () => [
  8. {
  9. title: 'Name',
  10. dataIndex: 'name',
  11. width: 150,
  12. fixed: true,
  13. filterMultiple: false,
  14. filters: [
  15. {
  16. // text: <span style={{ display: 'inline-flex', width: '100%', height: '100%' }}></span>,
  17. text: '',
  18. value: '',
  19. },
  20. {
  21. text: 'Code 45',
  22. value: '45',
  23. },
  24. {
  25. text: 'King 4',
  26. value: 'King 4',
  27. },
  28. ],
  29. onFilter: (value, record) => record.name.includes(value),
  30. },
  31. {
  32. title: 'Age',
  33. dataIndex: 'age',
  34. width: 150,
  35. sorter: (a, b) => (a.age - b.age > 0 ? 1 : -1),
  36. },
  37. {
  38. title: 'Address',
  39. width: 200,
  40. dataIndex: 'address',
  41. },
  42. {
  43. title: 'Description',
  44. // width: 400,
  45. dataIndex: 'description',
  46. },
  47. {
  48. fixed: 'right',
  49. width: 250,
  50. render: (text, record) => (
  51. <Tooltip content={record.description}>
  52. <Tag color="green">Show Info</Tag>
  53. </Tooltip>
  54. ),
  55. },
  56. ],
  57. []
  58. );
  59. const getData = () => {
  60. let newData = [];
  61. for (let i = 0; i < 46; i++) {
  62. let age = (i * 1000) % 149 ;
  63. let name = `Edward King ${i}`;
  64. newData.push({
  65. key: `${i}`,
  66. name,
  67. age,
  68. address: `London, Park Lane no. ${i}`,
  69. description: `My name is ${name}, I am ${age} years old, living in New York No. ${i + 1} Lake Park.`,
  70. });
  71. }
  72. return newData;
  73. };
  74. const toggleExpandAllRows = () => {
  75. setExpandAllRows(!expandAllRows);
  76. };
  77. useEffect(() => {
  78. setTimeout(() => {
  79. const newData = getData();
  80. setData(newData);
  81. console.log('load data');
  82. }, 1000);
  83. }, []);
  84. const expandRowRender = useCallback(record => {
  85. const { description } = record;
  86. return <article>{description}</article>;
  87. }, []);
  88. return (
  89. <div>
  90. <div>
  91. <Button onClick={toggleExpandAllRows}>动态设置expandAllRows</Button>
  92. <br /><br />
  93. </div>
  94. <label>expandAllRows={`${expandAllRows}`}</label>
  95. <Table
  96. // scroll={{ y: 500 }}
  97. columns={columns}
  98. dataSource={data}
  99. expandedRowRender={expandRowRender}
  100. expandAllRows={expandAllRows}
  101. pagination={false}
  102. />
  103. </div>
  104. );
  105. }