diff --git a/src/Input/Input.js b/src/Input/Input.js index d75c22dd729eb7..82bdbaedc45e4c 100644 --- a/src/Input/Input.js +++ b/src/Input/Input.js @@ -384,7 +384,7 @@ class Input extends React.Component { } componentWillUpdate(nextProps) { - if (this.isControlled()) { + if (this.isControlled(nextProps)) { this.checkDirty(nextProps); } // else performed in the onChange @@ -436,8 +436,8 @@ class Input extends React.Component { // // @see https://facebook.github.io/react/docs/forms.html#controlled-components // @returns {boolean} true if string (including '') or number (including zero) - isControlled() { - return hasValue(this.props.value); + isControlled(props = this.props) { + return hasValue(props.value); } checkDirty(obj) { diff --git a/src/Input/Input.spec.js b/src/Input/Input.spec.js index c3cdcf0d7fc81c..1168cea18a241a 100644 --- a/src/Input/Input.spec.js +++ b/src/Input/Input.spec.js @@ -127,6 +127,14 @@ describe('', () => { }); describe('controlled', () => { + it('should fire the onDirty callback when moving from uncontrolled to controlled', () => { + const handleDirty = spy(); + const wrapper = shallow(); + assert.strictEqual(handleDirty.callCount, 0); + wrapper.setProps({ value: 'foo' }); + assert.strictEqual(handleDirty.callCount, 1); + }); + ['', 0].forEach(value => { describe(`${typeof value} value`, () => { let wrapper; @@ -151,11 +159,7 @@ describe('', () => { }); it('should fire the onDirty callback when dirtied', () => { - assert.strictEqual( - handleDirty.callCount, - 0, - 'should not have called the onDirty cb yet', - ); + assert.strictEqual(handleDirty.callCount, 0); wrapper.setProps({ value: typeof value === 'number' ? 2 : 'hello' }); assert.strictEqual(handleDirty.callCount, 1, 'should have called the onDirty cb'); });