ant-design/components/skeleton/Paragraph.tsx
yexiaolong bdc382bbe4
chore: code optimization (#47852)
* perf: 代码优化: 1.Skeleton组件children null判断; 2.Paragraph组件内部getWidth方法抽离;

* perf: 代码优化: 1.Skeleton组件children null判断; 2.Paragraph组件内部getWidth方法抽离;

---------

Co-authored-by: 叶晓龙 <evermore.ye@quectel.com>
2024-03-13 11:38:40 +08:00

40 lines
984 B
TypeScript

import classNames from 'classnames';
import * as React from 'react';
type widthUnit = number | string;
export interface SkeletonParagraphProps {
prefixCls?: string;
className?: string;
style?: React.CSSProperties;
width?: widthUnit | Array<widthUnit>;
rows?: number;
}
const getWidth = (index: number, props: SkeletonParagraphProps) => {
const { width, rows = 2 } = props;
if (Array.isArray(width)) {
return width[index];
}
// last paragraph
if (rows - 1 === index) {
return width;
}
return undefined;
};
const Paragraph: React.FC<SkeletonParagraphProps> = (props) => {
const { prefixCls, className, style, rows } = props;
const rowList = [...Array(rows)].map((_, index) => (
// eslint-disable-next-line react/no-array-index-key
<li key={index} style={{ width: getWidth(index, props) }} />
));
return (
<ul className={classNames(prefixCls, className)} style={style}>
{rowList}
</ul>
);
};
export default Paragraph;