Skip to content

Commit 21be1b6

Browse files
authored
fix(Button): handle type properly (#4321)
* fix(Button): handle `type` properly * fix typings for FormButton
1 parent 6e4e98a commit 21be1b6

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/collections/Form/FormButton.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export interface FormButtonProps extends StrictFormButtonProps {
99
[key: string]: any
1010
}
1111

12-
export interface StrictFormButtonProps extends StrictFormFieldProps, StrictButtonProps {
12+
export interface StrictFormButtonProps
13+
extends StrictFormFieldProps,
14+
Omit<StrictButtonProps, 'type'> {
1315
/** An element type to render as (string or function). */
1416
as?: any
1517

src/elements/Button/Button.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ export interface StrictButtonProps {
113113

114114
/** A button can be formatted to toggle on and off. */
115115
toggle?: boolean
116+
117+
/** The type of the HTML element. */
118+
type?: 'submit' | 'reset' | 'button'
116119
}
117120

118121
declare class Button extends React.Component<ButtonProps> {

src/elements/Button/Button.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class Button extends Component {
9797
secondary,
9898
size,
9999
toggle,
100+
type,
100101
} = this.props
101102

102103
const baseClasses = cx(
@@ -144,6 +145,7 @@ class Button extends Component {
144145
className={buttonClasses}
145146
aria-pressed={toggle ? !!active : undefined}
146147
disabled={disabled}
148+
type={type}
147149
tabIndex={tabIndex}
148150
>
149151
{Icon.create(icon, { autoGenerateKey: false })} {content}
@@ -167,6 +169,7 @@ class Button extends Component {
167169
disabled={(disabled && ElementType === 'button') || undefined}
168170
onClick={this.handleClick}
169171
role={role}
172+
type={type}
170173
tabIndex={tabIndex}
171174
>
172175
{hasChildren && children}
@@ -296,6 +299,9 @@ Button.propTypes = {
296299

297300
/** A button can be formatted to toggle on and off. */
298301
toggle: PropTypes.bool,
302+
303+
/** The type of the HTML element. */
304+
type: PropTypes.oneOf(['button', 'submit', 'reset']),
299305
}
300306

301307
Button.defaultProps = {

test/specs/elements/Button/Button-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,26 @@ describe('Button', () => {
260260
})
261261
})
262262

263+
describe('type', () => {
264+
it('is not set by default', () => {
265+
mount(<Button />)
266+
.find('button')
267+
.should.not.have.prop('type')
268+
})
269+
270+
it('is passed to <button />', () => {
271+
mount(<Button type='submit' />)
272+
.find('button')
273+
.should.have.prop('type', 'submit')
274+
})
275+
276+
it('is passed to <button /> when "label" is defined', () => {
277+
mount(<Button label='Foo' type='submit' />)
278+
.find('button')
279+
.should.have.prop('type', 'submit')
280+
})
281+
})
282+
263283
describe('tabIndex', () => {
264284
it('is not set by default', () => {
265285
shallow(<Button />, { autoNesting: true }).should.not.have.prop('tabIndex')

0 commit comments

Comments
 (0)