Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import classNames from 'classnames';
  3. import Icon from '../icons/icon';
  4.  
  5. const Checkbox = class extends Component {
  6.   constructor(props) {
  7.     super(props);
  8.     this.state = {
  9.       focused: false,
  10.     };
  11.     this.check = this.check.bind(this);
  12.     this.focus = this.focus.bind(this);
  13.     this.blur = this.blur.bind(this);
  14.   }
  15.  
  16.   check(event) {
  17.     event.stopPropagation();
  18.     this.props.onCheck(this.props.name);
  19.   }
  20.  
  21.   focus() {
  22.     this.setState({ focused: true });
  23.   }
  24.  
  25.   blur() {
  26.     this.setState({ focused: false });
  27.   }
  28.  
  29.   render() {
  30.     let label;
  31.     let check;
  32.     if (this.props.label) {
  33.       label = <span className="checkbox__label">{this.props.label}</span>;
  34.     }
  35.     if (this.props.checked) {
  36.       check = <Icon type="check" className="checkbox__check" />;
  37.     }
  38.     return (
  39.       <div
  40.         className={classNames('checkbox', { '-error': this.props.error })}
  41.         onClick={this.check}
  42.       >
  43.         <input className="checkbox__input" type="checkbox" onFocus={this.focus} onBlur={this.blur}></input>
  44.         <div className={classNames('checkbox__box', { '-focused': this.state.focused })}>
  45.           {check}
  46.         </div>
  47.         {label}
  48.       </div>
  49.     );
  50.   }
  51. };
  52.  
  53. Checkbox.propTypes = {
  54.   error: React.PropTypes.bool,
  55.   checked: React.PropTypes.bool,
  56.   label: React.PropTypes.string,
  57.   name: React.PropTypes.string,
  58.   onCheck: React.PropTypes.func,
  59. };
  60.  
  61. export default Checkbox;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement