ant-design-vue/components/skeleton/index.jsx

169 lines
4.1 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import classNames from 'classnames';
import PropTypes from '../_util/vue-types';
import { initDefaultProps, hasProp } from '../_util/props-util';
2019-03-18 12:35:24 +00:00
import { ConfigConsumerProps } from '../config-provider';
2019-01-12 03:33:27 +00:00
import Avatar, { SkeletonAvatarProps } from './Avatar';
import Title, { SkeletonTitleProps } from './Title';
import Paragraph, { SkeletonParagraphProps } from './Paragraph';
import Base from '../base';
2018-12-10 03:34:51 +00:00
export const SkeletonProps = {
active: PropTypes.bool,
loading: PropTypes.bool,
prefixCls: PropTypes.string,
children: PropTypes.any,
2019-01-12 03:33:27 +00:00
avatar: PropTypes.oneOfType([PropTypes.string, SkeletonAvatarProps, PropTypes.bool]),
title: PropTypes.oneOfType([PropTypes.bool, PropTypes.string, SkeletonTitleProps]),
paragraph: PropTypes.oneOfType([PropTypes.bool, PropTypes.string, SkeletonParagraphProps]),
};
2018-12-10 03:34:51 +00:00
2019-01-12 03:33:27 +00:00
function getComponentProps(prop) {
2018-12-10 03:34:51 +00:00
if (prop && typeof prop === 'object') {
2019-01-12 03:33:27 +00:00
return prop;
2018-12-10 03:34:51 +00:00
}
2019-01-12 03:33:27 +00:00
return {};
2018-12-10 03:34:51 +00:00
}
2019-01-12 03:33:27 +00:00
function getAvatarBasicProps(hasTitle, hasParagraph) {
2018-12-10 03:34:51 +00:00
if (hasTitle && !hasParagraph) {
2019-01-12 03:33:27 +00:00
return { shape: 'square' };
2018-12-10 03:34:51 +00:00
}
2019-01-12 03:33:27 +00:00
return { shape: 'circle' };
2018-12-10 03:34:51 +00:00
}
2019-01-12 03:33:27 +00:00
function getTitleBasicProps(hasAvatar, hasParagraph) {
2018-12-10 03:34:51 +00:00
if (!hasAvatar && hasParagraph) {
2019-01-12 03:33:27 +00:00
return { width: '38%' };
2018-12-10 03:34:51 +00:00
}
if (hasAvatar && hasParagraph) {
2019-01-12 03:33:27 +00:00
return { width: '50%' };
2018-12-10 03:34:51 +00:00
}
2019-01-12 03:33:27 +00:00
return {};
2018-12-10 03:34:51 +00:00
}
2019-01-12 03:33:27 +00:00
function getParagraphBasicProps(hasAvatar, hasTitle) {
const basicProps = {};
2018-12-10 03:34:51 +00:00
// Width
if (!hasAvatar || !hasTitle) {
2019-01-12 03:33:27 +00:00
basicProps.width = '61%';
2018-12-10 03:34:51 +00:00
}
// Rows
if (!hasAvatar && hasTitle) {
2019-01-12 03:33:27 +00:00
basicProps.rows = 3;
2018-12-10 03:34:51 +00:00
} else {
2019-01-12 03:33:27 +00:00
basicProps.rows = 2;
2018-12-10 03:34:51 +00:00
}
2019-01-12 03:33:27 +00:00
return basicProps;
2018-12-10 03:34:51 +00:00
}
const Skeleton = {
name: 'ASkeleton',
props: initDefaultProps(SkeletonProps, {
avatar: false,
title: true,
paragraph: true,
}),
2019-03-18 12:35:24 +00:00
inject: {
configProvider: { default: () => ({}) },
},
2019-01-12 03:33:27 +00:00
render() {
2019-03-18 12:35:24 +00:00
const {
prefixCls: customizePrefixCls,
loading,
avatar,
title,
paragraph,
active,
} = this.$props;
const getPrefixCls = this.configProvider.getPrefixCls || ConfigConsumerProps.getPrefixCls;
const prefixCls = getPrefixCls('skeleton', customizePrefixCls);
2018-12-10 03:34:51 +00:00
if (loading || !hasProp(this, 'loading')) {
2019-01-12 03:33:27 +00:00
const hasAvatar = !!avatar || avatar === '';
const hasTitle = !!title;
const hasParagraph = !!paragraph;
2018-12-10 03:34:51 +00:00
// Avatar
2019-01-12 03:33:27 +00:00
let avatarNode;
2018-12-10 03:34:51 +00:00
if (hasAvatar) {
const avatarProps = {
props: {
2019-03-18 12:35:24 +00:00
prefixCls: `${prefixCls}-avatar`,
2018-12-10 03:34:51 +00:00
...getAvatarBasicProps(hasTitle, hasParagraph),
...getComponentProps(avatar),
},
2019-01-12 03:33:27 +00:00
};
2018-12-10 03:34:51 +00:00
avatarNode = (
<div class={`${prefixCls}-header`}>
<Avatar {...avatarProps} />
</div>
2019-01-12 03:33:27 +00:00
);
2018-12-10 03:34:51 +00:00
}
2019-01-12 03:33:27 +00:00
let contentNode;
2018-12-10 03:34:51 +00:00
if (hasTitle || hasParagraph) {
// Title
2019-01-12 03:33:27 +00:00
let $title;
2018-12-10 03:34:51 +00:00
if (hasTitle) {
const titleProps = {
props: {
2019-03-18 12:35:24 +00:00
prefixCls: `${prefixCls}-title`,
2018-12-10 03:34:51 +00:00
...getTitleBasicProps(hasAvatar, hasParagraph),
...getComponentProps(title),
},
2019-01-12 03:33:27 +00:00
};
2018-12-10 03:34:51 +00:00
2019-01-12 03:33:27 +00:00
$title = <Title {...titleProps} />;
2018-12-10 03:34:51 +00:00
}
// Paragraph
2019-01-12 03:33:27 +00:00
let paragraphNode;
2018-12-10 03:34:51 +00:00
if (hasParagraph) {
const paragraphProps = {
props: {
2019-03-18 12:35:24 +00:00
prefixCls: `${prefixCls}-paragraph`,
2018-12-10 03:34:51 +00:00
...getParagraphBasicProps(hasAvatar, hasTitle),
...getComponentProps(paragraph),
},
2019-01-12 03:33:27 +00:00
};
2018-12-10 03:34:51 +00:00
2019-01-12 03:33:27 +00:00
paragraphNode = <Paragraph {...paragraphProps} />;
2018-12-10 03:34:51 +00:00
}
contentNode = (
<div class={`${prefixCls}-content`}>
{$title}
{paragraphNode}
</div>
2019-01-12 03:33:27 +00:00
);
2018-12-10 03:34:51 +00:00
}
2019-01-12 03:33:27 +00:00
const cls = classNames(prefixCls, {
[`${prefixCls}-with-avatar`]: hasAvatar,
[`${prefixCls}-active`]: active,
});
2018-12-10 03:34:51 +00:00
return (
<div class={cls}>
{avatarNode}
{contentNode}
</div>
2019-01-12 03:33:27 +00:00
);
2018-12-10 03:34:51 +00:00
}
2019-01-12 03:33:27 +00:00
return this.$slots.default && this.$slots.default[0];
2018-12-10 03:34:51 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-12-10 03:34:51 +00:00
/* istanbul ignore next */
2019-01-12 03:33:27 +00:00
Skeleton.install = function(Vue) {
Vue.use(Base);
2019-01-12 03:33:27 +00:00
Vue.component(Skeleton.name, Skeleton);
};
export default Skeleton;