Skip to content

Commit

Permalink
Merge pull request #4951 from dugokontov/cancel-hover-on-disable-for-…
Browse files Browse the repository at this point in the history
…floating-action-button

[FloatingActionButton] Reset hover state when disabled prop is changed
  • Loading branch information
oliviertassinari authored Aug 15, 2016
2 parents 08e8765 + be15066 commit ccff9c6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/FlatButton/FlatButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class FlatButton extends Component {
};

componentWillReceiveProps(nextProps) {
if (nextProps.disabled && this.state.hovered) {
if (nextProps.disabled) {
this.setState({
hovered: false,
});
Expand Down
11 changes: 8 additions & 3 deletions src/FloatingActionButton/FloatingActionButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,16 @@ class FloatingActionButton extends Component {
}

componentWillReceiveProps(nextProps) {
const nextState = {};

if (nextProps.disabled !== this.props.disabled) {
this.setState({
zDepth: nextProps.disabled ? 0 : this.props.zDepth,
});
nextState.zDepth = nextProps.disabled ? 0 : this.props.zDepth;
}
if (nextProps.disabled) {
nextState.hovered = false;
}

this.setState(nextState);
}

handleMouseDown = (event) => {
Expand Down
30 changes: 30 additions & 0 deletions src/FloatingActionButton/FloatingActionButton.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-env mocha */
import React from 'react';
import {shallow} from 'enzyme';
import {assert} from 'chai';

import FloatingActionButton from './FloatingActionButton';
import getMuiTheme from '../styles/getMuiTheme';
import ContentAdd from '../svg-icons/content/add';

describe('<FloatingActionButton />', () => {
const muiTheme = getMuiTheme();
const shallowWithContext = (node) => shallow(node, {context: {muiTheme}});

describe('hover state', () => {
it('should reset the hover state when disabled', () => {
const wrapper = shallowWithContext(
<FloatingActionButton>
<ContentAdd />
</FloatingActionButton>
);
wrapper.setState({
hovered: true,
});
wrapper.setProps({
disabled: true,
});
assert.strictEqual(wrapper.state().hovered, false, 'should reset the state');
});
});
});
2 changes: 1 addition & 1 deletion src/RaisedButton/RaisedButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class RaisedButton extends Component {
initialZDepth: zDepth,
};

if (nextProps.disabled && this.state.hovered) {
if (nextProps.disabled) {
nextState.hovered = false;
}

Expand Down

0 comments on commit ccff9c6

Please sign in to comment.