index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import React, { CSSProperties, ComponentClass } 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 }>(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 type={typeName} tagName={tagName} {...this.props} />;
  21. }
  22. };
  23. }
  24. export interface BasicProps {
  25. prefixCls?: string;
  26. style?: CSSProperties;
  27. className?: string;
  28. tagName?: keyof HTMLElementTagNameMap;
  29. type?: string;
  30. }
  31. class Basic extends React.PureComponent<BasicProps> {
  32. static propTypes = {
  33. prefixCls: PropTypes.string,
  34. style: PropTypes.object,
  35. className: PropTypes.string,
  36. };
  37. static defaultProps = {
  38. prefixCls: cssClasses.PREFIX,
  39. };
  40. render() {
  41. const { prefixCls, type, className, children, tagName, ...others } = this.props;
  42. const classString = cls(className, `${prefixCls}-${type}`);
  43. return React.createElement(tagName, { className: classString, ...others }, children);
  44. }
  45. }
  46. const Header = generator<BasicProps>('Header')(Basic);
  47. const Footer = generator<BasicProps>('Footer')(Basic);
  48. const Content = generator<BasicProps>('Content')(Basic);
  49. export interface BasicLayoutProps {
  50. prefixCls?: string;
  51. style?: CSSProperties;
  52. className?: string;
  53. hasSider?: boolean;
  54. tagName?: keyof HTMLElementTagNameMap;
  55. }
  56. export interface BasicLayoutState {
  57. siders: Array<string>;
  58. }
  59. class Layout extends React.Component<BasicLayoutProps, BasicLayoutState> {
  60. static propTypes = {
  61. prefixCls: PropTypes.string,
  62. style: PropTypes.object,
  63. className: PropTypes.string,
  64. };
  65. static defaultProps = {
  66. prefixCls: cssClasses.PREFIX,
  67. tagName: 'section'
  68. };
  69. static Header = Header;
  70. static Footer = Footer;
  71. static Content = Content;
  72. static Sider = Sider;
  73. constructor(props: BasicLayoutProps) {
  74. super(props);
  75. this.state = {
  76. siders: [],
  77. };
  78. }
  79. getSiderHook(): ContextType['siderHook'] {
  80. return {
  81. addSider: (id: string): void => {
  82. this.setState(state => ({
  83. siders: [...state.siders, id],
  84. }));
  85. },
  86. removeSider: (id: string): void => {
  87. this.setState(state => ({
  88. siders: state.siders.filter(curr => curr !== id),
  89. }));
  90. },
  91. };
  92. }
  93. render() {
  94. const { prefixCls, className, children, hasSider, tagName, ...others } = this.props;
  95. const { siders } = this.state;
  96. const classString = cls(className, prefixCls, {
  97. [`${prefixCls}-has-sider`]: typeof hasSider === 'boolean' ? hasSider : siders.length > 0,
  98. });
  99. const Tag: any = tagName;
  100. return (
  101. <LayoutContext.Provider value={{ siderHook: this.getSiderHook() }}>
  102. <Tag className={classString} {...others}>
  103. {children}
  104. </Tag>
  105. </LayoutContext.Provider>
  106. );
  107. }
  108. }
  109. export { Layout };
  110. export default Layout;