Skip to content

Commit 426b6da

Browse files
authored
fix(Button): Use type attribute if specified (react-bootstrap#5208)
* fix(Button): Apply type attribute if "as" is an input * Use type prop if explicitly passed in
1 parent 8f23c13 commit 426b6da

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Button.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ const defaultProps = {
6060
variant: 'primary',
6161
active: false,
6262
disabled: false,
63-
type: 'button',
6463
};
6564

6665
const Button = React.forwardRef(
@@ -94,8 +93,10 @@ const Button = React.forwardRef(
9493
props.ref = ref;
9594
}
9695

97-
if (!as) {
96+
if (type) {
9897
props.type = type;
98+
} else if (!as) {
99+
props.type = 'button';
99100
}
100101

101102
const Component = as || 'button';

test/ButtonSpec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ describe('<Button>', () => {
2626
.should.equal('submit');
2727
});
2828

29+
it('Should show the type if explicitly passed in when "as" is used', () => {
30+
mount(
31+
<Button as="div" type="submit">
32+
Title
33+
</Button>,
34+
)
35+
.getDOMNode()
36+
.getAttribute('type')
37+
.should.equal('submit');
38+
});
39+
40+
it('Should not have default type=button when "as" is used', () => {
41+
const wrapper = mount(<Button as="div">Title</Button>);
42+
43+
expect(wrapper.getDOMNode().getAttribute('type')).to.be.null;
44+
});
45+
2946
it('should forward refs to the button', () => {
3047
const ref = React.createRef();
3148
mount(

0 commit comments

Comments
 (0)