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

feat(Button): add advanced handling of disabled prop #1781

Merged
merged 1 commit into from
Jun 19, 2017
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
63 changes: 22 additions & 41 deletions src/elements/Button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
createShorthandFactory,
getElementType,
getUnhandledProps,
makeDebugger,
META,
SUI,
useKeyOnly,
Expand All @@ -21,8 +20,6 @@ import ButtonContent from './ButtonContent'
import ButtonGroup from './ButtonGroup'
import ButtonOr from './ButtonOr'

const debug = makeDebugger('button')

/**
* A Button indicates a possible user action.
* @see Form
Expand Down Expand Up @@ -178,14 +175,14 @@ class Button extends Component {
focus = () => _.invoke(this.ref, 'focus')

handleClick = (e) => {
const { disabled, onClick } = this.props
const { disabled } = this.props

if (disabled) {
e.preventDefault()
return
}

if (onClick) onClick(e, this.props)
_.invoke(this.props, 'onClick', e, this.props)
}

handleRef = c => (this.ref = c)
Expand Down Expand Up @@ -256,56 +253,40 @@ class Button extends Component {
const ElementType = getElementType(Button, this.props, this.computeElementType)
const tabIndex = this.computeTabIndex(ElementType)

if (!_.isNil(children)) {
const classes = cx('ui', baseClasses, wrapperClasses, labeledClasses, 'button', className)
debug('render children:', { classes })

return (
<ElementType {...rest} className={classes} onClick={this.handleClick} ref={this.handleRef} tabIndex={tabIndex}>
{children}
</ElementType>
)
}

const labelElement = Label.create(label, { defaultProps: {
basic: true,
pointing: labelPosition === 'left' ? 'right' : 'left',
} })

if (labelElement) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this condition doesn't makes sence, we should use there check for the label prop.

const classes = cx('ui', baseClasses, 'button', className)
if (!_.isNil(label)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you take attention to these conditions you can see, that in fact there are two conditions: when the Button has a container and has not.

const buttonClasses = cx('ui', baseClasses, 'button', className)
const containerClasses = cx('ui', labeledClasses, 'button', className, wrapperClasses)

debug('render label:', { classes, containerClasses }, this.props)
const labelElement = Label.create(label, { defaultProps: {
basic: true,
pointing: labelPosition === 'left' ? 'right' : 'left',
} })

return (
<ElementType {...rest} className={containerClasses} onClick={this.handleClick}>
{labelPosition === 'left' && labelElement}
<button className={classes} ref={this.handleRef} tabIndex={tabIndex}>
<button className={buttonClasses} disabled={disabled} ref={this.handleRef} tabIndex={tabIndex}>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTML's button tag can have a disabled attr.

{Icon.create(icon)} {content}
</button>
{(labelPosition === 'right' || !labelPosition) && labelElement}
</ElementType>
)
}

if (!_.isNil(icon) && _.isNil(label)) {
const classes = cx('ui', labeledClasses, baseClasses, 'button', className, wrapperClasses)
debug('render icon && !label:', { classes })

return (
<ElementType {...rest} className={classes} onClick={this.handleClick} ref={this.handleRef} tabIndex={tabIndex}>
{Icon.create(icon)} {content}
</ElementType>
)
}

const classes = cx('ui', labeledClasses, baseClasses, 'button', className, wrapperClasses)
debug('render default:', { classes })
const classes = cx('ui', baseClasses, wrapperClasses, labeledClasses, 'button', className)
const hasChildren = !_.isNil(children)

return (
<ElementType {...rest} className={classes} onClick={this.handleClick} ref={this.handleRef} tabIndex={tabIndex}>
{content}
<ElementType
{...rest}
className={classes}
disabled={(disabled && ElementType === 'button') || undefined}
onClick={this.handleClick}
ref={this.handleRef}
tabIndex={tabIndex}
>
{hasChildren && children}
{!hasChildren && Icon.create(icon)}
{!hasChildren && content}
</ElementType>
)
}
Expand Down
36 changes: 36 additions & 0 deletions test/specs/elements/Button/Button-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,35 @@ describe('Button', () => {
})
})

describe('disabled', () => {
it('is not set by default', () => {
shallow(<Button />)
.should.not.have.prop('disabled')
})

it('applied when defined', () => {
shallow(<Button disabled />)
.should.have.prop('disabled', true)
})

it("don't apply when the element's type isn't button", () => {
shallow(<Button as='div' disabled />)
.should.not.have.prop('disabled')
})

it('is not set by default when has a label', () => {
shallow(<Button label='foo' />)
.find('button')
.should.not.have.prop('disabled')
})

it('applied when defined and has a label', () => {
shallow(<Button disabled label='foo' />)
.find('button')
.should.have.prop('disabled', true)
})
})

describe('focus', () => {
it('can be set via a ref', () => {
const mountNode = document.createElement('div')
Expand All @@ -92,12 +121,19 @@ describe('Button', () => {
shallow(<Button icon='user' />)
.should.have.className('icon')
})

it('adds className icon when true', () => {
shallow(<Button icon />)
.should.have.className('icon')
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test


it('does not add className icon when there is content', () => {
shallow(<Button icon='user' content={0} />)
.should.not.have.className('icon')
shallow(<Button icon='user' content='Yo' />)
.should.not.have.className('icon')
})

it('adds className icon given labelPosition and content', () => {
shallow(<Button labelPosition='left' icon='user' content='My Account' />)
.should.have.className('icon')
Expand Down