From f79f4cf05b1fcf0c1909d345ef7d53fa888905aa Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Tue, 27 Sep 2022 09:57:36 +0800 Subject: [PATCH] style: code optimization (#37743) --- components/skeleton/Avatar.tsx | 17 +++++++++-------- components/skeleton/Button.tsx | 16 +++++++++------- components/skeleton/Element.tsx | 21 ++++++++++++--------- components/skeleton/Image.tsx | 2 +- components/skeleton/Input.tsx | 10 +++------- components/skeleton/Node.tsx | 4 ++-- components/skeleton/Paragraph.tsx | 2 +- components/skeleton/Skeleton.tsx | 30 ++++++++++++++++-------------- components/skeleton/Title.tsx | 2 +- 9 files changed, 54 insertions(+), 50 deletions(-) diff --git a/components/skeleton/Avatar.tsx b/components/skeleton/Avatar.tsx index b507a48ac3..bf7bac1c62 100644 --- a/components/skeleton/Avatar.tsx +++ b/components/skeleton/Avatar.tsx @@ -9,8 +9,14 @@ export interface AvatarProps extends Omit { shape?: 'circle' | 'square'; } -const SkeletonAvatar = (props: AvatarProps) => { - const { prefixCls: customizePrefixCls, className, active } = props; +const SkeletonAvatar: React.FC = props => { + const { + prefixCls: customizePrefixCls, + className, + active, + shape = 'circle', + size = 'default', + } = props; const { getPrefixCls } = React.useContext(ConfigContext); const prefixCls = getPrefixCls('skeleton', customizePrefixCls); @@ -25,14 +31,9 @@ const SkeletonAvatar = (props: AvatarProps) => { ); return (
- +
); }; -SkeletonAvatar.defaultProps = { - size: 'default', - shape: 'circle', -}; - export default SkeletonAvatar; diff --git a/components/skeleton/Button.tsx b/components/skeleton/Button.tsx index 9b23b398a4..846c5da2ae 100644 --- a/components/skeleton/Button.tsx +++ b/components/skeleton/Button.tsx @@ -10,8 +10,14 @@ export interface SkeletonButtonProps extends Omit block?: boolean; } -const SkeletonButton = (props: SkeletonButtonProps) => { - const { prefixCls: customizePrefixCls, className, active, block = false } = props; +const SkeletonButton: React.FC = props => { + const { + prefixCls: customizePrefixCls, + className, + active, + block = false, + size = 'default', + } = props; const { getPrefixCls } = React.useContext(ConfigContext); const prefixCls = getPrefixCls('skeleton', customizePrefixCls); @@ -27,13 +33,9 @@ const SkeletonButton = (props: SkeletonButtonProps) => { ); return (
- +
); }; -SkeletonButton.defaultProps = { - size: 'default', -}; - export default SkeletonButton; diff --git a/components/skeleton/Element.tsx b/components/skeleton/Element.tsx index 4b9e59e20d..8782b7ea1c 100644 --- a/components/skeleton/Element.tsx +++ b/components/skeleton/Element.tsx @@ -10,7 +10,7 @@ export interface SkeletonElementProps { active?: boolean; } -const Element = (props: SkeletonElementProps) => { +const Element: React.FC = props => { const { prefixCls, className, style, size, shape } = props; const sizeCls = classNames({ @@ -24,14 +24,17 @@ const Element = (props: SkeletonElementProps) => { [`${prefixCls}-round`]: shape === 'round', }); - const sizeStyle: React.CSSProperties = - typeof size === 'number' - ? { - width: size, - height: size, - lineHeight: `${size}px`, - } - : {}; + const sizeStyle = React.useMemo( + () => + typeof size === 'number' + ? { + width: size, + height: size, + lineHeight: `${size}px`, + } + : {}, + [size], + ); return ( { +const SkeletonImage: React.FC = props => { const { prefixCls: customizePrefixCls, className, style, active } = props; const { getPrefixCls } = React.useContext(ConfigContext); const prefixCls = getPrefixCls('skeleton', customizePrefixCls); diff --git a/components/skeleton/Input.tsx b/components/skeleton/Input.tsx index 4a5d435ccd..6a50d5fac8 100644 --- a/components/skeleton/Input.tsx +++ b/components/skeleton/Input.tsx @@ -10,8 +10,8 @@ export interface SkeletonInputProps extends Omit { - const { prefixCls: customizePrefixCls, className, active, block } = props; +const SkeletonInput: React.FC = props => { + const { prefixCls: customizePrefixCls, className, active, block, size = 'default' } = props; const { getPrefixCls } = React.useContext(ConfigContext); const prefixCls = getPrefixCls('skeleton', customizePrefixCls); @@ -27,13 +27,9 @@ const SkeletonInput = (props: SkeletonInputProps) => { ); return (
- +
); }; -SkeletonInput.defaultProps = { - size: 'default', -}; - export default SkeletonInput; diff --git a/components/skeleton/Node.tsx b/components/skeleton/Node.tsx index ab9b34db64..4183d4dcc6 100644 --- a/components/skeleton/Node.tsx +++ b/components/skeleton/Node.tsx @@ -10,7 +10,7 @@ export interface SkeletonNodeProps extends Omit = props => { - const { prefixCls: customizePrefixCls, className, style, active } = props; + const { prefixCls: customizePrefixCls, className, style, active, children } = props; const { getPrefixCls } = React.useContext(ConfigContext); const prefixCls = getPrefixCls('skeleton', customizePrefixCls); @@ -23,7 +23,7 @@ const SkeletonNode: React.FC = props => { className, ); - const content = props.children ?? ; + const content = children ?? ; return (
diff --git a/components/skeleton/Paragraph.tsx b/components/skeleton/Paragraph.tsx index 4d13cd0b3b..1d4b63358d 100644 --- a/components/skeleton/Paragraph.tsx +++ b/components/skeleton/Paragraph.tsx @@ -11,7 +11,7 @@ export interface SkeletonParagraphProps { rows?: number; } -const Paragraph = (props: SkeletonParagraphProps) => { +const Paragraph: React.FC = props => { const getWidth = (index: number) => { const { width, rows = 2 } = props; if (Array.isArray(width)) { diff --git a/components/skeleton/Skeleton.tsx b/components/skeleton/Skeleton.tsx index cc7f123266..2c3f5315fb 100644 --- a/components/skeleton/Skeleton.tsx +++ b/components/skeleton/Skeleton.tsx @@ -75,16 +75,24 @@ function getParagraphBasicProps(hasAvatar: boolean, hasTitle: boolean): Skeleton return basicProps; } -const Skeleton = (props: SkeletonProps) => { +interface CompoundedComponent { + Button: typeof SkeletonButton; + Avatar: typeof SkeletonAvatar; + Input: typeof SkeletonInput; + Image: typeof SkeletonImage; + Node: typeof SkeletonNode; +} + +const Skeleton: React.FC & CompoundedComponent = props => { const { prefixCls: customizePrefixCls, loading, className, style, children, - avatar, - title, - paragraph, + avatar = false, + title = true, + paragraph = true, active, round, } = props; @@ -98,7 +106,7 @@ const Skeleton = (props: SkeletonProps) => { const hasParagraph = !!paragraph; // Avatar - let avatarNode; + let avatarNode: React.ReactNode; if (hasAvatar) { const avatarProps: SkeletonAvatarProps = { prefixCls: `${prefixCls}-avatar`, @@ -113,10 +121,10 @@ const Skeleton = (props: SkeletonProps) => { ); } - let contentNode; + let contentNode: React.ReactNode; if (hasTitle || hasParagraph) { // Title - let $title; + let $title: React.ReactNode; if (hasTitle) { const titleProps: SkeletonTitleProps = { prefixCls: `${prefixCls}-title`, @@ -128,7 +136,7 @@ const Skeleton = (props: SkeletonProps) => { } // Paragraph - let paragraphNode; + let paragraphNode: React.ReactNode; if (hasParagraph) { const paragraphProps: SkeletonParagraphProps = { prefixCls: `${prefixCls}-paragraph`, @@ -168,12 +176,6 @@ const Skeleton = (props: SkeletonProps) => { return typeof children !== 'undefined' ? (children as React.ReactElement) : null; }; -Skeleton.defaultProps = { - avatar: false, - title: true, - paragraph: true, -}; - Skeleton.Button = SkeletonButton; Skeleton.Avatar = SkeletonAvatar; Skeleton.Input = SkeletonInput; diff --git a/components/skeleton/Title.tsx b/components/skeleton/Title.tsx index 151a1c2e25..d1aa854030 100644 --- a/components/skeleton/Title.tsx +++ b/components/skeleton/Title.tsx @@ -9,7 +9,7 @@ export interface SkeletonTitleProps { width?: number | string; } -const Title = ({ prefixCls, className, width, style }: SkeletonTitleProps) => ( +const Title: React.FC = ({ prefixCls, className, width, style }) => (

);