index.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* eslint-disable no-unused-vars */
  2. /* eslint-disable max-lines-per-function */
  3. /* eslint-disable no-shadow */
  4. import React, { useState, useEffect, useMemo } from 'react';
  5. import { Table, Button } from '../../../index';
  6. export default function Demo(props = {}) {
  7. const [dataSource, setDataSource] = useState([]);
  8. const [useFullRender, setUseFullRender] = useState(true);
  9. const total = 46;
  10. const scroll = {
  11. // x: '160%',
  12. // y: 600,
  13. };
  14. const pagination = {
  15. pageSize: 12,
  16. };
  17. const rowSelection = useMemo(() => ({
  18. hidden: useFullRender,
  19. fixed: 'left',
  20. }), [useFullRender]);
  21. useEffect(() => {
  22. const data = [];
  23. for (let i = 0; i < total; i++) {
  24. const no = i + 1;
  25. const age = (i * 1000) % 149 ;
  26. const name = `Edward King ${i}`;
  27. const childrenCount = i % 4;
  28. const children = [];
  29. for (let j = 0; j < childrenCount; j++) {
  30. children.push({
  31. key: `${i}-${j}`,
  32. name: `Jr. Edward King ${i}-${j}`,
  33. age: age - j * 3,
  34. address: `Beijing, Zhong Guan Cun No. ${no}`,
  35. description: `My name is ${name}, I am ${age} years old, living in Zhong Guan Cun No. ${no}.`,
  36. });
  37. }
  38. data.push({
  39. key: i,
  40. name,
  41. age,
  42. address: `Beijing, Zhong Guan Cun No. ${no}`,
  43. description: `My name is ${name}, I am ${age} years old, living in Zhong Guan Cun No. ${no}.`,
  44. children,
  45. });
  46. }
  47. setDataSource(data);
  48. }, [total]);
  49. const columns = useMemo(() => {
  50. const columns = [
  51. {
  52. title: ({ sorter, filter, selection }) => (
  53. <span style={{ paddingLeft: 20, display: 'inline-flex', alignItems: 'center' }}>
  54. {selection}
  55. <span style={{ marginLeft: 8 }}>Name</span>
  56. {sorter}
  57. {filter}
  58. </span>
  59. ),
  60. dataIndex: 'name',
  61. filters: [
  62. {
  63. text: '名字包含"1"',
  64. value: '1',
  65. },
  66. {
  67. text: '名字包含"2"',
  68. value: '2',
  69. },
  70. ],
  71. onFilter: (value, record) => record.name.indexOf(value) > -1,
  72. sorter: (a, b) => a.name.length - b.name.length,
  73. useFullRender,
  74. render: (text, record, index, { expandIcon, selection, indentText }) => (
  75. useFullRender ? (
  76. <span style={{ display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
  77. {indentText}
  78. {expandIcon}
  79. {selection}
  80. <span style={{ marginLeft: 8 }}>{text}</span>
  81. </span>
  82. ) : (
  83. text
  84. )
  85. ),
  86. width: 250,
  87. },
  88. {
  89. title: 'Age',
  90. dataIndex: 'age',
  91. sorter: (a, b) => (a.age - b.age > 0 ? 1 : -1),
  92. },
  93. {
  94. title: 'Address',
  95. dataIndex: 'address',
  96. filters: [
  97. {
  98. text: 'London',
  99. value: 'London',
  100. },
  101. {
  102. text: 'New York',
  103. value: 'New York',
  104. },
  105. ],
  106. filterMultiple: false,
  107. onFilter: (value, record) => record.address.indexOf(value) === 0,
  108. sorter: (a, b) => a.address.length - b.address.length,
  109. },
  110. ];
  111. return columns;
  112. }, [useFullRender]);
  113. return (
  114. <div style={{ width: 800 }}>
  115. <Button onClick={() => setUseFullRender(!useFullRender)}>
  116. {useFullRender ? '非自定义渲染' : '自定义渲染'}
  117. </Button>
  118. <Table
  119. pagination={pagination}
  120. // scroll={scroll}
  121. rowSelection={rowSelection}
  122. columns={columns}
  123. dataSource={dataSource}
  124. onChange={(...args) => console.log(...args)}
  125. // expandedRowRender={record => <article>{record.description}</article>}
  126. />
  127. </div>
  128. );
  129. }