reactive-di-react
Version:
Reactive dependency injection - react bindings
107 lines (81 loc) • 4.25 kB
JavaScript
'use strict';
exports.__esModule = true;
exports.default = createReactWidget;
var _react = require('react');
var _reactDom = require('react-dom');
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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*:: import type {
FullWidget,
ComponentState,
GetComponentState
} from 'reactive-di-observable'*/
/*:: import type {
Element
} from 'react'*/
var ReactStateObserver = function () {
function ReactStateObserver /*:: <E, Props, State>*/(component /*: Component<void, Props, State>*/) {
_classCallCheck(this, ReactStateObserver);
this._component = component;
}
ReactStateObserver.prototype.next = function next(state /*: S*/) {
this._component.setState(state);
};
ReactStateObserver.prototype.error = function error(_error /*: E*/) {
throw _error;
};
ReactStateObserver.prototype.complete = function complete() {};
return ReactStateObserver;
}();
function createReactWidget /*:: <State, Props, El: Element<*>>*/(getState /*: GetComponentState<Props, State, El>*/, displayName /*: string*/) /*: mixed*/ {
var _class, _temp;
return _temp = _class = function (_Component) {
_inherits(WrappedWidget, _Component);
function WrappedWidget() {
_classCallCheck(this, WrappedWidget);
return _possibleConstructorReturn(this, _Component.apply(this, arguments));
}
WrappedWidget.prototype.componentWillMount = function componentWillMount() {
var _getState = getState();
var state = _getState.state;
var observable = _getState.observable;
var Widget = _getState.Widget;
this._observable = observable;
this._widget = new Widget(state);
this._widget.props = this.props;
this.setState(state);
};
WrappedWidget.prototype.componentWillReceiveProps = function componentWillReceiveProps(props /*: Props*/) {
this._widget.props = props;
};
WrappedWidget.prototype.componentDidMount = function componentDidMount() {
if (this._observable) {
this._subscription = this._observable.subscribe(new ReactStateObserver(this));
}
if (this._widget.onCreate) {
(this._widget /*: any*/).onCreate(this.props, this.state, (0, _reactDom.findDOMNode)(this));
}
};
WrappedWidget.prototype.componentDidUpdate = function componentDidUpdate() {
if (this._widget.onUpdate) {
(this._widget /*: any*/).onUpdate(this.props, this.state, (0, _reactDom.findDOMNode)(this));
}
};
WrappedWidget.prototype.componentWillUnmount = function componentWillUnmount() {
if (this._subscription) {
this._subscription.unsubscribe();
this._subscription = null;
}
if (this._widget.onRemove) {
(this._widget /*: any*/).onRemove(this.props, this.state, (0, _reactDom.findDOMNode)(this));
}
};
WrappedWidget.prototype.render = function render() {
this._widget.state = this.state;
return this._widget.render(this.props, this.state);
};
return WrappedWidget;
}(_react.Component), _class.displayName = displayName, _temp;
}
//# sourceMappingURL=createReactWidget.js.map