This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathCheckbox.js
50 lines (43 loc) · 1.51 KB
/
Checkbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import React from 'react';
import PropTypes from 'prop-types';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import mdlUpgrade from './utils/mdlUpgrade';
const propTypes = {
checked: PropTypes.bool,
className: PropTypes.string,
disabled: PropTypes.bool,
label: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
onChange: PropTypes.func,
ripple: PropTypes.bool
};
class Checkbox extends React.Component {
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 { className, label, ripple, ...inputProps } = this.props;
const classes = classNames('mdl-checkbox mdl-js-checkbox', {
'mdl-js-ripple-effect': ripple
}, className);
return (
<label className={classes}>
<input
type="checkbox"
className="mdl-checkbox__input"
{ ...inputProps }
/>
{label && <span className="mdl-checkbox__label">{label}</span>}
</label>
);
}
}
Checkbox.propTypes = propTypes;
export default mdlUpgrade(Checkbox, true);