Skip to content

Commit

Permalink
feat(Input): add focus() method
Browse files Browse the repository at this point in the history
  • Loading branch information
levithomason committed Mar 18, 2017
1 parent bc4c999 commit 6efe8a3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/app/Examples/elements/Input/Usage/InputExampleRefFocus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, { Component } from 'react'
import { Input, Button } from 'semantic-ui-react'

class InputExampleRefFocus extends Component {
handleRef = c => {
this.inputRef = c
}

focus = () => {
this.inputRef.focus()
}

render() {
return (
<div>
<Button content='focus' onClick={this.focus} />
<Input ref={this.handleRef} placeholder='Search...' />
</div>
)
}
}

export default InputExampleRefFocus
15 changes: 15 additions & 0 deletions docs/app/Examples/elements/Input/Usage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import ComponentExample from 'docs/app/Components/ComponentDoc/ComponentExample'
import ExampleSection from 'docs/app/Components/ComponentDoc/ExampleSection'

const InputUsageExamples = () => (
<ExampleSection title='Usage'>
<ComponentExample
title='Focus'
description='An input can be focused via a ref.'
examplePath='elements/Input/Usage/InputExampleRefFocus'
/>
</ExampleSection>
)

export default InputUsageExamples
2 changes: 2 additions & 0 deletions docs/app/Examples/elements/Input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React from 'react'
import Types from './Types'
import States from './States'
import Variations from './Variations'
import Usage from './Usage'

const InputExamples = () => (
<div>
<Types />
<States />
<Variations />
<Usage />
</div>
)

Expand Down
7 changes: 7 additions & 0 deletions src/elements/Input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ class Input extends Component {
onChange(e, { ...this.props, value })
}

focus = () => {
this.inputRef.focus()
}

handleInputRef = c => (this.inputRef = c)

render() {
const {
action,
Expand Down Expand Up @@ -168,6 +174,7 @@ class Input extends Component {
const [htmlInputProps, rest] = partitionHTMLInputProps({ ...unhandled, type })

if (onChange) htmlInputProps.onChange = this.handleChange
htmlInputProps.ref = this.handleInputRef

// tabIndex
if (!_.isNil(tabIndex)) htmlInputProps.tabIndex = tabIndex
Expand Down
16 changes: 16 additions & 0 deletions test/specs/elements/Input/Input-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,20 @@ describe('Input', () => {
.should.have.prop('tabIndex', 123)
})
})

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

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

const input = document.querySelector('.ui.input input')
document.activeElement.should.equal(input)

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

0 comments on commit 6efe8a3

Please sign in to comment.