index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React, { AriaRole, ComponentClass, CSSProperties } from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { cssClasses } from '@douyinfe/semi-foundation/layout/constants';
  5. import '@douyinfe/semi-foundation/layout/layout.scss';
  6. import LayoutContext, { ContextType } from './layout-context';
  7. import Sider from './Sider';
  8. export { ResponsiveMap, SiderProps } from './Sider';
  9. const htmlTag = {
  10. Header: 'header',
  11. Footer: 'footer',
  12. Content: 'main',
  13. Layout: 'section'
  14. };
  15. function generator<P extends { type?: string; tagName?: string; role?: AriaRole; 'aria-label'?: string }>(type: string): (ComponentType: ComponentClass<{ type?: string; tagName?: string } & P>) => ComponentClass<P> {
  16. const tagName = htmlTag[type];
  17. const typeName = type.toLowerCase();
  18. return (BasicComponent): ComponentClass<P> => class Adapter extends React.PureComponent<P> {
  19. render() {
  20. return <BasicComponent role={this.props.role} aria-label={this.props['aria-label']} type={typeName}
  21. tagName={tagName} {...this.props} />;
  22. }
  23. };
  24. }
  25. export interface BasicProps {
  26. prefixCls?: string;
  27. style?: CSSProperties;
  28. className?: string;
  29. tagName?: keyof HTMLElementTagNameMap;
  30. type?: string;
  31. children?: React.ReactNode | undefined;
  32. }
  33. class Basic extends React.PureComponent<BasicProps> {
  34. static propTypes = {
  35. prefixCls: PropTypes.string,
  36. style: PropTypes.object,
  37. className: PropTypes.string,
  38. };
  39. static defaultProps = {
  40. prefixCls: cssClasses.PREFIX,
  41. };
  42. render() {
  43. const { prefixCls, type, className, children, tagName, ...others } = this.props;
  44. const classString = cls(className, `${prefixCls}-${type}`);
  45. return React.createElement(tagName, { className: classString, ...others }, children);
  46. }
  47. }
  48. const Header = generator<BasicProps>('Header')(Basic);
  49. const Footer = generator<BasicProps>('Footer')(Basic);
  50. const Content = generator<BasicProps>('Content')(Basic);
  51. export interface BasicLayoutProps {
  52. prefixCls?: string;
  53. style?: CSSProperties;
  54. className?: string;
  55. children?: React.ReactNode | undefined;
  56. hasSider?: boolean;
  57. tagName?: keyof HTMLElementTagNameMap;
  58. }
  59. export interface BasicLayoutState {
  60. siders: Array<string>;
  61. }
  62. class Layout extends React.Component<BasicLayoutProps, BasicLayoutState> {
  63. static propTypes = {
  64. prefixCls: PropTypes.string,
  65. style: PropTypes.object,
  66. className: PropTypes.string,
  67. };
  68. static defaultProps = {
  69. prefixCls: cssClasses.PREFIX,
  70. tagName: 'section'
  71. };
  72. static Header = Header;
  73. static Footer = Footer;
  74. static Content = Content;
  75. static Sider = Sider;
  76. constructor(props: BasicLayoutProps) {
  77. super(props);
  78. this.state = {
  79. siders: [],
  80. };
  81. }
  82. getSiderHook(): ContextType['siderHook'] {
  83. return {
  84. addSider: (id: string): void => {
  85. this.setState(state => ({
  86. siders: [...state.siders, id],
  87. }));
  88. },
  89. removeSider: (id: string): void => {
  90. this.setState(state => ({
  91. siders: state.siders.filter(curr => curr !== id),
  92. }));
  93. },
  94. };
  95. }
  96. render() {
  97. const { prefixCls, className, children, hasSider, tagName, ...others } = this.props;
  98. const { siders } = this.state;
  99. const classString = cls(className, prefixCls, {
  100. [`${prefixCls}-has-sider`]: typeof hasSider === 'boolean' ? hasSider : siders.length > 0,
  101. });
  102. const Tag: any = tagName;
  103. return (
  104. <LayoutContext.Provider value={{ siderHook: this.getSiderHook() }}>
  105. <Tag className={classString} {...others}>
  106. {children}
  107. </Tag>
  108. </LayoutContext.Provider>
  109. );
  110. }
  111. }
  112. export { Layout };
  113. export default Layout;