all files / lib/button/ index.js

54.31% Statements 63/116
41.79% Branches 28/67
48% Functions 12/25
52.63% Lines 40/76
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238    1×       4×   1×   1×   1×   1×   1×   2×   1×   2×   2×   1×         1× 1×   1× 2×   2×   2×         2×               1×                                                         1×                                                                                                                   1×       1×         2× 2×     2×     2× 2× 2×               2×     2×     2× 2× 2×   2×   2× 2×         2× 2×               1×                                                                                          
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
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 _class, _temp;
 
var _react = require('react');
 
var _react2 = _interopRequireDefault(_react);
 
var _framework = require('../framework');
 
var _framework2 = _interopRequireDefault(_framework);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
function _possibleConstructorReturn(self, call) { Iif (!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 Button
 */
exports.default = _framework2.default.Component((_temp = _class = function (_React$Component) {
  _inherits(_class, _React$Component);
 
  function _class(props) {
    _classCallCheck(this, _class);
 
    var _this = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this, props));
 
    _this.state = {
      disabled: false,
      hovered: false,
      text: props.children || props.text
    };
    return _this;
  }
 
  /**
   * @event onMouseOver
   */
 
 
  _createClass(_class, [{
    key: 'onMouseOver',
    value: function onMouseOver() {
      this.setState({
        hovered: true
      });
    }
 
    /**
     * @event onMouseLeave
     */
 
  }, {
    key: 'onMouseLeave',
    value: function onMouseLeave() {
      this.setState({
        hovered: false
      });
    }
 
    /**
     * Click this button has different behaviors
     *
     * @event onClick
     */
 
  }, {
    key: 'onClick',
    value: function () {
      var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(event) {
        var _this2 = this;
 
        var isValid;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                this.setState({
                  disabled: true
                });
 
                isValid = void 0;
 
                if (!(typeof this.props.onClick === 'function')) {
                  _context.next = 6;
                  break;
                }
 
                _context.next = 5;
                return this.props.onClick(event);
 
              case 5:
                isValid = _context.sent;
 
              case 6:
                if ((isValid === undefined || isValid) && this.props.interval !== undefined) {
                  (function () {
                    var count = _this2.props.interval;
                    var interval = setInterval(function () {
                      count -= 1;
                      if (count >= 0) {
                        _this2.setState({
                          text: _this2.props.intervalFormat(count)
                        });
                      } else {
                        clearInterval(interval);
                        _this2.setState({
                          text: _this2.props.text,
                          disabled: false
                        });
                      }
                    }, 1000);
                  })();
                } else {
                  this.setState({
                    disabled: false
                  });
                }
 
              case 7:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));
 
      function onClick(_x) {
        return _ref.apply(this, arguments);
      }
 
      return onClick;
    }()
  }, {
    key: 'render',
    value: function render() {
      var disabled = void 0;
      Iif (this.state.disabled === true) {
        disabled = true;
      } else {
        disabled = this.props.disabled;
      }
 
      var srcProps = {};
      var style = Object.assign({}, this.props.style, this.props.styles.button);
      Iif (this.props.level) {
        var level = this.props.level;
        if (level && this.props.config.colors[level]) {
          style.backgroundColor = style.borderColor = this.props.config.colors[level];
          style.color = '#fff';
        }
      }
 
      Iif (disabled) {
        style = Object.assign(style, this.props.styles.disabled);
      } else {
        Iif (this.state.hovered) {
          style = Object.assign(style, this.props.styles.hovered);
        }
        srcProps.onMouseOver = this.onMouseOver.bind(this);
        srcProps.onMouseLeave = this.onMouseLeave.bind(this);
        srcProps.onClick = this.onClick.bind(this);
      }
      Iif (this.props.block) {
        srcProps.flex = 1.0;
      } else Eif (!this.props.flex) {
        style = Object.assign(style, {
          width: 'auto'
        });
      }
 
      srcProps.style = style;
      return _react2.default.createElement(
        'button',
        srcProps,
        this.state.text
      );
    }
  }]);
 
  return _class;
}(_react2.default.Component), _class.label = 'button', _class.flexbox = true, _class.propTypes = {
  text: _react2.default.PropTypes.string,
  level: _react2.default.PropTypes.oneOf(['primary', 'warning', 'error', 'success']),
  full: _react2.default.PropTypes.bool,
  disabled: _react2.default.PropTypes.bool,
  interval: _react2.default.PropTypes.number,
  intervalFormat: _react2.default.PropTypes.func
}, _class.defaultProps = {
  full: false,
  disabled: false,
  intervalFormat: function intervalFormat(count) {
    return 'waiting...(' + count + ')';
  }
}, _class.styles = {
  button: {
    display: 'inline-block',
    height: '30px',
    border: '1px solid #9b9b9b',
    borderRadius: '4px',
    padding: '0 10px',
    color: '#7e7e7e',
    backgroundColor: '#fff',
    cursor: 'pointer',
    transition: 'all .3s ease-in-out',
    boxSizing: 'border-box',
    outline: 'none'
  },
  disabled: {
    borderColor: '#eeeeee',
    color: '#eeeeee',
    cursor: 'no-drop'
  },
  levels: {
    primary: {
      color: '#ffffff',
      borderColor: '#70CCA1',
      backgroundColor: '#70CCA1'
    }
  },
  hovered: {
    color: '#ffffff',
    borderColor: '#70CCA1',
    backgroundColor: '#70CCA1'
  }
}, _temp));