@uifabric/experiments
Version:
Experimental React components for building experiences for Office 365.
97 lines • 4.3 kB
JavaScript
define(["require", "exports", "tslib", "react"], function (require, exports, tslib_1, React) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* BaseComponent class, which provides basic helpers for all components.
*
* @public
*/
// TODO: rename, BaseComponentMin is ambiguous. BaseComponent? BaseComponentRef?
// TODO: will probably need lifecycle deprecation changes before promotion similar to BaseComponent
var BaseComponentMin = /** @class */ (function (_super) {
tslib_1.__extends(BaseComponentMin, _super);
/**
* BaseComponent constructor
* @param props - The props for the component.
* @param context - The context for the component.
*/
// tslint:disable-next-line:no-any
function BaseComponentMin(props, context) {
var _this = _super.call(this, props, context) || this;
_makeAllSafe(_this, BaseComponentMin.prototype, ['componentWillReceiveProps', 'componentDidMount', 'componentWillUnmount']);
return _this;
}
/**
* When the component will receive props, make sure the componentRef is updated.
*/
// tslint:disable-next-line:no-any
BaseComponentMin.prototype.componentWillReceiveProps = function (newProps, newContext) {
this._updateComponentRef(this.props, newProps);
};
/**
* When the component has mounted, update the componentRef.
*/
BaseComponentMin.prototype.componentDidMount = function () {
this._setComponentRef(this.props.componentRef, this);
};
/**
* If we have disposables, dispose them automatically on unmount.
*/
BaseComponentMin.prototype.componentWillUnmount = function () {
this._setComponentRef(this.props.componentRef, null);
};
/**
* Updates the componentRef (by calling it with "this" when necessary.)
*/
BaseComponentMin.prototype._updateComponentRef = function (currentProps, newProps) {
if (newProps === void 0) { newProps = {}; }
if (currentProps.componentRef !== newProps.componentRef) {
this._setComponentRef(currentProps.componentRef, null);
this._setComponentRef(newProps.componentRef, this);
}
};
BaseComponentMin.prototype._setComponentRef = function (ref, value) {
if (!this._skipComponentRefResolution && ref) {
if (typeof ref === 'function') {
ref(value);
}
if (typeof ref === 'object') {
// tslint:disable:no-any
ref.current = value;
}
}
};
return BaseComponentMin;
}(React.Component));
exports.BaseComponentMin = BaseComponentMin;
/**
* Helper to override a given method with a wrapper method that can try/catch the original, but also
* ensures that the BaseComponent's methods are called before the subclass's. This ensures that
* componentWillUnmount in the base is called and that things in the _disposables array are disposed.
*/
function _makeAllSafe(obj, prototype, methodNames) {
for (var i = 0, len = methodNames.length; i < len; i++) {
_makeSafe(obj, prototype, methodNames[i]);
}
}
function _makeSafe(obj, prototype, methodName) {
// tslint:disable:no-any
var classMethod = obj[methodName];
var prototypeMethod = prototype[methodName];
// tslint:enable:no-any
if (classMethod || prototypeMethod) {
// tslint:disable-next-line:no-any
obj[methodName] = function () {
var retVal;
if (prototypeMethod) {
retVal = prototypeMethod.apply(this, arguments);
}
if (classMethod !== prototypeMethod) {
retVal = classMethod.apply(this, arguments);
}
return retVal;
};
}
}
});
//# sourceMappingURL=BaseComponentMin.js.map