Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Button): handle type properly #4321

Merged
merged 2 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/collections/Form/FormButton.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export interface FormButtonProps extends StrictFormButtonProps {
[key: string]: any
}

export interface StrictFormButtonProps extends StrictFormFieldProps, StrictButtonProps {
export interface StrictFormButtonProps
extends StrictFormFieldProps,
Omit<StrictButtonProps, 'type'> {
/** An element type to render as (string or function). */
as?: any

Expand Down
3 changes: 3 additions & 0 deletions src/elements/Button/Button.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ export interface StrictButtonProps {

/** A button can be formatted to toggle on and off. */
toggle?: boolean

/** The type of the HTML element. */
type?: 'submit' | 'reset' | 'button'
}

declare class Button extends React.Component<ButtonProps> {
Expand Down
6 changes: 6 additions & 0 deletions src/elements/Button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Button extends Component {
secondary,
size,
toggle,
type,
} = this.props

const baseClasses = cx(
Expand Down Expand Up @@ -144,6 +145,7 @@ class Button extends Component {
className={buttonClasses}
aria-pressed={toggle ? !!active : undefined}
disabled={disabled}
type={type}
tabIndex={tabIndex}
>
{Icon.create(icon, { autoGenerateKey: false })} {content}
Expand All @@ -167,6 +169,7 @@ class Button extends Component {
disabled={(disabled && ElementType === 'button') || undefined}
onClick={this.handleClick}
role={role}
type={type}
tabIndex={tabIndex}
>
{hasChildren && children}
Expand Down Expand Up @@ -296,6 +299,9 @@ Button.propTypes = {

/** A button can be formatted to toggle on and off. */
toggle: PropTypes.bool,

/** The type of the HTML element. */
type: PropTypes.oneOf(['button', 'submit', 'reset']),
}

Button.defaultProps = {
Expand Down
20 changes: 20 additions & 0 deletions test/specs/elements/Button/Button-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,26 @@ describe('Button', () => {
})
})

describe('type', () => {
it('is not set by default', () => {
mount(<Button />)
.find('button')
.should.not.have.prop('type')
})

it('is passed to <button />', () => {
mount(<Button type='submit' />)
.find('button')
.should.have.prop('type', 'submit')
})

it('is passed to <button /> when "label" is defined', () => {
mount(<Button label='Foo' type='submit' />)
.find('button')
.should.have.prop('type', 'submit')
})
})

describe('tabIndex', () => {
it('is not set by default', () => {
shallow(<Button />, { autoNesting: true }).should.not.have.prop('tabIndex')
Expand Down