import * as React from 'react'; import classnames from 'classnames'; import { CheckCircleFilled, CloseCircleFilled, ExclamationCircleFilled, WarningFilled, } from '@ant-design/icons'; import { ConfigConsumerProps, ConfigConsumer } from '../config-provider'; import warning from '../_util/warning'; import noFound from './noFound'; import serverError from './serverError'; import unauthorized from './unauthorized'; export const IconMap = { success: CheckCircleFilled, error: CloseCircleFilled, info: ExclamationCircleFilled, warning: WarningFilled, }; export const ExceptionMap = { '404': noFound, '500': serverError, '403': unauthorized, }; export type ExceptionStatusType = keyof typeof ExceptionMap; export type ResultStatusType = ExceptionStatusType | keyof typeof IconMap; export interface ResultProps { icon?: React.ReactNode; status?: ResultStatusType; 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`); 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`, ); if (ExceptionStatus.includes(status as ResultStatusType)) { const SVGComponent = ExceptionMap[status as ExceptionStatusType]; return (
); } const iconNode = React.createElement( IconMap[status as Exclude], ); return
{icon || iconNode}
; }; const renderExtra = (prefixCls: string, { extra }: ResultProps) => extra &&
{extra}
; export interface ResultType extends React.SFC { PRESENTED_IMAGE_404: React.ReactNode; PRESENTED_IMAGE_403: React.ReactNode; PRESENTED_IMAGE_500: React.ReactNode; } const Result: ResultType = props => ( {({ getPrefixCls }: ConfigConsumerProps) => { const { prefixCls: customizePrefixCls, className: customizeClassName, subTitle, title, style, children, status, } = props; const prefixCls = getPrefixCls('result', customizePrefixCls); const className = classnames(prefixCls, `${prefixCls}-${status}`, customizeClassName); return (
{renderIcon(prefixCls, props)}
{title}
{subTitle &&
{subTitle}
} {children &&
{children}
} {renderExtra(prefixCls, props)}
); }}
); Result.defaultProps = { status: 'info', }; Result.PRESENTED_IMAGE_403 = ExceptionMap[403]; Result.PRESENTED_IMAGE_404 = ExceptionMap[404]; Result.PRESENTED_IMAGE_500 = ExceptionMap[500]; export default Result;