2019-06-30 20:15:44 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classnames from 'classnames';
|
2020-03-02 12:09:38 +08:00
|
|
|
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
|
|
|
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
|
|
|
|
import WarningFilled from '@ant-design/icons/WarningFilled';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2019-06-30 20:15:44 +08:00
|
|
|
import { ConfigConsumerProps, ConfigConsumer } from '../config-provider';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2019-06-30 20:15:44 +08:00
|
|
|
import noFound from './noFound';
|
|
|
|
import serverError from './serverError';
|
|
|
|
import unauthorized from './unauthorized';
|
|
|
|
|
|
|
|
export const IconMap = {
|
2019-08-13 14:07:17 +08:00
|
|
|
success: CheckCircleFilled,
|
|
|
|
error: CloseCircleFilled,
|
|
|
|
info: ExclamationCircleFilled,
|
|
|
|
warning: WarningFilled,
|
2019-06-30 20:15:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const ExceptionMap = {
|
|
|
|
'404': noFound,
|
|
|
|
'500': serverError,
|
|
|
|
'403': unauthorized,
|
|
|
|
};
|
|
|
|
|
2020-02-28 23:41:20 +08:00
|
|
|
export type ExceptionStatusType = 403 | 404 | 500 | '403' | '404' | '500';
|
2019-06-30 20:15:44 +08:00
|
|
|
export type ResultStatusType = ExceptionStatusType | keyof typeof IconMap;
|
|
|
|
|
|
|
|
export interface ResultProps {
|
|
|
|
icon?: React.ReactNode;
|
2019-08-23 18:07:39 +08:00
|
|
|
status?: ResultStatusType;
|
2019-06-30 20:15:44 +08:00
|
|
|
title?: React.ReactNode;
|
|
|
|
subTitle?: React.ReactNode;
|
|
|
|
extra?: React.ReactNode;
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExceptionImageMap keys
|
|
|
|
const ExceptionStatus = Object.keys(ExceptionMap);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* render icon
|
|
|
|
* if ExceptionStatus includes ,render svg image
|
|
|
|
* else render iconNode
|
|
|
|
* @param prefixCls
|
|
|
|
* @param {status, icon}
|
|
|
|
*/
|
|
|
|
const renderIcon = (prefixCls: string, { status, icon }: ResultProps) => {
|
|
|
|
const className = classnames(`${prefixCls}-icon`);
|
|
|
|
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2019-12-13 10:18:12 +08:00
|
|
|
!(typeof icon === 'string' && icon.length > 2),
|
|
|
|
'Result',
|
|
|
|
`\`icon\` is using ReactNode instead of string naming in v4. Please check \`${icon}\` at https://ant.design/components/icon`,
|
|
|
|
);
|
|
|
|
|
2020-02-28 23:41:20 +08:00
|
|
|
if (ExceptionStatus.includes(`${status}`)) {
|
2019-06-30 20:15:44 +08:00
|
|
|
const SVGComponent = ExceptionMap[status as ExceptionStatusType];
|
|
|
|
return (
|
|
|
|
<div className={`${className} ${prefixCls}-image`}>
|
|
|
|
<SVGComponent />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
const iconNode = React.createElement(
|
|
|
|
IconMap[status as Exclude<ResultStatusType, ExceptionStatusType>],
|
|
|
|
);
|
2019-06-30 20:15:44 +08:00
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
return <div className={className}>{icon || iconNode}</div>;
|
2019-06-30 20:15:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const renderExtra = (prefixCls: string, { extra }: ResultProps) =>
|
|
|
|
extra && <div className={`${prefixCls}-extra`}>{extra}</div>;
|
|
|
|
|
2020-03-28 11:56:57 +08:00
|
|
|
export interface ResultType extends React.FC<ResultProps> {
|
2019-08-02 21:37:26 +08:00
|
|
|
PRESENTED_IMAGE_404: React.ReactNode;
|
|
|
|
PRESENTED_IMAGE_403: React.ReactNode;
|
|
|
|
PRESENTED_IMAGE_500: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Result: ResultType = props => (
|
2019-06-30 20:15:44 +08:00
|
|
|
<ConfigConsumer>
|
2020-01-02 19:10:16 +08:00
|
|
|
{({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2019-06-30 20:15:44 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2019-07-01 15:11:37 +08:00
|
|
|
className: customizeClassName,
|
2019-06-30 20:15:44 +08:00
|
|
|
subTitle,
|
|
|
|
title,
|
|
|
|
style,
|
|
|
|
children,
|
|
|
|
status,
|
|
|
|
} = props;
|
|
|
|
const prefixCls = getPrefixCls('result', customizePrefixCls);
|
2020-01-02 19:10:16 +08:00
|
|
|
const className = classnames(prefixCls, `${prefixCls}-${status}`, customizeClassName, {
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
2019-06-30 20:15:44 +08:00
|
|
|
return (
|
2019-07-01 15:11:37 +08:00
|
|
|
<div className={className} style={style}>
|
2019-06-30 20:15:44 +08:00
|
|
|
{renderIcon(prefixCls, props)}
|
|
|
|
<div className={`${prefixCls}-title`}>{title}</div>
|
|
|
|
{subTitle && <div className={`${prefixCls}-subtitle`}>{subTitle}</div>}
|
|
|
|
{renderExtra(prefixCls, props)}
|
2020-06-28 22:41:59 +08:00
|
|
|
{children && <div className={`${prefixCls}-content`}>{children}</div>}
|
2019-06-30 20:15:44 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
|
|
|
|
2019-08-02 21:37:26 +08:00
|
|
|
Result.defaultProps = {
|
2019-06-30 20:15:44 +08:00
|
|
|
status: 'info',
|
|
|
|
};
|
|
|
|
|
2020-06-08 18:01:50 +08:00
|
|
|
// eslint-disable-next-line prefer-destructuring
|
2019-08-02 21:37:26 +08:00
|
|
|
Result.PRESENTED_IMAGE_403 = ExceptionMap[403];
|
2020-06-08 18:01:50 +08:00
|
|
|
// eslint-disable-next-line prefer-destructuring
|
2019-08-02 21:37:26 +08:00
|
|
|
Result.PRESENTED_IMAGE_404 = ExceptionMap[404];
|
2020-06-08 18:01:50 +08:00
|
|
|
// eslint-disable-next-line prefer-destructuring
|
2019-08-02 21:37:26 +08:00
|
|
|
Result.PRESENTED_IMAGE_500 = ExceptionMap[500];
|
2019-06-30 20:15:44 +08:00
|
|
|
|
|
|
|
export default Result;
|