Skip to content

Commit 7422026

Browse files
committed
fix: button
Fixed the issue that when the href property is undefined, the Button will also be rendered as a anchor. Fixed the issue that Edge throws an error when setting the loading property.
1 parent 7dbf5ed commit 7422026

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

components/button/__tests__/__snapshots__/index.test.js.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ exports[`Button renders Chinese characters correctly 5`] = `
3838

3939
exports[`Button renders correctly 1`] = `<button type="button" class="ant-btn ant-btn-default"><span>Follow</span></button>`;
4040

41-
exports[`Button should support link button 1`] = `<a target="_blank" href="http://ant.design" type="button" class="ant-btn ant-btn-default"><span>link button</span></a>`;
41+
exports[`Button should not render as link button when href is undefined 1`] = `<button type="button" class="ant-btn ant-btn-primary"><span>button</span></button>`;
42+
43+
exports[`Button should support link button 1`] = `<a target="_blank" href="http://ant.design" class="ant-btn ant-btn-default"><span>link button</span></a>`;

components/button/__tests__/index.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,15 @@ describe('Button', () => {
173173
})
174174
expect(wrapper2.html()).toMatchSnapshot()
175175
})
176+
177+
it('should not render as link button when href is undefined', async () => {
178+
const wrapper = mount({
179+
render () {
180+
return (
181+
<Button type='primary' href={undefined}>button</Button>
182+
)
183+
},
184+
})
185+
expect(wrapper.html()).toMatchSnapshot()
186+
})
176187
})

components/button/button.jsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import Icon from '../icon'
33
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/
44
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar)
55
import buttonTypes from './buttonTypes'
6+
import { filterEmpty } from '../_util/props-util'
67
const props = buttonTypes()
78
export default {
9+
inheritAttrs: false,
810
name: 'AButton',
911
__ANT_BUTTON: true,
10-
props: {
11-
...props,
12-
},
12+
props,
1313
data () {
1414
return {
1515
sizeMap: {
@@ -48,13 +48,15 @@ export default {
4848
computed: {
4949
classes () {
5050
const { prefixCls, type, shape, size, hasTwoCNChar,
51-
sLoading, ghost, block, sizeMap } = this
51+
sLoading, ghost, block, sizeMap, icon, $slots } = this
5252
const sizeCls = sizeMap[size] || ''
53+
const children = filterEmpty($slots.default)
5354
return {
5455
[`${prefixCls}`]: true,
5556
[`${prefixCls}-${type}`]: type,
5657
[`${prefixCls}-${shape}`]: shape,
5758
[`${prefixCls}-${sizeCls}`]: sizeCls,
59+
[`${prefixCls}-icon-only`]: !children && children !== 0 && icon,
5860
[`${prefixCls}-loading`]: sLoading,
5961
[`${prefixCls}-background-ghost`]: ghost || type === 'ghost',
6062
[`${prefixCls}-two-chinese-chars`]: hasTwoCNChar,
@@ -106,11 +108,8 @@ export default {
106108
disabled, handleClick,
107109
sLoading, $slots, $attrs, $listeners } = this
108110
const buttonProps = {
109-
props: {
110-
},
111111
attrs: {
112112
...$attrs,
113-
type: htmlType,
114113
disabled,
115114
},
116115
class: classes,
@@ -123,7 +122,7 @@ export default {
123122
const iconNode = iconType ? <Icon type={iconType} /> : null
124123
const kids = $slots.default && $slots.default.length === 1 ? this.insertSpace($slots.default[0], this.isNeedInserted()) : $slots.default
125124

126-
if ('href' in $attrs) {
125+
if ($attrs.href !== undefined) {
127126
return (
128127
<a {...buttonProps} ref='buttonNode'>
129128
{iconNode}{kids}
@@ -132,7 +131,7 @@ export default {
132131
} else {
133132
return (
134133
<Wave>
135-
<button {...buttonProps} ref='buttonNode'>
134+
<button {...buttonProps} ref='buttonNode' type={htmlType || 'button'}>
136135
{iconNode}{kids}
137136
</button>
138137
</Wave>

0 commit comments

Comments
 (0)