All files / src/table/addons pageable.tsx

0% Statements 0/18
0% Branches 0/13
0% Functions 0/7
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                                                                                                                                                                             
import React, { useState } from "react";
import { TableAddon, TableProps } from "../TableProps";
import { Omit } from "../../_type";
import {
  Pagination,
  PaginationProps,
  PagingQuery,
  defaultPageSizeOptions,
} from "../../pagination/Pagination";
 
/**
 * `pageable` 插件用于支持表格分页
 */
export interface PageableOptions<Record = any>
  extends Omit<PaginationProps, "recordCount" | "className" | "style"> {
  /**
   * 数据总个数,用于计算页数
   * @default records.length
   */
  recordCount?: number;
}
 
export function pageable(options: PageableOptions = {}): TableAddon {
  return {
    onInjectTable: render => props => {
      const { records } = props;
      const { recordCount = records.length, ...paginationProps } = options;
      return (
        <TablePagination
          tableProps={props}
          tableRender={render}
          {...paginationProps}
          recordCount={recordCount}
        />
      );
    },
  };
}
 
function TablePagination({
  // Table
  tableRender,
  tableProps,
  // Pagination
  recordCount,
  pageSizeOptions,
  pageIndex,
  pageSize,
  onPagingChange = () => {},
  ...props
}: PaginationProps & {
  tableProps: TableProps<any>;
  tableRender: (props: TableProps<any>) => JSX.Element;
}) {
  if (!Array.isArray(pageSizeOptions) || pageSizeOptions.length === 0) {
    pageSizeOptions = defaultPageSizeOptions; // eslint-disable-line no-param-reassign
  }
 
  // 为非受控组件提供状态管理
  const [internalPaging, setInternalPaging] = useState<PagingQuery>({
    pageIndex: 1,
    pageSize: pageSizeOptions[0],
  });
 
  // 非受控组件,使用内部状态
  if (typeof pageIndex === "undefined") {
    // eslint-disable-next-line prefer-destructuring,no-param-reassign
    pageIndex = internalPaging.pageIndex;
  }
  if (typeof pageSize === "undefined") {
    // eslint-disable-next-line prefer-destructuring,no-param-reassign
    pageSize = internalPaging.pageSize;
  }
 
  // 状态改变同时更新内部状态和通知外部
  // eslint-disable-next-line no-param-reassign
  onPagingChange = (onPagingChange => (query: PagingQuery) => {
    setInternalPaging(query);
    onPagingChange(query);
  })(onPagingChange);
 
  const { records } = tableProps;
  return (
    <>
      {tableRender({
        ...tableProps,
        records:
          records.length < recordCount
            ? records
            : records.slice((pageIndex - 1) * pageSize, pageIndex * pageSize),
      })}
      <Pagination
        {...props}
        recordCount={recordCount}
        pageSizeOptions={pageSizeOptions}
        pageIndex={pageIndex}
        pageSize={pageSize}
        onPagingChange={onPagingChange}
      />
    </>
  );
}