/* global React */ import { addons } from 'react/addons'; import Ripple from '../ripple'; import style from './style'; import events from '../utils/events'; export default React.createClass({ mixins: [addons.PureRenderMixin], displayName: 'Checkbox', propTypes: { checked: React.PropTypes.bool, className: React.PropTypes.string, disabled: React.PropTypes.bool, label: React.PropTypes.string, name: React.PropTypes.string, onBlur: React.PropTypes.func, onChange: React.PropTypes.func, onFocus: React.PropTypes.func }, getDefaultProps () { return { checked: false, className: '', disabled: false }; }, getInitialState () { return { checked: this.props.checked }; }, handleChange (event) { this.setState({checked: !this.state.checked}, () => { if (this.props.onChange) this.props.onChange(event, this); }); }, handleClick (event) { events.pauseEvent(event); if (!this.props.disabled) this.handleChange(event); }, handleMouseDown (event) { if (!this.props.disabled) this.refs.ripple.start(event); }, render () { let labelClassName = style[this.props.disabled ? 'disabled' : 'field']; let checkboxClassName = style[this.state.checked ? 'checked' : 'check']; if (this.props.className) labelClassName += ` ${this.props.className}`; return ( ); }, blur () { this.refs.input.getDOMNode().blur(); }, focus () { this.refs.input.getDOMNode().focus(); }, getValue () { return this.state.checked; }, setValue (value) { this.setState({checked: value}); } });