Skip to content

Commit 61f4a11

Browse files
committed
feat(Button): render anchor tag if href prop exists
1 parent 22ef151 commit 61f4a11

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

lib/Button.jsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Button extends React.Component {
4343
size,
4444
...attributes
4545
} = this.props;
46+
let Tag = 'button';
4647

4748
const classes = classNames(
4849
className,
@@ -54,21 +55,17 @@ class Button extends React.Component {
5455
);
5556

5657
if (El) {
57-
return (
58-
<El {...attributes}
59-
className={classes}
60-
onClick={this.onClick}>
61-
{children}
62-
</El>
63-
);
58+
Tag = El;
59+
} else if (attributes.href) {
60+
Tag = 'a';
6461
}
6562

6663
return (
67-
<button {...attributes}
64+
<Tag {...attributes}
6865
className={classes}
6966
onClick={this.onClick}>
7067
{children}
71-
</button>
68+
</Tag>
7269
);
7370
}
7471
}

test/Button.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ describe('Button', () => {
1818
expect(wrapper.text()).toBe('Home');
1919
});
2020

21+
it('should render an anchor element if href exists', () => {
22+
const wrapper = mount(<Button href="/home">Home</Button>);
23+
24+
expect(wrapper.find('a').length).toBe(1);
25+
expect(wrapper.text()).toBe('Home');
26+
});
27+
2128
it('should render buttons with default color', () => {
2229
const wrapper = shallow(<Button>Default Button</Button>);
2330

0 commit comments

Comments
 (0)