@uifabric/utilities
Version:
Fluent UI React utilities for building components.
41 lines • 1.39 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var extendComponent_1 = require("./extendComponent");
/**
* Helper to manage componentRef resolution. Internally appends logic to
* lifetime methods to resolve componentRef to the passed in object.
*
* Usage: call initializeComponentRef(this) in the constructor,
*/
function initializeComponentRef(obj) {
extendComponent_1.extendComponent(obj, {
componentDidMount: _onMount,
componentDidUpdate: _onUpdate,
componentWillUnmount: _onUnmount,
});
}
exports.initializeComponentRef = initializeComponentRef;
function _onMount() {
_setComponentRef(this.props.componentRef, this);
}
function _onUpdate(prevProps) {
if (prevProps.componentRef !== this.props.componentRef) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_setComponentRef(prevProps.componentRef, null);
_setComponentRef(this.props.componentRef, this);
}
}
function _onUnmount() {
_setComponentRef(this.props.componentRef, null);
}
function _setComponentRef(componentRef, value) {
if (componentRef) {
if (typeof componentRef === 'object') {
componentRef.current = value;
}
else if (typeof componentRef === 'function') {
componentRef(value);
}
}
}
//# sourceMappingURL=initializeComponentRef.js.map
;