index.tsx 3.8 KB

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