All files / src/navmenu NavMenu.tsx

92.86% Statements 13/14
88.89% Branches 8/9
100% Functions 2/2
92.86% Lines 13/14

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                                                                                                          16x 16x                       16x 2x                             14x 2x             12x 5x               7x 7x                                         2x   5x 5x                              
import React from "react";
import classNames from "classnames";
import { StyledProps } from "../_type";
import { withStatics } from "../_util/with-statics";
import { Dropdown, DropdownProps } from "../dropdown";
import { useConfig } from "../_util/config-context";
 
export interface NavMenuItemProps extends StyledProps {
  /**
   * 菜单项类型
   *
   * - `default` 默认文字类型菜单项
   * - `logo` 导航 Logo
   * - `dropdown` 包含下拉的菜单项
   * - `icon` 图标类型菜单项
   *
   * @default "default"
   */
  type?: "default" | "logo" | "dropdown" | "icon";
 
  /**
   * 菜单项是否为选中样式
   *
   * @default false
   */
  selected?: boolean;
 
  /**
   * 菜单项点击回调
   */
  onClick?: (event: React.MouseEvent) => void;
 
  /**
   * 菜单项内容
   */
  children?: React.ReactNode;
 
  /**
   * `type="dropdown"` 时弹出层内容
   * @docType React.ReactNode | ((close: () => void) => React.ReactNode)
   */
  overlay?: DropdownProps["children"];
}
 
export function NavMenuItem({
  type = "default",
  selected,
  onClick,
  className,
  style,
  overlay,
  children,
}: NavMenuItemProps) {
  const { classPrefix } = useConfig();
  const wrapperProps = {
    className: classNames(className, {
      [`${classPrefix}-nav__service`]: type === "default",
      [`${classPrefix}-nav__logo`]: type === "logo",
      [`${classPrefix}-nav__dropdown`]: type === "dropdown",
      [`${classPrefix}-nav__operation`]: type === "icon",
      "is-selected": selected,
    }),
    style,
    onClick,
  };
 
  if (type === "dropdown") {
    return (
      <div {...wrapperProps}>
        <Dropdown
          trigger="hover"
          button={children}
          clickClose={false}
          boxClassName={`${classPrefix}-nav__dropdown-box`}
          placementOffset={0}
        >
          {overlay}
        </Dropdown>
      </div>
    );
  }
 
  if (type === "icon") {
    return (
      <div {...wrapperProps}>
        <a className={`${classPrefix}-nav__operation-inner`}>{children}</a>
      </div>
    );
  }
 
  if (type === "logo") {
    return (
      <div {...wrapperProps}>
        <a>{children}</a>
      </div>
    );
  }
 
  // default
  Eif (typeof children === "string") {
    return (
      <div {...wrapperProps}>
        <a>{children}</a>
      </div>
    );
  }
  return <div {...wrapperProps}>{children}</div>;
}
 
export interface NavMenuProps extends StyledProps {
  /**
   * 导航左侧 <NavMenu.Item />
   */
  left?: React.ReactNode;
 
  /**
   * 导航右侧 <NavMenu.Item />
   */
  right?: React.ReactNode;
}
 
export const NavMenu = withStatics(
  function NavMenu({ left, right, className, style }: NavMenuProps) {
    const { classPrefix } = useConfig();
    return (
      <nav
        className={classNames(`${classPrefix}-nav`, className)}
        style={style}
      >
        {left}
        <div className={`${classPrefix}-nav__flex-end`} />
        {right}
      </nav>
    );
  },
  {
    Item: NavMenuItem,
  }
);