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 1 commit
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
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,
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use factory instead of cloneElement

})

return <ElementType {...rest} className={classes}>{childElements}</ElementType>
Expand Down
51 changes: 32 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,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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@levithomason I can't get this test working, can you take a look?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refs are not passed through props. React makes them undefined. We'll have to accept the ref on a different prop name, or, update factories to include refs in the overrides. We should probably use a different prop name, such as, inputRef.

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