Skip to content

Commit

Permalink
[FormControl] Fix onFocus and onBlur events override (#6952)
Browse files Browse the repository at this point in the history
CLoses #6940
  • Loading branch information
oliviertassinari committed May 25, 2017
1 parent 2611239 commit 9c79509
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Form/FormControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ class FormControl extends Component {
}

handleFocus = () => {
if (this.props.onFocus) {
this.props.onFocus();
}
if (!this.state.focused) {
this.setState({ focused: true });
}
};

handleBlur = () => {
if (this.props.onBlur) {
this.props.onBlur();
}
if (this.state.focused) {
this.setState({ focused: false });
}
Expand Down Expand Up @@ -84,10 +90,10 @@ class FormControl extends Component {

return (
<div
onFocus={this.handleFocus}
onBlur={this.handleBlur}
className={classNames(classes.root, className)}
{...other}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
>
{children}
</div>
Expand All @@ -112,6 +118,14 @@ FormControl.propTypes = {
* If `true`, the label should be displayed in an error state.
*/
error: PropTypes.bool,
/**
* @ignore
*/
onBlur: PropTypes.func,
/**
* @ignore
*/
onFocus: PropTypes.func,
/**
* If `true`, the label will indicate that the input is required.
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Form/FormControl.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import React from 'react';
import { spy } from 'sinon';
import { assert } from 'chai';
import { createShallow } from '../test-utils';
import FormControl, { styleSheet } from './FormControl';
Expand Down Expand Up @@ -124,6 +125,15 @@ describe('<FormControl />', () => {
muiFormControlContext.onFocus();
assert.strictEqual(wrapper.state('focused'), true);
});

it('should be able to use a onFocus property', () => {
const handleFocus = spy();
wrapper.setProps({
onFocus: handleFocus,
});
muiFormControlContext.onFocus();
assert.strictEqual(handleFocus.callCount, 1);
});
});

describe('handleBlur', () => {
Expand All @@ -136,6 +146,16 @@ describe('<FormControl />', () => {
muiFormControlContext.onBlur();
assert.strictEqual(wrapper.state('focused'), false);
});

it('should be able to use a onBlur property', () => {
const handleBlur = spy();
wrapper.setProps({
onBlur: handleBlur,
});
muiFormControlContext.onFocus();
muiFormControlContext.onBlur();
assert.strictEqual(handleBlur.callCount, 1);
});
});
});
});
Expand Down

0 comments on commit 9c79509

Please sign in to comment.