diff --git a/src/button/README.md b/src/button/README.md index fbba897e5..9080768cb 100644 --- a/src/button/README.md +++ b/src/button/README.md @@ -111,6 +111,7 @@ Vue.use(Button); | color | Color, support linear-gradient | _string_ | - | | icon | Left Icon | _string_ | - | | icon-prefix `v2.6.0` | Icon className prefix | _string_ | `van-icon` | +| icon-position `v2.10.7` | Icon position, can be set to `right` | `left` | | tag | HTML Tag | _string_ | `button` | | native-type | Native Type Attribute | _string_ | `''` | | plain | Whether to be plain button | _boolean_ | `false` | diff --git a/src/button/README.zh-CN.md b/src/button/README.zh-CN.md index 66fe73a8e..672a87372 100644 --- a/src/button/README.zh-CN.md +++ b/src/button/README.zh-CN.md @@ -133,7 +133,8 @@ Vue.use(Button); | color | 按钮颜色,支持传入`linear-gradient`渐变色 | _string_ | - | | icon | 左侧[图标名称](#/zh-CN/icon)或图片链接 | _string_ | - | | icon-prefix `v2.6.0` | 图标类名前缀,同 Icon 组件的 [class-prefix 属性](#/zh-CN/icon#props) | _string_ | `van-icon` | -| tag | 根节点的 HTML 标签 | _string_ | `button` | +| icon-position `v2.10.7` | 图标展示位置,可选值为 `right` | `left` | +| tag | 按钮根节点的 HTML 标签 | _string_ | `button` | | native-type | 原生 button 标签的 type 属性 | _string_ | - | | block | 是否为块级元素 | _boolean_ | `false` | | plain | 是否为朴素按钮 | _boolean_ | `false` | diff --git a/src/button/index.less b/src/button/index.less index 5f2ad00ae..10cd8e797 100644 --- a/src/button/index.less +++ b/src/button/index.less @@ -160,8 +160,10 @@ } &__icon + &__text, - &__loading + &__text { - margin-left: 5px; + &__loading + &__text, + &__text + &__icon, + &__text + &__loading { + margin-left: @padding-base; } &--hairline { diff --git a/src/button/index.tsx b/src/button/index.tsx index 5cfd67ce8..dbcfc06ad 100644 --- a/src/button/index.tsx +++ b/src/button/index.tsx @@ -35,6 +35,7 @@ export type ButtonProps = RouteProps & { loadingSize: string; loadingType?: LoadingType; loadingText?: string; + iconPosition: 'left' | 'right'; }; export type ButtonEvents = { @@ -63,6 +64,7 @@ function Button( loading, hairline, loadingText, + iconPosition, } = props; const style: Record = {}; @@ -111,27 +113,33 @@ function Button( { [BORDER_SURROUND]: hairline }, ]; - function Content() { - const content = []; - + function renderIcon() { if (loading) { - content.push( - slots.loading ? ( - slots.loading() - ) : ( - - ) + return slots.loading ? ( + slots.loading() + ) : ( + ); - } else if (icon) { - content.push( + } + + if (icon) { + return ( ); } + } + + function renderContent() { + const content = []; + + if (iconPosition === 'left') { + content.push(renderIcon()); + } let text; if (loading) { @@ -144,6 +152,10 @@ function Button( content.push({text}); } + if (iconPosition === 'right') { + content.push(renderIcon()); + } + return content; } @@ -157,7 +169,7 @@ function Button( onTouchstart={onTouchstart} {...inherit(ctx)} > -
{Content()}
+
{renderContent()}
); } @@ -194,6 +206,10 @@ Button.props = { type: String, default: '20px', }, + iconPosition: { + type: String, + default: 'left', + }, }; export default createComponent(Button); diff --git a/src/button/test/__snapshots__/index.spec.js.snap b/src/button/test/__snapshots__/index.spec.js.snap index 6c8c5a8eb..09bd1be32 100644 --- a/src/button/test/__snapshots__/index.spec.js.snap +++ b/src/button/test/__snapshots__/index.spec.js.snap @@ -1,5 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`icon-position prop 1`] = ` + +`; + exports[`icon-prefix prop 1`] = `