Skip to content

Commit

Permalink
fix(skeleton): fix skeleton row and width (#1772)
Browse files Browse the repository at this point in the history
Co-authored-by: maxin <maxin@growingio.com>
  • Loading branch information
nnmax and maxin committed Jan 6, 2022
1 parent f6bf2e6 commit 09e740c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
35 changes: 26 additions & 9 deletions src/skeleton/Skeleton.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import React from 'react';
import classNames from 'classnames';
import { usePrefixCls } from '@gio-design/utils';
import { isArray, isObject } from 'lodash';
import Avatar from '../avatar';
import useDebounceLoading from '../utils/hooks/useDebounceLoading';
import { SkeletonProps } from './interface';
import SkeletonImage from './Image';
import WithSubComponent from '../utils/withSubComponent';

const Skeleton = (props: SkeletonProps) => {
const { prefixCls: customizePrefixCls, delay = 0, loading = true, children, active = true, className, style } = props;
const InnerSkeleton = React.forwardRef<HTMLDivElement, SkeletonProps>((props, ref) => {
const {
prefixCls: customizePrefixCls,
delay = 0,
loading = true,
children,
active = true,
className,
paragraph,
title,
...otherProps
} = props;
const prefixCls = usePrefixCls('skeleton', customizePrefixCls);
const shouldLoading = useDebounceLoading(loading, delay);

Expand All @@ -26,20 +38,21 @@ const Skeleton = (props: SkeletonProps) => {
);
};
const renderSkeletonParagraph = () => {
const { paragraph = true, title = true } = props;
if (!paragraph && !title) {
return null;
}
const row = 3;
const { row, width } = isObject(paragraph) ? paragraph : { row: 3, width: '100%' };

return (
<div className={`${prefixCls}-content`}>
{title && <div className={`${prefixCls}-title`} />}
{paragraph && (
<div className={`${prefixCls}-paragraph`}>
{Array(row)
.fill(0)
.map((...args) => (
<p key={args[1]} />
.map((_, index) => (
// eslint-disable-next-line react/no-array-index-key
<p key={index} style={{ width: isArray(width) ? width[index] : width }} />
))}
</div>
)}
Expand All @@ -52,16 +65,20 @@ const Skeleton = (props: SkeletonProps) => {
className={classNames(prefixCls, className, {
[`${prefixCls}-active`]: active,
})}
style={style}
ref={ref}
data-testid="skeleton"
{...otherProps}
>
{renderSkeletonAvatar()}
{renderSkeletonParagraph()}
</div>
) : (
<>{children}</>
);
};
});

Skeleton.Image = SkeletonImage;
const Skeleton = WithSubComponent(InnerSkeleton, {
Image: SkeletonImage,
});

export default Skeleton;
3 changes: 2 additions & 1 deletion src/skeleton/demos/Skeleton.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export const Default = Template.bind({});
Default.args = {
loading: true,
delay: 1000,
avatar: true,
avatar: false,
active: true,
paragraph: { row: 5, width: [100, 200, 300, 400, 500] },
};

const TemplateImage: Story<SkeletonImageProps> = (args) => (
Expand Down
14 changes: 11 additions & 3 deletions src/skeleton/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ import { AvatarProps } from '../avatar';

export type SkeletonAvatarProps = Pick<AvatarProps, 'size'>;
export interface SkeletonParagraphProps {
/**
* 段落展位图行数
* @default 3
*/
row?: number;

/**
* 设置段落占位图的宽度,若为数组时则为对应的每行宽度
* @default 100%
*/
width?: string | number | (string | number)[];
}

export interface SkeletonImageProps {
Expand Down Expand Up @@ -34,9 +44,7 @@ export interface SkeletonImageProps {
color?: string;
}

export interface SkeletonProps {
className?: string;
style?: React.CSSProperties;
export interface SkeletonProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'title'> {
/**
替代组件的类前缀
*/
Expand Down
2 changes: 1 addition & 1 deletion src/skeleton/style/index.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import '../../stylesheet/index.less';
@import (reference) '../../stylesheet/index.less';

@skeleton-prefix-cls: ~'@{component-prefix}-skeleton';

Expand Down

1 comment on commit 09e740c

@vercel
Copy link

@vercel vercel bot commented on 09e740c Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.