index.tsx 2.06 KB
Newer Older
xiaohuoni's avatar
xiaohuoni committed
1
import * as H from 'history';
陈帅's avatar
陈帅 committed
2

xiaohuoni's avatar
xiaohuoni committed
3
import React, { createElement } from 'react';
陈帅's avatar
陈帅 committed
4 5

import { Button } from 'antd';
陈帅's avatar
陈帅 committed
6
import Link from 'umi/link';
陈帅's avatar
陈帅 committed
7
import classNames from 'classnames';
xiaohuoni's avatar
xiaohuoni committed
8
import config from './typeConfig';
陈帅's avatar
陈帅 committed
9
import styles from './index.less';
xiaohuoni's avatar
xiaohuoni committed
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

export interface ExceptionProps<
  L = {
    to: H.LocationDescriptor;
    href?: H.LocationDescriptor;
    replace?: boolean;
    innerRef?: (node: HTMLAnchorElement | null) => void;
  }
> {
  type?: '403' | '404' | '500';
  title?: React.ReactNode;
  desc?: React.ReactNode;
  img?: string;
  actions?: React.ReactNode;
  linkElement?: string | React.ComponentType<L> | typeof Link;
  style?: React.CSSProperties;
  className?: string;
  backText?: React.ReactNode;
  redirect?: string;
}

class Exception extends React.Component<ExceptionProps, any> {
  static defaultProps = {
    backText: 'back to home',
    redirect: '/',
  };

  constructor(props: ExceptionProps) {
    super(props);
    this.state = {};
  }

  render() {
    const {
      className,
      backText,
      linkElement = 'a',
      type = '404',
      title,
      desc,
      img,
      actions,
      redirect,
      ...rest
    } = this.props;
    const pageType = type in config ? type : '404';
    const clsString = classNames(styles.exception, className);
    return (
      <div className={clsString} {...rest}>
        <div className={styles.imgBlock}>
          <div
            className={styles.imgEle}
            style={{ backgroundImage: `url(${img || config[pageType].img})` }}
          />
        </div>
        <div className={styles.content}>
          <h1>{title || config[pageType].title}</h1>
          <div className={styles.desc}>{desc || config[pageType].desc}</div>
          <div className={styles.actions}>
            {actions ||
              createElement(
                linkElement as any,
                {
                  to: redirect,
                  href: redirect,
                },
陈帅's avatar
陈帅 committed
76
                <Button type="primary">{backText}</Button>,
xiaohuoni's avatar
xiaohuoni committed
77 78 79 80 81 82 83 84 85
              )}
          </div>
        </div>
      </div>
    );
  }
}

export default Exception;