Header.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. context: NavContextType;
  33. renderLogo(logo: React.ReactNode) {
  34. if (React.isValidElement(logo)) {
  35. return logo;
  36. }
  37. return null;
  38. }
  39. render() {
  40. const { children, style, className, logo, text, link, linkOptions, prefixCls } = this.props;
  41. const { isCollapsed } = this.context;
  42. const wrapCls = cls(className, `${cssClasses.PREFIX}-header`, {
  43. [`${cssClasses.PREFIX}-header-collapsed`]: isCollapsed,
  44. });
  45. let wrappedChildren = (
  46. <>
  47. {logo ? <i className={`${cssClasses.PREFIX}-header-logo`}>{this.renderLogo(logo)}</i> : null}
  48. {!isNullOrUndefined(text) && !isCollapsed ? (
  49. <span className={`${cssClasses.PREFIX}-header-text`}>{text}</span>
  50. ) : null}
  51. {children}
  52. </>
  53. );
  54. if (typeof link === 'string') {
  55. wrappedChildren = (
  56. <a className={`${prefixCls}-header-link`} href={link} {...(linkOptions as any)}>
  57. {wrappedChildren}
  58. </a>
  59. );
  60. }
  61. return (
  62. <div className={wrapCls} style={style}>
  63. {wrappedChildren}
  64. </div>
  65. );
  66. }
  67. }