index.js 1.23 KB
Newer Older
1
import React, { Component } from 'react';
2
import PropTypes from 'prop-types';
3 4 5 6
import classNames from 'classnames';
import styles from './index.less';

export default class FooterToolbar extends Component {
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  static contextTypes = {
    isMobile: PropTypes.bool,
  };

  state = {
    width: undefined,
  };

  componentDidMount() {
    window.addEventListener('resize', this.resizeFooterToolbar);
    this.resizeFooterToolbar();
  }

  componentWillUnmount() {
    window.removeEventListener('resize', this.resizeFooterToolbar);
  }

  resizeFooterToolbar = () => {
    const sider = document.querySelector('.ant-layout-sider');
    if (sider == null) {
      return;
    }
    const { isMobile } = this.context;
    const width = isMobile ? null : `calc(100% - ${sider.style.width})`;
    const { width: stateWidth } = this.state;
    if (stateWidth !== width) {
      this.setState({ width });
    }
  };

37
  render() {
38
    const { children, className, extra, ...restProps } = this.props;
39
    const { width } = this.state;
40
    return (
41
      <div className={classNames(className, styles.toolbar)} style={{ width }} {...restProps}>
42 43 44 45 46 47
        <div className={styles.left}>{extra}</div>
        <div className={styles.right}>{children}</div>
      </div>
    );
  }
}