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

fix(Input): add handling of input's ref #1581

Merged
merged 2 commits into from
Apr 16, 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
17 changes: 12 additions & 5 deletions src/elements/Input/Input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cx from 'classnames'
import _ from 'lodash'
import React, { Children, cloneElement, Component, PropTypes } from 'react'
import cx from 'classnames'

import {
createHTMLInput,
Expand Down Expand Up @@ -114,16 +114,23 @@ 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()
}
handleChildOverrides = (child, defaultProps) => ({
...defaultProps,
...child.props,
ref: c => {
_.invoke(child, 'ref', c)
this.handleInputRef(c)
},
})

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

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

return cloneElement(child, { ...htmlInputProps, ...child.props })
return cloneElement(child, this.handleChildOverrides(child, htmlInputProps))
})

return <ElementType {...rest} className={classes}>{childElements}</ElementType>
Expand Down
59 changes: 40 additions & 19 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.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,47 @@ describe('Input', () => {
})
})

describe('ref', () => {
it('maintains ref on child node', () => {
const ref = sandbox.spy()
const mountNode = document.createElement('div')
document.body.appendChild(mountNode)

const wrapper = mount(<Input><input ref={ref} /></Input>, { attachTo: mountNode })
const input = document.querySelector('.ui.input input')

ref.should.have.been.calledOnce()
ref.should.have.been.calledWithMatch(input)
wrapper.instance().inputRef.should.equal(input)

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

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