Skip to content

Commit

Permalink
fix(Button): Use type attribute if specified (#5208)
Browse files Browse the repository at this point in the history
* fix(Button): Apply type attribute if "as" is an input

* Use type prop if explicitly passed in
  • Loading branch information
kyletsang committed Jun 15, 2020
1 parent 8f23c13 commit 426b6da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const defaultProps = {
variant: 'primary',
active: false,
disabled: false,
type: 'button',
};

const Button = React.forwardRef(
Expand Down Expand Up @@ -94,8 +93,10 @@ const Button = React.forwardRef(
props.ref = ref;
}

if (!as) {
if (type) {
props.type = type;
} else if (!as) {
props.type = 'button';
}

const Component = as || 'button';
Expand Down
17 changes: 17 additions & 0 deletions test/ButtonSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ describe('<Button>', () => {
.should.equal('submit');
});

it('Should show the type if explicitly passed in when "as" is used', () => {
mount(
<Button as="div" type="submit">
Title
</Button>,
)
.getDOMNode()
.getAttribute('type')
.should.equal('submit');
});

it('Should not have default type=button when "as" is used', () => {
const wrapper = mount(<Button as="div">Title</Button>);

expect(wrapper.getDOMNode().getAttribute('type')).to.be.null;
});

it('should forward refs to the button', () => {
const ref = React.createRef();
mount(
Expand Down

0 comments on commit 426b6da

Please sign in to comment.