Skip to content

Commit

Permalink
[TextField] Fix dirty state update (mui#8879)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored and the-noob committed Nov 17, 2017
1 parent ec8b9c9 commit 7820eec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Input/Input.js
Expand Up @@ -384,7 +384,7 @@ class Input extends React.Component<ProvidedProps & Props, State> {
}

componentWillUpdate(nextProps) {
if (this.isControlled()) {
if (this.isControlled(nextProps)) {
this.checkDirty(nextProps);
} // else performed in the onChange

Expand Down Expand Up @@ -436,8 +436,8 @@ class Input extends React.Component<ProvidedProps & Props, State> {
//
// @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) {
Expand Down
14 changes: 9 additions & 5 deletions src/Input/Input.spec.js
Expand Up @@ -127,6 +127,14 @@ describe('<Input />', () => {
});

describe('controlled', () => {
it('should fire the onDirty callback when moving from uncontrolled to controlled', () => {
const handleDirty = spy();
const wrapper = shallow(<Input value={undefined} onDirty={handleDirty} />);
assert.strictEqual(handleDirty.callCount, 0);
wrapper.setProps({ value: 'foo' });
assert.strictEqual(handleDirty.callCount, 1);
});

['', 0].forEach(value => {
describe(`${typeof value} value`, () => {
let wrapper;
Expand All @@ -151,11 +159,7 @@ describe('<Input />', () => {
});

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');
});
Expand Down

0 comments on commit 7820eec

Please sign in to comment.