Skip to content

Commit

Permalink
feat(Input): add focus() method (#1477)
Browse files Browse the repository at this point in the history
* fix(ComponentDoc): copy current editor code

* fix(Button): fix oneOfType propType

* feat(Input): add focus() method
  • Loading branch information
levithomason authored Mar 18, 2017
1 parent 67c737f commit 6cc4884
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/app/Components/ComponentDoc/ComponentExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ComponentExample extends Component {
}

copyJSX = () => {
copyToClipboard(this.getOriginalSourceCode())
copyToClipboard(this.state.sourceCode)
this.setState({ copiedCode: true })
setTimeout(() => this.setState({ copiedCode: false }), 1000)
}
Expand Down
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
4 changes: 2 additions & 2 deletions src/elements/Button/ButtonOr.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ ButtonOr.propTypes = {
className: PropTypes.string,

/** Or buttons can have their text localized, or adjusted by using the text prop. */
text: PropTypes.oneOfType(
text: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
),
]),
}

export default ButtonOr
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 6cc4884

Please sign in to comment.