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 focus method #1764

Merged
merged 1 commit into from
Jun 15, 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
28 changes: 28 additions & 0 deletions docs/app/Examples/elements/Button/Usage/ButtonExampleFocus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { Component } from 'react'
import { Button, Grid } from 'semantic-ui-react'

export default class ButtonExampleFocus extends Component {
handleClick = () => this.ref.focus()

handleRef = c => (this.ref = c)

render() {
return (
<Grid>
<Grid.Column width={8}>
<Button
content='A button that can be focused'
primary
ref={this.handleRef}
/>
</Grid.Column>
<Grid.Column width={8}>
<Button
content='Set focused'
onClick={this.handleClick}
/>
</Grid.Column>
</Grid>
)
}
}
16 changes: 16 additions & 0 deletions docs/app/Examples/elements/Button/Usage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'

import ComponentExample from 'docs/app/Components/ComponentDoc/ComponentExample'
import ExampleSection from 'docs/app/Components/ComponentDoc/ExampleSection'

const ButtonUsageExamples = () => (
<ExampleSection title='Usage'>
<ComponentExample
title='Focus'
description='A button can be focused.'
examplePath='elements/Button/Usage/ButtonExampleFocus'
/>
</ExampleSection>
)

export default ButtonUsageExamples
9 changes: 6 additions & 3 deletions docs/app/Examples/elements/Button/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react'
import Types from './Types'
import Groups from './Groups'

import Content from './Content'
import Groups from './Groups'
import GroupVariations from './GroupVariations'
import States from './States'
import Types from './Types'
import Variations from './Variations'
import GroupVariations from './GroupVariations'
import Usage from './Usage'

const ButtonExamples = () => (
<div>
Expand All @@ -14,6 +16,7 @@ const ButtonExamples = () => (
<States />
<Variations />
<GroupVariations />
<Usage />
</div>
)

Expand Down
12 changes: 8 additions & 4 deletions src/elements/Button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ class Button extends Component {
if (ElementType === 'div') return 0
}

focus = () => _.invoke(this.ref, 'focus')

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

Expand All @@ -186,6 +188,8 @@ class Button extends Component {
if (onClick) onClick(e, this.props)
}

handleRef = c => (this.ref = c)

hasIconClass = () => {
const { labelPosition, children, content, icon } = this.props

Expand Down Expand Up @@ -257,7 +261,7 @@ class Button extends Component {
debug('render children:', { classes })

return (
<ElementType {...rest} className={classes} tabIndex={tabIndex} onClick={this.handleClick}>
<ElementType {...rest} className={classes} onClick={this.handleClick} ref={this.handleRef} tabIndex={tabIndex}>
{children}
</ElementType>
)
Expand All @@ -277,7 +281,7 @@ class Button extends Component {
return (
<ElementType {...rest} className={containerClasses} onClick={this.handleClick}>
{labelPosition === 'left' && labelElement}
<button className={classes} tabIndex={tabIndex}>
<button className={classes} ref={this.handleRef} tabIndex={tabIndex}>
{Icon.create(icon)} {content}
</button>
{(labelPosition === 'right' || !labelPosition) && labelElement}
Expand All @@ -290,7 +294,7 @@ class Button extends Component {
debug('render icon && !label:', { classes })

return (
<ElementType {...rest} className={classes} tabIndex={tabIndex} onClick={this.handleClick}>
<ElementType {...rest} className={classes} onClick={this.handleClick} ref={this.handleRef} tabIndex={tabIndex}>
{Icon.create(icon)} {content}
</ElementType>
)
Expand All @@ -300,7 +304,7 @@ class Button extends Component {
debug('render default:', { classes })

return (
<ElementType {...rest} className={classes} tabIndex={tabIndex} onClick={this.handleClick}>
<ElementType {...rest} className={classes} onClick={this.handleClick} ref={this.handleRef} tabIndex={tabIndex}>
{content}
</ElementType>
)
Expand Down
16 changes: 16 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,22 @@ describe('Button', () => {
})
})

describe('focus', () => {
it('can be set via a ref', () => {
const mountNode = document.createElement('div')
document.body.appendChild(mountNode)

const wrapper = mount(<Button />, { attachTo: mountNode })
wrapper.instance().focus()

const button = document.querySelector('button')
document.activeElement.should.equal(button)

wrapper.detach()
document.body.removeChild(mountNode)
})
})

describe('icon', () => {
it('adds className icon', () => {
shallow(<Button icon='user' />)
Expand Down