mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-05 01:19:45 +08:00
338ec7dad7
* pref: better code style for production * refactor `devWarning` * don't use `useEffect` only wrap `devWarning` * chore: add 'noop' to coverage * chore: add test cases for devWarning * chore: add test case * chore: update test cases for devWarning * chore: restore test script command * fix: remove 'throw new Error' * should not use `throw` for browser * chore: update test case for AutoComplete * perf: add prefix for `devWarning` * update RegExp for UMD * add prefix for ES and CJS * chore: better code style * perf: * upgrade antd-tools * remove `injectWarningCondition` * rename `devWarning` to `warning` * chore: better code style * chore: better code style * chore: restore hasValidName
118 lines
3.4 KiB
TypeScript
118 lines
3.4 KiB
TypeScript
import * as React from 'react';
|
|
import classNames from 'classnames';
|
|
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';
|
|
|
|
import { ConfigContext } 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 = 403 | 404 | 500 | '403' | '404' | '500';
|
|
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;
|
|
children?: React.ReactNode;
|
|
}
|
|
|
|
// 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}`)) {
|
|
const SVGComponent = ExceptionMap[status as ExceptionStatusType];
|
|
return (
|
|
<div className={`${className} ${prefixCls}-image`}>
|
|
<SVGComponent />
|
|
</div>
|
|
);
|
|
}
|
|
const iconNode = React.createElement(
|
|
IconMap[status as Exclude<ResultStatusType, ExceptionStatusType>],
|
|
);
|
|
|
|
return <div className={className}>{icon || iconNode}</div>;
|
|
};
|
|
|
|
const renderExtra = (prefixCls: string, { extra }: ResultProps) =>
|
|
extra && <div className={`${prefixCls}-extra`}>{extra}</div>;
|
|
|
|
export interface ResultType extends React.FC<ResultProps> {
|
|
PRESENTED_IMAGE_404: React.FC;
|
|
PRESENTED_IMAGE_403: React.FC;
|
|
PRESENTED_IMAGE_500: React.FC;
|
|
}
|
|
|
|
const Result: ResultType = ({
|
|
prefixCls: customizePrefixCls,
|
|
className: customizeClassName,
|
|
subTitle,
|
|
title,
|
|
style,
|
|
children,
|
|
status = 'info',
|
|
icon,
|
|
extra,
|
|
}) => {
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('result', customizePrefixCls);
|
|
const className = classNames(prefixCls, `${prefixCls}-${status}`, customizeClassName, {
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
});
|
|
return (
|
|
<div className={className} style={style}>
|
|
{renderIcon(prefixCls, { status, icon })}
|
|
<div className={`${prefixCls}-title`}>{title}</div>
|
|
{subTitle && <div className={`${prefixCls}-subtitle`}>{subTitle}</div>}
|
|
{renderExtra(prefixCls, { extra })}
|
|
{children && <div className={`${prefixCls}-content`}>{children}</div>}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
Result.PRESENTED_IMAGE_403 = ExceptionMap['403'];
|
|
Result.PRESENTED_IMAGE_404 = ExceptionMap['404'];
|
|
Result.PRESENTED_IMAGE_500 = ExceptionMap['500'];
|
|
|
|
export default Result;
|