Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #136 from noisecapella/master
Browse files Browse the repository at this point in the history
Fixes programmatic handling of checkboxes
  • Loading branch information
tleunen committed Dec 1, 2015
2 parents 25e2a9d + 9ab26c9 commit 16da125
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Checkbox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { PropTypes } from 'react';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import mdlUpgrade from './utils/mdlUpgrade';

Expand All @@ -11,6 +12,17 @@ class Checkbox extends React.Component {
ripple: PropTypes.bool
}

componentDidUpdate(prevProps) {
if(this.props.disabled !== prevProps.disabled) {
const fnName = this.props.disabled ? 'disable' : 'enable';
findDOMNode(this).MaterialCheckbox[fnName]();
}
if(this.props.checked !== prevProps.checked) {
const fnName = this.props.checked ? 'check' : 'uncheck';
findDOMNode(this).MaterialCheckbox[fnName]();
}
}

render() {
const { label, ripple, ...inputProps } = this.props;

Expand Down
26 changes: 25 additions & 1 deletion src/__tests__/Checkbox-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-env mocha */
import expect from 'expect';
import React from 'react';
import { render } from './render';
import ReactDOM from 'react-dom';
import { render, renderDOM } from './render';
import Checkbox from '../Checkbox';

describe('Checkbox', () => {
Expand Down Expand Up @@ -65,4 +66,27 @@ describe('Checkbox', () => {
expect(output.type).toBe('label');
expect(output.props.className).toInclude('mdl-js-ripple-effect');
});

describe('should update the Checkbox after the first render', () => {
it('when `checked` has changed', () => {
const el = renderDOM(<Checkbox />);

expect(el.className).toExclude('is-checked');

ReactDOM.render(<Checkbox checked />, el.parentNode);
expect(el.className).toInclude('is-checked');
ReactDOM.render(<Checkbox />, el.parentNode);
expect(el.className).toExclude('is-checked');
});

it('when `disable` has changed', () => {
const el = renderDOM(<Checkbox />);

expect(el.className).toExclude('is-disabled');
ReactDOM.render(<Checkbox disabled />, el.parentNode);
expect(el.className).toInclude('is-disabled');
ReactDOM.render(<Checkbox />, el.parentNode);
expect(el.className).toExclude('is-disabled');
});
});
});

0 comments on commit 16da125

Please sign in to comment.