All files / react-dom/dist create-css-component.js

75% Statements 63/84
51.06% Branches 24/47
72.22% Functions 13/18
81.03% Lines 47/58
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    1x   4x   1x   1x   1x   1x   1x   1x   1x   1x   1x   3x   1x   1x   1x                     1x     1x 1x 1x 1x 1x 1x 1x   1x 1x       1x   1x   1x   1x   1x 1x                 1x     1x 1x         1x                                                                     1x   1x   1x 1x 1x 1x   1x 1x                 1x 1x       1x             1x          
'use strict';
 
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
 
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 _template = require('../../core/template');
 
var _bindAttrsToCssom = require('../../dom/dist/bind-attrs-to-cssom');
 
var _bindAttrsToCssom2 = _interopRequireDefault(_bindAttrsToCssom);
 
var _generateClassName = require('../../dom/dist/generate-class-name');
 
var _generateClassName2 = _interopRequireDefault(_generateClassName);
 
var _hot = require('../hot');
 
var _hot2 = _interopRequireDefault(_hot);
 
var _utils = require('./utils.js');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
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; }
 
/**
 * @param {string} displayName to be displayed in the React Dev Tools wrapped with Styled()
 * @param {string} className to be used for basic styles bound to the component's tag name
 * @param {Array<Attribute>} attributes selectors the component can be bound to
 * @param {Array<Attr>} attrs in properties that the component can be bound to
 * @param {string} [base] tag the component uses by default
 * @param {Object} invalidProps to avoid passing to the component's DOM element
 * @returns {Class<React.Component>}
 */
module.exports = function createCSSComponent(_ref) {
  var _class, _temp, _initialiseProps;
 
  var displayName = _ref.displayName,
      className = _ref.className,
      attributes = _ref.attributes,
      attrs = _ref.attrs,
      _ref$base = _ref.base,
      base = _ref$base === undefined ? 'div' : _ref$base,
      invalidProps = _ref.invalidProps;
 
  return _temp = _class = function (_Component) {
    _inherits(CSSComponent, _Component);
 
    /** Defined on the component so they can be redefined and be updated in instances later */
    function CSSComponent(props) {
      _classCallCheck(this, CSSComponent);
 
      var _this = _possibleConstructorReturn(this, (CSSComponent.__proto__ || Object.getPrototypeOf(CSSComponent)).call(this, props));
 
      _initialiseProps.call(_this);
 
      _this.init();
      /** Register the instance for hot updates */
      _hot2.default.register(_this);
      return _this;
    }
 
    /**
     * Defined outside the constructor as it can be reused when the component's static properties change.
     * Currently used for HMR
     */
 
 
    _createClass(CSSComponent, [{
      key: 'init',
      value: function init() {
        this.attrs = (0, _bindAttrsToCssom2.default)(this.constructor.attrs);
        this.generateClassName = (0, _generateClassName2.default)({
          className: this.constructor.className,
          attributes: this.constructor.attributes,
          attrs: this.attrs
        });
        this.applyAttrs(this.props);
      }
    }, {
      key: 'componentWillUpdate',
      value: function componentWillUpdate(nextProps) {
        this.applyAttrs(nextProps);
      }
    }, {
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        _hot2.default.unregister(this);
      }
 
      /** Updates the element's CSS properties using attr() */
 
 
      /**
       * @param {*} value of prop
       * @param {*} key of prop
       * @return {boolean}
       */
 
    }, {
      key: 'render',
      value: function render() {
        var props = this.props;
 
        return (0, _react.createElement)(this.constructor.base, _extends({
          ref: props.innerRef
        }, (0, _utils.omitBy)(props, this.shouldOmitProp), {
          className: this.generateClassName(this.props)
        }));
      }
    }]);
 
    return CSSComponent;
  }(_react.Component), _class.displayName = 'Styled(' + displayName + ')', _class.className = className, _class.attributes = attributes, _class.attrs = attrs, _class.base = base, _class.invalidProps = invalidProps, _initialiseProps = function _initialiseProps() {
    var _this2 = this;
 
    this.applyAttrs = function (props) {
      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;
 
      try {
        for (var _iterator = _this2.attrs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          var attr = _step.value;
 
          attr.cssRule.style[attr.prop] = (0, _template.format)(attr.template, props);
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          Iif (!_iteratorNormalCompletion && _iterator.return) {
            _iterator.return();
          }
        } finally {
          Iif (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }
    };
 
    this.shouldOmitProp = function (value, key) {
      return _this2.constructor.invalidProps[key] || key === 'innerRef';
    };
  }, _temp;
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjoz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