UNPKG

react-components-library

Version:
83 lines (68 loc) 2.43 kB
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 'use strict'; var React = require('react'); var classNames = require('classnames'); var Checkbox = React.createClass({ displayName: 'Checkbox', propTypes: { block: React.PropTypes.bool, checked: React.PropTypes.bool, disabled: React.PropTypes.bool, onInputChange: React.PropTypes.func }, getInitialState: function getInitialState() { return { active: false, checked: this.props.checked }; }, render: function render() { return React.createElement( 'label', { className: this.getClass() }, React.createElement('input', this.getInputProps()), React.createElement('div', { className: 'checkbox--box' }), React.createElement( 'span', { className: 'checkbox--label' }, this.props.children ) ); }, getInputProps: function getInputProps() { return { checked: this.props.checked !== undefined ? this.props.checked : this.state.checked, className: 'checkbox--input', disabled: this.props.disabled, type: 'checkbox', onBlur: this.handleInputBlur, onChange: this.handleInputOnChange, onFocus: this.handleInputFocus }; }, getClass: function getClass() { var classes = { 'checkbox': true, 'checkbox_active': this.state.active, 'checkbox_block': this.props.block, 'checkbox_checked': this.props.checked !== undefined ? this.props.checked : this.state.checked, 'checkbox_disabled': this.props.disabled }; return classNames(classes); }, handleInputFocus: function handleInputFocus() { this.setState({ active: true }); }, handleInputBlur: function handleInputBlur() { this.setState({ active: false }); }, handleInputOnChange: function handleInputOnChange(event) { this.setState({ checked: !this.state.checked }); if (this.props.onInputChange) { this.props.onInputChange(event); } } }); module.exports = Checkbox;