Skip to content

Commit

Permalink
feat(Button): Button组件对齐 mobile-vue (#469)
Browse files Browse the repository at this point in the history
* feat(Button): 同步Button组件特性

* chore: 解决开发环境无prettier导致提交校验prettier运行报错

* feat(Button): 升级Button组件样式表到v2

* feat(Demo): 更新Buttons示例样式

* chore: common仓库同步

* feat(Demo): 更新Buttons示例背景颜色

* chore: 同步新的类型规范

* docs(Button): 更新Buttons的API样式

* fix(Button-type): 更正Button组件的ref类型

* chore: 更新测试snap

* chore: 更新测试snap

* chore: 更新测试snap

* feat(Button): 使用parseTNode包裹TNode

* chore(Button): 更新测试snap

* fix(Button): 去掉children类型继承

* fix(Demo): 恢复演示背景默认颜色

* fix(Button): button演示背景改为白色

* chore: snap-update
  • Loading branch information
tobytovi authored Aug 21, 2024
1 parent f92c1ee commit f41bd72
Show file tree
Hide file tree
Showing 25 changed files with 2,123 additions and 380 deletions.
2 changes: 1 addition & 1 deletion site/mobile/mobile.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default {
{
title: 'Button 按钮',
name: 'button',
component: () => import('tdesign-mobile-react/button/_example/index.jsx'),
component: () => import('tdesign-mobile-react/button/_example/index.tsx'),
},
{
title: 'Divider 分割符',
Expand Down
35 changes: 23 additions & 12 deletions src/button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import React, { forwardRef } from 'react';
import classnames from 'classnames';
import { LoadingIcon } from 'tdesign-icons-react';
import useConfig from '../_util/useConfig';
import parseTNode from '../_util/parseTNode';
import { TdButtonProps } from './type';
import noop from '../_util/noop';
import { buttonDefaultProps } from './defaultProps';

export interface ButtonProps extends TdButtonProps, Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'content'> {}
export interface ButtonProps
extends TdButtonProps,
Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'content' | 'children'> {}

const Button = forwardRef((props: ButtonProps, ref: React.Ref<HTMLButtonElement>) => {
const Button = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
const {
className = '',
style,
Expand All @@ -18,39 +21,47 @@ const Button = forwardRef((props: ButtonProps, ref: React.Ref<HTMLButtonElement>
disabled,
ghost,
icon = null,
suffix = null,
loading,
shape,
size,
theme,
type,
variant,
onClick = noop,
loadingProps = {},
} = props;
const { classPrefix } = useConfig();

const childNode = content || children;

return (
<button
ref={ref}
type={type}
className={classnames(
[`${classPrefix}-button`, `${classPrefix}-button--${variant}`, `${classPrefix}-button--${theme}`, className],
[
`${classPrefix}-button`,
`${classPrefix}-button--size-${size}`,
`${classPrefix}-button--${variant}`,
`${classPrefix}-button--${theme}`,
`${classPrefix}-button--${shape}`,
className,
],
{
[`${classPrefix}-button--ghost`]: ghost,
[`${classPrefix}-size-s`]: size === 'small',
[`${classPrefix}-size-default`]: size === 'medium',
[`${classPrefix}-size-l`]: size === 'large',
[`${classPrefix}-is-loading`]: loading,
[`${classPrefix}-is-disabled`]: disabled,
[`${classPrefix}-is-block`]: block,
[`${classPrefix}-button--loading`]: loading,
[`${classPrefix}-button--disabled`]: disabled,
[`${classPrefix}-button--block`]: block,
},
[`${classPrefix}-button--shape-${shape}`],
)}
style={style}
onClick={!loading && !disabled ? onClick : undefined}
disabled={disabled || loading}
>
{loading ? <LoadingIcon /> : icon}
{content || children ? <span className={`${classPrefix}-button__text`}> {content || children}</span> : ''}
{loading ? <LoadingIcon {...loadingProps} /> : parseTNode(icon)}
{childNode && <span className={`${classPrefix}-button__content`}> {parseTNode(childNode)}</span>}
{parseTNode(suffix)}
</button>
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/button/__tests__/__snapshots__/button.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`Button 组件测试 > content 1`] = `
<span
class="t-button__text"
class="t-button__content"
>
按钮组件
Expand Down
78 changes: 0 additions & 78 deletions src/button/_example/base.jsx

This file was deleted.

29 changes: 29 additions & 0 deletions src/button/_example/base.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { Button } from 'tdesign-mobile-react';

export default function () {
return (
<>
<div className="row">
<Button size="large" theme="primary">
填充按钮
</Button>
<Button size="large" theme="light">
填充按钮
</Button>
<Button size="large">填充按钮</Button>
</div>
<div className="row">
<Button size="large" theme="primary" variant="outline">
描边按钮
</Button>
<Button size="large" theme="primary" variant="dashed">
虚框按钮
</Button>
<Button size="large" theme="primary" variant="text">
文字按钮
</Button>
</div>
</>
);
}
12 changes: 12 additions & 0 deletions src/button/_example/block.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { Button } from 'tdesign-mobile-react';

export default function () {
return (
<div className="row">
<Button size="large" theme="primary" block>
填充按钮
</Button>
</div>
);
}
18 changes: 18 additions & 0 deletions src/button/_example/ghost.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { Button } from 'tdesign-mobile-react';

export default function () {
return (
<div className="row section-ghost">
<Button size="large" ghost theme="primary">
幽灵按钮
</Button>
<Button size="large" ghost theme="danger">
幽灵按钮
</Button>
<Button size="large" ghost>
幽灵按钮
</Button>
</div>
);
}
15 changes: 15 additions & 0 deletions src/button/_example/group.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { Button } from 'tdesign-mobile-react';

export default function () {
return (
<div className="row section-group">
<Button size="large" theme="light">
组合按钮
</Button>
<Button size="large" theme="primary">
组合按钮
</Button>
</div>
);
}
17 changes: 17 additions & 0 deletions src/button/_example/icon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import { Button } from 'tdesign-mobile-react';
import { Icon } from 'tdesign-icons-react';

export default function () {
return (
<div className="row">
<Button theme="primary" size="large" icon={<Icon name="app" />}>
填充按钮
</Button>
<Button theme="primary" size="large" loading>
加载中
</Button>
<Button theme="primary" size="large" shape="square" icon={<Icon name="search" />} />
</div>
);
}
28 changes: 0 additions & 28 deletions src/button/_example/index.jsx

This file was deleted.

52 changes: 52 additions & 0 deletions src/button/_example/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';
import TDemoBlock from '../../../site/mobile/components/DemoBlock';
import TDemoHeader from '../../../site/mobile/components/DemoHeader';
import './style/index.less';
import BaseDemo from './base';
import IconDemo from './icon';
import GhostDemo from './ghost';
import GroupDemo from './group';
import BlockDemo from './block';
import SizeDemo from './size';
import StatusDemo from './status';
import ShapeDemo from './shape';
import ThemeDemo from './theme';

export default function Base() {
return (
<div className="tdesign-mobile-demo">
<TDemoHeader title="Button 按钮" summary="按钮用于开启一个闭环的操作任务,如“删除”对象、“购买”商品等。" />
<TDemoBlock title="01 组件类型" summary="基础按钮">
<BaseDemo />
</TDemoBlock>
<TDemoBlock summary="图标按钮">
<IconDemo />
</TDemoBlock>
<TDemoBlock summary="幽灵按钮">
<GhostDemo />
</TDemoBlock>
<TDemoBlock summary="组合按钮">
<GroupDemo />
</TDemoBlock>
<TDemoBlock summary="通栏按钮">
<BlockDemo />
</TDemoBlock>

<TDemoBlock title="02 组件状态" summary="按钮禁用态">
<StatusDemo />
</TDemoBlock>

<TDemoBlock title="03 组件样式" summary="按钮尺寸">
<SizeDemo />
</TDemoBlock>

<TDemoBlock summary="按钮形状">
<ShapeDemo />
</TDemoBlock>

<TDemoBlock summary="按钮主题">
<ThemeDemo />
</TDemoBlock>
</div>
);
}
23 changes: 23 additions & 0 deletions src/button/_example/shape.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { Button } from 'tdesign-mobile-react';
import { Icon } from 'tdesign-icons-react';

export default function () {
return (
<>
<div className="row section-shape">
<Button theme="primary" size="large">
填充按钮
</Button>
<Button theme="primary" size="large" shape="square" aria-label="搜索" icon={<Icon name="search" />}></Button>
<Button theme="primary" size="large" shape="round">
填充按钮
</Button>
<Button theme="primary" size="large" shape="circle" aria-label="搜索" icon={<Icon name="search" />}></Button>
</div>
<Button theme="primary" size="large" block className="rectangle-button">
填充按钮
</Button>
</>
);
}
Loading

0 comments on commit f41bd72

Please sign in to comment.