react-rebind
Version:
Declarative data binding to React components
136 lines (119 loc) • 4.97 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['module', 'exports', 'react', 'prop-types', 'object-path'], factory);
} else if (typeof exports !== "undefined") {
factory(module, exports, require('react'), require('prop-types'), require('object-path'));
} else {
var mod = {
exports: {}
};
factory(mod, mod.exports, global.react, global.propTypes, global.objectPath);
global.ScopedComponent = mod.exports;
}
})(this, function (module, exports, _react, _propTypes, _objectPath) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _propTypes2 = _interopRequireDefault(_propTypes);
var _objectPath2 = _interopRequireDefault(_objectPath);
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");
}
}
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;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
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) {
if (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
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}
var ScopedComponent = function (_Component) {
_inherits(ScopedComponent, _Component);
function ScopedComponent() {
_classCallCheck(this, ScopedComponent);
return _possibleConstructorReturn(this, (ScopedComponent.__proto__ || Object.getPrototypeOf(ScopedComponent)).apply(this, arguments));
}
_createClass(ScopedComponent, [{
key: 'getRebindContext',
value: function getRebindContext(markPath) {
var marks = markPath === null ? [] : markPath.split('.');
return marks.reduce(function (current, name) {
return current.marks[name];
}, this.context.rebindContext);
}
}, {
key: 'parsePath',
value: function parsePath(path) {
var pathString = path.toString();
var sepIdx = pathString.indexOf(':');
if (sepIdx > -1) {
return {
scope: pathString.slice(sepIdx + 1),
mark: pathString.slice(0, sepIdx)
};
} else {
return {
scope: pathString,
mark: null
};
}
}
}, {
key: 'getScope',
value: function getScope(path) {
var parsedPath = this.parsePath(path);
var rebindContext = this.getRebindContext(parsedPath.mark);
var result = _objectPath2.default.get(rebindContext.scope, parsedPath.scope);
var filtered = this.props.filter ? this.props.filter(result) : result;
return filtered;
}
}, {
key: 'scope',
get: function get() {
return this.getScope(this.props.scope);
}
}]);
return ScopedComponent;
}(_react.Component);
ScopedComponent.contextTypes = {
rebindContext: _propTypes2.default.any
};
exports.default = ScopedComponent;
module.exports = exports['default'];
});