Skip to content

Commit

Permalink
refactor(components/button): 重构 Button,内部 hooks 移动到 hooks 文件夹
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxinssfd committed Sep 25, 2023
1 parent ca51c04 commit 6a807ec
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 47 deletions.
51 changes: 28 additions & 23 deletions packages/components/src/button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import {
getComponentClass,
CLASS_SIZE_LG,
CLASS_SIZE_SM,
CLASS_SIZE_M,
} from '@pkg/shared';
import { getSizeClassName, getClasses } from '@pkg/shared';
import { ButtonContext } from '~/button/button.context';
import { useBtnIcon, useBtnWave } from './button.hooks';
import type { RequiredPart } from '@tool-pack/types';
import type { ButtonProps } from './button.types';
import { getClassNames } from '@tool-pack/basic';
import { useBtnWave, useBtnIcon } from './hooks';
import React, { useContext } from 'react';

const rootClass = getComponentClass('button');
const cls = getClasses(
'button',
['icon'],
[
'plain-dashed',
'plain-text',
'plain',
'icon-l',
'icon-r',
'icon-only',
'loading',
],
);
const defaultProps = {
shape: 'default',
type: 'default',
Expand Down Expand Up @@ -43,8 +50,8 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
keyof typeof defaultProps
>;

const [btnWave, activateWave] = useBtnWave(rootClass);
const btnIcon = useBtnIcon(rootClass, icon, loading);
const [btnWave, activateWave] = useBtnWave(cls.root);
const btnIcon = useBtnIcon(cls.root, icon, loading);

const clickHandler: React.MouseEventHandler<HTMLButtonElement> = (
e,
Expand All @@ -61,24 +68,22 @@ export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
<button
{...attrs}
className={getClassNames(
rootClass,
cls.root,
className,
attrs.className,
`${rootClass}--type-${type}`,
`${cls.root}--type-${type}`,
{
[`${rootClass}--shape-${shape}`]: shape !== 'default',
[`${rootClass}--plain-dashed`]: plain === 'dashed',
[`${cls.root}--shape-${shape}`]: shape !== 'default',
[cls['--']['plain-dashed']]: plain === 'dashed',
// 默认也要显示,否则 loading 出入场动画会不流畅
[`${rootClass}--icon-l`]: !iconOnly && !rightIcon,
[`${rootClass}--icon-r`]: !iconOnly && rightIcon,
[`${rootClass}--plain-text`]: plain === 'text',
[`${rootClass}--plain`]: plain === true,
[`${rootClass}--icon-only`]: iconOnly,
[`${rootClass}--loading`]: loading,
[CLASS_SIZE_SM]: size === 'small',
[CLASS_SIZE_M]: size === 'medium',
[CLASS_SIZE_LG]: size === 'large',
[cls['--']['icon-l']]: !iconOnly && !rightIcon,
[cls['--']['icon-r']]: !iconOnly && rightIcon,
[cls['--']['plain-text']]: plain === 'text',
[cls['--']['plain']]: plain === true,
[cls['--']['icon-only']]: iconOnly,
[cls['--']['loading']]: loading,
},
getSizeClassName(size),
)}
onClick={clickHandler}
disabled={disabled}
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/button/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './useBtnIcon';
export * from './useBtnWave';
24 changes: 24 additions & 0 deletions packages/components/src/button/hooks/useBtnIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CollapseTransition } from '@pkg/components';
import { Loading as LoadingIcon } from '@pkg/icons';
import { ButtonProps } from '~/button';
import React, { useMemo } from 'react';
import { Icon } from '~/icon';

export function useBtnIcon(
rootClass: string,
icon: ButtonProps['icon'],
loading: ButtonProps['loading'],
): React.ReactElement {
return useMemo(() => {
const className = `${rootClass}__icon`;
const Loading = (
<Icon className={className} key="loading-icon">
<LoadingIcon />
</Icon>
);

if (icon)
return loading ? Loading : <Icon className={className}>{icon}</Icon>;
return <CollapseTransition width>{loading && Loading}</CollapseTransition>;
}, [loading, icon]);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import React, { useEffect, useState, useMemo, useRef } from 'react';
import { CollapseTransition } from '@pkg/components';
import { Loading as LoadingIcon } from '@pkg/icons';
import type { ButtonProps } from './button.types';
import React, { useEffect, useState, useRef } from 'react';
import { getClassNames } from '@tool-pack/basic';
import { Icon } from '~/icon';

export function useBtnWave(
rootClass: string,
Expand Down Expand Up @@ -52,22 +48,3 @@ export function useBtnWave(

return [wave, activateWave];
}

export function useBtnIcon(
rootClass: string,
icon: ButtonProps['icon'],
loading: ButtonProps['loading'],
): React.ReactElement {
return useMemo(() => {
const className = `${rootClass}__icon`;
const Loading = (
<Icon className={className} key="loading-icon">
<LoadingIcon />
</Icon>
);

if (icon)
return loading ? Loading : <Icon className={className}>{icon}</Icon>;
return <CollapseTransition width>{loading && Loading}</CollapseTransition>;
}, [loading, icon]);
}

0 comments on commit 6a807ec

Please sign in to comment.