All files / src/grid Grid.tsx

100% Statements 18/18
100% Branches 14/14
100% Functions 2/2
100% Lines 18/18

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                                                                                              5x                   16x 16x 16x     16x 2x 2x           16x         16x         16x               48x 48x   48x 48x 5x           48x         48x         48x            
import React, { useContext } from "react";
import classNames from "classnames";
import { StyledProps } from "../_type";
import { useConfig } from "../_util/config-context";
 
export interface RowProps extends StyledProps {
  /**
   * 列之间的间隙
   * @default 20
   */
  gap?: number;
 
  /**
   * 栅格对齐方式,不传则栅格等高
   */
  verticalAlign?: "top" | "middle" | "bottom";
 
  /**
   * 是否展示分割线
   * @default false
   */
  showSplitLine?: boolean;
 
  /**
   * 包括的栅格列,请使用 <Col /> 作为子节点
   */
  children?: ColChild | ColChild[];
}
 
export interface ColProps extends StyledProps {
  /**
   * 栅格占位格数
   */
  span?: number;
 
  /**
   * 栅格单元中内容
   */
  children?: React.ReactNode;
}
 
type ColChild = React.ReactElement<ColProps, typeof Col>;
 
interface GridContextValue {
  gap: number;
}
 
const GridContext = React.createContext<GridContextValue>({ gap: 20 });
 
export function Row({
  gap,
  verticalAlign,
  showSplitLine,
  children,
  className,
  style,
}: RowProps) {
  const { classPrefix } = useConfig();
  let grid: GridContextValue = null;
  let rowStyle: React.CSSProperties = null;
 
  // 定义了 gap 的,才生成一个 grid 上下文,否则使用默认样式即可
  if (typeof gap === "number") {
    grid = { gap };
    rowStyle = {
      marginLeft: -gap / 2,
      marginRight: -gap / 2,
    };
  }
 
  rowStyle = {
    ...(rowStyle || {}),
    ...(style || {}),
  };
 
  const rowClassName = classNames(`${classPrefix}-grid`, className, {
    [`${classPrefix}-grid--split-line`]: showSplitLine,
    [`${classPrefix}-vertical--${verticalAlign}`]: verticalAlign,
  });
 
  return (
    <div className={rowClassName} style={rowStyle}>
      <GridContext.Provider value={grid}>{children}</GridContext.Provider>
    </div>
  );
}
 
export function Col({ span, className, style, children }: ColProps) {
  const { classPrefix } = useConfig();
  const grid = useContext(GridContext);
 
  let colStyle: React.CSSProperties = null;
  if (grid) {
    colStyle = {
      paddingLeft: grid.gap / 2,
      paddingRight: grid.gap / 2,
    };
  }
 
  const colClassName = classNames(
    `${classPrefix}-grid__item${span ? `-${span}` : ""}`,
    className
  );
 
  colStyle = {
    ...(colStyle || {}),
    ...(style || {}),
  };
 
  return (
    <div className={colClassName} style={colStyle}>
      <div className={`${classPrefix}-grid__box`}>{children}</div>
    </div>
  );
}