amis-rpc-design/node_modules/antd/es/skeleton/Skeleton.js
2023-10-07 19:42:30 +08:00

140 lines
4.1 KiB
JavaScript

"use client";
import classNames from 'classnames';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import SkeletonAvatar from './Avatar';
import SkeletonButton from './Button';
import Element from './Element';
import SkeletonImage from './Image';
import SkeletonInput from './Input';
import SkeletonNode from './Node';
import Paragraph from './Paragraph';
import Title from './Title';
import useStyle from './style';
function getComponentProps(prop) {
if (prop && typeof prop === 'object') {
return prop;
}
return {};
}
function getAvatarBasicProps(hasTitle, hasParagraph) {
if (hasTitle && !hasParagraph) {
// Square avatar
return {
size: 'large',
shape: 'square'
};
}
return {
size: 'large',
shape: 'circle'
};
}
function getTitleBasicProps(hasAvatar, hasParagraph) {
if (!hasAvatar && hasParagraph) {
return {
width: '38%'
};
}
if (hasAvatar && hasParagraph) {
return {
width: '50%'
};
}
return {};
}
function getParagraphBasicProps(hasAvatar, hasTitle) {
const basicProps = {};
// Width
if (!hasAvatar || !hasTitle) {
basicProps.width = '61%';
}
// Rows
if (!hasAvatar && hasTitle) {
basicProps.rows = 3;
} else {
basicProps.rows = 2;
}
return basicProps;
}
const Skeleton = props => {
const {
prefixCls: customizePrefixCls,
loading,
className,
rootClassName,
style,
children,
avatar = false,
title = true,
paragraph = true,
active,
round
} = props;
const {
getPrefixCls,
direction,
skeleton
} = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('skeleton', customizePrefixCls);
const [wrapSSR, hashId] = useStyle(prefixCls);
if (loading || !('loading' in props)) {
const hasAvatar = !!avatar;
const hasTitle = !!title;
const hasParagraph = !!paragraph;
// Avatar
let avatarNode;
if (hasAvatar) {
const avatarProps = Object.assign(Object.assign({
prefixCls: `${prefixCls}-avatar`
}, getAvatarBasicProps(hasTitle, hasParagraph)), getComponentProps(avatar));
// We direct use SkeletonElement as avatar in skeleton internal.
avatarNode = /*#__PURE__*/React.createElement("div", {
className: `${prefixCls}-header`
}, /*#__PURE__*/React.createElement(Element, Object.assign({}, avatarProps)));
}
let contentNode;
if (hasTitle || hasParagraph) {
// Title
let $title;
if (hasTitle) {
const titleProps = Object.assign(Object.assign({
prefixCls: `${prefixCls}-title`
}, getTitleBasicProps(hasAvatar, hasParagraph)), getComponentProps(title));
$title = /*#__PURE__*/React.createElement(Title, Object.assign({}, titleProps));
}
// Paragraph
let paragraphNode;
if (hasParagraph) {
const paragraphProps = Object.assign(Object.assign({
prefixCls: `${prefixCls}-paragraph`
}, getParagraphBasicProps(hasAvatar, hasTitle)), getComponentProps(paragraph));
paragraphNode = /*#__PURE__*/React.createElement(Paragraph, Object.assign({}, paragraphProps));
}
contentNode = /*#__PURE__*/React.createElement("div", {
className: `${prefixCls}-content`
}, $title, paragraphNode);
}
const cls = classNames(prefixCls, {
[`${prefixCls}-with-avatar`]: hasAvatar,
[`${prefixCls}-active`]: active,
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-round`]: round
}, skeleton === null || skeleton === void 0 ? void 0 : skeleton.className, className, rootClassName, hashId);
return wrapSSR( /*#__PURE__*/React.createElement("div", {
className: cls,
style: Object.assign(Object.assign({}, skeleton === null || skeleton === void 0 ? void 0 : skeleton.style), style)
}, avatarNode, contentNode));
}
return typeof children !== 'undefined' ? children : null;
};
Skeleton.Button = SkeletonButton;
Skeleton.Avatar = SkeletonAvatar;
Skeleton.Input = SkeletonInput;
Skeleton.Image = SkeletonImage;
Skeleton.Node = SkeletonNode;
if (process.env.NODE_ENV !== 'production') {
Skeleton.displayName = 'Skeleton';
}
export default Skeleton;