Skip to content

Commit

Permalink
[TextField] Bind the focus/blur explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Jun 9, 2018
1 parent a5df649 commit 7243e5c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 50 deletions.
5 changes: 0 additions & 5 deletions docs/src/pages/demos/selection-controls/SwitchLabels.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import PropTypes from 'prop-types';
import FormGroup from '@material-ui/core/FormGroup';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Switch from '@material-ui/core/Switch';
Expand Down Expand Up @@ -46,8 +45,4 @@ class SwitchLabels extends React.Component {
}
}

SwitchLabels.propTypes = {
classes: PropTypes.object.isRequired,
};

export default SwitchLabels;
23 changes: 2 additions & 21 deletions packages/material-ui/src/FormControl/FormControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,11 @@ class FormControl extends React.Component {
};
}

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

handleBlur = event => {
// The event might be undefined.
// For instance, a child component might call this hook
// when an input is disabled but still having the focus.
if (this.props.onBlur && event) {
this.props.onBlur(event);
}
handleBlur = () => {
this.setState(state => (state.focused ? { focused: false } : null));
};

Expand Down Expand Up @@ -146,8 +137,6 @@ class FormControl extends React.Component {
className,
)}
{...other}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
/>
);
}
Expand Down Expand Up @@ -188,14 +177,6 @@ FormControl.propTypes = {
* If `dense` or `normal`, will adjust vertical spacing of this and contained components.
*/
margin: PropTypes.oneOf(['none', 'dense', 'normal']),
/**
* @ignore
*/
onBlur: PropTypes.func,
/**
* @ignore
*/
onFocus: PropTypes.func,
/**
* If `true`, the label will indicate that the input is required.
*/
Expand Down
30 changes: 7 additions & 23 deletions packages/material-ui/src/FormControl/FormControl.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import { spy } from 'sinon';
import { assert } from 'chai';
import { createShallow, getClasses } from '../test-utils';
import Input from '../Input';
Expand Down Expand Up @@ -223,38 +222,23 @@ describe('<FormControl />', () => {

describe('handleFocus', () => {
it('should set the focused state', () => {
assert.strictEqual(wrapper.state('focused'), false);
assert.strictEqual(wrapper.state().focused, false);
muiFormControlContext.onFocus();
assert.strictEqual(wrapper.state('focused'), true);
assert.strictEqual(wrapper.state().focused, true);
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);
assert.strictEqual(wrapper.state().focused, true);
});
});

describe('handleBlur', () => {
it('should clear the focused state', () => {
assert.strictEqual(wrapper.state('focused'), false);
assert.strictEqual(wrapper.state().focused, false);
muiFormControlContext.onFocus();
assert.strictEqual(wrapper.state('focused'), true);
assert.strictEqual(wrapper.state().focused, true);
muiFormControlContext.onBlur();
assert.strictEqual(wrapper.state('focused'), false);
assert.strictEqual(wrapper.state().focused, false);
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);
assert.strictEqual(wrapper.state().focused, false);
});
});
});
Expand Down
12 changes: 11 additions & 1 deletion packages/material-ui/src/Input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class Input extends React.Component {
input = null; // Holds the input reference

handleFocus = event => {
// Fix an bug with IE11 where the focus/blur events are triggered
// Fix a bug with IE11 where the focus/blur events are triggered
// while the input is disabled.
if (formControlState(this.props, this.context).disabled) {
event.stopPropagation();
Expand All @@ -300,13 +300,23 @@ class Input extends React.Component {
if (this.props.onFocus) {
this.props.onFocus(event);
}

const { muiFormControl } = this.context;
if (muiFormControl && muiFormControl.onFocus) {
muiFormControl.onFocus(event);
}
};

handleBlur = event => {
this.setState({ focused: false });
if (this.props.onBlur) {
this.props.onBlur(event);
}

const { muiFormControl } = this.context;
if (muiFormControl && muiFormControl.onBlur) {
muiFormControl.onBlur(event);
}
};

handleChange = event => {
Expand Down
32 changes: 32 additions & 0 deletions packages/material-ui/src/internal/SwitchBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ class SwitchBase extends React.Component {
input = null;
isControlled = null;

handleFocus = event => {
if (this.props.onFocus) {
this.props.onFocus(event);
}

const { muiFormControl } = this.context;
if (muiFormControl && muiFormControl.onFocus) {
muiFormControl.onFocus(event);
}
};

handleBlur = event => {
if (this.props.onBlur) {
this.props.onBlur(event);
}

const { muiFormControl } = this.context;
if (muiFormControl && muiFormControl.onBlur) {
muiFormControl.onBlur(event);
}
};

handleInputChange = event => {
const checked = event.target.checked;

Expand Down Expand Up @@ -105,6 +127,8 @@ class SwitchBase extends React.Component {
disabled={disabled}
tabIndex={null}
role={undefined}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
{...other}
>
{checked ? checkedIcon : icon}
Expand Down Expand Up @@ -186,6 +210,10 @@ SwitchBase.propTypes = {
* @ignore
*/
name: PropTypes.string,
/**
* @ignore
*/
onBlur: PropTypes.func,
/**
* Callback fired when the state is changed.
*
Expand All @@ -194,6 +222,10 @@ SwitchBase.propTypes = {
* @param {boolean} checked The `checked` value of the switch
*/
onChange: PropTypes.func,
/**
* @ignore
*/
onFocus: PropTypes.func,
/**
* @ignore
*/
Expand Down

0 comments on commit 7243e5c

Please sign in to comment.