All files / src/drawer Drawer.tsx

62.5% Statements 10/16
38.46% Branches 10/26
25% Functions 1/4
66.67% Lines 10/15

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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                                                                                                                                                                                                                      1x 1x 1x 1x   1x 1x 1x   1x                                                                                                                                               1x         1x        
import React, { useRef } from "react";
import ReactDOM from "react-dom";
import classNames from "classnames";
import { createRocket } from "../_util/create-rocket";
import { StyledProps } from "../_type";
import { Justify } from "../justify";
import { Button } from "../button";
import { SlideTransition } from "../transition";
import { useOutsideClick } from "../_util/use-outside-click";
import { H3 } from "../heading";
import { useConfig } from "../_util/config-context";
 
export interface DrawerProps extends StyledProps {
  /**
   * Drawer 是否可见
   */
  visible: boolean;
 
  /**
   * 点击关闭图标或抽屉外区域的回调
   */
  onClose: () => void;
 
  /**
   * 抽屉方向
   * @default "right"
   */
  placement?: "right" | "left";
 
  /**
   * 抽屉大小
   *
   * - `"m"` - 360px
   * - `"l"` - 800px
   *
   * @default "m"
   */
  size?: "m" | "l";
 
  /**
   * Drawer 中的内容
   */
  children?: React.ReactNode;
 
  /**
   * Drawer 底部内容
   */
  footer?: React.ReactNode;
 
  /**
   * 头部标题
   */
  title?: React.ReactNode;
 
  /**
   * 头部副标题/说明文字
   */
  subtitle?: React.ReactNode;
 
  /**
   * **\[Deprecated\]** 请使用 `subtitle` 属性
   *
   * @deprecated
   */
  subTitle?: React.ReactNode;
 
  /**
   * 头部右侧渲染内容
   */
  extra?: React.ReactNode;
 
  /**
   * 点击面板外是否收起面板
   * @default true
   */
  outerClickClosable?: boolean;
 
  /**
   * 是否禁用头部关闭图标
   * @default false
   */
  disableCloseIcon?: boolean;
 
  /**
   * 是否禁用展开/收起动效
   * @default false
   */
  disableAnimation?: boolean;
}
 
export function Drawer({
  className,
  style,
  children,
  visible,
  onClose = () => null,
  size,
  placement,
  footer,
  title,
  subTitle,
  subtitle = subTitle,
  extra,
  disableCloseIcon,
  disableAnimation,
  outerClickClosable = true,
}: DrawerProps) {
  const { classPrefix } = useConfig();
  const ref = useRef(null);
  const { listen, ignoreProps } = useOutsideClick(ref);
  listen(() => outerClickClosable && visible && onClose());
 
  const width = size === "l" ? 800 : 320;
  const from = placement === "left" ? 0 - width : width;
  const showHeader = title || subtitle || extra || !disableCloseIcon;
 
  return ReactDOM.createPortal(
    <SlideTransition
      in={visible}
      from={[from, 0]}
      timeout={disableAnimation ? 0 : undefined}
    >
      <div
        ref={ref}
        style={style}
        className={classNames(`${classPrefix}-drawer`, className, {
          [`${classPrefix}-drawer--left`]: placement === "left",
          "size-l": size === "l",
        })}
        {...ignoreProps}
      >
        {showHeader && (
          <div className={`${classPrefix}-drawer__header`}>
            <Justify
              left={
                <DrawerTitle
                  title={title}
                  subtitle={subtitle}
                  classPrefix={classPrefix}
                />
              }
              right={
                <>
                  {extra}
                  {!disableCloseIcon && (
                    <Button icon="close" onClick={onClose} />
                  )}
                </>
              }
            />
          </div>
        )}
        <DrawerBody>{children}</DrawerBody>
        {footer && <DrawerFooter>{footer}</DrawerFooter>}
      </div>
    </SlideTransition>,
    document.body
  );
}
 
function DrawerTitle({
  title,
  subtitle,
  classPrefix,
}: Pick<DrawerProps, "title" | "subtitle"> & { classPrefix: string }) {
  const sub =
    typeof subtitle === "string" ? (
      <span className={`${classPrefix}-card__subtitle`}>{subtitle}</span>
    ) : (
      subtitle
    );
 
  if (typeof title === "string") {
    return (
      <H3>
        {title}
        {sub}
      </H3>
    );
  }
  return (
    <>
      {title}
      {sub}
    </>
  );
}
 
const DrawerBody = createRocket(
  "DrawerBody",
  "div.@{prefix}-drawer__body",
  "div.@{prefix}-drawer__body-inner"
);
const DrawerFooter = createRocket(
  "DrawerFooter",
  "div.@{prefix}-drawer__footer"
);