index.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, { useState, useCallback } from 'react';
  2. import { Table } from '../../../index';
  3. const Demo = () => {
  4. const columns = [
  5. {
  6. title: 'Name',
  7. dataIndex: 'name',
  8. width: 250,
  9. key: 'name',
  10. },
  11. { title: 'Age', dataIndex: 'age', key: 'age', width: 200 },
  12. { title: 'Address', dataIndex: 'address', key: 'address' },
  13. ];
  14. const childrenRecordName = 'children';
  15. const [data, setData] = useState([
  16. {
  17. key: 1,
  18. name: 'John Brown',
  19. age: 32,
  20. address: 'New York No. 1 Lake Park',
  21. description: 'My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park.',
  22. [childrenRecordName]: [],
  23. },
  24. {
  25. key: 2,
  26. name: 'Jim Green',
  27. age: 42,
  28. address: 'London No. 1 Lake Park',
  29. // description: 'My name is Jim Green, I am 42 years old, living in London No. 1 Lake Park.',
  30. },
  31. {
  32. key: 3,
  33. name: 'Joe Black',
  34. age: 32,
  35. address: 'Sidney No. 1 Lake Park',
  36. description: 'My name is Joe Black, I am 32 years old, living in Sidney No. 1 Lake Park.',
  37. },
  38. ]);
  39. const [loading, setLoading] = useState();
  40. const fetchChildrenData = useCallback(
  41. async key => {
  42. setLoading(true);
  43. await new Promise((resolve, reject) => {
  44. setTimeout(() => {
  45. setLoading(false);
  46. const newData = [...data];
  47. const objRecord = newData.find(item => item.key);
  48. if (objRecord) {
  49. objRecord.children = [
  50. {
  51. key: 11,
  52. name: 'Jr. John Brown',
  53. age: 32,
  54. address: 'New York No. 11 Lake Park',
  55. description:
  56. 'My name is John Brown, I am 32 years old, living in New York No. 11 Lake Park.',
  57. },
  58. ];
  59. setData(newData);
  60. }
  61. }, 2000);
  62. });
  63. },
  64. [data]
  65. );
  66. return (
  67. <Table
  68. loading={loading}
  69. childrenRecordName={childrenRecordName}
  70. columns={columns}
  71. onExpand={(expanded, record, domEvent) => {
  72. domEvent && domEvent.stopPropagation();
  73. console.log(`onExpand: `, expanded, record, domEvent);
  74. if (expanded && Array.isArray(record.children) && !record.children.length) {
  75. fetchChildrenData(record.key);
  76. }
  77. }}
  78. onExpandedRowsChange={rows => {
  79. console.log(`onExpandedRowsChange: `, rows);
  80. }}
  81. onRow={(record, index) => {
  82. return {
  83. onClick: () => {
  84. console.log(`onRow clicked, index: ${index} , record: `, record);
  85. },
  86. };
  87. }}
  88. rowExpandable={record => Array.isArray(record.children)}
  89. dataSource={data}
  90. />
  91. );
  92. };
  93. export default Demo;