2019-11-21 20:23:56 +08:00
|
|
|
import classNames from 'classnames';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2022-03-25 20:11:33 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { AvatarProps } from './Avatar';
|
|
|
|
import SkeletonAvatar from './Avatar';
|
2020-04-14 10:30:14 +08:00
|
|
|
import SkeletonButton from './Button';
|
2022-07-11 12:04:18 +08:00
|
|
|
import SkeletonNode from './Node';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Element from './Element';
|
2020-06-28 22:41:59 +08:00
|
|
|
import SkeletonImage from './Image';
|
2022-06-22 14:57:09 +08:00
|
|
|
import SkeletonInput from './Input';
|
|
|
|
import type { SkeletonParagraphProps } from './Paragraph';
|
|
|
|
import Paragraph from './Paragraph';
|
|
|
|
import type { SkeletonTitleProps } from './Title';
|
|
|
|
import Title from './Title';
|
2019-12-03 13:33:04 +08:00
|
|
|
|
2022-03-29 20:55:44 +08:00
|
|
|
import useStyle from './style';
|
2019-12-03 13:33:04 +08:00
|
|
|
|
|
|
|
/* This only for skeleton internal. */
|
2022-12-01 14:33:51 +08:00
|
|
|
type SkeletonAvatarProps = Omit<AvatarProps, 'active'>;
|
2019-11-21 20:23:56 +08:00
|
|
|
|
|
|
|
export interface SkeletonProps {
|
|
|
|
active?: boolean;
|
|
|
|
loading?: boolean;
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2021-12-24 14:23:32 +08:00
|
|
|
style?: React.CSSProperties;
|
2019-11-21 20:23:56 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-16 09:55:52 +08:00
|
|
|
function getComponentProps<T>(prop?: T | boolean): T | {} {
|
2019-11-21 20:23:56 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:56:23 +08:00
|
|
|
type CompoundedComponent = {
|
2022-09-27 09:57:36 +08:00
|
|
|
Button: typeof SkeletonButton;
|
|
|
|
Avatar: typeof SkeletonAvatar;
|
|
|
|
Input: typeof SkeletonInput;
|
|
|
|
Image: typeof SkeletonImage;
|
|
|
|
Node: typeof SkeletonNode;
|
2022-11-19 16:56:23 +08:00
|
|
|
};
|
2022-09-27 09:57:36 +08:00
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const Skeleton: React.FC<SkeletonProps> & CompoundedComponent = (props) => {
|
2022-03-25 20:11:33 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
loading,
|
|
|
|
className,
|
|
|
|
style,
|
|
|
|
children,
|
2022-09-27 09:57:36 +08:00
|
|
|
avatar = false,
|
|
|
|
title = true,
|
|
|
|
paragraph = true,
|
2022-03-25 20:11:33 +08:00
|
|
|
active,
|
|
|
|
round,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('skeleton', customizePrefixCls);
|
2022-03-29 20:55:44 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2022-03-25 20:11:33 +08:00
|
|
|
|
|
|
|
if (loading || !('loading' in props)) {
|
|
|
|
const hasAvatar = !!avatar;
|
|
|
|
const hasTitle = !!title;
|
|
|
|
const hasParagraph = !!paragraph;
|
|
|
|
|
|
|
|
// Avatar
|
2022-09-27 09:57:36 +08:00
|
|
|
let avatarNode: React.ReactNode;
|
2022-03-25 20:11:33 +08:00
|
|
|
if (hasAvatar) {
|
|
|
|
const avatarProps: SkeletonAvatarProps = {
|
|
|
|
prefixCls: `${prefixCls}-avatar`,
|
|
|
|
...getAvatarBasicProps(hasTitle, hasParagraph),
|
|
|
|
...getComponentProps(avatar),
|
|
|
|
};
|
|
|
|
// We direct use SkeletonElement as avatar in skeleton internal.
|
|
|
|
avatarNode = (
|
|
|
|
<div className={`${prefixCls}-header`}>
|
|
|
|
<Element {...avatarProps} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-27 09:57:36 +08:00
|
|
|
let contentNode: React.ReactNode;
|
2022-03-25 20:11:33 +08:00
|
|
|
if (hasTitle || hasParagraph) {
|
|
|
|
// Title
|
2022-09-27 09:57:36 +08:00
|
|
|
let $title: React.ReactNode;
|
2022-03-25 20:11:33 +08:00
|
|
|
if (hasTitle) {
|
|
|
|
const titleProps: SkeletonTitleProps = {
|
|
|
|
prefixCls: `${prefixCls}-title`,
|
|
|
|
...getTitleBasicProps(hasAvatar, hasParagraph),
|
|
|
|
...getComponentProps(title),
|
2019-11-21 20:23:56 +08:00
|
|
|
};
|
|
|
|
|
2022-03-25 20:11:33 +08:00
|
|
|
$title = <Title {...titleProps} />;
|
2019-11-21 20:23:56 +08:00
|
|
|
}
|
|
|
|
|
2022-03-25 20:11:33 +08:00
|
|
|
// Paragraph
|
2022-09-27 09:57:36 +08:00
|
|
|
let paragraphNode: React.ReactNode;
|
2022-03-25 20:11:33 +08:00
|
|
|
if (hasParagraph) {
|
|
|
|
const paragraphProps: SkeletonParagraphProps = {
|
|
|
|
prefixCls: `${prefixCls}-paragraph`,
|
|
|
|
...getParagraphBasicProps(hasAvatar, hasTitle),
|
|
|
|
...getComponentProps(paragraph),
|
|
|
|
};
|
|
|
|
|
|
|
|
paragraphNode = <Paragraph {...paragraphProps} />;
|
|
|
|
}
|
2019-11-21 20:23:56 +08:00
|
|
|
|
2022-03-25 20:11:33 +08:00
|
|
|
contentNode = (
|
|
|
|
<div className={`${prefixCls}-content`}>
|
|
|
|
{$title}
|
|
|
|
{paragraphNode}
|
2019-11-21 20:23:56 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-25 20:11:33 +08:00
|
|
|
const cls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-with-avatar`]: hasAvatar,
|
|
|
|
[`${prefixCls}-active`]: active,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-round`]: round,
|
|
|
|
},
|
|
|
|
className,
|
2022-03-29 20:55:44 +08:00
|
|
|
hashId,
|
2022-03-25 20:11:33 +08:00
|
|
|
);
|
|
|
|
|
2022-03-29 20:55:44 +08:00
|
|
|
return wrapSSR(
|
2022-03-25 20:11:33 +08:00
|
|
|
<div className={cls} style={style}>
|
|
|
|
{avatarNode}
|
|
|
|
{contentNode}
|
2022-03-29 20:55:44 +08:00
|
|
|
</div>,
|
2022-03-25 20:11:33 +08:00
|
|
|
);
|
|
|
|
}
|
2022-04-07 17:55:30 +08:00
|
|
|
return typeof children !== 'undefined' ? (children as React.ReactElement) : null;
|
2020-04-14 10:30:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Skeleton.Button = SkeletonButton;
|
|
|
|
Skeleton.Avatar = SkeletonAvatar;
|
|
|
|
Skeleton.Input = SkeletonInput;
|
2020-06-28 22:41:59 +08:00
|
|
|
Skeleton.Image = SkeletonImage;
|
2022-07-11 12:04:18 +08:00
|
|
|
Skeleton.Node = SkeletonNode;
|
2019-11-21 20:23:56 +08:00
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Skeleton.displayName = 'Skeleton';
|
|
|
|
}
|
|
|
|
|
2019-11-21 20:23:56 +08:00
|
|
|
export default Skeleton;
|