react-toolbox/components/checkbox/index.jsx

79 lines
2.0 KiB
React
Raw Normal View History

import React from 'react';
2015-10-05 10:12:16 +03:00
import Ripple from '../ripple';
import events from '../utils/events';
2015-10-22 02:31:17 +03:00
import style from './style';
2015-10-05 10:12:16 +03:00
2015-10-22 02:31:17 +03:00
class Checkbox extends React.Component {
static propTypes = {
2015-10-05 10:12:16 +03:00
checked: React.PropTypes.bool,
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
2015-10-19 03:38:25 +03:00
label: React.PropTypes.any,
name: React.PropTypes.string,
2015-10-05 10:12:16 +03:00
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func,
onFocus: React.PropTypes.func
};
2015-10-05 10:12:16 +03:00
static defaultProps = {
checked: false,
className: '',
disabled: false
};
2015-10-05 10:12:16 +03:00
2015-10-22 02:31:17 +03:00
handleClick = (event) => {
2015-10-05 10:12:16 +03:00
events.pauseEvent(event);
2015-11-12 03:24:31 +03:00
if (!this.props.disabled && this.props.onChange) {
this.props.onChange(event);
}
2015-10-22 02:31:17 +03:00
};
2015-10-05 10:12:16 +03:00
2015-10-22 02:31:17 +03:00
handleInputClick = (event) => {
events.pauseEvent(event);
2015-10-22 02:31:17 +03:00
};
handleMouseDown = (event) => {
2015-11-12 20:58:06 +03:00
if (!this.props.disabled) {
this.refs.ripple.start(event);
}
};
2015-10-05 10:12:16 +03:00
render () {
2015-10-08 13:23:42 +03:00
let fieldClassName = style.field;
let checkboxClassName = style.check;
2015-11-07 04:59:50 +03:00
if (this.props.checked) checkboxClassName += ` ${style.checked}`;
2015-10-08 13:23:42 +03:00
if (this.props.disabled) fieldClassName += ` ${style.disabled}`;
if (this.props.className) fieldClassName += ` ${this.props.className}`;
2015-10-05 10:12:16 +03:00
return (
2015-10-06 05:42:56 +03:00
<label
data-react-toolbox='checkbox'
2015-10-08 13:23:42 +03:00
className={fieldClassName}
2015-10-06 05:42:56 +03:00
onClick={this.handleClick}
>
2015-10-05 10:12:16 +03:00
<input
ref='input'
2015-10-05 10:12:16 +03:00
{...this.props}
className={style.input}
onClick={this.handleInputClick}
2015-11-07 04:59:50 +03:00
type='checkbox'
2015-10-05 10:12:16 +03:00
/>
2015-10-31 21:42:33 +03:00
<span data-role='checkbox' className={checkboxClassName} onMouseDown={this.handleMouseDown}>
<Ripple ref='ripple' data-role='ripple' className={style.ripple} spread={3} centered />
2015-10-05 10:12:16 +03:00
</span>
2015-10-31 21:42:33 +03:00
{ this.props.label ? <span data-role='label' className={style.text}>{this.props.label}</span> : null }
2015-10-05 10:12:16 +03:00
</label>
);
}
2015-10-05 10:12:16 +03:00
blur () {
this.refs.input.blur();
}
2015-10-05 10:12:16 +03:00
focus () {
this.refs.input.focus();
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Checkbox;