image.png
image.png

TabList.js

  1. import React, { memo } from 'react';
  2. import { array, func, bool, number } from 'prop-types';
  3. import { Table } from 'antd';
  4. import { FormattedMessage } from 'react-intl';
  5. import classnames from 'classnames';
  6. import equal from 'fast-deep-equal';
  7. TableList.propTypes = {
  8. columns: array.isRequired,
  9. dataSource: array.isRequired,
  10. onChange: func,
  11. total: number,
  12. current: number,
  13. pageSize: number,
  14. pagination: bool,
  15. showSizeChanger: bool,
  16. };
  17. TableList.defaultProps = {
  18. current: 1,
  19. pageSize: 10,
  20. pagination: true,
  21. showSizeChanger: true,
  22. };
  23. function TableList(props) {
  24. const {
  25. dataSource, onChange, className,
  26. pagination, total,current, pageSize, showSizeChanger,
  27. ...rest
  28. } = props;
  29. const attrs = {
  30. rowKey: (_, i) => _.id || i,
  31. ...rest,
  32. dataSource,
  33. onChange,
  34. pagination:
  35. (pagination && dataSource.length)
  36. ? {
  37. total,
  38. current,
  39. pageSize,
  40. showSizeChanger,
  41. showQuickJumper: true,
  42. showTotal: (totalCount, [start, end]) => (
  43. <>
  44. {start}~{end},
  45. <FormattedMessage
  46. id="monitor.pagination.total"
  47. defaultMessage="{value} 条数据"
  48. values={{ value: totalCount }}
  49. />
  50. </>
  51. ),
  52. }
  53. : false,
  54. className: classnames(className),
  55. };
  56. return <Table {...attrs} />;
  57. }
  58. function propsEqual({ dataSource, columns, loading }, props) {
  59. return equal(dataSource, props.dataSource)
  60. && equal(columns, props.columns)
  61. && loading === props.loading;
  62. }
  63. export default memo(TableList, propsEqual);

TableList

components/Table/TableList.js

  1. import {memo} from 'react';
  2. import {array, func, bool, number, string} from 'prop-types';
  3. import { Table } from 'antd';
  4. import classnames from 'classnames';
  5. import equal from 'fast-deep-equal';
  6. TableList.propTypes = {
  7. columns: array.isRequired,
  8. dataSource: array.isRequired,
  9. onChange: func,
  10. expandedRowRender: func, // 嵌套子表格
  11. pagination: bool,
  12. loading: bool,
  13. showSizeChanger: bool,
  14. total: number,
  15. current: number,
  16. pageSize: number,
  17. size: oneOf(['default', 'middle', 'small']),
  18. }
  19. TableList.defaultProps = {
  20. pagination: true,
  21. loading: false,
  22. showSizeChanger: false,
  23. current: 1,
  24. pageSize: 10,
  25. size: 'default',
  26. }
  27. function TableList(props) {
  28. const {
  29. total, current, pageSize, showSizeChanger,
  30. pagination, className,
  31. ...rest,
  32. } = props;
  33. const paginationConfig = {
  34. total,
  35. current,
  36. pageSize,
  37. showSizeChanger,
  38. showQuickJumper: true,
  39. showTotal: (totalCount, [start, end]) => {
  40. return `${start}~${end}, ${totalCount} 条数据`;
  41. },
  42. }
  43. const attrs = {
  44. ...rest,
  45. rowKey: (record, index) => (record.id || index), // rowKey: 'id',
  46. pagination: pagination && total ? paginationConfig : false,
  47. className: classnames(className),
  48. };
  49. return (
  50. <Table {...attrs} />
  51. )
  52. };
  53. function propsEqual({ dataSource, columns }, props) {
  54. return equal(dataSource, props.dataSource) && equal(columns, props.columns);
  55. }
  56. export default memo(TableList, propsEqual);

onChange 参数

onChange时,page的参数
image.png

TableList的用法

  1. import { TableList, EditAction } from '@components/Table';
  2. const columns = [
  3. {
  4. title: <FormattedMessage id="common.name" defaultMessage="名称" />,
  5. dataIndex: 'name',
  6. },
  7. {
  8. title: <FormattedMessage id="common.type" defaultMessage="类型" />,
  9. dataIndex: 'type',
  10. },
  11. {
  12. title: <FormattedMessage id="common.desc" defaultMessage="描述" />,
  13. dataIndex: 'desc',
  14. },
  15. {
  16. title: <FormattedMessage id="common.creator" defaultMessage="创建人" />,
  17. dataIndex: 'creator',
  18. },
  19. {
  20. title: <FormattedMessage id="common.createTime" defaultMessage="创建时间" />,
  21. dataIndex: 'created_at',
  22. },
  23. {
  24. title: <FormattedMessage id="common.action" defaultMessage="操作" />,
  25. key: 'action',
  26. width: 160,
  27. render: (_, record) => <EditAction {...record}/>,
  28. },
  29. ];
  30. function App() {
  31. const pageChange = useCallback(values => {
  32. // console.log('page', values)
  33. }, []);
  34. return (
  35. <TableList
  36. dataSource={dataSource}
  37. columns={columns}
  38. onChange={pageChange}
  39. />
  40. )
  41. }
  42. export default App;