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';
|
2019-12-13 10:18:12 +08:00
|
|
|
import warning from '../_util/warning';
|
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`);
|
|
|
|
|
2019-12-13 10:18:12 +08:00
|
|
|
warning(
|
|
|
|
!(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>;
|
|
|
|
|
2019-08-02 21:37:26 +08:00
|
|
|
export interface ResultType extends React.SFC<ResultProps> {
|
|
|
|
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>}
|
|
|
|
{children && <div className={`${prefixCls}-content`}>{children}</div>}
|
|
|
|
{renderExtra(prefixCls, props)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
|
|
|
|
2019-08-02 21:37:26 +08:00
|
|
|
Result.defaultProps = {
|
2019-06-30 20:15:44 +08:00
|
|
|
status: 'info',
|
|
|
|
};
|
|
|
|
|
2019-08-02 21:37:26 +08:00
|
|
|
Result.PRESENTED_IMAGE_403 = ExceptionMap[403];
|
|
|
|
Result.PRESENTED_IMAGE_404 = ExceptionMap[404];
|
|
|
|
Result.PRESENTED_IMAGE_500 = ExceptionMap[500];
|
2019-06-30 20:15:44 +08:00
|
|
|
|
|
|
|
export default Result;
|