Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve Empty component again #15487

Merged
merged 17 commits into from
Mar 20, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 60 additions & 61 deletions components/empty/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React from 'react';
import classNames from 'classnames';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
Expand All @@ -9,78 +9,77 @@ export interface TransferLocale {
description: string;
}

type ImageMode = 'default' | 'simple' | React.ReactNode;
type ImageType =
| string
| {
src?: string;
style?: React.CSSProperties;
}
| React.ReactNode;

export interface EmptyProps {
prefixCls?: string;
className?: string;
style?: React.CSSProperties;
image?: ImageMode;
image?: ImageType;
description?: React.ReactNode;
/**
* @since 3.16.0
*/
imageSize?: number;
children?: React.ReactNode;
}

const Empty: React.SFC<EmptyProps> = (props: EmptyProps) => (
<ConfigConsumer>
{({ getPrefixCls }: ConfigConsumerProps) => {
const {
className,
prefixCls: customizePrefixCls,
image = 'default',
description,
imageSize,
children,
...restProps
} = props;
const prefixCls = getPrefixCls('empty', customizePrefixCls);
export default class Empty extends React.Component<EmptyProps, any> {
DiamondYuan marked this conversation as resolved.
Show resolved Hide resolved
static PRESENTED_IMAGE_DEFAULT = defaultEmptyImg;

return (
<LocaleReceiver componentName="Empty">
{(locale: TransferLocale) => {
const des = description || locale.description;
const alt = typeof des === 'string' ? des : 'empty';
static PRESENTED_IMAGE_SIMPLE = simpleEmptyImg;

let imageNode: React.ReactNode = null;
if (typeof image === 'string') {
switch (image) {
case 'default': {
imageNode = <img alt={alt} src={defaultEmptyImg} />;
break;
}
case 'simple': {
imageNode = <img alt={alt} src={simpleEmptyImg} />;
break;
}
default: {
render() {
return (
<ConfigConsumer>
{({ getPrefixCls }: ConfigConsumerProps) => {
const {
className,
prefixCls: customizePrefixCls,
image = defaultEmptyImg,
description,
children,
...restProps
} = this.props;
const prefixCls = getPrefixCls('empty', customizePrefixCls);

return (
<LocaleReceiver componentName="Empty">
{(locale: TransferLocale) => {
const des = description || locale.description;
const alt = typeof des === 'string' ? des : 'empty';

let imageNode: React.ReactNode = null;
let imageStyle: React.CSSProperties = {};

if (typeof image === 'string') {
imageNode = <img alt={alt} src={image} />;
} else if (!React.isValidElement(image)) {
const { src, style } = image;
imageNode = <img alt={alt} src={src || defaultEmptyImg} />;
imageStyle = style;
} else {
imageNode = image;
DiamondYuan marked this conversation as resolved.
Show resolved Hide resolved
}
}
} else {
imageNode = image;
}

return (
<div className={classNames(prefixCls, className)} {...restProps}>
<div
style={imageSize ? { height: `${imageSize}px` } : undefined}
className={`${prefixCls}-image`}
>
{imageNode}
</div>
return (
<div className={classNames(prefixCls, className)} {...restProps}>
<div className={`${prefixCls}-image`} style={imageStyle}>
{imageNode}
</div>

<p className={`${prefixCls}-description`}>{des}</p>
<p className={`${prefixCls}-description`}>{des}</p>

{children && <div className={`${prefixCls}-footer`}>{children}</div>}
</div>
);
}}
</LocaleReceiver>
);
}}
</ConfigConsumer>
);

export default Empty;
{children && <div className={`${prefixCls}-footer`}>{children}</div>}
</div>
);
}}
</LocaleReceiver>
);
}}
</ConfigConsumer>
);
}
}