refactor(result): useContext and improve code (#27128)

* refactor(result): useContext and improve code

* Update index.tsx

* Update index.tsx
pull/27145/head
Tom Xu 4 years ago committed by GitHub
parent f6769e3e94
commit 2beb594b71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,7 +5,7 @@ import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled'; import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
import WarningFilled from '@ant-design/icons/WarningFilled'; import WarningFilled from '@ant-design/icons/WarningFilled';
import { ConfigConsumerProps, ConfigConsumer } from '../config-provider'; import { ConfigContext } from '../config-provider';
import devWarning from '../_util/devWarning'; import devWarning from '../_util/devWarning';
import noFound from './noFound'; import noFound from './noFound';
@ -66,7 +66,6 @@ const renderIcon = (prefixCls: string, { status, icon }: ResultProps) => {
</div> </div>
); );
} }
const iconNode = React.createElement( const iconNode = React.createElement(
IconMap[status as Exclude<ResultStatusType, ExceptionStatusType>], IconMap[status as Exclude<ResultStatusType, ExceptionStatusType>],
); );
@ -83,44 +82,36 @@ export interface ResultType extends React.FC<ResultProps> {
PRESENTED_IMAGE_500: React.ReactNode; PRESENTED_IMAGE_500: React.ReactNode;
} }
const Result: ResultType = props => ( const Result: ResultType = ({
<ConfigConsumer> prefixCls: customizePrefixCls,
{({ getPrefixCls, direction }: ConfigConsumerProps) => { className: customizeClassName,
const { subTitle,
prefixCls: customizePrefixCls, title,
className: customizeClassName, style,
subTitle, children,
title, status = 'info',
style, icon,
children, extra,
status, }) => {
} = props; const { getPrefixCls, direction } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('result', customizePrefixCls);
const className = classNames(prefixCls, `${prefixCls}-${status}`, customizeClassName, { const prefixCls = getPrefixCls('result', customizePrefixCls);
[`${prefixCls}-rtl`]: direction === 'rtl', const className = classNames(prefixCls, `${prefixCls}-${status}`, customizeClassName, {
}); [`${prefixCls}-rtl`]: direction === 'rtl',
return ( });
<div className={className} style={style}> return (
{renderIcon(prefixCls, props)} <div className={className} style={style}>
<div className={`${prefixCls}-title`}>{title}</div> {renderIcon(prefixCls, { status, icon })}
{subTitle && <div className={`${prefixCls}-subtitle`}>{subTitle}</div>} <div className={`${prefixCls}-title`}>{title}</div>
{renderExtra(prefixCls, props)} {subTitle && <div className={`${prefixCls}-subtitle`}>{subTitle}</div>}
{children && <div className={`${prefixCls}-content`}>{children}</div>} {renderExtra(prefixCls, { extra })}
</div> {children && <div className={`${prefixCls}-content`}>{children}</div>}
); </div>
}} );
</ConfigConsumer>
);
Result.defaultProps = {
status: 'info',
}; };
// eslint-disable-next-line prefer-destructuring Result.PRESENTED_IMAGE_403 = ExceptionMap['403'];
Result.PRESENTED_IMAGE_403 = ExceptionMap[403]; Result.PRESENTED_IMAGE_404 = ExceptionMap['404'];
// eslint-disable-next-line prefer-destructuring Result.PRESENTED_IMAGE_500 = ExceptionMap['500'];
Result.PRESENTED_IMAGE_404 = ExceptionMap[404];
// eslint-disable-next-line prefer-destructuring
Result.PRESENTED_IMAGE_500 = ExceptionMap[500];
export default Result; export default Result;

Loading…
Cancel
Save