Header.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React, { PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import cls from 'classnames';
  4. import isNullOrUndefined from '@douyinfe/semi-foundation/utils/isNullOrUndefined';
  5. import { cssClasses } from '@douyinfe/semi-foundation/navigation/constants';
  6. import '@douyinfe/semi-foundation/navigation/navigation.scss';
  7. import NavContext, { NavContextType } from './nav-context';
  8. import { BaseProps } from '../_base/baseComponent';
  9. export type Logo = React.ReactNode;
  10. export interface NavHeaderProps extends BaseProps {
  11. link?: string;
  12. linkOptions?: React.AnchorHTMLAttributes<HTMLAnchorElement>;
  13. logo?: Logo;
  14. prefixCls?: string;
  15. text?: React.ReactNode
  16. }
  17. export default class NavHeader extends PureComponent<NavHeaderProps> {
  18. static contextType = NavContext;
  19. static propTypes = {
  20. prefixCls: PropTypes.string,
  21. logo: PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.node]),
  22. text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  23. children: PropTypes.node,
  24. style: PropTypes.object,
  25. className: PropTypes.string,
  26. link: PropTypes.string,
  27. linkOptions: PropTypes.object,
  28. };
  29. static defaultProps = {
  30. prefixCls: cssClasses.PREFIX,
  31. };
  32. static elementType = "NavHeader";
  33. context: NavContextType;
  34. renderLogo(logo: React.ReactNode) {
  35. if (React.isValidElement(logo)) {
  36. return logo;
  37. }
  38. return null;
  39. }
  40. render() {
  41. const { children, style, className, logo, text, link, linkOptions, prefixCls } = this.props;
  42. const { isCollapsed } = this.context;
  43. const wrapCls = cls(className, `${cssClasses.PREFIX}-header`, {
  44. [`${cssClasses.PREFIX}-header-collapsed`]: isCollapsed,
  45. });
  46. let wrappedChildren = (
  47. <>
  48. {logo ? <i className={`${cssClasses.PREFIX}-header-logo`}>{this.renderLogo(logo)}</i> : null}
  49. {!isNullOrUndefined(text) && !isCollapsed ? (
  50. <span className={`${cssClasses.PREFIX}-header-text`}>{text}</span>
  51. ) : null}
  52. {children}
  53. </>
  54. );
  55. if (typeof link === 'string') {
  56. wrappedChildren = (
  57. <a className={`${prefixCls}-header-link`} href={link} {...(linkOptions as any)}>
  58. {wrappedChildren}
  59. </a>
  60. );
  61. }
  62. return (
  63. <div className={wrapCls} style={style}>
  64. {wrappedChildren}
  65. </div>
  66. );
  67. }
  68. }