Header.tsx 2.4 KB

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