index.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import {Table as OriTable} from 'antd';
  2. import {ColumnsType} from 'antd/es/table';
  3. import {createPageContext, createSearchContext, usePage, useTableSearch} from '@hooks';
  4. import {PAGE_SIZE_LIST} from '@utils';
  5. import {FC} from 'react';
  6. type Props<T> = {
  7. testId?: string,
  8. columns: ColumnsType<T>,
  9. data: T[],
  10. pageContext: ReturnType<typeof createPageContext>,
  11. searchContext: ReturnType<typeof createSearchContext>,
  12. count: number,
  13. rowKey?: string,
  14. 'data-testid': string,
  15. scrollX?: number,
  16. };
  17. const Table: FC<Props<any>> = function<T extends Record<string, any>>(props: Props<T>) {
  18. const {
  19. columns,
  20. data,
  21. pageContext,
  22. searchContext,
  23. count,
  24. rowKey,
  25. scrollX,
  26. } = props;
  27. const [{page, pageSize}, {onPageChange}] = usePage(pageContext);
  28. const [isSearching] = useTableSearch(searchContext);
  29. return (
  30. <OriTable
  31. data-testid={props['data-testid']}
  32. columns={columns}
  33. dataSource={data}
  34. pagination={{
  35. pageSize,
  36. pageSizeOptions: PAGE_SIZE_LIST,
  37. total: count,
  38. showSizeChanger: true,
  39. onChange: onPageChange,
  40. current: page,
  41. }}
  42. rowKey={rowKey ?? 'id'}
  43. loading={isSearching}
  44. scroll={{x: scrollX}}
  45. bordered
  46. />
  47. );
  48. };
  49. export default Table;