ant-design/components/empty/index.tsx

85 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-12-26 16:01:00 +08:00
import classNames from 'classnames';
2022-06-22 14:57:09 +08:00
import * as React from 'react';
import { ConfigContext } from '../config-provider';
2018-12-26 16:01:00 +08:00
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import DefaultEmptyImg from './empty';
import SimpleEmptyImg from './simple';
const defaultEmptyImg = <DefaultEmptyImg />;
const simpleEmptyImg = <SimpleEmptyImg />;
2018-12-26 16:01:00 +08:00
export interface TransferLocale {
description: string;
}
2018-12-26 16:01:00 +08:00
export interface EmptyProps {
prefixCls?: string;
className?: string;
style?: React.CSSProperties;
/** @since 3.16.0 */
imageStyle?: React.CSSProperties;
image?: React.ReactNode;
description?: React.ReactNode;
2018-12-26 16:01:00 +08:00
children?: React.ReactNode;
}
interface EmptyType extends React.FC<EmptyProps> {
PRESENTED_IMAGE_DEFAULT: React.ReactNode;
PRESENTED_IMAGE_SIMPLE: React.ReactNode;
}
const Empty: EmptyType = ({
className,
prefixCls: customizePrefixCls,
image = defaultEmptyImg,
description,
children,
imageStyle,
...restProps
}) => {
const { getPrefixCls, direction } = React.useContext(ConfigContext);
2018-12-26 16:01:00 +08:00
return (
<LocaleReceiver componentName="Empty">
{contextLocale => {
const prefixCls = getPrefixCls('empty', customizePrefixCls);
const des = typeof description !== 'undefined' ? description : contextLocale.description;
const alt = typeof des === 'string' ? des : 'empty';
2018-12-26 16:01:00 +08:00
let imageNode: React.ReactNode = null;
if (typeof image === 'string') {
imageNode = <img alt={alt} src={image} />;
} else {
imageNode = image;
}
2018-12-26 16:01:00 +08:00
return (
<div
className={classNames(
prefixCls,
{
[`${prefixCls}-normal`]: image === simpleEmptyImg,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
)}
{...restProps}
>
<div className={`${prefixCls}-image`} style={imageStyle}>
{imageNode}
</div>
{des && <div className={`${prefixCls}-description`}>{des}</div>}
{children && <div className={`${prefixCls}-footer`}>{children}</div>}
</div>
);
}}
</LocaleReceiver>
);
};
2018-12-26 16:01:00 +08:00
Empty.PRESENTED_IMAGE_DEFAULT = defaultEmptyImg;
Empty.PRESENTED_IMAGE_SIMPLE = simpleEmptyImg;
2018-12-26 16:01:00 +08:00
export default Empty;