2019-11-21 20:23:56 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import Title, { SkeletonTitleProps } from './Title';
|
|
|
|
import Paragraph, { SkeletonParagraphProps } from './Paragraph';
|
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2019-12-03 13:33:04 +08:00
|
|
|
import Element from './Element';
|
|
|
|
import SkeletonAvatar, { AvatarProps } from './Avatar';
|
2020-04-14 10:30:14 +08:00
|
|
|
import SkeletonButton from './Button';
|
2019-12-19 11:56:50 +08:00
|
|
|
import SkeletonInput from './Input';
|
2020-06-28 22:41:59 +08:00
|
|
|
import SkeletonImage from './Image';
|
2019-12-03 13:33:04 +08:00
|
|
|
|
|
|
|
/* This only for skeleton internal. */
|
2020-06-28 22:41:59 +08:00
|
|
|
interface SkeletonAvatarProps extends Omit<AvatarProps, 'active'> {}
|
2019-11-21 20:23:56 +08:00
|
|
|
|
|
|
|
export interface SkeletonProps {
|
|
|
|
active?: boolean;
|
|
|
|
loading?: boolean;
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
children?: React.ReactNode;
|
2019-12-03 13:33:04 +08:00
|
|
|
avatar?: SkeletonAvatarProps | boolean;
|
2019-11-21 20:23:56 +08:00
|
|
|
title?: SkeletonTitleProps | boolean;
|
|
|
|
paragraph?: SkeletonParagraphProps | boolean;
|
2020-05-16 13:19:54 +08:00
|
|
|
round?: boolean;
|
2019-11-21 20:23:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function getComponentProps<T>(prop: T | boolean | undefined): T | {} {
|
|
|
|
if (prop && typeof prop === 'object') {
|
|
|
|
return prop;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:33:04 +08:00
|
|
|
function getAvatarBasicProps(hasTitle: boolean, hasParagraph: boolean): SkeletonAvatarProps {
|
2019-11-21 20:23:56 +08:00
|
|
|
if (hasTitle && !hasParagraph) {
|
2019-12-03 13:33:04 +08:00
|
|
|
// Square avatar
|
|
|
|
return { size: 'large', shape: 'square' };
|
2019-11-21 20:23:56 +08:00
|
|
|
}
|
|
|
|
|
2019-12-03 13:33:04 +08:00
|
|
|
return { size: 'large', shape: 'circle' };
|
2019-11-21 20:23:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTitleBasicProps(hasAvatar: boolean, hasParagraph: boolean): SkeletonTitleProps {
|
|
|
|
if (!hasAvatar && hasParagraph) {
|
|
|
|
return { width: '38%' };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasAvatar && hasParagraph) {
|
|
|
|
return { width: '50%' };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getParagraphBasicProps(hasAvatar: boolean, hasTitle: boolean): SkeletonParagraphProps {
|
|
|
|
const basicProps: SkeletonParagraphProps = {};
|
|
|
|
|
|
|
|
// Width
|
|
|
|
if (!hasAvatar || !hasTitle) {
|
|
|
|
basicProps.width = '61%';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rows
|
|
|
|
if (!hasAvatar && hasTitle) {
|
|
|
|
basicProps.rows = 3;
|
|
|
|
} else {
|
|
|
|
basicProps.rows = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return basicProps;
|
|
|
|
}
|
|
|
|
|
2020-04-14 10:30:14 +08:00
|
|
|
const Skeleton = (props: SkeletonProps) => {
|
|
|
|
const renderSkeleton = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2019-11-21 20:23:56 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
loading,
|
|
|
|
className,
|
|
|
|
children,
|
|
|
|
avatar,
|
|
|
|
title,
|
|
|
|
paragraph,
|
|
|
|
active,
|
2020-05-16 13:19:54 +08:00
|
|
|
round,
|
2020-04-14 10:30:14 +08:00
|
|
|
} = props;
|
2019-11-21 20:23:56 +08:00
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('skeleton', customizePrefixCls);
|
|
|
|
|
2020-04-14 10:30:14 +08:00
|
|
|
if (loading || !('loading' in props)) {
|
2019-11-21 20:23:56 +08:00
|
|
|
const hasAvatar = !!avatar;
|
|
|
|
const hasTitle = !!title;
|
|
|
|
const hasParagraph = !!paragraph;
|
|
|
|
|
|
|
|
// Avatar
|
|
|
|
let avatarNode;
|
|
|
|
if (hasAvatar) {
|
2019-12-03 13:33:04 +08:00
|
|
|
const avatarProps: SkeletonAvatarProps = {
|
2019-11-21 20:23:56 +08:00
|
|
|
prefixCls: `${prefixCls}-avatar`,
|
|
|
|
...getAvatarBasicProps(hasTitle, hasParagraph),
|
|
|
|
...getComponentProps(avatar),
|
|
|
|
};
|
2019-12-03 13:33:04 +08:00
|
|
|
// We direct use SkeletonElement as avatar in skeleton internal.
|
2019-11-21 20:23:56 +08:00
|
|
|
avatarNode = (
|
|
|
|
<div className={`${prefixCls}-header`}>
|
2019-12-03 13:33:04 +08:00
|
|
|
<Element {...avatarProps} />
|
2019-11-21 20:23:56 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let contentNode;
|
|
|
|
if (hasTitle || hasParagraph) {
|
|
|
|
// Title
|
|
|
|
let $title;
|
|
|
|
if (hasTitle) {
|
|
|
|
const titleProps: SkeletonTitleProps = {
|
|
|
|
prefixCls: `${prefixCls}-title`,
|
|
|
|
...getTitleBasicProps(hasAvatar, hasParagraph),
|
|
|
|
...getComponentProps(title),
|
|
|
|
};
|
|
|
|
|
|
|
|
$title = <Title {...titleProps} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Paragraph
|
|
|
|
let paragraphNode;
|
|
|
|
if (hasParagraph) {
|
|
|
|
const paragraphProps: SkeletonParagraphProps = {
|
|
|
|
prefixCls: `${prefixCls}-paragraph`,
|
|
|
|
...getParagraphBasicProps(hasAvatar, hasTitle),
|
|
|
|
...getComponentProps(paragraph),
|
|
|
|
};
|
|
|
|
|
|
|
|
paragraphNode = <Paragraph {...paragraphProps} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
contentNode = (
|
|
|
|
<div className={`${prefixCls}-content`}>
|
|
|
|
{$title}
|
|
|
|
{paragraphNode}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cls = classNames(prefixCls, className, {
|
|
|
|
[`${prefixCls}-with-avatar`]: hasAvatar,
|
|
|
|
[`${prefixCls}-active`]: active,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2020-05-16 13:19:54 +08:00
|
|
|
[`${prefixCls}-round`]: round,
|
2019-11-21 20:23:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={cls}>
|
|
|
|
{avatarNode}
|
|
|
|
{contentNode}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
|
|
};
|
2020-04-14 10:30:14 +08:00
|
|
|
return <ConfigConsumer>{renderSkeleton}</ConfigConsumer>;
|
|
|
|
};
|
|
|
|
|
|
|
|
Skeleton.defaultProps = {
|
|
|
|
avatar: false,
|
|
|
|
title: true,
|
|
|
|
paragraph: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
Skeleton.Button = SkeletonButton;
|
|
|
|
Skeleton.Avatar = SkeletonAvatar;
|
|
|
|
Skeleton.Input = SkeletonInput;
|
2020-06-28 22:41:59 +08:00
|
|
|
Skeleton.Image = SkeletonImage;
|
2019-11-21 20:23:56 +08:00
|
|
|
|
|
|
|
export default Skeleton;
|