all files / lib/picker/ color.js

59.15% Statements 42/71
33.33% Branches 15/45
38.89% Functions 7/18
52.17% Lines 24/46
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193    1×       1×   6×   1×   1×   1×   1×   1×   2×   1×   1×   2×         1× 1×         1×                               1×                                                                       1×                   1×                           1×                                       1× 1×   1×                     1×                                                                         1×                
"use strict";
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _class, _temp;
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _react = require('react');
 
var _react2 = _interopRequireDefault(_react);
 
var _framework = require('../framework');
 
var _framework2 = _interopRequireDefault(_framework);
 
var _reactBindingComponent = require('react-binding-component');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
/**
 * @class UIColorItem
 */
var UIColorItem = function (_React$Component) {
  _inherits(UIColorItem, _React$Component);
 
  /**
   * @constructor
   */
  function UIColorItem(props) {
    _classCallCheck(this, UIColorItem);
 
    var _this = _possibleConstructorReturn(this, (UIColorItem.__proto__ || Object.getPrototypeOf(UIColorItem)).call(this, props));
 
    _this.state = {
      active: false
    };
    return _this;
  }
 
  /**
   * @event onMouseOver
   */
 
 
  _createClass(UIColorItem, [{
    key: 'onMouseOver',
    value: function onMouseOver() {
      this.setState({ active: true });
    }
 
    /**
     * @event onMouseLeave
     */
 
  }, {
    key: 'onMouseLeave',
    value: function onMouseLeave() {
      this.setState({ active: false });
    }
  }, {
    key: 'render',
    value: function render() {
      var containerStyl = Object.assign({}, UIColorItem.styles.container);
      var contentStyl = Object.assign({
        backgroundColor: this.props.color
      }, UIColorItem.styles.content);
      if (this.props.selected || this.state.active) {
        containerStyl = Object.assign(containerStyl, UIColorItem.styles.active);
      }
      return _react2.default.createElement(
        'li',
        { style: containerStyl,
          onClick: this.props.onClick,
          onMouseOver: this.onMouseOver.bind(this),
          onMouseLeave: this.onMouseLeave.bind(this) },
        _react2.default.createElement('div', { style: contentStyl })
      );
    }
  }]);
 
  return UIColorItem;
}(_react2.default.Component);
 
/**
 * @class ColorPicker
 * @extends UIFramework.Component
 * @extends UIFramework.Control
 */
 
 
UIColorItem.propTypes = {
  /**
   * @property {String} color
   */
  color: _react2.default.PropTypes.string,
  /**
   * @property {Boolean} selected
   */
  selected: _react2.default.PropTypes.bool,
  /**
   * @property {Function} onClick
   */
  onClick: _react2.default.PropTypes.func
};
UIColorItem.styles = {
  container: {
    display: 'inline-block',
    border: '1px solid #e6e6e6',
    borderRadius: '2px',
    marginRight: '5px',
    cursor: 'pointer',
    boxSizing: 'border-box',
    transition: 'all .2s ease-in-out'
  },
  content: {
    display: 'block',
    height: '20px',
    width: '20px',
    margin: '6px'
  },
  active: {
    backgroundColor: '#ebebeb'
  }
};
exports.default = _framework2.default.Component((_temp = _class = function (_UIFramework$Control) {
  _inherits(_class, _UIFramework$Control);
 
  function _class(props) {
    _classCallCheck(this, _class);
 
    var _this2 = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this, props));
 
    _this2.state = {
      colors: _this2.props.colors
    };
    return _this2;
  }
 
  _createClass(_class, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      if (this.props.bindStateCtx && !this.bindStateValue) {
        this.bindStateValue = this.state.colors[0];
      }
    }
  }, {
    key: 'onSelect',
    value: function onSelect(color) {
      this.onChange({
        target: {
          value: color
        }
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this3 = this;
 
      var selected = this.bindStateValue || this.state.colors[0];
      return _react2.default.createElement(
        'ul',
        { className: 'form-color-picker' },
        this.state.colors.map(function (color) {
          return _react2.default.createElement(UIColorItem, {
            key: color,
            color: color,
            selected: selected === color,
            onClick: _this3.onSelect.bind(_this3, color)
          });
        })
      );
    }
  }]);
 
  return _class;
}(_framework2.default.Control), _class.label = 'color-picker', _class.flexbox = true, _class.propTypes = {
  /**
   * @property {Array} colors - the colors
   */
  colors: _react2.default.PropTypes.arrayOf(_react2.default.PropTypes.string)
}, _class.defaultProps = {
  colors: ['#ff8ac2', '#6ed4a4', '#00e4ff', '#283547', '#f0ab51']
}, _temp));