Skip to content

Commit

Permalink
fix(Input): add handling of input's ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Fedyashov committed Apr 13, 2017
1 parent bf09457 commit cb0e4cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
20 changes: 14 additions & 6 deletions src/elements/Input/Input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash'
import React, { Children, cloneElement, Component, PropTypes } from 'react'
import cx from 'classnames'
import _ from 'lodash'
import React, { Children, Component, PropTypes } from 'react'

import {
createHTMLInput,
Expand Down Expand Up @@ -114,16 +114,21 @@ class Input extends Component {
type: META.TYPES.ELEMENT,
}

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

handleChange = (e) => {
const { onChange } = this.props
const value = _.get(e, 'target.value')

onChange(e, { ...this.props, value })
}

focus = () => {
this.inputRef.focus()
}
handleInputOverrides = predefinedProps => ({
ref: c => {
_.invoke(predefinedProps, 'ref', c)
this.handleInputRef(c)
},
})

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

Expand Down Expand Up @@ -187,7 +192,10 @@ class Input extends Component {
const childElements = _.map(Children.toArray(children), (child) => {
if (child.type !== 'input') return child

return cloneElement(child, { ...htmlInputProps, ...child.props })
return createHTMLInput(child, {
defaultProps: htmlInputProps,
overrideProps: this.handleInputOverrides,
})
})

return <ElementType {...rest} className={classes}>{childElements}</ElementType>
Expand Down
53 changes: 33 additions & 20 deletions test/specs/elements/Input/Input-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ describe('Input', () => {
common.hasUIClassName(Input)
common.rendersChildren(Input)

common.implementsLabelProp(Input, {
shorthandDefaultProps: { className: 'label' },
})
common.implementsCreateMethod(Input)
common.implementsButtonProp(Input, {
propKey: 'action',
shorthandDefaultProps: { className: 'button' },
})
common.implementsCreateMethod(Input)
common.implementsLabelProp(Input, {
shorthandDefaultProps: { className: 'label' },
})
common.implementsHTMLInputProp(Input, {
alwaysPresent: true,
shorthandDefaultProps: { type: 'text' },
Expand Down Expand Up @@ -138,6 +138,22 @@ describe('Input', () => {
})
})

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)
})
})

describe('onChange', () => {
it('is called with (e, data) on change', () => {
const spy = sandbox.spy()
Expand Down Expand Up @@ -170,42 +186,39 @@ describe('Input', () => {
})
})

describe('ref', () => {
it('maintains ref on child node', () => {
const ref = sandbox.spy()
const wrapper = mount(<Input><input ref={ref} /></Input>)

// ref.should.have.been.calledOnce()
wrapper.instance().inputRef.tagName.should.equal('INPUT')
})
})

describe('tabIndex', () => {
it('is not set by default', () => {
shallow(<Input />)
.find('input')
.should.not.have.prop('tabIndex')
})

it('defaults to -1 when disabled', () => {
shallow(<Input disabled />)
.find('input')
.should.have.prop('tabIndex', -1)
})

it('can be set explicitly', () => {
shallow(<Input tabIndex={123} />)
.find('input')
.should.have.prop('tabIndex', 123)
})

it('can be set explicitly when disabled', () => {
shallow(<Input tabIndex={123} disabled />)
.find('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 cb0e4cf

Please sign in to comment.