All files / src/table TableBody.tsx

90.32% Statements 28/31
76% Branches 19/25
88.89% Functions 8/9
90.32% Lines 28/31

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                      3x                                       7x   7x     7x   150x 150x 116x             34x 34x   150x       150x 62x                 150x               7x   28x         28x         150x             150x     150x                                     7x   24x 24x           24x     7x   7x                         24x                             7x       206x    
import React, { forwardRef, Fragment } from "react";
import classNames from "classnames";
import { Text } from "../text";
import { TableBox } from "./TableBox";
import { TableProps } from "./TableProps";
import { getRowKeyFromRecordKey } from "./util/get-row-key-from-record-key";
import { useMiddleware } from "./util/use-middleware";
import { SlideTransition } from "../transition";
import { useConfig } from "../_util/config-context";
 
// 表格组件核心实现
export const TableBody = forwardRef(function TableBody(
  {
    columns,
    records,
    addons,
    recordKey,
    rowClassName,
    rowDisabled,
    topTip,
    bottomTip,
    boxStyle,
    disableHoverHighlight,
    disableTextOverflow,
    onScrollCapture = () => null,
  }: TableProps & {
    boxStyle?: React.CSSProperties;
    onScrollCapture?: (event: React.UIEvent<HTMLDivElement>) => void;
  },
  ref: React.Ref<HTMLDivElement>
) {
  const { classPrefix } = useConfig();
  // 键值的获取方式
  const getRowKey = getRowKeyFromRecordKey(recordKey);
 
  // 列渲染:(record, column) => { props, children }
  const renderColumn = useMiddleware(addons, "onInjectColumn")(
    (record, rowKey, recordIndex, column, columnIndex) => {
      let children = null;
      if (isFunction(column.render)) {
        children = column.render(
          record,
          rowKey,
          recordIndex,
          column,
          columnIndex
        );
      } else Eif (typeof record === "object" && record) {
        children = record[column.key];
      }
      Iif (typeof children === "undefined") {
        // never render undefined
        children = null;
      }
      if (typeof children !== "object") {
        children = (
          <Text
            overflow={!disableTextOverflow}
            title={disableTextOverflow ? undefined : String(children)}
          >
            {children}
          </Text>
        );
      }
      return {
        props: {},
        children,
      };
    }
  );
 
  // 行渲染:(columns, record) => { prepends, row, appends }
  const renderRow = useMiddleware(addons, "onInjectRow")(
    (record, rowKey, recordIndex, columns) => {
      const className = classNames(
        isFunction(rowClassName) ? rowClassName(record) : null,
        isFunction(rowDisabled) ? { "is-disabled": rowDisabled(record) } : null,
        { "no-hover": !!disableHoverHighlight }
      );
      return {
        prepends: [],
        row: (
          <tr key={rowKey} className={className || null}>
            {columns.map((column, index) => {
              const { props, children } = renderColumn(
                record,
                rowKey,
                recordIndex,
                column,
                index
              );
              Iif (props.colSpan === 0 || props.rowSpan === 0) {
                return null;
              }
              return (
                <Text
                  key={column.key}
                  parent="td"
                  {...props}
                  align={column.align}
                >
                  <div>{children}</div>
                </Text>
              );
            })}
          </tr>
        ),
        appends: [],
      };
    }
  );
 
  // 记录渲染:(record) => <tr>[]
  const renderRecord = (record: any, recordIndex: number) => {
    // 生成 rowKey
    const rowKey = getRowKey(record, recordIndex);
    const { prepends, row, appends } = renderRow(
      record,
      rowKey,
      recordIndex,
      columns
    );
    return [...prepends, row, ...appends].filter(Boolean);
  };
 
  const renderBody = useMiddleware(addons, "onInjectBody")(
    (records, columns, topTip, bottomTip) => (
      <div
        className={`${classPrefix}-table__body`}
        ref={ref}
        onScrollCapture={onScrollCapture}
      >
        <TableBox columns={columns} style={boxStyle} classPrefix={classPrefix}>
          <tbody>
            <SlideTransition in={Boolean(topTip)} exit={false}>
              <tr className={`${classPrefix}-table__tr--blank-new`}>
                <td colSpan={columns.length}>{topTip}</td>
              </tr>
            </SlideTransition>
            {(records || []).map((record, index) => (
              <Fragment key={getRowKey(record, index)}>
                {renderRecord(record, index)}
              </Fragment>
            ))}
            <SlideTransition in={Boolean(bottomTip)} exit={false}>
              <tr className={`${classPrefix}-table__tr--blank-new`}>
                <td colSpan={columns.length}>{bottomTip}</td>
              </tr>
            </SlideTransition>
          </tbody>
        </TableBox>
      </div>
    )
  );
 
  return renderBody(records, columns, topTip, bottomTip);
});
 
function isFunction(target: any): target is Function {
  return typeof target === "function";
}