Skip to content

Commit

Permalink
feat(Input): Fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffcarbs committed Oct 5, 2016
1 parent 5ec08c7 commit 272d869
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/elements/Input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ function Input(props) {
)
const rest = getUnhandledProps(Input, props)
const ElementType = getElementType(Input, props)
const inputProps = _.pick(props, htmlInputPropNames)
const inputProps = {
..._.pick(props, htmlInputPropNames),
// React gives a warning if no onChange is given to a controlled input. We
// handle the onChange on the wrapping element, so we don't actually need
// onChange on the input itself but let's pass a noop to suppress the warning.
onChange: _.noop,
}

if (children) {
return <ElementType {...rest} className={classes}>{children}</ElementType>
Expand Down
22 changes: 21 additions & 1 deletion test/specs/elements/Input/Input-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react'

import Input, { htmlInputPropNames } from 'src/elements/Input/Input'
import * as common from 'test/specs/commonTests'
import { sandbox } from 'test/utils'

describe('Input', () => {
common.isConformant(Input)
Expand All @@ -27,7 +28,7 @@ describe('Input', () => {

common.implementsHTMLInputProp(Input, {
alwaysPresent: true,
shorthandDefaultProps: { type: 'text' },
shorthandDefaultProps: { type: 'text', onChange: _.noop },
})

common.propValueOnlyToClassName(Input, 'size')
Expand Down Expand Up @@ -62,4 +63,23 @@ describe('Input', () => {
})
})
})

describe('onChange', () => {
it('handles onChange', () => {
const onChangeSpy = sandbox.spy()
const wrapper = mount(<Input value='val' onChange={onChangeSpy} />)

wrapper
.find('input')
.simulate('change')

onChangeSpy.should.have.been.called()
})

it('supplies noop onChange to suppress controlled warning', () => {
shallow(<Input value='val' />)
.find('input')
.should.have.prop('onChange', _.noop)
})
})
})

0 comments on commit 272d869

Please sign in to comment.