List.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React from 'react';
  2. import { withRouter } from 'react-router-dom';
  3. import { Button, Table } from 'antd';
  4. import { Resizable } from 'react-resizable';
  5. // class ResizeableTitle extends React.Component {
  6. // constructor(props) {
  7. // super(props);
  8. // }
  9. //
  10. // }
  11. const ResizeableTitle = (props) => {
  12. const { onResize, width, ...restProps } = props;
  13. if (!width) {
  14. return <th {...restProps} />;
  15. }
  16. return (
  17. <Resizable
  18. width={width}
  19. height={0}
  20. handle={
  21. <span
  22. className="react-resizable-handle"
  23. onClick={(e) => {
  24. e.stopPropagation();
  25. }}
  26. />
  27. }
  28. onResize={onResize}
  29. draggableOpts={{ enableUserSelectHack: false }}
  30. >
  31. <th {...restProps} />
  32. </Resizable>
  33. );
  34. };
  35. export default class List extends React.Component {
  36. state = {
  37. columns: [
  38. {
  39. title: 'Date',
  40. dataIndex: 'date',
  41. width: 200,
  42. },
  43. {
  44. title: 'Amount',
  45. dataIndex: 'amount',
  46. width: 100,
  47. sorter: (a, b) => a.amount - b.amount,
  48. },
  49. {
  50. title: 'Type',
  51. dataIndex: 'type',
  52. width: 100,
  53. },
  54. {
  55. title: 'Note',
  56. dataIndex: 'note',
  57. width: 100,
  58. },
  59. {
  60. title: 'Action',
  61. key: 'action',
  62. render: () => <a>Delete</a>,
  63. },
  64. ],
  65. };
  66. components = {
  67. header: {
  68. cell: ResizeableTitle,
  69. },
  70. };
  71. data = [
  72. {
  73. key: 0,
  74. date: '2018-02-11',
  75. amount: 120,
  76. type: 'income',
  77. note: 'transfer',
  78. },
  79. {
  80. key: 1,
  81. date: '2018-03-11',
  82. amount: 243,
  83. type: 'income',
  84. note: 'transfer',
  85. },
  86. {
  87. key: 2,
  88. date: '2018-04-11',
  89. amount: 98,
  90. type: 'income',
  91. note: 'transfer',
  92. },
  93. ];
  94. handleResize = (index) => (e, { size }) => {
  95. this.setState(({ columns }) => {
  96. const nextColumns = [...columns];
  97. nextColumns[index] = {
  98. ...nextColumns[index],
  99. width: size.width,
  100. };
  101. return { columns: nextColumns };
  102. });
  103. };
  104. render() {
  105. const columns = this.state.columns.map((col, index) => ({
  106. ...col,
  107. onHeaderCell: (column) => ({
  108. width: column.width,
  109. onResize: this.handleResize(index),
  110. }),
  111. }));
  112. return (
  113. <Table
  114. className={'list-wrapper'}
  115. bordered
  116. components={this.components}
  117. columns={columns}
  118. dataSource={this.data}
  119. />
  120. );
  121. }
  122. }